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/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);
48 /*
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.
52 */
53 const struct got_error *
54 buf_alloc(BUF **b, size_t len)
55 {
56 const struct got_error *err = NULL;
58 *b = malloc(sizeof(**b));
59 if (*b == NULL)
60 return NULL;
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;
69 }
70 } else
71 (*b)->cb_buf = NULL;
73 (*b)->cb_size = len;
74 (*b)->cb_len = 0;
76 return NULL;
77 }
79 /*
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.
84 */
85 const struct got_error *
86 buf_load(BUF **buf, FILE *f)
87 {
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;
99 }
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 void
130 buf_free(BUF *b)
132 if (b == NULL)
133 return;
134 free(b->cb_buf);
135 free(b);
138 /*
139 * Free the buffer <b>'s structural information but do not free the contents
140 * of the buffer. Instead, they are returned and should be freed later using
141 * free().
142 */
143 void *
144 buf_release(BUF *b)
146 void *tmp;
148 tmp = b->cb_buf;
149 free(b);
150 return (tmp);
153 u_char *
154 buf_get(BUF *b)
156 return (b->cb_buf);
159 /*
160 * Empty the contents of the buffer <b> and reset pointers.
161 */
162 void
163 buf_empty(BUF *b)
165 memset(b->cb_buf, 0, b->cb_size);
166 b->cb_len = 0;
169 /*
170 * Append a single character <c> to the end of the buffer <b>.
171 */
172 const struct got_error *
173 buf_putc(BUF *b, int c)
175 const struct got_error *err = NULL;
176 u_char *bp;
178 if (SIZE_LEFT(b) == 0) {
179 err = buf_grow(b, BUF_INCR);
180 if (err)
181 return err;
183 bp = b->cb_buf + b->cb_len;
184 *bp = (u_char)c;
185 b->cb_len++;
186 return NULL;
189 /*
190 * Append a string <s> to the end of buffer <b>.
191 */
192 const struct got_error *
193 buf_puts(size_t *newlen, BUF *b, const char *str)
195 return buf_append(newlen, b, str, strlen(str));
198 /*
199 * Return u_char at buffer position <pos>.
200 */
201 u_char
202 buf_getc(BUF *b, size_t pos)
204 return (b->cb_buf[pos]);
207 /*
208 * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
209 * buffer is too small to accept all data, it will get resized to an
210 * appropriate size to accept all data.
211 * Returns the number of bytes successfully appended to the buffer.
212 */
213 const struct got_error *
214 buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
216 const struct got_error *err = NULL;
217 size_t left, rlen;
218 u_char *bp;
220 left = SIZE_LEFT(b);
221 rlen = len;
223 if (left < len) {
224 err = buf_grow(b, len - left);
225 if (err)
226 return err;
228 bp = b->cb_buf + b->cb_len;
229 memcpy(bp, data, rlen);
230 b->cb_len += rlen;
232 *newlen = rlen;
233 return NULL;
236 /*
237 * Returns the size of the buffer that is being used.
238 */
239 size_t
240 buf_len(BUF *b)
242 return (b->cb_len);
245 /*
246 * Write the contents of the buffer <b> to the specified <fd>
247 */
248 int
249 buf_write_fd(BUF *b, int fd)
251 u_char *bp;
252 size_t len;
253 ssize_t ret;
255 len = b->cb_len;
256 bp = b->cb_buf;
258 do {
259 ret = write(fd, bp, len);
260 if (ret == -1) {
261 if (errno == EINTR || errno == EAGAIN)
262 continue;
263 return (-1);
266 len -= (size_t)ret;
267 bp += (size_t)ret;
268 } while (len > 0);
270 return (0);
273 /*
274 * Write the contents of the buffer <b> to the file whose path is given in
275 * <path>. If the file does not exist, it is created with mode <mode>.
276 */
277 const struct got_error *
278 buf_write(BUF *b, const char *path, mode_t mode)
280 const struct got_error *err = NULL;
281 int fd;
282 open:
283 if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, mode)) == -1) {
284 err = got_error_from_errno2("open", path);
285 if (errno == EACCES && unlink(path) != -1)
286 goto open;
287 else
288 return err;
291 if (buf_write_fd(b, fd) == -1) {
292 err = got_error_from_errno("buf_write_fd");
293 (void)unlink(path);
294 return err;
297 if (fchmod(fd, mode) < 0)
298 err = got_error_from_errno2("fchmod", path);
300 if (close(fd) == -1 && err == NULL)
301 err = got_error_from_errno2("close", path);
303 return err;
306 /*
307 * Write the contents of the buffer <b> to a temporary file whose path is
308 * specified using <template> (see mkstemp.3).
309 * NB. This function will modify <template>, as per mkstemp
310 */
311 const struct got_error *
312 buf_write_stmp(BUF *b, char *template)
314 const struct got_error *err = NULL;
315 int fd;
317 if ((fd = mkstemp(template)) == -1)
318 return got_error_from_errno("mkstemp");
320 if (buf_write_fd(b, fd) == -1) {
321 err = got_error_from_errno("buf_write_fd");
322 (void)unlink(template);
325 if (close(fd) == -1 && err == NULL)
326 err = got_error_from_errno("close");
328 return err;
331 /*
332 * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
333 * operation regardless of the result.
334 */
335 static const struct got_error *
336 buf_grow(BUF *b, size_t len)
338 u_char *buf;
339 buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
340 if (buf == NULL)
341 return got_error_from_errno("reallocarray");
342 b->cb_buf = buf;
343 b->cb_size += len;
344 return NULL;