5 static aim_tlv_t *createtlv(void)
9 if (!(newtlv = (aim_tlv_t *)malloc(sizeof(aim_tlv_t))))
11 memset(newtlv, 0, sizeof(aim_tlv_t));
16 static void freetlv(aim_tlv_t **oldtlv)
19 if (!oldtlv || !*oldtlv)
22 free((*oldtlv)->value);
30 * aim_readtlvchain - Read a TLV chain from a buffer.
32 * @maxlen: Length of input buffer
34 * Reads and parses a series of TLV patterns from a data buffer; the
35 * returned structure is manipulatable with the rest of the TLV
36 * routines. When done with a TLV chain, aim_freetlvchain() should
37 * be called to free the dynamic substructures.
39 * XXX There should be a flag setable here to have the tlvlist contain
40 * bstream references, so that at least the ->value portion of each
41 * element doesn't need to be malloc/memcpy'd. This could prove to be
42 * just as effecient as the in-place TLV parsing used in a couple places
46 faim_internal aim_tlvlist_t *aim_readtlvchain(aim_bstream_t *bs)
48 aim_tlvlist_t *list = NULL, *cur;
51 while (aim_bstream_empty(bs)) {
53 type = aimbs_get16(bs);
54 length = aimbs_get16(bs);
56 #if 0 /* temporarily disabled until I know if they're still doing it or not */
58 * Okay, so now AOL has decided that any TLV of
59 * type 0x0013 can only be two bytes, despite
60 * what the actual given length is. So here
61 * we dump any invalid TLVs of that sort. Hopefully
62 * theres no special cases to this special case.
65 if ((type == 0x0013) && (length != 0x0002))
73 cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
74 memset(cur, 0, sizeof(aim_tlvlist_t));
76 cur->tlv = createtlv();
77 cur->tlv->type = type;
78 if ((cur->tlv->length = length))
79 cur->tlv->value = aimbs_getraw(bs, length);
90 * aim_freetlvchain - Free a TLV chain structure
91 * @list: Chain to be freed
93 * Walks the list of TLVs in the passed TLV chain and
94 * frees each one. Note that any references to this data
95 * should be removed before calling this.
98 faim_internal void aim_freetlvchain(aim_tlvlist_t **list)
105 for (cur = *list; cur; ) {
121 * aim_counttlvchain - Count the number of TLVs in a chain
122 * @list: Chain to be counted
124 * Returns the number of TLVs stored in the passed chain.
127 faim_internal int aim_counttlvchain(aim_tlvlist_t **list)
135 for (cur = *list, count = 0; cur; cur = cur->next)
142 * aim_sizetlvchain - Count the number of bytes in a TLV chain
143 * @list: Chain to be sized
145 * Returns the number of bytes that would be needed to
146 * write the passed TLV chain to a data buffer.
149 faim_internal int aim_sizetlvchain(aim_tlvlist_t **list)
157 for (cur = *list, size = 0; cur; cur = cur->next)
158 size += (4 + cur->tlv->length);
164 * aim_addtlvtochain_str - Add a string to a TLV chain
165 * @list: Desination chain (%NULL pointer if empty)
167 * @str: String to add
168 * @len: Length of string to add (not including %NULL)
170 * Adds the passed string as a TLV element of the passed type
174 faim_internal int aim_addtlvtochain_raw(aim_tlvlist_t **list, const fu16_t t, const fu16_t l, const fu8_t *v)
176 aim_tlvlist_t *newtlv, *cur;
181 if (!(newtlv = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t))))
183 memset(newtlv, 0x00, sizeof(aim_tlvlist_t));
185 if (!(newtlv->tlv = createtlv())) {
189 newtlv->tlv->type = t;
190 if ((newtlv->tlv->length = l)) {
191 newtlv->tlv->value = (fu8_t *)malloc(newtlv->tlv->length);
192 memcpy(newtlv->tlv->value, v, newtlv->tlv->length);
198 for(cur = *list; cur->next; cur = cur->next)
203 return newtlv->tlv->length;
207 * aim_addtlvtochain16 - Add a 16bit integer to a TLV chain
208 * @list: Destination chain
209 * @type: TLV type to add
212 * Adds a two-byte unsigned integer to a TLV chain.
215 faim_internal int aim_addtlvtochain16(aim_tlvlist_t **list, const fu16_t t, const fu16_t v)
219 aimutil_put16(v16, v);
221 return aim_addtlvtochain_raw(list, t, 2, v16);
225 * aim_addtlvtochain32 - Add a 32bit integer to a TLV chain
226 * @list: Destination chain
227 * @type: TLV type to add
230 * Adds a four-byte unsigned integer to a TLV chain.
233 faim_internal int aim_addtlvtochain32(aim_tlvlist_t **list, const fu16_t t, const fu32_t v)
237 aimutil_put32(v32, v);
239 return aim_addtlvtochain_raw(list, t, 4, v32);
243 * aim_addtlvtochain_caps - Add a capability block to a TLV chain
244 * @list: Destination chain
245 * @type: TLV type to add
246 * @caps: Bitfield of capability flags to send
248 * Adds a block of capability blocks to a TLV chain. The bitfield
249 * passed in should be a bitwise %OR of any of the %AIM_CAPS constants:
251 * %AIM_CAPS_BUDDYICON Supports Buddy Icons
253 * %AIM_CAPS_VOICE Supports Voice Chat
255 * %AIM_CAPS_IMIMAGE Supports DirectIM/IMImage
257 * %AIM_CAPS_CHAT Supports Chat
259 * %AIM_CAPS_GETFILE Supports Get File functions
261 * %AIM_CAPS_SENDFILE Supports Send File functions
264 faim_internal int aim_addtlvtochain_caps(aim_tlvlist_t **list, const fu16_t t, const fu16_t caps)
266 fu8_t buf[16*16]; /* icky fixed length buffer */
270 return 0; /* nothing there anyway */
272 aim_bstream_init(&bs, buf, sizeof(buf));
274 aim_putcap(&bs, caps);
276 return aim_addtlvtochain_raw(list, t, aim_bstream_curpos(&bs), buf);
279 faim_internal int aim_addtlvtochain_userinfo(aim_tlvlist_t **list, fu16_t type, aim_userinfo_t *ui)
281 fu8_t buf[1024]; /* bleh */
284 aim_bstream_init(&bs, buf, sizeof(buf));
286 aim_putuserinfo(&bs, ui);
288 return aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf);
292 * aim_addtlvtochain_noval - Add a blank TLV to a TLV chain
293 * @list: Destination chain
294 * @type: TLV type to add
296 * Adds a TLV with a zero length to a TLV chain.
299 faim_internal int aim_addtlvtochain_noval(aim_tlvlist_t **list, const fu16_t t)
301 return aim_addtlvtochain_raw(list, t, 0, NULL);
305 * Note that the inner TLV chain will not be modifiable as a tlvchain once
306 * it is written using this. Or rather, it can be, but updates won't be
309 * XXX should probably support sublists for real.
314 faim_internal int aim_addtlvtochain_frozentlvlist(aim_tlvlist_t **list, fu16_t type, aim_tlvlist_t **tl)
320 buflen = aim_sizetlvchain(tl);
325 if (!(buf = malloc(buflen)))
328 aim_bstream_init(&bs, buf, buflen);
330 aim_writetlvchain(&bs, tl);
332 aim_addtlvtochain_raw(list, type, aim_bstream_curpos(&bs), buf);
340 * aim_writetlvchain - Write a TLV chain into a data buffer.
341 * @buf: Destination buffer
342 * @buflen: Maximum number of bytes that will be written to buffer
343 * @list: Source TLV chain
345 * Copies a TLV chain into a raw data buffer, writing only the number
346 * of bytes specified. This operation does not free the chain;
347 * aim_freetlvchain() must still be called to free up the memory used
348 * by the chain structures.
350 * XXX clean this up, make better use of bstreams
352 faim_internal int aim_writetlvchain(aim_bstream_t *bs, aim_tlvlist_t **list)
357 /* do an initial run to test total length */
358 for (cur = *list, goodbuflen = 0; cur; cur = cur->next) {
359 goodbuflen += 2 + 2; /* type + len */
360 goodbuflen += cur->tlv->length;
363 if (goodbuflen > aim_bstream_empty(bs))
364 return 0; /* not enough buffer */
366 /* do the real write-out */
367 for (cur = *list; cur; cur = cur->next) {
368 aimbs_put16(bs, cur->tlv->type);
369 aimbs_put16(bs, cur->tlv->length);
370 if (cur->tlv->length)
371 aimbs_putraw(bs, cur->tlv->value, cur->tlv->length);
374 return 1; /* XXX this is a nonsensical return */
379 * aim_gettlv - Grab the Nth TLV of type type in the TLV list list.
380 * @list: Source chain
381 * @type: Requested TLV type
382 * @nth: Index of TLV of type to get
384 * Returns a pointer to an aim_tlv_t of the specified type;
385 * %NULL on error. The @nth parameter is specified starting at %1.
386 * In most cases, there will be no more than one TLV of any type
390 faim_internal aim_tlv_t *aim_gettlv(aim_tlvlist_t *list, const fu16_t t, const int n)
395 for (cur = list, i = 0; cur; cur = cur->next) {
396 if (cur && cur->tlv) {
397 if (cur->tlv->type == t)
408 * aim_gettlv_str - Retrieve the Nth TLV in chain as a string.
409 * @list: Source TLV chain
410 * @type: TLV type to search for
411 * @nth: Index of TLV to return
413 * Same as aim_gettlv(), except that the return value is a %NULL-
414 * terminated string instead of an aim_tlv_t. This is a
415 * dynamic buffer and must be freed by the caller.
418 faim_internal char *aim_gettlv_str(aim_tlvlist_t *list, const fu16_t t, const int n)
423 if (!(tlv = aim_gettlv(list, t, n)))
426 newstr = (char *) malloc(tlv->length + 1);
427 memcpy(newstr, tlv->value, tlv->length);
428 *(newstr + tlv->length) = '\0';
434 * aim_gettlv8 - Retrieve the Nth TLV in chain as a 8bit integer.
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
440 * 8bit integer instead of an aim_tlv_t.
443 faim_internal fu8_t aim_gettlv8(aim_tlvlist_t *list, const fu16_t t, const int n)
447 if (!(tlv = aim_gettlv(list, t, n)))
449 return aimutil_get8(tlv->value);
453 * aim_gettlv16 - Retrieve the Nth TLV in chain as a 16bit integer.
454 * @list: Source TLV chain
455 * @type: TLV type to search for
456 * @nth: Index of TLV to return
458 * Same as aim_gettlv(), except that the return value is a
459 * 16bit integer instead of an aim_tlv_t.
462 faim_internal fu16_t aim_gettlv16(aim_tlvlist_t *list, const fu16_t t, const int n)
466 if (!(tlv = aim_gettlv(list, t, n)))
468 return aimutil_get16(tlv->value);
472 * aim_gettlv32 - Retrieve the Nth TLV in chain as a 32bit integer.
473 * @list: Source TLV chain
474 * @type: TLV type to search for
475 * @nth: Index of TLV to return
477 * Same as aim_gettlv(), except that the return value is a
478 * 32bit integer instead of an aim_tlv_t.
481 faim_internal fu32_t aim_gettlv32(aim_tlvlist_t *list, const fu16_t t, const int n)
485 if (!(tlv = aim_gettlv(list, t, n)))
487 return aimutil_get32(tlv->value);
492 * aim_puttlv_8 - Write a one-byte TLV.
493 * @buf: Destination buffer
497 * Writes a TLV with a one-byte integer value portion.
500 faim_export int aim_puttlv_8(fu8_t *buf, const fu16_t t, const fu8_t v)
506 return aim_puttlv_raw(buf, t, 1, v8);
510 * aim_puttlv_16 - Write a two-byte TLV.
511 * @buf: Destination buffer
515 * Writes a TLV with a two-byte integer value portion.
518 faim_export int aim_puttlv_16(fu8_t *buf, const fu16_t t, const fu16_t v)
522 aimutil_put16(v16, v);
524 return aim_puttlv_raw(buf, t, 2, v16);
529 * aim_puttlv_32 - Write a four-byte TLV.
530 * @buf: Destination buffer
534 * Writes a TLV with a four-byte integer value portion.
537 faim_export int aim_puttlv_32(fu8_t *buf, const fu16_t t, const fu32_t v)
541 aimutil_put32(v32, v);
543 return aim_puttlv_raw(buf, t, 4, v32);
547 * aim_puttlv_raw - Write a raw TLV.
548 * @buf: Destination buffer
550 * @l: Length of string
551 * @v: String to write
553 * Writes a TLV with a raw value portion. (Only the first @l
554 * bytes of the passed buffer will be written, which should not
555 * include a terminating NULL.)
558 faim_export int aim_puttlv_raw(fu8_t *buf, const fu16_t t, const fu16_t l, const fu8_t *v)
562 i = aimutil_put16(buf, t);
563 i += aimutil_put16(buf+i, l);