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");
291 13b2bc37 2022-10-23 stsp err = got_inflate_to_fd(&len, f, &csum, outfd);
296 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
297 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
301 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
302 ae25a666 2023-02-23 op if (got_object_id_cmp(expected_id, &id) != 0) {
303 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
307 13b2bc37 2022-10-23 stsp *size = obj->size;
308 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
310 13b2bc37 2022-10-23 stsp got_object_close(obj);
311 13b2bc37 2022-10-23 stsp if (f && fclose(f) == EOF && err == NULL)
312 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
313 13b2bc37 2022-10-23 stsp return err;
316 a440fac0 2018-09-06 stsp struct got_commit_object *
317 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
319 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
321 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
322 a440fac0 2018-09-06 stsp if (commit == NULL)
323 a440fac0 2018-09-06 stsp return NULL;
324 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
325 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
326 a440fac0 2018-09-06 stsp free(commit);
327 a440fac0 2018-09-06 stsp return NULL;
330 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commit->parent_ids);
332 a440fac0 2018-09-06 stsp return commit;
335 a440fac0 2018-09-06 stsp const struct got_error *
336 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
337 a440fac0 2018-09-06 stsp const char *id_str)
339 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
340 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
342 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
344 7762fe12 2018-11-05 stsp return err;
346 87a3ab84 2023-02-23 op if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
347 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
349 a440fac0 2018-09-06 stsp return err;
352 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
353 a440fac0 2018-09-06 stsp commit->nparents++;
355 a440fac0 2018-09-06 stsp return NULL;
358 a440fac0 2018-09-06 stsp static const struct got_error *
359 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
361 a440fac0 2018-09-06 stsp int sign = 1;
362 a440fac0 2018-09-06 stsp const char *p = tzstr;
363 a440fac0 2018-09-06 stsp time_t h, m;
365 a440fac0 2018-09-06 stsp *gmtoff = 0;
367 a440fac0 2018-09-06 stsp if (*p == '-')
369 a440fac0 2018-09-06 stsp else if (*p != '+')
370 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
372 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
373 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
374 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
375 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
378 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
379 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
380 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
381 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
383 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
384 a440fac0 2018-09-06 stsp return NULL;
387 a440fac0 2018-09-06 stsp static const struct got_error *
388 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
390 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
391 a440fac0 2018-09-06 stsp const char *errstr;
392 a440fac0 2018-09-06 stsp char *space, *tzstr;
394 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
395 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
396 a440fac0 2018-09-06 stsp if (space == NULL)
397 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
398 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
399 a440fac0 2018-09-06 stsp if (tzstr == NULL)
400 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
401 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
402 a440fac0 2018-09-06 stsp free(tzstr);
404 9dbd8627 2021-02-04 stsp if (err->code != GOT_ERR_BAD_OBJ_DATA)
405 9dbd8627 2021-02-04 stsp return err;
406 9dbd8627 2021-02-04 stsp /* Old versions of Git omitted the timestamp. */
408 9dbd8627 2021-02-04 stsp *gmtoff = 0;
409 9dbd8627 2021-02-04 stsp return NULL;
411 a440fac0 2018-09-06 stsp *space = '\0';
413 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
414 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
415 a440fac0 2018-09-06 stsp if (space == NULL)
416 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
418 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
419 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
420 a440fac0 2018-09-06 stsp if (errstr)
421 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
423 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
424 a440fac0 2018-09-06 stsp *space = '\0';
426 a440fac0 2018-09-06 stsp return NULL;
430 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
432 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
433 03fa71c8 2018-09-06 stsp commit->refcnt--;
434 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
438 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
439 03fa71c8 2018-09-06 stsp free(commit->tree_id);
440 03fa71c8 2018-09-06 stsp free(commit->author);
441 03fa71c8 2018-09-06 stsp free(commit->committer);
442 03fa71c8 2018-09-06 stsp free(commit->logmsg);
443 03fa71c8 2018-09-06 stsp free(commit);
446 45d799e2 2018-12-23 stsp struct got_object_id *
447 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
449 45d799e2 2018-12-23 stsp return commit->tree_id;
453 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
455 45d799e2 2018-12-23 stsp return commit->nparents;
458 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
459 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
461 45d799e2 2018-12-23 stsp return &commit->parent_ids;
464 45d799e2 2018-12-23 stsp const char *
465 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
467 45d799e2 2018-12-23 stsp return commit->author;
471 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
473 45d799e2 2018-12-23 stsp return commit->author_time;
476 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
478 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
481 45d799e2 2018-12-23 stsp const char *
482 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
484 45d799e2 2018-12-23 stsp return commit->committer;
488 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
490 45d799e2 2018-12-23 stsp return commit->committer_time;
494 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
496 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
499 5943eee2 2019-08-13 stsp const struct got_error *
500 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
502 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
503 b9c41b54 2021-08-03 stsp const char *src;
505 5943eee2 2019-08-13 stsp size_t len;
507 b9c41b54 2021-08-03 stsp len = strlen(commit->logmsg);
508 b9c41b54 2021-08-03 stsp *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
509 b9c41b54 2021-08-03 stsp if (*logmsg == NULL)
510 b9c41b54 2021-08-03 stsp return got_error_from_errno("malloc");
513 b9c41b54 2021-08-03 stsp * Strip out unusual headers. Headers are separated from the commit
514 b9c41b54 2021-08-03 stsp * message body by a single empty line.
516 b9c41b54 2021-08-03 stsp src = commit->logmsg;
517 b9c41b54 2021-08-03 stsp dst = *logmsg;
518 b9c41b54 2021-08-03 stsp while (*src != '\0' && *src != '\n') {
519 b9c41b54 2021-08-03 stsp int copy_header = 1, eol = 0;
520 b9c41b54 2021-08-03 stsp if (strncmp(src, GOT_COMMIT_LABEL_TREE,
521 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
522 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
523 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
524 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_PARENT,
525 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
526 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
527 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
528 b9c41b54 2021-08-03 stsp copy_header = 0;
530 b9c41b54 2021-08-03 stsp while (*src != '\0' && !eol) {
531 b9c41b54 2021-08-03 stsp if (copy_header) {
532 b9c41b54 2021-08-03 stsp *dst = *src;
535 b9c41b54 2021-08-03 stsp if (*src == '\n')
540 b9c41b54 2021-08-03 stsp *dst = '\0';
542 b9c41b54 2021-08-03 stsp if (strlcat(*logmsg, src, len + 1) >= len + 1) {
543 b9c41b54 2021-08-03 stsp err = got_error(GOT_ERR_NO_SPACE);
547 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
548 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
549 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
550 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
551 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
555 b9c41b54 2021-08-03 stsp /* Append a trailing newline if missing. */
556 b9c41b54 2021-08-03 stsp if (len > 0 && (*logmsg)[len - 1] != '\n') {
557 b9c41b54 2021-08-03 stsp (*logmsg)[len] = '\n';
558 b9c41b54 2021-08-03 stsp (*logmsg)[len + 1] = '\0';
562 5943eee2 2019-08-13 stsp free(*logmsg);
563 5943eee2 2019-08-13 stsp *logmsg = NULL;
565 5943eee2 2019-08-13 stsp return err;
568 24ea5512 2019-08-22 stsp const char *
569 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
571 24ea5512 2019-08-22 stsp return commit->logmsg;
574 a440fac0 2018-09-06 stsp const struct got_error *
575 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
576 5e0b25c4 2018-12-24 stsp size_t len)
578 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
579 87a3ab84 2023-02-23 op enum got_hash_algorithm algo = GOT_HASH_SHA1;
580 a440fac0 2018-09-06 stsp char *s = buf;
581 ff2a4428 2019-03-19 stsp size_t label_len;
582 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
584 4793d91b 2019-09-22 stsp if (remain == 0)
585 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
587 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
588 a440fac0 2018-09-06 stsp if (*commit == NULL)
589 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
591 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
592 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
593 ff2a4428 2019-03-19 stsp remain -= label_len;
594 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
595 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 ff2a4428 2019-03-19 stsp s += label_len;
599 87a3ab84 2023-02-23 op if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
600 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
603 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
604 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
606 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
610 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
611 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
612 ff2a4428 2019-03-19 stsp remain -= label_len;
613 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
614 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
617 ff2a4428 2019-03-19 stsp s += label_len;
618 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
622 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
623 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
626 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
627 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
629 a440fac0 2018-09-06 stsp size_t slen;
631 ff2a4428 2019-03-19 stsp remain -= label_len;
632 a440fac0 2018-09-06 stsp if (remain <= 0) {
633 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
636 ff2a4428 2019-03-19 stsp s += label_len;
637 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
638 a440fac0 2018-09-06 stsp if (p == NULL) {
639 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 a440fac0 2018-09-06 stsp slen = strlen(s);
644 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
645 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
648 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
649 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
650 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
653 a440fac0 2018-09-06 stsp s += slen + 1;
654 a440fac0 2018-09-06 stsp remain -= slen + 1;
657 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
658 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
660 a440fac0 2018-09-06 stsp size_t slen;
662 ff2a4428 2019-03-19 stsp remain -= label_len;
663 a440fac0 2018-09-06 stsp if (remain <= 0) {
664 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
667 ff2a4428 2019-03-19 stsp s += label_len;
668 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
669 a440fac0 2018-09-06 stsp if (p == NULL) {
670 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 a440fac0 2018-09-06 stsp slen = strlen(s);
675 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
676 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
679 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
680 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
681 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
684 a440fac0 2018-09-06 stsp s += slen + 1;
685 a440fac0 2018-09-06 stsp remain -= slen + 1;
688 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
689 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
695 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
696 a440fac0 2018-09-06 stsp *commit = NULL;
698 a440fac0 2018-09-06 stsp return err;
701 13b2bc37 2022-10-23 stsp const struct got_error *
702 13b2bc37 2022-10-23 stsp got_object_read_commit(struct got_commit_object **commit, int fd,
703 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
705 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
706 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
707 13b2bc37 2022-10-23 stsp size_t len;
708 13b2bc37 2022-10-23 stsp uint8_t *p;
709 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
710 ae25a666 2023-02-23 op struct got_hash ctx;
711 13b2bc37 2022-10-23 stsp struct got_object_id id;
713 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
714 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
715 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
717 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
719 13b2bc37 2022-10-23 stsp return err;
721 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
722 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
723 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
727 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
731 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
732 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
736 13b2bc37 2022-10-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
737 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
741 13b2bc37 2022-10-23 stsp /* Skip object header. */
742 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
743 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, p + obj->hdrlen, len);
747 13b2bc37 2022-10-23 stsp got_object_close(obj);
748 13b2bc37 2022-10-23 stsp return err;
752 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
754 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
755 03fa71c8 2018-09-06 stsp tree->refcnt--;
756 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
760 56e0773d 2019-11-28 stsp free(tree->entries);
761 03fa71c8 2018-09-06 stsp free(tree);
764 a440fac0 2018-09-06 stsp static const struct got_error *
765 9985f404 2022-05-19 stsp parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
766 a440fac0 2018-09-06 stsp size_t maxlen)
768 8914529d 2019-04-13 stsp char *p, *space;
772 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
773 9985f404 2022-05-19 stsp if (*elen > maxlen)
774 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
776 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
777 9985f404 2022-05-19 stsp if (space == NULL || space <= buf)
778 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
780 9985f404 2022-05-19 stsp pte->mode = 0;
782 8914529d 2019-04-13 stsp while (p < space) {
783 00d10bca 2023-01-19 mark if (*p < '0' || *p > '7')
784 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
785 9985f404 2022-05-19 stsp pte->mode <<= 3;
786 9985f404 2022-05-19 stsp pte->mode |= *p - '0';
790 9985f404 2022-05-19 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
791 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
793 9985f404 2022-05-19 stsp pte->name = space + 1;
794 9985f404 2022-05-19 stsp pte->namelen = strlen(pte->name);
795 68bf1b1e 2018-11-07 stsp buf += *elen;
796 9985f404 2022-05-19 stsp pte->id = buf;
797 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
798 9985f404 2022-05-19 stsp return NULL;
802 9985f404 2022-05-19 stsp pte_cmp(const void *pa, const void *pb)
804 9985f404 2022-05-19 stsp const struct got_parsed_tree_entry *a = pa, *b = pb;
806 9985f404 2022-05-19 stsp return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
809 a440fac0 2018-09-06 stsp const struct got_error *
810 d294b1dc 2022-10-18 stsp got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
811 d294b1dc 2022-10-18 stsp size_t *nentries_alloc, uint8_t *buf, size_t len)
813 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
814 d294b1dc 2022-10-18 stsp size_t remain = len;
815 9985f404 2022-05-19 stsp const size_t nalloc = 16;
816 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *pte;
819 3022d272 2019-11-14 stsp *nentries = 0;
820 db1d3576 2019-10-04 stsp if (remain == 0)
821 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
823 a440fac0 2018-09-06 stsp while (remain > 0) {
824 a440fac0 2018-09-06 stsp size_t elen;
826 d294b1dc 2022-10-18 stsp if (*nentries >= *nentries_alloc) {
827 d294b1dc 2022-10-18 stsp pte = recallocarray(*entries, *nentries_alloc,
828 d294b1dc 2022-10-18 stsp *nentries_alloc + nalloc, sizeof(**entries));
829 9985f404 2022-05-19 stsp if (pte == NULL) {
830 9985f404 2022-05-19 stsp err = got_error_from_errno("recallocarray");
833 9985f404 2022-05-19 stsp *entries = pte;
834 d294b1dc 2022-10-18 stsp *nentries_alloc += nalloc;
837 9985f404 2022-05-19 stsp pte = &(*entries)[*nentries];
838 9985f404 2022-05-19 stsp err = parse_tree_entry(pte, &elen, buf, remain);
841 a440fac0 2018-09-06 stsp buf += elen;
842 a440fac0 2018-09-06 stsp remain -= elen;
843 3022d272 2019-11-14 stsp (*nentries)++;
846 a440fac0 2018-09-06 stsp if (remain != 0) {
847 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
851 9985f404 2022-05-19 stsp if (*nentries > 1) {
852 9985f404 2022-05-19 stsp mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
854 9985f404 2022-05-19 stsp for (i = 0; i < *nentries - 1; i++) {
855 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *prev = &(*entries)[i];
856 9985f404 2022-05-19 stsp pte = &(*entries)[i + 1];
857 9985f404 2022-05-19 stsp if (got_path_cmp(prev->name, pte->name,
858 9985f404 2022-05-19 stsp prev->namelen, pte->namelen) == 0) {
859 9985f404 2022-05-19 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
866 3022d272 2019-11-14 stsp *nentries = 0;
867 f5d3d7af 2019-02-05 stsp return err;
870 13b2bc37 2022-10-23 stsp const struct got_error *
871 13b2bc37 2022-10-23 stsp got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
872 13b2bc37 2022-10-23 stsp size_t *nentries_alloc, uint8_t **p, int fd,
873 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id)
875 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
876 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
877 13b2bc37 2022-10-23 stsp size_t len;
878 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
879 ae25a666 2023-02-23 op struct got_hash ctx;
880 13b2bc37 2022-10-23 stsp struct got_object_id id;
882 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
883 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
884 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
886 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
888 13b2bc37 2022-10-23 stsp return err;
890 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
891 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
892 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
896 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, *p, len);
900 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
901 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
905 13b2bc37 2022-10-23 stsp /* Skip object header. */
906 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
907 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(entries, nentries, nentries_alloc,
908 13b2bc37 2022-10-23 stsp *p + obj->hdrlen, len);
911 13b2bc37 2022-10-23 stsp got_object_close(obj);
912 13b2bc37 2022-10-23 stsp return err;
916 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
918 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
919 ca0d469c 2019-08-13 stsp tag->refcnt--;
920 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
924 f4a881ce 2018-11-17 stsp free(tag->tag);
925 f4a881ce 2018-11-17 stsp free(tag->tagger);
926 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
930 ad242220 2018-09-08 stsp const struct got_error *
931 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
933 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
934 87a3ab84 2023-02-23 op enum got_hash_algorithm algo = GOT_HASH_SHA1;
935 f4a881ce 2018-11-17 stsp size_t remain = len;
936 f4a881ce 2018-11-17 stsp char *s = buf;
937 ff2a4428 2019-03-19 stsp size_t label_len;
939 4793d91b 2019-09-22 stsp if (remain == 0)
940 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
942 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
943 f4a881ce 2018-11-17 stsp if (*tag == NULL)
944 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
946 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
947 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
948 ff2a4428 2019-03-19 stsp remain -= label_len;
949 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
950 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
953 ff2a4428 2019-03-19 stsp s += label_len;
954 87a3ab84 2023-02-23 op if (!got_parse_object_id(&(*tag)->id, s, algo)) {
955 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
958 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
959 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
961 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
965 f4a881ce 2018-11-17 stsp if (remain <= 0) {
966 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
970 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
971 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
972 ff2a4428 2019-03-19 stsp remain -= label_len;
973 f4a881ce 2018-11-17 stsp if (remain <= 0) {
974 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
977 ff2a4428 2019-03-19 stsp s += label_len;
978 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
979 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
980 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
981 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
982 ff2a4428 2019-03-19 stsp s += label_len;
983 ff2a4428 2019-03-19 stsp remain -= label_len;
984 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
985 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
986 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
987 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
988 ff2a4428 2019-03-19 stsp s += label_len;
989 ff2a4428 2019-03-19 stsp remain -= label_len;
990 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
991 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
992 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
993 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
994 ff2a4428 2019-03-19 stsp s += label_len;
995 ff2a4428 2019-03-19 stsp remain -= label_len;
996 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
997 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
998 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
999 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
1000 ff2a4428 2019-03-19 stsp s += label_len;
1001 ff2a4428 2019-03-19 stsp remain -= label_len;
1003 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1004 f4a881ce 2018-11-17 stsp goto done;
1007 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
1008 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1009 f4a881ce 2018-11-17 stsp goto done;
1013 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1014 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1015 f4a881ce 2018-11-17 stsp goto done;
1018 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1019 f4a881ce 2018-11-17 stsp goto done;
1022 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
1023 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1025 f4a881ce 2018-11-17 stsp size_t slen;
1026 ff2a4428 2019-03-19 stsp remain -= label_len;
1027 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1028 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1029 f4a881ce 2018-11-17 stsp goto done;
1031 ff2a4428 2019-03-19 stsp s += label_len;
1032 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1033 f4a881ce 2018-11-17 stsp if (p == NULL) {
1034 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1035 f4a881ce 2018-11-17 stsp goto done;
1037 f4a881ce 2018-11-17 stsp *p = '\0';
1038 f4a881ce 2018-11-17 stsp slen = strlen(s);
1039 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
1040 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1041 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1042 f4a881ce 2018-11-17 stsp goto done;
1044 f4a881ce 2018-11-17 stsp s += slen + 1;
1045 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1046 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1047 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1048 f4a881ce 2018-11-17 stsp goto done;
1051 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1052 f4a881ce 2018-11-17 stsp goto done;
1055 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
1056 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1058 f4a881ce 2018-11-17 stsp size_t slen;
1060 ff2a4428 2019-03-19 stsp remain -= label_len;
1061 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1062 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1063 f4a881ce 2018-11-17 stsp goto done;
1065 ff2a4428 2019-03-19 stsp s += label_len;
1066 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1067 f4a881ce 2018-11-17 stsp if (p == NULL) {
1068 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 f4a881ce 2018-11-17 stsp goto done;
1071 f4a881ce 2018-11-17 stsp *p = '\0';
1072 f4a881ce 2018-11-17 stsp slen = strlen(s);
1073 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
1074 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
1076 f4a881ce 2018-11-17 stsp goto done;
1077 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
1078 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1079 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1080 f4a881ce 2018-11-17 stsp goto done;
1082 f4a881ce 2018-11-17 stsp s += slen + 1;
1083 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1084 5a8b373c 2020-12-18 stsp if (remain < 0) {
1085 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1086 f4a881ce 2018-11-17 stsp goto done;
1089 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
1090 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
1091 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
1092 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1093 e0e55b50 2019-02-01 stsp goto done;
1097 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
1098 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1099 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1100 f4a881ce 2018-11-17 stsp goto done;
1103 f4a881ce 2018-11-17 stsp if (err) {
1104 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
1105 f4a881ce 2018-11-17 stsp *tag = NULL;
1107 13b2bc37 2022-10-23 stsp return err;
1110 13b2bc37 2022-10-23 stsp const struct got_error *
1111 758dc042 2022-11-06 stsp got_object_read_tag(struct got_tag_object **tag, int fd,
1112 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
1114 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1115 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
1116 13b2bc37 2022-10-23 stsp size_t len;
1117 13b2bc37 2022-10-23 stsp uint8_t *p;
1118 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
1119 ae25a666 2023-02-23 op struct got_hash ctx;
1120 13b2bc37 2022-10-23 stsp struct got_object_id id;
1122 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
1123 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
1124 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
1126 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1127 13b2bc37 2022-10-23 stsp expected_size, fd);
1129 13b2bc37 2022-10-23 stsp return err;
1131 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
1132 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
1133 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
1134 13b2bc37 2022-10-23 stsp goto done;
1137 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
1139 13b2bc37 2022-10-23 stsp goto done;
1141 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
1142 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1143 13b2bc37 2022-10-23 stsp goto done;
1146 13b2bc37 2022-10-23 stsp /* Skip object header. */
1147 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
1148 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1152 13b2bc37 2022-10-23 stsp got_object_close(obj);
1153 f4a881ce 2018-11-17 stsp return err;