1321653Ssjg/*	$NetBSD: buf.h,v 1.19 2017/05/31 22:02:06 maya Exp $	*/
2236769Sobrien
3236769Sobrien/*
4236769Sobrien * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
5236769Sobrien *
6236769Sobrien * This code is derived from software contributed to Berkeley by
7236769Sobrien * Adam de Boor.
8236769Sobrien *
9236769Sobrien * Redistribution and use in source and binary forms, with or without
10236769Sobrien * modification, are permitted provided that the following conditions
11236769Sobrien * are met:
12236769Sobrien * 1. Redistributions of source code must retain the above copyright
13236769Sobrien *    notice, this list of conditions and the following disclaimer.
14236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
15236769Sobrien *    notice, this list of conditions and the following disclaimer in the
16236769Sobrien *    documentation and/or other materials provided with the distribution.
17236769Sobrien * 3. Neither the name of the University nor the names of its contributors
18236769Sobrien *    may be used to endorse or promote products derived from this software
19236769Sobrien *    without specific prior written permission.
20236769Sobrien *
21236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31236769Sobrien * SUCH DAMAGE.
32236769Sobrien *
33236769Sobrien *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
34236769Sobrien */
35236769Sobrien
36236769Sobrien/*
37236769Sobrien * Copyright (c) 1988, 1989 by Adam de Boor
38236769Sobrien * Copyright (c) 1989 by Berkeley Softworks
39236769Sobrien * All rights reserved.
40236769Sobrien *
41236769Sobrien * This code is derived from software contributed to Berkeley by
42236769Sobrien * Adam de Boor.
43236769Sobrien *
44236769Sobrien * Redistribution and use in source and binary forms, with or without
45236769Sobrien * modification, are permitted provided that the following conditions
46236769Sobrien * are met:
47236769Sobrien * 1. Redistributions of source code must retain the above copyright
48236769Sobrien *    notice, this list of conditions and the following disclaimer.
49236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright
50236769Sobrien *    notice, this list of conditions and the following disclaimer in the
51236769Sobrien *    documentation and/or other materials provided with the distribution.
52236769Sobrien * 3. All advertising materials mentioning features or use of this software
53236769Sobrien *    must display the following acknowledgement:
54236769Sobrien *	This product includes software developed by the University of
55236769Sobrien *	California, Berkeley and its contributors.
56236769Sobrien * 4. Neither the name of the University nor the names of its contributors
57236769Sobrien *    may be used to endorse or promote products derived from this software
58236769Sobrien *    without specific prior written permission.
59236769Sobrien *
60236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63236769Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70236769Sobrien * SUCH DAMAGE.
71236769Sobrien *
72236769Sobrien *	from: @(#)buf.h	8.1 (Berkeley) 6/6/93
73236769Sobrien */
74236769Sobrien
75236769Sobrien/*-
76236769Sobrien * buf.h --
77236769Sobrien *	Header for users of the buf library.
78236769Sobrien */
79236769Sobrien
80321653Ssjg#ifndef MAKE_BUF_H
81321653Ssjg#define MAKE_BUF_H
82236769Sobrien
83236769Sobrientypedef char Byte;
84236769Sobrien
85236769Sobrientypedef struct Buffer {
86236769Sobrien    int	    size; 	/* Current size of the buffer */
87236769Sobrien    int     count;	/* Number of bytes in buffer */
88236769Sobrien    Byte    *buffer;	/* The buffer itself (zero terminated) */
89236769Sobrien} Buffer;
90236769Sobrien
91236769Sobrien/* If we aren't on netbsd, __predict_false() might not be defined. */
92236769Sobrien#ifndef __predict_false
93236769Sobrien#define __predict_false(x) (x)
94236769Sobrien#endif
95236769Sobrien
96236769Sobrien/* Buf_AddByte adds a single byte to a buffer. */
97236769Sobrien#define	Buf_AddByte(bp, byte) do { \
98236769Sobrien	int _count = ++(bp)->count; \
99236769Sobrien	char *_ptr; \
100236769Sobrien	if (__predict_false(_count >= (bp)->size)) \
101236769Sobrien		Buf_Expand_1(bp); \
102236769Sobrien	_ptr = (bp)->buffer + _count; \
103236769Sobrien	_ptr[-1] = (byte); \
104236769Sobrien	_ptr[0] = 0; \
105236769Sobrien    } while (0)
106236769Sobrien
107236769Sobrien#define BUF_ERROR 256
108236769Sobrien
109236769Sobrien#define Buf_Size(bp) ((bp)->count)
110236769Sobrien
111236769Sobrienvoid Buf_Expand_1(Buffer *);
112236769Sobrienvoid Buf_AddBytes(Buffer *, int, const Byte *);
113236769SobrienByte *Buf_GetAll(Buffer *, int *);
114236769Sobrienvoid Buf_Empty(Buffer *);
115236769Sobrienvoid Buf_Init(Buffer *, int);
116236769SobrienByte *Buf_Destroy(Buffer *, Boolean);
117236769SobrienByte *Buf_DestroyCompact(Buffer *);
118236769Sobrien
119321653Ssjg#endif /* MAKE_BUF_H */
120