comparison libfaim/tlv.c @ 1593:ec31e23aadc7

[gaim-migrate @ 1603] libfaim updates and a bug fix for bad password. committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Fri, 16 Mar 2001 14:29:54 +0000
parents 1e2cc8c8bf3c
children
comparison
equal deleted inserted replaced
1592:41cecde078c1 1593:ec31e23aadc7
11 * returned structure is manipulatable with the rest of the TLV 11 * returned structure is manipulatable with the rest of the TLV
12 * routines. When done with a TLV chain, aim_freetlvchain() should 12 * routines. When done with a TLV chain, aim_freetlvchain() should
13 * be called to free the dynamic substructures. 13 * be called to free the dynamic substructures.
14 * 14 *
15 */ 15 */
16 faim_export struct aim_tlvlist_t *aim_readtlvchain(u_char *buf, int maxlen) 16 faim_export struct aim_tlvlist_t *aim_readtlvchain(const unsigned char *buf, const int maxlen)
17 { 17 {
18 int pos; 18 int pos;
19 struct aim_tlvlist_t *list; 19 struct aim_tlvlist_t *list;
20 struct aim_tlvlist_t *cur; 20 struct aim_tlvlist_t *cur;
21 21
22 u_short type; 22 unsigned short type, length;
23 u_short length;
24 23
25 if (!buf) 24 if (!buf)
26 return NULL; 25 return NULL;
27 26
28 list = NULL; 27 list = NULL;
154 * 153 *
155 * Adds the passed string as a TLV element of the passed type 154 * Adds the passed string as a TLV element of the passed type
156 * to the TLV chain. 155 * to the TLV chain.
157 * 156 *
158 */ 157 */
159 faim_export int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str, int len) 158 faim_export int aim_addtlvtochain_str(struct aim_tlvlist_t **list, const unsigned short type, const char *str, const int len)
160 { 159 {
161 struct aim_tlvlist_t *newtlv; 160 struct aim_tlvlist_t *newtlv;
162 struct aim_tlvlist_t *cur; 161 struct aim_tlvlist_t *cur;
163 162
164 if (!list) 163 if (!list)
194 * @val: Value to add 193 * @val: Value to add
195 * 194 *
196 * Adds a two-byte unsigned integer to a TLV chain. 195 * Adds a two-byte unsigned integer to a TLV chain.
197 * 196 *
198 */ 197 */
199 faim_export int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val) 198 faim_export int aim_addtlvtochain16(struct aim_tlvlist_t **list, const unsigned short type, const unsigned short val)
200 { 199 {
201 struct aim_tlvlist_t *newtl; 200 struct aim_tlvlist_t *newtl;
202 struct aim_tlvlist_t *cur; 201 struct aim_tlvlist_t *cur;
203 202
204 if (!list) 203 if (!list)
234 * @val: Value to add 233 * @val: Value to add
235 * 234 *
236 * Adds a four-byte unsigned integer to a TLV chain. 235 * Adds a four-byte unsigned integer to a TLV chain.
237 * 236 *
238 */ 237 */
239 faim_export int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val) 238 faim_export int aim_addtlvtochain32(struct aim_tlvlist_t **list, const unsigned short type, const unsigned long val)
240 { 239 {
241 struct aim_tlvlist_t *newtl; 240 struct aim_tlvlist_t *newtl;
242 struct aim_tlvlist_t *cur; 241 struct aim_tlvlist_t *cur;
243 242
244 if (!list) 243 if (!list)
287 * %AIM_CAPS_GETFILE Supports Get File functions 286 * %AIM_CAPS_GETFILE Supports Get File functions
288 * 287 *
289 * %AIM_CAPS_SENDFILE Supports Send File functions 288 * %AIM_CAPS_SENDFILE Supports Send File functions
290 * 289 *
291 */ 290 */
292 faim_export int aim_addtlvtochain_caps(struct aim_tlvlist_t **list, unsigned short type, unsigned short caps) 291 faim_export int aim_addtlvtochain_caps(struct aim_tlvlist_t **list, const unsigned short type, const unsigned short caps)
293 { 292 {
294 unsigned char buf[128]; /* icky fixed length buffer */ 293 unsigned char buf[128]; /* icky fixed length buffer */
295 struct aim_tlvlist_t *newtl; 294 struct aim_tlvlist_t *newtl;
296 struct aim_tlvlist_t *cur; 295 struct aim_tlvlist_t *cur;
297 296
328 * @type: TLV type to add 327 * @type: TLV type to add
329 * 328 *
330 * Adds a TLV with a zero length to a TLV chain. 329 * Adds a TLV with a zero length to a TLV chain.
331 * 330 *
332 */ 331 */
333 faim_internal int aim_addtlvtochain_noval(struct aim_tlvlist_t **list, unsigned short type) 332 faim_internal int aim_addtlvtochain_noval(struct aim_tlvlist_t **list, const unsigned short type)
334 { 333 {
335 struct aim_tlvlist_t *newtlv; 334 struct aim_tlvlist_t *newtlv;
336 struct aim_tlvlist_t *cur; 335 struct aim_tlvlist_t *cur;
337 336
338 newtlv = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t)); 337 newtlv = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
367 * of bytes specified. This operation does not free the chain; 366 * of bytes specified. This operation does not free the chain;
368 * aim_freetlvchain() must still be called to free up the memory used 367 * aim_freetlvchain() must still be called to free up the memory used
369 * by the chain structures. 368 * by the chain structures.
370 * 369 *
371 */ 370 */
372 faim_export int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list) 371 faim_export int aim_writetlvchain(unsigned char *buf, int buflen, struct aim_tlvlist_t **list)
373 { 372 {
374 int goodbuflen = 0; 373 int goodbuflen = 0;
375 int i = 0; 374 int i = 0;
376 struct aim_tlvlist_t *cur; 375 struct aim_tlvlist_t *cur;
377 376
409 * %NULL on error. The @nth parameter is specified starting at %1. 408 * %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 409 * In most cases, there will be no more than one TLV of any type
411 * in a chain. 410 * in a chain.
412 * 411 *
413 */ 412 */
414 faim_export struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *list, u_short type, int nth) 413 faim_export struct aim_tlv_t *aim_gettlv(struct aim_tlvlist_t *list, const unsigned short type, const int nth)
415 { 414 {
416 int i; 415 int i;
417 struct aim_tlvlist_t *cur; 416 struct aim_tlvlist_t *cur;
418 417
419 i = 0; 418 i = 0;
439 * Same as aim_gettlv(), except that the return value is a %NULL- 438 * Same as aim_gettlv(), except that the return value is a %NULL-
440 * terminated string instead of an aim_tlv_t. This is a 439 * terminated string instead of an aim_tlv_t. This is a
441 * dynamic buffer and must be freed by the caller. 440 * dynamic buffer and must be freed by the caller.
442 * 441 *
443 */ 442 */
444 faim_export char *aim_gettlv_str(struct aim_tlvlist_t *list, u_short type, int nth) 443 faim_export char *aim_gettlv_str(struct aim_tlvlist_t *list, const unsigned short type, const int nth)
445 { 444 {
446 struct aim_tlv_t *tlv; 445 struct aim_tlv_t *tlv;
447 char *newstr; 446 char *newstr;
448 447
449 if (!(tlv = aim_gettlv(list, type, nth))) 448 if (!(tlv = aim_gettlv(list, type, nth)))
464 * 463 *
465 * Same as aim_gettlv(), except that the return value is a 464 * Same as aim_gettlv(), except that the return value is a
466 * 8bit integer instead of an aim_tlv_t. 465 * 8bit integer instead of an aim_tlv_t.
467 * 466 *
468 */ 467 */
469 faim_internal unsigned char aim_gettlv8(struct aim_tlvlist_t *list, unsigned short type, int num) 468 faim_internal unsigned char aim_gettlv8(struct aim_tlvlist_t *list, const unsigned short type, const int num)
470 { 469 {
471 struct aim_tlv_t *tlv; 470 struct aim_tlv_t *tlv;
472 471
473 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value) 472 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value)
474 return 0; /* erm */ 473 return 0; /* erm */
483 * 482 *
484 * Same as aim_gettlv(), except that the return value is a 483 * Same as aim_gettlv(), except that the return value is a
485 * 16bit integer instead of an aim_tlv_t. 484 * 16bit integer instead of an aim_tlv_t.
486 * 485 *
487 */ 486 */
488 faim_internal unsigned short aim_gettlv16(struct aim_tlvlist_t *list, unsigned short type, int num) 487 faim_internal unsigned short aim_gettlv16(struct aim_tlvlist_t *list, const unsigned short type, const int num)
489 { 488 {
490 struct aim_tlv_t *tlv; 489 struct aim_tlv_t *tlv;
491 490
492 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value) 491 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value)
493 return 0; /* erm */ 492 return 0; /* erm */
502 * 501 *
503 * Same as aim_gettlv(), except that the return value is a 502 * Same as aim_gettlv(), except that the return value is a
504 * 32bit integer instead of an aim_tlv_t. 503 * 32bit integer instead of an aim_tlv_t.
505 * 504 *
506 */ 505 */
507 faim_internal unsigned long aim_gettlv32(struct aim_tlvlist_t *list, unsigned short type, int num) 506 faim_internal unsigned long aim_gettlv32(struct aim_tlvlist_t *list, const unsigned short type, const int num)
508 { 507 {
509 struct aim_tlv_t *tlv; 508 struct aim_tlv_t *tlv;
510 509
511 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value) 510 if (!(tlv = aim_gettlv(list, type, num)) || !tlv->value)
512 return 0; /* erm */ 511 return 0; /* erm */
523 * 522 *
524 * Both the aim_tlv_t and the tlv->value pointer 523 * Both the aim_tlv_t and the tlv->value pointer
525 * must be freed by the caller if non-%NULL. 524 * must be freed by the caller if non-%NULL.
526 * 525 *
527 */ 526 */
528 faim_export struct aim_tlv_t *aim_grabtlv(u_char *src) 527 faim_export struct aim_tlv_t *aim_grabtlv(const unsigned char *src)
529 { 528 {
530 struct aim_tlv_t *dest = NULL; 529 struct aim_tlv_t *dest = NULL;
531 530
532 dest = aim_createtlv(); 531 dest = aim_createtlv();
533 532
535 dest->type += src[1]; 534 dest->type += src[1];
536 535
537 dest->length = src[2] << 8; 536 dest->length = src[2] << 8;
538 dest->length += src[3]; 537 dest->length += src[3];
539 538
540 dest->value = (u_char *) malloc(dest->length*sizeof(u_char)); 539 dest->value = (unsigned char *) malloc(dest->length);
541 memset(dest->value, 0, dest->length*sizeof(u_char)); 540 memset(dest->value, 0, dest->length);
542 541
543 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char)); 542 memcpy(dest->value, &(src[4]), dest->length);
544 543
545 return dest; 544 return dest;
546 } 545 }
547 546
548 /** 547 /**
556 * 555 *
557 * Both the aim_tlv_t and the tlv->value pointer 556 * Both the aim_tlv_t and the tlv->value pointer
558 * must be freed by the caller if non-%NULL. 557 * must be freed by the caller if non-%NULL.
559 * 558 *
560 */ 559 */
561 faim_export struct aim_tlv_t *aim_grabtlvstr(u_char *src) 560 faim_export struct aim_tlv_t *aim_grabtlvstr(const unsigned char *src)
562 { 561 {
563 struct aim_tlv_t *dest = NULL; 562 struct aim_tlv_t *dest = NULL;
564 563
565 dest = aim_createtlv(); 564 dest = aim_createtlv();
566 565
568 dest->type += src[1]; 567 dest->type += src[1];
569 568
570 dest->length = src[2] << 8; 569 dest->length = src[2] << 8;
571 dest->length += src[3]; 570 dest->length += src[3];
572 571
573 dest->value = (u_char *) malloc((dest->length+1)*sizeof(u_char)); 572 dest->value = (unsigned char *) malloc(dest->length+1);
574 memset(dest->value, 0, (dest->length+1)*sizeof(u_char)); 573 memset(dest->value, 0, dest->length+1);
575 574
576 memcpy(dest->value, &(src[4]), dest->length*sizeof(u_char)); 575 memcpy(dest->value, src+4, dest->length);
577 dest->value[dest->length] = '\0'; 576 dest->value[dest->length] = '\0';
578 577
579 return dest; 578 return dest;
580 } 579 }
581 580
589 * 588 *
590 * The passed aim_tlv_t is not freed. aim_freetlv() should 589 * The passed aim_tlv_t is not freed. aim_freetlv() should
591 * still be called by the caller to free the structure. 590 * still be called by the caller to free the structure.
592 * 591 *
593 */ 592 */
594 faim_export int aim_puttlv(u_char *dest, struct aim_tlv_t *newtlv) 593 faim_export int aim_puttlv(unsigned char *dest, struct aim_tlv_t *newtlv)
595 { 594 {
596 int i=0; 595 int i=0;
597 596
598 dest[i++] = newtlv->type >> 8; 597 dest[i++] = newtlv->type >> 8;
599 dest[i++] = newtlv->type & 0x00FF; 598 dest[i++] = newtlv->type & 0x00FF;
649 * @v: Value 648 * @v: Value
650 * 649 *
651 * Writes a TLV with a one-byte integer value portion. 650 * Writes a TLV with a one-byte integer value portion.
652 * 651 *
653 */ 652 */
654 faim_export int aim_puttlv_8(unsigned char *buf, unsigned short t, unsigned char v) 653 faim_export int aim_puttlv_8(unsigned char *buf, const unsigned short t, const unsigned char v)
655 { 654 {
656 int curbyte=0; 655 int curbyte=0;
657 656
658 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(t&0xffff)); 657 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(t&0xffff));
659 curbyte += aimutil_put16(buf+curbyte, (unsigned short)0x0001); 658 curbyte += aimutil_put16(buf+curbyte, (unsigned short)0x0001);
669 * @v: Value 668 * @v: Value
670 * 669 *
671 * Writes a TLV with a two-byte integer value portion. 670 * Writes a TLV with a two-byte integer value portion.
672 * 671 *
673 */ 672 */
674 faim_export int aim_puttlv_16(u_char *buf, u_short t, u_short v) 673 faim_export int aim_puttlv_16(unsigned char *buf, const unsigned short t, const unsigned short v)
675 { 674 {
676 int curbyte=0; 675 int curbyte=0;
677 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff)); 676 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(t&0xffff));
678 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0002); 677 curbyte += aimutil_put16(buf+curbyte, (unsigned short)0x0002);
679 curbyte += aimutil_put16(buf+curbyte, (u_short)(v&0xffff)); 678 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(v&0xffff));
680 return curbyte; 679 return curbyte;
681 } 680 }
682 681
683 /** 682 /**
684 * aim_puttlv_32 - Write a four-byte TLV. 683 * aim_puttlv_32 - Write a four-byte TLV.
687 * @v: Value 686 * @v: Value
688 * 687 *
689 * Writes a TLV with a four-byte integer value portion. 688 * Writes a TLV with a four-byte integer value portion.
690 * 689 *
691 */ 690 */
692 faim_export int aim_puttlv_32(u_char *buf, u_short t, u_long v) 691 faim_export int aim_puttlv_32(unsigned char *buf, const unsigned short t, const unsigned long v)
693 { 692 {
694 int curbyte=0; 693 int curbyte=0;
695 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff)); 694 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(t&0xffff));
696 curbyte += aimutil_put16(buf+curbyte, (u_short)0x0004); 695 curbyte += aimutil_put16(buf+curbyte, (unsigned short)0x0004);
697 curbyte += aimutil_put32(buf+curbyte, (u_long)(v&0xffffffff)); 696 curbyte += aimutil_put32(buf+curbyte, (unsigned long)(v&0xffffffff));
698 return curbyte; 697 return curbyte;
699 } 698 }
700 699
701 /** 700 /**
702 * aim_puttlv_str - Write a string TLV. 701 * aim_puttlv_str - Write a string TLV.
708 * Writes a TLV with a string value portion. (Only the first @l 707 * Writes a TLV with a string value portion. (Only the first @l
709 * bytes of the passed string will be written, which should not 708 * bytes of the passed string will be written, which should not
710 * include the terminating NULL.) 709 * include the terminating NULL.)
711 * 710 *
712 */ 711 */
713 faim_export int aim_puttlv_str(u_char *buf, u_short t, int l, char *v) 712 faim_export int aim_puttlv_str(unsigned char *buf, const unsigned short t, const int l, const char *v)
714 { 713 {
715 int curbyte; 714 int curbyte;
716 715
717 curbyte = 0; 716 curbyte = 0;
718 curbyte += aimutil_put16(buf+curbyte, (u_short)(t&0xffff)); 717 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(t&0xffff));
719 curbyte += aimutil_put16(buf+curbyte, (u_short)(l&0xffff)); 718 curbyte += aimutil_put16(buf+curbyte, (unsigned short)(l&0xffff));
720 if (v) 719 if (v)
721 memcpy(buf+curbyte, (unsigned char *)v, l); 720 memcpy(buf+curbyte, (unsigned char *)v, l);
722 curbyte += l; 721 curbyte += l;
723 return curbyte; 722 return curbyte;
724 } 723 }