BIND - Update BIND to 9.5.2
[dragonfly.git] / contrib / bind-9.5.2 / lib / isc / include / isc / buffer.h
1 /*
2  * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or 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.51 2007/06/19 23:47:18 tbox Exp $ */
19
20 #ifndef ISC_BUFFER_H
21 #define ISC_BUFFER_H 1
22
23 /*****
24  ***** Module Info
25  *****/
26
27 /*! \file isc/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_initnull(isc_buffer_t *b);
239 /*!<
240  *\brief Initialize a buffer 'b' with a null data and zero length/
241  */
242
243 void
244 isc_buffer_reinit(isc_buffer_t *b, void *base, unsigned int length);
245 /*!<
246  * \brief Make 'b' refer to the 'length'-byte region starting at base.
247  * Any existing data will be copied.
248  *
249  * Requires:
250  *
251  *\li   'length' > 0 AND length >= previous length
252  *
253  *\li   'base' is a pointer to a sequence of 'length' bytes.
254  *
255  */
256
257 void
258 isc__buffer_invalidate(isc_buffer_t *b);
259 /*!<
260  * \brief Make 'b' an invalid buffer.
261  *
262  * Requires:
263  *\li   'b' is a valid buffer.
264  *
265  * Ensures:
266  *\li   If assertion checking is enabled, future attempts to use 'b' without
267  *      calling isc_buffer_init() on it will cause an assertion failure.
268  */
269
270 void
271 isc__buffer_region(isc_buffer_t *b, isc_region_t *r);
272 /*!<
273  * \brief Make 'r' refer to the region of 'b'.
274  *
275  * Requires:
276  *
277  *\li   'b' is a valid buffer.
278  *
279  *\li   'r' points to a region structure.
280  */
281
282 void
283 isc__buffer_usedregion(isc_buffer_t *b, isc_region_t *r);
284 /*!<
285  * \brief Make 'r' refer to the used region of 'b'.
286  *
287  * Requires:
288  *
289  *\li   'b' is a valid buffer.
290  *
291  *\li   'r' points to a region structure.
292  */
293
294 void
295 isc__buffer_availableregion(isc_buffer_t *b, isc_region_t *r);
296 /*!<
297  * \brief Make 'r' refer to the available region of 'b'.
298  *
299  * Requires:
300  *
301  *\li   'b' is a valid buffer.
302  *
303  *\li   'r' points to a region structure.
304  */
305
306 void
307 isc__buffer_add(isc_buffer_t *b, unsigned int n);
308 /*!<
309  * \brief Increase the 'used' region of 'b' by 'n' bytes.
310  *
311  * Requires:
312  *
313  *\li   'b' is a valid buffer
314  *
315  *\li   used + n <= length
316  *
317  */
318
319 void
320 isc__buffer_subtract(isc_buffer_t *b, unsigned int n);
321 /*!<
322  * \brief Decrease the 'used' region of 'b' by 'n' bytes.
323  *
324  * Requires:
325  *
326  *\li   'b' is a valid buffer
327  *
328  *\li   used >= n
329  *
330  */
331
332 void
333 isc__buffer_clear(isc_buffer_t *b);
334 /*!<
335  * \brief Make the used region empty.
336  *
337  * Requires:
338  *
339  *\li   'b' is a valid buffer
340  *
341  * Ensures:
342  *
343  *\li   used = 0
344  *
345  */
346
347 void
348 isc__buffer_consumedregion(isc_buffer_t *b, isc_region_t *r);
349 /*!<
350  * \brief Make 'r' refer to the consumed region of 'b'.
351  *
352  * Requires:
353  *
354  *\li   'b' is a valid buffer.
355  *
356  *\li   'r' points to a region structure.
357  */
358
359 void
360 isc__buffer_remainingregion(isc_buffer_t *b, isc_region_t *r);
361 /*!<
362  * \brief Make 'r' refer to the remaining region of 'b'.
363  *
364  * Requires:
365  *
366  *\li   'b' is a valid buffer.
367  *
368  *\li   'r' points to a region structure.
369  */
370
371 void
372 isc__buffer_activeregion(isc_buffer_t *b, isc_region_t *r);
373 /*!<
374  * \brief Make 'r' refer to the active region of 'b'.
375  *
376  * Requires:
377  *
378  *\li   'b' is a valid buffer.
379  *
380  *\li   'r' points to a region structure.
381  */
382
383 void
384 isc__buffer_setactive(isc_buffer_t *b, unsigned int n);
385 /*!<
386  * \brief Sets the end of the active region 'n' bytes after current.
387  *
388  * Requires:
389  *
390  *\li   'b' is a valid buffer.
391  *
392  *\li   current + n <= used
393  */
394
395 void
396 isc__buffer_first(isc_buffer_t *b);
397 /*!<
398  * \brief Make the consumed region empty.
399  *
400  * Requires:
401  *
402  *\li   'b' is a valid buffer
403  *
404  * Ensures:
405  *
406  *\li   current == 0
407  *
408  */
409
410 void
411 isc__buffer_forward(isc_buffer_t *b, unsigned int n);
412 /*!<
413  * \brief Increase the 'consumed' region of 'b' by 'n' bytes.
414  *
415  * Requires:
416  *
417  *\li   'b' is a valid buffer
418  *
419  *\li   current + n <= used
420  *
421  */
422
423 void
424 isc__buffer_back(isc_buffer_t *b, unsigned int n);
425 /*!<
426  * \brief Decrease the 'consumed' region of 'b' by 'n' bytes.
427  *
428  * Requires:
429  *
430  *\li   'b' is a valid buffer
431  *
432  *\li   n <= current
433  *
434  */
435
436 void
437 isc_buffer_compact(isc_buffer_t *b);
438 /*!<
439  * \brief Compact the used region by moving the remaining region so it occurs
440  * at the start of the buffer.  The used region is shrunk by the size of
441  * the consumed region, and the consumed region is then made empty.
442  *
443  * Requires:
444  *
445  *\li   'b' is a valid buffer
446  *
447  * Ensures:
448  *
449  *\li   current == 0
450  *
451  *\li   The size of the used region is now equal to the size of the remaining
452  *      region (as it was before the call).  The contents of the used region
453  *      are those of the remaining region (as it was before the call).
454  */
455
456 isc_uint8_t
457 isc_buffer_getuint8(isc_buffer_t *b);
458 /*!<
459  * \brief Read an unsigned 8-bit integer from 'b' and return it.
460  *
461  * Requires:
462  *
463  *\li   'b' is a valid buffer.
464  *
465  *\li   The length of the available region of 'b' is at least 1.
466  *
467  * Ensures:
468  *
469  *\li   The current pointer in 'b' is advanced by 1.
470  *
471  * Returns:
472  *
473  *\li   A 8-bit unsigned integer.
474  */
475
476 void
477 isc__buffer_putuint8(isc_buffer_t *b, isc_uint8_t val);
478 /*!<
479  * \brief Store an unsigned 8-bit integer from 'val' into 'b'.
480  *
481  * Requires:
482  *\li   'b' is a valid buffer.
483  *
484  *\li   The length of the unused region of 'b' is at least 1.
485  *
486  * Ensures:
487  *\li   The used pointer in 'b' is advanced by 1.
488  */
489
490 isc_uint16_t
491 isc_buffer_getuint16(isc_buffer_t *b);
492 /*!<
493  * \brief Read an unsigned 16-bit integer in network byte order from 'b', convert
494  * it to host byte order, and return it.
495  *
496  * Requires:
497  *
498  *\li   'b' is a valid buffer.
499  *
500  *\li   The length of the available region of 'b' is at least 2.
501  *
502  * Ensures:
503  *
504  *\li   The current pointer in 'b' is advanced by 2.
505  *
506  * Returns:
507  *
508  *\li   A 16-bit unsigned integer.
509  */
510
511 void
512 isc__buffer_putuint16(isc_buffer_t *b, isc_uint16_t val);
513 /*!<
514  * \brief Store an unsigned 16-bit integer in host byte order from 'val'
515  * into 'b' in network byte order.
516  *
517  * Requires:
518  *\li   'b' is a valid buffer.
519  *
520  *\li   The length of the unused region of 'b' is at least 2.
521  *
522  * Ensures:
523  *\li   The used pointer in 'b' is advanced by 2.
524  */
525
526 isc_uint32_t
527 isc_buffer_getuint32(isc_buffer_t *b);
528 /*!<
529  * \brief Read an unsigned 32-bit integer in network byte order from 'b', convert
530  * it to host byte order, and return it.
531  *
532  * Requires:
533  *
534  *\li   'b' is a valid buffer.
535  *
536  *\li   The length of the available region of 'b' is at least 4.
537  *
538  * Ensures:
539  *
540  *\li   The current pointer in 'b' is advanced by 4.
541  *
542  * Returns:
543  *
544  *\li   A 32-bit unsigned integer.
545  */
546
547 void
548 isc__buffer_putuint32(isc_buffer_t *b, isc_uint32_t val);
549 /*!<
550  * \brief Store an unsigned 32-bit integer in host byte order from 'val'
551  * into 'b' in network byte order.
552  *
553  * Requires:
554  *\li   'b' is a valid buffer.
555  *
556  *\li   The length of the unused region of 'b' is at least 4.
557  *
558  * Ensures:
559  *\li   The used pointer in 'b' is advanced by 4.
560  */
561
562 isc_uint64_t
563 isc_buffer_getuint48(isc_buffer_t *b);
564 /*!<
565  * \brief Read an unsigned 48-bit integer in network byte order from 'b',
566  * convert it to host byte order, and return it.
567  *
568  * Requires:
569  *
570  *\li   'b' is a valid buffer.
571  *
572  *\li   The length of the available region of 'b' is at least 6.
573  *
574  * Ensures:
575  *
576  *\li   The current pointer in 'b' is advanced by 6.
577  *
578  * Returns:
579  *
580  *\li   A 48-bit unsigned integer (stored in a 64-bit integer).
581  */
582
583 void
584 isc__buffer_putuint48(isc_buffer_t *b, isc_uint64_t val);
585 /*!<
586  * \brief Store an unsigned 48-bit integer in host byte order from 'val'
587  * into 'b' in network byte order.
588  *
589  * Requires:
590  *\li   'b' is a valid buffer.
591  *
592  *\li   The length of the unused region of 'b' is at least 6.
593  *
594  * Ensures:
595  *\li   The used pointer in 'b' is advanced by 6.
596  */
597
598 void
599 isc__buffer_putmem(isc_buffer_t *b, const unsigned char *base,
600                    unsigned int length);
601 /*!<
602  * \brief Copy 'length' bytes of memory at 'base' into 'b'.
603  *
604  * Requires:
605  *\li   'b' is a valid buffer.
606  *
607  *\li   'base' points to 'length' bytes of valid memory.
608  *
609  */
610
611 void
612 isc__buffer_putstr(isc_buffer_t *b, const char *source);
613 /*!<
614  * \brief Copy 'source' into 'b', not including terminating NUL.
615  *
616  * Requires:
617  *\li   'b' is a valid buffer.
618  *
619  *\li   'source' to be a valid NULL terminated string.
620  *
621  *\li   strlen(source) <= isc_buffer_available(b)
622  */
623
624 isc_result_t
625 isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r);
626 /*!<
627  * \brief Copy the contents of 'r' into 'b'.
628  *
629  * Requires:
630  *\li   'b' is a valid buffer.
631  *
632  *\li   'r' is a valid region.
633  *
634  * Returns:
635  *
636  *\li   ISC_R_SUCCESS
637  *\li   ISC_R_NOSPACE                   The available region of 'b' is not
638  *                                      big enough.
639  */
640
641 ISC_LANG_ENDDECLS
642
643 /*
644  * Inline macro versions of the functions.  These should never be called
645  * directly by an application, but will be used by the functions within
646  * buffer.c.  The callers should always use "isc_buffer_*()" names, never
647  * ones beginning with "isc__"
648  */
649
650 /*! \note
651  * XXXDCL Something more could be done with initializing buffers that
652  * point to const data.  For example, a new function, isc_buffer_initconst,
653  * could be used, and a new boolean flag in the buffer structure could
654  * indicate whether the buffer was initialized with that function.
655  * (isc_bufer_init itself would be reprototyped to *not* have its "base"
656  * parameter be const.)  Then if the boolean were true, the isc_buffer_put*
657  * functions could assert a contractual requirement for a non-const buffer.
658  * One drawback is that the isc_buffer_* functions (macros) that return
659  * pointers would still need to return non-const pointers to avoid compiler
660  * warnings, so it would be up to code that uses them to have to deal
661  * with the possibility that the buffer was initialized as const --
662  * a problem that they *already* have to deal with but have absolutely
663  * no ability to.  With a new isc_buffer_isconst() function returning
664  * true/false, they could at least assert a contractual requirement for
665  * non-const buffers when needed.
666  */
667 #define ISC__BUFFER_INIT(_b, _base, _length) \
668         do { \
669                 union { \
670                         const void *    konst; \
671                         void *          var; \
672                 } _u; \
673                 _u.konst = (_base); \
674                 (_b)->base = _u.var; \
675                 (_b)->length = (_length); \
676                 (_b)->used = 0; \
677                 (_b)->current = 0; \
678                 (_b)->active = 0; \
679                 (_b)->mctx = NULL; \
680                 ISC_LINK_INIT(_b, link); \
681                 (_b)->magic = ISC_BUFFER_MAGIC; \
682         } while (0)
683
684 #define ISC__BUFFER_INITNULL(_b) ISC__BUFFER_INIT(_b, NULL, 0)
685
686 #define ISC__BUFFER_INVALIDATE(_b) \
687         do { \
688                 (_b)->magic = 0; \
689                 (_b)->base = NULL; \
690                 (_b)->length = 0; \
691                 (_b)->used = 0; \
692                 (_b)->current = 0; \
693                 (_b)->active = 0; \
694         } while (0)
695
696 #define ISC__BUFFER_REGION(_b, _r) \
697         do { \
698                 (_r)->base = (_b)->base; \
699                 (_r)->length = (_b)->length; \
700         } while (0)
701
702 #define ISC__BUFFER_USEDREGION(_b, _r) \
703         do { \
704                 (_r)->base = (_b)->base; \
705                 (_r)->length = (_b)->used; \
706         } while (0)
707
708 #define ISC__BUFFER_AVAILABLEREGION(_b, _r) \
709         do { \
710                 (_r)->base = isc_buffer_used(_b); \
711                 (_r)->length = isc_buffer_availablelength(_b); \
712         } while (0)
713
714 #define ISC__BUFFER_ADD(_b, _n) \
715         do { \
716                 (_b)->used += (_n); \
717         } while (0)
718
719 #define ISC__BUFFER_SUBTRACT(_b, _n) \
720         do { \
721                 (_b)->used -= (_n); \
722                 if ((_b)->current > (_b)->used) \
723                         (_b)->current = (_b)->used; \
724                 if ((_b)->active > (_b)->used) \
725                         (_b)->active = (_b)->used; \
726         } while (0)
727
728 #define ISC__BUFFER_CLEAR(_b) \
729         do { \
730                 (_b)->used = 0; \
731                 (_b)->current = 0; \
732                 (_b)->active = 0; \
733         } while (0)
734
735 #define ISC__BUFFER_CONSUMEDREGION(_b, _r) \
736         do { \
737                 (_r)->base = (_b)->base; \
738                 (_r)->length = (_b)->current; \
739         } while (0)
740
741 #define ISC__BUFFER_REMAININGREGION(_b, _r) \
742         do { \
743                 (_r)->base = isc_buffer_current(_b); \
744                 (_r)->length = isc_buffer_remaininglength(_b); \
745         } while (0)
746
747 #define ISC__BUFFER_ACTIVEREGION(_b, _r) \
748         do { \
749                 if ((_b)->current < (_b)->active) { \
750                         (_r)->base = isc_buffer_current(_b); \
751                         (_r)->length = isc_buffer_activelength(_b); \
752                 } else { \
753                         (_r)->base = NULL; \
754                         (_r)->length = 0; \
755                 } \
756         } while (0)
757
758 #define ISC__BUFFER_SETACTIVE(_b, _n) \
759         do { \
760                 (_b)->active = (_b)->current + (_n); \
761         } while (0)
762
763 #define ISC__BUFFER_FIRST(_b) \
764         do { \
765                 (_b)->current = 0; \
766         } while (0)
767
768 #define ISC__BUFFER_FORWARD(_b, _n) \
769         do { \
770                 (_b)->current += (_n); \
771         } while (0)
772
773 #define ISC__BUFFER_BACK(_b, _n) \
774         do { \
775                 (_b)->current -= (_n); \
776         } while (0)
777
778 #define ISC__BUFFER_PUTMEM(_b, _base, _length) \
779         do { \
780                 memcpy(isc_buffer_used(_b), (_base), (_length)); \
781                 (_b)->used += (_length); \
782         } while (0)
783
784 #define ISC__BUFFER_PUTSTR(_b, _source) \
785         do { \
786                 unsigned int _length; \
787                 unsigned char *_cp; \
788                 _length = strlen(_source); \
789                 _cp = isc_buffer_used(_b); \
790                 memcpy(_cp, (_source), _length); \
791                 (_b)->used += (_length); \
792         } while (0)
793
794 #define ISC__BUFFER_PUTUINT8(_b, _val) \
795         do { \
796                 unsigned char *_cp; \
797                 isc_uint8_t _val2 = (_val); \
798                 _cp = isc_buffer_used(_b); \
799                 (_b)->used++; \
800                 _cp[0] = _val2 & 0x00ff; \
801         } while (0)
802
803 #define ISC__BUFFER_PUTUINT16(_b, _val) \
804         do { \
805                 unsigned char *_cp; \
806                 isc_uint16_t _val2 = (_val); \
807                 _cp = isc_buffer_used(_b); \
808                 (_b)->used += 2; \
809                 _cp[0] = (unsigned char)((_val2 & 0xff00U) >> 8); \
810                 _cp[1] = (unsigned char)(_val2 & 0x00ffU); \
811         } while (0)
812
813 #define ISC__BUFFER_PUTUINT32(_b, _val) \
814         do { \
815                 unsigned char *_cp; \
816                 isc_uint32_t _val2 = (_val); \
817                 _cp = isc_buffer_used(_b); \
818                 (_b)->used += 4; \
819                 _cp[0] = (unsigned char)((_val2 & 0xff000000) >> 24); \
820                 _cp[1] = (unsigned char)((_val2 & 0x00ff0000) >> 16); \
821                 _cp[2] = (unsigned char)((_val2 & 0x0000ff00) >> 8); \
822                 _cp[3] = (unsigned char)((_val2 & 0x000000ff)); \
823         } while (0)
824
825 #if defined(ISC_BUFFER_USEINLINE)
826 #define isc_buffer_init                 ISC__BUFFER_INIT
827 #define isc_buffer_initnull             ISC__BUFFER_INITNULL
828 #define isc_buffer_invalidate           ISC__BUFFER_INVALIDATE
829 #define isc_buffer_region               ISC__BUFFER_REGION
830 #define isc_buffer_usedregion           ISC__BUFFER_USEDREGION
831 #define isc_buffer_availableregion      ISC__BUFFER_AVAILABLEREGION
832 #define isc_buffer_add                  ISC__BUFFER_ADD
833 #define isc_buffer_subtract             ISC__BUFFER_SUBTRACT
834 #define isc_buffer_clear                ISC__BUFFER_CLEAR
835 #define isc_buffer_consumedregion       ISC__BUFFER_CONSUMEDREGION
836 #define isc_buffer_remainingregion      ISC__BUFFER_REMAININGREGION
837 #define isc_buffer_activeregion         ISC__BUFFER_ACTIVEREGION
838 #define isc_buffer_setactive            ISC__BUFFER_SETACTIVE
839 #define isc_buffer_first                ISC__BUFFER_FIRST
840 #define isc_buffer_forward              ISC__BUFFER_FORWARD
841 #define isc_buffer_back                 ISC__BUFFER_BACK
842 #define isc_buffer_putmem               ISC__BUFFER_PUTMEM
843 #define isc_buffer_putstr               ISC__BUFFER_PUTSTR
844 #define isc_buffer_putuint8             ISC__BUFFER_PUTUINT8
845 #define isc_buffer_putuint16            ISC__BUFFER_PUTUINT16
846 #define isc_buffer_putuint32            ISC__BUFFER_PUTUINT32
847 #else
848 #define isc_buffer_init                 isc__buffer_init
849 #define isc_buffer_initnull             isc__buffer_initnull
850 #define isc_buffer_invalidate           isc__buffer_invalidate
851 #define isc_buffer_region               isc__buffer_region
852 #define isc_buffer_usedregion           isc__buffer_usedregion
853 #define isc_buffer_availableregion      isc__buffer_availableregion
854 #define isc_buffer_add                  isc__buffer_add
855 #define isc_buffer_subtract             isc__buffer_subtract
856 #define isc_buffer_clear                isc__buffer_clear
857 #define isc_buffer_consumedregion       isc__buffer_consumedregion
858 #define isc_buffer_remainingregion      isc__buffer_remainingregion
859 #define isc_buffer_activeregion         isc__buffer_activeregion
860 #define isc_buffer_setactive            isc__buffer_setactive
861 #define isc_buffer_first                isc__buffer_first
862 #define isc_buffer_forward              isc__buffer_forward
863 #define isc_buffer_back                 isc__buffer_back
864 #define isc_buffer_putmem               isc__buffer_putmem
865 #define isc_buffer_putstr               isc__buffer_putstr
866 #define isc_buffer_putuint8             isc__buffer_putuint8
867 #define isc_buffer_putuint16            isc__buffer_putuint16
868 #define isc_buffer_putuint32            isc__buffer_putuint32
869 #endif
870
871 /*
872  * No inline method for this one (yet).
873  */
874 #define isc_buffer_putuint48            isc__buffer_putuint48
875
876 #endif /* ISC_BUFFER_H */