2 fae40e36 2019-04-13 stsp * Copyright (c) 2018, 2019 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 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
49 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
50 15a94983 2018-12-23 stsp #include "got_lib_privsep.h"
51 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
53 a440fac0 2018-09-06 stsp #ifndef nitems
54 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
59 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
61 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
64 2ff12563 2018-09-15 stsp const struct got_error *
65 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
67 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
69 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
70 5df4932d 2018-11-05 stsp if (*qid == NULL)
71 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
73 5df4932d 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 5df4932d 2018-11-05 stsp if ((*qid)->id == NULL) {
75 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
76 5df4932d 2018-11-05 stsp got_object_qid_free(*qid);
77 5df4932d 2018-11-05 stsp *qid = NULL;
81 5df4932d 2018-11-05 stsp return NULL;
84 5df4932d 2018-11-05 stsp const struct got_error *
85 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
87 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
89 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
90 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
91 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
93 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
94 2ff12563 2018-09-15 stsp free(*outbuf);
95 2ff12563 2018-09-15 stsp *outbuf = NULL;
96 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
99 2ff12563 2018-09-15 stsp return NULL;
103 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
105 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
106 03fa71c8 2018-09-06 stsp obj->refcnt--;
107 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
111 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
112 03fa71c8 2018-09-06 stsp struct got_delta *delta;
113 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
114 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
115 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
116 2256993b 2019-07-15 stsp free(delta->delta_buf);
117 2256993b 2019-07-15 stsp free(delta);
120 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
121 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
126 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
128 03fa71c8 2018-09-06 stsp free(qid->id);
133 dd88155e 2019-06-29 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
135 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
137 dd88155e 2019-06-29 stsp while (!SIMPLEQ_EMPTY(ids)) {
138 dd88155e 2019-06-29 stsp qid = SIMPLEQ_FIRST(ids);
139 dd88155e 2019-06-29 stsp SIMPLEQ_REMOVE_HEAD(ids, entry);
140 dd88155e 2019-06-29 stsp got_object_qid_free(qid);
144 1785f84a 2018-12-23 stsp const struct got_error *
145 1785f84a 2018-12-23 stsp got_object_parse_header(struct got_object **obj, char *buf, size_t len)
147 ff2a4428 2019-03-19 stsp const char *obj_labels[] = {
148 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_COMMIT,
149 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TREE,
150 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_BLOB,
151 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TAG,
153 1785f84a 2018-12-23 stsp const int obj_types[] = {
154 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_COMMIT,
155 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TREE,
156 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_BLOB,
157 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TAG,
159 1785f84a 2018-12-23 stsp int type = 0;
160 1785f84a 2018-12-23 stsp size_t size = 0, hdrlen = 0;
163 1785f84a 2018-12-23 stsp *obj = NULL;
165 9ef4ac16 2019-04-13 stsp hdrlen = strnlen(buf, len) + 1 /* '\0' */;
166 9ef4ac16 2019-04-13 stsp if (hdrlen > len)
167 9ef4ac16 2019-04-13 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
169 ff2a4428 2019-03-19 stsp for (i = 0; i < nitems(obj_labels); i++) {
170 ff2a4428 2019-03-19 stsp const char *label = obj_labels[i];
171 ff2a4428 2019-03-19 stsp size_t label_len = strlen(label);
172 1785f84a 2018-12-23 stsp const char *errstr;
174 ff2a4428 2019-03-19 stsp if (strncmp(buf, label, label_len) != 0)
177 1785f84a 2018-12-23 stsp type = obj_types[i];
178 ff2a4428 2019-03-19 stsp if (len <= label_len)
179 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
180 ff2a4428 2019-03-19 stsp size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
181 1785f84a 2018-12-23 stsp if (errstr != NULL)
182 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
186 1785f84a 2018-12-23 stsp if (type == 0)
187 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
189 1785f84a 2018-12-23 stsp *obj = calloc(1, sizeof(**obj));
190 1785f84a 2018-12-23 stsp if (*obj == NULL)
191 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
192 1785f84a 2018-12-23 stsp (*obj)->type = type;
193 1785f84a 2018-12-23 stsp (*obj)->hdrlen = hdrlen;
194 1785f84a 2018-12-23 stsp (*obj)->size = size;
195 1785f84a 2018-12-23 stsp return NULL;
198 1785f84a 2018-12-23 stsp const struct got_error *
199 1785f84a 2018-12-23 stsp got_object_read_header(struct got_object **obj, int fd)
201 1785f84a 2018-12-23 stsp const struct got_error *err;
202 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
204 1785f84a 2018-12-23 stsp const size_t zbsize = 64;
205 1785f84a 2018-12-23 stsp size_t outlen, totlen;
206 1785f84a 2018-12-23 stsp int nbuf = 1;
208 1785f84a 2018-12-23 stsp *obj = NULL;
210 1785f84a 2018-12-23 stsp buf = malloc(zbsize);
211 1785f84a 2018-12-23 stsp if (buf == NULL)
212 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
214 1785f84a 2018-12-23 stsp err = got_inflate_init(&zb, buf, zbsize);
216 1785f84a 2018-12-23 stsp return err;
218 1785f84a 2018-12-23 stsp totlen = 0;
220 1785f84a 2018-12-23 stsp err = got_inflate_read_fd(&zb, fd, &outlen);
223 1785f84a 2018-12-23 stsp if (outlen == 0)
225 1785f84a 2018-12-23 stsp totlen += outlen;
226 dedbbd9d 2019-04-13 stsp if (memchr(zb.outbuf, '\0', outlen) == NULL) {
227 1785f84a 2018-12-23 stsp char *newbuf;
229 1785f84a 2018-12-23 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
230 1785f84a 2018-12-23 stsp if (newbuf == NULL) {
231 638f9024 2019-05-13 stsp err = got_error_from_errno("recallocarray");
234 1785f84a 2018-12-23 stsp buf = newbuf;
235 1785f84a 2018-12-23 stsp zb.outbuf = newbuf + totlen;
236 1785f84a 2018-12-23 stsp zb.outlen = (nbuf * zbsize) - totlen;
238 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
240 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
243 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
244 1785f84a 2018-12-23 stsp return err;
247 a440fac0 2018-09-06 stsp struct got_commit_object *
248 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
250 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
252 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
253 a440fac0 2018-09-06 stsp if (commit == NULL)
254 a440fac0 2018-09-06 stsp return NULL;
255 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
256 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
257 a440fac0 2018-09-06 stsp free(commit);
258 a440fac0 2018-09-06 stsp return NULL;
261 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
263 a440fac0 2018-09-06 stsp return commit;
266 a440fac0 2018-09-06 stsp const struct got_error *
267 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
268 a440fac0 2018-09-06 stsp const char *id_str)
270 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
271 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
273 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
275 7762fe12 2018-11-05 stsp return err;
277 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
278 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
279 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
280 a440fac0 2018-09-06 stsp return err;
283 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
284 a440fac0 2018-09-06 stsp commit->nparents++;
286 a440fac0 2018-09-06 stsp return NULL;
289 a440fac0 2018-09-06 stsp static const struct got_error *
290 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
292 a440fac0 2018-09-06 stsp int sign = 1;
293 a440fac0 2018-09-06 stsp const char *p = tzstr;
294 a440fac0 2018-09-06 stsp time_t h, m;
296 a440fac0 2018-09-06 stsp *gmtoff = 0;
298 a440fac0 2018-09-06 stsp if (*p == '-')
300 a440fac0 2018-09-06 stsp else if (*p != '+')
301 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
303 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
304 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
305 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
308 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
309 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
310 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
312 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
313 a440fac0 2018-09-06 stsp return NULL;
316 a440fac0 2018-09-06 stsp static const struct got_error *
317 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
319 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
320 a440fac0 2018-09-06 stsp const char *errstr;
321 a440fac0 2018-09-06 stsp char *space, *tzstr;
323 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
324 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
325 a440fac0 2018-09-06 stsp if (space == NULL)
326 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
327 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
328 a440fac0 2018-09-06 stsp if (tzstr == NULL)
329 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
330 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
331 a440fac0 2018-09-06 stsp free(tzstr);
333 a440fac0 2018-09-06 stsp return err;
334 a440fac0 2018-09-06 stsp *space = '\0';
336 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
337 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
338 a440fac0 2018-09-06 stsp if (space == NULL)
339 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
341 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
342 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
343 a440fac0 2018-09-06 stsp if (errstr)
344 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
346 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
347 a440fac0 2018-09-06 stsp *space = '\0';
349 a440fac0 2018-09-06 stsp return NULL;
353 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
355 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
356 03fa71c8 2018-09-06 stsp commit->refcnt--;
357 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
361 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
362 03fa71c8 2018-09-06 stsp free(commit->tree_id);
363 03fa71c8 2018-09-06 stsp free(commit->author);
364 03fa71c8 2018-09-06 stsp free(commit->committer);
365 03fa71c8 2018-09-06 stsp free(commit->logmsg);
366 03fa71c8 2018-09-06 stsp free(commit);
369 45d799e2 2018-12-23 stsp struct got_object_id *
370 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
372 45d799e2 2018-12-23 stsp return commit->tree_id;
376 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
378 45d799e2 2018-12-23 stsp return commit->nparents;
381 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
382 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
384 45d799e2 2018-12-23 stsp return &commit->parent_ids;
387 45d799e2 2018-12-23 stsp const char *
388 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
390 45d799e2 2018-12-23 stsp return commit->author;
394 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
396 45d799e2 2018-12-23 stsp return commit->author_time;
399 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
401 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
404 45d799e2 2018-12-23 stsp const char *
405 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
407 45d799e2 2018-12-23 stsp return commit->committer;
411 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
413 45d799e2 2018-12-23 stsp return commit->committer_time;
417 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
419 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
422 5943eee2 2019-08-13 stsp #define GOT_GPG_BEGIN_STR "gpgsig -----BEGIN PGP SIGNATURE-----"
423 5943eee2 2019-08-13 stsp #define GOT_GPG_END_STR " -----END PGP SIGNATURE-----"
425 5943eee2 2019-08-13 stsp const struct got_error *
426 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
428 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
429 5943eee2 2019-08-13 stsp int gpgsig = 0;
430 5943eee2 2019-08-13 stsp char *msg0, *msg, *line, *s;
431 5943eee2 2019-08-13 stsp size_t len;
433 5943eee2 2019-08-13 stsp *logmsg = NULL;
435 5943eee2 2019-08-13 stsp msg0 = strdup(commit->logmsg);
436 5943eee2 2019-08-13 stsp if (msg0 == NULL)
437 5943eee2 2019-08-13 stsp return got_error_from_errno("strdup");
439 5943eee2 2019-08-13 stsp /* Copy log message line by line to strip out GPG sigs... */
440 5943eee2 2019-08-13 stsp msg = msg0;
442 5943eee2 2019-08-13 stsp line = strsep(&msg, "\n");
444 5943eee2 2019-08-13 stsp if (line) {
445 5943eee2 2019-08-13 stsp /* Skip over GPG signatures. */
446 5943eee2 2019-08-13 stsp if (gpgsig) {
447 5943eee2 2019-08-13 stsp if (strcmp(line, GOT_GPG_END_STR) == 0) {
448 5943eee2 2019-08-13 stsp gpgsig = 0;
449 5943eee2 2019-08-13 stsp /* Skip empty line after sig. */
450 5943eee2 2019-08-13 stsp line = strsep(&msg, "\n");
453 5943eee2 2019-08-13 stsp } else if (strcmp(line, GOT_GPG_BEGIN_STR) == 0) {
454 5943eee2 2019-08-13 stsp gpgsig = 1;
457 5943eee2 2019-08-13 stsp if (asprintf(&s, "%s%s\n",
458 5943eee2 2019-08-13 stsp *logmsg ? *logmsg : "", line) == -1) {
459 5943eee2 2019-08-13 stsp err = got_error_from_errno("asprintf");
462 5943eee2 2019-08-13 stsp free(*logmsg);
463 5943eee2 2019-08-13 stsp *logmsg = s;
465 5943eee2 2019-08-13 stsp } while (line);
467 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
468 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
469 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
470 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
471 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
475 5943eee2 2019-08-13 stsp free(msg0);
477 5943eee2 2019-08-13 stsp free(*logmsg);
478 5943eee2 2019-08-13 stsp *logmsg = NULL;
480 5943eee2 2019-08-13 stsp return err;
483 a440fac0 2018-09-06 stsp const struct got_error *
484 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
485 5e0b25c4 2018-12-24 stsp size_t len)
487 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
488 a440fac0 2018-09-06 stsp char *s = buf;
489 ff2a4428 2019-03-19 stsp size_t label_len;
490 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
492 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
493 a440fac0 2018-09-06 stsp if (*commit == NULL)
494 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
496 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
497 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
498 ff2a4428 2019-03-19 stsp remain -= label_len;
499 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
500 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
503 ff2a4428 2019-03-19 stsp s += label_len;
504 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
505 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
508 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
509 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
511 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
515 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
516 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
517 ff2a4428 2019-03-19 stsp remain -= label_len;
518 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
519 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
522 ff2a4428 2019-03-19 stsp s += label_len;
523 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
527 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
528 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
531 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
532 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
534 a440fac0 2018-09-06 stsp size_t slen;
536 ff2a4428 2019-03-19 stsp remain -= label_len;
537 a440fac0 2018-09-06 stsp if (remain <= 0) {
538 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
541 ff2a4428 2019-03-19 stsp s += label_len;
542 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
543 a440fac0 2018-09-06 stsp if (p == NULL) {
544 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
548 a440fac0 2018-09-06 stsp slen = strlen(s);
549 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
550 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
553 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
554 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
555 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
558 a440fac0 2018-09-06 stsp s += slen + 1;
559 a440fac0 2018-09-06 stsp remain -= slen + 1;
562 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
563 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
565 a440fac0 2018-09-06 stsp size_t slen;
567 ff2a4428 2019-03-19 stsp remain -= label_len;
568 a440fac0 2018-09-06 stsp if (remain <= 0) {
569 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
572 ff2a4428 2019-03-19 stsp s += label_len;
573 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
574 a440fac0 2018-09-06 stsp if (p == NULL) {
575 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
579 a440fac0 2018-09-06 stsp slen = strlen(s);
580 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
581 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
584 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
585 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
586 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
589 a440fac0 2018-09-06 stsp s += slen + 1;
590 a440fac0 2018-09-06 stsp remain -= slen + 1;
593 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
594 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
595 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
600 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
601 a440fac0 2018-09-06 stsp *commit = NULL;
603 a440fac0 2018-09-06 stsp return err;
607 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
609 a440fac0 2018-09-06 stsp free(te->id);
610 a440fac0 2018-09-06 stsp free(te->name);
615 ed175427 2019-05-09 stsp got_object_tree_entries_close(struct got_tree_entries *entries)
617 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
619 ed175427 2019-05-09 stsp while (!SIMPLEQ_EMPTY(&entries->head)) {
620 ed175427 2019-05-09 stsp te = SIMPLEQ_FIRST(&entries->head);
621 ed175427 2019-05-09 stsp SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
622 ed175427 2019-05-09 stsp got_object_tree_entry_close(te);
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 ed175427 2019-05-09 stsp got_object_tree_entries_close(&tree->entries);
636 03fa71c8 2018-09-06 stsp free(tree);
639 a440fac0 2018-09-06 stsp struct got_tree_entry *
640 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
642 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
644 32ac459c 2018-11-05 stsp te = malloc(sizeof(*te));
645 a440fac0 2018-09-06 stsp if (te == NULL)
646 a440fac0 2018-09-06 stsp return NULL;
648 32ac459c 2018-11-05 stsp te->id = malloc(sizeof(*te->id));
649 a440fac0 2018-09-06 stsp if (te->id == NULL) {
656 a440fac0 2018-09-06 stsp static const struct got_error *
657 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
658 a440fac0 2018-09-06 stsp size_t maxlen)
660 8914529d 2019-04-13 stsp char *p, *space;
661 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
665 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
666 a440fac0 2018-09-06 stsp if (*te == NULL)
667 638f9024 2019-05-13 stsp return got_error_from_errno("got_alloc_tree_entry_partial");
669 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
670 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
672 a440fac0 2018-09-06 stsp *te = NULL;
673 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
676 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
677 8914529d 2019-04-13 stsp if (space == NULL || space <= buf) {
678 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
680 a440fac0 2018-09-06 stsp *te = NULL;
681 a440fac0 2018-09-06 stsp return err;
683 6dfaee02 2018-11-05 stsp (*te)->mode = 0;
685 8914529d 2019-04-13 stsp while (p < space) {
686 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
687 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
690 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
691 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
695 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
696 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
697 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
700 68bf1b1e 2018-11-07 stsp buf += *elen;
701 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
702 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
705 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
706 a440fac0 2018-09-06 stsp *te = NULL;
708 a440fac0 2018-09-06 stsp return err;
711 a440fac0 2018-09-06 stsp const struct got_error *
712 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
714 a440fac0 2018-09-06 stsp const struct got_error *err;
715 a440fac0 2018-09-06 stsp size_t remain = len;
716 f5d3d7af 2019-02-05 stsp struct got_pathlist_head pathlist;
717 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *pe;
719 f5d3d7af 2019-02-05 stsp TAILQ_INIT(&pathlist);
721 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
722 a440fac0 2018-09-06 stsp if (*tree == NULL)
723 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
725 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
727 a440fac0 2018-09-06 stsp while (remain > 0) {
728 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
729 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
730 a440fac0 2018-09-06 stsp size_t elen;
732 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
735 f5d3d7af 2019-02-05 stsp err = got_pathlist_insert(&new, &pathlist, te->name, te);
738 f5d3d7af 2019-02-05 stsp if (new == NULL) {
739 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
742 a440fac0 2018-09-06 stsp buf += elen;
743 a440fac0 2018-09-06 stsp remain -= elen;
746 a440fac0 2018-09-06 stsp if (remain != 0) {
747 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
748 13f977b4 2018-11-17 stsp *tree = NULL;
749 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 f5d3d7af 2019-02-05 stsp TAILQ_FOREACH(pe, &pathlist, entry) {
754 f5d3d7af 2019-02-05 stsp struct got_tree_entry *te = pe->data;
755 f5d3d7af 2019-02-05 stsp (*tree)->entries.nentries++;
756 f5d3d7af 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
759 f5d3d7af 2019-02-05 stsp got_pathlist_free(&pathlist);
760 f5d3d7af 2019-02-05 stsp return err;
764 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
766 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
767 ca0d469c 2019-08-13 stsp tag->refcnt--;
768 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
772 f4a881ce 2018-11-17 stsp free(tag->tag);
773 f4a881ce 2018-11-17 stsp free(tag->tagger);
774 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
778 ad242220 2018-09-08 stsp const struct got_error *
779 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
781 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
782 f4a881ce 2018-11-17 stsp size_t remain = len;
783 f4a881ce 2018-11-17 stsp char *s = buf;
784 ff2a4428 2019-03-19 stsp size_t label_len;
786 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
787 f4a881ce 2018-11-17 stsp if (*tag == NULL)
788 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
790 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
791 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
792 ff2a4428 2019-03-19 stsp remain -= label_len;
793 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
794 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
797 ff2a4428 2019-03-19 stsp s += label_len;
798 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
799 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
802 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
803 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
805 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
809 f4a881ce 2018-11-17 stsp if (remain <= 0) {
810 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
814 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
815 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
816 ff2a4428 2019-03-19 stsp remain -= label_len;
817 f4a881ce 2018-11-17 stsp if (remain <= 0) {
818 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
821 ff2a4428 2019-03-19 stsp s += label_len;
822 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
823 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
824 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
825 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
826 ff2a4428 2019-03-19 stsp s += label_len;
827 ff2a4428 2019-03-19 stsp remain -= label_len;
828 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
829 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
830 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
831 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
832 ff2a4428 2019-03-19 stsp s += label_len;
833 ff2a4428 2019-03-19 stsp remain -= label_len;
834 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
835 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
836 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
837 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
838 ff2a4428 2019-03-19 stsp s += label_len;
839 ff2a4428 2019-03-19 stsp remain -= label_len;
840 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
841 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
842 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
843 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
844 ff2a4428 2019-03-19 stsp s += label_len;
845 ff2a4428 2019-03-19 stsp remain -= label_len;
847 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
851 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
852 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
857 f4a881ce 2018-11-17 stsp if (remain <= 0) {
858 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
862 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
867 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
869 f4a881ce 2018-11-17 stsp size_t slen;
870 ff2a4428 2019-03-19 stsp remain -= label_len;
871 f4a881ce 2018-11-17 stsp if (remain <= 0) {
872 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
875 ff2a4428 2019-03-19 stsp s += label_len;
876 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
877 f4a881ce 2018-11-17 stsp if (p == NULL) {
878 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
882 f4a881ce 2018-11-17 stsp slen = strlen(s);
883 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
884 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
885 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
888 f4a881ce 2018-11-17 stsp s += slen + 1;
889 f4a881ce 2018-11-17 stsp remain -= slen + 1;
890 f4a881ce 2018-11-17 stsp if (remain <= 0) {
891 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
895 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
900 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
902 f4a881ce 2018-11-17 stsp size_t slen;
904 ff2a4428 2019-03-19 stsp remain -= label_len;
905 f4a881ce 2018-11-17 stsp if (remain <= 0) {
906 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
909 ff2a4428 2019-03-19 stsp s += label_len;
910 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
911 f4a881ce 2018-11-17 stsp if (p == NULL) {
912 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
916 f4a881ce 2018-11-17 stsp slen = strlen(s);
917 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
918 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
921 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
922 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
923 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
926 f4a881ce 2018-11-17 stsp s += slen + 1;
927 f4a881ce 2018-11-17 stsp remain -= slen + 1;
928 f4a881ce 2018-11-17 stsp if (remain <= 0) {
929 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
933 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
934 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
935 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
936 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
941 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
942 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
943 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
948 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
949 f4a881ce 2018-11-17 stsp *tag = NULL;
951 f4a881ce 2018-11-17 stsp return err;
954 f4a881ce 2018-11-17 stsp const struct got_error *
955 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
957 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
958 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
959 a440fac0 2018-09-06 stsp size_t n, total, remain;
960 a440fac0 2018-09-06 stsp uint8_t *buf;
962 a440fac0 2018-09-06 stsp *outbuf = NULL;
963 a440fac0 2018-09-06 stsp *outlen = 0;
965 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
966 a440fac0 2018-09-06 stsp if (buf == NULL)
967 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
969 a440fac0 2018-09-06 stsp remain = blocksize;
972 a440fac0 2018-09-06 stsp if (remain == 0) {
973 a440fac0 2018-09-06 stsp uint8_t *newbuf;
974 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
975 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
976 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
979 a440fac0 2018-09-06 stsp buf = newbuf;
980 a440fac0 2018-09-06 stsp remain += blocksize;
982 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
983 a440fac0 2018-09-06 stsp if (n == 0) {
984 a440fac0 2018-09-06 stsp if (ferror(f)) {
985 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
988 a440fac0 2018-09-06 stsp break; /* EOF */
990 a440fac0 2018-09-06 stsp remain -= n;
991 a440fac0 2018-09-06 stsp total += n;
995 a440fac0 2018-09-06 stsp if (err == NULL) {
996 a440fac0 2018-09-06 stsp *outbuf = buf;
997 a440fac0 2018-09-06 stsp *outlen = total;
1000 ad242220 2018-09-08 stsp return err;