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 74b37681 2019-02-07 stsp #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 af45e626 2019-02-08 stsp return NULL;
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 575e8218 2019-10-07 stsp buf_load(BUF **buf, const char *path)
88 575e8218 2019-10-07 stsp const struct got_error *err = NULL;
90 74b37681 2019-02-07 stsp ssize_t ret;
93 74b37681 2019-02-07 stsp struct stat st;
95 575e8218 2019-10-07 stsp *buf = NULL;
97 575e8218 2019-10-07 stsp fd = open(path, O_RDONLY, 0600);
98 575e8218 2019-10-07 stsp if (fd == -1)
99 575e8218 2019-10-07 stsp return got_error_from_errno2("open", path);
101 575e8218 2019-10-07 stsp if (fstat(fd, &st) == -1) {
102 575e8218 2019-10-07 stsp err = got_error_from_errno2("fstat", path);
106 74b37681 2019-02-07 stsp if ((uintmax_t)st.st_size > SIZE_MAX) {
107 575e8218 2019-10-07 stsp err = got_error_set_errno(EFBIG, path);
110 575e8218 2019-10-07 stsp err = buf_alloc(buf, st.st_size);
113 575e8218 2019-10-07 stsp for (bp = (*buf)->cb_buf; ; bp += (size_t)ret) {
114 575e8218 2019-10-07 stsp len = SIZE_LEFT(*buf);
115 74b37681 2019-02-07 stsp ret = read(fd, bp, len);
116 74b37681 2019-02-07 stsp if (ret == -1) {
117 575e8218 2019-10-07 stsp err = got_error_from_errno2("read", path);
119 74b37681 2019-02-07 stsp } else if (ret == 0)
122 575e8218 2019-10-07 stsp (*buf)->cb_len += (size_t)ret;
126 575e8218 2019-10-07 stsp if (close(fd) == -1 && err == NULL)
127 575e8218 2019-10-07 stsp err = got_error_from_errno2("close", path);
129 575e8218 2019-10-07 stsp buf_free(*buf);
130 575e8218 2019-10-07 stsp *buf = NULL;
132 575e8218 2019-10-07 stsp return err;
136 74b37681 2019-02-07 stsp buf_free(BUF *b)
138 74b37681 2019-02-07 stsp if (b == NULL)
140 74b37681 2019-02-07 stsp free(b->cb_buf);
145 74b37681 2019-02-07 stsp * Free the buffer <b>'s structural information but do not free the contents
146 74b37681 2019-02-07 stsp * of the buffer. Instead, they are returned and should be freed later using
150 74b37681 2019-02-07 stsp buf_release(BUF *b)
154 74b37681 2019-02-07 stsp tmp = b->cb_buf;
156 74b37681 2019-02-07 stsp return (tmp);
160 74b37681 2019-02-07 stsp buf_get(BUF *b)
162 74b37681 2019-02-07 stsp return (b->cb_buf);
166 74b37681 2019-02-07 stsp * Empty the contents of the buffer <b> and reset pointers.
169 74b37681 2019-02-07 stsp buf_empty(BUF *b)
171 74b37681 2019-02-07 stsp memset(b->cb_buf, 0, b->cb_size);
172 74b37681 2019-02-07 stsp b->cb_len = 0;
176 74b37681 2019-02-07 stsp * Append a single character <c> to the end of the buffer <b>.
178 af45e626 2019-02-08 stsp const struct got_error *
179 74b37681 2019-02-07 stsp buf_putc(BUF *b, int c)
181 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
182 74b37681 2019-02-07 stsp u_char *bp;
184 af45e626 2019-02-08 stsp if (SIZE_LEFT(b) == 0) {
185 af45e626 2019-02-08 stsp err = buf_grow(b, BUF_INCR);
187 af45e626 2019-02-08 stsp return err;
189 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
190 74b37681 2019-02-07 stsp *bp = (u_char)c;
191 74b37681 2019-02-07 stsp b->cb_len++;
192 af45e626 2019-02-08 stsp return NULL;
196 74b37681 2019-02-07 stsp * Append a string <s> to the end of buffer <b>.
198 af45e626 2019-02-08 stsp const struct got_error *
199 af45e626 2019-02-08 stsp buf_puts(size_t *newlen, BUF *b, const char *str)
201 af45e626 2019-02-08 stsp return buf_append(newlen, b, str, strlen(str));
205 74b37681 2019-02-07 stsp * Return u_char at buffer position <pos>.
208 74b37681 2019-02-07 stsp buf_getc(BUF *b, size_t pos)
210 74b37681 2019-02-07 stsp return (b->cb_buf[pos]);
214 74b37681 2019-02-07 stsp * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
215 74b37681 2019-02-07 stsp * buffer is too small to accept all data, it will get resized to an
216 74b37681 2019-02-07 stsp * appropriate size to accept all data.
217 74b37681 2019-02-07 stsp * Returns the number of bytes successfully appended to the buffer.
219 af45e626 2019-02-08 stsp const struct got_error *
220 af45e626 2019-02-08 stsp buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
222 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
223 74b37681 2019-02-07 stsp size_t left, rlen;
224 74b37681 2019-02-07 stsp u_char *bp;
226 74b37681 2019-02-07 stsp left = SIZE_LEFT(b);
227 74b37681 2019-02-07 stsp rlen = len;
229 af45e626 2019-02-08 stsp if (left < len) {
230 af45e626 2019-02-08 stsp err = buf_grow(b, len - left);
232 af45e626 2019-02-08 stsp return err;
234 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
235 74b37681 2019-02-07 stsp memcpy(bp, data, rlen);
236 74b37681 2019-02-07 stsp b->cb_len += rlen;
238 af45e626 2019-02-08 stsp *newlen = rlen;
239 af45e626 2019-02-08 stsp return NULL;
243 74b37681 2019-02-07 stsp * Returns the size of the buffer that is being used.
246 74b37681 2019-02-07 stsp buf_len(BUF *b)
248 74b37681 2019-02-07 stsp return (b->cb_len);
252 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the specified <fd>
255 74b37681 2019-02-07 stsp buf_write_fd(BUF *b, int fd)
257 74b37681 2019-02-07 stsp u_char *bp;
258 74b37681 2019-02-07 stsp size_t len;
259 74b37681 2019-02-07 stsp ssize_t ret;
261 74b37681 2019-02-07 stsp len = b->cb_len;
262 74b37681 2019-02-07 stsp bp = b->cb_buf;
265 74b37681 2019-02-07 stsp ret = write(fd, bp, len);
266 74b37681 2019-02-07 stsp if (ret == -1) {
267 74b37681 2019-02-07 stsp if (errno == EINTR || errno == EAGAIN)
269 74b37681 2019-02-07 stsp return (-1);
272 74b37681 2019-02-07 stsp len -= (size_t)ret;
273 74b37681 2019-02-07 stsp bp += (size_t)ret;
274 74b37681 2019-02-07 stsp } while (len > 0);
276 74b37681 2019-02-07 stsp return (0);
280 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the file whose path is given in
281 74b37681 2019-02-07 stsp * <path>. If the file does not exist, it is created with mode <mode>.
283 af45e626 2019-02-08 stsp const struct got_error *
284 74b37681 2019-02-07 stsp buf_write(BUF *b, const char *path, mode_t mode)
286 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
289 74b37681 2019-02-07 stsp if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
290 202329ae 2019-08-11 stsp err = got_error_from_errno2("open", path);
291 74b37681 2019-02-07 stsp if (errno == EACCES && unlink(path) != -1)
294 202329ae 2019-08-11 stsp return err;
297 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
298 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
299 74b37681 2019-02-07 stsp (void)unlink(path);
300 af45e626 2019-02-08 stsp return err;
303 74b37681 2019-02-07 stsp if (fchmod(fd, mode) < 0)
304 638f9024 2019-05-13 stsp err = got_error_from_errno2("fchmod", path);
306 3a6ce05a 2019-02-11 stsp if (close(fd) != 0 && err == NULL)
307 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path);
309 af45e626 2019-02-08 stsp return err;
313 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to a temporary file whose path is
314 74b37681 2019-02-07 stsp * specified using <template> (see mkstemp.3).
315 74b37681 2019-02-07 stsp * NB. This function will modify <template>, as per mkstemp
317 af45e626 2019-02-08 stsp const struct got_error *
318 96cbb597 2019-10-09 stsp buf_write_stmp(BUF *b, char *template)
320 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
323 74b37681 2019-02-07 stsp if ((fd = mkstemp(template)) == -1)
324 638f9024 2019-05-13 stsp return got_error_from_errno("mkstemp");
326 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
327 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
328 74b37681 2019-02-07 stsp (void)unlink(template);
331 3a6ce05a 2019-02-11 stsp if (close(fd) != 0 && err == NULL)
332 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
334 af45e626 2019-02-08 stsp return err;
338 74b37681 2019-02-07 stsp * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
339 74b37681 2019-02-07 stsp * operation regardless of the result.
341 af45e626 2019-02-08 stsp static const struct got_error *
342 74b37681 2019-02-07 stsp buf_grow(BUF *b, size_t len)
344 af45e626 2019-02-08 stsp u_char *buf;
345 af45e626 2019-02-08 stsp buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
346 af45e626 2019-02-08 stsp if (buf == NULL)
347 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
348 af45e626 2019-02-08 stsp b->cb_buf = buf;
349 74b37681 2019-02-07 stsp b->cb_size += len;
350 af45e626 2019-02-08 stsp return NULL;