Changeset 5ebff60 for protocols/oscar/tlv.c
- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/oscar/tlv.c
raf359b4 r5ebff60 3 3 static void freetlv(aim_tlv_t **oldtlv) 4 4 { 5 if (!oldtlv || !*oldtlv) 5 if (!oldtlv || !*oldtlv) { 6 6 return; 7 7 } 8 8 9 g_free((*oldtlv)->value); 9 10 g_free(*oldtlv); … … 22 23 * 23 24 * XXX There should be a flag setable here to have the tlvlist contain 24 * bstream references, so that at least the ->value portion of each 25 * bstream references, so that at least the ->value portion of each 25 26 * element doesn't need to be malloc/memcpy'd. This could prove to be 26 27 * just as effecient as the in-place TLV parsing used in a couple places … … 42 43 cur->tlv = g_new0(aim_tlv_t, 1); 43 44 cur->tlv->type = type; 44 if ((cur->tlv->length = length)) 45 cur->tlv->value = aimbs_getraw(bs, length); 45 if ((cur->tlv->length = length)) { 46 cur->tlv->value = aimbs_getraw(bs, length); 47 } 46 48 47 49 cur->next = list; … … 65 67 aim_tlvlist_t *cur; 66 68 67 if (!list || !*list) 69 if (!list || !*list) { 68 70 return; 71 } 69 72 70 73 for (cur = *list; cur; ) { 71 74 aim_tlvlist_t *tmp; 72 75 73 76 freetlv(&cur->tlv); 74 77 … … 95 98 int count; 96 99 97 if (!list || !*list) 98 return 0; 99 100 for (cur = *list, count = 0; cur; cur = cur->next) 100 if (!list || !*list) { 101 return 0; 102 } 103 104 for (cur = *list, count = 0; cur; cur = cur->next) { 101 105 count++; 106 } 102 107 103 108 return count; … … 108 113 * @list: Chain to be sized 109 114 * 110 * Returns the number of bytes that would be needed to 115 * Returns the number of bytes that would be needed to 111 116 * write the passed TLV chain to a data buffer. 112 117 * … … 117 122 int size; 118 123 119 if (!list || !*list) 120 return 0; 121 122 for (cur = *list, size = 0; cur; cur = cur->next) 124 if (!list || !*list) { 125 return 0; 126 } 127 128 for (cur = *list, size = 0; cur; cur = cur->next) { 123 129 size += (4 + cur->tlv->length); 130 } 124 131 125 132 return size; … … 141 148 aim_tlvlist_t *newtlv, *cur; 142 149 143 if (!list) 144 return 0; 145 146 if (!(newtlv = g_new0(aim_tlvlist_t, 1))) 147 return 0; 150 if (!list) { 151 return 0; 152 } 153 154 if (!(newtlv = g_new0(aim_tlvlist_t, 1))) { 155 return 0; 156 } 148 157 149 158 if (!(newtlv->tlv = g_new0(aim_tlv_t, 1))) { … … 153 162 newtlv->tlv->type = t; 154 163 if ((newtlv->tlv->length = l)) { 155 newtlv->tlv->value = (guint8 *) g_malloc(newtlv->tlv->length);164 newtlv->tlv->value = (guint8 *) g_malloc(newtlv->tlv->length); 156 165 memcpy(newtlv->tlv->value, v, newtlv->tlv->length); 157 166 } 158 167 159 if (!*list) 168 if (!*list) { 160 169 *list = newtlv; 161 else {162 for (cur = *list; cur->next; cur = cur->next)170 } else { 171 for (cur = *list; cur->next; cur = cur->next) { 163 172 ; 173 } 164 174 cur->next = newtlv; 165 175 } … … 234 244 int aim_addtlvtochain_caps(aim_tlvlist_t **list, const guint16 t, const guint32 caps) 235 245 { 236 guint8 buf[16 *16]; /* XXX icky fixed length buffer */246 guint8 buf[16 * 16]; /* XXX icky fixed length buffer */ 237 247 aim_bstream_t bs; 238 248 239 if (!caps) 249 if (!caps) { 240 250 return 0; /* nothing there anyway */ 241 251 252 } 242 253 aim_bstream_init(&bs, buf, sizeof(buf)); 243 254 … … 266 277 * 267 278 * XXX should probably support sublists for real. 268 * 279 * 269 280 * This is so neat. 270 281 * … … 278 289 buflen = aim_sizetlvchain(tl); 279 290 280 if (buflen <= 0) 281 return 0; 282 283 if (!(buf = g_malloc(buflen))) 284 return 0; 291 if (buflen <= 0) { 292 return 0; 293 } 294 295 if (!(buf = g_malloc(buflen))) { 296 return 0; 297 } 285 298 286 299 aim_bstream_init(&bs, buf, buflen); … … 295 308 } 296 309 297 int aim_addtlvtochain_chatroom(aim_tlvlist_t **list, guint16 type, guint16 exchange, const char *roomname, guint16 instance) 310 int aim_addtlvtochain_chatroom(aim_tlvlist_t **list, guint16 type, guint16 exchange, const char *roomname, 311 guint16 instance) 298 312 { 299 313 guint8 *buf; … … 302 316 303 317 buflen = 2 + 1 + strlen(roomname) + 2; 304 305 if (!(buf = g_malloc(buflen))) 306 return 0; 318 319 if (!(buf = g_malloc(buflen))) { 320 return 0; 321 } 307 322 308 323 aim_bstream_init(&bs, buf, buflen); … … 310 325 aimbs_put16(&bs, exchange); 311 326 aimbs_put8(&bs, strlen(roomname)); 312 aimbs_putraw(&bs, (guint8 *) roomname, strlen(roomname));327 aimbs_putraw(&bs, (guint8 *) roomname, strlen(roomname)); 313 328 aimbs_put16(&bs, instance); 314 329 … … 327 342 * 328 343 * Copies a TLV chain into a raw data buffer, writing only the number 329 * of bytes specified. This operation does not free the chain; 344 * of bytes specified. This operation does not free the chain; 330 345 * aim_freetlvchain() must still be called to free up the memory used 331 346 * by the chain structures. 332 347 * 333 * XXX clean this up, make better use of bstreams 348 * XXX clean this up, make better use of bstreams 334 349 */ 335 350 int aim_writetlvchain(aim_bstream_t *bs, aim_tlvlist_t **list) … … 344 359 } 345 360 346 if (goodbuflen > aim_bstream_empty(bs)) 361 if (goodbuflen > aim_bstream_empty(bs)) { 347 362 return 0; /* not enough buffer */ 348 363 364 } 349 365 /* do the real write-out */ 350 366 for (cur = *list; cur; cur = cur->next) { 351 367 aimbs_put16(bs, cur->tlv->type); 352 368 aimbs_put16(bs, cur->tlv->length); 353 if (cur->tlv->length) 369 if (cur->tlv->length) { 354 370 aimbs_putraw(bs, cur->tlv->value, cur->tlv->length); 371 } 355 372 } 356 373 … … 365 382 * @nth: Index of TLV of type to get 366 383 * 367 * Returns a pointer to an aim_tlv_t of the specified type; 384 * Returns a pointer to an aim_tlv_t of the specified type; 368 385 * %NULL on error. The @nth parameter is specified starting at %1. 369 386 * In most cases, there will be no more than one TLV of any type … … 378 395 for (cur = list, i = 0; cur; cur = cur->next) { 379 396 if (cur && cur->tlv) { 380 if (cur->tlv->type == t) 397 if (cur->tlv->type == t) { 381 398 i++; 382 if (i >= n) 399 } 400 if (i >= n) { 383 401 return cur->tlv; 402 } 384 403 } 385 404 } … … 395 414 * 396 415 * Same as aim_gettlv(), except that the return value is a %NULL- 397 * terminated string instead of an aim_tlv_t. This is a 416 * terminated string instead of an aim_tlv_t. This is a 398 417 * dynamic buffer and must be freed by the caller. 399 418 * … … 404 423 char *newstr; 405 424 406 if (!(tlv = aim_gettlv(list, t, n))) 425 if (!(tlv = aim_gettlv(list, t, n))) { 407 426 return NULL; 427 } 408 428 409 429 newstr = (char *) g_malloc(tlv->length + 1); … … 420 440 * @nth: Index of TLV to return 421 441 * 422 * Same as aim_gettlv(), except that the return value is a 423 * 8bit integer instead of an aim_tlv_t. 442 * Same as aim_gettlv(), except that the return value is a 443 * 8bit integer instead of an aim_tlv_t. 424 444 * 425 445 */ … … 428 448 aim_tlv_t *tlv; 429 449 430 if (!(tlv = aim_gettlv(list, t, n))) 450 if (!(tlv = aim_gettlv(list, t, n))) { 431 451 return 0; /* erm */ 452 } 432 453 return aimutil_get8(tlv->value); 433 454 } … … 439 460 * @nth: Index of TLV to return 440 461 * 441 * Same as aim_gettlv(), except that the return value is a 442 * 16bit integer instead of an aim_tlv_t. 462 * Same as aim_gettlv(), except that the return value is a 463 * 16bit integer instead of an aim_tlv_t. 443 464 * 444 465 */ … … 447 468 aim_tlv_t *tlv; 448 469 449 if (!(tlv = aim_gettlv(list, t, n))) 470 if (!(tlv = aim_gettlv(list, t, n))) { 450 471 return 0; /* erm */ 472 } 451 473 return aimutil_get16(tlv->value); 452 474 } … … 458 480 * @nth: Index of TLV to return 459 481 * 460 * Same as aim_gettlv(), except that the return value is a 461 * 32bit integer instead of an aim_tlv_t. 482 * Same as aim_gettlv(), except that the return value is a 483 * 32bit integer instead of an aim_tlv_t. 462 484 * 463 485 */ … … 466 488 aim_tlv_t *tlv; 467 489 468 if (!(tlv = aim_gettlv(list, t, n))) 490 if (!(tlv = aim_gettlv(list, t, n))) { 469 491 return 0; /* erm */ 492 } 470 493 return aimutil_get32(tlv->value); 471 494 } … … 544 567 545 568 i = aimutil_put16(buf, t); 546 i += aimutil_put16(buf+i, l); 547 if (l) 548 memcpy(buf+i, v, l); 569 i += aimutil_put16(buf + i, l); 570 if (l) { 571 memcpy(buf + i, v, l); 572 } 549 573 i += l; 550 574
Note: See TracChangeset
for help on using the changeset viewer.