1 74b37681 2019-02-07 stsp /* $OpenBSD: buf.c,v 1.27 2016/10/16 13:35:51 okan Exp $ */
3 74b37681 2019-02-07 stsp * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
4 74b37681 2019-02-07 stsp * All rights reserved.
6 74b37681 2019-02-07 stsp * Redistribution and use in source and binary forms, with or without
7 74b37681 2019-02-07 stsp * modification, are permitted provided that the following conditions
10 74b37681 2019-02-07 stsp * 1. Redistributions of source code must retain the above copyright
11 74b37681 2019-02-07 stsp * notice, this list of conditions and the following disclaimer.
12 74b37681 2019-02-07 stsp * 2. The name of the author may not be used to endorse or promote products
13 74b37681 2019-02-07 stsp * derived from this software without specific prior written permission.
15 74b37681 2019-02-07 stsp * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 74b37681 2019-02-07 stsp * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 74b37681 2019-02-07 stsp * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 74b37681 2019-02-07 stsp * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 74b37681 2019-02-07 stsp * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 74b37681 2019-02-07 stsp * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 74b37681 2019-02-07 stsp * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 74b37681 2019-02-07 stsp * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 74b37681 2019-02-07 stsp * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 74b37681 2019-02-07 stsp * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
28 74b37681 2019-02-07 stsp #include <sys/stat.h>
30 74b37681 2019-02-07 stsp #include <errno.h>
31 74b37681 2019-02-07 stsp #include <fcntl.h>
32 74b37681 2019-02-07 stsp #include <stdint.h>
33 74b37681 2019-02-07 stsp #include <stdio.h>
34 74b37681 2019-02-07 stsp #include <stdlib.h>
35 74b37681 2019-02-07 stsp #include <string.h>
36 74b37681 2019-02-07 stsp #include <unistd.h>
38 74b37681 2019-02-07 stsp #include "buf.h"
40 af45e626 2019-02-08 stsp #include "got_error.h"
42 74b37681 2019-02-07 stsp #define BUF_INCR 128
44 575e8218 2019-10-07 stsp #define SIZE_LEFT(b) ((b)->cb_size - (b)->cb_len)
46 af45e626 2019-02-08 stsp static const struct got_error *buf_grow(BUF *, size_t);
49 74b37681 2019-02-07 stsp * Create a new buffer structure and return a pointer to it. This structure
50 74b37681 2019-02-07 stsp * uses dynamically-allocated memory and must be freed with buf_free(), once
51 74b37681 2019-02-07 stsp * the buffer is no longer needed.
53 575e8218 2019-10-07 stsp const struct got_error *
54 575e8218 2019-10-07 stsp buf_alloc(BUF **b, size_t len)
56 575e8218 2019-10-07 stsp const struct got_error *err = NULL;
58 575e8218 2019-10-07 stsp *b = malloc(sizeof(**b));
59 575e8218 2019-10-07 stsp if (*b == NULL)
60 6dc6a24c 2022-07-06 thomas return got_error_from_errno("malloc");
61 74b37681 2019-02-07 stsp /* Postpone creation of zero-sized buffers */
62 af45e626 2019-02-08 stsp if (len > 0) {
63 575e8218 2019-10-07 stsp (*b)->cb_buf = calloc(1, len);
64 575e8218 2019-10-07 stsp if ((*b)->cb_buf == NULL) {
65 575e8218 2019-10-07 stsp err = got_error_from_errno("calloc");
71 575e8218 2019-10-07 stsp (*b)->cb_buf = NULL;
73 575e8218 2019-10-07 stsp (*b)->cb_size = len;
74 575e8218 2019-10-07 stsp (*b)->cb_len = 0;
76 575e8218 2019-10-07 stsp return NULL;
80 74b37681 2019-02-07 stsp * Open the file specified by <path> and load all of its contents into a
82 74b37681 2019-02-07 stsp * Returns the loaded buffer on success or NULL on failure.
83 74b37681 2019-02-07 stsp * Sets errno on error.
85 575e8218 2019-10-07 stsp const struct got_error *
86 db590691 2021-06-02 stsp buf_load(BUF **buf, FILE *f)
88 575e8218 2019-10-07 stsp const struct got_error *err = NULL;
92 74b37681 2019-02-07 stsp struct stat st;
94 575e8218 2019-10-07 stsp *buf = NULL;
96 db590691 2021-06-02 stsp if (fstat(fileno(f), &st) == -1) {
97 db590691 2021-06-02 stsp err = got_error_from_errno("fstat");
101 74b37681 2019-02-07 stsp if ((uintmax_t)st.st_size > SIZE_MAX) {
102 db590691 2021-06-02 stsp err = got_error_set_errno(EFBIG,
103 db590691 2021-06-02 stsp "cannot fit file into memory buffer");
106 575e8218 2019-10-07 stsp err = buf_alloc(buf, st.st_size);
109 db590691 2021-06-02 stsp for (bp = (*buf)->cb_buf; ; bp += ret) {
110 575e8218 2019-10-07 stsp len = SIZE_LEFT(*buf);
111 db590691 2021-06-02 stsp ret = fread(bp, 1, len, f);
112 db590691 2021-06-02 stsp if (ret == 0 && ferror(f)) {
113 db590691 2021-06-02 stsp err = got_ferror(f, GOT_ERR_IO);
115 74b37681 2019-02-07 stsp } else if (ret == 0)
118 575e8218 2019-10-07 stsp (*buf)->cb_len += (size_t)ret;
123 575e8218 2019-10-07 stsp buf_free(*buf);
124 575e8218 2019-10-07 stsp *buf = NULL;
126 575e8218 2019-10-07 stsp return err;
129 3cb93379 2022-07-12 thomas const struct got_error *
130 3cb93379 2022-07-12 thomas buf_load_fd(BUF **buf, int fd)
132 3cb93379 2022-07-12 thomas const struct got_error *err = NULL;
133 3cb93379 2022-07-12 thomas unsigned char out[8192];
134 3cb93379 2022-07-12 thomas ssize_t r;
135 3cb93379 2022-07-12 thomas size_t len;
137 3cb93379 2022-07-12 thomas err = buf_alloc(buf, 8192);
139 3cb93379 2022-07-12 thomas return err;
142 3cb93379 2022-07-12 thomas r = read(fd, out, sizeof(out));
143 3cb93379 2022-07-12 thomas if (r == -1)
144 3cb93379 2022-07-12 thomas return got_error_from_errno("read");
145 3cb93379 2022-07-12 thomas if (r > 0) {
146 3cb93379 2022-07-12 thomas err = buf_append(&len, *buf, out, r);
148 3cb93379 2022-07-12 thomas return err;
150 3cb93379 2022-07-12 thomas } while (r > 0);
152 3cb93379 2022-07-12 thomas return NULL;
156 74b37681 2019-02-07 stsp buf_free(BUF *b)
158 74b37681 2019-02-07 stsp if (b == NULL)
160 74b37681 2019-02-07 stsp free(b->cb_buf);
165 74b37681 2019-02-07 stsp * Free the buffer <b>'s structural information but do not free the contents
166 74b37681 2019-02-07 stsp * of the buffer. Instead, they are returned and should be freed later using
170 74b37681 2019-02-07 stsp buf_release(BUF *b)
174 74b37681 2019-02-07 stsp tmp = b->cb_buf;
176 74b37681 2019-02-07 stsp return (tmp);
180 74b37681 2019-02-07 stsp buf_get(BUF *b)
182 74b37681 2019-02-07 stsp return (b->cb_buf);
186 74b37681 2019-02-07 stsp * Empty the contents of the buffer <b> and reset pointers.
189 74b37681 2019-02-07 stsp buf_empty(BUF *b)
191 74b37681 2019-02-07 stsp memset(b->cb_buf, 0, b->cb_size);
192 74b37681 2019-02-07 stsp b->cb_len = 0;
196 74b37681 2019-02-07 stsp * Append a single character <c> to the end of the buffer <b>.
198 af45e626 2019-02-08 stsp const struct got_error *
199 74b37681 2019-02-07 stsp buf_putc(BUF *b, int c)
201 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
202 74b37681 2019-02-07 stsp u_char *bp;
204 af45e626 2019-02-08 stsp if (SIZE_LEFT(b) == 0) {
205 af45e626 2019-02-08 stsp err = buf_grow(b, BUF_INCR);
207 af45e626 2019-02-08 stsp return err;
209 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
210 74b37681 2019-02-07 stsp *bp = (u_char)c;
211 74b37681 2019-02-07 stsp b->cb_len++;
212 af45e626 2019-02-08 stsp return NULL;
216 74b37681 2019-02-07 stsp * Append a string <s> to the end of buffer <b>.
218 af45e626 2019-02-08 stsp const struct got_error *
219 af45e626 2019-02-08 stsp buf_puts(size_t *newlen, BUF *b, const char *str)
221 af45e626 2019-02-08 stsp return buf_append(newlen, b, str, strlen(str));
225 74b37681 2019-02-07 stsp * Return u_char at buffer position <pos>.
228 74b37681 2019-02-07 stsp buf_getc(BUF *b, size_t pos)
230 74b37681 2019-02-07 stsp return (b->cb_buf[pos]);
234 74b37681 2019-02-07 stsp * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
235 74b37681 2019-02-07 stsp * buffer is too small to accept all data, it will get resized to an
236 74b37681 2019-02-07 stsp * appropriate size to accept all data.
237 74b37681 2019-02-07 stsp * Returns the number of bytes successfully appended to the buffer.
239 af45e626 2019-02-08 stsp const struct got_error *
240 af45e626 2019-02-08 stsp buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
242 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
243 74b37681 2019-02-07 stsp size_t left, rlen;
244 74b37681 2019-02-07 stsp u_char *bp;
246 74b37681 2019-02-07 stsp left = SIZE_LEFT(b);
247 74b37681 2019-02-07 stsp rlen = len;
249 af45e626 2019-02-08 stsp if (left < len) {
250 af45e626 2019-02-08 stsp err = buf_grow(b, len - left);
252 af45e626 2019-02-08 stsp return err;
254 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
255 74b37681 2019-02-07 stsp memcpy(bp, data, rlen);
256 74b37681 2019-02-07 stsp b->cb_len += rlen;
258 af45e626 2019-02-08 stsp *newlen = rlen;
259 af45e626 2019-02-08 stsp return NULL;
263 74b37681 2019-02-07 stsp * Returns the size of the buffer that is being used.
266 74b37681 2019-02-07 stsp buf_len(BUF *b)
268 74b37681 2019-02-07 stsp return (b->cb_len);
272 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the specified <fd>
275 74b37681 2019-02-07 stsp buf_write_fd(BUF *b, int fd)
277 74b37681 2019-02-07 stsp u_char *bp;
278 74b37681 2019-02-07 stsp size_t len;
279 74b37681 2019-02-07 stsp ssize_t ret;
281 74b37681 2019-02-07 stsp len = b->cb_len;
282 74b37681 2019-02-07 stsp bp = b->cb_buf;
285 74b37681 2019-02-07 stsp ret = write(fd, bp, len);
286 74b37681 2019-02-07 stsp if (ret == -1) {
287 74b37681 2019-02-07 stsp if (errno == EINTR || errno == EAGAIN)
289 74b37681 2019-02-07 stsp return (-1);
292 74b37681 2019-02-07 stsp len -= (size_t)ret;
293 74b37681 2019-02-07 stsp bp += (size_t)ret;
294 74b37681 2019-02-07 stsp } while (len > 0);
296 74b37681 2019-02-07 stsp return (0);
300 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the file whose path is given in
301 74b37681 2019-02-07 stsp * <path>. If the file does not exist, it is created with mode <mode>.
303 af45e626 2019-02-08 stsp const struct got_error *
304 74b37681 2019-02-07 stsp buf_write(BUF *b, const char *path, mode_t mode)
306 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
309 06340621 2021-12-31 thomas if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, mode)) == -1) {
310 202329ae 2019-08-11 stsp err = got_error_from_errno2("open", path);
311 74b37681 2019-02-07 stsp if (errno == EACCES && unlink(path) != -1)
314 202329ae 2019-08-11 stsp return err;
317 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
318 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
319 74b37681 2019-02-07 stsp (void)unlink(path);
320 af45e626 2019-02-08 stsp return err;
323 74b37681 2019-02-07 stsp if (fchmod(fd, mode) < 0)
324 638f9024 2019-05-13 stsp err = got_error_from_errno2("fchmod", path);
326 08578a35 2021-01-22 stsp if (close(fd) == -1 && err == NULL)
327 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path);
329 af45e626 2019-02-08 stsp return err;
333 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to a temporary file whose path is
334 74b37681 2019-02-07 stsp * specified using <template> (see mkstemp.3).
335 74b37681 2019-02-07 stsp * NB. This function will modify <template>, as per mkstemp
337 af45e626 2019-02-08 stsp const struct got_error *
338 96cbb597 2019-10-09 stsp buf_write_stmp(BUF *b, char *template)
340 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
343 74b37681 2019-02-07 stsp if ((fd = mkstemp(template)) == -1)
344 638f9024 2019-05-13 stsp return got_error_from_errno("mkstemp");
346 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
347 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
348 74b37681 2019-02-07 stsp (void)unlink(template);
351 08578a35 2021-01-22 stsp if (close(fd) == -1 && err == NULL)
352 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
354 af45e626 2019-02-08 stsp return err;
358 74b37681 2019-02-07 stsp * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
359 74b37681 2019-02-07 stsp * operation regardless of the result.
361 af45e626 2019-02-08 stsp static const struct got_error *
362 74b37681 2019-02-07 stsp buf_grow(BUF *b, size_t len)
364 af45e626 2019-02-08 stsp u_char *buf;
365 af45e626 2019-02-08 stsp buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
366 af45e626 2019-02-08 stsp if (buf == NULL)
367 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
368 af45e626 2019-02-08 stsp b->cb_buf = buf;
369 74b37681 2019-02-07 stsp b->cb_size += len;
370 af45e626 2019-02-08 stsp return NULL;