Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / include / isc / buffer.h
blob45d04e8e25b1062352f53c157aa06b1fb181aaa9
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2002 Internet Software Consortium.
7 * Permission to use, copy, modify, and 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.
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.
20 /* Id: buffer.h,v 1.39.12.2 2004/03/08 09:04:51 marka Exp */
22 #ifndef ISC_BUFFER_H
23 #define ISC_BUFFER_H 1
25 /*****
26 ***** Module Info
27 *****/
30 * Buffers
32 * A buffer is a region of memory, together with a set of related subregions.
33 * Buffers are used for parsing and I/O operations.
35 * The 'used region' and the 'available' region are disjoint, and their
36 * union is the buffer's region. The used region extends from the beginning
37 * of the buffer region to the last used byte. The available region
38 * extends from one byte greater than the last used byte to the end of the
39 * buffer's region. The size of the used region can be changed using various
40 * buffer commands. Initially, the used region is empty.
42 * The used region is further subdivided into two disjoint regions: the
43 * 'consumed region' and the 'remaining region'. The union of these two
44 * regions is the used region. The consumed region extends from the beginning
45 * of the used region to the byte before the 'current' offset (if any). The
46 * 'remaining' region the current pointer to the end of the used
47 * region. The size of the consumed region can be changed using various
48 * buffer commands. Initially, the consumed region is empty.
50 * The 'active region' is an (optional) subregion of the remaining region.
51 * It extends from the current offset to an offset in the remaining region
52 * that is selected with isc_buffer_setactive(). Initially, the active region
53 * is empty. If the current offset advances beyond the chosen offset, the
54 * active region will also be empty.
56 * /------------entire length---------------\
57 * /----- used region -----\/-- available --\
58 * +----------------------------------------+
59 * | consumed | remaining | |
60 * +----------------------------------------+
61 * a b c d e
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.
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.
75 * The following invariants are maintained by all routines:
77 * length > 0
79 * base is a valid pointer to length bytes of memory
81 * 0 <= used <= length
83 * 0 <= current <= used
85 * 0 <= active <= used
86 * (although active < current implies empty active region)
88 * MP:
89 * Buffers have no synchronization. Clients must ensure exclusive
90 * access.
92 * Reliability:
93 * No anticipated impact.
95 * Resources:
96 * Memory: 1 pointer + 6 unsigned integers per buffer.
98 * Security:
99 * No anticipated impact.
101 * Standards:
102 * None.
105 /***
106 *** Imports
107 ***/
109 #include <isc/lang.h>
110 #include <isc/magic.h>
111 #include <isc/types.h>
114 * To make many functions be inline macros (via #define) define this.
115 * If it is undefined, a function will be used.
117 #define ISC_BUFFER_USEINLINE
119 ISC_LANG_BEGINDECLS
121 /***
122 *** Magic numbers
123 ***/
124 #define ISC_BUFFER_MAGIC 0x42756621U /* Buf!. */
125 #define ISC_BUFFER_VALID(b) ISC_MAGIC_VALID(b, ISC_BUFFER_MAGIC)
128 * The following macros MUST be used only on valid buffers. It is the
129 * caller's responsibility to ensure this by using the ISC_BUFFER_VALID
130 * check above, or by calling another isc_buffer_*() function (rather than
131 * another macro.)
135 * Fundamental buffer elements. (A through E in the introductory comment.)
137 #define isc_buffer_base(b) ((void *)(b)->base) /*a*/
138 #define isc_buffer_current(b) \
139 ((void *)((unsigned char *)(b)->base + (b)->current)) /*b*/
140 #define isc_buffer_active(b) \
141 ((void *)((unsigned char *)(b)->base + (b)->active)) /*c*/
142 #define isc_buffer_used(b) \
143 ((void *)((unsigned char *)(b)->base + (b)->used)) /*d*/
144 #define isc_buffer_length(b) ((b)->length) /*e*/
147 * Derived lengths. (Described in the introductory comment.)
149 #define isc_buffer_usedlength(b) ((b)->used) /* d-a */
150 #define isc_buffer_consumedlength(b) ((b)->current) /* b-a */
151 #define isc_buffer_remaininglength(b) ((b)->used - (b)->current) /* d-b */
152 #define isc_buffer_activelength(b) ((b)->active - (b)->current) /* c-b */
153 #define isc_buffer_availablelength(b) ((b)->length - (b)->used) /* e-d */
156 * Note that the buffer structure is public. This is principally so buffer
157 * operations can be implemented using macros. Applications are strongly
158 * discouraged from directly manipulating the structure.
161 struct isc_buffer {
162 unsigned int magic;
163 void *base;
164 /* The following integers are byte offsets from 'base'. */
165 unsigned int length;
166 unsigned int used;
167 unsigned int current;
168 unsigned int active;
169 /* linkable */
170 ISC_LINK(isc_buffer_t) link;
171 /* private internal elements */
172 isc_mem_t *mctx;
175 /***
176 *** Functions
177 ***/
179 isc_result_t
180 isc_buffer_allocate(isc_mem_t *mctx, isc_buffer_t **dynbuffer,
181 unsigned int length);
183 * Allocate a dynamic linkable buffer which has "length" bytes in the
184 * data region.
186 * Requires:
187 * "mctx" is valid.
189 * "dynbuffer" is non-NULL, and "*dynbuffer" is NULL.
191 * Returns:
192 * ISC_R_SUCCESS - success
193 * ISC_R_NOMEMORY - no memory available
195 * Note:
196 * Changing the buffer's length field is not permitted.
199 void
200 isc_buffer_free(isc_buffer_t **dynbuffer);
202 * Release resources allocated for a dynamic buffer.
204 * Requires:
205 * "dynbuffer" is not NULL.
207 * "*dynbuffer" is a valid dynamic buffer.
209 * Ensures:
210 * "*dynbuffer" will be NULL on return, and all memory associated with
211 * the dynamic buffer is returned to the memory context used in
212 * isc_buffer_allocate().
215 void
216 isc__buffer_init(isc_buffer_t *b, const void *base, unsigned int length);
218 * Make 'b' refer to the 'length'-byte region starting at base.
220 * Requires:
222 * 'length' > 0
224 * 'base' is a pointer to a sequence of 'length' bytes.
228 void
229 isc__buffer_invalidate(isc_buffer_t *b);
231 * Make 'b' an invalid buffer.
233 * Requires:
234 * 'b' is a valid buffer.
236 * Ensures:
237 * If assertion checking is enabled, future attempts to use 'b' without
238 * calling isc_buffer_init() on it will cause an assertion failure.
241 void
242 isc__buffer_region(isc_buffer_t *b, isc_region_t *r);
244 * Make 'r' refer to the region of 'b'.
246 * Requires:
248 * 'b' is a valid buffer.
250 * 'r' points to a region structure.
253 void
254 isc__buffer_usedregion(isc_buffer_t *b, isc_region_t *r);
256 * Make 'r' refer to the used region of 'b'.
258 * Requires:
260 * 'b' is a valid buffer.
262 * 'r' points to a region structure.
265 void
266 isc__buffer_availableregion(isc_buffer_t *b, isc_region_t *r);
268 * Make 'r' refer to the available region of 'b'.
270 * Requires:
272 * 'b' is a valid buffer.
274 * 'r' points to a region structure.
277 void
278 isc__buffer_add(isc_buffer_t *b, unsigned int n);
280 * Increase the 'used' region of 'b' by 'n' bytes.
282 * Requires:
284 * 'b' is a valid buffer
286 * used + n <= length
290 void
291 isc__buffer_subtract(isc_buffer_t *b, unsigned int n);
293 * Decrease the 'used' region of 'b' by 'n' bytes.
295 * Requires:
297 * 'b' is a valid buffer
299 * used >= n
303 void
304 isc__buffer_clear(isc_buffer_t *b);
306 * Make the used region empty.
308 * Requires:
310 * 'b' is a valid buffer
312 * Ensures:
314 * used = 0
318 void
319 isc__buffer_consumedregion(isc_buffer_t *b, isc_region_t *r);
321 * Make 'r' refer to the consumed region of 'b'.
323 * Requires:
325 * 'b' is a valid buffer.
327 * 'r' points to a region structure.
330 void
331 isc__buffer_remainingregion(isc_buffer_t *b, isc_region_t *r);
333 * Make 'r' refer to the remaining region of 'b'.
335 * Requires:
337 * 'b' is a valid buffer.
339 * 'r' points to a region structure.
342 void
343 isc__buffer_activeregion(isc_buffer_t *b, isc_region_t *r);
345 * Make 'r' refer to the active region of 'b'.
347 * Requires:
349 * 'b' is a valid buffer.
351 * 'r' points to a region structure.
354 void
355 isc__buffer_setactive(isc_buffer_t *b, unsigned int n);
357 * Sets the end of the active region 'n' bytes after current.
359 * Requires:
361 * 'b' is a valid buffer.
363 * current + n <= used
366 void
367 isc__buffer_first(isc_buffer_t *b);
369 * Make the consumed region empty.
371 * Requires:
373 * 'b' is a valid buffer
375 * Ensures:
377 * current == 0
381 void
382 isc__buffer_forward(isc_buffer_t *b, unsigned int n);
384 * Increase the 'consumed' region of 'b' by 'n' bytes.
386 * Requires:
388 * 'b' is a valid buffer
390 * current + n <= used
394 void
395 isc__buffer_back(isc_buffer_t *b, unsigned int n);
397 * Decrease the 'consumed' region of 'b' by 'n' bytes.
399 * Requires:
401 * 'b' is a valid buffer
403 * n <= current
407 void
408 isc_buffer_compact(isc_buffer_t *b);
410 * Compact the used region by moving the remaining region so it occurs
411 * at the start of the buffer. The used region is shrunk by the size of
412 * the consumed region, and the consumed region is then made empty.
414 * Requires:
416 * 'b' is a valid buffer
418 * Ensures:
420 * current == 0
422 * The size of the used region is now equal to the size of the remaining
423 * region (as it was before the call). The contents of the used region
424 * are those of the remaining region (as it was before the call).
427 isc_uint8_t
428 isc_buffer_getuint8(isc_buffer_t *b);
430 * Read an unsigned 8-bit integer from 'b' and return it.
432 * Requires:
434 * 'b' is a valid buffer.
436 * The length of the available region of 'b' is at least 1.
438 * Ensures:
440 * The current pointer in 'b' is advanced by 1.
442 * Returns:
444 * A 8-bit unsigned integer.
447 void
448 isc__buffer_putuint8(isc_buffer_t *b, isc_uint8_t val);
450 * Store an unsigned 8-bit integer from 'val' into 'b'.
452 * Requires:
453 * 'b' is a valid buffer.
455 * The length of the unused region of 'b' is at least 1.
457 * Ensures:
458 * The used pointer in 'b' is advanced by 1.
461 isc_uint16_t
462 isc_buffer_getuint16(isc_buffer_t *b);
464 * Read an unsigned 16-bit integer in network byte order from 'b', convert
465 * it to host byte order, and return it.
467 * Requires:
469 * 'b' is a valid buffer.
471 * The length of the available region of 'b' is at least 2.
473 * Ensures:
475 * The current pointer in 'b' is advanced by 2.
477 * Returns:
479 * A 16-bit unsigned integer.
482 void
483 isc__buffer_putuint16(isc_buffer_t *b, isc_uint16_t val);
485 * Store an unsigned 16-bit integer in host byte order from 'val'
486 * into 'b' in network byte order.
488 * Requires:
489 * 'b' is a valid buffer.
491 * The length of the unused region of 'b' is at least 2.
493 * Ensures:
494 * The used pointer in 'b' is advanced by 2.
497 isc_uint32_t
498 isc_buffer_getuint32(isc_buffer_t *b);
500 * Read an unsigned 32-bit integer in network byte order from 'b', convert
501 * it to host byte order, and return it.
503 * Requires:
505 * 'b' is a valid buffer.
507 * The length of the available region of 'b' is at least 4.
509 * Ensures:
511 * The current pointer in 'b' is advanced by 4.
513 * Returns:
515 * A 32-bit unsigned integer.
518 void
519 isc__buffer_putuint32(isc_buffer_t *b, isc_uint32_t val);
521 * Store an unsigned 32-bit integer in host byte order from 'val'
522 * into 'b' in network byte order.
524 * Requires:
525 * 'b' is a valid buffer.
527 * The length of the unused region of 'b' is at least 4.
529 * Ensures:
530 * The used pointer in 'b' is advanced by 4.
533 void
534 isc__buffer_putmem(isc_buffer_t *b, const unsigned char *base,
535 unsigned int length);
537 * Copy 'length' bytes of memory at 'base' into 'b'.
539 * Requires:
540 * 'b' is a valid buffer.
542 * 'base' points to 'length' bytes of valid memory.
546 void
547 isc__buffer_putstr(isc_buffer_t *b, const char *source);
549 * Copy 'source' into 'b', not including terminating NUL.
551 * Requires:
552 * 'b' is a valid buffer.
554 * 'source' to be a valid NULL terminated string.
556 * strlen(source) <= isc_buffer_available(b)
559 isc_result_t
560 isc_buffer_copyregion(isc_buffer_t *b, const isc_region_t *r);
562 * Copy the contents of 'r' into 'b'.
564 * Requires:
565 * 'b' is a valid buffer.
567 * 'r' is a valid region.
569 * Returns:
571 * ISC_R_SUCCESS
572 * ISC_R_NOSPACE The available region of 'b' is not
573 * big enough.
576 ISC_LANG_ENDDECLS
579 * Inline macro versions of the functions. These should never be called
580 * directly by an application, but will be used by the functions within
581 * buffer.c. The callers should always use "isc_buffer_*()" names, never
582 * ones beginning with "isc__"
586 * XXXDCL Something more could be done with initializing buffers that
587 * point to const data. For example, a new function, isc_buffer_initconst,
588 * could be used, and a new boolean flag in the buffer structure could
589 * indicate whether the buffer was initialized with that function.
590 * (isc_bufer_init itself would be reprototyped to *not* have its "base"
591 * parameter be const.) Then if the boolean were true, the isc_buffer_put*
592 * functions could assert a contractual requirement for a non-const buffer.
593 * One drawback is that the isc_buffer_* functions (macros) that return
594 * pointers would still need to return non-const pointers to avoid compiler
595 * warnings, so it would be up to code that uses them to have to deal
596 * with the possibility that the buffer was initialized as const --
597 * a problem that they *already* have to deal with but have absolutely
598 * no ability to. With a new isc_buffer_isconst() function returning
599 * true/false, they could at least assert a contractual requirement for
600 * non-const buffers when needed.
602 #define ISC__BUFFER_INIT(_b, _base, _length) \
603 do { \
604 union { \
605 const void * konst; \
606 void * var; \
607 } _u; \
608 _u.konst = (_base); \
609 (_b)->base = _u.var; \
610 (_b)->length = (_length); \
611 (_b)->used = 0; \
612 (_b)->current = 0; \
613 (_b)->active = 0; \
614 (_b)->mctx = NULL; \
615 ISC_LINK_INIT(_b, link); \
616 (_b)->magic = ISC_BUFFER_MAGIC; \
617 } while (0)
619 #define ISC__BUFFER_INVALIDATE(_b) \
620 do { \
621 (_b)->magic = 0; \
622 (_b)->base = NULL; \
623 (_b)->length = 0; \
624 (_b)->used = 0; \
625 (_b)->current = 0; \
626 (_b)->active = 0; \
627 } while (0)
629 #define ISC__BUFFER_REGION(_b, _r) \
630 do { \
631 (_r)->base = (_b)->base; \
632 (_r)->length = (_b)->length; \
633 } while (0)
635 #define ISC__BUFFER_USEDREGION(_b, _r) \
636 do { \
637 (_r)->base = (_b)->base; \
638 (_r)->length = (_b)->used; \
639 } while (0)
641 #define ISC__BUFFER_AVAILABLEREGION(_b, _r) \
642 do { \
643 (_r)->base = isc_buffer_used(_b); \
644 (_r)->length = isc_buffer_availablelength(_b); \
645 } while (0)
647 #define ISC__BUFFER_ADD(_b, _n) \
648 do { \
649 (_b)->used += (_n); \
650 } while (0)
652 #define ISC__BUFFER_SUBTRACT(_b, _n) \
653 do { \
654 (_b)->used -= (_n); \
655 if ((_b)->current > (_b)->used) \
656 (_b)->current = (_b)->used; \
657 if ((_b)->active > (_b)->used) \
658 (_b)->active = (_b)->used; \
659 } while (0)
661 #define ISC__BUFFER_CLEAR(_b) \
662 do { \
663 (_b)->used = 0; \
664 (_b)->current = 0; \
665 (_b)->active = 0; \
666 } while (0)
668 #define ISC__BUFFER_CONSUMEDREGION(_b, _r) \
669 do { \
670 (_r)->base = (_b)->base; \
671 (_r)->length = (_b)->current; \
672 } while (0)
674 #define ISC__BUFFER_REMAININGREGION(_b, _r) \
675 do { \
676 (_r)->base = isc_buffer_current(_b); \
677 (_r)->length = isc_buffer_remaininglength(_b); \
678 } while (0)
680 #define ISC__BUFFER_ACTIVEREGION(_b, _r) \
681 do { \
682 if ((_b)->current < (_b)->active) { \
683 (_r)->base = isc_buffer_current(_b); \
684 (_r)->length = isc_buffer_activelength(_b); \
685 } else { \
686 (_r)->base = NULL; \
687 (_r)->length = 0; \
689 } while (0)
691 #define ISC__BUFFER_SETACTIVE(_b, _n) \
692 do { \
693 (_b)->active = (_b)->current + (_n); \
694 } while (0)
696 #define ISC__BUFFER_FIRST(_b) \
697 do { \
698 (_b)->current = 0; \
699 } while (0)
701 #define ISC__BUFFER_FORWARD(_b, _n) \
702 do { \
703 (_b)->current += (_n); \
704 } while (0)
706 #define ISC__BUFFER_BACK(_b, _n) \
707 do { \
708 (_b)->current -= (_n); \
709 } while (0)
711 #define ISC__BUFFER_PUTMEM(_b, _base, _length) \
712 do { \
713 memcpy(isc_buffer_used(_b), (_base), (_length)); \
714 (_b)->used += (_length); \
715 } while (0)
717 #define ISC__BUFFER_PUTSTR(_b, _source) \
718 do { \
719 unsigned int _length; \
720 unsigned char *_cp; \
721 _length = strlen(_source); \
722 _cp = isc_buffer_used(_b); \
723 memcpy(_cp, (_source), _length); \
724 (_b)->used += (_length); \
725 } while (0)
727 #define ISC__BUFFER_PUTUINT8(_b, _val) \
728 do { \
729 unsigned char *_cp; \
730 isc_uint8_t _val2 = (_val); \
731 _cp = isc_buffer_used(_b); \
732 (_b)->used++; \
733 _cp[0] = _val2 & 0x00ff; \
734 } while (0)
736 #define ISC__BUFFER_PUTUINT16(_b, _val) \
737 do { \
738 unsigned char *_cp; \
739 isc_uint16_t _val2 = (_val); \
740 _cp = isc_buffer_used(_b); \
741 (_b)->used += 2; \
742 _cp[0] = (unsigned char)((_val2 & 0xff00U) >> 8); \
743 _cp[1] = (unsigned char)(_val2 & 0x00ffU); \
744 } while (0)
746 #define ISC__BUFFER_PUTUINT32(_b, _val) \
747 do { \
748 unsigned char *_cp; \
749 isc_uint32_t _val2 = (_val); \
750 _cp = isc_buffer_used(_b); \
751 (_b)->used += 4; \
752 _cp[0] = (unsigned char)((_val2 & 0xff000000) >> 24); \
753 _cp[1] = (unsigned char)((_val2 & 0x00ff0000) >> 16); \
754 _cp[2] = (unsigned char)((_val2 & 0x0000ff00) >> 8); \
755 _cp[3] = (unsigned char)((_val2 & 0x000000ff)); \
756 } while (0)
758 #if defined(ISC_BUFFER_USEINLINE)
759 #define isc_buffer_init ISC__BUFFER_INIT
760 #define isc_buffer_invalidate ISC__BUFFER_INVALIDATE
761 #define isc_buffer_region ISC__BUFFER_REGION
762 #define isc_buffer_usedregion ISC__BUFFER_USEDREGION
763 #define isc_buffer_availableregion ISC__BUFFER_AVAILABLEREGION
764 #define isc_buffer_add ISC__BUFFER_ADD
765 #define isc_buffer_subtract ISC__BUFFER_SUBTRACT
766 #define isc_buffer_clear ISC__BUFFER_CLEAR
767 #define isc_buffer_consumedregion ISC__BUFFER_CONSUMEDREGION
768 #define isc_buffer_remainingregion ISC__BUFFER_REMAININGREGION
769 #define isc_buffer_activeregion ISC__BUFFER_ACTIVEREGION
770 #define isc_buffer_setactive ISC__BUFFER_SETACTIVE
771 #define isc_buffer_first ISC__BUFFER_FIRST
772 #define isc_buffer_forward ISC__BUFFER_FORWARD
773 #define isc_buffer_back ISC__BUFFER_BACK
774 #define isc_buffer_putmem ISC__BUFFER_PUTMEM
775 #define isc_buffer_putstr ISC__BUFFER_PUTSTR
776 #define isc_buffer_putuint8 ISC__BUFFER_PUTUINT8
777 #define isc_buffer_putuint16 ISC__BUFFER_PUTUINT16
778 #define isc_buffer_putuint32 ISC__BUFFER_PUTUINT32
779 #else
780 #define isc_buffer_init isc__buffer_init
781 #define isc_buffer_invalidate isc__buffer_invalidate
782 #define isc_buffer_region isc__buffer_region
783 #define isc_buffer_usedregion isc__buffer_usedregion
784 #define isc_buffer_availableregion isc__buffer_availableregion
785 #define isc_buffer_add isc__buffer_add
786 #define isc_buffer_subtract isc__buffer_subtract
787 #define isc_buffer_clear isc__buffer_clear
788 #define isc_buffer_consumedregion isc__buffer_consumedregion
789 #define isc_buffer_remainingregion isc__buffer_remainingregion
790 #define isc_buffer_activeregion isc__buffer_activeregion
791 #define isc_buffer_setactive isc__buffer_setactive
792 #define isc_buffer_first isc__buffer_first
793 #define isc_buffer_forward isc__buffer_forward
794 #define isc_buffer_back isc__buffer_back
795 #define isc_buffer_putmem isc__buffer_putmem
796 #define isc_buffer_putstr isc__buffer_putstr
797 #define isc_buffer_putuint8 isc__buffer_putuint8
798 #define isc_buffer_putuint16 isc__buffer_putuint16
799 #define isc_buffer_putuint32 isc__buffer_putuint32
800 #endif
802 #endif /* ISC_BUFFER_H */