home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / isc / buffer.h < prev    next >
Encoding:
C/C++ Source or Header  |  2008-09-17  |  20.2 KB  |  812 lines

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