Annotation of src/usr.bin/cvs/buf.c, Revision 1.5
1.5 ! deraadt 1: /* $OpenBSD: buf.c,v 1.4 2004/11/28 15:12:17 pat Exp $ */
1.1 jfb 2: /*
1.2 jfb 3: * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
1.1 jfb 4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include <sys/param.h>
28: #include <sys/stat.h>
29:
30: #include <stdio.h>
31: #include <ctype.h>
32: #include <fcntl.h>
33: #include <stdarg.h>
34: #include <unistd.h>
35: #include <string.h>
36: #include <stdlib.h>
37:
38: #include "buf.h"
39: #include "log.h"
40:
41:
42: #define BUF_INCR 128
43:
44:
45: struct cvs_buf {
46: u_int cb_flags;
47:
48: /* buffer handle and size */
49: void *cb_buf;
50: size_t cb_size;
51:
52: /* start and length of valid data in buffer */
53: u_char *cb_cur;
54: size_t cb_len;
55: };
56:
57:
58:
59: #define SIZE_LEFT(b) ((size_t)((u_char *)b->cb_buf - b->cb_cur) + b->cb_size)
60:
61:
62: static ssize_t cvs_buf_grow (BUF *, size_t);
63:
64:
65:
66: /*
67: * cvs_buf_alloc()
68: *
69: * Create a new buffer structure and return a pointer to it. This structure
70: * uses dynamically-allocated memory and must be freed with cvs_buf_free(),
71: * once the buffer is no longer needed.
72: */
73:
74: BUF*
75: cvs_buf_alloc(size_t len, u_int flags)
76: {
77: BUF *b;
78:
79: b = (BUF *)malloc(sizeof(*b));
80: if (b == NULL) {
81: cvs_log(LP_ERRNO, "failed to allocate buffer");
82: return (NULL);
83: }
84:
85: b->cb_buf = malloc(len);
86: if (b->cb_buf == NULL) {
87: cvs_log(LP_ERRNO, "failed to allocate buffer");
88: free(b);
89: return (NULL);
90: }
1.3 joris 91: memset(b->cb_buf, 0, len);
1.1 jfb 92:
93: b->cb_flags = flags;
94: b->cb_size = len;
95: b->cb_cur = (u_char *)b->cb_buf;
96: b->cb_len = 0;
97:
98: return (b);
99: }
100:
101:
102: /*
103: * cvs_buf_load()
104: *
105: * Open the file specified by <path> and load all of its contents into a
106: * buffer.
107: * Returns the loaded buffer on success, or NULL on failure.
108: */
109:
110: BUF*
111: cvs_buf_load(const char *path, u_int flags)
112: {
113: int fd;
114: ssize_t ret;
115: size_t len;
116: void *bp;
117: struct stat st;
118: BUF *buf;
119:
120: fd = open(path, O_RDONLY, 0600);
121: if (fd == -1) {
122: cvs_log(LP_ERRNO, "failed to open buffer source");
123: return (NULL);
124: }
125:
126: if (fstat(fd, &st) == -1) {
127: cvs_log(LP_ERRNO, "failed to stat buffer source");
128: (void)close(fd);
129: return (NULL);
130: }
131:
132: buf = cvs_buf_alloc((size_t)st.st_size, flags);
133: if (buf == NULL) {
134: (void)close(fd);
135: return (NULL);
136: }
137:
138: for (bp = buf->cb_cur; ; bp += (size_t)ret) {
139: len = MIN(SIZE_LEFT(buf), 4096);
140: ret = read(fd, bp, len);
141: if (ret == -1) {
142: cvs_log(LP_ERRNO, "read failed from buffer source");
143: (void)close(fd);
144: cvs_buf_free(bp);
145: return (NULL);
1.5 ! deraadt 146: } else if (ret == 0)
1.1 jfb 147: break;
148:
149: buf->cb_len += (size_t)ret;
150: }
151:
152: (void)close(fd);
153:
154: return (buf);
155: }
156:
157:
158: /*
159: * cvs_buf_free()
160: *
161: * Free the buffer <b> and all associated data.
162: */
163:
164: void
165: cvs_buf_free(BUF *b)
166: {
167: free(b->cb_buf);
168: free(b);
169: }
170:
171:
172: /*
173: * cvs_buf_release()
174: *
175: * Free the buffer <b>'s structural information but do not free the contents
176: * of the buffer. Instead, they are returned and should be freed later using
177: * free().
178: */
179:
180: void*
181: cvs_buf_release(BUF *b)
182: {
183: void *tmp;
184: tmp = b->cb_buf;
185: free(b);
186: return (tmp);
187: }
188:
189:
190: /*
191: * cvs_buf_empty()
192: *
193: * Empty the contents of the buffer <b> and reset pointers.
194: */
195:
196: void
197: cvs_buf_empty(BUF *b)
198: {
199: b->cb_cur = (u_char *)b->cb_buf;
200: b->cb_len = 0;
201: }
202:
203:
204: /*
205: * cvs_buf_copy()
206: *
207: * Copy the first <len> bytes of data in the buffer <b> starting at offset
208: * <off> in the destination buffer <dst>, which can accept up to <len> bytes.
209: * Returns the number of bytes successfully copied, or -1 on failure.
210: */
211:
212: ssize_t
213: cvs_buf_copy(BUF *b, size_t off, void *dst, size_t len)
214: {
215: size_t rc;
216:
217: if (off > b->cb_len)
218: return (-1);
219:
220: rc = MIN(len, (b->cb_len - off));
221: memcpy(dst, b->cb_buf, rc);
222:
223: return (ssize_t)rc;
224: }
225:
226:
227: /*
228: * cvs_buf_set()
229: *
230: * Set the contents of the buffer <b> to the first <len> bytes of data found
231: * at <src>. If the buffer was not created with BUF_AUTOEXT, as many bytes
232: * as possible will be copied in the buffer.
233: */
234:
235: int
236: cvs_buf_set(BUF *b, const void *src, size_t len, size_t off)
237: {
238: size_t rlen;
239:
240: if (b->cb_size < (len + off)) {
241: if ((b->cb_flags & BUF_AUTOEXT) && (cvs_buf_grow(b,
242: len + off - b->cb_size) < 0))
243: return (-1);
244: else
245: rlen = b->cb_size - off;
1.5 ! deraadt 246: } else
1.1 jfb 247: rlen = len;
248:
249: memcpy((b->cb_buf + off), src, rlen);
250:
251: if (b->cb_len == 0) {
252: b->cb_cur = b->cb_buf + off;
253: b->cb_len = rlen;
254: }
255:
256: return (int)rlen;
257: }
258:
259:
260: /*
261: * cvs_buf_putc()
262: *
263: * Append a single character <c> to the end of the buffer <b>.
264: * Returns 0 on success, or -1 on failure.
265: */
266:
267: int
268: cvs_buf_putc(BUF *b, int c)
269: {
270: u_char *bp;
271:
272: bp = b->cb_cur + b->cb_len;
273: if (bp == (b->cb_buf + b->cb_size)) {
274: /* extend */
275: if (!(b->cb_flags & BUF_AUTOEXT) ||
276: (cvs_buf_grow(b, BUF_INCR) < 0))
277: return (-1);
278:
279: /* the buffer might have been moved */
280: bp = b->cb_cur + b->cb_len;
281: }
282: *bp = (u_char)c;
283: b->cb_len++;
284:
285: return (0);
286: }
287:
288:
289: /*
290: * cvs_buf_append()
291: *
292: * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
293: * buffer is too small to accept all data, it will attempt to append as much
294: * data as possible, or if the BUF_AUTOEXT flag is set for the buffer, it
295: * will get resized to an appropriate size to accept all data.
296: * Returns the number of bytes successfully appended to the buffer, or -1
297: * on failure.
298: */
299:
300: ssize_t
301: cvs_buf_append(BUF *b, const void *data, size_t len)
302: {
303: size_t left, rlen;
304: void *bp, *bep;
305:
306: bp = b->cb_cur + b->cb_len;
307: bep = b->cb_buf + b->cb_size;
308: left = bep - bp;
309: rlen = len;
310:
311: if (left < len) {
312: if (b->cb_flags & BUF_AUTOEXT) {
313: if (cvs_buf_grow(b, len - left) < 0)
314: return (-1);
315: bp = b->cb_cur + b->cb_len;
1.5 ! deraadt 316: } else
1.1 jfb 317: rlen = bep - bp;
318: }
319:
320: memcpy(bp, data, rlen);
321: b->cb_len += rlen;
322:
323: return (rlen);
324: }
325:
326:
327: /*
328: * cvs_buf_fappend()
329: *
330: */
331:
332: int
333: cvs_buf_fappend(BUF *b, const char *fmt, ...)
334: {
335: int ret;
336: char *str;
337: va_list vap;
338:
339: va_start(vap, fmt);
1.4 pat 340: ret = vasprintf(&str, fmt, vap);
341: va_end(vap);
1.1 jfb 342:
343: if (ret == -1) {
344: cvs_log(LP_ERRNO, "failed to format data");
345: return (-1);
346: }
347:
348: ret = cvs_buf_append(b, str, ret);
349: free(str);
350: return (ret);
351: }
352:
353:
354: /*
355: * cvs_buf_size()
356: *
357: * Returns the size of the buffer that is being used.
358: */
359:
360: size_t
361: cvs_buf_size(BUF *b)
362: {
363: return (b->cb_len);
364: }
365:
366:
367: /*
368: * cvs_buf_peek()
369: *
370: * Peek at the contents of the buffer <b> at offset <off>.
371: */
372:
373: const void*
374: cvs_buf_peek(BUF *b, size_t off)
375: {
376: if (off >= b->cb_len)
377: return (NULL);
378:
379: return (b->cb_buf + off);
380: }
381:
382:
383: /*
384: * cvs_buf_write()
385: *
386: * Write the contents of the buffer <b> to the file whose path is given in
387: * <path>. If the file does not exist, it is created with mode <mode>.
388: */
389:
390: int
391: cvs_buf_write(BUF *b, const char *path, mode_t mode)
392: {
393: int fd;
394: u_char *bp;
395: size_t len;
396: ssize_t ret;
397:
398: fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
399: if (fd == -1) {
400: cvs_log(LP_ERRNO, "failed to open file `%s'", path);
401: return (-1);
402: }
403:
404: len = b->cb_len;
405: bp = b->cb_cur;
406:
407: do {
408: ret = write(fd, bp, MIN(len, 8192));
409: if (ret == -1) {
410: cvs_log(LP_ERRNO, "failed to write to file `%s'", path);
411: (void)close(fd);
412: (void)unlink(path);
413: return (-1);
414: }
415:
416: len -= (size_t)ret;
417: bp += (size_t)ret;
418: } while (len > 0);
419:
420: (void)close(fd);
421:
422: return (0);
423: }
424:
425:
426: /*
427: * cvs_buf_grow()
428: *
429: * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
430: * operation regardless of the result.
431: * Returns the new size on success, or -1 on failure.
432: */
433:
434: static ssize_t
435: cvs_buf_grow(BUF *b, size_t len)
436: {
437: void *tmp;
438: size_t diff;
439:
440: diff = b->cb_cur - (u_char *)b->cb_buf;
441: tmp = realloc(b->cb_buf, b->cb_size + len);
442: if (tmp == NULL) {
443: cvs_log(LP_ERRNO, "failed to grow buffer");
444: return (-1);
445: }
446: b->cb_buf = (u_char *)tmp;
447: b->cb_size += len;
448:
449: /* readjust pointers in case the buffer moved in memory */
450: b->cb_cur = b->cb_buf + diff;
451:
452: return (ssize_t)b->cb_size;
453: }