2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2002 Internet Software Consortium.
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.
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.
18 /* $Id: buffer.h,v 1.43.18.2 2005/04/29 00:16:53 marka Exp $ */
21 #define ISC_BUFFER_H 1
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.
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.
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.
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.
54 * /------------entire length---------------\
55 * /----- used region -----\/-- available --\
56 * +----------------------------------------+
57 * | consumed | remaining | |
58 * +----------------------------------------+
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.
65 * e == length of buffer.
67 * a-e == entire length of buffer.
69 * a-b == consumed region.
70 * b-d == remaining region.
71 * b-c == optional active region.
74 * The following invariants are maintained by all routines:
79 * base is a valid pointer to length bytes of memory
83 * 0 <= current <= used
86 * (although active < current implies empty active region)
90 * Buffers have no synchronization. Clients must ensure exclusive
94 * No anticipated impact.
97 * Memory: 1 pointer + 6 unsigned integers per buffer.
100 * No anticipated impact.
110 #include <isc/lang.h>
111 #include <isc/magic.h>
112 #include <isc/types.h>
115 * To make many functions be inline macros (via #define) define this.
116 * If it is undefined, a function will be used.
118 /* #define ISC_BUFFER_USEINLINE */
126 #define ISC_BUFFER_MAGIC 0x42756621U /* Buf!. */
127 #define ISC_BUFFER_VALID(b) ISC_MAGIC_VALID(b, ISC_BUFFER_MAGIC)
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
139 * Fundamental buffer elements. (A through E in the introductory comment.)
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*/
153 * Derived lengths. (Described in the introductory comment.)
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 */
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.
172 /*! The following integers are byte offsets from 'base'. */
175 unsigned int current;
179 ISC_LINK(isc_buffer_t) link;
180 /*! private internal elements */
189 isc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
190 unsigned int length);
192 * \brief Allocate a dynamic linkable buffer which has "length" bytes in the
196 *\li "mctx" is valid.
198 *\li "dynbuffer" is non-NULL, and "*dynbuffer" is NULL.
201 *\li ISC_R_SUCCESS - success
202 *\li ISC_R_NOMEMORY - no memory available
205 *\li Changing the buffer's length field is not permitted.
209 isc_buffer_free(isc_buffer_t **dynbuffer);
211 * \brief Release resources allocated for a dynamic buffer.
214 *\li "dynbuffer" is not NULL.
216 *\li "*dynbuffer" is a valid dynamic buffer.
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().
225 isc__buffer_init(isc_buffer_t *b, const void *base, unsigned int length);
227 * \brief Make 'b' refer to the 'length'-byte region starting at base.
233 *\li 'base' is a pointer to a sequence of 'length' bytes.
238 isc__buffer_invalidate(isc_buffer_t *b);
240 * \brief Make 'b' an invalid buffer.
243 *\li 'b' is a valid buffer.
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.
251 isc__buffer_region(isc_buffer_t *b, isc_region_t *r);
253 * \brief Make 'r' refer to the region of 'b'.
257 *\li 'b' is a valid buffer.
259 *\li 'r' points to a region structure.
263 isc__buffer_usedregion(isc_buffer_t *b, isc_region_t *r);
265 * \brief Make 'r' refer to the used region of 'b'.
269 *\li 'b' is a valid buffer.
271 *\li 'r' points to a region structure.
275 isc__buffer_availableregion(isc_buffer_t *b, isc_region_t *r);
277 * \brief Make 'r' refer to the available region of 'b'.
281 *\li 'b' is a valid buffer.
283 *\li 'r' points to a region structure.
287 isc__buffer_add(isc_buffer_t *b, unsigned int n);
289 * \brief Increase the 'used' region of 'b' by 'n' bytes.
293 *\li 'b' is a valid buffer
295 *\li used + n <= length
300 isc__buffer_subtract(isc_buffer_t *b, unsigned int n);
302 * \brief Decrease the 'used' region of 'b' by 'n' bytes.
306 *\li 'b' is a valid buffer
313 isc__buffer_clear(isc_buffer_t *b);
315 * \brief Make the used region empty.
319 *\li 'b' is a valid buffer
328 isc__buffer_consumedregion(isc_buffer_t *b, isc_region_t *r);
330 * \brief Make 'r' refer to the consumed region of 'b'.
334 *\li 'b' is a valid buffer.
336 *\li 'r' points to a region structure.
340 isc__buffer_remainingregion(isc_buffer_t *b, isc_region_t *r);
342 * \brief Make 'r' refer to the remaining region of 'b'.
346 *\li 'b' is a valid buffer.
348 *\li 'r' points to a region structure.
352 isc__buffer_activeregion(isc_buffer_t *b, isc_region_t *r);
354 * \brief Make 'r' refer to the active region of 'b'.
358 *\li 'b' is a valid buffer.
360 *\li 'r' points to a region structure.
364 isc__buffer_setactive(isc_buffer_t *b, unsigned int n);
366 * \brief Sets the end of the active region 'n' bytes after current.
370 *\li 'b' is a valid buffer.
372 *\li current + n <= used
376 isc__buffer_first(isc_buffer_t *b);
378 * \brief Make the consumed region empty.
382 *\li 'b' is a valid buffer
391 isc__buffer_forward(isc_buffer_t *b, unsigned int n);
393 * \brief Increase the 'consumed' region of 'b' by 'n' bytes.
397 *\li 'b' is a valid buffer
399 *\li current + n <= used
404 isc__buffer_back(isc_buffer_t *b, unsigned int n);
406 * \brief Decrease the 'consumed' region of 'b' by 'n' bytes.
410 *\li 'b' is a valid buffer
417 isc_buffer_compact(isc_buffer_t *b);
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.
425 *\li 'b' is a valid buffer
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).
437 isc_buffer_getuint8(isc_buffer_t *b);
439 * \brief Read an unsigned 8-bit integer from 'b' and return it.
443 *\li 'b' is a valid buffer.
445 *\li The length of the available region of 'b' is at least 1.
449 *\li The current pointer in 'b' is advanced by 1.
453 *\li A 8-bit unsigned integer.
457 isc__buffer_putuint8(isc_buffer_t *b, isc_uint8_t val);
459 * \brief Store an unsigned 8-bit integer from 'val' into 'b'.
462 *\li 'b' is a valid buffer.
464 *\li The length of the unused region of 'b' is at least 1.
467 *\li The used pointer in 'b' is advanced by 1.
471 isc_buffer_getuint16(isc_buffer_t *b);
473 * \brief Read an unsigned 16-bit integer in network byte order from 'b', convert
474 * it to host byte order, and return it.
478 *\li 'b' is a valid buffer.
480 *\li The length of the available region of 'b' is at least 2.
484 *\li The current pointer in 'b' is advanced by 2.
488 *\li A 16-bit unsigned integer.
492 isc__buffer_putuint16(isc_buffer_t *b, isc_uint16_t val);
494 * \brief Store an unsigned 16-bit integer in host byte order from 'val'
495 * into 'b' in network byte order.
498 *\li 'b' is a valid buffer.
500 *\li The length of the unused region of 'b' is at least 2.
503 *\li The used pointer in 'b' is advanced by 2.
507 isc_buffer_getuint32(isc_buffer_t *b);
509 * \brief Read an unsigned 32-bit integer in network byte order from 'b', convert
510 * it to host byte order, and return it.
514 *\li 'b' is a valid buffer.
516 *\li The length of the available region of 'b' is at least 4.
520 *\li The current pointer in 'b' is advanced by 4.
524 *\li A 32-bit unsigned integer.
528 isc__buffer_putuint32(isc_buffer_t *b, isc_uint32_t val);
530 * \brief Store an unsigned 32-bit integer in host byte order from 'val'
531 * into 'b' in network byte order.
534 *\li 'b' is a valid buffer.
536 *\li The length of the unused region of 'b' is at least 4.
539 *\li The used pointer in 'b' is advanced by 4.
543 isc__buffer_putmem(isc_buffer_t *b, const unsigned char *base,
544 unsigned int length);
546 * \brief Copy 'length' bytes of memory at 'base' into 'b'.
549 *\li 'b' is a valid buffer.
551 *\li 'base' points to 'length' bytes of valid memory.
556 isc__buffer_putstr(isc_buffer_t *b, const char *source);
558 * \brief Copy 'source' into 'b', not including terminating NUL.
561 *\li 'b' is a valid buffer.
563 *\li 'source' to be a valid NULL terminated string.
565 *\li strlen(source) <= isc_buffer_available(b)
569 isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r);
571 * \brief Copy the contents of 'r' into 'b'.
574 *\li 'b' is a valid buffer.
576 *\li 'r' is a valid region.
581 *\li ISC_R_NOSPACE The available region of 'b' is not
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__"
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.
611 #define ISC__BUFFER_INIT(_b, _base, _length) \
614 const void * konst; \
617 _u.konst = (_base); \
618 (_b)->base = _u.var; \
619 (_b)->length = (_length); \
624 ISC_LINK_INIT(_b, link); \
625 (_b)->magic = ISC_BUFFER_MAGIC; \
628 #define ISC__BUFFER_INVALIDATE(_b) \
638 #define ISC__BUFFER_REGION(_b, _r) \
640 (_r)->base = (_b)->base; \
641 (_r)->length = (_b)->length; \
644 #define ISC__BUFFER_USEDREGION(_b, _r) \
646 (_r)->base = (_b)->base; \
647 (_r)->length = (_b)->used; \
650 #define ISC__BUFFER_AVAILABLEREGION(_b, _r) \
652 (_r)->base = isc_buffer_used(_b); \
653 (_r)->length = isc_buffer_availablelength(_b); \
656 #define ISC__BUFFER_ADD(_b, _n) \
658 (_b)->used += (_n); \
661 #define ISC__BUFFER_SUBTRACT(_b, _n) \
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; \
670 #define ISC__BUFFER_CLEAR(_b) \
677 #define ISC__BUFFER_CONSUMEDREGION(_b, _r) \
679 (_r)->base = (_b)->base; \
680 (_r)->length = (_b)->current; \
683 #define ISC__BUFFER_REMAININGREGION(_b, _r) \
685 (_r)->base = isc_buffer_current(_b); \
686 (_r)->length = isc_buffer_remaininglength(_b); \
689 #define ISC__BUFFER_ACTIVEREGION(_b, _r) \
691 if ((_b)->current < (_b)->active) { \
692 (_r)->base = isc_buffer_current(_b); \
693 (_r)->length = isc_buffer_activelength(_b); \
700 #define ISC__BUFFER_SETACTIVE(_b, _n) \
702 (_b)->active = (_b)->current + (_n); \
705 #define ISC__BUFFER_FIRST(_b) \
710 #define ISC__BUFFER_FORWARD(_b, _n) \
712 (_b)->current += (_n); \
715 #define ISC__BUFFER_BACK(_b, _n) \
717 (_b)->current -= (_n); \
720 #define ISC__BUFFER_PUTMEM(_b, _base, _length) \
722 memcpy(isc_buffer_used(_b), (_base), (_length)); \
723 (_b)->used += (_length); \
726 #define ISC__BUFFER_PUTSTR(_b, _source) \
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); \
736 #define ISC__BUFFER_PUTUINT8(_b, _val) \
738 unsigned char *_cp; \
739 isc_uint8_t _val2 = (_val); \
740 _cp = isc_buffer_used(_b); \
742 _cp[0] = _val2 & 0x00ff; \
745 #define ISC__BUFFER_PUTUINT16(_b, _val) \
747 unsigned char *_cp; \
748 isc_uint16_t _val2 = (_val); \
749 _cp = isc_buffer_used(_b); \
751 _cp[0] = (unsigned char)((_val2 & 0xff00U) >> 8); \
752 _cp[1] = (unsigned char)(_val2 & 0x00ffU); \
755 #define ISC__BUFFER_PUTUINT32(_b, _val) \
757 unsigned char *_cp; \
758 isc_uint32_t _val2 = (_val); \
759 _cp = isc_buffer_used(_b); \
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)); \
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
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
811 #endif /* ISC_BUFFER_H */