Changeset 862371b for libfaim/tlv.c


Ignore:
Timestamp:
Jun 29, 2003, 1:47:04 PM (21 years ago)
Author:
James M. Kretchmar <kretch@mit.edu>
Branches:
master, barnowl_perlaim, debian, owl, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
e016fc2
Parents:
03ad7b2
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libfaim/tlv.c

    r5e53c4a r862371b  
    2929/**
    3030 * aim_readtlvchain - Read a TLV chain from a buffer.
    31  * @buf: Input buffer
    32  * @maxlen: Length of input buffer
     31 * @param bs Input bstream
    3332 *
    3433 * Reads and parses a series of TLV patterns from a data buffer; the
     
    4746{
    4847        aim_tlvlist_t *list = NULL, *cur;
    49 
     48       
    5049        while (aim_bstream_empty(bs) > 0) {
    5150                fu16_t type, length;
     
    7170                else {
    7271
    73                         if (length > aim_bstream_empty(bs))
    74                                 goto errout;
     72                        if (length > aim_bstream_empty(bs)) {
     73                                aim_freetlvchain(&list);
     74                                return NULL;
     75                        }
    7576
    7677                        cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
    77                         if (!cur)
    78                                 goto errout;
     78                        if (!cur) {
     79                                aim_freetlvchain(&list);
     80                                return NULL;
     81                        }
    7982
    8083                        memset(cur, 0, sizeof(aim_tlvlist_t));
    8184
    82                         cur->tlv = createtlv(); 
     85                        cur->tlv = createtlv();
    8386                        if (!cur->tlv) {
    8487                                free(cur);
    85                                 goto errout;
     88                                aim_freetlvchain(&list);
     89                                return NULL;
    8690                        }
    8791                        cur->tlv->type = type;
     
    9195                                       freetlv(&cur->tlv);
    9296                                       free(cur);
    93                                        goto errout;
     97                                       aim_freetlvchain(&list);
     98                                       return NULL;
    9499                               }
    95100                        }
     
    101106
    102107        return list;
    103 
    104 errout:
    105         aim_freetlvchain(&list);
    106         return NULL;
     108}
     109
     110/**
     111 * aim_readtlvchain_num - Read a TLV chain from a buffer.
     112 * @param bs Input bstream
     113 * @param num The max number of TLVs that will be read, or -1 if unlimited. 
     114 *        There are a number of places where you want to read in a tlvchain,
     115 *        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.
     117 *
     118 * Reads and parses a series of TLV patterns from a data buffer; the
     119 * returned structure is manipulatable with the rest of the TLV
     120 * routines.  When done with a TLV chain, aim_freetlvchain() should
     121 * be called to free the dynamic substructures.
     122 *
     123 * XXX There should be a flag setable here to have the tlvlist contain
     124 * bstream references, so that at least the ->value portion of each
     125 * element doesn't need to be malloc/memcpy'd.  This could prove to be
     126 * just as effecient as the in-place TLV parsing used in a couple places
     127 * in libfaim.
     128 *
     129 */
     130faim_internal aim_tlvlist_t *aim_readtlvchain_num(aim_bstream_t *bs, fu16_t num)
     131{
     132        aim_tlvlist_t *list = NULL, *cur;
     133       
     134        while ((aim_bstream_empty(bs) > 0) && (num != 0)) {
     135                fu16_t type, length;
     136
     137                type = aimbs_get16(bs);
     138                length = aimbs_get16(bs);
     139
     140                if (length > aim_bstream_empty(bs)) {
     141                        aim_freetlvchain(&list);
     142                        return NULL;
     143                }
     144
     145                cur = (aim_tlvlist_t *)malloc(sizeof(aim_tlvlist_t));
     146                if (!cur) {
     147                        aim_freetlvchain(&list);
     148                        return NULL;
     149                }
     150
     151                memset(cur, 0, sizeof(aim_tlvlist_t));
     152
     153                cur->tlv = createtlv();
     154                if (!cur->tlv) {
     155                        free(cur);
     156                        aim_freetlvchain(&list);
     157                        return NULL;
     158                }
     159                cur->tlv->type = type;
     160                if ((cur->tlv->length = length)) {
     161                       cur->tlv->value = aimbs_getraw(bs, length);     
     162                       if (!cur->tlv->value) {
     163                               freetlv(&cur->tlv);
     164                               free(cur);
     165                               aim_freetlvchain(&list);
     166                               return NULL;
     167                       }
     168                }
     169
     170                num--;
     171                cur->next = list;
     172                list = cur;
     173        }
     174
     175        return list;
    107176}
    108177
     
    182251
    183252/**
    184  * aim_addtlvtochain_str - Add a string to a TLV chain
     253 * aim_addtlvtochain_raw - Add a string to a TLV chain
    185254 * @list: Desination chain (%NULL pointer if empty)
    186  * @type: TLV type
    187  * @str: String to add
    188  * @len: Length of string to add (not including %NULL)
     255 * @t: TLV type
     256 * @l: Length of string to add (not including %NULL)
     257 * @v: String to add
    189258 *
    190259 * Adds the passed string as a TLV element of the passed type
     
    245314 * aim_addtlvtochain16 - Add a 16bit integer to a TLV chain
    246315 * @list: Destination chain
    247  * @type: TLV type to add
    248  * @val: Value to add
     316 * @t: TLV type to add
     317 * @v: Value to add
    249318 *
    250319 * Adds a two-byte unsigned integer to a TLV chain.
     
    607676}
    608677#endif
    609 
Note: See TracChangeset for help on using the changeset viewer.