chiark / gitweb /
struct/sym.c: Fix loading following table extension.
[mLib] / struct / buf.h
1 /* -*-c-*-
2  *
3  * Reading and writing packet buffers
4  *
5  * (c) 2001 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
12  * mLib is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * mLib is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with mLib; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 #ifndef MLIB_BUF_H
29 #define MLIB_BUF_H
30
31 #ifdef __cplusplus
32   extern "C" {
33 #endif
34
35 /*----- Header files ------------------------------------------------------*/
36
37 #include <stdarg.h>
38 #include <stddef.h>
39
40 #ifndef MLIB_BITS_H
41 #  include "bits.h"
42 #endif
43
44 #ifndef MLIB_CONTROL_H
45 #  include "control.h"
46 #endif
47
48 #ifndef MLIB_DSTR_H
49 #  include "dstr.h"
50 #endif
51
52 #ifndef MLIB_MACROS_H
53 #  include "macros.h"
54 #endif
55
56 /*----- Data structures ---------------------------------------------------*/
57
58 /* --- Buffers --- *
59  *
60  * Buffers provide a simple stream-like interface for building and parsing
61  * packets.
62  */
63
64 typedef struct buf {
65   octet *base, *p, *limit;              /* Pointers to the buffer */
66   unsigned f;                           /* Various flags */
67 } buf;
68
69 #define BF_BROKEN 1u                    /* Buffer is broken */
70 #define BF_ALLOC 2u                     /* Buffer is dynamic */
71 #define BF_WRITE 4u                     /* Currently writing to buffer */
72
73 typedef struct dbuf {
74   buf _b;
75   arena *a;                             /* Allocation arena */
76   size_t sz;                            /* Allocated size */
77 } dbuf;
78
79 #define DBUF_INIT { { 0, 0, 0, BF_ALLOC | BF_WRITE}, &arena_stdlib, 0 }
80 #define DBUF_BUF(db) (&(db)->_b)
81
82 extern const struct gprintf_ops buf_printops;
83
84 /*----- Useful macros -----------------------------------------------------*/
85
86 #define BBASE(b) ((b)->base)
87 #define BLIM(b) ((b)->limit)
88 #define BCUR(b) ((b)->p)
89 #define BSZ(b) ((b)->limit - (b)->base)
90 #define BLEN(b) ((b)->p - (b)->base)
91 #define BLEFT(b) ((b)->limit - (b)->p)
92 #define BSTEP(b, sz) ((b)->p += (sz))
93 #define BBAD(b) ((b)->f & BF_BROKEN)
94 #define BOK(b) (!BBAD(b))
95
96 #define BENSURE(b, sz)                                          \
97      (BBAD(b) ? -1 : (sz) > BLEFT(b) ? buf_tryextend(b, sz) : 0)
98
99 #define DBBASE(db) BBASE(DBUF_BUF(db))
100 #define DBLIM(db) BLIM(DBUF_BUF(db))
101 #define DBCUR(db) BCUR(DBUF_BUF(db))
102 #define DBSZ(db) BSZ(DBUF_BUF(db))
103 #define DBLEN(db) BLEN(DBUF_BUF(db))
104 #define DBLEFT(db) BLEFT(DBUF_BUF(db))
105 #define DBSTEP(db, sz) BSTEP(DBUF_BUF(db), (sz))
106 #define DBBAD(db) BBAD(DBUF_BUF(db))
107 #define DBOK(db) BOK(DBUF_BUF(db))
108 #define DBENSURE(b, sz) BENSURE(DBUF_BUF(db), (sz))
109
110 #ifdef HAVE_UINT64
111 #  define BUF_DOKLUDGESUFFIXES(_)
112 #else
113 #  define BUF_DOKLUDGESUFFIXES(_)                                       \
114         _(64, 64, 64) _(64, 64_B, 64b) _(64, 64_L, 64l)
115 #endif
116
117 #define BUF_DOSUFFIXES(_) DOUINTCONV(_) BUF_DOKLUDGESUFFIXES(_) _(z, z, z)
118
119 /*----- Functions provided ------------------------------------------------*/
120
121 /* --- @buf_init@ --- *
122  *
123  * Arguments:   @buf *b@ = pointer to a buffer block
124  *              @void *p@ = pointer to a buffer
125  *              @size_t sz@ = size of the buffer
126  *
127  * Returns:     ---
128  *
129  * Use:         Initializes the buffer block appropriately.
130  */
131
132 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
133
134 /* --- @dbuf_create@ --- *
135  *
136  * Arguments:   @dbuf *db@ = pointer to a dynamic buffer block
137  *
138  * Returns:     ---
139  *
140  * Use:         Initializes a dynamic buffer.  The buffer is initially empty,
141  *              and ready for writing.
142  */
143
144 extern void dbuf_create(dbuf */*db*/);
145 #define DBCREATE(db) do {                                               \
146   (db)->_b.base = (db)->_b.p = (db)->_b.limit = 0;                      \
147   (db)->_b.f = BF_ALLOC | BF_WRITE;                                     \
148   (db)->a = &arena_stdlib; (db)->sz = 0;                                \
149 } while (0)
150
151 /* --- @dbuf_reset@ --- *
152  *
153  * Arguments:   @dbuf *db@ = pointer to a buffer block
154  *
155  * Returns:     ---
156  *
157  * Use:         Resets a buffer so that it can be written again.
158  */
159
160 extern void dbuf_reset(dbuf */*db*/);
161 #define DBRESET(db) do {                                                \
162   (db)->_b.p = (db)->_b.base; (db)->_b.limit = (db)->_b.base + (db)->sz; \
163   (db)->_b.f = ((db)->_b.f&~BF_BROKEN) | BF_WRITE;                      \
164 } while (0)
165
166 /* --- @dbuf_destroy@ --- *
167  *
168  * Arguments:   @dbuf *db@ = pointer to a buffer block
169  *
170  * Returns:     ---
171  *
172  * Use:         Release all of the resources held by a dynamic buffer.
173  */
174
175 extern void dbuf_destroy(dbuf */*db*/);
176 #define DBDESTROY(db) do {                                              \
177   if ((db)->_b.base) x_free((db)->a, (db)->_b.base);                    \
178   (db)->_b.base = (db)->_b.p = (db)->_b.limit = 0;                      \
179   (db)->_b.f = BF_ALLOC | BF_WRITE; (db)->sz = 0;                       \
180 } while (0)
181
182 /* --- @{,d}buf_break@ --- *
183  *
184  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
185  *
186  * Returns:     Some negative value.
187  *
188  * Use:         Marks a buffer as broken.
189  */
190
191 extern int buf_break(buf */*b*/);
192 extern int dbuf_break(dbuf */*db*/);
193 #define dbuf_break(db) (buf_break(DBUF_BUF(db)))
194 #define BBREAK(b) do { (b)->f |= BF_BROKEN; } while (0)
195 #define DBBREAK(db) BBREAK(DBUF_BUF(db))
196
197 /* --- @{,d}buf_flip@ --- *
198  *
199  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
200  *
201  * Returns:     ---
202  *
203  * Use:         Flips a buffer so that if you've just been writing to it,
204  *              you can now read from the bit you've written.
205  */
206
207 extern void buf_flip(buf */*b*/);
208 extern void dbuf_flip(dbuf */*db*/);
209 #define dbuf_flip(db) (buf_flip(DBUF_BUF(db)))
210
211 #define BFLIP(b) do {                                                   \
212   (b)->limit = (b)->p; (b)->p = (b)->base;                              \
213   (b)->f &= ~BF_WRITE;                                                  \
214 } while (0)
215 #define DBFLIP(db) BFLIP(DBUF_BUF(db))
216
217 /* --- @{,d}buf_ensure@ --- *
218  *
219  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
220  *              @size_t sz@ = size of data wanted
221  *
222  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
223  *
224  * Use:         Ensures that there are @sz@ bytes still in the buffer.
225  */
226
227 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
228 extern int dbuf_ensure(dbuf */*db*/, size_t /*sz*/);
229 #define dbuf_ensure(db, sz) (buf_ensure(DBUF_BUF(db), (sz)))
230
231 /* --- @{,d}buf_tryextend@ --- *
232  *
233  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
234  *              @size_t sz@ = size of data wanted
235  *
236  * Returns:     Zero if it worked, nonzero if the buffer won't grow.
237  *
238  * Use:         Extend the buffer so that at least @sz@ bytes are available.
239  *              This only works if the buffer is allocated.
240  */
241
242 extern int buf_tryextend(buf */*b*/, size_t /*sz*/);
243 extern int dbuf_tryextend(dbuf */*db*/, size_t /*sz*/);
244 #define dbuf_tryextend(db, sz) (buf_tryextend(DBUF_BUF(db), (sz)))
245
246 /* --- @{,d}buf_get@ --- *
247  *
248  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
249  *              @size_t sz@ = size of the buffer
250  *
251  * Returns:     Pointer to the place in the buffer.
252  *
253  * Use:         Reserves a space in the buffer of the requested size, and
254  *              returns its start address.
255  */
256
257 extern void *buf_get(buf */*b*/, size_t /*sz*/);
258 extern void *dbuf_get(dbuf */*db*/, size_t /*sz*/);
259 #define dbuf_get(db, sz) (buf_get(DBUF_BUF(db), (sz)))
260
261 /* --- @{,d}buf_put@ --- *
262  *
263  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
264  *              @const void *p@ = pointer to a buffer
265  *              @size_t sz@ = size of the buffer
266  *
267  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
268  *
269  * Use:         Fetches data from some place and puts it in the buffer
270  */
271
272 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
273 extern int dbuf_put(dbuf */*db*/, const void */*p*/, size_t /*sz*/);
274 #define dbuf_put(db, p, sz) (buf_put(DBUF_BUF(db), (p), (sz)))
275
276 /* --- @{,d}buf_fill@ --- *
277  *
278  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
279  *              @int ch@ = fill character
280  *              @size_t sz@ = size to fill
281  *
282  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
283  *
284  * Use:         Write @sz@ bytes with value @ch@ to the buffer, as if with
285  *              @memset@.
286  */
287
288 extern int buf_fill(buf */*b*/, int /*ch*/, size_t /*sz*/);
289 extern int dbuf_fill(dbuf */*db*/, int /*ch*/, size_t /*sz*/);
290 #define dbuf_fill(db, ch, sz) (buf_fill(DBUF_BUF(db), (ch), (sz)))
291
292 /* --- @{,d}buf_alignskip@ --- *
293  *
294  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
295  *              @size_t m, a@ = alignment multiple and offset
296  *
297  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
298  *
299  * Use:         Advance the buffer position as little as possible such that
300  *              it is @a@ greater than a multiple of @m@.  This doesn't write
301  *              anything to the buffer, so it's probably not suitable for
302  *              output: use @buf_alignfill@ instead.
303  */
304
305 extern int buf_alignskip(buf */*b*/, size_t /*m*/, size_t /*a*/);
306 extern int dbuf_alignskip(dbuf */*db*/, size_t /*m*/, size_t /*a*/);
307 #define dbuf_alignskip(db, m, a) (buf_alignskip(DBUF_BUF(db), (m), (a)))
308
309 /* --- @{,d}buf_alignfill@ --- *
310  *
311  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
312  *              @int ch@ = fill character
313  *              @size_t m, a@ = alignment multiple and offset
314  *
315  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
316  *
317  * Use:         Fill the buffer with as few copies of @ch@ as possible, as if
318  *              by @memset@, to advance the buffer position to a value @a@
319  *              greater than a multiple of @m@.
320  */
321
322 extern int buf_alignfill(buf */*b*/, int /*ch*/, size_t /*m*/, size_t /*a*/);
323 extern int (dbuf_alignfill)(dbuf */*db*/, int /*ch*/,
324                             size_t /*m*/, size_t /*a*/);
325 #define dbuf_alignfill(db, ch, m, a)                                    \
326   (buf_alignfill(DBUF_BUF(db), (ch), (a), (m)))
327
328 /* --- @{,d}buf_getbyte@ --- *
329  *
330  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
331  *
332  * Returns:     A byte, or less than zero if there wasn't a byte there.
333  *
334  * Use:         Gets a single byte from a buffer.
335  */
336
337 extern int buf_getbyte(buf */*b*/);
338 extern int dbuf_getbyte(dbuf */*db*/);
339 #define dbuf_getbyte(db) (buf_getbyte(DBUF_BUF(db)))
340
341 /* --- @{,d}buf_putbyte@ --- *
342  *
343  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
344  *              @int ch@ = byte to write
345  *
346  * Returns:     Zero if OK, nonzero if there wasn't enough space.
347  *
348  * Use:         Puts a single byte in a buffer.
349  */
350
351 extern int buf_putbyte(buf */*b*/, int /*ch*/);
352 extern int dbuf_putbyte(dbuf */*db*/, int /*ch*/);
353 #define dbuf_putbyte(db, ch) (buf_putbyte(DBUF_BUF(db), (ch)))
354
355 /* --- @{,d}buf_getu{8,{16,24,32,64}{,l,b}}@ --- *
356  *
357  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
358  *              @uintSZ *w@ = where to put the word
359  *
360  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
361  *
362  * Use:         Gets a word of appropriate size and order from a buffer.
363  */
364
365 #define BUF_DECL_GETU_(n, W, w)                                         \
366   extern int buf_getu##w(buf */*b*/, uint##n */*w*/);                   \
367   extern int dbuf_getu##w(dbuf */*db*/, uint##n */*w*/);
368 DOUINTCONV(BUF_DECL_GETU_)
369 #define dbuf_getu8(db, w) (buf_getu8(DBUF_BUF(db), (w)))
370 #define dbuf_getu16(db, w) (buf_getu16(DBUF_BUF(db), (w)))
371 #define dbuf_getu16l(db, w) (buf_getu16l(DBUF_BUF(db), (w)))
372 #define dbuf_getu16b(db, w) (buf_getu16b(DBUF_BUF(db), (w)))
373 #define dbuf_getu24(db, w) (buf_getu24(DBUF_BUF(db), (w)))
374 #define dbuf_getu24l(db, w) (buf_getu24l(DBUF_BUF(db), (w)))
375 #define dbuf_getu24b(db, w) (buf_getu24b(DBUF_BUF(db), (w)))
376 #define dbuf_getu32(db, w) (buf_getu32(DBUF_BUF(db), (w)))
377 #define dbuf_getu32l(db, w) (buf_getu32l(DBUF_BUF(db), (w)))
378 #define dbuf_getu32b(db, w) (buf_getu32b(DBUF_BUF(db), (w)))
379 #ifdef HAVE_UINT64
380 #  define dbuf_getu64(db, w) (buf_getu64(DBUF_BUF(db), (w)))
381 #  define dbuf_getu64l(db, w) (buf_getu64l(DBUF_BUF(db), (w)))
382 #  define dbuf_getu64b(db, w) (buf_getu64b(DBUF_BUF(db), (w)))
383 #endif
384
385 /* --- @{,d}buf_getk64{,l,b}@ --- *
386  *
387  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
388  *              @kludge64 *w@ = where to put the word
389  *
390  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
391  *
392  * Use:         Gets a word of appropriate size and order from a buffer.
393  */
394
395 extern int buf_getk64(buf */*b*/, kludge64 */*w*/);
396 extern int buf_getk64l(buf */*b*/, kludge64 */*w*/);
397 extern int buf_getk64b(buf */*b*/, kludge64 */*w*/);
398 extern int dbuf_getk64(dbuf */*db*/, kludge64 */*w*/);
399 extern int dbuf_getk64l(dbuf */*db*/, kludge64 */*w*/);
400 extern int dbuf_getk64b(dbuf */*db*/, kludge64 */*w*/);
401 #define dbuf_getk64(db, w) (buf_getk64(DBUF_BUF(db), (w)))
402 #define dbuf_getk64l(db, w) (buf_getk64l(DBUF_BUF(db), (w)))
403 #define dbuf_getk64b(db, w) (buf_getk64b(DBUF_BUF(db), (w)))
404
405 /* --- @{,d}buf_putu{8,{16,24,32,64}{,l,b}}@ --- *
406  *
407  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
408  *              @uintSZ w@ = word to write
409  *
410  * Returns:     Zero if OK, or nonzero if there wasn't enough space
411  *
412  * Use:         Puts a word into a buffer with appropriate size and order.
413  */
414
415 #define BUF_DECL_PUTU_(n, W, w)                                         \
416   extern int buf_putu##w(buf */*b*/, uint##n /*w*/);                    \
417   extern int dbuf_putu##w(dbuf */*db*/, uint##n /*w*/);
418 DOUINTCONV(BUF_DECL_PUTU_)
419 #define dbuf_putu8(db, w) (buf_putu8(DBUF_BUF(db), (w)))
420 #define dbuf_putu16(db, w) (buf_putu16(DBUF_BUF(db), (w)))
421 #define dbuf_putu16l(db, w) (buf_putu16l(DBUF_BUF(db), (w)))
422 #define dbuf_putu16b(db, w) (buf_putu16b(DBUF_BUF(db), (w)))
423 #define dbuf_putu24(db, w) (buf_putu24(DBUF_BUF(db), (w)))
424 #define dbuf_putu24l(db, w) (buf_putu24l(DBUF_BUF(db), (w)))
425 #define dbuf_putu24b(db, w) (buf_putu24b(DBUF_BUF(db), (w)))
426 #define dbuf_putu32(db, w) (buf_putu32(DBUF_BUF(db), (w)))
427 #define dbuf_putu32l(db, w) (buf_putu32l(DBUF_BUF(db), (w)))
428 #define dbuf_putu32b(db, w) (buf_putu32b(DBUF_BUF(db), (w)))
429 #ifdef HAVE_UINT64
430 #  define dbuf_putu64(db, w) (buf_putu64(DBUF_BUF(db), (w)))
431 #  define dbuf_putu64l(db, w) (buf_putu64l(DBUF_BUF(db), (w)))
432 #  define dbuf_putu64b(db, w) (buf_putu64b(DBUF_BUF(db), (w)))
433 #endif
434
435 /* --- @{,d}buf_putk64{,l,b}@ --- *
436  *
437  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
438  *              @kludge64 w@ = word to write
439  *
440  * Returns:     Zero if OK, or nonzero if there wasn't enough space
441  *
442  * Use:         Gets a word of appropriate size and order from a buffer.
443  */
444
445 extern int buf_putk64(buf */*b*/, kludge64 /*w*/);
446 extern int buf_putk64l(buf */*b*/, kludge64 /*w*/);
447 extern int buf_putk64b(buf */*b*/, kludge64 /*w*/);
448 extern int dbuf_putk64(dbuf */*db*/, kludge64 /*w*/);
449 extern int dbuf_putk64l(dbuf */*db*/, kludge64 /*w*/);
450 extern int dbuf_putk64b(dbuf */*db*/, kludge64 /*w*/);
451 #define dbuf_putk64(db, w) (buf_putk64(DBUF_BUF(db), (w)))
452 #define dbuf_putk64l(db, w) (buf_putk64l(DBUF_BUF(db), (w)))
453 #define dbuf_putk64b(db, w) (buf_putk64b(DBUF_BUF(db), (w)))
454
455 /* --- @buf_getf{32,64}{,l,b} --- *
456  *
457  * Arguments:   @buf *b@ = a buffer to read from
458  *              @float *x_out@, @double *x_out@ = where to put the result
459  *
460  * Returns:     Zero on success, %$-1$% on failure (and the buffer is
461  *              broken).
462  *
463  * Use:         Get an IEEE Binary32 or Binary64 value from the buffer.
464  *              Conversion is performed using the `fltfmt' machinery, with
465  *              the usual round-to-nearest/ties-to-even rounding mode.
466  */
467
468 extern int buf_getf32(buf */*b*/, float */*x_out*/);
469 extern int buf_getf32l(buf */*b*/, float */*x_out*/);
470 extern int buf_getf32b(buf */*b*/, float */*x_out*/);
471 #define dbuf_getf32(db, x_out) (buf_getf32(DBUF_BUF(db), (x_out)))
472 #define dbuf_getf32l(db, x_out) (buf_getf32l(DBUF_BUF(db), (x_out)))
473 #define dbuf_getf32b(db, x_out) (buf_getf32b(DBUF_BUF(db), (x_out)))
474
475 extern int buf_getf64(buf */*b*/, double */*x_out*/);
476 extern int buf_getf64l(buf */*b*/, double */*x_out*/);
477 extern int buf_getf64b(buf */*b*/, double */*x_out*/);
478 #define dbuf_getf64(db, x_out) (buf_getf64(DBUF_BUF(db), (x_out)))
479 #define dbuf_getf64l(db, x_out) (buf_getf64l(DBUF_BUF(db), (x_out)))
480 #define dbuf_getf64b(db, x_out) (buf_getf64b(DBUF_BUF(db), (x_out)))
481
482 /* --- @buf_putf{32,64}{,l,b} --- *
483  *
484  * Arguments:   @buf *b@ = a buffer to write to
485  *              @double x@ = a number to write
486  *
487  * Returns:     Zero on success, %$-1$% on failure (and the buffer is
488  *              broken).
489  *
490  * Use:         Get an IEEE Binary32 or Binary64 value from the buffer.
491  *              Conversion is performed using the `fltfmt' machinery, with
492  *              the usual round-to-nearest/ties-to-even rounding mode.
493  */
494
495 extern int buf_putf32(buf */*b*/, float /*x*/);
496 extern int buf_putf32l(buf */*b*/, float /*x*/);
497 extern int buf_putf32b(buf */*b*/, float /*x*/);
498 #define dbuf_putf32(db, x) (buf_putf32(DBUF_BUF(db), (x)))
499 #define dbuf_putf32l(db, x) (buf_putf32l(DBUF_BUF(db), (x)))
500 #define dbuf_putf32b(db, x) (buf_putf32b(DBUF_BUF(db), (x)))
501
502 extern int buf_putf64(buf */*b*/, double /*x*/);
503 extern int buf_putf64l(buf */*b*/, double /*x*/);
504 extern int buf_putf64b(buf */*b*/, double /*x*/);
505 #define dbuf_putf64(db, x) (buf_putf64(DBUF_BUF(db), (x)))
506 #define dbuf_putf64l(db, x) (buf_putf64l(DBUF_BUF(db), (x)))
507 #define dbuf_putf64b(db, x) (buf_putf64b(DBUF_BUF(db), (x)))
508
509 /* --- @{,d}buf_getmem{8,{16,24,32,64}{,l,b},z} --- *
510  *
511  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
512  *              @size_t *nn@ = where to put the length
513  *
514  * Returns:     Pointer to the buffer data, or null.
515  *
516  * Use:         Gets a chunk of memory from a buffer.  The suffix is the
517  *              width and byte order of the length; @z@ means null-
518  *              terminated.
519  */
520
521 #define BUF_DECL_GETMEM_(n, W, w)                                       \
522   extern void *buf_getmem##w(buf */*b*/, size_t */*nn*/);               \
523   extern void *dbuf_getmem##w(dbuf */*db*/, size_t */*nn*/);
524 BUF_DOSUFFIXES(BUF_DECL_GETMEM_)
525 #define dbuf_getmem8(db, nn) (buf_getmem8(DBUF_BUF(db), (nn)))
526 #define dbuf_getmem16(db, nn) (buf_getmem16(DBUF_BUF(db), (nn)))
527 #define dbuf_getmem16l(db, nn) (buf_getmem16l(DBUF_BUF(db), (nn)))
528 #define dbuf_getmem16b(db, nn) (buf_getmem16b(DBUF_BUF(db), (nn)))
529 #define dbuf_getmem24(db, nn) (buf_getmem24(DBUF_BUF(db), (nn)))
530 #define dbuf_getmem24l(db, nn) (buf_getmem24l(DBUF_BUF(db), (nn)))
531 #define dbuf_getmem24b(db, nn) (buf_getmem24b(DBUF_BUF(db), (nn)))
532 #define dbuf_getmem32(db, nn) (buf_getmem32(DBUF_BUF(db), (nn)))
533 #define dbuf_getmem32l(db, nn) (buf_getmem32l(DBUF_BUF(db), (nn)))
534 #define dbuf_getmem32b(db, nn) (buf_getmem32b(DBUF_BUF(db), (nn)))
535 #define dbuf_getmem64(db, nn) (buf_getmem64(DBUF_BUF(db), (nn)))
536 #define dbuf_getmem64l(db, nn) (buf_getmem64l(DBUF_BUF(db), (nn)))
537 #define dbuf_getmem64b(db, nn) (buf_getmem64b(DBUF_BUF(db), (nn)))
538 #define dbuf_getmemz(db, nn) (buf_getmemz(DBUF_BUF(db), (nn)))
539
540 /* --- @{,d}buf_putmem{8,{16,24,32,64}{,l,b},z} --- *
541  *
542  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
543  *              @const void *p@ = pointer to data to write
544  *              @size_t n@ = length to write
545  *
546  * Returns:     Zero if OK, nonzero if there wasn't enough space.
547  *
548  * Use:         Writes a chunk of data to a buffer.  The suffix is the
549  *              width and byte order of the length; @z@ means null-
550  *              terminated.
551  */
552
553 #define BUF_DECL_PUTMEM_(n, W, w)                                       \
554   extern int buf_putmem##w(buf */*b*/, const void */*p*/, size_t /*nn*/); \
555   extern int dbuf_putmem##w(dbuf */*db*/, const void */*p*/, size_t /*nn*/);
556 BUF_DOSUFFIXES(BUF_DECL_PUTMEM_)
557 #define dbuf_putmem8(db, p, nn) (buf_putmem8(DBUF_BUF(db), (p), (nn)))
558 #define dbuf_putmem16(db, p, nn) (buf_putmem16(DBUF_BUF(db), (p), (nn)))
559 #define dbuf_putmem16l(db, p, nn) (buf_putmem16l(DBUF_BUF(db), (p), (nn)))
560 #define dbuf_putmem16b(db, p, nn) (buf_putmem16b(DBUF_BUF(db), (p), (nn)))
561 #define dbuf_putmem24(db, p, nn) (buf_putmem24(DBUF_BUF(db), (p), (nn)))
562 #define dbuf_putmem24l(db, p, nn) (buf_putmem24l(DBUF_BUF(db), (p), (nn)))
563 #define dbuf_putmem24b(db, p, nn) (buf_putmem24b(DBUF_BUF(db), (p), (nn)))
564 #define dbuf_putmem32(db, p, nn) (buf_putmem32(DBUF_BUF(db), (p), (nn)))
565 #define dbuf_putmem32l(db, p, nn) (buf_putmem32l(DBUF_BUF(db), (p), (nn)))
566 #define dbuf_putmem32b(db, p, nn) (buf_putmem32b(DBUF_BUF(db), (p), (nn)))
567 #define dbuf_putmem64(db, p, nn) (buf_putmem64(DBUF_BUF(db), (p), (nn)))
568 #define dbuf_putmem64l(db, p, nn) (buf_putmem64l(DBUF_BUF(db), (p), (nn)))
569 #define dbuf_putmem64b(db, p, nn) (buf_putmem64b(DBUF_BUF(db), (p), (nn)))
570 #define dbuf_putmemz(db, p, nn) (buf_putmemz(DBUF_BUF(db), (p), (nn)))
571
572 /* --- @{,d}buf_getbuf{8,{16,24,32,64}{,l,b},z} --- *
573  *
574  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
575  *              @buf *bb@ = where to put the result
576  *
577  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
578  *
579  * Use:         Gets a block of data from a buffer, and writes its bounds to
580  *              another buffer.
581  */
582
583 #define BUF_DECL_GETBUF_(n, W, w)                                       \
584   extern int buf_getbuf##w(buf */*b*/, buf */*bb*/);                    \
585   extern int dbuf_getbuf##w(dbuf */*db*/, buf */*bb*/);
586 BUF_DOSUFFIXES(BUF_DECL_GETBUF_)
587 #define dbuf_getbuf8(db, bb) (buf_getbuf8(DBUF_BUF(db), (bb)))
588 #define dbuf_getbuf16(db, bb) (buf_getbuf16(DBUF_BUF(db), (bb)))
589 #define dbuf_getbuf16l(db, bb) (buf_getbuf16l(DBUF_BUF(db), (bb)))
590 #define dbuf_getbuf16b(db, bb) (buf_getbuf16b(DBUF_BUF(db), (bb)))
591 #define dbuf_getbuf24(db, bb) (buf_getbuf24(DBUF_BUF(db), (bb)))
592 #define dbuf_getbuf24l(db, bb) (buf_getbuf24l(DBUF_BUF(db), (bb)))
593 #define dbuf_getbuf24b(db, bb) (buf_getbuf24b(DBUF_BUF(db), (bb)))
594 #define dbuf_getbuf32(db, bb) (buf_getbuf32(DBUF_BUF(db), (bb)))
595 #define dbuf_getbuf32l(db, bb) (buf_getbuf32l(DBUF_BUF(db), (bb)))
596 #define dbuf_getbuf32b(db, bb) (buf_getbuf32b(DBUF_BUF(db), (bb)))
597 #define dbuf_getbuf64(db, bb) (buf_getbuf64(DBUF_BUF(db), (bb)))
598 #define dbuf_getbuf64l(db, bb) (buf_getbuf64l(DBUF_BUF(db), (bb)))
599 #define dbuf_getbuf64b(db, bb) (buf_getbuf64b(DBUF_BUF(db), (bb)))
600 #define dbuf_getbufz(db, bb) (buf_getbufz(DBUF_BUF(db), (bb)))
601
602 /* --- @{,d}buf_putbuf{8,{16,24,32,64}{,l,b},z} --- *
603  *
604  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
605  *              @buf *bb@ = buffer to write
606  *
607  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
608  *
609  * Use:         Puts the contents of a buffer to a buffer.
610  */
611
612 #define BUF_DECL_PUTBUF_(n, W, w)                                       \
613   extern int buf_putbuf##w(buf */*b*/, buf */*bb*/);                    \
614   extern int dbuf_putbuf##w(dbuf */*db*/, buf */*bb*/);
615 BUF_DOSUFFIXES(BUF_DECL_PUTBUF_)
616 #define dbuf_putbuf8(db, bb) (buf_putbuf8(DBUF_BUF(db), (bb)))
617 #define dbuf_putbuf16(db, bb) (buf_putbuf16(DBUF_BUF(db), (bb)))
618 #define dbuf_putbuf16l(db, bb) (buf_putbuf16l(DBUF_BUF(db), (bb)))
619 #define dbuf_putbuf16b(db, bb) (buf_putbuf16b(DBUF_BUF(db), (bb)))
620 #define dbuf_putbuf24(db, bb) (buf_putbuf24(DBUF_BUF(db), (bb)))
621 #define dbuf_putbuf24l(db, bb) (buf_putbuf24l(DBUF_BUF(db), (bb)))
622 #define dbuf_putbuf24b(db, bb) (buf_putbuf24b(DBUF_BUF(db), (bb)))
623 #define dbuf_putbuf32(db, bb) (buf_putbuf32(DBUF_BUF(db), (bb)))
624 #define dbuf_putbuf32l(db, bb) (buf_putbuf32l(DBUF_BUF(db), (bb)))
625 #define dbuf_putbuf32b(db, bb) (buf_putbuf32b(DBUF_BUF(db), (bb)))
626 #define dbuf_putbuf64(db, bb) (buf_putbuf64(DBUF_BUF(db), (bb)))
627 #define dbuf_putbuf64l(db, bb) (buf_putbuf64l(DBUF_BUF(db), (bb)))
628 #define dbuf_putbuf64b(db, bb) (buf_putbuf64b(DBUF_BUF(db), (bb)))
629 #define dbuf_putbufz(db, bb) (buf_putbufz(DBUF_BUF(db), (bb)))
630
631 /* --- @{,d}buf_getdstr{8,{16,24,32,64}{,l,b},z} --- *
632  *
633  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
634  *              @dstr *d@ = where to put the result
635  *
636  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
637  *
638  * Use:         Gets a block of data from a buffer, and writes its contents
639  *              to a string.
640  */
641
642 #define BUF_DECL_GETDSTR_(n, W, w)                                      \
643   extern int buf_getdstr##w(buf */*b*/, dstr */*d*/);                   \
644   extern int dbuf_getdstr##w(dbuf */*db*/, dstr */*d*/);
645 BUF_DOSUFFIXES(BUF_DECL_GETDSTR_)
646 #define dbuf_getdstr8(db, d) (buf_getdstr8(DBUF_BUF(db), (d)))
647 #define dbuf_getdstr16(db, d) (buf_getdstr16(DBUF_BUF(db), (d)))
648 #define dbuf_getdstr16l(db, d) (buf_getdstr16l(DBUF_BUF(db), (d)))
649 #define dbuf_getdstr16b(db, d) (buf_getdstr16b(DBUF_BUF(db), (d)))
650 #define dbuf_getdstr24(db, d) (buf_getdstr24(DBUF_BUF(db), (d)))
651 #define dbuf_getdstr24l(db, d) (buf_getdstr24l(DBUF_BUF(db), (d)))
652 #define dbuf_getdstr24b(db, d) (buf_getdstr24b(DBUF_BUF(db), (d)))
653 #define dbuf_getdstr32(db, d) (buf_getdstr32(DBUF_BUF(db), (d)))
654 #define dbuf_getdstr32l(db, d) (buf_getdstr32l(DBUF_BUF(db), (d)))
655 #define dbuf_getdstr32b(db, d) (buf_getdstr32b(DBUF_BUF(db), (d)))
656 #define dbuf_getdstr64(db, d) (buf_getdstr64(DBUF_BUF(db), (d)))
657 #define dbuf_getdstr64l(db, d) (buf_getdstr64l(DBUF_BUF(db), (d)))
658 #define dbuf_getdstr64b(db, d) (buf_getdstr64b(DBUF_BUF(db), (d)))
659 #define dbuf_getdstrz(db, d) (buf_getdstrz(DBUF_BUF(db), (d)))
660
661 /* --- @{,d}buf_putdstr{8,{16,24,32,64}{,l,b},z} --- *
662  *
663  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
664  *              @dstr *d@ = string to write
665  *
666  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
667  *
668  * Use:         Puts a dynamic string to a buffer.
669  */
670
671 #define BUF_DECL_PUTDSTR_(n, W, w)                                      \
672   extern int buf_putdstr##w(buf */*b*/, dstr */*d*/);                   \
673   extern int dbuf_putdstr##w(dbuf */*db*/, dstr */*d*/);
674 BUF_DOSUFFIXES(BUF_DECL_PUTDSTR_)
675 #define dbuf_putdstr8(db, d) (buf_putdstr8(DBUF_BUF(db), (d)))
676 #define dbuf_putdstr16(db, d) (buf_putdstr16(DBUF_BUF(db), (d)))
677 #define dbuf_putdstr16l(db, d) (buf_putdstr16l(DBUF_BUF(db), (d)))
678 #define dbuf_putdstr16b(db, d) (buf_putdstr16b(DBUF_BUF(db), (d)))
679 #define dbuf_putdstr24(db, d) (buf_putdstr24(DBUF_BUF(db), (d)))
680 #define dbuf_putdstr24l(db, d) (buf_putdstr24l(DBUF_BUF(db), (d)))
681 #define dbuf_putdstr24b(db, d) (buf_putdstr24b(DBUF_BUF(db), (d)))
682 #define dbuf_putdstr32(db, d) (buf_putdstr32(DBUF_BUF(db), (d)))
683 #define dbuf_putdstr32l(db, d) (buf_putdstr32l(DBUF_BUF(db), (d)))
684 #define dbuf_putdstr32b(db, d) (buf_putdstr32b(DBUF_BUF(db), (d)))
685 #define dbuf_putdstr64(db, d) (buf_putdstr64(DBUF_BUF(db), (d)))
686 #define dbuf_putdstr64l(db, d) (buf_putdstr64l(DBUF_BUF(db), (d)))
687 #define dbuf_putdstr64b(db, d) (buf_putdstr64b(DBUF_BUF(db), (d)))
688 #define dbuf_putdstrz(db, d) (buf_putdstrz(DBUF_BUF(db), (d)))
689
690 /* --- @{,d}buf_putstr{8,{16,24,32,64}{,l,b},z} --- *
691  *
692  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
693  *              @const char *p@ = string to write
694  *
695  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
696  *
697  * Use:         Puts a null-terminated string to a buffer.
698  */
699
700 #define BUF_DECL_PUTSTR_(n, W, w)                                       \
701   extern int buf_putstr##w(buf */*b*/, const char */*p*/);              \
702   extern int dbuf_putstr##w(dbuf */*db*/, const char */*p*/);
703 BUF_DOSUFFIXES(BUF_DECL_PUTSTR_)
704 #define dbuf_putstr8(db, p) (buf_putstr8(DBUF_BUF(db), (p)))
705 #define dbuf_putstr16(db, p) (buf_putstr16(DBUF_BUF(db), (p)))
706 #define dbuf_putstr16l(db, p) (buf_putstr16l(DBUF_BUF(db), (p)))
707 #define dbuf_putstr16b(db, p) (buf_putstr16b(DBUF_BUF(db), (p)))
708 #define dbuf_putstr24(db, p) (buf_putstr24(DBUF_BUF(db), (p)))
709 #define dbuf_putstr24l(db, p) (buf_putstr24l(DBUF_BUF(db), (p)))
710 #define dbuf_putstr24b(db, p) (buf_putstr24b(DBUF_BUF(db), (p)))
711 #define dbuf_putstr32(db, p) (buf_putstr32(DBUF_BUF(db), (p)))
712 #define dbuf_putstr32l(db, p) (buf_putstr32l(DBUF_BUF(db), (p)))
713 #define dbuf_putstr32b(db, p) (buf_putstr32b(DBUF_BUF(db), (p)))
714 #define dbuf_putstr64(db, p) (buf_putstr64(DBUF_BUF(db), (p)))
715 #define dbuf_putstr64l(db, p) (buf_putstr64l(DBUF_BUF(db), (p)))
716 #define dbuf_putstr64b(db, p) (buf_putstr64b(DBUF_BUF(db), (p)))
717 #define dbuf_putstrz(db, p) (buf_putstrz(DBUF_BUF(db), (p)))
718
719 /* --- @{,D}BUF_ENCLOSETAG@ --- *
720  *
721  * Arguments:   @tag@ = a control-structure macro tag
722  *              @buf *b@ or @dbuf *db@ = pointer to a buffer block
723  *              @size_t mk@ = temporary, used to stash starting offset
724  *              @check@ = expression which is true if the length @_delta@
725  *                      is representable
726  *              @poke@ = function or macro called as @poke(octet *, size_t)@
727  *                      to store the final size at the given address
728  *              @size_t lensz@ = space to leave for the length
729  *
730  * Use:         This is a statement head.  It ensures that there is enough
731  *              space in the buffer, saves the current output offset in @mk,
732  *              and reserves @lensz@ bytes for a length prefix.  It then
733  *              executes the @body@, which should contribute zero or more
734  *              further bytes to the buffer.  Finally, it invokes @poke@ to
735  *              store the length of the material written by @body@ in the
736  *              space reserved.
737  */
738
739 #define BUF_ENCLOSETAG(tag, b, mk, check, poke, lensz)                  \
740   MC_BEFORE(tag##__save, {                                              \
741     (mk) = BLEN(b);                                                     \
742     if (!BENSURE(b, lensz)) BSTEP(b, (lensz));                          \
743   })                                                                    \
744   MC_AFTER(tag##__poke, {                                               \
745     size_t _delta = BLEN(b) - (mk) - (lensz);                           \
746     if (!(check)) (b)->f |= BF_BROKEN;                                  \
747     else if (BOK(b)) poke(BBASE(b) + (mk), _delta);                     \
748   })
749
750 #define DBUF_ENCLOSETAG(tag, b, mk, check, poke, lensz)                 \
751   BUF_ENCLOSETAG(tag, DBUF_BUF(b), (mk), (check), poke, (lensz))
752
753 /* --- @{,D}BUF_ENCLOSE{I,K,Z}TAG@ --- *
754  *
755  * Arguments:   @tag@ = a control-structure macro tag
756  *              @buf *b@ or @dbuf *db@ = pointer to a buffer block
757  *              @size_t mk@ = temporary, used to stash starting offset
758  *              @W@ = word-size and -order suffix
759  *
760  * Use:         Specialized versions of @BUF_ENCLOSETAG@ above.
761  *
762  *              @BUF_ENCLOSEZTAG@ just writes a terminating zero byte.
763  *              @BUF_ENCLOSEITAG@ writes a word with the given size and
764  *              byte ordering.  @BUF_ENCLOSEKTAG@ does the same using the
765  *              @kludge64@ machinery.
766  */
767
768 #define MLIB__BUF_STORESZK64(p, sz)                                             \
769   do { kludge64 _k; ASSIGN64(_k, (sz)); STORE64_((p), _k); } while (0)
770 #define MLIB__BUF_STORESZK64_B(p, sz)                                           \
771   do { kludge64 _k; ASSIGN64(_k, (sz)); STORE64_B_((p), _k); } while (0)
772 #define MLIB__BUF_STORESZK64_L(p, sz)                                           \
773   do { kludge64 _k; ASSIGN64(_k, (sz)); STORE64_L_((p), _k); } while (0)
774
775 #define BUF_ENCLOSEITAG(tag, b, mk, W)                                  \
776   BUF_ENCLOSETAG(tag, (b), (mk), (_delta <= MASK##W), STORE##W, SZ_##W)
777 #define BUF_ENCLOSEKTAG(tag, b, mk, W)                                  \
778   BUF_ENCLOSETAG(tag, (b), (mk), 1, MLIB__BUF_STORESZK##W, 8)
779 #define BUF_ENCLOSEZTAG(tag, b)                                         \
780   MC_AFTER(tag##__zero, { buf_putbyte((b), 0); })
781
782 #define DBUF_ENCLOSEITAG(tag, b, mk, W)                                 \
783   BUF_ENCLOSEITAG(tag, DBUF_BUF(b), (mk), W)
784 #define DBUF_ENCLOSEKTAG(tag, b, mk, W)                                 \
785   BUF_ENCLOSEKTAG(tag, DBUF_BUF(b), (mk), W)
786 #define DBUF_ENCLOSEZTAG(tag, b)                                        \
787   BUF_ENCLOSEZTAG(tag, DBUF_BUF(b))
788
789 /* --- @{,D}BUF_ENCLOSE{8,{16,24,32,64}{,_L,_B},Z}@ --- *
790  *
791  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
792  *              @size_t mk@ = temporary, used to stash starting offset
793  *              @W@ = word-size and -order suffix
794  *
795  * Use:         User versions of @BUF_ENCLOSETAG@; see that macro for
796  *              details.
797  *
798  *              These are statement heads.  They reserve space for a length
799  *              prefix and execute the statement.  When the statement
800  *              completes, they patch the length of material written by the
801  *              statement into the reserved space.
802  */
803
804 #define BUF_ENCLOSE8(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 8)
805 #define BUF_ENCLOSE16(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 16)
806 #define BUF_ENCLOSE16_L(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 16_L)
807 #define BUF_ENCLOSE16_B(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 16_B)
808 #define BUF_ENCLOSE24(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 24)
809 #define BUF_ENCLOSE24_L(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 24_L)
810 #define BUF_ENCLOSE24_B(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 24_B)
811 #define BUF_ENCLOSE32(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 32)
812 #define BUF_ENCLOSE32_L(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 32_L)
813 #define BUF_ENCLOSE32_B(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 32_B)
814 #ifdef HAVE_UINT64
815 #  define BUF_ENCLOSE64(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 64)
816 #  define BUF_ENCLOSE64_L(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 64_L)
817 #  define BUF_ENCLOSE64_B(b, mk) BUF_ENCLOSEITAG(encl, (b), (mk), 64_B)
818 #else
819 #  define BUF_ENCLOSE64(b, mk) BUF_ENCLOSEKTAG(encl, (b), (mk), 64)
820 #  define BUF_ENCLOSE64_L(b, mk) BUF_ENCLOSEKTAG(encl, (b), (mk), 64_L)
821 #  define BUF_ENCLOSE64_B(b, mk) BUF_ENCLOSEKTAG(encl, (b), (mk), 64_B)
822 #endif
823 #define BUF_ENCLOSEZ(b) BUF_ENCLOSEZTAG(encl, (b))
824
825 #define DBUF_ENCLOSE8(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 8)
826 #define DBUF_ENCLOSE16(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 16)
827 #define DBUF_ENCLOSE16_L(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 16_L)
828 #define DBUF_ENCLOSE16_B(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 16_B)
829 #define DBUF_ENCLOSE24(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 24)
830 #define DBUF_ENCLOSE24_L(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 24_L)
831 #define DBUF_ENCLOSE24_B(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 24_B)
832 #define DBUF_ENCLOSE32(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 32)
833 #define DBUF_ENCLOSE32_L(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 32_L)
834 #define DBUF_ENCLOSE32_B(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 32_B)
835 #ifdef HAVE_UINT64
836 #  define DBUF_ENCLOSE64(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 64)
837 #  define DBUF_ENCLOSE64_L(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 64_L)
838 #  define DBUF_ENCLOSE64_B(db, mk) DBUF_ENCLOSEITAG(encl, (db), (mk), 64_B)
839 #else
840 #  define DBUF_ENCLOSE64(db, mk) DBUF_ENCLOSEKTAG(encl, (db), (mk), 64)
841 #  define DBUF_ENCLOSE64_L(db, mk) DBUF_ENCLOSEKTAG(encl, (db), (mk), 64_L)
842 #  define DBUF_ENCLOSE64_B(db, mk) DBUF_ENCLOSEKTAG(encl, (db), (mk), 64_B)
843 #endif
844 #define DBUF_ENCLOSEZ(db) DBUF_ENCLOSEZTAG(encl, (db))
845
846 /* --- @{,d}buf_putstrf@, @{,d}buf_vputstrf@ --- *
847  *
848  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
849  *              @const char *p@ = pointer to @printf@-style format string
850  *              @va_list *ap@ = argument handle
851  *
852  * Returns:     The number of characters written to the string, or @-1@ on
853  *              failure.
854  *
855  * Use:         Format a string to a buffer.  The resulting output is not
856  *              null-terminated.
857  */
858
859 extern PRINTF_LIKE(2, 3)
860   int buf_putstrf(buf */*b*/, const char */*p*/, ...);
861 extern PRINTF_LIKE(2, 3)
862   int dbuf_putstrf(dbuf */*db*/, const char */*p*/, ...);
863 #if __STDC__ >= 199901
864 #  define dbuf_putstrf(db, /*p*/...) (buf_putstr(DBUF_BUF(db), __VA_ARGS__))
865 #endif
866 extern int buf_vputstrf(buf */*b*/, const char */*p*/, va_list */*ap*/);
867 extern int dbuf_vputstrf(dbuf */*db*/, const char */*p*/, va_list */*ap*/);
868 #define dbuf_vputstrf(db, p, ap) (buf_vputstrf(DBUF_BUF(db), (p), (ap)))
869
870 /* --- @{,d}buf_{,v}putstrf{8,{16,24,32,64}{,b,l},z}@ --- *
871  *
872  * Arguments:   @buf *b@ or @dbuf *db@ = pointer to a buffer block
873  *              @const char *p@ = pointer to @printf@-style format string
874  *              @va_list *ap@ = argument handle
875  *
876  * Returns:     The number of characters written to the string, or @-1@ on
877  *              failure.
878  *
879  * Use:         As for @buf_putstr@, but using a format string.
880  */
881
882 #define BUF_DECL_PUTSTRF_(n, W, w)                                      \
883   extern PRINTF_LIKE(2, 3)                                              \
884     int buf_putstrf##w(buf */*b*/, const char */*p*/, ...);             \
885   extern PRINTF_LIKE(2, 3)                                              \
886     int dbuf_putstrf##w(dbuf */*db*/, const char */*p*/, ...);          \
887   extern int buf_vputstrf##w(buf */*b*/,                                \
888                              const char */*p*/, va_list */*ap*/);       \
889   extern int dbuf_vputstrf##w(dbuf */*db*/,                             \
890                               const char */*p*/, va_list */*ap*/);
891 BUF_DOSUFFIXES(BUF_DECL_PUTSTRF_)
892 #if __STDC__ >= 199901
893 #  define dbuf_putstrf8(db, /*p*/...)                                   \
894      (buf_putstrf8(DBUF_BUF(db), __VA_ARGS__))
895 #  define dbuf_putstrf16(db, /*p*/...)                                  \
896      (buf_putstrf16(DBUF_BUF(db), __VA_ARGS__))
897 #  define dbuf_putstrf16l(db, /*p*/...)                                 \
898      (buf_putstrf16l(DBUF_BUF(db), __VA_ARGS__))
899 #  define dbuf_putstrf16b(db, /*p*/...)                                 \
900      (buf_putstrf16b(DBUF_BUF(db), __VA_ARGS__))
901 #  define dbuf_putstrf24(db, /*p*/...)                                  \
902      (buf_putstrf24(DBUF_BUF(db), __VA_ARGS__))
903 #  define dbuf_putstrf24l(db, /*p*/...)                                 \
904      (buf_putstrf24l(DBUF_BUF(db), __VA_ARGS__))
905 #  define dbuf_putstrf24b(db, /*p*/...)                                 \
906      (buf_putstrf24b(DBUF_BUF(db), __VA_ARGS__))
907 #  define dbuf_putstrf32(db, /*p*/...)                                  \
908      (buf_putstrf32(DBUF_BUF(db), __VA_ARGS__))
909 #  define dbuf_putstrf32l(db, /*p*/...)                                 \
910      (buf_putstrf32l(DBUF_BUF(db), __VA_ARGS__))
911 #  define dbuf_putstrf32b(db, /*p*/...)                                 \
912      (buf_putstrf32b(DBUF_BUF(db), __VA_ARGS__))
913 #  define dbuf_putstrf64(db, /*p*/...)                                  \
914      (buf_putstrf64(DBUF_BUF(db), __VA_ARGS__))
915 #  define dbuf_putstrf64l(db, /*p*/...)                                 \
916      (buf_putstrf64l(DBUF_BUF(db), __VA_ARGS__))
917 #  define dbuf_putstrf64b(db, /*p*/...)                                 \
918      (buf_putstrf64b(DBUF_BUF(db), __VA_ARGS__))
919 #  define dbuf_putstrfz(db, /*p*/...)                                   \
920      (buf_putstrfz(DBUF_BUF(db), __VA_ARGS__))
921 #endif
922 #define dbuf_vputstrf8(db, p, ap) (buf_vputstrf8(DBUF_BUF(db), (p), (ap)))
923 #define dbuf_vputstrf16(db, p, ap) (buf_vputstrf16(DBUF_BUF(db), (p), (ap)))
924 #define dbuf_vputstrf16l(db, p, ap) (buf_vputstrf16l(DBUF_BUF(db), (p), (ap)))
925 #define dbuf_vputstrf16b(db, p, ap) (buf_vputstrf16b(DBUF_BUF(db), (p), (ap)))
926 #define dbuf_vputstrf24(db, p, ap) (buf_vputstrf24(DBUF_BUF(db), (p), (ap)))
927 #define dbuf_vputstrf24l(db, p, ap) (buf_vputstrf24l(DBUF_BUF(db), (p), (ap)))
928 #define dbuf_vputstrf24b(db, p, ap) (buf_vputstrf24b(DBUF_BUF(db), (p), (ap)))
929 #define dbuf_vputstrf32(db, p, ap) (buf_vputstrf32(DBUF_BUF(db), (p), (ap)))
930 #define dbuf_vputstrf32l(db, p, ap) (buf_vputstrf32l(DBUF_BUF(db), (p), (ap)))
931 #define dbuf_vputstrf32b(db, p, ap) (buf_vputstrf32b(DBUF_BUF(db), (p), (ap)))
932 #define dbuf_vputstrf64(db, p, ap) (buf_vputstrf64(DBUF_BUF(db), (p), (ap)))
933 #define dbuf_vputstrf64l(db, p, ap) (buf_vputstrf64l(DBUF_BUF(db), (p), (ap)))
934 #define dbuf_vputstrf64b(db, p, ap) (buf_vputstrf64b(DBUF_BUF(db), (p), (ap)))
935 #define dbuf_vputstrfz(db, p, ap) (buf_vputstrfz(DBUF_BUF(db), (p), (ap)))
936
937 /*----- That's all, folks -------------------------------------------------*/
938
939 #ifdef __cplusplus
940   }
941 #endif
942
943 #endif