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 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
21 a440fac0 2018-09-06 stsp #include <sys/uio.h>
22 a440fac0 2018-09-06 stsp #include <sys/socket.h>
23 a440fac0 2018-09-06 stsp #include <sys/wait.h>
24 64a8571e 2022-01-07 stsp #include <sys/mman.h>
26 a440fac0 2018-09-06 stsp #include <errno.h>
27 a440fac0 2018-09-06 stsp #include <stdio.h>
28 a440fac0 2018-09-06 stsp #include <stdlib.h>
29 a440fac0 2018-09-06 stsp #include <string.h>
30 a440fac0 2018-09-06 stsp #include <stdint.h>
31 a440fac0 2018-09-06 stsp #include <sha1.h>
32 5822e79e 2023-02-23 op #include <sha2.h>
33 a440fac0 2018-09-06 stsp #include <zlib.h>
34 a440fac0 2018-09-06 stsp #include <ctype.h>
35 a440fac0 2018-09-06 stsp #include <limits.h>
36 a440fac0 2018-09-06 stsp #include <imsg.h>
37 a440fac0 2018-09-06 stsp #include <time.h>
38 ad242220 2018-09-08 stsp #include <unistd.h>
40 a440fac0 2018-09-06 stsp #include "got_error.h"
41 a440fac0 2018-09-06 stsp #include "got_object.h"
42 a440fac0 2018-09-06 stsp #include "got_repository.h"
43 a440fac0 2018-09-06 stsp #include "got_opentemp.h"
44 324d37e7 2019-05-11 stsp #include "got_path.h"
46 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
47 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
48 41fa1437 2018-11-05 stsp #include "got_lib_inflate.h"
49 41fa1437 2018-11-05 stsp #include "got_lib_object.h"
50 3022d272 2019-11-14 stsp #include "got_lib_object_parse.h"
51 2f43cd69 2023-04-14 stsp #include "got_lib_object_qid.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
53 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
54 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
56 a440fac0 2018-09-06 stsp #ifndef nitems
57 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
60 2ff12563 2018-09-15 stsp const struct got_error *
61 9afa3de2 2023-04-04 stsp got_object_type_label(const char **label, int obj_type)
63 9afa3de2 2023-04-04 stsp const struct got_error *err = NULL;
65 9afa3de2 2023-04-04 stsp switch (obj_type) {
66 9afa3de2 2023-04-04 stsp case GOT_OBJ_TYPE_BLOB:
67 9afa3de2 2023-04-04 stsp *label = GOT_OBJ_LABEL_BLOB;
69 9afa3de2 2023-04-04 stsp case GOT_OBJ_TYPE_TREE:
70 9afa3de2 2023-04-04 stsp *label = GOT_OBJ_LABEL_TREE;
72 9afa3de2 2023-04-04 stsp case GOT_OBJ_TYPE_COMMIT:
73 9afa3de2 2023-04-04 stsp *label = GOT_OBJ_LABEL_COMMIT;
75 9afa3de2 2023-04-04 stsp case GOT_OBJ_TYPE_TAG:
76 9afa3de2 2023-04-04 stsp *label = GOT_OBJ_LABEL_TAG;
79 9afa3de2 2023-04-04 stsp *label = NULL;
80 9afa3de2 2023-04-04 stsp err = got_error(GOT_ERR_OBJ_TYPE);
88 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
90 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
91 03fa71c8 2018-09-06 stsp obj->refcnt--;
92 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
96 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
97 03fa71c8 2018-09-06 stsp struct got_delta *delta;
98 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&obj->deltas.entries)) {
99 dbdddfee 2021-06-23 naddy delta = STAILQ_FIRST(&obj->deltas.entries);
100 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
101 2256993b 2019-07-15 stsp free(delta);
107 d3c116bf 2021-10-15 stsp const struct got_error *
108 d3c116bf 2021-10-15 stsp got_object_raw_close(struct got_raw_object *obj)
110 d3c116bf 2021-10-15 stsp const struct got_error *err = NULL;
112 d3c116bf 2021-10-15 stsp if (obj->refcnt > 0) {
113 d3c116bf 2021-10-15 stsp obj->refcnt--;
114 d3c116bf 2021-10-15 stsp if (obj->refcnt > 0)
115 d3c116bf 2021-10-15 stsp return NULL;
118 13b2bc37 2022-10-23 stsp if (obj->close_cb)
119 13b2bc37 2022-10-23 stsp obj->close_cb(obj);
121 64a8571e 2022-01-07 stsp if (obj->f == NULL) {
122 64a8571e 2022-01-07 stsp if (obj->fd != -1) {
123 64a8571e 2022-01-07 stsp if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
124 64a8571e 2022-01-07 stsp err = got_error_from_errno("munmap");
125 64a8571e 2022-01-07 stsp if (close(obj->fd) == -1 && err == NULL)
126 64a8571e 2022-01-07 stsp err = got_error_from_errno("close");
128 64a8571e 2022-01-07 stsp free(obj->data);
130 64a8571e 2022-01-07 stsp if (fclose(obj->f) == EOF && err == NULL)
131 64a8571e 2022-01-07 stsp err = got_error_from_errno("fclose");
134 d3c116bf 2021-10-15 stsp return err;
137 1785f84a 2018-12-23 stsp const struct got_error *
138 1785f84a 2018-12-23 stsp got_object_parse_header(struct got_object **obj, char *buf, size_t len)
140 ff2a4428 2019-03-19 stsp const char *obj_labels[] = {
141 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_COMMIT,
142 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TREE,
143 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_BLOB,
144 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TAG,
146 1785f84a 2018-12-23 stsp const int obj_types[] = {
147 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_COMMIT,
148 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TREE,
149 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_BLOB,
150 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TAG,
152 1785f84a 2018-12-23 stsp int type = 0;
153 c7b17232 2022-01-28 stsp size_t size = 0;
157 1785f84a 2018-12-23 stsp *obj = NULL;
159 c7b17232 2022-01-28 stsp end = memchr(buf, '\0', len);
160 c7b17232 2022-01-28 stsp if (end == NULL)
161 9ef4ac16 2019-04-13 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
163 ff2a4428 2019-03-19 stsp for (i = 0; i < nitems(obj_labels); i++) {
164 ff2a4428 2019-03-19 stsp const char *label = obj_labels[i];
165 ff2a4428 2019-03-19 stsp size_t label_len = strlen(label);
166 1785f84a 2018-12-23 stsp const char *errstr;
168 c7b17232 2022-01-28 stsp if (len <= label_len || buf + label_len >= end ||
169 c7b17232 2022-01-28 stsp strncmp(buf, label, label_len) != 0)
172 1785f84a 2018-12-23 stsp type = obj_types[i];
173 ff2a4428 2019-03-19 stsp size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
174 1785f84a 2018-12-23 stsp if (errstr != NULL)
175 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
179 1785f84a 2018-12-23 stsp if (type == 0)
180 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
182 1785f84a 2018-12-23 stsp *obj = calloc(1, sizeof(**obj));
183 1785f84a 2018-12-23 stsp if (*obj == NULL)
184 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
185 1785f84a 2018-12-23 stsp (*obj)->type = type;
186 c7b17232 2022-01-28 stsp (*obj)->hdrlen = end - buf + 1;
187 1785f84a 2018-12-23 stsp (*obj)->size = size;
188 1785f84a 2018-12-23 stsp return NULL;
191 1785f84a 2018-12-23 stsp const struct got_error *
192 1785f84a 2018-12-23 stsp got_object_read_header(struct got_object **obj, int fd)
194 1785f84a 2018-12-23 stsp const struct got_error *err;
195 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
196 31e61ec1 2021-09-28 naddy uint8_t *buf;
197 1785f84a 2018-12-23 stsp const size_t zbsize = 64;
198 1785f84a 2018-12-23 stsp size_t outlen, totlen;
199 1785f84a 2018-12-23 stsp int nbuf = 1;
201 1785f84a 2018-12-23 stsp *obj = NULL;
203 1785f84a 2018-12-23 stsp buf = malloc(zbsize);
204 1785f84a 2018-12-23 stsp if (buf == NULL)
205 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
206 c7b17232 2022-01-28 stsp buf[0] = '\0';
208 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, buf, zbsize, NULL);
210 1785f84a 2018-12-23 stsp return err;
212 1785f84a 2018-12-23 stsp totlen = 0;
214 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
217 1785f84a 2018-12-23 stsp if (outlen == 0)
219 1785f84a 2018-12-23 stsp totlen += outlen;
220 dedbbd9d 2019-04-13 stsp if (memchr(zb.outbuf, '\0', outlen) == NULL) {
221 31e61ec1 2021-09-28 naddy uint8_t *newbuf;
223 1785f84a 2018-12-23 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
224 1785f84a 2018-12-23 stsp if (newbuf == NULL) {
225 638f9024 2019-05-13 stsp err = got_error_from_errno("recallocarray");
228 1785f84a 2018-12-23 stsp buf = newbuf;
229 1785f84a 2018-12-23 stsp zb.outbuf = newbuf + totlen;
230 1785f84a 2018-12-23 stsp zb.outlen = (nbuf * zbsize) - totlen;
232 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
234 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
237 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
238 1785f84a 2018-12-23 stsp return err;
241 13b2bc37 2022-10-23 stsp const struct got_error *
242 13b2bc37 2022-10-23 stsp got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
243 13b2bc37 2022-10-23 stsp size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
246 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
247 13b2bc37 2022-10-23 stsp struct got_object *obj;
248 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
249 ae25a666 2023-02-23 op struct got_object_id id;
250 ae25a666 2023-02-23 op struct got_hash ctx;
251 13b2bc37 2022-10-23 stsp size_t len, consumed;
252 13b2bc37 2022-10-23 stsp FILE *f = NULL;
254 13b2bc37 2022-10-23 stsp *outbuf = NULL;
256 13b2bc37 2022-10-23 stsp *hdrlen = 0;
258 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
259 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
260 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
262 13b2bc37 2022-10-23 stsp if (lseek(infd, SEEK_SET, 0) == -1)
263 13b2bc37 2022-10-23 stsp return got_error_from_errno("lseek");
265 13b2bc37 2022-10-23 stsp err = got_object_read_header(&obj, infd);
267 13b2bc37 2022-10-23 stsp return err;
269 13b2bc37 2022-10-23 stsp if (lseek(infd, SEEK_SET, 0) == -1)
270 13b2bc37 2022-10-23 stsp return got_error_from_errno("lseek");
272 13b2bc37 2022-10-23 stsp if (obj->size + obj->hdrlen <= max_in_mem_size) {
273 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
274 13b2bc37 2022-10-23 stsp obj->size + obj->hdrlen, infd);
278 13b2bc37 2022-10-23 stsp * XXX This uses an extra file descriptor for no good reason.
279 13b2bc37 2022-10-23 stsp * We should have got_inflate_fd_to_fd().
281 13b2bc37 2022-10-23 stsp fd = dup(infd);
282 13b2bc37 2022-10-23 stsp if (fd == -1)
283 13b2bc37 2022-10-23 stsp return got_error_from_errno("dup");
284 13b2bc37 2022-10-23 stsp f = fdopen(fd, "r");
285 13b2bc37 2022-10-23 stsp if (f == NULL) {
286 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
290 13b2bc37 2022-10-23 stsp err = got_inflate_to_fd(&len, f, &csum, outfd);
295 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
296 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
300 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
301 ae25a666 2023-02-23 op if (got_object_id_cmp(expected_id, &id) != 0) {
302 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
306 13b2bc37 2022-10-23 stsp *size = obj->size;
307 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
309 13b2bc37 2022-10-23 stsp got_object_close(obj);
310 13b2bc37 2022-10-23 stsp if (f && fclose(f) == EOF && err == NULL)
311 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
312 13b2bc37 2022-10-23 stsp return err;
315 a440fac0 2018-09-06 stsp struct got_commit_object *
316 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
318 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
320 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
321 a440fac0 2018-09-06 stsp if (commit == NULL)
322 a440fac0 2018-09-06 stsp return NULL;
323 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
324 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
325 a440fac0 2018-09-06 stsp free(commit);
326 a440fac0 2018-09-06 stsp return NULL;
329 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commit->parent_ids);
331 a440fac0 2018-09-06 stsp return commit;
334 a440fac0 2018-09-06 stsp const struct got_error *
335 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
336 a440fac0 2018-09-06 stsp const char *id_str)
338 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
339 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
341 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
343 7762fe12 2018-11-05 stsp return err;
345 87a3ab84 2023-02-23 op if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
346 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
348 a440fac0 2018-09-06 stsp return err;
351 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
352 a440fac0 2018-09-06 stsp commit->nparents++;
354 a440fac0 2018-09-06 stsp return NULL;
357 a440fac0 2018-09-06 stsp static const struct got_error *
358 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
360 a440fac0 2018-09-06 stsp int sign = 1;
361 a440fac0 2018-09-06 stsp const char *p = tzstr;
362 a440fac0 2018-09-06 stsp time_t h, m;
364 a440fac0 2018-09-06 stsp *gmtoff = 0;
366 a440fac0 2018-09-06 stsp if (*p == '-')
368 a440fac0 2018-09-06 stsp else if (*p != '+')
369 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
371 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
372 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
373 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
374 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
377 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
378 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
379 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
380 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
382 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
383 a440fac0 2018-09-06 stsp return NULL;
386 a440fac0 2018-09-06 stsp static const struct got_error *
387 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
389 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
390 a440fac0 2018-09-06 stsp const char *errstr;
391 a440fac0 2018-09-06 stsp char *space, *tzstr;
393 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
394 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
395 a440fac0 2018-09-06 stsp if (space == NULL)
396 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
397 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
398 a440fac0 2018-09-06 stsp if (tzstr == NULL)
399 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
400 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
401 a440fac0 2018-09-06 stsp free(tzstr);
403 9dbd8627 2021-02-04 stsp if (err->code != GOT_ERR_BAD_OBJ_DATA)
404 9dbd8627 2021-02-04 stsp return err;
405 9dbd8627 2021-02-04 stsp /* Old versions of Git omitted the timestamp. */
407 9dbd8627 2021-02-04 stsp *gmtoff = 0;
408 9dbd8627 2021-02-04 stsp return NULL;
410 a440fac0 2018-09-06 stsp *space = '\0';
412 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
413 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
414 a440fac0 2018-09-06 stsp if (space == NULL)
415 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
417 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
418 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
419 a440fac0 2018-09-06 stsp if (errstr)
420 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
422 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
423 a440fac0 2018-09-06 stsp *space = '\0';
425 a440fac0 2018-09-06 stsp return NULL;
429 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
431 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
432 03fa71c8 2018-09-06 stsp commit->refcnt--;
433 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
437 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
438 03fa71c8 2018-09-06 stsp free(commit->tree_id);
439 03fa71c8 2018-09-06 stsp free(commit->author);
440 03fa71c8 2018-09-06 stsp free(commit->committer);
441 03fa71c8 2018-09-06 stsp free(commit->logmsg);
442 03fa71c8 2018-09-06 stsp free(commit);
445 45d799e2 2018-12-23 stsp struct got_object_id *
446 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
448 45d799e2 2018-12-23 stsp return commit->tree_id;
452 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
454 45d799e2 2018-12-23 stsp return commit->nparents;
457 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
458 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
460 45d799e2 2018-12-23 stsp return &commit->parent_ids;
463 45d799e2 2018-12-23 stsp const char *
464 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
466 45d799e2 2018-12-23 stsp return commit->author;
470 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
472 45d799e2 2018-12-23 stsp return commit->author_time;
475 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
477 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
480 45d799e2 2018-12-23 stsp const char *
481 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
483 45d799e2 2018-12-23 stsp return commit->committer;
487 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
489 45d799e2 2018-12-23 stsp return commit->committer_time;
493 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
495 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
498 5943eee2 2019-08-13 stsp const struct got_error *
499 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
501 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
502 b9c41b54 2021-08-03 stsp const char *src;
504 5943eee2 2019-08-13 stsp size_t len;
506 b9c41b54 2021-08-03 stsp len = strlen(commit->logmsg);
507 b9c41b54 2021-08-03 stsp *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
508 b9c41b54 2021-08-03 stsp if (*logmsg == NULL)
509 b9c41b54 2021-08-03 stsp return got_error_from_errno("malloc");
512 b9c41b54 2021-08-03 stsp * Strip out unusual headers. Headers are separated from the commit
513 b9c41b54 2021-08-03 stsp * message body by a single empty line.
515 b9c41b54 2021-08-03 stsp src = commit->logmsg;
516 b9c41b54 2021-08-03 stsp dst = *logmsg;
517 b9c41b54 2021-08-03 stsp while (*src != '\0' && *src != '\n') {
518 b9c41b54 2021-08-03 stsp int copy_header = 1, eol = 0;
519 b9c41b54 2021-08-03 stsp if (strncmp(src, GOT_COMMIT_LABEL_TREE,
520 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
521 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
522 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
523 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_PARENT,
524 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
525 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
526 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
527 b9c41b54 2021-08-03 stsp copy_header = 0;
529 b9c41b54 2021-08-03 stsp while (*src != '\0' && !eol) {
530 b9c41b54 2021-08-03 stsp if (copy_header) {
531 b9c41b54 2021-08-03 stsp *dst = *src;
534 b9c41b54 2021-08-03 stsp if (*src == '\n')
539 b9c41b54 2021-08-03 stsp *dst = '\0';
541 b9c41b54 2021-08-03 stsp if (strlcat(*logmsg, src, len + 1) >= len + 1) {
542 b9c41b54 2021-08-03 stsp err = got_error(GOT_ERR_NO_SPACE);
546 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
547 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
548 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
549 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
550 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
554 b9c41b54 2021-08-03 stsp /* Append a trailing newline if missing. */
555 b9c41b54 2021-08-03 stsp if (len > 0 && (*logmsg)[len - 1] != '\n') {
556 b9c41b54 2021-08-03 stsp (*logmsg)[len] = '\n';
557 b9c41b54 2021-08-03 stsp (*logmsg)[len + 1] = '\0';
561 5943eee2 2019-08-13 stsp free(*logmsg);
562 5943eee2 2019-08-13 stsp *logmsg = NULL;
564 5943eee2 2019-08-13 stsp return err;
567 24ea5512 2019-08-22 stsp const char *
568 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
570 24ea5512 2019-08-22 stsp return commit->logmsg;
573 a440fac0 2018-09-06 stsp const struct got_error *
574 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
575 5e0b25c4 2018-12-24 stsp size_t len)
577 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
578 87a3ab84 2023-02-23 op enum got_hash_algorithm algo = GOT_HASH_SHA1;
579 a440fac0 2018-09-06 stsp char *s = buf;
580 ff2a4428 2019-03-19 stsp size_t label_len;
581 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
583 4793d91b 2019-09-22 stsp if (remain == 0)
584 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
586 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
587 a440fac0 2018-09-06 stsp if (*commit == NULL)
588 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
590 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
591 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
592 ff2a4428 2019-03-19 stsp remain -= label_len;
593 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
594 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
597 ff2a4428 2019-03-19 stsp s += label_len;
598 87a3ab84 2023-02-23 op if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
599 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
602 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
603 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
605 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
609 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
610 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
611 ff2a4428 2019-03-19 stsp remain -= label_len;
612 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
613 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
616 ff2a4428 2019-03-19 stsp s += label_len;
617 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
621 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
622 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
625 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
626 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
628 a440fac0 2018-09-06 stsp size_t slen;
630 ff2a4428 2019-03-19 stsp remain -= label_len;
631 a440fac0 2018-09-06 stsp if (remain <= 0) {
632 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 ff2a4428 2019-03-19 stsp s += label_len;
636 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
637 a440fac0 2018-09-06 stsp if (p == NULL) {
638 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 a440fac0 2018-09-06 stsp slen = strlen(s);
643 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
644 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
647 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
648 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
649 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
652 a440fac0 2018-09-06 stsp s += slen + 1;
653 a440fac0 2018-09-06 stsp remain -= slen + 1;
656 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
657 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
659 a440fac0 2018-09-06 stsp size_t slen;
661 ff2a4428 2019-03-19 stsp remain -= label_len;
662 a440fac0 2018-09-06 stsp if (remain <= 0) {
663 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
666 ff2a4428 2019-03-19 stsp s += label_len;
667 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
668 a440fac0 2018-09-06 stsp if (p == NULL) {
669 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
673 a440fac0 2018-09-06 stsp slen = strlen(s);
674 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
675 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
678 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
679 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
680 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
683 a440fac0 2018-09-06 stsp s += slen + 1;
684 a440fac0 2018-09-06 stsp remain -= slen + 1;
687 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
688 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
689 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
694 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
695 a440fac0 2018-09-06 stsp *commit = NULL;
697 a440fac0 2018-09-06 stsp return err;
700 13b2bc37 2022-10-23 stsp const struct got_error *
701 13b2bc37 2022-10-23 stsp got_object_read_commit(struct got_commit_object **commit, int fd,
702 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
704 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
705 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
706 13b2bc37 2022-10-23 stsp size_t len;
707 13b2bc37 2022-10-23 stsp uint8_t *p;
708 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
709 ae25a666 2023-02-23 op struct got_hash ctx;
710 13b2bc37 2022-10-23 stsp struct got_object_id id;
712 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
713 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
714 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
716 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
718 13b2bc37 2022-10-23 stsp return err;
720 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
721 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
722 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
726 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
730 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
731 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
735 13b2bc37 2022-10-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
736 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
740 13b2bc37 2022-10-23 stsp /* Skip object header. */
741 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
742 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, p + obj->hdrlen, len);
746 13b2bc37 2022-10-23 stsp got_object_close(obj);
747 13b2bc37 2022-10-23 stsp return err;
751 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
753 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
754 03fa71c8 2018-09-06 stsp tree->refcnt--;
755 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
759 56e0773d 2019-11-28 stsp free(tree->entries);
760 03fa71c8 2018-09-06 stsp free(tree);
763 cdfe5501 2023-04-24 stsp const struct got_error *
764 cdfe5501 2023-04-24 stsp got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
765 cdfe5501 2023-04-24 stsp char *buf, size_t maxlen)
767 8914529d 2019-04-13 stsp char *p, *space;
771 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
772 9985f404 2022-05-19 stsp if (*elen > maxlen)
773 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
775 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
776 9985f404 2022-05-19 stsp if (space == NULL || space <= buf)
777 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
779 9985f404 2022-05-19 stsp pte->mode = 0;
781 8914529d 2019-04-13 stsp while (p < space) {
782 00d10bca 2023-01-19 mark if (*p < '0' || *p > '7')
783 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
784 9985f404 2022-05-19 stsp pte->mode <<= 3;
785 9985f404 2022-05-19 stsp pte->mode |= *p - '0';
789 9985f404 2022-05-19 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
790 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
792 9985f404 2022-05-19 stsp pte->name = space + 1;
793 9985f404 2022-05-19 stsp pte->namelen = strlen(pte->name);
794 68bf1b1e 2018-11-07 stsp buf += *elen;
795 9985f404 2022-05-19 stsp pte->id = buf;
796 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
797 9985f404 2022-05-19 stsp return NULL;
801 9985f404 2022-05-19 stsp pte_cmp(const void *pa, const void *pb)
803 9985f404 2022-05-19 stsp const struct got_parsed_tree_entry *a = pa, *b = pb;
805 9985f404 2022-05-19 stsp return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
808 a440fac0 2018-09-06 stsp const struct got_error *
809 d294b1dc 2022-10-18 stsp got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
810 d294b1dc 2022-10-18 stsp size_t *nentries_alloc, uint8_t *buf, size_t len)
812 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
813 d294b1dc 2022-10-18 stsp size_t remain = len;
814 9985f404 2022-05-19 stsp const size_t nalloc = 16;
815 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *pte;
818 3022d272 2019-11-14 stsp *nentries = 0;
819 db1d3576 2019-10-04 stsp if (remain == 0)
820 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
822 a440fac0 2018-09-06 stsp while (remain > 0) {
823 a440fac0 2018-09-06 stsp size_t elen;
825 d294b1dc 2022-10-18 stsp if (*nentries >= *nentries_alloc) {
826 d294b1dc 2022-10-18 stsp pte = recallocarray(*entries, *nentries_alloc,
827 d294b1dc 2022-10-18 stsp *nentries_alloc + nalloc, sizeof(**entries));
828 9985f404 2022-05-19 stsp if (pte == NULL) {
829 9985f404 2022-05-19 stsp err = got_error_from_errno("recallocarray");
832 9985f404 2022-05-19 stsp *entries = pte;
833 d294b1dc 2022-10-18 stsp *nentries_alloc += nalloc;
836 9985f404 2022-05-19 stsp pte = &(*entries)[*nentries];
837 cdfe5501 2023-04-24 stsp err = got_object_parse_tree_entry(pte, &elen, buf, remain);
840 a440fac0 2018-09-06 stsp buf += elen;
841 a440fac0 2018-09-06 stsp remain -= elen;
842 3022d272 2019-11-14 stsp (*nentries)++;
845 a440fac0 2018-09-06 stsp if (remain != 0) {
846 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
850 9985f404 2022-05-19 stsp if (*nentries > 1) {
851 9985f404 2022-05-19 stsp mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
853 9985f404 2022-05-19 stsp for (i = 0; i < *nentries - 1; i++) {
854 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *prev = &(*entries)[i];
855 9985f404 2022-05-19 stsp pte = &(*entries)[i + 1];
856 9985f404 2022-05-19 stsp if (got_path_cmp(prev->name, pte->name,
857 9985f404 2022-05-19 stsp prev->namelen, pte->namelen) == 0) {
858 9985f404 2022-05-19 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
865 3022d272 2019-11-14 stsp *nentries = 0;
866 f5d3d7af 2019-02-05 stsp return err;
869 13b2bc37 2022-10-23 stsp const struct got_error *
870 13b2bc37 2022-10-23 stsp got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
871 13b2bc37 2022-10-23 stsp size_t *nentries_alloc, uint8_t **p, int fd,
872 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id)
874 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
875 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
876 13b2bc37 2022-10-23 stsp size_t len;
877 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
878 ae25a666 2023-02-23 op struct got_hash ctx;
879 13b2bc37 2022-10-23 stsp struct got_object_id id;
881 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
882 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
883 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
885 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
887 13b2bc37 2022-10-23 stsp return err;
889 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
890 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
891 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
895 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, *p, len);
899 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
900 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
904 13b2bc37 2022-10-23 stsp /* Skip object header. */
905 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
906 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(entries, nentries, nentries_alloc,
907 13b2bc37 2022-10-23 stsp *p + obj->hdrlen, len);
910 13b2bc37 2022-10-23 stsp got_object_close(obj);
911 13b2bc37 2022-10-23 stsp return err;
915 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
917 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
918 ca0d469c 2019-08-13 stsp tag->refcnt--;
919 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
923 f4a881ce 2018-11-17 stsp free(tag->tag);
924 f4a881ce 2018-11-17 stsp free(tag->tagger);
925 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
929 ad242220 2018-09-08 stsp const struct got_error *
930 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
932 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
933 87a3ab84 2023-02-23 op enum got_hash_algorithm algo = GOT_HASH_SHA1;
934 f4a881ce 2018-11-17 stsp size_t remain = len;
935 f4a881ce 2018-11-17 stsp char *s = buf;
936 ff2a4428 2019-03-19 stsp size_t label_len;
938 4793d91b 2019-09-22 stsp if (remain == 0)
939 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
941 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
942 f4a881ce 2018-11-17 stsp if (*tag == NULL)
943 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
945 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
946 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
947 ff2a4428 2019-03-19 stsp remain -= label_len;
948 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
949 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
952 ff2a4428 2019-03-19 stsp s += label_len;
953 87a3ab84 2023-02-23 op if (!got_parse_object_id(&(*tag)->id, s, algo)) {
954 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
957 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
958 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
960 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
964 f4a881ce 2018-11-17 stsp if (remain <= 0) {
965 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
969 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
970 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
971 ff2a4428 2019-03-19 stsp remain -= label_len;
972 f4a881ce 2018-11-17 stsp if (remain <= 0) {
973 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
976 ff2a4428 2019-03-19 stsp s += label_len;
977 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
978 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
979 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
980 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
981 ff2a4428 2019-03-19 stsp s += label_len;
982 ff2a4428 2019-03-19 stsp remain -= label_len;
983 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
984 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
985 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
986 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
987 ff2a4428 2019-03-19 stsp s += label_len;
988 ff2a4428 2019-03-19 stsp remain -= label_len;
989 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
990 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
991 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
992 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
993 ff2a4428 2019-03-19 stsp s += label_len;
994 ff2a4428 2019-03-19 stsp remain -= label_len;
995 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
996 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
997 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
998 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
999 ff2a4428 2019-03-19 stsp s += label_len;
1000 ff2a4428 2019-03-19 stsp remain -= label_len;
1002 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1003 f4a881ce 2018-11-17 stsp goto done;
1006 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
1007 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1008 f4a881ce 2018-11-17 stsp goto done;
1012 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1013 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1014 f4a881ce 2018-11-17 stsp goto done;
1017 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1018 f4a881ce 2018-11-17 stsp goto done;
1021 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
1022 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1024 f4a881ce 2018-11-17 stsp size_t slen;
1025 ff2a4428 2019-03-19 stsp remain -= label_len;
1026 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1027 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1028 f4a881ce 2018-11-17 stsp goto done;
1030 ff2a4428 2019-03-19 stsp s += label_len;
1031 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1032 f4a881ce 2018-11-17 stsp if (p == NULL) {
1033 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1034 f4a881ce 2018-11-17 stsp goto done;
1036 f4a881ce 2018-11-17 stsp *p = '\0';
1037 f4a881ce 2018-11-17 stsp slen = strlen(s);
1038 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
1039 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1040 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1041 f4a881ce 2018-11-17 stsp goto done;
1043 f4a881ce 2018-11-17 stsp s += slen + 1;
1044 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1045 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1046 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1047 f4a881ce 2018-11-17 stsp goto done;
1050 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 f4a881ce 2018-11-17 stsp goto done;
1054 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
1055 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1057 f4a881ce 2018-11-17 stsp size_t slen;
1059 ff2a4428 2019-03-19 stsp remain -= label_len;
1060 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1061 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1062 f4a881ce 2018-11-17 stsp goto done;
1064 ff2a4428 2019-03-19 stsp s += label_len;
1065 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1066 f4a881ce 2018-11-17 stsp if (p == NULL) {
1067 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1068 f4a881ce 2018-11-17 stsp goto done;
1070 f4a881ce 2018-11-17 stsp *p = '\0';
1071 f4a881ce 2018-11-17 stsp slen = strlen(s);
1072 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
1073 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
1075 f4a881ce 2018-11-17 stsp goto done;
1076 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
1077 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1078 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1079 f4a881ce 2018-11-17 stsp goto done;
1081 f4a881ce 2018-11-17 stsp s += slen + 1;
1082 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1083 5a8b373c 2020-12-18 stsp if (remain < 0) {
1084 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1085 f4a881ce 2018-11-17 stsp goto done;
1088 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
1089 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
1090 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
1091 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1092 e0e55b50 2019-02-01 stsp goto done;
1096 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
1097 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1098 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1099 f4a881ce 2018-11-17 stsp goto done;
1102 f4a881ce 2018-11-17 stsp if (err) {
1103 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
1104 f4a881ce 2018-11-17 stsp *tag = NULL;
1106 13b2bc37 2022-10-23 stsp return err;
1109 13b2bc37 2022-10-23 stsp const struct got_error *
1110 758dc042 2022-11-06 stsp got_object_read_tag(struct got_tag_object **tag, int fd,
1111 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
1113 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1114 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
1115 13b2bc37 2022-10-23 stsp size_t len;
1116 13b2bc37 2022-10-23 stsp uint8_t *p;
1117 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
1118 ae25a666 2023-02-23 op struct got_hash ctx;
1119 13b2bc37 2022-10-23 stsp struct got_object_id id;
1121 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
1122 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
1123 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
1125 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1126 13b2bc37 2022-10-23 stsp expected_size, fd);
1128 13b2bc37 2022-10-23 stsp return err;
1130 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
1131 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
1132 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
1133 13b2bc37 2022-10-23 stsp goto done;
1136 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
1138 13b2bc37 2022-10-23 stsp goto done;
1140 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
1141 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1142 13b2bc37 2022-10-23 stsp goto done;
1145 13b2bc37 2022-10-23 stsp /* Skip object header. */
1146 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
1147 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1151 13b2bc37 2022-10-23 stsp got_object_close(obj);
1152 f4a881ce 2018-11-17 stsp return err;