3 struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen)
6 struct aim_tlvlist_t *list;
7 struct aim_tlvlist_t *cur;
21 type = aimutil_get16(buf+pos);
26 length = aimutil_get16(buf+pos);
29 if ((pos+length) <= maxlen)
31 cur = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
32 memset(cur, 0x00, sizeof(struct aim_tlvlist_t));
34 cur->tlv = aim_createtlv();
35 cur->tlv->type = type;
36 cur->tlv->length = length;
37 cur->tlv->value = (u_char *)malloc(length*sizeof(u_char));
38 memcpy(cur->tlv->value, buf+pos, length);
51 void aim_freetlvchain(struct aim_tlvlist_t **list)
53 struct aim_tlvlist_t *cur, *cur2;
55 if (!list || !(*list))
61 aim_freetlv(&cur->tlv);
71 * Grab the Nth TLV of type type in the TLV list list.
73 struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *list, u_short type, int nth)
76 struct aim_tlvlist_t *cur;
79 for (cur = list; cur != NULL; cur = cur->next)
83 if (cur->tlv->type == type)
92 char *aim_gettlv_str(struct aim_tlvlist_t *list, u_short type, int nth)
94 struct aim_tlv_t *tlv;
97 if (!(tlv = aim_gettlv(list, type, nth)))
100 newstr = (char *) malloc(tlv->length + 1);
101 memcpy(newstr, tlv->value, tlv->length);
102 *(newstr + tlv->length) = '\0';
107 struct aim_tlv_t *aim_grabtlv(u_char *src)
109 struct aim_tlv_t *dest = NULL;
111 dest = aim_createtlv();
113 dest->type = src[0] << 8;
114 dest->type += src[1];
116 dest->length = src[2] << 8;
117 dest->length += src[3];
119 dest->value = (u_char *) malloc(dest->length*sizeof(u_char));
120 memset(dest->value, 0, dest->length*sizeof(u_char));
122 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
127 struct aim_tlv_t *aim_grabtlvstr(u_char *src)
129 struct aim_tlv_t *dest = NULL;
131 dest = aim_createtlv();
133 dest->type = src[0] << 8;
134 dest->type += src[1];
136 dest->length = src[2] << 8;
137 dest->length += src[3];
139 dest->value = (u_char *) malloc((dest->length+1)*sizeof(u_char));
140 memset(dest->value, 0, (dest->length+1)*sizeof(u_char));
142 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
143 dest->value[dest->length] = '\0';
148 int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv)
152 dest[i++] = newtlv->type >> 8;
153 dest[i++] = newtlv->type & 0x00FF;
154 dest[i++] = newtlv->length >> 8;
155 dest[i++] = newtlv->length & 0x00FF;
156 memcpy(&(dest[i]), newtlv->value, newtlv->length);
161 struct aim_tlv_t *aim_createtlv(void)
163 struct aim_tlv_t *newtlv = NULL;
164 newtlv = (struct aim_tlv_t *)malloc(sizeof(struct aim_tlv_t));
165 memset(newtlv, 0, sizeof(struct aim_tlv_t));
169 int aim_freetlv(struct aim_tlv_t **oldtlv)
175 if ((*oldtlv)->value)
176 free((*oldtlv)->value);
183 int aim_puttlv_16(u_char *buf, u_short t, u_short v)
186 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
187 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0002);
188 curbyte += aimutil_put16(buf+curbyte, (u_short)(v&0xffff));
192 int aim_puttlv_32(u_char *buf, u_short t, u_long v)
195 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
196 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0004);
197 curbyte += aimutil_put32(buf+curbyte, (u_long)(v&0xffffffff));
201 int aim_puttlv_str(u_char *buf, u_short t, u_short l, u_char *v)
206 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
207 curbyte += aimutil_put16(buf+curbyte, (u_short)(l&0xffff));
209 memcpy(buf+curbyte, v, l);