chiark / gitweb /
struct/buf.3.in: Correct the type of `buf_put' in the synopsis.
[mLib] / struct / buf.3.in
1 .\" -*-nroff-*-
2 .\"
3 .\" Manual for buffer handling
4 .\"
5 .\" (c) 2005, 2007, 2009, 2017, 2023, 2024 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 it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
16 .\"
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
20 .\" 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 Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 .\" USA.
26 .
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
29 .
30 .\"--------------------------------------------------------------------------
31 .TH buf 3mLib "23 September 2005" "Straylight/Edgeware" "mLib utilities library"
32 .\" @BBASE
33 .\" @BLIM
34 .\" @BCUR
35 .\" @BSZ
36 .\" @BLEN
37 .\" @BLEFT
38 .\" @BSTEP
39 .\" @BBAD
40 .\" @BOK
41 .\" @BENSURE
42 .
43 .\" @DBBASE
44 .\" @DBLIM
45 .\" @DBCUR
46 .\" @DBSZ
47 .\" @DBLEN
48 .\" @DBLEFT
49 .\" @DBSTEP
50 .\" @DBBAD
51 .\" @DBOK
52 .\" @DBENSURE
53 .
54 .\" @buf_init
55 .
56 .\" @dbuf_create
57 .\" @dbuf_reset
58 .\" @dbuf_destroy
59 .\" @DBCREATE
60 .\" @DBRESET
61 .\" @DBDESTROY
62 .\" @DBUF_INIT
63 .\" @DBUF_BUF
64 .
65 .\" @buf_break
66 .\" @buf_flip
67 .\" @buf_ensure
68 .\" @buf_tryextend
69 .\" @buf_get
70 .\" @buf_put
71 .\" @dbuf_break
72 .\" @dbuf_flip
73 .\" @dbuf_ensure
74 .\" @dbuf_tryextend
75 .\" @dbuf_get
76 .\" @dbuf_put
77 .
78 .\" @buf_getbyte
79 .\" @buf_putbyte
80 .\" @dbuf_getbyte
81 .\" @dbuf_putbyte
82 .
83 .\" @buf_putstrf
84 .\" @buf_vputstrf
85 .\" @dbuf_putstrf
86 .\" @dbuf_vputstrf
87 .\" @buf_printops
88 .
89 .\" @buf_getu8
90 .\" @buf_getu16
91 .\" @buf_getu16b
92 .\" @buf_getu16l
93 .\" @buf_getu24
94 .\" @buf_getu24b
95 .\" @buf_getu24l
96 .\" @buf_getu32
97 .\" @buf_getu32b
98 .\" @buf_getu32l
99 .\" @buf_getu64
100 .\" @buf_getu64b
101 .\" @buf_getu64l
102 .\" @buf_getk64
103 .\" @buf_getk64b
104 .\" @buf_getk64l
105 .\" @dbuf_getu8
106 .\" @dbuf_getu16
107 .\" @dbuf_getu16b
108 .\" @dbuf_getu16l
109 .\" @dbuf_getu24
110 .\" @dbuf_getu24b
111 .\" @dbuf_getu24l
112 .\" @dbuf_getu32
113 .\" @dbuf_getu32b
114 .\" @dbuf_getu32l
115 .\" @dbuf_getu64
116 .\" @dbuf_getu64b
117 .\" @dbuf_getu64l
118 .\" @dbuf_getk64
119 .\" @dbuf_getk64b
120 .\" @dbuf_getk64l
121 .
122 .\" @buf_putu8
123 .\" @buf_putu16
124 .\" @buf_putu16b
125 .\" @buf_putu16l
126 .\" @buf_putu24
127 .\" @buf_putu24b
128 .\" @buf_putu24l
129 .\" @buf_putu32
130 .\" @buf_putu32b
131 .\" @buf_putu32l
132 .\" @buf_putu64
133 .\" @buf_putu64b
134 .\" @buf_putu64l
135 .\" @buf_putk64
136 .\" @buf_putk64b
137 .\" @buf_putk64l
138 .\" @dbuf_putu8
139 .\" @dbuf_putu16
140 .\" @dbuf_putu16b
141 .\" @dbuf_putu16l
142 .\" @dbuf_putu24
143 .\" @dbuf_putu24b
144 .\" @dbuf_putu24l
145 .\" @dbuf_putu32
146 .\" @dbuf_putu32b
147 .\" @dbuf_putu32l
148 .\" @dbuf_putu64
149 .\" @dbuf_putu64b
150 .\" @dbuf_putu64l
151 .\" @dbuf_putk64
152 .\" @dbuf_putk64b
153 .\" @dbuf_putk64l
154 .
155 .\" @buf_getf32
156 .\" @buf_getf32l
157 .\" @buf_getf32b
158 .\" @buf_putf32
159 .\" @buf_putf32l
160 .\" @buf_putf32b
161 .\" @buf_getf64
162 .\" @buf_getf64l
163 .\" @buf_getf64b
164 .\" @buf_putf64
165 .\" @buf_putf64l
166 .\" @buf_putf64b
167 .\" @dbuf_getf32
168 .\" @dbuf_getf32l
169 .\" @dbuf_getf32b
170 .\" @dbuf_putf32
171 .\" @dbuf_putf32l
172 .\" @dbuf_putf32b
173 .\" @dbuf_getf64
174 .\" @dbuf_getf64l
175 .\" @dbuf_getf64b
176 .\" @dbuf_putf64
177 .\" @dbuf_putf64l
178 .\" @dbuf_putf64b
179 .
180 .\" @buf_getbuf8
181 .\" @buf_getbuf16
182 .\" @buf_getbuf16b
183 .\" @buf_getbuf16l
184 .\" @buf_getbuf24
185 .\" @buf_getbuf24b
186 .\" @buf_getbuf24l
187 .\" @buf_getbuf32
188 .\" @buf_getbuf32b
189 .\" @buf_getbuf32l
190 .\" @buf_getbuf64
191 .\" @buf_getbuf64b
192 .\" @buf_getbuf64l
193 .\" @buf_getbufz
194 .\" @dbuf_getbuf8
195 .\" @dbuf_getbuf16
196 .\" @dbuf_getbuf16b
197 .\" @dbuf_getbuf16l
198 .\" @dbuf_getbuf24
199 .\" @dbuf_getbuf24b
200 .\" @dbuf_getbuf24l
201 .\" @dbuf_getbuf32
202 .\" @dbuf_getbuf32b
203 .\" @dbuf_getbuf32l
204 .\" @dbuf_getbuf64
205 .\" @dbuf_getbuf64b
206 .\" @dbuf_getbuf64l
207 .\" @dbuf_getbufz
208 .
209 .\" @buf_putbuf8
210 .\" @buf_putbuf16
211 .\" @buf_putbuf16b
212 .\" @buf_putbuf16l
213 .\" @buf_putbuf24
214 .\" @buf_putbuf24b
215 .\" @buf_putbuf24l
216 .\" @buf_putbuf32
217 .\" @buf_putbuf32b
218 .\" @buf_putbuf32l
219 .\" @buf_putbuf64
220 .\" @buf_putbuf64b
221 .\" @buf_putbuf64l
222 .\" @buf_putbufz
223 .\" @dbuf_putbuf8
224 .\" @dbuf_putbuf16
225 .\" @dbuf_putbuf16b
226 .\" @dbuf_putbuf16l
227 .\" @dbuf_putbuf24
228 .\" @dbuf_putbuf24b
229 .\" @dbuf_putbuf24l
230 .\" @dbuf_putbuf32
231 .\" @dbuf_putbuf32b
232 .\" @dbuf_putbuf32l
233 .\" @dbuf_putbuf64
234 .\" @dbuf_putbuf64b
235 .\" @dbuf_putbuf64l
236 .\" @dbuf_putbufz
237 .
238 .\" @buf_getmem8
239 .\" @buf_getmem16
240 .\" @buf_getmem16b
241 .\" @buf_getmem16l
242 .\" @buf_getmem24
243 .\" @buf_getmem24b
244 .\" @buf_getmem24l
245 .\" @buf_getmem32
246 .\" @buf_getmem32b
247 .\" @buf_getmem32l
248 .\" @buf_getmem64
249 .\" @buf_getmem64b
250 .\" @buf_getmem64l
251 .\" @buf_getmemz
252 .\" @dbuf_getmem8
253 .\" @dbuf_getmem16
254 .\" @dbuf_getmem16b
255 .\" @dbuf_getmem16l
256 .\" @dbuf_getmem24
257 .\" @dbuf_getmem24b
258 .\" @dbuf_getmem24l
259 .\" @dbuf_getmem32
260 .\" @dbuf_getmem32b
261 .\" @dbuf_getmem32l
262 .\" @dbuf_getmem64
263 .\" @dbuf_getmem64b
264 .\" @dbuf_getmem64l
265 .\" @dbuf_getmemz
266 .
267 .\" @buf_putmem8
268 .\" @buf_putmem16
269 .\" @buf_putmem16b
270 .\" @buf_putmem16l
271 .\" @buf_putmem24
272 .\" @buf_putmem24b
273 .\" @buf_putmem24l
274 .\" @buf_putmem32
275 .\" @buf_putmem32b
276 .\" @buf_putmem32l
277 .\" @buf_putmem64
278 .\" @buf_putmem64b
279 .\" @buf_putmem64l
280 .\" @buf_putmemz
281 .\" @dbuf_putmem8
282 .\" @dbuf_putmem16
283 .\" @dbuf_putmem16b
284 .\" @dbuf_putmem16l
285 .\" @dbuf_putmem24
286 .\" @dbuf_putmem24b
287 .\" @dbuf_putmem24l
288 .\" @dbuf_putmem32
289 .\" @dbuf_putmem32b
290 .\" @dbuf_putmem32l
291 .\" @dbuf_putmem64
292 .\" @dbuf_putmem64b
293 .\" @dbuf_putmem64l
294 .\" @dbuf_putmemz
295 .
296 .\" @buf_putstr8
297 .\" @buf_putstr16
298 .\" @buf_putstr16b
299 .\" @buf_putstr16l
300 .\" @buf_putstr24
301 .\" @buf_putstr24b
302 .\" @buf_putstr24l
303 .\" @buf_putstr32
304 .\" @buf_putstr32b
305 .\" @buf_putstr32l
306 .\" @buf_putstr64
307 .\" @buf_putstr64b
308 .\" @buf_putstr64l
309 .\" @buf_putstrz
310 .\" @dbuf_putstr8
311 .\" @dbuf_putstr16
312 .\" @dbuf_putstr16b
313 .\" @dbuf_putstr16l
314 .\" @dbuf_putstr24
315 .\" @dbuf_putstr24b
316 .\" @dbuf_putstr24l
317 .\" @dbuf_putstr32
318 .\" @dbuf_putstr32b
319 .\" @dbuf_putstr32l
320 .\" @dbuf_putstr64
321 .\" @dbuf_putstr64b
322 .\" @dbuf_putstr64l
323 .\" @dbuf_putstrz
324 .
325 .\" @buf_getdstr8
326 .\" @buf_getdstr16
327 .\" @buf_getdstr16b
328 .\" @buf_getdstr16l
329 .\" @buf_getdstr24
330 .\" @buf_getdstr24b
331 .\" @buf_getdstr24l
332 .\" @buf_getdstr32
333 .\" @buf_getdstr32b
334 .\" @buf_getdstr32l
335 .\" @buf_getdstr64
336 .\" @buf_getdstr64b
337 .\" @buf_getdstr64l
338 .\" @buf_getdstrz
339 .\" @dbuf_getdstr8
340 .\" @dbuf_getdstr16
341 .\" @dbuf_getdstr16b
342 .\" @dbuf_getdstr16l
343 .\" @dbuf_getdstr24
344 .\" @dbuf_getdstr24b
345 .\" @dbuf_getdstr24l
346 .\" @dbuf_getdstr32
347 .\" @dbuf_getdstr32b
348 .\" @dbuf_getdstr32l
349 .\" @dbuf_getdstr64
350 .\" @dbuf_getdstr64b
351 .\" @dbuf_getdstr64l
352 .\" @dbuf_getdstrz
353 .
354 .\" @buf_putdstr8
355 .\" @buf_putdstr16
356 .\" @buf_putdstr16b
357 .\" @buf_putdstr16l
358 .\" @buf_putdstr24
359 .\" @buf_putdstr24b
360 .\" @buf_putdstr24l
361 .\" @buf_putdstr32
362 .\" @buf_putdstr32b
363 .\" @buf_putdstr32l
364 .\" @buf_putdstr64
365 .\" @buf_putdstr64b
366 .\" @buf_putdstr64l
367 .\" @buf_putdstrz
368 .\" @dbuf_putdstr8
369 .\" @dbuf_putdstr16
370 .\" @dbuf_putdstr16b
371 .\" @dbuf_putdstr16l
372 .\" @dbuf_putdstr24
373 .\" @dbuf_putdstr24b
374 .\" @dbuf_putdstr24l
375 .\" @dbuf_putdstr32
376 .\" @dbuf_putdstr32b
377 .\" @dbuf_putdstr32l
378 .\" @dbuf_putdstr64
379 .\" @dbuf_putdstr64b
380 .\" @dbuf_putdstr64l
381 .\" @dbuf_putdstrz
382 .
383 .\" @buf_putstrf8
384 .\" @buf_putstrf16
385 .\" @buf_putstrf16b
386 .\" @buf_putstrf16l
387 .\" @buf_putstrf24
388 .\" @buf_putstrf24b
389 .\" @buf_putstrf24l
390 .\" @buf_putstrf32
391 .\" @buf_putstrf32b
392 .\" @buf_putstrf32l
393 .\" @buf_putstrf64
394 .\" @buf_putstrf64b
395 .\" @buf_putstrf64l
396 .\" @buf_putstrfz
397 .\" @buf_vputstrf8
398 .\" @buf_vputstrf16
399 .\" @buf_vputstrf16b
400 .\" @buf_vputstrf16l
401 .\" @buf_vputstrf24
402 .\" @buf_vputstrf24b
403 .\" @buf_vputstrf24l
404 .\" @buf_vputstrf32
405 .\" @buf_vputstrf32b
406 .\" @buf_vputstrf32l
407 .\" @buf_vputstrf64
408 .\" @buf_vputstrf64b
409 .\" @buf_vputstrf64l
410 .\" @buf_vputstrfz
411 .\" @dbuf_putstrf8
412 .\" @dbuf_putstrf16
413 .\" @dbuf_putstrf16b
414 .\" @dbuf_putstrf16l
415 .\" @dbuf_putstrf24
416 .\" @dbuf_putstrf24b
417 .\" @dbuf_putstrf24l
418 .\" @dbuf_putstrf32
419 .\" @dbuf_putstrf32b
420 .\" @dbuf_putstrf32l
421 .\" @dbuf_putstrf64
422 .\" @dbuf_putstrf64b
423 .\" @dbuf_putstrf64l
424 .\" @dbuf_putstrfz
425 .\" @dbuf_vputstrf8
426 .\" @dbuf_vputstrf16
427 .\" @dbuf_vputstrf16b
428 .\" @dbuf_vputstrf16l
429 .\" @dbuf_vputstrf24
430 .\" @dbuf_vputstrf24b
431 .\" @dbuf_vputstrf24l
432 .\" @dbuf_vputstrf32
433 .\" @dbuf_vputstrf32b
434 .\" @dbuf_vputstrf32l
435 .\" @dbuf_vputstrf64
436 .\" @dbuf_vputstrf64b
437 .\" @dbuf_vputstrf64l
438 .\" @dbuf_vputstrfz
439 .
440 .\" @BUF_ENCLOSETAG
441 .\" @BUF_ENCLOSEITAG
442 .\" @BUF_ENCLOSEKTAG
443 .\" @BUF_ENCLOSEZTAG
444 .\" @BUF_ENCLOSE8
445 .\" @BUF_ENCLOSE16
446 .\" @BUF_ENCLOSE16_L
447 .\" @BUF_ENCLOSE16_B
448 .\" @BUF_ENCLOSE24
449 .\" @BUF_ENCLOSE24_L
450 .\" @BUF_ENCLOSE24_B
451 .\" @BUF_ENCLOSE32
452 .\" @BUF_ENCLOSE32_L
453 .\" @BUF_ENCLOSE32_B
454 .\" @BUF_ENCLOSE64
455 .\" @BUF_ENCLOSE64_L
456 .\" @BUF_ENCLOSE64_B
457 .\" @BUF_ENCLOSEZ
458 .\" @DBUF_ENCLOSETAG
459 .\" @DBUF_ENCLOSEITAG
460 .\" @DBUF_ENCLOSEKTAG
461 .\" @DBUF_ENCLOSEZTAG
462 .\" @DBUF_ENCLOSE8
463 .\" @DBUF_ENCLOSE16
464 .\" @DBUF_ENCLOSE16_L
465 .\" @DBUF_ENCLOSE16_B
466 .\" @DBUF_ENCLOSE24
467 .\" @DBUF_ENCLOSE24_L
468 .\" @DBUF_ENCLOSE24_B
469 .\" @DBUF_ENCLOSE32
470 .\" @DBUF_ENCLOSE32_L
471 .\" @DBUF_ENCLOSE32_B
472 .\" @DBUF_ENCLOSE64
473 .\" @DBUF_ENCLOSE64_L
474 .\" @DBUF_ENCLOSE64_B
475 .\" @DBUF_ENCLOSEZ
476 .
477 .\"--------------------------------------------------------------------------
478 .SH NAME
479 buf \- reading and writing stuff in buffers
480 .
481 .\"--------------------------------------------------------------------------
482 .SH SYNOPSIS
483 .
484 .nf
485 .B "#include <mLib/dstr.h>"
486 .PP
487 .B "typedef struct { ...\& } buf;"
488 .B "typedef struct { ...\& } dbuf;"
489 .PP
490 .BI "void buf_init(buf *" b ", void *" p ", size_t " sz );
491 .BI "void dbuf_create(dbuf *" db );
492 .BI "void dbuf_reset(dbuf *" db );
493 .BI "void dbuf_destroy(dbuf *" db );
494 .BI "buf *DBUF_BUF(dbuf *" db );
495 .BI "void DBCREATE(dbuf *" db );
496 .BI "void DBRESET(dbuf *" db );
497 .BI "void DBDESTROY(dbuf *" db );
498 .B "#define DBUF_INIT ..."
499 .PP
500 .fi
501 All of the following functions and macros exist in two variants:
502 one with a name beginning
503 .BR buf_ ,
504 .BR B ,
505 or
506 .BR BUF_ ,
507 and taking a first argument of type
508 .BR "buf *" ;
509 and a corresponding similarly named version with name beginning instead
510 .BR dbuf_ ,
511 .BR DB ,
512 or
513 .BR DBUF_ ,
514 and taking a first argument of type
515 .BR "dbuf *" .
516 .nf
517 .PP
518 .BI "void buf_flip(buf *" b );
519 .BI "octet *BBASE(buf *" b );
520 .BI "octet *BLIM(buf *" b );
521 .BI "octet *BCUR(buf *" b );
522 .BI "ptrdiff_t BSZ(buf *" b );
523 .BI "ptrdiff_t BLEN(buf *" b );
524 .BI "ptrdiff_t BLEFT(buf *" b );
525 .BI "void BFLIP(buf *" b );
526 .PP
527 .BI "int buf_break(buf *" b );
528 .BI "int BBREAK(buf *" b );
529 .BI "int BBAD(buf *" b );
530 .BI "int BOK(buf *" b );
531 .PP
532 .BI "int buf_ensure(buf *" b ", size_t " sz );
533 .BI "int buf_tryextend(buf *" b ", size_t " sz );
534 .BI "int BENSURE(buf *" b ", size_t " sz );
535 .BI "octet *BSTEP(buf *" b ", size_t " sz );
536 .PP
537 .BI "void *buf_get(buf *" b ", size_t " sz );
538 .BI "int buf_put(buf *" b ", const void *" p ", size_t " sz );
539 .PP
540 .BI "int buf_getbyte(buf *" b );
541 .BI "int buf_putbyte(buf *" b ", int " ch );
542 .PP
543 .BI "int buf_putstr(buf *" b ", const char *" p ", ...);"
544 .BI "int buf_vputstr(buf *" b ", const char *" p ", va_list *" ap );
545 .PP
546 .fi
547 For
548 .I suff
549 in
550 .BR 8 ,
551 .BR 16 ,
552 .BR 16l ,
553 .BR 16b ,
554 .BR 24 ,
555 .BR 24l ,
556 .BR 24b ,
557 .BR 32 ,
558 .BR 32l ,
559 and
560 .BR 32b ,
561 and, if a 64-bit integer type is available,
562 .BR 64 ,
563 .BR 64l ,
564 and
565 .BR 64b :
566 .nf
567 .BI "int buf_putu" suff "(buf *" b ", uint" suff " " w );
568 .BI "int buf_getu" suff "(buf *" b ", uint" suff " *" w );
569 .PP
570 .fi
571 For
572 .I suff
573 in
574 .BR 64 ,
575 .BR 64l ,
576 and
577 .BR 64b :
578 .nf
579 .BI "int buf_putk" suff "(buf *" b ", kludge64 " w );
580 .BI "int buf_getk" suff "(buf *" b ", kludge64 *" w );
581 .PP
582 .BI "int buf_getf32(buf *" b ", float " x );
583 .BI "int buf_getf32l(buf *" b ", float " x );
584 .BI "int buf_getf32b(buf *" b ", float " x );
585 .BI "int buf_getf64(buf *" b ", double " x );
586 .BI "int buf_getf64l(buf *" b ", double " x );
587 .BI "int buf_getf64b(buf *" b ", double " x );
588 .BI "int buf_putf32(buf *" b ", float *" x_out );
589 .BI "int buf_putf32l(buf *" b ", float *" x_out );
590 .BI "int buf_putf32b(buf *" b ", float *" x_out );
591 .BI "int buf_putf64(buf *" b ", double *" x_out );
592 .BI "int buf_putf64l(buf *" b ", double *" x_out );
593 .BI "int buf_putf64b(buf *" b ", double *" x_out );
594 .PP
595 .ta 2n
596 .BI "BUF_ENCLOSETAG(" tag ", buf *" b ", size_t " mk ", " check ", " poke ", size_t " lensz )
597 .I "    body"
598 .BI "BUF_ENCLOSEITAG(" tag ", buf *" b ", size_t " mk ", " W )
599 .I "    body"
600 .BI "BUF_ENCLOSEKTAG(" tag ", buf *" b ", size_t " mk ", " W )
601 .I "    body"
602 .BI "BUF_ENCLOSEZTAG(" tag ", buf *" b )
603 .I "    body"
604 .PP
605 .fi
606 For
607 .I suff
608 in
609 .BR 8 ,
610 .BR 16 ,
611 .BR 16_L ,
612 .BR 16_B ,
613 .BR 24 ,
614 .BR 24_L ,
615 .BR 24_B ,
616 .BR 32 ,
617 .BR 32_L ,
618 .BR 32_B ,
619 .BR 64 ,
620 .BR 64_L ,
621 and
622 .BR 64_B ,
623 .nf
624 .ta 2n
625 .BI "BUF_ENCLOSE" suff "(buf *" b ", size_t " mk )
626 .I "    body"
627 .PP
628 .BI "BUF_ENCLOSEZ(buf *" b )
629 .I "    body"
630 .PP
631 .fi
632 For
633 .I suff
634 in
635 .BR 8 ,
636 .BR 16 ,
637 .BR 16l ,
638 .BR 16b ,
639 .BR 24 ,
640 .BR 24l ,
641 .BR 24b ,
642 .BR 32 ,
643 .BR 32l ,
644 .BR 32b ,
645 .BR 64 ,
646 .BR 64l ,
647 .BR 64b ,
648 and
649 .BR z :
650 .nf
651 .BI "int buf_putstr" suff "(buf *" b ", const char *" p );
652 .BI "int buf_putstr" suff "(buf *" b ", const char *" p ", ...);"
653 .BI "int buf_vputstr" suff "(buf *" b ", const char *" p ", va_list *" ap );
654 .BI "int buf_putdstr" suff "(buf *" b ", dstr *" d );
655 .BI "int buf_getdstr" suff "(buf *" b ", dstr *" d );
656 .BI "int buf_putbuf" suff "(buf *" b ", buf *" bb );
657 .BI "int buf_getbuf" suff "(buf *" b ", buf *" bb );
658 .BI "int buf_putmem" suff "(buf *" b ", const void *" p ", size_t " sz );
659 .BI "void *buf_getmem" suff "(buf *" b ", size_t *" sz );
660 .PP
661 .fi
662 For
663 .I suff
664 in
665 .BR 64 ,
666 .BR 64l ,
667 and
668 .BR 64b :
669 .nf
670 .BI "int buf_putf" suff "(buf *" b ", double " x );
671 .BI "int buf_getf" suff "(buf *" b ", double *" x );
672 .fi
673 .
674 .\"--------------------------------------------------------------------------
675 .SH DESCRIPTION
676 The
677 .B buf
678 interface allows relatively convenient reading and writing of structured
679 binary data from and to fixed-size memory buffers.  It's useful for
680 formatting and parsing down network data packets, for example.
681 .
682 .SS "Buffer basics"
683 A buffer has three important pointers associated with it:
684 .TP
685 .I base
686 The base address of the buffer.
687 .TP
688 .I limit
689 Just past the last usable byte in the buffer
690 .TP
691 .I current
692 The position in the buffer at which the next read or write will occur.
693 .PP
694 A buffer is created using the
695 .B buf_init
696 function.  You must pass it the buffer base address and size, and a
697 pointer to a
698 .B buf
699 structure to fill in.  It doesn't allocate any memory, so you don't need
700 to dispose of the
701 .B buf
702 structure in any way before forgetting about it.
703 .PP
704 A collection of macros is provided for finding the positions of the
705 various interesting pointers known about a buffer, and the sizes of the
706 regions of memory they imply.
707 .TP
708 .B BBASE
709 The buffer's
710 .I base
711 pointer.
712 .TP
713 .B BLIM
714 The buffer's
715 .I limit
716 pointer.
717 .TP
718 .B BCUR
719 The buffer's
720 .I current
721 pointer.
722 .TP
723 .B BSZ
724 The size of the buffer; i.e.,
725 .I limit
726 \-
727 .IR base .
728 .TP
729 .B BLEN
730 The length of data in the buffer (if writing) or the amount of data
731 read (if reading); i.e.,
732 .I current
733 \-
734 .IR base .
735 .TP
736 .B BLEFT
737 The amount of space left in the buffer (if writing) or the amount of
738 data yet to read (if reading); i.e.,
739 .I limit
740 \-
741 .IR current .
742 .PP
743 The function
744 .B buf_flip
745 takes a buffer which has been used for writing, and makes it suitable
746 for reading.  This turns out to be useful when building packets in
747 multi-layered networking software.  Its precise behaviour is to preserve
748 .IR base ,
749 to set
750 .I limit
751 to
752 .IR current ,
753 and to set
754 .I current
755 to
756 .IR base .
757 There is a macro version,
758 .BR BFLIP ,
759 which does the same thing,
760 but it may evaluate its buffer argument multiple times.
761 .PP
762 A buffer can be
763 .IR broken ,
764 to indicate that it has overflowed or that its contents are otherwise
765 invalid.  The various buffer access functions described below all fail
766 on a broken buffer, and any errors they encounter cause the buffer to
767 become broken.  Most simple programs which only use the supplied buffer
768 access functions can avoid the tedium of error-checking every function
769 call and just check the brokenness state at the end of their run.
770 .PP
771 The function
772 .B buf_break
773 or its
774 macro equivalent
775 .B BBREAK
776 will break a buffer:
777 the function returns \-1 as a possible, but minor, convenience;
778 the macro expands to a statement and cannot return a value.
779 The macro
780 .B BBAD
781 reports true (nonzero) if its buffer argument is broken, or false (zero)
782 otherwise; its counterpart
783 .B BOK
784 reports true if the buffer is OK, and false if it is broken.
785 .
786 .SS "Low-level buffer access"
787 Access to the data in the buffer is usually sequential.  The
788 .B BENSURE
789 macro (or the equivalent
790 .B buf_ensure
791 function) checks that the buffer is OK and that there is enough space
792 remaining in the buffer for
793 .I sz
794 bytes: if so, it returns zero; otherwise it breaks the buffer and
795 returns \-1.
796 .PP
797 The
798 .B BSTEP
799 macro advances the buffer's
800 .I current
801 pointer by
802 .I sz
803 bytes.  It does no bounds checking.  Together with
804 .BR BENSURE ,
805 this provides sequential access to the buffer's contents.
806 .PP
807 The
808 .B buf_get
809 function is the basis of most buffer access functions, whether for
810 reading or writing.  If the buffer is OK, and there are
811 .I sz
812 or more bytes remaining, it steps the buffer's
813 .I current
814 pointer by
815 .I sz
816 and returns the
817 .I original
818 (pre-stepping)
819 .I current
820 pointer; otherwise it breaks the buffer if necessary, and returns a null
821 pointer.
822 .PP
823 The
824 .B buf_put
825 function writes
826 .I sz
827 bytes of data starting at
828 .I p
829 to the buffer.  If it succeeded, it returns 0; otherwise it returns \-1.
830 .
831 .SS "Formatted buffer access"
832 The function
833 .B buf_getbyte
834 returns the next byte from a buffer as a nonnegative integer, or \-1 on
835 error.  The function
836 .B buf_putbyte
837 writes its argument to a buffer, and returns 0 on succes; it returns \-1
838 if it failed.
839 .PP
840 Many of the remaining functions deal with integer formatting and buffer
841 lengths.  The functions support 8-, 16-, 24- and 32-bit integers, in
842 big- or little-endian order; on platforms with 64-bit integers, these
843 are supported too.  The functions' names carry a suffix which is the
844 width in bits of the integers they deal with and an optional
845 .RB ` l '
846 for little- or
847 .RB ` b '
848 for big-endian byte order.  (The variant with no letter uses big-endian
849 order.  Use of these variants tends to mean `I don't really care, but be
850 consistent,' and is not recommended if you have an externally-defined
851 spec you're meant to be compatible with.)
852 .PP
853 The function
854 .BI buf_getu suff
855 reads an integer.  On success, it stores the integer it read at the
856 address
857 .I w
858 given, and returns zero; on failure, it returns \-1.  The function
859 .BI buf_putu suff
860 write an integer.  It returns zero on success or \-1 on failure.
861 .PP
862 For (portability to) platforms without 64-bit integers, the functions
863 .B buf_getk64
864 and
865 .B buf_putk64
866 (and
867 .RB ` l '-
868 and
869 .RB ` b '-suffixed
870 variants) perform the necessary functionality, but acting on the
871 .B kludge64
872 type; see
873 .BR bits (3).
874 .PP
875 The functions
876 .BR buf_getf32 ,
877 .BR buf_getf32l ,
878 and
879 .BR buf_getf32b ,
880 and
881 .BR buf_getf64 ,
882 .BR buf_getf64l ,
883 and
884 .BR buf_getf64b ,
885 read floating-point values
886 in IEEE\ 754 Binary32 and Binary64 format
887 from the buffer;
888 as usual, the suffix indicates the format and byte ordering convention.
889 On success, they store the result in
890 .BI *x
891 and return zero;
892 on failure, they break the buffer and return zero.
893 The functions
894 .BR buf_putf32 ,
895 .BR buf_putf32l ,
896 and
897 .BR buf_putf32b ,
898 and
899 .BR buf_putf64 ,
900 .BR buf_putf64l ,
901 and
902 .BR buf_putf64b
903 write floating-point numbers
904 in IEEE\ 754 Binary32 and Binary64 format
905 from the buffer.
906 On success, they return zero; on failure, they return \-1.
907 Note that these functions use IEEE\ 754 format
908 even if this is not the platform-native floating-point representation:
909 they use the
910 .BR fltfmt (3)
911 functions to do their work.
912 Specifically,
913 they use the
914 .B FLTRND_NEAREVEN
915 rounding convention,
916 and they ignore
917 .BR FLTERR_INEXACT ,
918 .BR FLTERR_UFLOW ,
919 and
920 .B FLTERR_OFLOW
921 errors,
922 and fail on
923 .B FLTERR_INVAL
924 and
925 .B FLTERR_REPR
926 errors.
927 If more subtle control over error handling is necessary,
928 use the
929 .BR fltfmt (3)
930 functions directly.
931 .PP
932 The function
933 .B buf_putstrf
934 processes a
935 .BR printf (3)-like
936 format string and arguments,
937 writing the output to the buffer.
938 The function
939 .B buf_vputstrf
940 does the same,
941 except that it reads arguments from a
942 .B va_list
943 captured argument tail,
944 leaving the tail ready to read the next unprocessed argument.
945 Both functions return the number of bytes written on success
946 or \-1 on failure.
947 Note that these functions apply no length framing or termination.
948 They are implemented using
949 .BR gprintf (3);
950 the output operations table is exposed as
951 .BR buf_printops ;
952 the functions expect the output pointer to be the address of the output
953 .BR buf .
954 .PP
955 Functions which deal with block lengths assume the length is prefixed to
956 the data, and don't include themselves.  They come in all of the integer
957 size variants, including 64-bits even on platforms without 64-bit integers;
958 they also have an additional
959 .RB ` z '
960 variant, which deals with zero-terminated data.  No checks are done on
961 writing that the data written contains no zero bytes.
962 .PP
963 The function
964 .BI buf_getmem suff
965 fetches a block of data.  On success, it returns its base address and
966 stores its length at the given address; on failure, it returns null.
967 The function
968 .BI buf_putmem suff
969 writes a block of data; it return zero on success or \-1 on failure.
970 .PP
971 The functon
972 .BI buf_getbuf suff
973 fetches a block of data and makes a second buffer point to it, i.e.,
974 setting its
975 .I base
976 and
977 .I current
978 pointers to the start of the block and its
979 .I limit
980 pointer to just past the end.  No copying of bulk data is performed.
981 The function
982 .BI buf_putbuf suff
983 writes the contents of a buffer (i.e., between its
984 .I base
985 and
986 .I current
987 pointers).  The function
988 .BI buf_getdstr suff
989 fetches a block of data and append it to a dynamic string (see
990 .BR dstr (3)).
991 The function
992 .BI buf_putdstr suff
993 writes the contents of a dynamic string to a buffer.  Finally, the
994 function
995 .BI buf_putstr suff
996 writes a standard C null-terminated string to a buffer.  All these
997 functions return zero on success or \-1 on failure.
998 .PP
999 The function
1000 .BI buf_putstrf suff
1001 processes a
1002 .BR printf (3)-like
1003 format string and arguments,
1004 writing the output to the buffer.
1005 The function
1006 .BI buf_vputstrf suff
1007 does the same,
1008 except that it reads arguments from a
1009 .B va_list
1010 captured argument tail,
1011 leaving the tail ready to read the next unprocessed argument.
1012 Both functions return the number of bytes written on success
1013 or \-1 on failure.
1014 These functions add framing around the output:
1015 either a length prefix, or a trailing zero byte.
1016 .PP
1017 The
1018 .BI BUF_ENCLOSE suff
1019 macros are syntactically statement heads.
1020 (Notice that these macros use
1021 .RB ` _L '
1022 and
1023 .RB ` _B '
1024 suffixes for little- and big-endian byte order.)
1025 They leave space in the buffer for appropriate length framing,
1026 and execute the following
1027 .I body
1028 statement
1029 (which, of course, can be a compound statement enclosed in braces).
1030 When the
1031 .I body
1032 completes, the macro fills in space
1033 with the length of material written by the
1034 .IR body .
1035 The
1036 .I mk
1037 argument should be a variable of type
1038 .B size_t
1039 which will be overwritten by the macro.
1040 If the material is so large that its won't fit in the space
1041 then the buffer is broken.
1042 The
1043 .B BUF_ENCLOSEZ
1044 macro is similar,
1045 except that it just writes a terminating zero byte
1046 after whatever material was written by the
1047 .IR body .
1048 .PP
1049 The
1050 .BR BUF_ENCLOSE ...\&
1051 macros are based on lower-level machinery.
1052 The
1053 .B BUF_ENCLOSEITAG
1054 macro takes an additional argument
1055 .IR W ;
1056 it leaves
1057 .BI SZ_ W
1058 bytes for the length,
1059 checks that the length doesn't exceed
1060 .BI MASK W \fR,
1061 and stores the length using
1062 .BI STORE W \fR;
1063 all of these constants and macros are defined in
1064 .BR <mLib/bits.h> .
1065 The
1066 .B BUF_ENCLOSEKTAG
1067 is similar, except that it uses the
1068 .B kludge64
1069 machinery to handle 64-bit length fields.
1070 The
1071 .B BUF_ENCLOSEZTAG
1072 macro is superficially similar,
1073 but much simpler,
1074 since it all it does is write a zero byte after its
1075 .I body
1076 completes.
1077 All of those macros also take an additional
1078 .I tag
1079 argument
1080 used to scope the internal labels they construct:
1081 see
1082 .BR control (3)
1083 for the details on how this works.
1084 .PP
1085 The
1086 .B BUF_ENCLOSEITAG
1087 and
1088 .B BUF_ENCLOSEKTAG
1089 macros are themselves built from a lower-level macro named
1090 .BR BUF_ENCLOSETAG .
1091 In place of the
1092 .I W
1093 argument, it takes three arguments:
1094 .I check
1095 is an expression which should evaluate true if the length
1096 .B _delta
1097 can be represented;
1098 .I poke
1099 is a macro, invoked as
1100 .IB poke "(unsigned char *" p ", " size_t n ")" \fR,
1101 which should store
1102 .I n
1103 at address
1104 .IR p ,
1105 formatted in whatever way is appropriate;
1106 and
1107 .I lensz
1108 is the amount of space, in bytes, to save for the length.
1109 .
1110 .SS "Dynamic buffers"
1111 The type
1112 .B dbuf
1113 is a
1114 .IR "dynamic buffer" .
1115 It contains a buffer structure,
1116 accessible using the
1117 p.B DBUF_BUF
1118 macro.
1119 The ordinary buffer functions and macros can be used on this buffer,
1120 though, for convenience,
1121 there are similarly named functions and macros
1122 which accept a
1123 .B dbuf
1124 argument directly.
1125 There is
1126 .I "no difference"
1127 between the behaviour of the
1128 .B "buf"
1129 and
1130 .B "dbuf"
1131 functions.
1132 .PP
1133 A dynamic buffer is created by statically initializing it with
1134 .BR DBUF_INIT ,
1135 or by calling
1136 .BR dbuf_create
1137 or its macro equivalent
1138 .BR DBCREATE .
1139 The memory backing a dynamic buffer can be freed by
1140 .BR dbuf_destroy
1141 or the macro equivalent
1142 .BR DBDESTROY ;
1143 these leave the buffer in the state established by initialization:
1144 the buffer holds no resources, but is ready for immediate use.
1145 .PP
1146 A dynamic buffer contains a
1147 .B buf
1148 buffer,
1149 called its
1150 .I underlying
1151 buffer.
1152 The underlying buffer is accessible through the
1153 .B DBUF_BUF
1154 macro.
1155 All of the above functions and macros can be applied
1156 to a dynamic buffer's underlying buffer.
1157 As a convenience,
1158 corresponding to each of the functions and macros described above,
1159 there is a version named with an initial
1160 .RB ` d '
1161 or
1162 .RB ` D '
1163 as appropriate,
1164 which accepts a pointer to a dynamic buffer
1165 rather than an ordinary buffer,
1166 and acts on its underlying buffer.
1167 Note that these functions are in no way special.
1168 A dynamic buffer will grow automatically
1169 in response to either kind of functions.
1170 .PP
1171 A freshly created buffer is in
1172 .I write
1173 mode,
1174 and is empty, with.
1175 In this state, it will automatically extend its backing storage
1176 in response to
1177 .B BENSURE
1178 calls, rather than breaking.
1179 As a result,
1180 an
1181 .I "a priori"
1182 unpredictable amount of data can be written to a dynamic buffer
1183 and it will automatically grow as necessary to accommodate it.
1184 Of course, the
1185 .B BSZ
1186 and
1187 .B BLEFT
1188 queries are somewhat meaningless when applied to dynamic buffers \(en
1189 though perfectly valid.
1190 The critical function for this is
1191 .B buf_tryextend
1192 (also accessible as
1193 .BR dbuf_tryextend )
1194 which attempts to arrange that at least
1195 .I sz
1196 unused bytes are available in the buffer \(en
1197 i.e., that
1198 .B BLEFT
1199 would return at least
1200 .IR sz .
1201 If it succeeds, it returns zero;
1202 it will fail if the buffer is not in write mode,
1203 or if the buffer is not dynamic,
1204 in which case it returns \-1.
1205 It is unlikely that applications will call this function directly.
1206 .PP
1207 The
1208 .B buf_flip
1209 (or its macro equivalent)
1210 switches the buffer to
1211 .I read
1212 mode,
1213 in addition to its usual behaviour of
1214 setting the buffer's limit to its current position
1215 and its current position to its base.
1216 In read mode, a dynamic buffer will no longer grow dynamically,
1217 as one would expect.
1218 .PP
1219 The
1220 .B dbuf_reset
1221 function,
1222 and its macro equivalent
1223 .B DBRESET
1224 (which may evaluate its argument multiple times)
1225 will return a dynamic buffer to write mode,
1226 and also restore its current position to its base and
1227 clear its broken flag.
1228 .
1229 .\"--------------------------------------------------------------------------
1230 .SH "SEE ALSO"
1231 .
1232 .BR bits (3),
1233 .BR control (3),
1234 .BR dstr (3),
1235 .BR fltfmt (3),
1236 .BR gprintf (3),
1237 .BR mLib (3).
1238 .
1239 .\"--------------------------------------------------------------------------
1240 .SH AUTHOR
1241 .
1242 Mark Wooding, <mdw@distorted.org.uk>
1243 .
1244 .\"----- That's all, folks --------------------------------------------------