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 (bp, byte)
101 BufExpand (bp, nbytes);
113 *-----------------------------------------------------------------------
115 * Add a number of bytes to the buffer.
123 *-----------------------------------------------------------------------
126 Buf_AddBytes (bp, numBytes, bytesPtr)
129 const Byte *bytesPtr;
132 BufExpand (bp, numBytes);
134 memcpy (bp->inPtr, bytesPtr, numBytes);
135 bp->inPtr += numBytes;
136 bp->left -= numBytes;
145 *-----------------------------------------------------------------------
147 * Place the byte back at the beginning of the buffer.
150 * SUCCESS if the byte was added ok. FAILURE if not.
153 * The byte is stuffed in the buffer and outPtr is decremented.
155 *-----------------------------------------------------------------------
158 Buf_UngetByte (bp, byte)
163 if (bp->outPtr != bp->buffer) {
166 } else if (bp->outPtr == bp->inPtr) {
173 * Yech. have to expand the buffer to stuff this thing in.
174 * We use a different expansion constant because people don't
175 * usually push back many bytes when they're doing it a byte at
178 int numBytes = bp->inPtr - bp->outPtr;
181 newBuf = (Byte *)emalloc(bp->size + BUF_UNGET_INC);
182 memcpy ((char *)(newBuf+BUF_UNGET_INC), (char *)bp->outPtr, numBytes+1);
183 bp->outPtr = newBuf + BUF_UNGET_INC;
184 bp->inPtr = bp->outPtr + numBytes;
185 free ((char *)bp->buffer);
187 bp->size += BUF_UNGET_INC;
188 bp->left = bp->size - (bp->inPtr - bp->buffer);
195 *-----------------------------------------------------------------------
197 * Push back a series of bytes at the beginning of the buffer.
203 * outPtr is decremented and the bytes copied into the buffer.
205 *-----------------------------------------------------------------------
208 Buf_UngetBytes (bp, numBytes, bytesPtr)
214 if (bp->outPtr - bp->buffer >= numBytes) {
215 bp->outPtr -= numBytes;
216 memcpy (bp->outPtr, bytesPtr, numBytes);
217 } else if (bp->outPtr == bp->inPtr) {
218 Buf_AddBytes (bp, numBytes, bytesPtr);
220 int curNumBytes = bp->inPtr - bp->outPtr;
222 int newBytes = max(numBytes,BUF_UNGET_INC);
224 newBuf = (Byte *)emalloc (bp->size + newBytes);
225 memcpy((char *)(newBuf+newBytes), (char *)bp->outPtr, curNumBytes+1);
226 bp->outPtr = newBuf + newBytes;
227 bp->inPtr = bp->outPtr + curNumBytes;
228 free ((char *)bp->buffer);
230 bp->size += newBytes;
231 bp->left = bp->size - (bp->inPtr - bp->buffer);
232 bp->outPtr -= numBytes;
233 memcpy ((char *)bp->outPtr, (char *)bytesPtr, numBytes);
238 *-----------------------------------------------------------------------
240 * Return the next byte from the buffer. Actually returns an integer.
243 * Returns BUF_ERROR if there's no byte in the buffer, or the byte
244 * itself if there is one.
247 * outPtr is incremented and both outPtr and inPtr will be reset if
248 * the buffer is emptied.
250 *-----------------------------------------------------------------------
258 if (bp->inPtr == bp->outPtr) {
261 res = (int) *bp->outPtr;
263 if (bp->outPtr == bp->inPtr) {
264 bp->outPtr = bp->inPtr = bp->buffer;
273 *-----------------------------------------------------------------------
275 * Extract a number of bytes from the buffer.
278 * The number of bytes gotten.
281 * The passed array is overwritten.
283 *-----------------------------------------------------------------------
286 Buf_GetBytes (bp, numBytes, bytesPtr)
292 if (bp->inPtr - bp->outPtr < numBytes) {
293 numBytes = bp->inPtr - bp->outPtr;
295 memcpy (bytesPtr, bp->outPtr, numBytes);
296 bp->outPtr += numBytes;
298 if (bp->outPtr == bp->inPtr) {
299 bp->outPtr = bp->inPtr = bp->buffer;
307 *-----------------------------------------------------------------------
309 * Get all the available data at once.
312 * A pointer to the data and the number of bytes available.
317 *-----------------------------------------------------------------------
320 Buf_GetAll (bp, numBytesPtr)
325 if (numBytesPtr != (int *)NULL) {
326 *numBytesPtr = bp->inPtr - bp->outPtr;
333 *-----------------------------------------------------------------------
335 * Throw away bytes in a buffer.
341 * The bytes are discarded.
343 *-----------------------------------------------------------------------
346 Buf_Discard (bp, numBytes)
351 if (bp->inPtr - bp->outPtr <= numBytes) {
352 bp->inPtr = bp->outPtr = bp->buffer;
356 bp->outPtr += numBytes;
361 *-----------------------------------------------------------------------
363 * Returns the number of bytes in the given buffer. Doesn't include
364 * the null-terminating byte.
367 * The number of bytes.
372 *-----------------------------------------------------------------------
378 return (buf->inPtr - buf->outPtr);
382 *-----------------------------------------------------------------------
384 * Initialize a buffer. If no initial size is given, a reasonable
388 * A buffer to be given to other functions in this library.
391 * The buffer is created, the space allocated and pointers
394 *-----------------------------------------------------------------------
398 int size; /* Initial size for the buffer */
400 Buffer bp; /* New Buffer */
402 bp = (Buffer)emalloc(sizeof(*bp));
407 bp->left = bp->size = size;
408 bp->buffer = (Byte *)emalloc(size);
409 bp->inPtr = bp->outPtr = bp->buffer;
416 *-----------------------------------------------------------------------
418 * Nuke a buffer and all its resources.
424 * The buffer is freed.
426 *-----------------------------------------------------------------------
429 Buf_Destroy (buf, freeData)
430 Buffer buf; /* Buffer to destroy */
431 Boolean freeData; /* TRUE if the data should be destroyed as well */
435 free ((char *)buf->buffer);
441 *-----------------------------------------------------------------------
442 * Buf_ReplaceLastByte --
443 * Replace the last byte in a buffer.
449 * If the buffer was empty intially, then a new byte will be added.
450 * Otherwise, the last byte is overwritten.
452 *-----------------------------------------------------------------------
455 Buf_ReplaceLastByte (buf, byte)
456 Buffer buf; /* buffer to augment */
457 int byte; /* byte to be written */
459 if (buf->inPtr == buf->outPtr)
460 Buf_AddByte(buf, byte);
462 *(buf->inPtr - 1) = byte;