1 /* $NetBSD: buffer.h,v 1.8 2014/12/10 04:38:00 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004-2008, 2010, 2012, 2014 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 1998-2002 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: buffer.h,v 1.55 2010/12/20 23:47:21 tbox Exp */ 21 22 #ifndef ISC_BUFFER_H 23 #define ISC_BUFFER_H 1 24 25 /***** 26 ***** Module Info 27 *****/ 28 29 /*! \file isc/buffer.h 30 * 31 * \brief A buffer is a region of memory, together with a set of related subregions. 32 * Buffers are used for parsing and I/O operations. 33 * 34 * The 'used region' and the 'available' region are disjoint, and their 35 * union is the buffer's region. The used region extends from the beginning 36 * of the buffer region to the last used byte. The available region 37 * extends from one byte greater than the last used byte to the end of the 38 * buffer's region. The size of the used region can be changed using various 39 * buffer commands. Initially, the used region is empty. 40 * 41 * The used region is further subdivided into two disjoint regions: the 42 * 'consumed region' and the 'remaining region'. The union of these two 43 * regions is the used region. The consumed region extends from the beginning 44 * of the used region to the byte before the 'current' offset (if any). The 45 * 'remaining' region the current pointer to the end of the used 46 * region. The size of the consumed region can be changed using various 47 * buffer commands. Initially, the consumed region is empty. 48 * 49 * The 'active region' is an (optional) subregion of the remaining region. 50 * It extends from the current offset to an offset in the remaining region 51 * that is selected with isc_buffer_setactive(). Initially, the active region 52 * is empty. If the current offset advances beyond the chosen offset, the 53 * active region will also be empty. 54 * 55 * \verbatim 56 * /------------entire length---------------\ 57 * /----- used region -----\/-- available --\ 58 * +----------------------------------------+ 59 * | consumed | remaining | | 60 * +----------------------------------------+ 61 * a b c d e 62 * 63 * a == base of buffer. 64 * b == current pointer. Can be anywhere between a and d. 65 * c == active pointer. Meaningful between b and d. 66 * d == used pointer. 67 * e == length of buffer. 68 * 69 * a-e == entire length of buffer. 70 * a-d == used region. 71 * a-b == consumed region. 72 * b-d == remaining region. 73 * b-c == optional active region. 74 *\endverbatim 75 * 76 * The following invariants are maintained by all routines: 77 * 78 *\code 79 * length > 0 80 * 81 * base is a valid pointer to length bytes of memory 82 * 83 * 0 <= used <= length 84 * 85 * 0 <= current <= used 86 * 87 * 0 <= active <= used 88 * (although active < current implies empty active region) 89 *\endcode 90 * 91 * \li MP: 92 * Buffers have no synchronization. Clients must ensure exclusive 93 * access. 94 * 95 * \li Reliability: 96 * No anticipated impact. 97 * 98 * \li Resources: 99 * Memory: 1 pointer + 6 unsigned integers per buffer. 100 * 101 * \li Security: 102 * No anticipated impact. 103 * 104 * \li Standards: 105 * None. 106 */ 107 108 /*** 109 *** Imports 110 ***/ 111 112 #include <isc/lang.h> 113 #include <isc/magic.h> 114 #include <isc/types.h> 115 116 /*! 117 * To make many functions be inline macros (via \#define) define this. 118 * If it is undefined, a function will be used. 119 */ 120 /* #define ISC_BUFFER_USEINLINE */ 121 122 ISC_LANG_BEGINDECLS 123 124 /*@{*/ 125 /*! 126 *** Magic numbers 127 ***/ 128 #define ISC_BUFFER_MAGIC 0x42756621U /* Buf!. */ 129 #define ISC_BUFFER_VALID(b) ISC_MAGIC_VALID(b, ISC_BUFFER_MAGIC) 130 /*@}*/ 131 132 /* 133 * The following macros MUST be used only on valid buffers. It is the 134 * caller's responsibility to ensure this by using the ISC_BUFFER_VALID 135 * check above, or by calling another isc_buffer_*() function (rather than 136 * another macro.) 137 */ 138 139 /*@{*/ 140 /*! 141 * Fundamental buffer elements. (A through E in the introductory comment.) 142 */ 143 #define isc_buffer_base(b) ((void *)(b)->base) /*a*/ 144 #define isc_buffer_current(b) \ 145 ((void *)((unsigned char *)(b)->base + (b)->current)) /*b*/ 146 #define isc_buffer_active(b) \ 147 ((void *)((unsigned char *)(b)->base + (b)->active)) /*c*/ 148 #define isc_buffer_used(b) \ 149 ((void *)((unsigned char *)(b)->base + (b)->used)) /*d*/ 150 #define isc_buffer_length(b) ((b)->length) /*e*/ 151 /*@}*/ 152 153 /*@{*/ 154 /*! 155 * Derived lengths. (Described in the introductory comment.) 156 */ 157 #define isc_buffer_usedlength(b) ((b)->used) /* d-a */ 158 #define isc_buffer_consumedlength(b) ((b)->current) /* b-a */ 159 #define isc_buffer_remaininglength(b) ((b)->used - (b)->current) /* d-b */ 160 #define isc_buffer_activelength(b) ((b)->active - (b)->current) /* c-b */ 161 #define isc_buffer_availablelength(b) ((b)->length - (b)->used) /* e-d */ 162 /*@}*/ 163 164 /*! 165 * Note that the buffer structure is public. This is principally so buffer 166 * operations can be implemented using macros. Applications are strongly 167 * discouraged from directly manipulating the structure. 168 */ 169 170 struct isc_buffer { 171 unsigned int magic; 172 void *base; 173 /*@{*/ 174 /*! The following integers are byte offsets from 'base'. */ 175 unsigned int length; 176 unsigned int used; 177 unsigned int current; 178 unsigned int active; 179 /*@}*/ 180 /*! linkable */ 181 ISC_LINK(isc_buffer_t) link; 182 /*! private internal elements */ 183 isc_mem_t *mctx; 184 }; 185 186 /*** 187 *** Functions 188 ***/ 189 190 isc_result_t 191 isc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer, 192 unsigned int length); 193 /*!< 194 * \brief Allocate a dynamic linkable buffer which has "length" bytes in the 195 * data region. 196 * 197 * Requires: 198 *\li "mctx" is valid. 199 * 200 *\li "dynbuffer" is non-NULL, and "*dynbuffer" is NULL. 201 * 202 * Returns: 203 *\li ISC_R_SUCCESS - success 204 *\li ISC_R_NOMEMORY - no memory available 205 * 206 * Note: 207 *\li Changing the buffer's length field is not permitted. 208 */ 209 210 void 211 isc_buffer_free(isc_buffer_t **dynbuffer); 212 /*!< 213 * \brief Release resources allocated for a dynamic buffer. 214 * 215 * Requires: 216 *\li "dynbuffer" is not NULL. 217 * 218 *\li "*dynbuffer" is a valid dynamic buffer. 219 * 220 * Ensures: 221 *\li "*dynbuffer" will be NULL on return, and all memory associated with 222 * the dynamic buffer is returned to the memory context used in 223 * isc_buffer_allocate(). 224 */ 225 226 void 227 isc__buffer_init(isc_buffer_t *b, void *base, unsigned int length); 228 /*!< 229 * \brief Make 'b' refer to the 'length'-byte region starting at base. 230 * 231 * Requires: 232 * 233 *\li 'length' > 0 234 * 235 *\li 'base' is a pointer to a sequence of 'length' bytes. 236 * 237 */ 238 239 void 240 isc__buffer_initnull(isc_buffer_t *b); 241 /*!< 242 *\brief Initialize a buffer 'b' with a null data and zero length/ 243 */ 244 245 void 246 isc_buffer_reinit(isc_buffer_t *b, void *base, unsigned int length); 247 /*!< 248 * \brief Make 'b' refer to the 'length'-byte region starting at base. 249 * Any existing data will be copied. 250 * 251 * Requires: 252 * 253 *\li 'length' > 0 AND length >= previous length 254 * 255 *\li 'base' is a pointer to a sequence of 'length' bytes. 256 * 257 */ 258 259 void 260 isc__buffer_invalidate(isc_buffer_t *b); 261 /*!< 262 * \brief Make 'b' an invalid buffer. 263 * 264 * Requires: 265 *\li 'b' is a valid buffer. 266 * 267 * Ensures: 268 *\li If assertion checking is enabled, future attempts to use 'b' without 269 * calling isc_buffer_init() on it will cause an assertion failure. 270 */ 271 272 void 273 isc__buffer_region(isc_buffer_t *b, isc_region_t *r); 274 /*!< 275 * \brief Make 'r' refer to the region of 'b'. 276 * 277 * Requires: 278 * 279 *\li 'b' is a valid buffer. 280 * 281 *\li 'r' points to a region structure. 282 */ 283 284 void 285 isc__buffer_usedregion(isc_buffer_t *b, isc_region_t *r); 286 /*!< 287 * \brief Make 'r' refer to the used region of 'b'. 288 * 289 * Requires: 290 * 291 *\li 'b' is a valid buffer. 292 * 293 *\li 'r' points to a region structure. 294 */ 295 296 void 297 isc__buffer_availableregion(isc_buffer_t *b, isc_region_t *r); 298 /*!< 299 * \brief Make 'r' refer to the available region of 'b'. 300 * 301 * Requires: 302 * 303 *\li 'b' is a valid buffer. 304 * 305 *\li 'r' points to a region structure. 306 */ 307 308 void 309 isc__buffer_add(isc_buffer_t *b, unsigned int n); 310 /*!< 311 * \brief Increase the 'used' region of 'b' by 'n' bytes. 312 * 313 * Requires: 314 * 315 *\li 'b' is a valid buffer 316 * 317 *\li used + n <= length 318 * 319 */ 320 321 void 322 isc__buffer_subtract(isc_buffer_t *b, unsigned int n); 323 /*!< 324 * \brief Decrease the 'used' region of 'b' by 'n' bytes. 325 * 326 * Requires: 327 * 328 *\li 'b' is a valid buffer 329 * 330 *\li used >= n 331 * 332 */ 333 334 void 335 isc__buffer_clear(isc_buffer_t *b); 336 /*!< 337 * \brief Make the used region empty. 338 * 339 * Requires: 340 * 341 *\li 'b' is a valid buffer 342 * 343 * Ensures: 344 * 345 *\li used = 0 346 * 347 */ 348 349 void 350 isc__buffer_consumedregion(isc_buffer_t *b, isc_region_t *r); 351 /*!< 352 * \brief Make 'r' refer to the consumed region of 'b'. 353 * 354 * Requires: 355 * 356 *\li 'b' is a valid buffer. 357 * 358 *\li 'r' points to a region structure. 359 */ 360 361 void 362 isc__buffer_remainingregion(isc_buffer_t *b, isc_region_t *r); 363 /*!< 364 * \brief Make 'r' refer to the remaining region of 'b'. 365 * 366 * Requires: 367 * 368 *\li 'b' is a valid buffer. 369 * 370 *\li 'r' points to a region structure. 371 */ 372 373 void 374 isc__buffer_activeregion(isc_buffer_t *b, isc_region_t *r); 375 /*!< 376 * \brief Make 'r' refer to the active region of 'b'. 377 * 378 * Requires: 379 * 380 *\li 'b' is a valid buffer. 381 * 382 *\li 'r' points to a region structure. 383 */ 384 385 void 386 isc__buffer_setactive(isc_buffer_t *b, unsigned int n); 387 /*!< 388 * \brief Sets the end of the active region 'n' bytes after current. 389 * 390 * Requires: 391 * 392 *\li 'b' is a valid buffer. 393 * 394 *\li current + n <= used 395 */ 396 397 void 398 isc__buffer_first(isc_buffer_t *b); 399 /*!< 400 * \brief Make the consumed region empty. 401 * 402 * Requires: 403 * 404 *\li 'b' is a valid buffer 405 * 406 * Ensures: 407 * 408 *\li current == 0 409 * 410 */ 411 412 void 413 isc__buffer_forward(isc_buffer_t *b, unsigned int n); 414 /*!< 415 * \brief Increase the 'consumed' region of 'b' by 'n' bytes. 416 * 417 * Requires: 418 * 419 *\li 'b' is a valid buffer 420 * 421 *\li current + n <= used 422 * 423 */ 424 425 void 426 isc__buffer_back(isc_buffer_t *b, unsigned int n); 427 /*!< 428 * \brief Decrease the 'consumed' region of 'b' by 'n' bytes. 429 * 430 * Requires: 431 * 432 *\li 'b' is a valid buffer 433 * 434 *\li n <= current 435 * 436 */ 437 438 void 439 isc_buffer_compact(isc_buffer_t *b); 440 /*!< 441 * \brief Compact the used region by moving the remaining region so it occurs 442 * at the start of the buffer. The used region is shrunk by the size of 443 * the consumed region, and the consumed region is then made empty. 444 * 445 * Requires: 446 * 447 *\li 'b' is a valid buffer 448 * 449 * Ensures: 450 * 451 *\li current == 0 452 * 453 *\li The size of the used region is now equal to the size of the remaining 454 * region (as it was before the call). The contents of the used region 455 * are those of the remaining region (as it was before the call). 456 */ 457 458 isc_uint8_t 459 isc_buffer_getuint8(isc_buffer_t *b); 460 /*!< 461 * \brief Read an unsigned 8-bit integer from 'b' and return it. 462 * 463 * Requires: 464 * 465 *\li 'b' is a valid buffer. 466 * 467 *\li The length of the available region of 'b' is at least 1. 468 * 469 * Ensures: 470 * 471 *\li The current pointer in 'b' is advanced by 1. 472 * 473 * Returns: 474 * 475 *\li A 8-bit unsigned integer. 476 */ 477 478 void 479 isc__buffer_putuint8(isc_buffer_t *b, isc_uint8_t val); 480 /*!< 481 * \brief Store an unsigned 8-bit integer from 'val' into 'b'. 482 * 483 * Requires: 484 *\li 'b' is a valid buffer. 485 * 486 *\li The length of the unused region of 'b' is at least 1. 487 * 488 * Ensures: 489 *\li The used pointer in 'b' is advanced by 1. 490 */ 491 492 isc_uint16_t 493 isc_buffer_getuint16(isc_buffer_t *b); 494 /*!< 495 * \brief Read an unsigned 16-bit integer in network byte order from 'b', convert 496 * it to host byte order, and return it. 497 * 498 * Requires: 499 * 500 *\li 'b' is a valid buffer. 501 * 502 *\li The length of the available region of 'b' is at least 2. 503 * 504 * Ensures: 505 * 506 *\li The current pointer in 'b' is advanced by 2. 507 * 508 * Returns: 509 * 510 *\li A 16-bit unsigned integer. 511 */ 512 513 void 514 isc__buffer_putuint16(isc_buffer_t *b, isc_uint16_t val); 515 /*!< 516 * \brief Store an unsigned 16-bit integer in host byte order from 'val' 517 * into 'b' in network byte order. 518 * 519 * Requires: 520 *\li 'b' is a valid buffer. 521 * 522 *\li The length of the unused region of 'b' is at least 2. 523 * 524 * Ensures: 525 *\li The used pointer in 'b' is advanced by 2. 526 */ 527 528 isc_uint32_t 529 isc_buffer_getuint32(isc_buffer_t *b); 530 /*!< 531 * \brief Read an unsigned 32-bit integer in network byte order from 'b', convert 532 * it to host byte order, and return it. 533 * 534 * Requires: 535 * 536 *\li 'b' is a valid buffer. 537 * 538 *\li The length of the available region of 'b' is at least 4. 539 * 540 * Ensures: 541 * 542 *\li The current pointer in 'b' is advanced by 4. 543 * 544 * Returns: 545 * 546 *\li A 32-bit unsigned integer. 547 */ 548 549 void 550 isc__buffer_putuint32(isc_buffer_t *b, isc_uint32_t val); 551 /*!< 552 * \brief Store an unsigned 32-bit integer in host byte order from 'val' 553 * into 'b' in network byte order. 554 * 555 * Requires: 556 *\li 'b' is a valid buffer. 557 * 558 *\li The length of the unused region of 'b' is at least 4. 559 * 560 * Ensures: 561 *\li The used pointer in 'b' is advanced by 4. 562 */ 563 564 isc_uint64_t 565 isc_buffer_getuint48(isc_buffer_t *b); 566 /*!< 567 * \brief Read an unsigned 48-bit integer in network byte order from 'b', 568 * convert it to host byte order, and return it. 569 * 570 * Requires: 571 * 572 *\li 'b' is a valid buffer. 573 * 574 *\li The length of the available region of 'b' is at least 6. 575 * 576 * Ensures: 577 * 578 *\li The current pointer in 'b' is advanced by 6. 579 * 580 * Returns: 581 * 582 *\li A 48-bit unsigned integer (stored in a 64-bit integer). 583 */ 584 585 void 586 isc__buffer_putuint48(isc_buffer_t *b, isc_uint64_t val); 587 /*!< 588 * \brief Store an unsigned 48-bit integer in host byte order from 'val' 589 * into 'b' in network byte order. 590 * 591 * Requires: 592 *\li 'b' is a valid buffer. 593 * 594 *\li The length of the unused region of 'b' is at least 6. 595 * 596 * Ensures: 597 *\li The used pointer in 'b' is advanced by 6. 598 */ 599 600 void 601 isc__buffer_putuint24(isc_buffer_t *b, isc_uint32_t val); 602 /*!< 603 * Store an unsigned 24-bit integer in host byte order from 'val' 604 * into 'b' in network byte order. 605 * 606 * Requires: 607 *\li 'b' is a valid buffer. 608 * 609 * The length of the unused region of 'b' is at least 3. 610 * 611 * Ensures: 612 *\li The used pointer in 'b' is advanced by 3. 613 */ 614 615 void 616 isc__buffer_putmem(isc_buffer_t *b, const unsigned char *base, 617 unsigned int length); 618 /*!< 619 * \brief Copy 'length' bytes of memory at 'base' into 'b'. 620 * 621 * Requires: 622 *\li 'b' is a valid buffer. 623 * 624 *\li 'base' points to 'length' bytes of valid memory. 625 * 626 */ 627 628 void 629 isc__buffer_putstr(isc_buffer_t *b, const char *source); 630 /*!< 631 * \brief Copy 'source' into 'b', not including terminating NUL. 632 * 633 * Requires: 634 *\li 'b' is a valid buffer. 635 * 636 *\li 'source' to be a valid NULL terminated string. 637 * 638 *\li strlen(source) <= isc_buffer_available(b) 639 */ 640 641 isc_result_t 642 isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r); 643 /*!< 644 * \brief Copy the contents of 'r' into 'b'. 645 * 646 * Requires: 647 *\li 'b' is a valid buffer. 648 * 649 *\li 'r' is a valid region. 650 * 651 * Returns: 652 * 653 *\li ISC_R_SUCCESS 654 *\li ISC_R_NOSPACE The available region of 'b' is not 655 * big enough. 656 */ 657 658 ISC_LANG_ENDDECLS 659 660 /* 661 * Inline macro versions of the functions. These should never be called 662 * directly by an application, but will be used by the functions within 663 * buffer.c. The callers should always use "isc_buffer_*()" names, never 664 * ones beginning with "isc__" 665 */ 666 667 /*! \note 668 * XXXDCL Something more could be done with initializing buffers that 669 * point to const data. For example, isc_buffer_constinit() could 670 * set a new boolean flag in the buffer structure indicating whether 671 * the buffer was initialized with that function. * Then if the 672 * boolean were true, the isc_buffer_put* functions could assert a 673 * contractual requirement for a non-const buffer. 674 * 675 * One drawback is that the isc_buffer_* functions (macros) that return 676 * pointers would still need to return non-const pointers to avoid compiler 677 * warnings, so it would be up to code that uses them to have to deal 678 * with the possibility that the buffer was initialized as const -- 679 * a problem that they *already* have to deal with but have absolutely 680 * no ability to. With a new isc_buffer_isconst() function returning 681 * true/false, they could at least assert a contractual requirement for 682 * non-const buffers when needed. 683 */ 684 #define ISC__BUFFER_INIT(_b, _base, _length) \ 685 do { \ 686 (_b)->base = _base; \ 687 (_b)->length = (_length); \ 688 (_b)->used = 0; \ 689 (_b)->current = 0; \ 690 (_b)->active = 0; \ 691 (_b)->mctx = NULL; \ 692 ISC_LINK_INIT(_b, link); \ 693 (_b)->magic = ISC_BUFFER_MAGIC; \ 694 } while (/*CONSTCOND*/0) 695 696 #define ISC__BUFFER_INITNULL(_b) ISC__BUFFER_INIT(_b, NULL, 0) 697 698 #define ISC__BUFFER_INVALIDATE(_b) \ 699 do { \ 700 (_b)->magic = 0; \ 701 (_b)->base = NULL; \ 702 (_b)->length = 0; \ 703 (_b)->used = 0; \ 704 (_b)->current = 0; \ 705 (_b)->active = 0; \ 706 } while (/*CONSTCOND*/0) 707 708 #define ISC__BUFFER_REGION(_b, _r) \ 709 do { \ 710 (_r)->base = (_b)->base; \ 711 (_r)->length = (_b)->length; \ 712 } while (/*CONSTCOND*/0) 713 714 #define ISC__BUFFER_USEDREGION(_b, _r) \ 715 do { \ 716 (_r)->base = (_b)->base; \ 717 (_r)->length = (_b)->used; \ 718 } while (/*CONSTCOND*/0) 719 720 #define ISC__BUFFER_AVAILABLEREGION(_b, _r) \ 721 do { \ 722 (_r)->base = isc_buffer_used(_b); \ 723 (_r)->length = isc_buffer_availablelength(_b); \ 724 } while (/*CONSTCOND*/0) 725 726 #define ISC__BUFFER_ADD(_b, _n) \ 727 do { \ 728 (_b)->used += (_n); \ 729 } while (/*CONSTCOND*/0) 730 731 #define ISC__BUFFER_SUBTRACT(_b, _n) \ 732 do { \ 733 (_b)->used -= (_n); \ 734 if ((_b)->current > (_b)->used) \ 735 (_b)->current = (_b)->used; \ 736 if ((_b)->active > (_b)->used) \ 737 (_b)->active = (_b)->used; \ 738 } while (/*CONSTCOND*/0) 739 740 #define ISC__BUFFER_CLEAR(_b) \ 741 do { \ 742 (_b)->used = 0; \ 743 (_b)->current = 0; \ 744 (_b)->active = 0; \ 745 } while (/*CONSTCOND*/0) 746 747 #define ISC__BUFFER_CONSUMEDREGION(_b, _r) \ 748 do { \ 749 (_r)->base = (_b)->base; \ 750 (_r)->length = (_b)->current; \ 751 } while (/*CONSTCOND*/0) 752 753 #define ISC__BUFFER_REMAININGREGION(_b, _r) \ 754 do { \ 755 (_r)->base = isc_buffer_current(_b); \ 756 (_r)->length = isc_buffer_remaininglength(_b); \ 757 } while (/*CONSTCOND*/0) 758 759 #define ISC__BUFFER_ACTIVEREGION(_b, _r) \ 760 do { \ 761 if ((_b)->current < (_b)->active) { \ 762 (_r)->base = isc_buffer_current(_b); \ 763 (_r)->length = isc_buffer_activelength(_b); \ 764 } else { \ 765 (_r)->base = NULL; \ 766 (_r)->length = 0; \ 767 } \ 768 } while (/*CONSTCOND*/0) 769 770 #define ISC__BUFFER_SETACTIVE(_b, _n) \ 771 do { \ 772 (_b)->active = (_b)->current + (_n); \ 773 } while (/*CONSTCOND*/0) 774 775 #define ISC__BUFFER_FIRST(_b) \ 776 do { \ 777 (_b)->current = 0; \ 778 } while (/*CONSTCOND*/0) 779 780 #define ISC__BUFFER_FORWARD(_b, _n) \ 781 do { \ 782 (_b)->current += (_n); \ 783 } while (/*CONSTCOND*/0) 784 785 #define ISC__BUFFER_BACK(_b, _n) \ 786 do { \ 787 (_b)->current -= (_n); \ 788 } while (/*CONSTCOND*/0) 789 790 #define ISC__BUFFER_PUTMEM(_b, _base, _length) \ 791 do { \ 792 memmove(isc_buffer_used(_b), (_base), (_length)); \ 793 (_b)->used += (_length); \ 794 } while (/*CONSTCOND*/0) 795 796 #define ISC__BUFFER_PUTSTR(_b, _source) \ 797 do { \ 798 unsigned int _length; \ 799 unsigned char *_cp; \ 800 _length = strlen(_source); \ 801 _cp = isc_buffer_used(_b); \ 802 memmove(_cp, (_source), _length); \ 803 (_b)->used += (_length); \ 804 } while (/*CONSTCOND*/0) 805 806 #define ISC__BUFFER_PUTUINT8(_b, _val) \ 807 do { \ 808 unsigned char *_cp; \ 809 isc_uint8_t _val2 = (_val); \ 810 _cp = isc_buffer_used(_b); \ 811 (_b)->used++; \ 812 _cp[0] = _val2 & 0x00ff; \ 813 } while (/*CONSTCOND*/0) 814 815 #define ISC__BUFFER_PUTUINT16(_b, _val) \ 816 do { \ 817 unsigned char *_cp; \ 818 isc_uint16_t _val2 = (_val); \ 819 _cp = isc_buffer_used(_b); \ 820 (_b)->used += 2; \ 821 _cp[0] = (unsigned char)((_val2 & 0xff00U) >> 8); \ 822 _cp[1] = (unsigned char)(_val2 & 0x00ffU); \ 823 } while (/*CONSTCOND*/0) 824 825 #define ISC__BUFFER_PUTUINT24(_b, _val) \ 826 do { \ 827 unsigned char *_cp; \ 828 isc_uint32_t _val2 = (_val); \ 829 _cp = isc_buffer_used(_b); \ 830 (_b)->used += 3; \ 831 _cp[0] = (unsigned char)((_val2 & 0xff0000U) >> 16); \ 832 _cp[1] = (unsigned char)((_val2 & 0xff00U) >> 8); \ 833 _cp[2] = (unsigned char)(_val2 & 0x00ffU); \ 834 } while (/*CONSTCOND*/0) 835 836 #define ISC__BUFFER_PUTUINT32(_b, _val) \ 837 do { \ 838 unsigned char *_cp; \ 839 isc_uint32_t _val2 = (_val); \ 840 _cp = isc_buffer_used(_b); \ 841 (_b)->used += 4; \ 842 _cp[0] = (unsigned char)((_val2 & 0xff000000) >> 24); \ 843 _cp[1] = (unsigned char)((_val2 & 0x00ff0000) >> 16); \ 844 _cp[2] = (unsigned char)((_val2 & 0x0000ff00) >> 8); \ 845 _cp[3] = (unsigned char)((_val2 & 0x000000ff)); \ 846 } while (/*CONSTCOND*/0) 847 848 #if defined(ISC_BUFFER_USEINLINE) 849 #define isc_buffer_init ISC__BUFFER_INIT 850 #define isc_buffer_initnull ISC__BUFFER_INITNULL 851 #define isc_buffer_invalidate ISC__BUFFER_INVALIDATE 852 #define isc_buffer_region ISC__BUFFER_REGION 853 #define isc_buffer_usedregion ISC__BUFFER_USEDREGION 854 #define isc_buffer_availableregion ISC__BUFFER_AVAILABLEREGION 855 #define isc_buffer_add ISC__BUFFER_ADD 856 #define isc_buffer_subtract ISC__BUFFER_SUBTRACT 857 #define isc_buffer_clear ISC__BUFFER_CLEAR 858 #define isc_buffer_consumedregion ISC__BUFFER_CONSUMEDREGION 859 #define isc_buffer_remainingregion ISC__BUFFER_REMAININGREGION 860 #define isc_buffer_activeregion ISC__BUFFER_ACTIVEREGION 861 #define isc_buffer_setactive ISC__BUFFER_SETACTIVE 862 #define isc_buffer_first ISC__BUFFER_FIRST 863 #define isc_buffer_forward ISC__BUFFER_FORWARD 864 #define isc_buffer_back ISC__BUFFER_BACK 865 #define isc_buffer_putmem ISC__BUFFER_PUTMEM 866 #define isc_buffer_putstr ISC__BUFFER_PUTSTR 867 #define isc_buffer_putuint8 ISC__BUFFER_PUTUINT8 868 #define isc_buffer_putuint16 ISC__BUFFER_PUTUINT16 869 #define isc_buffer_putuint24 ISC__BUFFER_PUTUINT24 870 #define isc_buffer_putuint32 ISC__BUFFER_PUTUINT32 871 #else 872 #define isc_buffer_init isc__buffer_init 873 #define isc_buffer_initnull isc__buffer_initnull 874 #define isc_buffer_invalidate isc__buffer_invalidate 875 #define isc_buffer_region isc__buffer_region 876 #define isc_buffer_usedregion isc__buffer_usedregion 877 #define isc_buffer_availableregion isc__buffer_availableregion 878 #define isc_buffer_add isc__buffer_add 879 #define isc_buffer_subtract isc__buffer_subtract 880 #define isc_buffer_clear isc__buffer_clear 881 #define isc_buffer_consumedregion isc__buffer_consumedregion 882 #define isc_buffer_remainingregion isc__buffer_remainingregion 883 #define isc_buffer_activeregion isc__buffer_activeregion 884 #define isc_buffer_setactive isc__buffer_setactive 885 #define isc_buffer_first isc__buffer_first 886 #define isc_buffer_forward isc__buffer_forward 887 #define isc_buffer_back isc__buffer_back 888 #define isc_buffer_putmem isc__buffer_putmem 889 #define isc_buffer_putstr isc__buffer_putstr 890 #define isc_buffer_putuint8 isc__buffer_putuint8 891 #define isc_buffer_putuint16 isc__buffer_putuint16 892 #define isc_buffer_putuint24 isc__buffer_putuint24 893 #define isc_buffer_putuint32 isc__buffer_putuint32 894 #endif 895 896 #define isc_buffer_constinit(_b, _d, _l) \ 897 do { \ 898 union { void *_var; const void *_const; } _deconst; \ 899 _deconst._const = (_d); \ 900 isc_buffer_init((_b), _deconst._var, (_l)); \ 901 } while (/*CONSTCOND*/0) 902 903 /* 904 * No inline method for this one (yet). 905 */ 906 #define isc_buffer_putuint48 isc__buffer_putuint48 907 908 #endif /* ISC_BUFFER_H */ 909