portable: configure: use config.h
[got-portable.git] / lib / buf.c
blob050225122b5b7622f11da5c0a7196021b719fe68
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 <sys/queue.h>
28 #include <sys/stat.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "buf.h"
40 #include "got_error.h"
42 #define BUF_INCR 128
44 #define SIZE_LEFT(b) ((b)->cb_size - (b)->cb_len)
46 static const struct got_error *buf_grow(BUF *, size_t);
49 * Create a new buffer structure and return a pointer to it. This structure
50 * uses dynamically-allocated memory and must be freed with buf_free(), once
51 * the buffer is no longer needed.
53 const struct got_error *
54 buf_alloc(BUF **b, size_t len)
56 const struct got_error *err = NULL;
58 *b = malloc(sizeof(**b));
59 if (*b == NULL)
60 return got_error_from_errno("malloc");
61 /* Postpone creation of zero-sized buffers */
62 if (len > 0) {
63 (*b)->cb_buf = calloc(1, len);
64 if ((*b)->cb_buf == NULL) {
65 err = got_error_from_errno("calloc");
66 free(*b);
67 *b = NULL;
68 return err;
70 } else
71 (*b)->cb_buf = NULL;
73 (*b)->cb_size = len;
74 (*b)->cb_len = 0;
76 return NULL;
80 * Open the file specified by <path> and load all of its contents into a
81 * buffer.
82 * Returns the loaded buffer on success or NULL on failure.
83 * Sets errno on error.
85 const struct got_error *
86 buf_load(BUF **buf, FILE *f)
88 const struct got_error *err = NULL;
89 size_t ret;
90 size_t len;
91 u_char *bp;
92 struct stat st;
94 *buf = NULL;
96 if (fstat(fileno(f), &st) == -1) {
97 err = got_error_from_errno("fstat");
98 goto out;
101 if ((uintmax_t)st.st_size > SIZE_MAX) {
102 err = got_error_set_errno(EFBIG,
103 "cannot fit file into memory buffer");
104 goto out;
106 err = buf_alloc(buf, st.st_size);
107 if (err)
108 goto out;
109 for (bp = (*buf)->cb_buf; ; bp += ret) {
110 len = SIZE_LEFT(*buf);
111 ret = fread(bp, 1, len, f);
112 if (ret == 0 && ferror(f)) {
113 err = got_ferror(f, GOT_ERR_IO);
114 goto out;
115 } else if (ret == 0)
116 break;
118 (*buf)->cb_len += (size_t)ret;
121 out:
122 if (err) {
123 buf_free(*buf);
124 *buf = NULL;
126 return err;
129 const struct got_error *
130 buf_load_fd(BUF **buf, int fd)
132 const struct got_error *err = NULL;
133 unsigned char out[8192];
134 ssize_t r;
135 size_t len;
137 err = buf_alloc(buf, 8192);
138 if (err)
139 return err;
141 do {
142 r = read(fd, out, sizeof(out));
143 if (r == -1)
144 return got_error_from_errno("read");
145 if (r > 0) {
146 err = buf_append(&len, *buf, out, r);
147 if (err)
148 return err;
150 } while (r > 0);
152 return NULL;
155 void
156 buf_free(BUF *b)
158 if (b == NULL)
159 return;
160 free(b->cb_buf);
161 free(b);
165 * Free the buffer <b>'s structural information but do not free the contents
166 * of the buffer. Instead, they are returned and should be freed later using
167 * free().
169 void *
170 buf_release(BUF *b)
172 void *tmp;
174 tmp = b->cb_buf;
175 free(b);
176 return (tmp);
179 u_char *
180 buf_get(BUF *b)
182 return (b->cb_buf);
186 * Empty the contents of the buffer <b> and reset pointers.
188 void
189 buf_empty(BUF *b)
191 memset(b->cb_buf, 0, b->cb_size);
192 b->cb_len = 0;
195 /* Discard the leading <n> bytes from the buffer. */
196 const struct got_error *
197 buf_discard(BUF *b, size_t n)
199 if (n > b->cb_len)
200 return got_error(GOT_ERR_RANGE);
202 if (n == b->cb_len)
203 buf_empty(b);
204 else {
205 memmove(b->cb_buf, b->cb_buf + n, b->cb_len - n);
206 b->cb_len -= n;
209 return NULL;
213 * Append a single character <c> to the end of the buffer <b>.
215 const struct got_error *
216 buf_putc(BUF *b, int c)
218 const struct got_error *err = NULL;
219 u_char *bp;
221 if (SIZE_LEFT(b) == 0) {
222 err = buf_grow(b, BUF_INCR);
223 if (err)
224 return err;
226 bp = b->cb_buf + b->cb_len;
227 *bp = (u_char)c;
228 b->cb_len++;
229 return NULL;
233 * Append a string <s> to the end of buffer <b>.
235 const struct got_error *
236 buf_puts(size_t *newlen, BUF *b, const char *str)
238 return buf_append(newlen, b, str, strlen(str));
242 * Return u_char at buffer position <pos>.
244 u_char
245 buf_getc(BUF *b, size_t pos)
247 return (b->cb_buf[pos]);
251 * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
252 * buffer is too small to accept all data, it will get resized to an
253 * appropriate size to accept all data.
254 * Returns the number of bytes successfully appended to the buffer.
256 const struct got_error *
257 buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
259 const struct got_error *err = NULL;
260 size_t left, rlen;
261 u_char *bp;
263 left = SIZE_LEFT(b);
264 rlen = len;
266 if (left < len) {
267 err = buf_grow(b, len - left);
268 if (err)
269 return err;
271 bp = b->cb_buf + b->cb_len;
272 memcpy(bp, data, rlen);
273 b->cb_len += rlen;
275 *newlen = rlen;
276 return NULL;
280 * Returns the size of the buffer that is being used.
282 size_t
283 buf_len(BUF *b)
285 return (b->cb_len);
289 * Write the contents of the buffer <b> to the specified <fd>
292 buf_write_fd(BUF *b, int fd)
294 u_char *bp;
295 size_t len;
296 ssize_t ret;
298 len = b->cb_len;
299 bp = b->cb_buf;
301 do {
302 ret = write(fd, bp, len);
303 if (ret == -1) {
304 if (errno == EINTR || errno == EAGAIN)
305 continue;
306 return (-1);
309 len -= (size_t)ret;
310 bp += (size_t)ret;
311 } while (len > 0);
313 return (0);
317 * Write the contents of the buffer <b> to the file whose path is given in
318 * <path>. If the file does not exist, it is created with mode <mode>.
320 const struct got_error *
321 buf_write(BUF *b, const char *path, mode_t mode)
323 const struct got_error *err = NULL;
324 int fd;
325 open:
326 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, mode)) == -1) {
327 err = got_error_from_errno2("open", path);
328 if (errno == EACCES && unlink(path) != -1)
329 goto open;
330 else
331 return err;
334 if (buf_write_fd(b, fd) == -1) {
335 err = got_error_from_errno("buf_write_fd");
336 (void)unlink(path);
337 return err;
340 if (fchmod(fd, mode) < 0)
341 err = got_error_from_errno2("fchmod", path);
343 if (close(fd) == -1 && err == NULL)
344 err = got_error_from_errno2("close", path);
346 return err;
350 * Write the contents of the buffer <b> to a temporary file whose path is
351 * specified using <template> (see mkstemp.3).
352 * NB. This function will modify <template>, as per mkstemp
354 const struct got_error *
355 buf_write_stmp(BUF *b, char *template)
357 const struct got_error *err = NULL;
358 int fd;
360 if ((fd = mkstemp(template)) == -1)
361 return got_error_from_errno("mkstemp");
363 if (buf_write_fd(b, fd) == -1) {
364 err = got_error_from_errno("buf_write_fd");
365 (void)unlink(template);
368 if (close(fd) == -1 && err == NULL)
369 err = got_error_from_errno("close");
371 return err;
375 * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
376 * operation regardless of the result.
378 static const struct got_error *
379 buf_grow(BUF *b, size_t len)
381 u_char *buf;
382 buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
383 if (buf == NULL)
384 return got_error_from_errno("reallocarray");
385 b->cb_buf = buf;
386 b->cb_size += len;
387 return NULL;