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);
70 int aim_counttlvchain(struct aim_tlvlist_t **list)
72 struct aim_tlvlist_t *cur;
75 if (!list || !(*list))
78 for (cur = *list; cur; cur = cur->next)
84 int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str, int len)
86 struct aim_tlvlist_t *new;
87 struct aim_tlvlist_t *cur;
92 new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
93 memset(new, 0x00, sizeof(struct aim_tlvlist_t));
95 new->tlv = aim_createtlv();
96 new->tlv->type = type;
97 new->tlv->length = len;
98 new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
99 memcpy(new->tlv->value, str, new->tlv->length);
105 } else if ((*list)->next == NULL) {
108 for(cur = *list; cur->next; cur = cur->next)
112 return new->tlv->length;
115 int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val)
117 struct aim_tlvlist_t *new;
118 struct aim_tlvlist_t *cur;
123 new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
124 memset(new, 0x00, sizeof(struct aim_tlvlist_t));
126 new->tlv = aim_createtlv();
127 new->tlv->type = type;
128 new->tlv->length = 2;
129 new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
130 aimutil_put16(new->tlv->value, val);
136 } else if ((*list)->next == NULL) {
139 for(cur = *list; cur->next; cur = cur->next)
146 int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val)
148 struct aim_tlvlist_t *new;
149 struct aim_tlvlist_t *cur;
154 new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
155 memset(new, 0x00, sizeof(struct aim_tlvlist_t));
157 new->tlv = aim_createtlv();
158 new->tlv->type = type;
159 new->tlv->length = 4;
160 new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
161 aimutil_put32(new->tlv->value, val);
167 } else if ((*list)->next == NULL) {
170 for(cur = *list; cur->next; cur = cur->next)
177 int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list)
181 struct aim_tlvlist_t *cur;
183 if (!list || !buf || !buflen)
186 /* do an initial run to test total length */
187 for (cur = *list; cur; cur = cur->next) {
188 goodbuflen += 2 + 2; /* type + len */
189 goodbuflen += cur->tlv->length;
192 if (goodbuflen > buflen)
193 return 0; /* not enough buffer */
195 /* do the real write-out */
196 for (cur = *list; cur; cur = cur->next) {
197 i += aimutil_put16(buf+i, cur->tlv->type);
198 i += aimutil_put16(buf+i, cur->tlv->length);
199 memcpy(buf+i, cur->tlv->value, cur->tlv->length);
200 i += cur->tlv->length;
208 * Grab the Nth TLV of type type in the TLV list list.
210 struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *list, u_short type, int nth)
213 struct aim_tlvlist_t *cur;
216 for (cur = list; cur != NULL; cur = cur->next)
220 if (cur->tlv->type == type)
229 char *aim_gettlv_str(struct aim_tlvlist_t *list, u_short type, int nth)
231 struct aim_tlv_t *tlv;
234 if (!(tlv = aim_gettlv(list, type, nth)))
237 newstr = (char *) malloc(tlv->length + 1);
238 memcpy(newstr, tlv->value, tlv->length);
239 *(newstr + tlv->length) = '\0';
244 struct aim_tlv_t *aim_grabtlv(u_char *src)
246 struct aim_tlv_t *dest = NULL;
248 dest = aim_createtlv();
250 dest->type = src[0] << 8;
251 dest->type += src[1];
253 dest->length = src[2] << 8;
254 dest->length += src[3];
256 dest->value = (u_char *) malloc(dest->length*sizeof(u_char));
257 memset(dest->value, 0, dest->length*sizeof(u_char));
259 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
264 struct aim_tlv_t *aim_grabtlvstr(u_char *src)
266 struct aim_tlv_t *dest = NULL;
268 dest = aim_createtlv();
270 dest->type = src[0] << 8;
271 dest->type += src[1];
273 dest->length = src[2] << 8;
274 dest->length += src[3];
276 dest->value = (u_char *) malloc((dest->length+1)*sizeof(u_char));
277 memset(dest->value, 0, (dest->length+1)*sizeof(u_char));
279 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
280 dest->value[dest->length] = '\0';
285 int aim_puttlv (u_char *dest, struct aim_tlv_t *newtlv)
289 dest[i++] = newtlv->type >> 8;
290 dest[i++] = newtlv->type & 0x00FF;
291 dest[i++] = newtlv->length >> 8;
292 dest[i++] = newtlv->length & 0x00FF;
293 memcpy(&(dest[i]), newtlv->value, newtlv->length);
298 struct aim_tlv_t *aim_createtlv(void)
300 struct aim_tlv_t *newtlv = NULL;
301 newtlv = (struct aim_tlv_t *)malloc(sizeof(struct aim_tlv_t));
302 memset(newtlv, 0, sizeof(struct aim_tlv_t));
306 int aim_freetlv(struct aim_tlv_t **oldtlv)
312 if ((*oldtlv)->value)
313 free((*oldtlv)->value);
320 int aim_puttlv_16(u_char *buf, u_short t, u_short v)
323 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
324 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0002);
325 curbyte += aimutil_put16(buf+curbyte, (u_short)(v&0xffff));
329 int aim_puttlv_32(u_char *buf, u_short t, u_long v)
332 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
333 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0004);
334 curbyte += aimutil_put32(buf+curbyte, (u_long)(v&0xffffffff));
338 int aim_puttlv_str(u_char *buf, u_short t, u_short l, u_char *v)
343 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
344 curbyte += aimutil_put16(buf+curbyte, (u_short)(l&0xffff));
346 memcpy(buf+curbyte, v, l);