6 * aim_readtlvchain - Read a TLV chain from a buffer.
8 * @maxlen: Length of input buffer
10 * Reads and parses a series of TLV patterns from a data buffer; the
11 * returned structure is manipulatable with the rest of the TLV
12 * routines. When done with a TLV chain, aim_freetlvchain() should
13 * be called to free the dynamic substructures.
16 faim_export struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen)
19 struct aim_tlvlist_t *list;
20 struct aim_tlvlist_t *cur;
34 type = aimutil_get16(buf+pos);
39 length = aimutil_get16(buf+pos);
42 if ((pos+length) <= maxlen)
45 * Okay, so now AOL has decided that any TLV of
46 * type 0x0013 can only be two bytes, despite
47 * what the actual given length is. So here
48 * we dump any invalid TLVs of that sort. Hopefully
49 * theres no special cases to this special case.
52 if ((type == 0x0013) && (length != 0x0002))
55 cur = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
56 memset(cur, 0x00, sizeof(struct aim_tlvlist_t));
58 cur->tlv = aim_createtlv();
59 cur->tlv->type = type;
60 cur->tlv->length = length;
62 cur->tlv->value = (unsigned char *)malloc(length);
63 memcpy(cur->tlv->value, buf+pos, length);
78 * aim_freetlvchain - Free a TLV chain structure
79 * @list: Chain to be freed
81 * Walks the list of TLVs in the passed TLV chain and
82 * frees each one. Note that any references to this data
83 * should be removed before calling this.
86 faim_export void aim_freetlvchain(struct aim_tlvlist_t **list)
88 struct aim_tlvlist_t *cur, *cur2;
90 if (!list || !(*list))
96 aim_freetlv(&cur->tlv);
106 * aim_counttlvchain - Count the number of TLVs in a chain
107 * @list: Chain to be counted
109 * Returns the number of TLVs stored in the passed chain.
112 faim_export int aim_counttlvchain(struct aim_tlvlist_t **list)
114 struct aim_tlvlist_t *cur;
117 if (!list || !(*list))
120 for (cur = *list; cur; cur = cur->next)
127 * aim_sizetlvchain - Count the number of bytes in a TLV chain
128 * @list: Chain to be sized
130 * Returns the number of bytes that would be needed to
131 * write the passed TLV chain to a data buffer.
134 faim_export int aim_sizetlvchain(struct aim_tlvlist_t **list)
136 struct aim_tlvlist_t *cur;
139 if (!list || !(*list))
142 for (cur = *list; cur; cur = cur->next)
143 size += (4 + cur->tlv->length);
149 * aim_addtlvtochain_str - Add a string to a TLV chain
150 * @list: Desination chain (%NULL pointer if empty)
152 * @str: String to add
153 * @len: Length of string to add (not including %NULL)
155 * Adds the passed string as a TLV element of the passed type
159 faim_export int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str, int len)
161 struct aim_tlvlist_t *newtlv;
162 struct aim_tlvlist_t *cur;
167 newtlv = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
168 memset(newtlv, 0x00, sizeof(struct aim_tlvlist_t));
170 newtlv->tlv = aim_createtlv();
171 newtlv->tlv->type = type;
172 newtlv->tlv->length = len;
173 newtlv->tlv->value = (unsigned char *)malloc(newtlv->tlv->length*sizeof(unsigned char));
174 memcpy(newtlv->tlv->value, str, newtlv->tlv->length);
180 } else if ((*list)->next == NULL) {
181 (*list)->next = newtlv;
183 for(cur = *list; cur->next; cur = cur->next)
187 return newtlv->tlv->length;
191 * aim_addtlvtochain16 - Add a 16bit integer to a TLV chain
192 * @list: Destination chain
193 * @type: TLV type to add
196 * Adds a two-byte unsigned integer to a TLV chain.
199 faim_export int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val)
201 struct aim_tlvlist_t *newtl;
202 struct aim_tlvlist_t *cur;
207 newtl = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
208 memset(newtl, 0x00, sizeof(struct aim_tlvlist_t));
210 newtl->tlv = aim_createtlv();
211 newtl->tlv->type = type;
212 newtl->tlv->length = 2;
213 newtl->tlv->value = (unsigned char *)malloc(newtl->tlv->length*sizeof(unsigned char));
214 aimutil_put16(newtl->tlv->value, val);
220 } else if ((*list)->next == NULL) {
221 (*list)->next = newtl;
223 for(cur = *list; cur->next; cur = cur->next)
231 * aim_addtlvtochain32 - Add a 32bit integer to a TLV chain
232 * @list: Destination chain
233 * @type: TLV type to add
236 * Adds a four-byte unsigned integer to a TLV chain.
239 faim_export int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val)
241 struct aim_tlvlist_t *newtl;
242 struct aim_tlvlist_t *cur;
247 newtl = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
248 memset(newtl, 0x00, sizeof(struct aim_tlvlist_t));
250 newtl->tlv = aim_createtlv();
251 newtl->tlv->type = type;
252 newtl->tlv->length = 4;
253 newtl->tlv->value = (unsigned char *)malloc(newtl->tlv->length*sizeof(unsigned char));
254 aimutil_put32(newtl->tlv->value, val);
260 } else if ((*list)->next == NULL) {
261 (*list)->next = newtl;
263 for(cur = *list; cur->next; cur = cur->next)
271 * aim_addtlvtochain_caps - Add a capability block to a TLV chain
272 * @list: Destination chain
273 * @type: TLV type to add
274 * @caps: Bitfield of capability flags to send
276 * Adds a block of capability blocks to a TLV chain. The bitfield
277 * passed in should be a bitwise %OR of any of the %AIM_CAPS constants:
279 * %AIM_CAPS_BUDDYICON Supports Buddy Icons
281 * %AIM_CAPS_VOICE Supports Voice Chat
283 * %AIM_CAPS_IMIMAGE Supports DirectIM/IMImage
285 * %AIM_CAPS_CHAT Supports Chat
287 * %AIM_CAPS_GETFILE Supports Get File functions
289 * %AIM_CAPS_SENDFILE Supports Send File functions
292 faim_export int aim_addtlvtochain_caps(struct aim_tlvlist_t **list, unsigned short type, unsigned short caps)
294 unsigned char buf[128]; /* icky fixed length buffer */
295 struct aim_tlvlist_t *newtl;
296 struct aim_tlvlist_t *cur;
301 newtl = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
302 memset(newtl, 0x00, sizeof(struct aim_tlvlist_t));
304 newtl->tlv = aim_createtlv();
305 newtl->tlv->type = type;
307 newtl->tlv->length = aim_putcap(buf, sizeof(buf), caps);
308 newtl->tlv->value = (unsigned char *)calloc(1, newtl->tlv->length);
309 memcpy(newtl->tlv->value, buf, newtl->tlv->length);
315 } else if ((*list)->next == NULL) {
316 (*list)->next = newtl;
318 for(cur = *list; cur->next; cur = cur->next)
322 return newtl->tlv->length;
326 * aim_addtlvtochain_noval - Add a blank TLV to a TLV chain
327 * @list: Destination chain
328 * @type: TLV type to add
330 * Adds a TLV with a zero length to a TLV chain.
333 faim_internal int aim_addtlvtochain_noval(struct aim_tlvlist_t **list, unsigned short type)
335 struct aim_tlvlist_t *newtlv;
336 struct aim_tlvlist_t *cur;
338 newtlv = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
339 memset(newtlv, 0x00, sizeof(struct aim_tlvlist_t));
341 newtlv->tlv = aim_createtlv();
342 newtlv->tlv->type = type;
343 newtlv->tlv->length = 0;
344 newtlv->tlv->value = NULL;
350 } else if ((*list)->next == NULL) {
351 (*list)->next = newtlv;
353 for(cur = *list; cur->next; cur = cur->next)
357 return newtlv->tlv->length;
361 * aim_writetlvchain - Write a TLV chain into a data buffer.
362 * @buf: Destination buffer
363 * @buflen: Maximum number of bytes that will be written to buffer
364 * @list: Source TLV chain
366 * Copies a TLV chain into a raw data buffer, writing only the number
367 * of bytes specified. This operation does not free the chain;
368 * aim_freetlvchain() must still be called to free up the memory used
369 * by the chain structures.
372 faim_export int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list)
376 struct aim_tlvlist_t *cur;
378 if (!list || !buf || !buflen)
381 /* do an initial run to test total length */
382 for (cur = *list; cur; cur = cur->next) {
383 goodbuflen += 2 + 2; /* type + len */
384 goodbuflen += cur->tlv->length;
387 if (goodbuflen > buflen)
388 return 0; /* not enough buffer */
390 /* do the real write-out */
391 for (cur = *list; cur; cur = cur->next) {
392 i += aimutil_put16(buf+i, cur->tlv->type);
393 i += aimutil_put16(buf+i, cur->tlv->length);
394 memcpy(buf+i, cur->tlv->value, cur->tlv->length);
395 i += cur->tlv->length;
403 * aim_gettlv - Grab the Nth TLV of type type in the TLV list list.
404 * @list: Source chain
405 * @type: Requested TLV type
406 * @nth: Index of TLV of type to get
408 * Returns a pointer to an aim_tlv_t of the specified type;
409 * %NULL on error. The @nth parameter is specified starting at %1.
410 * In most cases, there will be no more than one TLV of any type
414 faim_export struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *list, u_short type, int nth)
417 struct aim_tlvlist_t *cur;
420 for (cur = list; cur != NULL; cur = cur->next)
424 if (cur->tlv->type == type)
434 * aim_gettlv_str - Retrieve the Nth TLV in chain as a string.
435 * @list: Source TLV chain
436 * @type: TLV type to search for
437 * @nth: Index of TLV to return
439 * Same as aim_gettlv(), except that the return value is a %NULL-
440 * terminated string instead of an aim_tlv_t. This is a
441 * dynamic buffer and must be freed by the caller.
444 faim_export char *aim_gettlv_str(struct aim_tlvlist_t *list, u_short type, int nth)
446 struct aim_tlv_t *tlv;
449 if (!(tlv = aim_gettlv(list, type, nth)))
452 newstr = (char *) malloc(tlv->length + 1);
453 memcpy(newstr, tlv->value, tlv->length);
454 *(newstr + tlv->length) = '\0';
460 * aim_gettlv8 - Retrieve the Nth TLV in chain as a 8bit integer.
461 * @list: Source TLV chain
462 * @type: TLV type to search for
463 * @nth: Index of TLV to return
465 * Same as aim_gettlv(), except that the return value is a
466 * 8bit integer instead of an aim_tlv_t.
469 faim_internal unsigned char aim_gettlv8(struct aim_tlvlist_t *list, unsigned short type, int num)
471 struct aim_tlv_t *tlv;
473 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value)
475 return aimutil_get8(tlv->value);
479 * aim_gettlv16 - Retrieve the Nth TLV in chain as a 16bit integer.
480 * @list: Source TLV chain
481 * @type: TLV type to search for
482 * @nth: Index of TLV to return
484 * Same as aim_gettlv(), except that the return value is a
485 * 16bit integer instead of an aim_tlv_t.
488 faim_internal unsigned short aim_gettlv16(struct aim_tlvlist_t *list, unsigned short type, int num)
490 struct aim_tlv_t *tlv;
492 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value)
494 return aimutil_get16(tlv->value);
498 * aim_gettlv32 - Retrieve the Nth TLV in chain as a 32bit integer.
499 * @list: Source TLV chain
500 * @type: TLV type to search for
501 * @nth: Index of TLV to return
503 * Same as aim_gettlv(), except that the return value is a
504 * 32bit integer instead of an aim_tlv_t.
507 faim_internal unsigned long aim_gettlv32(struct aim_tlvlist_t *list, unsigned short type, int num)
509 struct aim_tlv_t *tlv;
511 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value)
513 return aimutil_get32(tlv->value);
517 * aim_grabtlv - Grab a single TLV from a data buffer
518 * @src: Source data buffer (must be at least 4 bytes long)
520 * Creates a TLV structure aim_tlv_t and returns it
521 * filled with values from a buffer, possibly including a
522 * dynamically allocated buffer for the value portion.
524 * Both the aim_tlv_t and the tlv->value pointer
525 * must be freed by the caller if non-%NULL.
528 faim_export struct aim_tlv_t *aim_grabtlv(u_char *src)
530 struct aim_tlv_t *dest = NULL;
532 dest = aim_createtlv();
534 dest->type = src[0] << 8;
535 dest->type += src[1];
537 dest->length = src[2] << 8;
538 dest->length += src[3];
540 dest->value = (u_char *) malloc(dest->length*sizeof(u_char));
541 memset(dest->value, 0, dest->length*sizeof(u_char));
543 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
549 * aim_grabtlvstr - Grab a single TLV from a data buffer as string
550 * @src: Source data buffer (must be at least 4 bytes long)
552 * Creates a TLV structure aim_tlv_t and returns it
553 * filled with values from a buffer, possibly including a
554 * dynamically allocated buffer for the value portion, which
555 * is %NULL-terminated as a string.
557 * Both the aim_tlv_t and the tlv->value pointer
558 * must be freed by the caller if non-%NULL.
561 faim_export struct aim_tlv_t *aim_grabtlvstr(u_char *src)
563 struct aim_tlv_t *dest = NULL;
565 dest = aim_createtlv();
567 dest->type = src[0] << 8;
568 dest->type += src[1];
570 dest->length = src[2] << 8;
571 dest->length += src[3];
573 dest->value = (u_char *) malloc((dest->length+1)*sizeof(u_char));
574 memset(dest->value, 0, (dest->length+1)*sizeof(u_char));
576 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char));
577 dest->value[dest->length] = '\0';
583 * aim_puttlv - Write a aim_tlv_t into a data buffer
584 * @dest: Destination data buffer
585 * @newtlv: Source TLV structure
587 * Writes out the passed TLV structure into the buffer. No bounds
588 * checking is done on the output buffer.
590 * The passed aim_tlv_t is not freed. aim_freetlv() should
591 * still be called by the caller to free the structure.
594 faim_export int aim_puttlv(u_char *dest, struct aim_tlv_t *newtlv)
598 dest[i++] = newtlv->type >> 8;
599 dest[i++] = newtlv->type & 0x00FF;
600 dest[i++] = newtlv->length >> 8;
601 dest[i++] = newtlv->length & 0x00FF;
602 memcpy(&(dest[i]), newtlv->value, newtlv->length);
608 * aim_createtlv - Generate an aim_tlv_t structure.
610 * Allocates an empty TLV structure and returns a pointer
611 * to it; %NULL on error.
614 faim_export struct aim_tlv_t *aim_createtlv(void)
616 struct aim_tlv_t *newtlv;
618 if (!(newtlv = (struct aim_tlv_t *)malloc(sizeof(struct aim_tlv_t))))
620 memset(newtlv, 0, sizeof(struct aim_tlv_t));
625 * aim_freetlv - Free a aim_tlv_t structure
626 * @oldtlv: TLV to be destroyed
628 * Frees both the TLV structure and the value portion.
631 faim_export int aim_freetlv(struct aim_tlv_t **oldtlv)
637 if ((*oldtlv)->value)
638 free((*oldtlv)->value);
646 * aim_puttlv_8 - Write a one-byte TLV.
647 * @buf: Destination buffer
651 * Writes a TLV with a one-byte integer value portion.
654 faim_export int aim_puttlv_8(unsigned char *buf, unsigned short t, unsigned char v)
658 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(t&0xffff));
659 curbyte += aimutil_put16(buf+curbyte, (unsigned short)0x0001);
660 curbyte += aimutil_put8(buf+curbyte, (unsigned char)(v&0xff));
666 * aim_puttlv_16 - Write a two-byte TLV.
667 * @buf: Destination buffer
671 * Writes a TLV with a two-byte integer value portion.
674 faim_export int aim_puttlv_16(u_char *buf, u_short t, u_short v)
677 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
678 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0002);
679 curbyte += aimutil_put16(buf+curbyte, (u_short)(v&0xffff));
684 * aim_puttlv_32 - Write a four-byte TLV.
685 * @buf: Destination buffer
689 * Writes a TLV with a four-byte integer value portion.
692 faim_export int aim_puttlv_32(u_char *buf, u_short t, u_long v)
695 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
696 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0004);
697 curbyte += aimutil_put32(buf+curbyte, (u_long)(v&0xffffffff));
702 * aim_puttlv_str - Write a string TLV.
703 * @buf: Destination buffer
705 * @l: Length of string
706 * @v: String to write
708 * Writes a TLV with a string value portion. (Only the first @l
709 * bytes of the passed string will be written, which should not
710 * include the terminating NULL.)
713 faim_export int aim_puttlv_str(u_char *buf, u_short t, int l, char *v)
718 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff));
719 curbyte += aimutil_put16(buf+curbyte, (u_short)(l&0xffff));
721 memcpy(buf+curbyte, (unsigned char *)v, l);