1 /* $OpenPackages$ */ 2 /* $OpenBSD: buf.c,v 1.18 2001/05/30 00:43:00 deraadt Exp $ */ 3 /* $NetBSD: buf.c,v 1.9 1996/12/31 17:53:21 christos Exp $ */ 4 5 /* 6 * Copyright (c) 1999 Marc Espie. 7 * 8 * Extensive code changes for the OpenBSD project. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 23 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. 33 * Copyright (c) 1988, 1989 by Adam de Boor 34 * Copyright (c) 1989 by Berkeley Softworks 35 * All rights reserved. 36 * 37 * This code is derived from software contributed to Berkeley by 38 * Adam de Boor. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the University of 51 * California, Berkeley and its contributors. 52 * 4. Neither the name of the University nor the names of its contributors 53 * may be used to endorse or promote products derived from this software 54 * without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 66 * SUCH DAMAGE. 67 */ 68 69 /*- 70 * buf.c -- 71 * Functions for automatically-expanded buffers. 72 */ 73 74 #include <ctype.h> 75 #include <stddef.h> 76 #include <string.h> 77 #include "config.h" 78 #include "defines.h" 79 #include "buf.h" 80 #include "stats.h" 81 #include "memory.h" 82 83 #ifdef STATS_BUF 84 #define DO_STAT_BUF(bp, nb) \ 85 STAT_BUFS_EXPANSION++; \ 86 if ((bp)->endPtr - (bp)->buffer == 1) \ 87 STAT_WEIRD_INEFFICIENT++; 88 #else 89 #define DO_STAT_BUF(a, b) 90 #endif 91 92 /* BufExpand(bp, nb) 93 * Expand buffer bp to hold upto nb additional 94 * chars. Makes sure there's room for an extra '\0' char at 95 * the end of the buffer to terminate the string. */ 96 #define BufExpand(bp,nb) \ 97 do { \ 98 size_t occupied = (bp)->inPtr - (bp)->buffer; \ 99 size_t size = (bp)->endPtr - (bp)->buffer; \ 100 DO_STAT_BUF(bp, nb); \ 101 \ 102 do { \ 103 size *= 2 ; \ 104 } while (size - occupied < (nb)+1+BUF_MARGIN); \ 105 (bp)->buffer = (bp)->inPtr = (bp)->endPtr = \ 106 erealloc((bp)->buffer, size); \ 107 (bp)->inPtr += occupied; \ 108 (bp)->endPtr += size; \ 109 } while (0); 110 111 #define BUF_DEF_SIZE 256 /* Default buffer size */ 112 #define BUF_MARGIN 256 /* Make sure we are comfortable */ 113 114 /* the hard case for Buf_AddChar: buffer must be expanded to accommodate 115 * one more char. */ 116 void 117 BufOverflow(bp) 118 Buffer bp; 119 { 120 BufExpand(bp, 1); 121 } 122 123 124 void 125 Buf_AddChars(bp, numBytes, bytesPtr) 126 Buffer bp; 127 size_t numBytes; 128 const char *bytesPtr; 129 { 130 131 if ((size_t)(bp->endPtr - bp->inPtr) < numBytes+1) 132 BufExpand(bp, numBytes); 133 134 memcpy(bp->inPtr, bytesPtr, numBytes); 135 bp->inPtr += numBytes; 136 } 137 138 139 void 140 Buf_Init(bp, size) 141 Buffer bp; 142 size_t size; 143 { 144 #ifdef STATS_BUF 145 STAT_TOTAL_BUFS++; 146 if (size == 0) 147 STAT_DEFAULT_BUFS++; 148 if (size == 1) 149 STAT_WEIRD_BUFS++; 150 #endif 151 if (size == 0) 152 size = BUF_DEF_SIZE; 153 bp->inPtr = bp->endPtr = bp->buffer = emalloc(size); 154 bp->endPtr += size; 155 } 156 157 void 158 Buf_KillTrailingSpaces(bp) 159 Buffer bp; 160 { 161 while (bp->inPtr > bp->buffer + 1 && isspace(bp->inPtr[-1])) { 162 if (bp->inPtr[-2] == '\\') 163 break; 164 bp->inPtr--; 165 } 166 } 167