portable: release 0.93
[got-portable.git] / lib / buf.c
blobda410d35b967f8f509d65b491a4f06b7d2e695f0
1 /* $OpenBSD: buf.c,v 1.27 2016/10/16 13:35:51 okan Exp $ */
2 /*
3 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
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.
27 #include "got_compat.h"
29 #include <sys/queue.h>
30 #include <sys/stat.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "buf.h"
42 #include "got_error.h"
44 #define BUF_INCR 128
46 #define SIZE_LEFT(b) ((b)->cb_size - (b)->cb_len)
48 static const struct got_error *buf_grow(BUF *, size_t);
51 * Create a new buffer structure and return a pointer to it. This structure
52 * uses dynamically-allocated memory and must be freed with buf_free(), once
53 * the buffer is no longer needed.
55 const struct got_error *
56 buf_alloc(BUF **b, size_t len)
58 const struct got_error *err = NULL;
60 *b = malloc(sizeof(**b));
61 if (*b == NULL)
62 return got_error_from_errno("malloc");
63 /* Postpone creation of zero-sized buffers */
64 if (len > 0) {
65 (*b)->cb_buf = calloc(1, len);
66 if ((*b)->cb_buf == NULL) {
67 err = got_error_from_errno("calloc");
68 free(*b);
69 *b = NULL;
70 return err;
72 } else
73 (*b)->cb_buf = NULL;
75 (*b)->cb_size = len;
76 (*b)->cb_len = 0;
78 return NULL;
82 * Open the file specified by <path> and load all of its contents into a
83 * buffer.
84 * Returns the loaded buffer on success or NULL on failure.
85 * Sets errno on error.
87 const struct got_error *
88 buf_load(BUF **buf, FILE *f)
90 const struct got_error *err = NULL;
91 size_t ret;
92 size_t len;
93 u_char *bp;
94 struct stat st;
96 *buf = NULL;
98 if (fstat(fileno(f), &st) == -1) {
99 err = got_error_from_errno("fstat");
100 goto out;
103 if ((uintmax_t)st.st_size > SIZE_MAX) {
104 err = got_error_set_errno(EFBIG,
105 "cannot fit file into memory buffer");
106 goto out;
108 err = buf_alloc(buf, st.st_size);
109 if (err)
110 goto out;
111 for (bp = (*buf)->cb_buf; ; bp += ret) {
112 len = SIZE_LEFT(*buf);
113 ret = fread(bp, 1, len, f);
114 if (ret == 0 && ferror(f)) {
115 err = got_ferror(f, GOT_ERR_IO);
116 goto out;
117 } else if (ret == 0)
118 break;
120 (*buf)->cb_len += (size_t)ret;
123 out:
124 if (err) {
125 buf_free(*buf);
126 *buf = NULL;
128 return err;
131 const struct got_error *
132 buf_load_fd(BUF **buf, int fd)
134 const struct got_error *err = NULL;
135 unsigned char out[8192];
136 ssize_t r;
137 size_t len;
139 err = buf_alloc(buf, 8192);
140 if (err)
141 return err;
143 do {
144 r = read(fd, out, sizeof(out));
145 if (r == -1)
146 return got_error_from_errno("read");
147 if (r > 0) {
148 err = buf_append(&len, *buf, out, r);
149 if (err)
150 return err;
152 } while (r > 0);
154 return NULL;
157 void
158 buf_free(BUF *b)
160 if (b == NULL)
161 return;
162 free(b->cb_buf);
163 free(b);
167 * Free the buffer <b>'s structural information but do not free the contents
168 * of the buffer. Instead, they are returned and should be freed later using
169 * free().
171 void *
172 buf_release(BUF *b)
174 void *tmp;
176 tmp = b->cb_buf;
177 free(b);
178 return (tmp);
181 u_char *
182 buf_get(BUF *b)
184 return (b->cb_buf);
188 * Empty the contents of the buffer <b> and reset pointers.
190 void
191 buf_empty(BUF *b)
193 memset(b->cb_buf, 0, b->cb_size);
194 b->cb_len = 0;
197 /* Discard the leading <n> bytes from the buffer. */
198 const struct got_error *
199 buf_discard(BUF *b, size_t n)
201 if (n > b->cb_len)
202 return got_error(GOT_ERR_RANGE);
204 if (n == b->cb_len)
205 buf_empty(b);
206 else {
207 memmove(b->cb_buf, b->cb_buf + n, b->cb_len - n);
208 b->cb_len -= n;
211 return NULL;
215 * Append a single character <c> to the end of the buffer <b>.
217 const struct got_error *
218 buf_putc(BUF *b, int c)
220 const struct got_error *err = NULL;
221 u_char *bp;
223 if (SIZE_LEFT(b) == 0) {
224 err = buf_grow(b, BUF_INCR);
225 if (err)
226 return err;
228 bp = b->cb_buf + b->cb_len;
229 *bp = (u_char)c;
230 b->cb_len++;
231 return NULL;
235 * Append a string <s> to the end of buffer <b>.
237 const struct got_error *
238 buf_puts(size_t *newlen, BUF *b, const char *str)
240 return buf_append(newlen, b, str, strlen(str));
244 * Return u_char at buffer position <pos>.
246 u_char
247 buf_getc(BUF *b, size_t pos)
249 return (b->cb_buf[pos]);
253 * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
254 * buffer is too small to accept all data, it will get resized to an
255 * appropriate size to accept all data.
256 * Returns the number of bytes successfully appended to the buffer.
258 const struct got_error *
259 buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
261 const struct got_error *err = NULL;
262 size_t left, rlen;
263 u_char *bp;
265 left = SIZE_LEFT(b);
266 rlen = len;
268 if (left < len) {
269 err = buf_grow(b, len - left);
270 if (err)
271 return err;
273 bp = b->cb_buf + b->cb_len;
274 memcpy(bp, data, rlen);
275 b->cb_len += rlen;
277 *newlen = rlen;
278 return NULL;
282 * Returns the size of the buffer that is being used.
284 size_t
285 buf_len(BUF *b)
287 return (b->cb_len);
291 * Write the contents of the buffer <b> to the specified <fd>
294 buf_write_fd(BUF *b, int fd)
296 u_char *bp;
297 size_t len;
298 ssize_t ret;
300 len = b->cb_len;
301 bp = b->cb_buf;
303 do {
304 ret = write(fd, bp, len);
305 if (ret == -1) {
306 if (errno == EINTR || errno == EAGAIN)
307 continue;
308 return (-1);
311 len -= (size_t)ret;
312 bp += (size_t)ret;
313 } while (len > 0);
315 return (0);
319 * Write the contents of the buffer <b> to the file whose path is given in
320 * <path>. If the file does not exist, it is created with mode <mode>.
322 const struct got_error *
323 buf_write(BUF *b, const char *path, mode_t mode)
325 const struct got_error *err = NULL;
326 int fd;
327 open:
328 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, mode)) == -1) {
329 err = got_error_from_errno2("open", path);
330 if (errno == EACCES && unlink(path) != -1)
331 goto open;
332 else
333 return err;
336 if (buf_write_fd(b, fd) == -1) {
337 err = got_error_from_errno("buf_write_fd");
338 (void)unlink(path);
339 return err;
342 if (fchmod(fd, mode) < 0)
343 err = got_error_from_errno2("fchmod", path);
345 if (close(fd) == -1 && err == NULL)
346 err = got_error_from_errno2("close", path);
348 return err;
352 * Write the contents of the buffer <b> to a temporary file whose path is
353 * specified using <template> (see mkstemp.3).
354 * NB. This function will modify <template>, as per mkstemp
356 const struct got_error *
357 buf_write_stmp(BUF *b, char *template)
359 const struct got_error *err = NULL;
360 int fd;
362 if ((fd = mkstemp(template)) == -1)
363 return got_error_from_errno("mkstemp");
365 if (buf_write_fd(b, fd) == -1) {
366 err = got_error_from_errno("buf_write_fd");
367 (void)unlink(template);
370 if (close(fd) == -1 && err == NULL)
371 err = got_error_from_errno("close");
373 return err;
377 * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
378 * operation regardless of the result.
380 static const struct got_error *
381 buf_grow(BUF *b, size_t len)
383 u_char *buf;
384 buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
385 if (buf == NULL)
386 return got_error_from_errno("reallocarray");
387 b->cb_buf = buf;
388 b->cb_size += len;
389 return NULL;