2 * Copyright (c) 1988, 1989, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1988, 1989 by Adam de Boor
5 * Copyright (c) 1989 by Berkeley Softworks
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * @(#)buf.c 8.1 (Berkeley) 6/6/93
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
47 * Functions for automatically-expanded buffers.
55 #define max(a,b) ((a) > (b) ? (a) : (b))
60 * Expand the given buffer to hold the given number of additional
62 * Makes sure there's room for an extra NULL byte at the end of the
63 * buffer in case it holds a string.
65 #define BufExpand(bp,nb) \
66 if (bp->left < (nb)+1) {\
67 int newSize = (bp)->size + max((nb) + 1, BUF_ADD_INC); \
68 Byte *newBuf = (Byte *)erealloc((bp)->buffer, newSize); \
70 (bp)->inPtr = newBuf + ((bp)->inPtr - (bp)->buffer); \
71 (bp)->outPtr = newBuf + ((bp)->outPtr - (bp)->buffer);\
72 (bp)->buffer = newBuf;\
73 (bp)->size = newSize;\
74 (bp)->left = newSize - ((bp)->inPtr - (bp)->buffer);\
77 #define BUF_DEF_SIZE 256 /* Default buffer size */
78 #define BUF_ADD_INC 256 /* Expansion increment when Adding */
79 #define BUF_UNGET_INC 16 /* Expansion increment when Ungetting */
82 *-----------------------------------------------------------------------
84 * Add a single byte to the buffer. left is zero or negative.
90 * The buffer may be expanded.
92 *-----------------------------------------------------------------------
95 Buf_OvAddByte(Buffer bp, int byte)
110 *-----------------------------------------------------------------------
112 * Add a number of bytes to the buffer.
120 *-----------------------------------------------------------------------
123 Buf_AddBytes(Buffer bp, int numBytes, const Byte *bytesPtr)
126 BufExpand(bp, numBytes);
128 memcpy(bp->inPtr, bytesPtr, numBytes);
129 bp->inPtr += numBytes;
130 bp->left -= numBytes;
139 *-----------------------------------------------------------------------
141 * Place the byte back at the beginning of the buffer.
144 * SUCCESS if the byte was added ok. FAILURE if not.
147 * The byte is stuffed in the buffer and outPtr is decremented.
149 *-----------------------------------------------------------------------
152 Buf_UngetByte(Buffer bp, int byte)
155 if (bp->outPtr != bp->buffer) {
158 } else if (bp->outPtr == bp->inPtr) {
165 * Yech. have to expand the buffer to stuff this thing in.
166 * We use a different expansion constant because people don't
167 * usually push back many bytes when they're doing it a byte at
170 int numBytes = bp->inPtr - bp->outPtr;
173 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
174 memcpy((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr,
176 bp->outPtr = newBuf + BUF_UNGET_INC;
177 bp->inPtr = bp->outPtr + numBytes;
178 free((char *)bp->buffer);
180 bp->size += BUF_UNGET_INC;
181 bp->left = bp->size - (bp->inPtr - bp->buffer);
188 *-----------------------------------------------------------------------
190 * Push back a series of bytes at the beginning of the buffer.
196 * outPtr is decremented and the bytes copied into the buffer.
198 *-----------------------------------------------------------------------
201 Buf_UngetBytes(Buffer bp, int numBytes, Byte *bytesPtr)
204 if (bp->outPtr - bp->buffer >= numBytes) {
205 bp->outPtr -= numBytes;
206 memcpy(bp->outPtr, bytesPtr, numBytes);
207 } else if (bp->outPtr == bp->inPtr) {
208 Buf_AddBytes(bp, numBytes, bytesPtr);
210 int curNumBytes = bp->inPtr - bp->outPtr;
212 int newBytes = max(numBytes,BUF_UNGET_INC);
214 newBuf = (Byte *)emalloc (bp->size + newBytes);
215 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes + 1);
216 bp->outPtr = newBuf + newBytes;
217 bp->inPtr = bp->outPtr + curNumBytes;
218 free((char *)bp->buffer);
220 bp->size += newBytes;
221 bp->left = bp->size - (bp->inPtr - bp->buffer);
222 bp->outPtr -= numBytes;
223 memcpy((char *)bp->outPtr, (char *)bytesPtr, numBytes);
228 *-----------------------------------------------------------------------
230 * Return the next byte from the buffer. Actually returns an integer.
233 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
234 * itself if there is one.
237 * outPtr is incremented and both outPtr and inPtr will be reset if
238 * the buffer is emptied.
240 *-----------------------------------------------------------------------
243 Buf_GetByte(Buffer bp)
247 if (bp->inPtr == bp->outPtr) {
250 res = (int)*bp->outPtr;
252 if (bp->outPtr == bp->inPtr) {
253 bp->outPtr = bp->inPtr = bp->buffer;
262 *-----------------------------------------------------------------------
264 * Extract a number of bytes from the buffer.
267 * The number of bytes gotten.
270 * The passed array is overwritten.
272 *-----------------------------------------------------------------------
275 Buf_GetBytes(Buffer bp, int numBytes, Byte *bytesPtr)
278 if (bp->inPtr - bp->outPtr < numBytes) {
279 numBytes = bp->inPtr - bp->outPtr;
281 memcpy(bytesPtr, bp->outPtr, numBytes);
282 bp->outPtr += numBytes;
284 if (bp->outPtr == bp->inPtr) {
285 bp->outPtr = bp->inPtr = bp->buffer;
293 *-----------------------------------------------------------------------
295 * Get all the available data at once.
298 * A pointer to the data and the number of bytes available.
303 *-----------------------------------------------------------------------
306 Buf_GetAll(Buffer bp, int *numBytesPtr)
309 if (numBytesPtr != (int *)NULL) {
310 *numBytesPtr = bp->inPtr - bp->outPtr;
317 *-----------------------------------------------------------------------
319 * Throw away bytes in a buffer.
325 * The bytes are discarded.
327 *-----------------------------------------------------------------------
330 Buf_Discard(Buffer bp, int numBytes)
333 if (bp->inPtr - bp->outPtr <= numBytes) {
334 bp->inPtr = bp->outPtr = bp->buffer;
338 bp->outPtr += numBytes;
343 *-----------------------------------------------------------------------
345 * Returns the number of bytes in the given buffer. Doesn't include
346 * the null-terminating byte.
349 * The number of bytes.
354 *-----------------------------------------------------------------------
359 return (buf->inPtr - buf->outPtr);
363 *-----------------------------------------------------------------------
365 * Initialize a buffer. If no initial size is given, a reasonable
369 * A buffer to be given to other functions in this library.
372 * The buffer is created, the space allocated and pointers
375 *-----------------------------------------------------------------------
380 Buffer bp; /* New Buffer */
382 bp = (Buffer)emalloc(sizeof(*bp));
387 bp->left = bp->size = size;
388 bp->buffer = (Byte *)emalloc(size);
389 bp->inPtr = bp->outPtr = bp->buffer;
396 *-----------------------------------------------------------------------
398 * Destroy a buffer, and optionally free its data, too.
404 * The buffer is freed.
406 *-----------------------------------------------------------------------
409 Buf_Destroy(Buffer buf, Boolean freeData)
413 free((char *)buf->buffer);
419 *-----------------------------------------------------------------------
420 * Buf_ReplaceLastByte --
421 * Replace the last byte in a buffer.
427 * If the buffer was empty intially, then a new byte will be added.
428 * Otherwise, the last byte is overwritten.
430 *-----------------------------------------------------------------------
433 Buf_ReplaceLastByte(Buffer buf, int byte)
435 if (buf->inPtr == buf->outPtr)
436 Buf_AddByte(buf, byte);
438 *(buf->inPtr - 1) = byte;