Blame


1 74b37681 2019-02-07 stsp /* $OpenBSD: buf.c,v 1.27 2016/10/16 13:35:51 okan Exp $ */
2 74b37681 2019-02-07 stsp /*
3 74b37681 2019-02-07 stsp * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
4 74b37681 2019-02-07 stsp * All rights reserved.
5 74b37681 2019-02-07 stsp *
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
8 74b37681 2019-02-07 stsp * are met:
9 74b37681 2019-02-07 stsp *
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.
14 74b37681 2019-02-07 stsp *
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.
25 74b37681 2019-02-07 stsp */
26 74b37681 2019-02-07 stsp
27 74b37681 2019-02-07 stsp #include <sys/stat.h>
28 74b37681 2019-02-07 stsp
29 74b37681 2019-02-07 stsp #include <errno.h>
30 74b37681 2019-02-07 stsp #include <fcntl.h>
31 74b37681 2019-02-07 stsp #include <stdint.h>
32 74b37681 2019-02-07 stsp #include <stdio.h>
33 74b37681 2019-02-07 stsp #include <stdlib.h>
34 74b37681 2019-02-07 stsp #include <string.h>
35 74b37681 2019-02-07 stsp #include <unistd.h>
36 74b37681 2019-02-07 stsp
37 74b37681 2019-02-07 stsp #include "buf.h"
38 74b37681 2019-02-07 stsp
39 af45e626 2019-02-08 stsp #include "got_error.h"
40 af45e626 2019-02-08 stsp
41 74b37681 2019-02-07 stsp #define BUF_INCR 128
42 74b37681 2019-02-07 stsp
43 575e8218 2019-10-07 stsp #define SIZE_LEFT(b) ((b)->cb_size - (b)->cb_len)
44 74b37681 2019-02-07 stsp
45 af45e626 2019-02-08 stsp static const struct got_error *buf_grow(BUF *, size_t);
46 74b37681 2019-02-07 stsp
47 74b37681 2019-02-07 stsp /*
48 74b37681 2019-02-07 stsp * Create a new buffer structure and return a pointer to it. This structure
49 74b37681 2019-02-07 stsp * uses dynamically-allocated memory and must be freed with buf_free(), once
50 74b37681 2019-02-07 stsp * the buffer is no longer needed.
51 74b37681 2019-02-07 stsp */
52 575e8218 2019-10-07 stsp const struct got_error *
53 575e8218 2019-10-07 stsp buf_alloc(BUF **b, size_t len)
54 74b37681 2019-02-07 stsp {
55 575e8218 2019-10-07 stsp const struct got_error *err = NULL;
56 74b37681 2019-02-07 stsp
57 575e8218 2019-10-07 stsp *b = malloc(sizeof(**b));
58 575e8218 2019-10-07 stsp if (*b == NULL)
59 af45e626 2019-02-08 stsp return NULL;
60 74b37681 2019-02-07 stsp /* Postpone creation of zero-sized buffers */
61 af45e626 2019-02-08 stsp if (len > 0) {
62 575e8218 2019-10-07 stsp (*b)->cb_buf = calloc(1, len);
63 575e8218 2019-10-07 stsp if ((*b)->cb_buf == NULL) {
64 575e8218 2019-10-07 stsp err = got_error_from_errno("calloc");
65 575e8218 2019-10-07 stsp free(*b);
66 575e8218 2019-10-07 stsp *b = NULL;
67 575e8218 2019-10-07 stsp return err;
68 57ebf19f 2019-08-28 hiltjo }
69 af45e626 2019-02-08 stsp } else
70 575e8218 2019-10-07 stsp (*b)->cb_buf = NULL;
71 74b37681 2019-02-07 stsp
72 575e8218 2019-10-07 stsp (*b)->cb_size = len;
73 575e8218 2019-10-07 stsp (*b)->cb_len = 0;
74 74b37681 2019-02-07 stsp
75 575e8218 2019-10-07 stsp return NULL;
76 74b37681 2019-02-07 stsp }
77 74b37681 2019-02-07 stsp
78 74b37681 2019-02-07 stsp /*
79 74b37681 2019-02-07 stsp * Open the file specified by <path> and load all of its contents into a
80 74b37681 2019-02-07 stsp * buffer.
81 74b37681 2019-02-07 stsp * Returns the loaded buffer on success or NULL on failure.
82 74b37681 2019-02-07 stsp * Sets errno on error.
83 74b37681 2019-02-07 stsp */
84 575e8218 2019-10-07 stsp const struct got_error *
85 db590691 2021-06-02 stsp buf_load(BUF **buf, FILE *f)
86 74b37681 2019-02-07 stsp {
87 575e8218 2019-10-07 stsp const struct got_error *err = NULL;
88 db590691 2021-06-02 stsp size_t ret;
89 74b37681 2019-02-07 stsp size_t len;
90 74b37681 2019-02-07 stsp u_char *bp;
91 74b37681 2019-02-07 stsp struct stat st;
92 74b37681 2019-02-07 stsp
93 575e8218 2019-10-07 stsp *buf = NULL;
94 74b37681 2019-02-07 stsp
95 db590691 2021-06-02 stsp if (fstat(fileno(f), &st) == -1) {
96 db590691 2021-06-02 stsp err = got_error_from_errno("fstat");
97 74b37681 2019-02-07 stsp goto out;
98 575e8218 2019-10-07 stsp }
99 74b37681 2019-02-07 stsp
100 74b37681 2019-02-07 stsp if ((uintmax_t)st.st_size > SIZE_MAX) {
101 db590691 2021-06-02 stsp err = got_error_set_errno(EFBIG,
102 db590691 2021-06-02 stsp "cannot fit file into memory buffer");
103 74b37681 2019-02-07 stsp goto out;
104 74b37681 2019-02-07 stsp }
105 575e8218 2019-10-07 stsp err = buf_alloc(buf, st.st_size);
106 575e8218 2019-10-07 stsp if (err)
107 575e8218 2019-10-07 stsp goto out;
108 db590691 2021-06-02 stsp for (bp = (*buf)->cb_buf; ; bp += ret) {
109 575e8218 2019-10-07 stsp len = SIZE_LEFT(*buf);
110 db590691 2021-06-02 stsp ret = fread(bp, 1, len, f);
111 db590691 2021-06-02 stsp if (ret == 0 && ferror(f)) {
112 db590691 2021-06-02 stsp err = got_ferror(f, GOT_ERR_IO);
113 74b37681 2019-02-07 stsp goto out;
114 74b37681 2019-02-07 stsp } else if (ret == 0)
115 74b37681 2019-02-07 stsp break;
116 74b37681 2019-02-07 stsp
117 575e8218 2019-10-07 stsp (*buf)->cb_len += (size_t)ret;
118 74b37681 2019-02-07 stsp }
119 74b37681 2019-02-07 stsp
120 74b37681 2019-02-07 stsp out:
121 575e8218 2019-10-07 stsp if (err) {
122 575e8218 2019-10-07 stsp buf_free(*buf);
123 575e8218 2019-10-07 stsp *buf = NULL;
124 74b37681 2019-02-07 stsp }
125 575e8218 2019-10-07 stsp return err;
126 74b37681 2019-02-07 stsp }
127 74b37681 2019-02-07 stsp
128 74b37681 2019-02-07 stsp void
129 74b37681 2019-02-07 stsp buf_free(BUF *b)
130 74b37681 2019-02-07 stsp {
131 74b37681 2019-02-07 stsp if (b == NULL)
132 74b37681 2019-02-07 stsp return;
133 74b37681 2019-02-07 stsp free(b->cb_buf);
134 74b37681 2019-02-07 stsp free(b);
135 74b37681 2019-02-07 stsp }
136 74b37681 2019-02-07 stsp
137 74b37681 2019-02-07 stsp /*
138 74b37681 2019-02-07 stsp * Free the buffer <b>'s structural information but do not free the contents
139 74b37681 2019-02-07 stsp * of the buffer. Instead, they are returned and should be freed later using
140 74b37681 2019-02-07 stsp * free().
141 74b37681 2019-02-07 stsp */
142 74b37681 2019-02-07 stsp void *
143 74b37681 2019-02-07 stsp buf_release(BUF *b)
144 74b37681 2019-02-07 stsp {
145 74b37681 2019-02-07 stsp void *tmp;
146 74b37681 2019-02-07 stsp
147 74b37681 2019-02-07 stsp tmp = b->cb_buf;
148 74b37681 2019-02-07 stsp free(b);
149 74b37681 2019-02-07 stsp return (tmp);
150 74b37681 2019-02-07 stsp }
151 74b37681 2019-02-07 stsp
152 74b37681 2019-02-07 stsp u_char *
153 74b37681 2019-02-07 stsp buf_get(BUF *b)
154 74b37681 2019-02-07 stsp {
155 74b37681 2019-02-07 stsp return (b->cb_buf);
156 74b37681 2019-02-07 stsp }
157 74b37681 2019-02-07 stsp
158 74b37681 2019-02-07 stsp /*
159 74b37681 2019-02-07 stsp * Empty the contents of the buffer <b> and reset pointers.
160 74b37681 2019-02-07 stsp */
161 74b37681 2019-02-07 stsp void
162 74b37681 2019-02-07 stsp buf_empty(BUF *b)
163 74b37681 2019-02-07 stsp {
164 74b37681 2019-02-07 stsp memset(b->cb_buf, 0, b->cb_size);
165 74b37681 2019-02-07 stsp b->cb_len = 0;
166 74b37681 2019-02-07 stsp }
167 74b37681 2019-02-07 stsp
168 74b37681 2019-02-07 stsp /*
169 74b37681 2019-02-07 stsp * Append a single character <c> to the end of the buffer <b>.
170 74b37681 2019-02-07 stsp */
171 af45e626 2019-02-08 stsp const struct got_error *
172 74b37681 2019-02-07 stsp buf_putc(BUF *b, int c)
173 74b37681 2019-02-07 stsp {
174 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
175 74b37681 2019-02-07 stsp u_char *bp;
176 74b37681 2019-02-07 stsp
177 af45e626 2019-02-08 stsp if (SIZE_LEFT(b) == 0) {
178 af45e626 2019-02-08 stsp err = buf_grow(b, BUF_INCR);
179 af45e626 2019-02-08 stsp if (err)
180 af45e626 2019-02-08 stsp return err;
181 af45e626 2019-02-08 stsp }
182 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
183 74b37681 2019-02-07 stsp *bp = (u_char)c;
184 74b37681 2019-02-07 stsp b->cb_len++;
185 af45e626 2019-02-08 stsp return NULL;
186 74b37681 2019-02-07 stsp }
187 74b37681 2019-02-07 stsp
188 74b37681 2019-02-07 stsp /*
189 74b37681 2019-02-07 stsp * Append a string <s> to the end of buffer <b>.
190 74b37681 2019-02-07 stsp */
191 af45e626 2019-02-08 stsp const struct got_error *
192 af45e626 2019-02-08 stsp buf_puts(size_t *newlen, BUF *b, const char *str)
193 74b37681 2019-02-07 stsp {
194 af45e626 2019-02-08 stsp return buf_append(newlen, b, str, strlen(str));
195 74b37681 2019-02-07 stsp }
196 74b37681 2019-02-07 stsp
197 74b37681 2019-02-07 stsp /*
198 74b37681 2019-02-07 stsp * Return u_char at buffer position <pos>.
199 74b37681 2019-02-07 stsp */
200 74b37681 2019-02-07 stsp u_char
201 74b37681 2019-02-07 stsp buf_getc(BUF *b, size_t pos)
202 74b37681 2019-02-07 stsp {
203 74b37681 2019-02-07 stsp return (b->cb_buf[pos]);
204 74b37681 2019-02-07 stsp }
205 74b37681 2019-02-07 stsp
206 74b37681 2019-02-07 stsp /*
207 74b37681 2019-02-07 stsp * Append <len> bytes of data pointed to by <data> to the buffer <b>. If the
208 74b37681 2019-02-07 stsp * buffer is too small to accept all data, it will get resized to an
209 74b37681 2019-02-07 stsp * appropriate size to accept all data.
210 74b37681 2019-02-07 stsp * Returns the number of bytes successfully appended to the buffer.
211 74b37681 2019-02-07 stsp */
212 af45e626 2019-02-08 stsp const struct got_error *
213 af45e626 2019-02-08 stsp buf_append(size_t *newlen, BUF *b, const void *data, size_t len)
214 74b37681 2019-02-07 stsp {
215 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
216 74b37681 2019-02-07 stsp size_t left, rlen;
217 74b37681 2019-02-07 stsp u_char *bp;
218 74b37681 2019-02-07 stsp
219 74b37681 2019-02-07 stsp left = SIZE_LEFT(b);
220 74b37681 2019-02-07 stsp rlen = len;
221 74b37681 2019-02-07 stsp
222 af45e626 2019-02-08 stsp if (left < len) {
223 af45e626 2019-02-08 stsp err = buf_grow(b, len - left);
224 af45e626 2019-02-08 stsp if (err)
225 af45e626 2019-02-08 stsp return err;
226 af45e626 2019-02-08 stsp }
227 74b37681 2019-02-07 stsp bp = b->cb_buf + b->cb_len;
228 74b37681 2019-02-07 stsp memcpy(bp, data, rlen);
229 74b37681 2019-02-07 stsp b->cb_len += rlen;
230 74b37681 2019-02-07 stsp
231 af45e626 2019-02-08 stsp *newlen = rlen;
232 af45e626 2019-02-08 stsp return NULL;
233 74b37681 2019-02-07 stsp }
234 74b37681 2019-02-07 stsp
235 74b37681 2019-02-07 stsp /*
236 74b37681 2019-02-07 stsp * Returns the size of the buffer that is being used.
237 74b37681 2019-02-07 stsp */
238 74b37681 2019-02-07 stsp size_t
239 74b37681 2019-02-07 stsp buf_len(BUF *b)
240 74b37681 2019-02-07 stsp {
241 74b37681 2019-02-07 stsp return (b->cb_len);
242 74b37681 2019-02-07 stsp }
243 74b37681 2019-02-07 stsp
244 74b37681 2019-02-07 stsp /*
245 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the specified <fd>
246 74b37681 2019-02-07 stsp */
247 74b37681 2019-02-07 stsp int
248 74b37681 2019-02-07 stsp buf_write_fd(BUF *b, int fd)
249 74b37681 2019-02-07 stsp {
250 74b37681 2019-02-07 stsp u_char *bp;
251 74b37681 2019-02-07 stsp size_t len;
252 74b37681 2019-02-07 stsp ssize_t ret;
253 74b37681 2019-02-07 stsp
254 74b37681 2019-02-07 stsp len = b->cb_len;
255 74b37681 2019-02-07 stsp bp = b->cb_buf;
256 74b37681 2019-02-07 stsp
257 74b37681 2019-02-07 stsp do {
258 74b37681 2019-02-07 stsp ret = write(fd, bp, len);
259 74b37681 2019-02-07 stsp if (ret == -1) {
260 74b37681 2019-02-07 stsp if (errno == EINTR || errno == EAGAIN)
261 74b37681 2019-02-07 stsp continue;
262 74b37681 2019-02-07 stsp return (-1);
263 74b37681 2019-02-07 stsp }
264 74b37681 2019-02-07 stsp
265 74b37681 2019-02-07 stsp len -= (size_t)ret;
266 74b37681 2019-02-07 stsp bp += (size_t)ret;
267 74b37681 2019-02-07 stsp } while (len > 0);
268 74b37681 2019-02-07 stsp
269 74b37681 2019-02-07 stsp return (0);
270 74b37681 2019-02-07 stsp }
271 74b37681 2019-02-07 stsp
272 74b37681 2019-02-07 stsp /*
273 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to the file whose path is given in
274 74b37681 2019-02-07 stsp * <path>. If the file does not exist, it is created with mode <mode>.
275 74b37681 2019-02-07 stsp */
276 af45e626 2019-02-08 stsp const struct got_error *
277 74b37681 2019-02-07 stsp buf_write(BUF *b, const char *path, mode_t mode)
278 74b37681 2019-02-07 stsp {
279 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
280 74b37681 2019-02-07 stsp int fd;
281 74b37681 2019-02-07 stsp open:
282 74b37681 2019-02-07 stsp if ((fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode)) == -1) {
283 202329ae 2019-08-11 stsp err = got_error_from_errno2("open", path);
284 74b37681 2019-02-07 stsp if (errno == EACCES && unlink(path) != -1)
285 74b37681 2019-02-07 stsp goto open;
286 74b37681 2019-02-07 stsp else
287 202329ae 2019-08-11 stsp return err;
288 74b37681 2019-02-07 stsp }
289 74b37681 2019-02-07 stsp
290 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
291 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
292 74b37681 2019-02-07 stsp (void)unlink(path);
293 af45e626 2019-02-08 stsp return err;
294 74b37681 2019-02-07 stsp }
295 74b37681 2019-02-07 stsp
296 74b37681 2019-02-07 stsp if (fchmod(fd, mode) < 0)
297 638f9024 2019-05-13 stsp err = got_error_from_errno2("fchmod", path);
298 74b37681 2019-02-07 stsp
299 08578a35 2021-01-22 stsp if (close(fd) == -1 && err == NULL)
300 638f9024 2019-05-13 stsp err = got_error_from_errno2("close", path);
301 74b37681 2019-02-07 stsp
302 af45e626 2019-02-08 stsp return err;
303 74b37681 2019-02-07 stsp }
304 74b37681 2019-02-07 stsp
305 74b37681 2019-02-07 stsp /*
306 74b37681 2019-02-07 stsp * Write the contents of the buffer <b> to a temporary file whose path is
307 74b37681 2019-02-07 stsp * specified using <template> (see mkstemp.3).
308 74b37681 2019-02-07 stsp * NB. This function will modify <template>, as per mkstemp
309 74b37681 2019-02-07 stsp */
310 af45e626 2019-02-08 stsp const struct got_error *
311 96cbb597 2019-10-09 stsp buf_write_stmp(BUF *b, char *template)
312 74b37681 2019-02-07 stsp {
313 af45e626 2019-02-08 stsp const struct got_error *err = NULL;
314 74b37681 2019-02-07 stsp int fd;
315 74b37681 2019-02-07 stsp
316 74b37681 2019-02-07 stsp if ((fd = mkstemp(template)) == -1)
317 638f9024 2019-05-13 stsp return got_error_from_errno("mkstemp");
318 74b37681 2019-02-07 stsp
319 74b37681 2019-02-07 stsp if (buf_write_fd(b, fd) == -1) {
320 638f9024 2019-05-13 stsp err = got_error_from_errno("buf_write_fd");
321 74b37681 2019-02-07 stsp (void)unlink(template);
322 74b37681 2019-02-07 stsp }
323 74b37681 2019-02-07 stsp
324 08578a35 2021-01-22 stsp if (close(fd) == -1 && err == NULL)
325 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
326 3a6ce05a 2019-02-11 stsp
327 af45e626 2019-02-08 stsp return err;
328 74b37681 2019-02-07 stsp }
329 74b37681 2019-02-07 stsp
330 74b37681 2019-02-07 stsp /*
331 74b37681 2019-02-07 stsp * Grow the buffer <b> by <len> bytes. The contents are unchanged by this
332 74b37681 2019-02-07 stsp * operation regardless of the result.
333 74b37681 2019-02-07 stsp */
334 af45e626 2019-02-08 stsp static const struct got_error *
335 74b37681 2019-02-07 stsp buf_grow(BUF *b, size_t len)
336 74b37681 2019-02-07 stsp {
337 af45e626 2019-02-08 stsp u_char *buf;
338 af45e626 2019-02-08 stsp buf = reallocarray(b->cb_buf, 1, b->cb_size + len);
339 af45e626 2019-02-08 stsp if (buf == NULL)
340 638f9024 2019-05-13 stsp return got_error_from_errno("reallocarray");
341 af45e626 2019-02-08 stsp b->cb_buf = buf;
342 74b37681 2019-02-07 stsp b->cb_size += len;
343 af45e626 2019-02-08 stsp return NULL;
344 74b37681 2019-02-07 stsp }