2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 a440fac0 2018-09-06 stsp * Permission to use, copy, modify, and distribute this software for any
5 a440fac0 2018-09-06 stsp * purpose with or without fee is hereby granted, provided that the above
6 a440fac0 2018-09-06 stsp * copyright notice and this permission notice appear in all copies.
8 a440fac0 2018-09-06 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a440fac0 2018-09-06 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a440fac0 2018-09-06 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a440fac0 2018-09-06 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a440fac0 2018-09-06 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a440fac0 2018-09-06 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a440fac0 2018-09-06 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a440fac0 2018-09-06 stsp #include <sys/types.h>
18 a440fac0 2018-09-06 stsp #include <sys/stat.h>
19 a440fac0 2018-09-06 stsp #include <sys/queue.h>
20 a440fac0 2018-09-06 stsp #include <sys/uio.h>
21 a440fac0 2018-09-06 stsp #include <sys/socket.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 a440fac0 2018-09-06 stsp #include <sys/wait.h>
25 a440fac0 2018-09-06 stsp #include <errno.h>
26 a440fac0 2018-09-06 stsp #include <stdio.h>
27 a440fac0 2018-09-06 stsp #include <stdlib.h>
28 a440fac0 2018-09-06 stsp #include <string.h>
29 a440fac0 2018-09-06 stsp #include <stdint.h>
30 a440fac0 2018-09-06 stsp #include <sha1.h>
31 a440fac0 2018-09-06 stsp #include <zlib.h>
32 a440fac0 2018-09-06 stsp #include <ctype.h>
33 a440fac0 2018-09-06 stsp #include <limits.h>
34 a440fac0 2018-09-06 stsp #include <imsg.h>
35 a440fac0 2018-09-06 stsp #include <time.h>
36 ad242220 2018-09-08 stsp #include <unistd.h>
38 a440fac0 2018-09-06 stsp #include "got_error.h"
39 a440fac0 2018-09-06 stsp #include "got_object.h"
40 a440fac0 2018-09-06 stsp #include "got_repository.h"
41 a440fac0 2018-09-06 stsp #include "got_opentemp.h"
42 324d37e7 2019-05-11 stsp #include "got_path.h"
44 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
45 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
46 41fa1437 2018-11-05 stsp #include "got_lib_inflate.h"
47 41fa1437 2018-11-05 stsp #include "got_lib_object.h"
48 3022d272 2019-11-14 stsp #include "got_lib_object_parse.h"
49 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
50 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
51 15a94983 2018-12-23 stsp #include "got_lib_privsep.h"
52 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
54 a440fac0 2018-09-06 stsp #ifndef nitems
55 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 ca6e02ac 2020-01-07 stsp struct got_object_id *
59 ca6e02ac 2020-01-07 stsp got_object_id_dup(struct got_object_id *id1)
61 ca6e02ac 2020-01-07 stsp struct got_object_id *id2;
63 ca6e02ac 2020-01-07 stsp id2 = malloc(sizeof(*id2));
64 ca6e02ac 2020-01-07 stsp if (id2 == NULL)
65 ca6e02ac 2020-01-07 stsp return NULL;
66 ca6e02ac 2020-01-07 stsp memcpy(id2, id1, sizeof(*id2));
71 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
72 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
74 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
77 2ff12563 2018-09-15 stsp const struct got_error *
78 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
80 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
82 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
83 5df4932d 2018-11-05 stsp if (*qid == NULL)
84 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
86 5df4932d 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
87 5df4932d 2018-11-05 stsp if ((*qid)->id == NULL) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
89 5df4932d 2018-11-05 stsp got_object_qid_free(*qid);
90 5df4932d 2018-11-05 stsp *qid = NULL;
94 5df4932d 2018-11-05 stsp return NULL;
97 5df4932d 2018-11-05 stsp const struct got_error *
98 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
100 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
102 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
103 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
104 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
106 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
107 2ff12563 2018-09-15 stsp free(*outbuf);
108 2ff12563 2018-09-15 stsp *outbuf = NULL;
109 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
112 2ff12563 2018-09-15 stsp return NULL;
116 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
118 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
119 03fa71c8 2018-09-06 stsp obj->refcnt--;
120 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
124 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
125 03fa71c8 2018-09-06 stsp struct got_delta *delta;
126 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
127 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
128 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
129 2256993b 2019-07-15 stsp free(delta);
136 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
138 03fa71c8 2018-09-06 stsp free(qid->id);
143 dd88155e 2019-06-29 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
145 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
147 dd88155e 2019-06-29 stsp while (!SIMPLEQ_EMPTY(ids)) {
148 dd88155e 2019-06-29 stsp qid = SIMPLEQ_FIRST(ids);
149 dd88155e 2019-06-29 stsp SIMPLEQ_REMOVE_HEAD(ids, entry);
150 dd88155e 2019-06-29 stsp got_object_qid_free(qid);
154 1785f84a 2018-12-23 stsp const struct got_error *
155 1785f84a 2018-12-23 stsp got_object_parse_header(struct got_object **obj, char *buf, size_t len)
157 ff2a4428 2019-03-19 stsp const char *obj_labels[] = {
158 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_COMMIT,
159 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TREE,
160 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_BLOB,
161 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TAG,
163 1785f84a 2018-12-23 stsp const int obj_types[] = {
164 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_COMMIT,
165 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TREE,
166 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_BLOB,
167 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TAG,
169 1785f84a 2018-12-23 stsp int type = 0;
170 1785f84a 2018-12-23 stsp size_t size = 0, hdrlen = 0;
173 1785f84a 2018-12-23 stsp *obj = NULL;
175 9ef4ac16 2019-04-13 stsp hdrlen = strnlen(buf, len) + 1 /* '\0' */;
176 9ef4ac16 2019-04-13 stsp if (hdrlen > len)
177 9ef4ac16 2019-04-13 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
179 ff2a4428 2019-03-19 stsp for (i = 0; i < nitems(obj_labels); i++) {
180 ff2a4428 2019-03-19 stsp const char *label = obj_labels[i];
181 ff2a4428 2019-03-19 stsp size_t label_len = strlen(label);
182 1785f84a 2018-12-23 stsp const char *errstr;
184 ff2a4428 2019-03-19 stsp if (strncmp(buf, label, label_len) != 0)
187 1785f84a 2018-12-23 stsp type = obj_types[i];
188 ff2a4428 2019-03-19 stsp if (len <= label_len)
189 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
190 ff2a4428 2019-03-19 stsp size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
191 1785f84a 2018-12-23 stsp if (errstr != NULL)
192 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
196 1785f84a 2018-12-23 stsp if (type == 0)
197 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
199 1785f84a 2018-12-23 stsp *obj = calloc(1, sizeof(**obj));
200 1785f84a 2018-12-23 stsp if (*obj == NULL)
201 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
202 1785f84a 2018-12-23 stsp (*obj)->type = type;
203 1785f84a 2018-12-23 stsp (*obj)->hdrlen = hdrlen;
204 1785f84a 2018-12-23 stsp (*obj)->size = size;
205 1785f84a 2018-12-23 stsp return NULL;
208 1785f84a 2018-12-23 stsp const struct got_error *
209 1785f84a 2018-12-23 stsp got_object_read_header(struct got_object **obj, int fd)
211 1785f84a 2018-12-23 stsp const struct got_error *err;
212 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
214 1785f84a 2018-12-23 stsp const size_t zbsize = 64;
215 1785f84a 2018-12-23 stsp size_t outlen, totlen;
216 1785f84a 2018-12-23 stsp int nbuf = 1;
218 1785f84a 2018-12-23 stsp *obj = NULL;
220 1785f84a 2018-12-23 stsp buf = malloc(zbsize);
221 1785f84a 2018-12-23 stsp if (buf == NULL)
222 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
224 1785f84a 2018-12-23 stsp err = got_inflate_init(&zb, buf, zbsize);
226 1785f84a 2018-12-23 stsp return err;
228 1785f84a 2018-12-23 stsp totlen = 0;
230 1785f84a 2018-12-23 stsp err = got_inflate_read_fd(&zb, fd, &outlen);
233 1785f84a 2018-12-23 stsp if (outlen == 0)
235 1785f84a 2018-12-23 stsp totlen += outlen;
236 dedbbd9d 2019-04-13 stsp if (memchr(zb.outbuf, '\0', outlen) == NULL) {
237 1785f84a 2018-12-23 stsp char *newbuf;
239 1785f84a 2018-12-23 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
240 1785f84a 2018-12-23 stsp if (newbuf == NULL) {
241 638f9024 2019-05-13 stsp err = got_error_from_errno("recallocarray");
244 1785f84a 2018-12-23 stsp buf = newbuf;
245 1785f84a 2018-12-23 stsp zb.outbuf = newbuf + totlen;
246 1785f84a 2018-12-23 stsp zb.outlen = (nbuf * zbsize) - totlen;
248 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
250 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
253 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
254 1785f84a 2018-12-23 stsp return err;
257 a440fac0 2018-09-06 stsp struct got_commit_object *
258 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
260 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
262 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
263 a440fac0 2018-09-06 stsp if (commit == NULL)
264 a440fac0 2018-09-06 stsp return NULL;
265 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
266 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
267 a440fac0 2018-09-06 stsp free(commit);
268 a440fac0 2018-09-06 stsp return NULL;
271 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
273 a440fac0 2018-09-06 stsp return commit;
276 a440fac0 2018-09-06 stsp const struct got_error *
277 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
278 a440fac0 2018-09-06 stsp const char *id_str)
280 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
281 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
283 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
285 7762fe12 2018-11-05 stsp return err;
287 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
288 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
289 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
290 a440fac0 2018-09-06 stsp return err;
293 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
294 a440fac0 2018-09-06 stsp commit->nparents++;
296 a440fac0 2018-09-06 stsp return NULL;
299 a440fac0 2018-09-06 stsp static const struct got_error *
300 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
302 a440fac0 2018-09-06 stsp int sign = 1;
303 a440fac0 2018-09-06 stsp const char *p = tzstr;
304 a440fac0 2018-09-06 stsp time_t h, m;
306 a440fac0 2018-09-06 stsp *gmtoff = 0;
308 a440fac0 2018-09-06 stsp if (*p == '-')
310 a440fac0 2018-09-06 stsp else if (*p != '+')
311 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
313 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
314 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
315 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
318 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
319 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
320 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
322 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
323 a440fac0 2018-09-06 stsp return NULL;
326 a440fac0 2018-09-06 stsp static const struct got_error *
327 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
329 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
330 a440fac0 2018-09-06 stsp const char *errstr;
331 a440fac0 2018-09-06 stsp char *space, *tzstr;
333 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
334 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
335 a440fac0 2018-09-06 stsp if (space == NULL)
336 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
337 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
338 a440fac0 2018-09-06 stsp if (tzstr == NULL)
339 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
340 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
341 a440fac0 2018-09-06 stsp free(tzstr);
343 a440fac0 2018-09-06 stsp return err;
344 a440fac0 2018-09-06 stsp *space = '\0';
346 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
347 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
348 a440fac0 2018-09-06 stsp if (space == NULL)
349 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
351 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
352 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
353 a440fac0 2018-09-06 stsp if (errstr)
354 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
356 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
357 a440fac0 2018-09-06 stsp *space = '\0';
359 a440fac0 2018-09-06 stsp return NULL;
363 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
365 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
366 03fa71c8 2018-09-06 stsp commit->refcnt--;
367 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
371 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
372 03fa71c8 2018-09-06 stsp free(commit->tree_id);
373 03fa71c8 2018-09-06 stsp free(commit->author);
374 03fa71c8 2018-09-06 stsp free(commit->committer);
375 03fa71c8 2018-09-06 stsp free(commit->logmsg);
376 03fa71c8 2018-09-06 stsp free(commit);
379 45d799e2 2018-12-23 stsp struct got_object_id *
380 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
382 45d799e2 2018-12-23 stsp return commit->tree_id;
386 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
388 45d799e2 2018-12-23 stsp return commit->nparents;
391 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
392 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
394 45d799e2 2018-12-23 stsp return &commit->parent_ids;
397 45d799e2 2018-12-23 stsp const char *
398 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
400 45d799e2 2018-12-23 stsp return commit->author;
404 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
406 45d799e2 2018-12-23 stsp return commit->author_time;
409 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
411 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
414 45d799e2 2018-12-23 stsp const char *
415 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
417 45d799e2 2018-12-23 stsp return commit->committer;
421 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
423 45d799e2 2018-12-23 stsp return commit->committer_time;
427 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
429 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
432 5943eee2 2019-08-13 stsp const struct got_error *
433 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
435 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
436 5943eee2 2019-08-13 stsp char *msg0, *msg, *line, *s;
437 5943eee2 2019-08-13 stsp size_t len;
438 13555e04 2019-09-28 semarie int headers = 1;
440 5943eee2 2019-08-13 stsp *logmsg = NULL;
442 5943eee2 2019-08-13 stsp msg0 = strdup(commit->logmsg);
443 5943eee2 2019-08-13 stsp if (msg0 == NULL)
444 5943eee2 2019-08-13 stsp return got_error_from_errno("strdup");
446 13555e04 2019-09-28 semarie /* Copy log message line by line to strip out unusual headers... */
447 5943eee2 2019-08-13 stsp msg = msg0;
449 13555e04 2019-09-28 semarie if ((line = strsep(&msg, "\n")) == NULL)
452 13555e04 2019-09-28 semarie if (headers == 1) {
453 13555e04 2019-09-28 semarie if (line[0] != '\0' &&
454 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_TREE,
455 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
456 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
457 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
458 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_PARENT,
459 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
460 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
461 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
464 13555e04 2019-09-28 semarie if (line[0] == '\0')
465 13555e04 2019-09-28 semarie headers = 0;
468 13555e04 2019-09-28 semarie if (asprintf(&s, "%s%s\n",
469 13555e04 2019-09-28 semarie *logmsg ? *logmsg : "", line) == -1) {
470 13555e04 2019-09-28 semarie err = got_error_from_errno("asprintf");
471 13555e04 2019-09-28 semarie goto done;
473 13555e04 2019-09-28 semarie free(*logmsg);
474 13555e04 2019-09-28 semarie *logmsg = s;
476 5943eee2 2019-08-13 stsp } while (line);
478 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
479 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
480 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
481 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
482 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
486 5943eee2 2019-08-13 stsp free(msg0);
488 5943eee2 2019-08-13 stsp free(*logmsg);
489 5943eee2 2019-08-13 stsp *logmsg = NULL;
491 5943eee2 2019-08-13 stsp return err;
494 24ea5512 2019-08-22 stsp const char *
495 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
497 24ea5512 2019-08-22 stsp return commit->logmsg;
500 a440fac0 2018-09-06 stsp const struct got_error *
501 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
502 5e0b25c4 2018-12-24 stsp size_t len)
504 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
505 a440fac0 2018-09-06 stsp char *s = buf;
506 ff2a4428 2019-03-19 stsp size_t label_len;
507 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
509 4793d91b 2019-09-22 stsp if (remain == 0)
510 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
512 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
513 a440fac0 2018-09-06 stsp if (*commit == NULL)
514 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
516 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
517 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
518 ff2a4428 2019-03-19 stsp remain -= label_len;
519 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
520 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
523 ff2a4428 2019-03-19 stsp s += label_len;
524 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
525 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
529 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
531 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
535 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
536 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
537 ff2a4428 2019-03-19 stsp remain -= label_len;
538 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
539 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
542 ff2a4428 2019-03-19 stsp s += label_len;
543 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
547 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
548 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
551 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
552 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
554 a440fac0 2018-09-06 stsp size_t slen;
556 ff2a4428 2019-03-19 stsp remain -= label_len;
557 a440fac0 2018-09-06 stsp if (remain <= 0) {
558 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
561 ff2a4428 2019-03-19 stsp s += label_len;
562 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
563 a440fac0 2018-09-06 stsp if (p == NULL) {
564 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
568 a440fac0 2018-09-06 stsp slen = strlen(s);
569 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
570 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
573 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
574 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
575 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
578 a440fac0 2018-09-06 stsp s += slen + 1;
579 a440fac0 2018-09-06 stsp remain -= slen + 1;
582 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
583 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
585 a440fac0 2018-09-06 stsp size_t slen;
587 ff2a4428 2019-03-19 stsp remain -= label_len;
588 a440fac0 2018-09-06 stsp if (remain <= 0) {
589 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
592 ff2a4428 2019-03-19 stsp s += label_len;
593 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
594 a440fac0 2018-09-06 stsp if (p == NULL) {
595 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
599 a440fac0 2018-09-06 stsp slen = strlen(s);
600 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
601 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
604 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
605 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
606 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
609 a440fac0 2018-09-06 stsp s += slen + 1;
610 a440fac0 2018-09-06 stsp remain -= slen + 1;
613 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
614 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
615 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
620 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
621 a440fac0 2018-09-06 stsp *commit = NULL;
623 a440fac0 2018-09-06 stsp return err;
627 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
629 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
630 03fa71c8 2018-09-06 stsp tree->refcnt--;
631 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
635 56e0773d 2019-11-28 stsp free(tree->entries);
636 03fa71c8 2018-09-06 stsp free(tree);
639 a440fac0 2018-09-06 stsp static const struct got_error *
640 3022d272 2019-11-14 stsp parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
641 3022d272 2019-11-14 stsp size_t *elen, char *buf,
642 a440fac0 2018-09-06 stsp size_t maxlen)
644 8914529d 2019-04-13 stsp char *p, *space;
645 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
647 3022d272 2019-11-14 stsp *name = NULL;
650 3022d272 2019-11-14 stsp *pte = malloc(sizeof(**pte));
651 3022d272 2019-11-14 stsp if (*pte == NULL)
652 3022d272 2019-11-14 stsp return got_error_from_errno("malloc");
654 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
655 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
656 3022d272 2019-11-14 stsp free(*pte);
657 3022d272 2019-11-14 stsp *pte = NULL;
658 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
661 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
662 8914529d 2019-04-13 stsp if (space == NULL || space <= buf) {
663 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
664 3022d272 2019-11-14 stsp free(*pte);
665 3022d272 2019-11-14 stsp *pte = NULL;
666 a440fac0 2018-09-06 stsp return err;
668 3022d272 2019-11-14 stsp (*pte)->mode = 0;
670 8914529d 2019-04-13 stsp while (p < space) {
671 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
672 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
675 3022d272 2019-11-14 stsp (*pte)->mode <<= 3;
676 3022d272 2019-11-14 stsp (*pte)->mode |= *p - '0';
680 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
681 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
684 3022d272 2019-11-14 stsp *name = space + 1;
685 68bf1b1e 2018-11-07 stsp buf += *elen;
686 3022d272 2019-11-14 stsp (*pte)->id = buf;
687 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
690 3022d272 2019-11-14 stsp free(*pte);
691 3022d272 2019-11-14 stsp *pte = NULL;
693 a440fac0 2018-09-06 stsp return err;
696 a440fac0 2018-09-06 stsp const struct got_error *
697 3022d272 2019-11-14 stsp got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
698 3022d272 2019-11-14 stsp uint8_t *buf, size_t len)
700 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
701 a440fac0 2018-09-06 stsp size_t remain = len;
703 3022d272 2019-11-14 stsp *nentries = 0;
704 db1d3576 2019-10-04 stsp if (remain == 0)
705 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
707 a440fac0 2018-09-06 stsp while (remain > 0) {
708 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte;
709 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
710 3022d272 2019-11-14 stsp const char *name;
711 a440fac0 2018-09-06 stsp size_t elen;
713 3022d272 2019-11-14 stsp err = parse_tree_entry(&pte, &name, &elen, buf, remain);
716 3022d272 2019-11-14 stsp err = got_pathlist_insert(&new, entries, name, pte);
719 f5d3d7af 2019-02-05 stsp if (new == NULL) {
720 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
723 a440fac0 2018-09-06 stsp buf += elen;
724 a440fac0 2018-09-06 stsp remain -= elen;
725 3022d272 2019-11-14 stsp (*nentries)++;
728 a440fac0 2018-09-06 stsp if (remain != 0) {
729 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
734 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(entries);
735 3022d272 2019-11-14 stsp *nentries = 0;
737 f5d3d7af 2019-02-05 stsp return err;
741 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
743 b64b1f95 2020-01-06 stsp struct got_pathlist_entry *pe;
745 b64b1f95 2020-01-06 stsp TAILQ_FOREACH(pe, entries, entry) {
746 b64b1f95 2020-01-06 stsp struct got_parsed_tree_entry *pte = pe->data;
749 b64b1f95 2020-01-06 stsp got_pathlist_free(entries);
753 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
755 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
756 ca0d469c 2019-08-13 stsp tag->refcnt--;
757 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
761 f4a881ce 2018-11-17 stsp free(tag->tag);
762 f4a881ce 2018-11-17 stsp free(tag->tagger);
763 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
767 ad242220 2018-09-08 stsp const struct got_error *
768 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
770 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
771 f4a881ce 2018-11-17 stsp size_t remain = len;
772 f4a881ce 2018-11-17 stsp char *s = buf;
773 ff2a4428 2019-03-19 stsp size_t label_len;
775 4793d91b 2019-09-22 stsp if (remain == 0)
776 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
778 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
779 f4a881ce 2018-11-17 stsp if (*tag == NULL)
780 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
782 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
783 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
784 ff2a4428 2019-03-19 stsp remain -= label_len;
785 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
786 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
789 ff2a4428 2019-03-19 stsp s += label_len;
790 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
791 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
794 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
795 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
797 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
801 f4a881ce 2018-11-17 stsp if (remain <= 0) {
802 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
806 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
807 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
808 ff2a4428 2019-03-19 stsp remain -= label_len;
809 f4a881ce 2018-11-17 stsp if (remain <= 0) {
810 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
813 ff2a4428 2019-03-19 stsp s += label_len;
814 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
815 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
816 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
817 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
818 ff2a4428 2019-03-19 stsp s += label_len;
819 ff2a4428 2019-03-19 stsp remain -= label_len;
820 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
821 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
822 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
823 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
824 ff2a4428 2019-03-19 stsp s += label_len;
825 ff2a4428 2019-03-19 stsp remain -= label_len;
826 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
827 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
828 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
829 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
830 ff2a4428 2019-03-19 stsp s += label_len;
831 ff2a4428 2019-03-19 stsp remain -= label_len;
832 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
833 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
834 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
835 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
836 ff2a4428 2019-03-19 stsp s += label_len;
837 ff2a4428 2019-03-19 stsp remain -= label_len;
839 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
843 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
844 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
849 f4a881ce 2018-11-17 stsp if (remain <= 0) {
850 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
854 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
858 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
859 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
861 f4a881ce 2018-11-17 stsp size_t slen;
862 ff2a4428 2019-03-19 stsp remain -= label_len;
863 f4a881ce 2018-11-17 stsp if (remain <= 0) {
864 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
867 ff2a4428 2019-03-19 stsp s += label_len;
868 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
869 f4a881ce 2018-11-17 stsp if (p == NULL) {
870 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
874 f4a881ce 2018-11-17 stsp slen = strlen(s);
875 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
876 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
877 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
880 f4a881ce 2018-11-17 stsp s += slen + 1;
881 f4a881ce 2018-11-17 stsp remain -= slen + 1;
882 f4a881ce 2018-11-17 stsp if (remain <= 0) {
883 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
887 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
891 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
892 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
894 f4a881ce 2018-11-17 stsp size_t slen;
896 ff2a4428 2019-03-19 stsp remain -= label_len;
897 f4a881ce 2018-11-17 stsp if (remain <= 0) {
898 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 ff2a4428 2019-03-19 stsp s += label_len;
902 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
903 f4a881ce 2018-11-17 stsp if (p == NULL) {
904 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
908 f4a881ce 2018-11-17 stsp slen = strlen(s);
909 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
910 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
913 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
914 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
915 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
918 f4a881ce 2018-11-17 stsp s += slen + 1;
919 f4a881ce 2018-11-17 stsp remain -= slen + 1;
920 f4a881ce 2018-11-17 stsp if (remain <= 0) {
921 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
925 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
926 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
927 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
928 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
933 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
934 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
935 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
940 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
941 f4a881ce 2018-11-17 stsp *tag = NULL;
943 f4a881ce 2018-11-17 stsp return err;
946 f4a881ce 2018-11-17 stsp const struct got_error *
947 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
949 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
950 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
951 a440fac0 2018-09-06 stsp size_t n, total, remain;
952 a440fac0 2018-09-06 stsp uint8_t *buf;
954 a440fac0 2018-09-06 stsp *outbuf = NULL;
955 a440fac0 2018-09-06 stsp *outlen = 0;
957 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
958 a440fac0 2018-09-06 stsp if (buf == NULL)
959 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
961 a440fac0 2018-09-06 stsp remain = blocksize;
964 a440fac0 2018-09-06 stsp if (remain == 0) {
965 a440fac0 2018-09-06 stsp uint8_t *newbuf;
966 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
967 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
968 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
971 a440fac0 2018-09-06 stsp buf = newbuf;
972 a440fac0 2018-09-06 stsp remain += blocksize;
974 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
975 a440fac0 2018-09-06 stsp if (n == 0) {
976 a440fac0 2018-09-06 stsp if (ferror(f)) {
977 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
980 a440fac0 2018-09-06 stsp break; /* EOF */
982 a440fac0 2018-09-06 stsp remain -= n;
983 a440fac0 2018-09-06 stsp total += n;
987 a440fac0 2018-09-06 stsp if (err == NULL) {
988 a440fac0 2018-09-06 stsp *outbuf = buf;
989 a440fac0 2018-09-06 stsp *outlen = total;
992 ad242220 2018-09-08 stsp return err;