Blob


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.
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 */
27 #include <sys/stat.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
37 #include "buf.h"
39 #include "got_error.h"
41 #define BUF_INCR 128
43 #define SIZE_LEFT(b) ((b)->cb_size - (b)->cb_len)
45 static const struct got_error *buf_grow(BUF *, size_t);
47 /*
48 * Create a new buffer structure and return a pointer to it. This structure
49 * uses dynamically-allocated memory and must be freed with buf_free(), once
50 * the buffer is no longer needed.
51 */
52 const struct got_error *
53 buf_alloc(BUF **b, size_t len)
54 {
55 const struct got_error *err = NULL;
57 *b = malloc(sizeof(**b));
58 if (*b == NULL)
59 return got_error_from_errno("malloc");
60 /* Postpone creation of zero-sized buffers */
61 if (len > 0) {
62 (*b)->cb_buf = calloc(1, len);
63 if ((*b)->cb_buf == NULL) {
64 err = got_error_from_errno("calloc");
65 free(*b);
66 *b = NULL;
67 return err;
68 }
69 } else
70 (*b)->cb_buf = NULL;
72 (*b)->cb_size = len;
73 (*b)->cb_len = 0;
75 return NULL;
76 }
78 /*
79 * Open the file specified by <path> and load all of its contents into a
80 * buffer.
81 * Returns the loaded buffer on success or NULL on failure.
82 * Sets errno on error.
83 */
84 const struct got_error *
85 buf_load(BUF **buf, FILE *f)
86 {
87 const struct got_error *err = NULL;
88 size_t ret;
89 size_t len;
90 u_char *bp;
91 struct stat st;
93 *buf = NULL;
95 if (fstat(fileno(f), &st) == -1) {
96 err = got_error_from_errno("fstat");
97 goto out;
98 }
100 if ((uintmax_t)st.st_size > SIZE_MAX) {
101 err = got_error_set_errno(EFBIG,
102 "cannot fit file into memory buffer");
103 goto out;
105 err = buf_alloc(buf, st.st_size);
106 if (err)
107 goto out;
108 for (bp = (*buf)->cb_buf; ; bp += ret) {
109 len = SIZE_LEFT(*buf);
110 ret = fread(bp, 1, len, f);
111 if (ret == 0 && ferror(f)) {
112 err = got_ferror(f, GOT_ERR_IO);
113 goto out;
114 } else if (ret == 0)
115 break;
117 (*buf)->cb_len += (size_t)ret;
120 out:
121 if (err) {
122 buf_free(*buf);
123 *buf = NULL;
125 return err;
128 const struct got_error *
129 buf_load_fd(BUF **buf, int fd)
131 const struct got_error *err = NULL;
132 unsigned char out[8192];
133 ssize_t r;
134 size_t len;
136 err = buf_alloc(buf, 8192);
137 if (err)
138 return err;
140 do {
141 r = read(fd, out, sizeof(out));
142 if (r == -1)
143 return got_error_from_errno("read");
144 if (r > 0) {
145 err = buf_append(&len, *buf, out, r);
146 if (err)
147 return err;
149 } while (r > 0);
151 return NULL;
154 void
155 buf_free(BUF *b)
157 if (b == NULL)
158 return;
159 free(b->cb_buf);
160 free(b);
163 /*
164 * Free the buffer <b>'s structural information but do not free the contents
165 * of the buffer. Instead, they are returned and should be freed later using
166 * free().
167 */
168 void *
169 buf_release(BUF *b)
171 void *tmp;
173 tmp = b->cb_buf;
174 free(b);
175 return (tmp);
178 u_char *
179 buf_get(BUF *b)
181 return (b->cb_buf);
184 /*
185 * Empty the contents of the buffer <b> and reset pointers.
186 */
187 void
188 buf_empty(BUF *b)
190 memset(b->cb_buf, 0, b->cb_size);
191 b->cb_len = 0;
194 /*
195 * Append a single character <c> to the end of the buffer <b>.
196 */
197 const struct got_error *
198 buf_putc(BUF *b, int c)
200 const struct got_error *err = NULL;
201 u_char *bp;
203 if (SIZE_LEFT(b) == 0) {
204 err = buf_grow(b, BUF_INCR);
205 if (err)
206 return err;
208 bp = b->cb_buf + b->cb_len;
209 *bp = (u_char)c;
210 b->cb_len++;
211 return NULL;
214 /*
215 * Append a string <s> to the end of buffer <b>.
216 */
217 const struct got_error *
218 buf_puts(size_t *newlen, BUF *b, const char *str)
220 return buf_append(newlen, b, str, strlen(str));
223 /*
224 * Return u_char at buffer position <pos>.
225 */
226 u_char
227 buf_getc(BUF *b, size_t pos)
229 return (b->cb_buf[pos]);
232 /*
233 * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
234 * buffer is too small to accept all data, it will get resized to an
235 * appropriate size to accept all data.
236 * Returns the number of bytes successfully appended to the buffer.
237 */
238 const struct got_error *
239 buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
241 const struct got_error *err = NULL;
242 size_t left, rlen;
243 u_char *bp;
245 left = SIZE_LEFT(b);
246 rlen = len;
248 if (left < len) {
249 err = buf_grow(b, len - left);
250 if (err)
251 return err;
253 bp = b->cb_buf + b->cb_len;
254 memcpy(bp, data, rlen);
255 b->cb_len += rlen;
257 *newlen = rlen;
258 return NULL;
261 /*
262 * Returns the size of the buffer that is being used.
263 */
264 size_t
265 buf_len(BUF *b)
267 return (b->cb_len);
270 /*
271 * Write the contents of the buffer <b> to the specified <fd>
272 */
273 int
274 buf_write_fd(BUF *b, int fd)
276 u_char *bp;
277 size_t len;
278 ssize_t ret;
280 len = b->cb_len;
281 bp = b->cb_buf;
283 do {
284 ret = write(fd, bp, len);
285 if (ret == -1) {
286 if (errno == EINTR || errno == EAGAIN)
287 continue;
288 return (-1);
291 len -= (size_t)ret;
292 bp += (size_t)ret;
293 } while (len > 0);
295 return (0);
298 /*
299 * Write the contents of the buffer <b> to the file whose path is given in
300 * <path>. If the file does not exist, it is created with mode <mode>.
301 */
302 const struct got_error *
303 buf_write(BUF *b, const char *path, mode_t mode)
305 const struct got_error *err = NULL;
306 int fd;
307 open:
308 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, mode)) == -1) {
309 err = got_error_from_errno2("open", path);
310 if (errno == EACCES && unlink(path) != -1)
311 goto open;
312 else
313 return err;
316 if (buf_write_fd(b, fd) == -1) {
317 err = got_error_from_errno("buf_write_fd");
318 (void)unlink(path);
319 return err;
322 if (fchmod(fd, mode) < 0)
323 err = got_error_from_errno2("fchmod", path);
325 if (close(fd) == -1 && err == NULL)
326 err = got_error_from_errno2("close", path);
328 return err;
331 /*
332 * Write the contents of the buffer <b> to a temporary file whose path is
333 * specified using <template> (see mkstemp.3).
334 * NB. This function will modify <template>, as per mkstemp
335 */
336 const struct got_error *
337 buf_write_stmp(BUF *b, char *template)
339 const struct got_error *err = NULL;
340 int fd;
342 if ((fd = mkstemp(template)) == -1)
343 return got_error_from_errno("mkstemp");
345 if (buf_write_fd(b, fd) == -1) {
346 err = got_error_from_errno("buf_write_fd");
347 (void)unlink(template);
350 if (close(fd) == -1 && err == NULL)
351 err = got_error_from_errno("close");
353 return err;
356 /*
357 * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
358 * operation regardless of the result.
359 */
360 static const struct got_error *
361 buf_grow(BUF *b, size_t len)
363 u_char *buf;
364 buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
365 if (buf == NULL)
366 return got_error_from_errno("reallocarray");
367 b->cb_buf = buf;
368 b->cb_size += len;
369 return NULL;