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