Changeset e374dee for libfaim/tlv.c


Ignore:
Timestamp:
Oct 10, 2003, 5:12:30 PM (18 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
fe6f1d3
Parents:
f4d0975
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libfaim/tlv.c

    r862371b re374dee  
    114114 *        There are a number of places where you want to read in a tlvchain,
    115115 *        but the chain is not at the end of the SNAC, and the chain is
    116  *        preceeded by the number of TLVs.  So you can limit that.
     116 *        preceeded by the number of TLVs.  So you can limit that with this.
    117117 *
    118118 * Reads and parses a series of TLV patterns from a data buffer; the
     
    131131{
    132132        aim_tlvlist_t *list = NULL, *cur;
    133        
     133
    134134        while ((aim_bstream_empty(bs) > 0) && (num != 0)) {
    135135                fu16_t type, length;
     
    159159                cur->tlv->type = type;
    160160                if ((cur->tlv->length = length)) {
    161                        cur->tlv->value = aimbs_getraw(bs, length);     
     161                       cur->tlv->value = aimbs_getraw(bs, length);
    162162                       if (!cur->tlv->value) {
    163163                               freetlv(&cur->tlv);
     
    177177
    178178/**
     179 * aim_readtlvchain_len - Read a TLV chain from a buffer.
     180 * @param bs Input bstream
     181 * @param len The max length in bytes that will be read.
     182 *        There are a number of places where you want to read in a tlvchain,
     183 *        but the chain is not at the end of the SNAC, and the chain is
     184 *        preceeded by the length of the TLVs.  So you can limit that with this.
     185 *
     186 * Reads and parses a series of TLV patterns from a data buffer; the
     187 * returned structure is manipulatable with the rest of the TLV
     188 * routines.  When done with a TLV chain, aim_freetlvchain() should
     189 * be called to free the dynamic substructures.
     190 *
     191 * XXX There should be a flag setable here to have the tlvlist contain
     192 * bstream references, so that at least the ->value portion of each
     193 * element doesn't need to be malloc/memcpy'd.  This could prove to be
     194 * just as effecient as the in-place TLV parsing used in a couple places
     195 * in libfaim.
     196 *
     197 */
     198faim_internal aim_tlvlist_t *aim_readtlvchain_len(aim_bstream_t *bs, fu16_t len)
     199{
     200        aim_tlvlist_t *list = NULL, *cur;
     201
     202        while ((aim_bstream_empty(bs) > 0) && (len > 0)) {
     203                fu16_t type, length;
     204
     205                type = aimbs_get16(bs);
     206                length = aimbs_get16(bs);
     207
     208                if (length > aim_bstream_empty(bs)) {
     209                        aim_freetlvchain(&list);
     210                        return NULL;
     211                }
     212
     213                cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
     214                if (!cur) {
     215                        aim_freetlvchain(&list);
     216                        return NULL;
     217                }
     218
     219                memset(cur, 0, sizeof(aim_tlvlist_t));
     220
     221                cur->tlv = createtlv();
     222                if (!cur->tlv) {
     223                        free(cur);
     224                        aim_freetlvchain(&list);
     225                        return NULL;
     226                }
     227                cur->tlv->type = type;
     228                if ((cur->tlv->length = length)) {
     229                       cur->tlv->value = aimbs_getraw(bs, length);
     230                       if (!cur->tlv->value) {
     231                               freetlv(&cur->tlv);
     232                               free(cur);
     233                               aim_freetlvchain(&list);
     234                               return NULL;
     235                       }
     236                }
     237
     238                len -= aim_sizetlvchain(&cur);
     239                cur->next = list;
     240                list = cur;
     241        }
     242
     243        return list;
     244}
     245
     246/**
     247 * aim_tlvlist_copy - Duplicate a TLV chain.
     248 * @param orig
     249 *
     250 * This is pretty pelf exslanatory.
     251 *
     252 */
     253faim_internal aim_tlvlist_t *aim_tlvlist_copy(aim_tlvlist_t *orig)
     254{
     255        aim_tlvlist_t *new = NULL;
     256
     257        while (orig) {
     258                aim_addtlvtochain_raw(&new, orig->tlv->type, orig->tlv->length, orig->tlv->value);
     259                orig = orig->next;
     260        }
     261
     262        return new;
     263}
     264
     265/*
     266 * Compare two TLV lists for equality.  This probably is not the most
     267 * efficient way to do this.
     268 *
     269 * @param one One of the TLV chains to compare.
     270 * @param two The other TLV chain to compare.
     271 * @preturn Retrun 0 if the lists are the same, return 1 if they are different.
     272 */
     273faim_internal int aim_tlvlist_cmp(aim_tlvlist_t *one, aim_tlvlist_t *two)
     274{
     275        aim_bstream_t bs1, bs2;
     276
     277        if (aim_sizetlvchain(&one) != aim_sizetlvchain(&two))
     278                return 1;
     279
     280        aim_bstream_init(&bs1, ((fu8_t *)malloc(aim_sizetlvchain(&one)*sizeof(fu8_t))), aim_sizetlvchain(&one));
     281        aim_bstream_init(&bs2, ((fu8_t *)malloc(aim_sizetlvchain(&two)*sizeof(fu8_t))), aim_sizetlvchain(&two));
     282
     283        aim_writetlvchain(&bs1, &one);
     284        aim_writetlvchain(&bs2, &two);
     285
     286        if (memcmp(bs1.data, bs2.data, bs1.len)) {
     287                free(bs1.data);
     288                free(bs2.data);
     289                return 1;
     290        }
     291
     292        free(bs1.data);
     293        free(bs2.data);
     294
     295        return 0;
     296}
     297
     298/**
    179299 * aim_freetlvchain - Free a TLV chain structure
    180300 * @list: Chain to be freed
     
    463583
    464584        /* do an initial run to test total length */
    465         for (cur = *list, goodbuflen = 0; cur; cur = cur->next) {
    466                 goodbuflen += 2 + 2; /* type + len */
    467                 goodbuflen += cur->tlv->length;
    468         }
     585        goodbuflen = aim_sizetlvchain(list);
    469586
    470587        if (goodbuflen > aim_bstream_empty(bs))
Note: See TracChangeset for help on using the changeset viewer.