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