Blame


1 a440fac0 2018-09-06 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 a440fac0 2018-09-06 stsp *
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.
7 a440fac0 2018-09-06 stsp *
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.
15 a440fac0 2018-09-06 stsp */
16 a440fac0 2018-09-06 stsp
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>
25 a440fac0 2018-09-06 stsp
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 a440fac0 2018-09-06 stsp #include <zlib.h>
33 a440fac0 2018-09-06 stsp #include <ctype.h>
34 a440fac0 2018-09-06 stsp #include <limits.h>
35 a440fac0 2018-09-06 stsp #include <imsg.h>
36 a440fac0 2018-09-06 stsp #include <time.h>
37 ad242220 2018-09-08 stsp #include <unistd.h>
38 a440fac0 2018-09-06 stsp
39 a440fac0 2018-09-06 stsp #include "got_error.h"
40 a440fac0 2018-09-06 stsp #include "got_object.h"
41 a440fac0 2018-09-06 stsp #include "got_repository.h"
42 a440fac0 2018-09-06 stsp #include "got_opentemp.h"
43 324d37e7 2019-05-11 stsp #include "got_path.h"
44 a440fac0 2018-09-06 stsp
45 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
47 41fa1437 2018-11-05 stsp #include "got_lib_inflate.h"
48 41fa1437 2018-11-05 stsp #include "got_lib_object.h"
49 3022d272 2019-11-14 stsp #include "got_lib_object_parse.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
51 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
52 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
53 a440fac0 2018-09-06 stsp
54 a440fac0 2018-09-06 stsp #ifndef nitems
55 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 a440fac0 2018-09-06 stsp #endif
57 ca6e02ac 2020-01-07 stsp
58 ca6e02ac 2020-01-07 stsp struct got_object_id *
59 ca6e02ac 2020-01-07 stsp got_object_id_dup(struct got_object_id *id1)
60 ca6e02ac 2020-01-07 stsp {
61 ca6e02ac 2020-01-07 stsp struct got_object_id *id2;
62 ca6e02ac 2020-01-07 stsp
63 ca6e02ac 2020-01-07 stsp id2 = malloc(sizeof(*id2));
64 ca6e02ac 2020-01-07 stsp if (id2 == NULL)
65 ca6e02ac 2020-01-07 stsp return NULL;
66 ca6e02ac 2020-01-07 stsp memcpy(id2, id1, sizeof(*id2));
67 ca6e02ac 2020-01-07 stsp return id2;
68 ca6e02ac 2020-01-07 stsp }
69 a440fac0 2018-09-06 stsp
70 f054b67a 2018-11-05 stsp int
71 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
72 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
73 f054b67a 2018-11-05 stsp {
74 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
75 f054b67a 2018-11-05 stsp }
76 f054b67a 2018-11-05 stsp
77 2ff12563 2018-09-15 stsp const struct got_error *
78 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
79 5df4932d 2018-11-05 stsp {
80 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
81 5df4932d 2018-11-05 stsp if (*qid == NULL)
82 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
83 5df4932d 2018-11-05 stsp
84 74a2356f 2021-06-18 stsp (*qid)->data = NULL;
85 5df4932d 2018-11-05 stsp return NULL;
86 5df4932d 2018-11-05 stsp }
87 5df4932d 2018-11-05 stsp
88 5df4932d 2018-11-05 stsp const struct got_error *
89 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
90 2ff12563 2018-09-15 stsp {
91 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
92 2ff12563 2018-09-15 stsp
93 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
94 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
95 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
96 2ff12563 2018-09-15 stsp
97 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
98 2ff12563 2018-09-15 stsp free(*outbuf);
99 2ff12563 2018-09-15 stsp *outbuf = NULL;
100 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
101 2ff12563 2018-09-15 stsp }
102 a440fac0 2018-09-06 stsp
103 2ff12563 2018-09-15 stsp return NULL;
104 2ff12563 2018-09-15 stsp }
105 2ff12563 2018-09-15 stsp
106 03fa71c8 2018-09-06 stsp void
107 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
108 03fa71c8 2018-09-06 stsp {
109 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
110 03fa71c8 2018-09-06 stsp obj->refcnt--;
111 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
112 03fa71c8 2018-09-06 stsp return;
113 03fa71c8 2018-09-06 stsp }
114 03fa71c8 2018-09-06 stsp
115 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
116 03fa71c8 2018-09-06 stsp struct got_delta *delta;
117 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&obj->deltas.entries)) {
118 dbdddfee 2021-06-23 naddy delta = STAILQ_FIRST(&obj->deltas.entries);
119 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
120 2256993b 2019-07-15 stsp free(delta);
121 03fa71c8 2018-09-06 stsp }
122 03fa71c8 2018-09-06 stsp }
123 03fa71c8 2018-09-06 stsp free(obj);
124 03fa71c8 2018-09-06 stsp }
125 03fa71c8 2018-09-06 stsp
126 d3c116bf 2021-10-15 stsp const struct got_error *
127 d3c116bf 2021-10-15 stsp got_object_raw_close(struct got_raw_object *obj)
128 d3c116bf 2021-10-15 stsp {
129 d3c116bf 2021-10-15 stsp const struct got_error *err = NULL;
130 d3c116bf 2021-10-15 stsp
131 d3c116bf 2021-10-15 stsp if (obj->refcnt > 0) {
132 d3c116bf 2021-10-15 stsp obj->refcnt--;
133 d3c116bf 2021-10-15 stsp if (obj->refcnt > 0)
134 d3c116bf 2021-10-15 stsp return NULL;
135 d3c116bf 2021-10-15 stsp }
136 d3c116bf 2021-10-15 stsp
137 13b2bc37 2022-10-23 stsp if (obj->close_cb)
138 13b2bc37 2022-10-23 stsp obj->close_cb(obj);
139 13b2bc37 2022-10-23 stsp
140 64a8571e 2022-01-07 stsp if (obj->f == NULL) {
141 64a8571e 2022-01-07 stsp if (obj->fd != -1) {
142 64a8571e 2022-01-07 stsp if (munmap(obj->data, obj->hdrlen + obj->size) == -1)
143 64a8571e 2022-01-07 stsp err = got_error_from_errno("munmap");
144 64a8571e 2022-01-07 stsp if (close(obj->fd) == -1 && err == NULL)
145 64a8571e 2022-01-07 stsp err = got_error_from_errno("close");
146 64a8571e 2022-01-07 stsp } else
147 64a8571e 2022-01-07 stsp free(obj->data);
148 64a8571e 2022-01-07 stsp } else {
149 64a8571e 2022-01-07 stsp if (fclose(obj->f) == EOF && err == NULL)
150 64a8571e 2022-01-07 stsp err = got_error_from_errno("fclose");
151 64a8571e 2022-01-07 stsp }
152 d3c116bf 2021-10-15 stsp free(obj);
153 d3c116bf 2021-10-15 stsp return err;
154 d3c116bf 2021-10-15 stsp }
155 d3c116bf 2021-10-15 stsp
156 03fa71c8 2018-09-06 stsp void
157 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
158 03fa71c8 2018-09-06 stsp {
159 03fa71c8 2018-09-06 stsp free(qid);
160 1785f84a 2018-12-23 stsp }
161 1785f84a 2018-12-23 stsp
162 dd88155e 2019-06-29 stsp void
163 dd88155e 2019-06-29 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
164 dd88155e 2019-06-29 stsp {
165 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
166 dd88155e 2019-06-29 stsp
167 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(ids)) {
168 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(ids);
169 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(ids, entry);
170 dd88155e 2019-06-29 stsp got_object_qid_free(qid);
171 dd88155e 2019-06-29 stsp }
172 dd88155e 2019-06-29 stsp }
173 dd88155e 2019-06-29 stsp
174 1785f84a 2018-12-23 stsp const struct got_error *
175 1785f84a 2018-12-23 stsp got_object_parse_header(struct got_object **obj, char *buf, size_t len)
176 1785f84a 2018-12-23 stsp {
177 ff2a4428 2019-03-19 stsp const char *obj_labels[] = {
178 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_COMMIT,
179 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TREE,
180 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_BLOB,
181 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TAG,
182 1785f84a 2018-12-23 stsp };
183 1785f84a 2018-12-23 stsp const int obj_types[] = {
184 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_COMMIT,
185 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TREE,
186 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_BLOB,
187 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TAG,
188 1785f84a 2018-12-23 stsp };
189 1785f84a 2018-12-23 stsp int type = 0;
190 c7b17232 2022-01-28 stsp size_t size = 0;
191 16aeacf7 2020-11-26 stsp size_t i;
192 c7b17232 2022-01-28 stsp char *end;
193 1785f84a 2018-12-23 stsp
194 1785f84a 2018-12-23 stsp *obj = NULL;
195 1785f84a 2018-12-23 stsp
196 c7b17232 2022-01-28 stsp end = memchr(buf, '\0', len);
197 c7b17232 2022-01-28 stsp if (end == NULL)
198 9ef4ac16 2019-04-13 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
199 1785f84a 2018-12-23 stsp
200 ff2a4428 2019-03-19 stsp for (i = 0; i < nitems(obj_labels); i++) {
201 ff2a4428 2019-03-19 stsp const char *label = obj_labels[i];
202 ff2a4428 2019-03-19 stsp size_t label_len = strlen(label);
203 1785f84a 2018-12-23 stsp const char *errstr;
204 1785f84a 2018-12-23 stsp
205 c7b17232 2022-01-28 stsp if (len <= label_len || buf + label_len >= end ||
206 c7b17232 2022-01-28 stsp strncmp(buf, label, label_len) != 0)
207 1785f84a 2018-12-23 stsp continue;
208 1785f84a 2018-12-23 stsp
209 1785f84a 2018-12-23 stsp type = obj_types[i];
210 ff2a4428 2019-03-19 stsp size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
211 1785f84a 2018-12-23 stsp if (errstr != NULL)
212 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
213 1785f84a 2018-12-23 stsp break;
214 1785f84a 2018-12-23 stsp }
215 1785f84a 2018-12-23 stsp
216 1785f84a 2018-12-23 stsp if (type == 0)
217 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
218 1785f84a 2018-12-23 stsp
219 1785f84a 2018-12-23 stsp *obj = calloc(1, sizeof(**obj));
220 1785f84a 2018-12-23 stsp if (*obj == NULL)
221 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
222 1785f84a 2018-12-23 stsp (*obj)->type = type;
223 c7b17232 2022-01-28 stsp (*obj)->hdrlen = end - buf + 1;
224 1785f84a 2018-12-23 stsp (*obj)->size = size;
225 1785f84a 2018-12-23 stsp return NULL;
226 1785f84a 2018-12-23 stsp }
227 1785f84a 2018-12-23 stsp
228 1785f84a 2018-12-23 stsp const struct got_error *
229 1785f84a 2018-12-23 stsp got_object_read_header(struct got_object **obj, int fd)
230 1785f84a 2018-12-23 stsp {
231 1785f84a 2018-12-23 stsp const struct got_error *err;
232 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
233 31e61ec1 2021-09-28 naddy uint8_t *buf;
234 1785f84a 2018-12-23 stsp const size_t zbsize = 64;
235 1785f84a 2018-12-23 stsp size_t outlen, totlen;
236 1785f84a 2018-12-23 stsp int nbuf = 1;
237 1785f84a 2018-12-23 stsp
238 1785f84a 2018-12-23 stsp *obj = NULL;
239 1785f84a 2018-12-23 stsp
240 1785f84a 2018-12-23 stsp buf = malloc(zbsize);
241 1785f84a 2018-12-23 stsp if (buf == NULL)
242 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
243 c7b17232 2022-01-28 stsp buf[0] = '\0';
244 1785f84a 2018-12-23 stsp
245 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, buf, zbsize, NULL);
246 1785f84a 2018-12-23 stsp if (err)
247 1785f84a 2018-12-23 stsp return err;
248 1785f84a 2018-12-23 stsp
249 1785f84a 2018-12-23 stsp totlen = 0;
250 1785f84a 2018-12-23 stsp do {
251 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
252 1785f84a 2018-12-23 stsp if (err)
253 1785f84a 2018-12-23 stsp goto done;
254 1785f84a 2018-12-23 stsp if (outlen == 0)
255 1785f84a 2018-12-23 stsp break;
256 1785f84a 2018-12-23 stsp totlen += outlen;
257 dedbbd9d 2019-04-13 stsp if (memchr(zb.outbuf, '\0', outlen) == NULL) {
258 31e61ec1 2021-09-28 naddy uint8_t *newbuf;
259 1785f84a 2018-12-23 stsp nbuf++;
260 1785f84a 2018-12-23 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
261 1785f84a 2018-12-23 stsp if (newbuf == NULL) {
262 638f9024 2019-05-13 stsp err = got_error_from_errno("recallocarray");
263 1785f84a 2018-12-23 stsp goto done;
264 1785f84a 2018-12-23 stsp }
265 1785f84a 2018-12-23 stsp buf = newbuf;
266 1785f84a 2018-12-23 stsp zb.outbuf = newbuf + totlen;
267 1785f84a 2018-12-23 stsp zb.outlen = (nbuf * zbsize) - totlen;
268 1785f84a 2018-12-23 stsp }
269 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
270 1785f84a 2018-12-23 stsp
271 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
272 1785f84a 2018-12-23 stsp done:
273 1785f84a 2018-12-23 stsp free(buf);
274 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
275 1785f84a 2018-12-23 stsp return err;
276 876c234b 2018-09-10 stsp }
277 13b2bc37 2022-10-23 stsp
278 13b2bc37 2022-10-23 stsp const struct got_error *
279 13b2bc37 2022-10-23 stsp got_object_read_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
280 13b2bc37 2022-10-23 stsp size_t max_in_mem_size, int outfd, struct got_object_id *expected_id,
281 13b2bc37 2022-10-23 stsp int infd)
282 13b2bc37 2022-10-23 stsp {
283 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
284 13b2bc37 2022-10-23 stsp struct got_object *obj;
285 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
286 13b2bc37 2022-10-23 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
287 13b2bc37 2022-10-23 stsp SHA1_CTX sha1_ctx;
288 13b2bc37 2022-10-23 stsp size_t len, consumed;
289 13b2bc37 2022-10-23 stsp FILE *f = NULL;
290 13b2bc37 2022-10-23 stsp
291 13b2bc37 2022-10-23 stsp *outbuf = NULL;
292 13b2bc37 2022-10-23 stsp *size = 0;
293 13b2bc37 2022-10-23 stsp *hdrlen = 0;
294 13b2bc37 2022-10-23 stsp
295 13b2bc37 2022-10-23 stsp SHA1Init(&sha1_ctx);
296 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
297 13b2bc37 2022-10-23 stsp csum.output_sha1 = &sha1_ctx;
298 13b2bc37 2022-10-23 stsp
299 13b2bc37 2022-10-23 stsp if (lseek(infd, SEEK_SET, 0) == -1)
300 13b2bc37 2022-10-23 stsp return got_error_from_errno("lseek");
301 13b2bc37 2022-10-23 stsp
302 13b2bc37 2022-10-23 stsp err = got_object_read_header(&obj, infd);
303 13b2bc37 2022-10-23 stsp if (err)
304 13b2bc37 2022-10-23 stsp return err;
305 876c234b 2018-09-10 stsp
306 13b2bc37 2022-10-23 stsp if (lseek(infd, SEEK_SET, 0) == -1)
307 13b2bc37 2022-10-23 stsp return got_error_from_errno("lseek");
308 13b2bc37 2022-10-23 stsp
309 13b2bc37 2022-10-23 stsp if (obj->size + obj->hdrlen <= max_in_mem_size) {
310 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(outbuf, &len, &consumed, &csum,
311 13b2bc37 2022-10-23 stsp obj->size + obj->hdrlen, infd);
312 13b2bc37 2022-10-23 stsp } else {
313 13b2bc37 2022-10-23 stsp int fd;
314 13b2bc37 2022-10-23 stsp /*
315 13b2bc37 2022-10-23 stsp * XXX This uses an extra file descriptor for no good reason.
316 13b2bc37 2022-10-23 stsp * We should have got_inflate_fd_to_fd().
317 13b2bc37 2022-10-23 stsp */
318 13b2bc37 2022-10-23 stsp fd = dup(infd);
319 13b2bc37 2022-10-23 stsp if (fd == -1)
320 13b2bc37 2022-10-23 stsp return got_error_from_errno("dup");
321 13b2bc37 2022-10-23 stsp f = fdopen(fd, "r");
322 13b2bc37 2022-10-23 stsp if (f == NULL) {
323 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
324 13b2bc37 2022-10-23 stsp abort();
325 13b2bc37 2022-10-23 stsp close(fd);
326 13b2bc37 2022-10-23 stsp goto done;
327 13b2bc37 2022-10-23 stsp }
328 13b2bc37 2022-10-23 stsp err = got_inflate_to_fd(&len, f, &csum, outfd);
329 13b2bc37 2022-10-23 stsp }
330 13b2bc37 2022-10-23 stsp if (err)
331 13b2bc37 2022-10-23 stsp goto done;
332 13b2bc37 2022-10-23 stsp
333 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
334 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
335 13b2bc37 2022-10-23 stsp goto done;
336 13b2bc37 2022-10-23 stsp }
337 13b2bc37 2022-10-23 stsp
338 13b2bc37 2022-10-23 stsp SHA1Final(sha1, &sha1_ctx);
339 13b2bc37 2022-10-23 stsp if (memcmp(expected_id->sha1, sha1, SHA1_DIGEST_LENGTH) != 0) {
340 13b2bc37 2022-10-23 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
341 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_OBJ_CSUM,
342 13b2bc37 2022-10-23 stsp "checksum failure for object %s",
343 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_id->sha1, buf,
344 13b2bc37 2022-10-23 stsp sizeof(buf)));
345 13b2bc37 2022-10-23 stsp goto done;
346 13b2bc37 2022-10-23 stsp }
347 13b2bc37 2022-10-23 stsp
348 13b2bc37 2022-10-23 stsp *size = obj->size;
349 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
350 13b2bc37 2022-10-23 stsp done:
351 13b2bc37 2022-10-23 stsp got_object_close(obj);
352 13b2bc37 2022-10-23 stsp if (f && fclose(f) == EOF && err == NULL)
353 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
354 13b2bc37 2022-10-23 stsp return err;
355 13b2bc37 2022-10-23 stsp }
356 13b2bc37 2022-10-23 stsp
357 a440fac0 2018-09-06 stsp struct got_commit_object *
358 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
359 a440fac0 2018-09-06 stsp {
360 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
361 a440fac0 2018-09-06 stsp
362 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
363 a440fac0 2018-09-06 stsp if (commit == NULL)
364 a440fac0 2018-09-06 stsp return NULL;
365 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
366 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
367 a440fac0 2018-09-06 stsp free(commit);
368 a440fac0 2018-09-06 stsp return NULL;
369 a440fac0 2018-09-06 stsp }
370 a440fac0 2018-09-06 stsp
371 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commit->parent_ids);
372 a440fac0 2018-09-06 stsp
373 a440fac0 2018-09-06 stsp return commit;
374 a440fac0 2018-09-06 stsp }
375 a440fac0 2018-09-06 stsp
376 a440fac0 2018-09-06 stsp const struct got_error *
377 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
378 a440fac0 2018-09-06 stsp const char *id_str)
379 a440fac0 2018-09-06 stsp {
380 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
381 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
382 a440fac0 2018-09-06 stsp
383 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
384 5df4932d 2018-11-05 stsp if (err)
385 7762fe12 2018-11-05 stsp return err;
386 a440fac0 2018-09-06 stsp
387 d7b5a0e8 2022-04-20 stsp if (!got_parse_sha1_digest(qid->id.sha1, id_str)) {
388 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
390 a440fac0 2018-09-06 stsp return err;
391 a440fac0 2018-09-06 stsp }
392 a440fac0 2018-09-06 stsp
393 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
394 a440fac0 2018-09-06 stsp commit->nparents++;
395 a440fac0 2018-09-06 stsp
396 a440fac0 2018-09-06 stsp return NULL;
397 a440fac0 2018-09-06 stsp }
398 a440fac0 2018-09-06 stsp
399 a440fac0 2018-09-06 stsp static const struct got_error *
400 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
401 a440fac0 2018-09-06 stsp {
402 a440fac0 2018-09-06 stsp int sign = 1;
403 a440fac0 2018-09-06 stsp const char *p = tzstr;
404 a440fac0 2018-09-06 stsp time_t h, m;
405 a440fac0 2018-09-06 stsp
406 a440fac0 2018-09-06 stsp *gmtoff = 0;
407 a440fac0 2018-09-06 stsp
408 a440fac0 2018-09-06 stsp if (*p == '-')
409 a440fac0 2018-09-06 stsp sign = -1;
410 a440fac0 2018-09-06 stsp else if (*p != '+')
411 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
412 a440fac0 2018-09-06 stsp p++;
413 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
414 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
415 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
416 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
417 a440fac0 2018-09-06 stsp
418 a440fac0 2018-09-06 stsp p += 2;
419 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
420 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
421 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
422 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
423 a440fac0 2018-09-06 stsp
424 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
425 a440fac0 2018-09-06 stsp return NULL;
426 a440fac0 2018-09-06 stsp }
427 a440fac0 2018-09-06 stsp
428 a440fac0 2018-09-06 stsp static const struct got_error *
429 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
430 a440fac0 2018-09-06 stsp {
431 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
432 a440fac0 2018-09-06 stsp const char *errstr;
433 a440fac0 2018-09-06 stsp char *space, *tzstr;
434 a440fac0 2018-09-06 stsp
435 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
436 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
437 a440fac0 2018-09-06 stsp if (space == NULL)
438 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
439 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
440 a440fac0 2018-09-06 stsp if (tzstr == NULL)
441 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
442 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
443 a440fac0 2018-09-06 stsp free(tzstr);
444 5e91dae4 2022-08-30 stsp if (err) {
445 9dbd8627 2021-02-04 stsp if (err->code != GOT_ERR_BAD_OBJ_DATA)
446 9dbd8627 2021-02-04 stsp return err;
447 9dbd8627 2021-02-04 stsp /* Old versions of Git omitted the timestamp. */
448 9dbd8627 2021-02-04 stsp *time = 0;
449 9dbd8627 2021-02-04 stsp *gmtoff = 0;
450 9dbd8627 2021-02-04 stsp return NULL;
451 9dbd8627 2021-02-04 stsp }
452 a440fac0 2018-09-06 stsp *space = '\0';
453 a440fac0 2018-09-06 stsp
454 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
455 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
456 a440fac0 2018-09-06 stsp if (space == NULL)
457 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
458 a440fac0 2018-09-06 stsp
459 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
460 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
461 a440fac0 2018-09-06 stsp if (errstr)
462 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
463 a440fac0 2018-09-06 stsp
464 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
465 a440fac0 2018-09-06 stsp *space = '\0';
466 a440fac0 2018-09-06 stsp
467 a440fac0 2018-09-06 stsp return NULL;
468 a440fac0 2018-09-06 stsp }
469 a440fac0 2018-09-06 stsp
470 03fa71c8 2018-09-06 stsp void
471 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
472 03fa71c8 2018-09-06 stsp {
473 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
474 03fa71c8 2018-09-06 stsp commit->refcnt--;
475 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
476 03fa71c8 2018-09-06 stsp return;
477 03fa71c8 2018-09-06 stsp }
478 03fa71c8 2018-09-06 stsp
479 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
480 03fa71c8 2018-09-06 stsp free(commit->tree_id);
481 03fa71c8 2018-09-06 stsp free(commit->author);
482 03fa71c8 2018-09-06 stsp free(commit->committer);
483 03fa71c8 2018-09-06 stsp free(commit->logmsg);
484 03fa71c8 2018-09-06 stsp free(commit);
485 45d799e2 2018-12-23 stsp }
486 45d799e2 2018-12-23 stsp
487 45d799e2 2018-12-23 stsp struct got_object_id *
488 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
489 45d799e2 2018-12-23 stsp {
490 45d799e2 2018-12-23 stsp return commit->tree_id;
491 45d799e2 2018-12-23 stsp }
492 45d799e2 2018-12-23 stsp
493 45d799e2 2018-12-23 stsp int
494 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
495 45d799e2 2018-12-23 stsp {
496 45d799e2 2018-12-23 stsp return commit->nparents;
497 03fa71c8 2018-09-06 stsp }
498 03fa71c8 2018-09-06 stsp
499 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
500 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
501 45d799e2 2018-12-23 stsp {
502 45d799e2 2018-12-23 stsp return &commit->parent_ids;
503 45d799e2 2018-12-23 stsp }
504 45d799e2 2018-12-23 stsp
505 45d799e2 2018-12-23 stsp const char *
506 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
507 45d799e2 2018-12-23 stsp {
508 45d799e2 2018-12-23 stsp return commit->author;
509 45d799e2 2018-12-23 stsp }
510 45d799e2 2018-12-23 stsp
511 45d799e2 2018-12-23 stsp time_t
512 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
513 45d799e2 2018-12-23 stsp {
514 45d799e2 2018-12-23 stsp return commit->author_time;
515 45d799e2 2018-12-23 stsp }
516 45d799e2 2018-12-23 stsp
517 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
518 45d799e2 2018-12-23 stsp {
519 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
520 45d799e2 2018-12-23 stsp }
521 45d799e2 2018-12-23 stsp
522 45d799e2 2018-12-23 stsp const char *
523 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
524 45d799e2 2018-12-23 stsp {
525 45d799e2 2018-12-23 stsp return commit->committer;
526 45d799e2 2018-12-23 stsp }
527 45d799e2 2018-12-23 stsp
528 45d799e2 2018-12-23 stsp time_t
529 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
530 45d799e2 2018-12-23 stsp {
531 45d799e2 2018-12-23 stsp return commit->committer_time;
532 45d799e2 2018-12-23 stsp }
533 45d799e2 2018-12-23 stsp
534 45d799e2 2018-12-23 stsp time_t
535 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
536 45d799e2 2018-12-23 stsp {
537 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
538 45d799e2 2018-12-23 stsp }
539 5943eee2 2019-08-13 stsp
540 5943eee2 2019-08-13 stsp const struct got_error *
541 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
542 45d799e2 2018-12-23 stsp {
543 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
544 b9c41b54 2021-08-03 stsp const char *src;
545 b9c41b54 2021-08-03 stsp char *dst;
546 5943eee2 2019-08-13 stsp size_t len;
547 5943eee2 2019-08-13 stsp
548 b9c41b54 2021-08-03 stsp len = strlen(commit->logmsg);
549 b9c41b54 2021-08-03 stsp *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
550 b9c41b54 2021-08-03 stsp if (*logmsg == NULL)
551 b9c41b54 2021-08-03 stsp return got_error_from_errno("malloc");
552 5943eee2 2019-08-13 stsp
553 b9c41b54 2021-08-03 stsp /*
554 b9c41b54 2021-08-03 stsp * Strip out unusual headers. Headers are separated from the commit
555 b9c41b54 2021-08-03 stsp * message body by a single empty line.
556 b9c41b54 2021-08-03 stsp */
557 b9c41b54 2021-08-03 stsp src = commit->logmsg;
558 b9c41b54 2021-08-03 stsp dst = *logmsg;
559 b9c41b54 2021-08-03 stsp while (*src != '\0' && *src != '\n') {
560 b9c41b54 2021-08-03 stsp int copy_header = 1, eol = 0;
561 b9c41b54 2021-08-03 stsp if (strncmp(src, GOT_COMMIT_LABEL_TREE,
562 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
563 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
564 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
565 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_PARENT,
566 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
567 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
568 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
569 b9c41b54 2021-08-03 stsp copy_header = 0;
570 13555e04 2019-09-28 semarie
571 b9c41b54 2021-08-03 stsp while (*src != '\0' && !eol) {
572 b9c41b54 2021-08-03 stsp if (copy_header) {
573 b9c41b54 2021-08-03 stsp *dst = *src;
574 b9c41b54 2021-08-03 stsp dst++;
575 b9c41b54 2021-08-03 stsp }
576 b9c41b54 2021-08-03 stsp if (*src == '\n')
577 b9c41b54 2021-08-03 stsp eol = 1;
578 b9c41b54 2021-08-03 stsp src++;
579 5943eee2 2019-08-13 stsp }
580 b9c41b54 2021-08-03 stsp }
581 b9c41b54 2021-08-03 stsp *dst = '\0';
582 13555e04 2019-09-28 semarie
583 b9c41b54 2021-08-03 stsp if (strlcat(*logmsg, src, len + 1) >= len + 1) {
584 b9c41b54 2021-08-03 stsp err = got_error(GOT_ERR_NO_SPACE);
585 b9c41b54 2021-08-03 stsp goto done;
586 ef744db3 2020-08-27 stsp }
587 ef744db3 2020-08-27 stsp
588 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
589 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
590 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
591 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
592 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
593 5943eee2 2019-08-13 stsp len--;
594 5943eee2 2019-08-13 stsp }
595 b9c41b54 2021-08-03 stsp
596 b9c41b54 2021-08-03 stsp /* Append a trailing newline if missing. */
597 b9c41b54 2021-08-03 stsp if (len > 0 && (*logmsg)[len - 1] != '\n') {
598 b9c41b54 2021-08-03 stsp (*logmsg)[len] = '\n';
599 b9c41b54 2021-08-03 stsp (*logmsg)[len + 1] = '\0';
600 b9c41b54 2021-08-03 stsp }
601 5943eee2 2019-08-13 stsp done:
602 5943eee2 2019-08-13 stsp if (err) {
603 5943eee2 2019-08-13 stsp free(*logmsg);
604 5943eee2 2019-08-13 stsp *logmsg = NULL;
605 5943eee2 2019-08-13 stsp }
606 5943eee2 2019-08-13 stsp return err;
607 24ea5512 2019-08-22 stsp }
608 24ea5512 2019-08-22 stsp
609 24ea5512 2019-08-22 stsp const char *
610 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
611 24ea5512 2019-08-22 stsp {
612 24ea5512 2019-08-22 stsp return commit->logmsg;
613 45d799e2 2018-12-23 stsp }
614 45d799e2 2018-12-23 stsp
615 a440fac0 2018-09-06 stsp const struct got_error *
616 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
617 5e0b25c4 2018-12-24 stsp size_t len)
618 a440fac0 2018-09-06 stsp {
619 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
620 a440fac0 2018-09-06 stsp char *s = buf;
621 ff2a4428 2019-03-19 stsp size_t label_len;
622 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
623 4793d91b 2019-09-22 stsp
624 4793d91b 2019-09-22 stsp if (remain == 0)
625 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
626 230a42bd 2019-05-11 jcs
627 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
628 a440fac0 2018-09-06 stsp if (*commit == NULL)
629 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
630 a440fac0 2018-09-06 stsp
631 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
632 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
633 ff2a4428 2019-03-19 stsp remain -= label_len;
634 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
635 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
636 a440fac0 2018-09-06 stsp goto done;
637 a440fac0 2018-09-06 stsp }
638 ff2a4428 2019-03-19 stsp s += label_len;
639 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
640 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 a440fac0 2018-09-06 stsp goto done;
642 a440fac0 2018-09-06 stsp }
643 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
644 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
645 a440fac0 2018-09-06 stsp } else {
646 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
647 a440fac0 2018-09-06 stsp goto done;
648 a440fac0 2018-09-06 stsp }
649 a440fac0 2018-09-06 stsp
650 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
651 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
652 ff2a4428 2019-03-19 stsp remain -= label_len;
653 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
654 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
655 a440fac0 2018-09-06 stsp goto done;
656 a440fac0 2018-09-06 stsp }
657 ff2a4428 2019-03-19 stsp s += label_len;
658 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
659 a440fac0 2018-09-06 stsp if (err)
660 a440fac0 2018-09-06 stsp goto done;
661 a440fac0 2018-09-06 stsp
662 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
663 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
664 a440fac0 2018-09-06 stsp }
665 a440fac0 2018-09-06 stsp
666 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
667 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
668 a440fac0 2018-09-06 stsp char *p;
669 a440fac0 2018-09-06 stsp size_t slen;
670 a440fac0 2018-09-06 stsp
671 ff2a4428 2019-03-19 stsp remain -= label_len;
672 a440fac0 2018-09-06 stsp if (remain <= 0) {
673 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 a440fac0 2018-09-06 stsp goto done;
675 a440fac0 2018-09-06 stsp }
676 ff2a4428 2019-03-19 stsp s += label_len;
677 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
678 a440fac0 2018-09-06 stsp if (p == NULL) {
679 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
680 a440fac0 2018-09-06 stsp goto done;
681 a440fac0 2018-09-06 stsp }
682 a440fac0 2018-09-06 stsp *p = '\0';
683 a440fac0 2018-09-06 stsp slen = strlen(s);
684 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
685 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
686 a440fac0 2018-09-06 stsp if (err)
687 a440fac0 2018-09-06 stsp goto done;
688 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
689 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
691 a440fac0 2018-09-06 stsp goto done;
692 a440fac0 2018-09-06 stsp }
693 a440fac0 2018-09-06 stsp s += slen + 1;
694 a440fac0 2018-09-06 stsp remain -= slen + 1;
695 a440fac0 2018-09-06 stsp }
696 a440fac0 2018-09-06 stsp
697 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
698 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
699 a440fac0 2018-09-06 stsp char *p;
700 a440fac0 2018-09-06 stsp size_t slen;
701 a440fac0 2018-09-06 stsp
702 ff2a4428 2019-03-19 stsp remain -= label_len;
703 a440fac0 2018-09-06 stsp if (remain <= 0) {
704 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
705 a440fac0 2018-09-06 stsp goto done;
706 a440fac0 2018-09-06 stsp }
707 ff2a4428 2019-03-19 stsp s += label_len;
708 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
709 a440fac0 2018-09-06 stsp if (p == NULL) {
710 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
711 a440fac0 2018-09-06 stsp goto done;
712 a440fac0 2018-09-06 stsp }
713 a440fac0 2018-09-06 stsp *p = '\0';
714 a440fac0 2018-09-06 stsp slen = strlen(s);
715 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
716 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
717 a440fac0 2018-09-06 stsp if (err)
718 a440fac0 2018-09-06 stsp goto done;
719 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
720 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
721 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
722 a440fac0 2018-09-06 stsp goto done;
723 a440fac0 2018-09-06 stsp }
724 a440fac0 2018-09-06 stsp s += slen + 1;
725 a440fac0 2018-09-06 stsp remain -= slen + 1;
726 a440fac0 2018-09-06 stsp }
727 a440fac0 2018-09-06 stsp
728 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
729 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
730 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
731 a440fac0 2018-09-06 stsp goto done;
732 a440fac0 2018-09-06 stsp }
733 a440fac0 2018-09-06 stsp done:
734 a440fac0 2018-09-06 stsp if (err) {
735 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
736 a440fac0 2018-09-06 stsp *commit = NULL;
737 a440fac0 2018-09-06 stsp }
738 a440fac0 2018-09-06 stsp return err;
739 ed175427 2019-05-09 stsp }
740 13b2bc37 2022-10-23 stsp
741 13b2bc37 2022-10-23 stsp const struct got_error *
742 13b2bc37 2022-10-23 stsp got_object_read_commit(struct got_commit_object **commit, int fd,
743 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
744 13b2bc37 2022-10-23 stsp {
745 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
746 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
747 13b2bc37 2022-10-23 stsp size_t len;
748 13b2bc37 2022-10-23 stsp uint8_t *p;
749 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
750 13b2bc37 2022-10-23 stsp SHA1_CTX sha1_ctx;
751 13b2bc37 2022-10-23 stsp struct got_object_id id;
752 ed175427 2019-05-09 stsp
753 13b2bc37 2022-10-23 stsp SHA1Init(&sha1_ctx);
754 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
755 13b2bc37 2022-10-23 stsp csum.output_sha1 = &sha1_ctx;
756 13b2bc37 2022-10-23 stsp
757 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
758 13b2bc37 2022-10-23 stsp if (err)
759 13b2bc37 2022-10-23 stsp return err;
760 13b2bc37 2022-10-23 stsp
761 13b2bc37 2022-10-23 stsp SHA1Final(id.sha1, &sha1_ctx);
762 13b2bc37 2022-10-23 stsp if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
763 13b2bc37 2022-10-23 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
764 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_OBJ_CSUM,
765 13b2bc37 2022-10-23 stsp "checksum failure for object %s",
766 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_id->sha1, buf,
767 13b2bc37 2022-10-23 stsp sizeof(buf)));
768 13b2bc37 2022-10-23 stsp goto done;
769 13b2bc37 2022-10-23 stsp }
770 13b2bc37 2022-10-23 stsp
771 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
772 13b2bc37 2022-10-23 stsp if (err)
773 13b2bc37 2022-10-23 stsp goto done;
774 13b2bc37 2022-10-23 stsp
775 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
776 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
777 13b2bc37 2022-10-23 stsp goto done;
778 13b2bc37 2022-10-23 stsp }
779 13b2bc37 2022-10-23 stsp
780 13b2bc37 2022-10-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
781 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
782 13b2bc37 2022-10-23 stsp goto done;
783 13b2bc37 2022-10-23 stsp }
784 13b2bc37 2022-10-23 stsp
785 13b2bc37 2022-10-23 stsp /* Skip object header. */
786 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
787 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, p + obj->hdrlen, len);
788 13b2bc37 2022-10-23 stsp done:
789 13b2bc37 2022-10-23 stsp free(p);
790 13b2bc37 2022-10-23 stsp if (obj)
791 13b2bc37 2022-10-23 stsp got_object_close(obj);
792 13b2bc37 2022-10-23 stsp return err;
793 13b2bc37 2022-10-23 stsp }
794 13b2bc37 2022-10-23 stsp
795 ed175427 2019-05-09 stsp void
796 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
797 ed175427 2019-05-09 stsp {
798 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
799 03fa71c8 2018-09-06 stsp tree->refcnt--;
800 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
801 03fa71c8 2018-09-06 stsp return;
802 03fa71c8 2018-09-06 stsp }
803 03fa71c8 2018-09-06 stsp
804 56e0773d 2019-11-28 stsp free(tree->entries);
805 03fa71c8 2018-09-06 stsp free(tree);
806 03fa71c8 2018-09-06 stsp }
807 03fa71c8 2018-09-06 stsp
808 a440fac0 2018-09-06 stsp static const struct got_error *
809 9985f404 2022-05-19 stsp parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen, char *buf,
810 a440fac0 2018-09-06 stsp size_t maxlen)
811 a440fac0 2018-09-06 stsp {
812 8914529d 2019-04-13 stsp char *p, *space;
813 a0de39f3 2019-08-09 stsp
814 a0de39f3 2019-08-09 stsp *elen = 0;
815 a440fac0 2018-09-06 stsp
816 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
817 9985f404 2022-05-19 stsp if (*elen > maxlen)
818 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
819 a440fac0 2018-09-06 stsp
820 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
821 9985f404 2022-05-19 stsp if (space == NULL || space <= buf)
822 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
823 9985f404 2022-05-19 stsp
824 9985f404 2022-05-19 stsp pte->mode = 0;
825 8914529d 2019-04-13 stsp p = buf;
826 8914529d 2019-04-13 stsp while (p < space) {
827 00d10bca 2023-01-19 mark if (*p < '0' || *p > '7')
828 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
829 9985f404 2022-05-19 stsp pte->mode <<= 3;
830 9985f404 2022-05-19 stsp pte->mode |= *p - '0';
831 a440fac0 2018-09-06 stsp p++;
832 a440fac0 2018-09-06 stsp }
833 a440fac0 2018-09-06 stsp
834 9985f404 2022-05-19 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
835 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
836 9985f404 2022-05-19 stsp
837 9985f404 2022-05-19 stsp pte->name = space + 1;
838 9985f404 2022-05-19 stsp pte->namelen = strlen(pte->name);
839 68bf1b1e 2018-11-07 stsp buf += *elen;
840 9985f404 2022-05-19 stsp pte->id = buf;
841 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
842 9985f404 2022-05-19 stsp return NULL;
843 a440fac0 2018-09-06 stsp }
844 a440fac0 2018-09-06 stsp
845 9985f404 2022-05-19 stsp static int
846 9985f404 2022-05-19 stsp pte_cmp(const void *pa, const void *pb)
847 9985f404 2022-05-19 stsp {
848 9985f404 2022-05-19 stsp const struct got_parsed_tree_entry *a = pa, *b = pb;
849 9985f404 2022-05-19 stsp
850 9985f404 2022-05-19 stsp return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
851 9985f404 2022-05-19 stsp }
852 9985f404 2022-05-19 stsp
853 a440fac0 2018-09-06 stsp const struct got_error *
854 d294b1dc 2022-10-18 stsp got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
855 d294b1dc 2022-10-18 stsp size_t *nentries_alloc, uint8_t *buf, size_t len)
856 a440fac0 2018-09-06 stsp {
857 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
858 d294b1dc 2022-10-18 stsp size_t remain = len;
859 9985f404 2022-05-19 stsp const size_t nalloc = 16;
860 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *pte;
861 9985f404 2022-05-19 stsp int i;
862 f5d3d7af 2019-02-05 stsp
863 3022d272 2019-11-14 stsp *nentries = 0;
864 db1d3576 2019-10-04 stsp if (remain == 0)
865 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
866 db1d3576 2019-10-04 stsp
867 a440fac0 2018-09-06 stsp while (remain > 0) {
868 a440fac0 2018-09-06 stsp size_t elen;
869 a440fac0 2018-09-06 stsp
870 d294b1dc 2022-10-18 stsp if (*nentries >= *nentries_alloc) {
871 d294b1dc 2022-10-18 stsp pte = recallocarray(*entries, *nentries_alloc,
872 d294b1dc 2022-10-18 stsp *nentries_alloc + nalloc, sizeof(**entries));
873 9985f404 2022-05-19 stsp if (pte == NULL) {
874 9985f404 2022-05-19 stsp err = got_error_from_errno("recallocarray");
875 9985f404 2022-05-19 stsp goto done;
876 9985f404 2022-05-19 stsp }
877 9985f404 2022-05-19 stsp *entries = pte;
878 d294b1dc 2022-10-18 stsp *nentries_alloc += nalloc;
879 9985f404 2022-05-19 stsp }
880 9985f404 2022-05-19 stsp
881 9985f404 2022-05-19 stsp pte = &(*entries)[*nentries];
882 9985f404 2022-05-19 stsp err = parse_tree_entry(pte, &elen, buf, remain);
883 a440fac0 2018-09-06 stsp if (err)
884 f5d3d7af 2019-02-05 stsp goto done;
885 a440fac0 2018-09-06 stsp buf += elen;
886 a440fac0 2018-09-06 stsp remain -= elen;
887 3022d272 2019-11-14 stsp (*nentries)++;
888 a440fac0 2018-09-06 stsp }
889 a440fac0 2018-09-06 stsp
890 a440fac0 2018-09-06 stsp if (remain != 0) {
891 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
892 f5d3d7af 2019-02-05 stsp goto done;
893 a440fac0 2018-09-06 stsp }
894 9985f404 2022-05-19 stsp
895 9985f404 2022-05-19 stsp if (*nentries > 1) {
896 9985f404 2022-05-19 stsp mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
897 9985f404 2022-05-19 stsp
898 9985f404 2022-05-19 stsp for (i = 0; i < *nentries - 1; i++) {
899 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *prev = &(*entries)[i];
900 9985f404 2022-05-19 stsp pte = &(*entries)[i + 1];
901 9985f404 2022-05-19 stsp if (got_path_cmp(prev->name, pte->name,
902 9985f404 2022-05-19 stsp prev->namelen, pte->namelen) == 0) {
903 9985f404 2022-05-19 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
904 9985f404 2022-05-19 stsp break;
905 9985f404 2022-05-19 stsp }
906 9985f404 2022-05-19 stsp }
907 9985f404 2022-05-19 stsp }
908 3022d272 2019-11-14 stsp done:
909 d294b1dc 2022-10-18 stsp if (err)
910 3022d272 2019-11-14 stsp *nentries = 0;
911 f5d3d7af 2019-02-05 stsp return err;
912 b64b1f95 2020-01-06 stsp }
913 b64b1f95 2020-01-06 stsp
914 13b2bc37 2022-10-23 stsp const struct got_error *
915 13b2bc37 2022-10-23 stsp got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
916 13b2bc37 2022-10-23 stsp size_t *nentries_alloc, uint8_t **p, int fd,
917 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id)
918 13b2bc37 2022-10-23 stsp {
919 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
920 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
921 13b2bc37 2022-10-23 stsp size_t len;
922 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
923 13b2bc37 2022-10-23 stsp SHA1_CTX sha1_ctx;
924 13b2bc37 2022-10-23 stsp struct got_object_id id;
925 13b2bc37 2022-10-23 stsp
926 13b2bc37 2022-10-23 stsp SHA1Init(&sha1_ctx);
927 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
928 13b2bc37 2022-10-23 stsp csum.output_sha1 = &sha1_ctx;
929 13b2bc37 2022-10-23 stsp
930 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
931 13b2bc37 2022-10-23 stsp if (err)
932 13b2bc37 2022-10-23 stsp return err;
933 13b2bc37 2022-10-23 stsp
934 13b2bc37 2022-10-23 stsp SHA1Final(id.sha1, &sha1_ctx);
935 13b2bc37 2022-10-23 stsp if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
936 13b2bc37 2022-10-23 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
937 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_OBJ_CSUM,
938 13b2bc37 2022-10-23 stsp "checksum failure for object %s",
939 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_id->sha1, buf,
940 13b2bc37 2022-10-23 stsp sizeof(buf)));
941 13b2bc37 2022-10-23 stsp goto done;
942 13b2bc37 2022-10-23 stsp }
943 13b2bc37 2022-10-23 stsp
944 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, *p, len);
945 13b2bc37 2022-10-23 stsp if (err)
946 13b2bc37 2022-10-23 stsp goto done;
947 13b2bc37 2022-10-23 stsp
948 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
949 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
950 13b2bc37 2022-10-23 stsp goto done;
951 13b2bc37 2022-10-23 stsp }
952 13b2bc37 2022-10-23 stsp
953 13b2bc37 2022-10-23 stsp /* Skip object header. */
954 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
955 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(entries, nentries, nentries_alloc,
956 13b2bc37 2022-10-23 stsp *p + obj->hdrlen, len);
957 13b2bc37 2022-10-23 stsp done:
958 13b2bc37 2022-10-23 stsp if (obj)
959 13b2bc37 2022-10-23 stsp got_object_close(obj);
960 13b2bc37 2022-10-23 stsp return err;
961 13b2bc37 2022-10-23 stsp }
962 13b2bc37 2022-10-23 stsp
963 b64b1f95 2020-01-06 stsp void
964 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
965 f4a881ce 2018-11-17 stsp {
966 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
967 ca0d469c 2019-08-13 stsp tag->refcnt--;
968 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
969 ca0d469c 2019-08-13 stsp return;
970 ca0d469c 2019-08-13 stsp }
971 ca0d469c 2019-08-13 stsp
972 f4a881ce 2018-11-17 stsp free(tag->tag);
973 f4a881ce 2018-11-17 stsp free(tag->tagger);
974 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
975 f4a881ce 2018-11-17 stsp free(tag);
976 f4a881ce 2018-11-17 stsp }
977 f4a881ce 2018-11-17 stsp
978 ad242220 2018-09-08 stsp const struct got_error *
979 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
980 f4a881ce 2018-11-17 stsp {
981 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
982 f4a881ce 2018-11-17 stsp size_t remain = len;
983 f4a881ce 2018-11-17 stsp char *s = buf;
984 ff2a4428 2019-03-19 stsp size_t label_len;
985 4793d91b 2019-09-22 stsp
986 4793d91b 2019-09-22 stsp if (remain == 0)
987 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
988 f4a881ce 2018-11-17 stsp
989 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
990 f4a881ce 2018-11-17 stsp if (*tag == NULL)
991 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
992 f4a881ce 2018-11-17 stsp
993 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
994 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
995 ff2a4428 2019-03-19 stsp remain -= label_len;
996 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
997 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
998 f4a881ce 2018-11-17 stsp goto done;
999 f4a881ce 2018-11-17 stsp }
1000 ff2a4428 2019-03-19 stsp s += label_len;
1001 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
1002 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1003 f4a881ce 2018-11-17 stsp goto done;
1004 f4a881ce 2018-11-17 stsp }
1005 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
1006 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
1007 f4a881ce 2018-11-17 stsp } else {
1008 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1009 f4a881ce 2018-11-17 stsp goto done;
1010 f4a881ce 2018-11-17 stsp }
1011 f4a881ce 2018-11-17 stsp
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;
1015 f4a881ce 2018-11-17 stsp }
1016 f4a881ce 2018-11-17 stsp
1017 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
1018 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
1019 ff2a4428 2019-03-19 stsp remain -= label_len;
1020 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1021 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1022 f4a881ce 2018-11-17 stsp goto done;
1023 f4a881ce 2018-11-17 stsp }
1024 ff2a4428 2019-03-19 stsp s += label_len;
1025 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
1026 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
1027 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
1028 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
1029 ff2a4428 2019-03-19 stsp s += label_len;
1030 ff2a4428 2019-03-19 stsp remain -= label_len;
1031 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
1032 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
1033 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
1034 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
1035 ff2a4428 2019-03-19 stsp s += label_len;
1036 ff2a4428 2019-03-19 stsp remain -= label_len;
1037 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
1038 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
1039 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
1040 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
1041 ff2a4428 2019-03-19 stsp s += label_len;
1042 ff2a4428 2019-03-19 stsp remain -= label_len;
1043 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
1044 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
1045 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
1046 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
1047 ff2a4428 2019-03-19 stsp s += label_len;
1048 ff2a4428 2019-03-19 stsp remain -= label_len;
1049 f4a881ce 2018-11-17 stsp } else {
1050 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1051 f4a881ce 2018-11-17 stsp goto done;
1052 f4a881ce 2018-11-17 stsp }
1053 f4a881ce 2018-11-17 stsp
1054 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
1055 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1056 f4a881ce 2018-11-17 stsp goto done;
1057 f4a881ce 2018-11-17 stsp }
1058 f4a881ce 2018-11-17 stsp s++;
1059 f4a881ce 2018-11-17 stsp remain--;
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;
1063 f4a881ce 2018-11-17 stsp }
1064 f4a881ce 2018-11-17 stsp } else {
1065 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1066 f4a881ce 2018-11-17 stsp goto done;
1067 f4a881ce 2018-11-17 stsp }
1068 f4a881ce 2018-11-17 stsp
1069 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
1070 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1071 f4a881ce 2018-11-17 stsp char *p;
1072 f4a881ce 2018-11-17 stsp size_t slen;
1073 ff2a4428 2019-03-19 stsp remain -= label_len;
1074 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1075 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1076 f4a881ce 2018-11-17 stsp goto done;
1077 f4a881ce 2018-11-17 stsp }
1078 ff2a4428 2019-03-19 stsp s += label_len;
1079 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1080 f4a881ce 2018-11-17 stsp if (p == NULL) {
1081 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1082 f4a881ce 2018-11-17 stsp goto done;
1083 f4a881ce 2018-11-17 stsp }
1084 f4a881ce 2018-11-17 stsp *p = '\0';
1085 f4a881ce 2018-11-17 stsp slen = strlen(s);
1086 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
1087 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1088 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1089 f4a881ce 2018-11-17 stsp goto done;
1090 f4a881ce 2018-11-17 stsp }
1091 f4a881ce 2018-11-17 stsp s += slen + 1;
1092 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1093 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1094 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1095 f4a881ce 2018-11-17 stsp goto done;
1096 f4a881ce 2018-11-17 stsp }
1097 f4a881ce 2018-11-17 stsp } else {
1098 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1099 f4a881ce 2018-11-17 stsp goto done;
1100 f4a881ce 2018-11-17 stsp }
1101 f4a881ce 2018-11-17 stsp
1102 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
1103 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1104 f4a881ce 2018-11-17 stsp char *p;
1105 f4a881ce 2018-11-17 stsp size_t slen;
1106 f4a881ce 2018-11-17 stsp
1107 ff2a4428 2019-03-19 stsp remain -= label_len;
1108 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1109 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1110 f4a881ce 2018-11-17 stsp goto done;
1111 f4a881ce 2018-11-17 stsp }
1112 ff2a4428 2019-03-19 stsp s += label_len;
1113 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1114 f4a881ce 2018-11-17 stsp if (p == NULL) {
1115 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1116 f4a881ce 2018-11-17 stsp goto done;
1117 f4a881ce 2018-11-17 stsp }
1118 f4a881ce 2018-11-17 stsp *p = '\0';
1119 f4a881ce 2018-11-17 stsp slen = strlen(s);
1120 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
1121 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
1122 f4a881ce 2018-11-17 stsp if (err)
1123 f4a881ce 2018-11-17 stsp goto done;
1124 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
1125 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1126 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1127 f4a881ce 2018-11-17 stsp goto done;
1128 f4a881ce 2018-11-17 stsp }
1129 f4a881ce 2018-11-17 stsp s += slen + 1;
1130 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1131 5a8b373c 2020-12-18 stsp if (remain < 0) {
1132 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1133 f4a881ce 2018-11-17 stsp goto done;
1134 f4a881ce 2018-11-17 stsp }
1135 f4a881ce 2018-11-17 stsp } else {
1136 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
1137 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
1138 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
1139 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1140 e0e55b50 2019-02-01 stsp goto done;
1141 e0e55b50 2019-02-01 stsp }
1142 f4a881ce 2018-11-17 stsp }
1143 f4a881ce 2018-11-17 stsp
1144 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
1145 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1146 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1147 f4a881ce 2018-11-17 stsp goto done;
1148 f4a881ce 2018-11-17 stsp }
1149 f4a881ce 2018-11-17 stsp done:
1150 f4a881ce 2018-11-17 stsp if (err) {
1151 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
1152 f4a881ce 2018-11-17 stsp *tag = NULL;
1153 13b2bc37 2022-10-23 stsp }
1154 13b2bc37 2022-10-23 stsp return err;
1155 13b2bc37 2022-10-23 stsp }
1156 13b2bc37 2022-10-23 stsp
1157 13b2bc37 2022-10-23 stsp const struct got_error *
1158 758dc042 2022-11-06 stsp got_object_read_tag(struct got_tag_object **tag, int fd,
1159 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
1160 13b2bc37 2022-10-23 stsp {
1161 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1162 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
1163 13b2bc37 2022-10-23 stsp size_t len;
1164 13b2bc37 2022-10-23 stsp uint8_t *p;
1165 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
1166 13b2bc37 2022-10-23 stsp SHA1_CTX sha1_ctx;
1167 13b2bc37 2022-10-23 stsp struct got_object_id id;
1168 13b2bc37 2022-10-23 stsp
1169 13b2bc37 2022-10-23 stsp SHA1Init(&sha1_ctx);
1170 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
1171 13b2bc37 2022-10-23 stsp csum.output_sha1 = &sha1_ctx;
1172 13b2bc37 2022-10-23 stsp
1173 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1174 13b2bc37 2022-10-23 stsp expected_size, fd);
1175 13b2bc37 2022-10-23 stsp if (err)
1176 13b2bc37 2022-10-23 stsp return err;
1177 13b2bc37 2022-10-23 stsp
1178 13b2bc37 2022-10-23 stsp SHA1Final(id.sha1, &sha1_ctx);
1179 13b2bc37 2022-10-23 stsp if (memcmp(expected_id->sha1, id.sha1, SHA1_DIGEST_LENGTH) != 0) {
1180 13b2bc37 2022-10-23 stsp char buf[SHA1_DIGEST_STRING_LENGTH];
1181 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_OBJ_CSUM,
1182 13b2bc37 2022-10-23 stsp "checksum failure for object %s",
1183 13b2bc37 2022-10-23 stsp got_sha1_digest_to_str(expected_id->sha1, buf,
1184 13b2bc37 2022-10-23 stsp sizeof(buf)));
1185 13b2bc37 2022-10-23 stsp goto done;
1186 f4a881ce 2018-11-17 stsp }
1187 13b2bc37 2022-10-23 stsp
1188 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
1189 13b2bc37 2022-10-23 stsp if (err)
1190 13b2bc37 2022-10-23 stsp goto done;
1191 13b2bc37 2022-10-23 stsp
1192 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
1193 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1194 13b2bc37 2022-10-23 stsp goto done;
1195 13b2bc37 2022-10-23 stsp }
1196 13b2bc37 2022-10-23 stsp
1197 13b2bc37 2022-10-23 stsp /* Skip object header. */
1198 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
1199 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1200 13b2bc37 2022-10-23 stsp done:
1201 13b2bc37 2022-10-23 stsp free(p);
1202 13b2bc37 2022-10-23 stsp if (obj)
1203 13b2bc37 2022-10-23 stsp got_object_close(obj);
1204 f4a881ce 2018-11-17 stsp return err;
1205 f4a881ce 2018-11-17 stsp }
1206 f4a881ce 2018-11-17 stsp
1207 f4a881ce 2018-11-17 stsp const struct got_error *
1208 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
1209 a440fac0 2018-09-06 stsp {
1210 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
1211 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
1212 a440fac0 2018-09-06 stsp size_t n, total, remain;
1213 a440fac0 2018-09-06 stsp uint8_t *buf;
1214 a440fac0 2018-09-06 stsp
1215 a440fac0 2018-09-06 stsp *outbuf = NULL;
1216 a440fac0 2018-09-06 stsp *outlen = 0;
1217 a440fac0 2018-09-06 stsp
1218 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
1219 a440fac0 2018-09-06 stsp if (buf == NULL)
1220 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1221 a440fac0 2018-09-06 stsp
1222 a440fac0 2018-09-06 stsp remain = blocksize;
1223 a440fac0 2018-09-06 stsp total = 0;
1224 656b1f76 2019-05-11 jcs for (;;) {
1225 a440fac0 2018-09-06 stsp if (remain == 0) {
1226 a440fac0 2018-09-06 stsp uint8_t *newbuf;
1227 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
1228 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
1229 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
1230 a440fac0 2018-09-06 stsp goto done;
1231 a440fac0 2018-09-06 stsp }
1232 a440fac0 2018-09-06 stsp buf = newbuf;
1233 a440fac0 2018-09-06 stsp remain += blocksize;
1234 a440fac0 2018-09-06 stsp }
1235 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
1236 a440fac0 2018-09-06 stsp if (n == 0) {
1237 a440fac0 2018-09-06 stsp if (ferror(f)) {
1238 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
1239 a440fac0 2018-09-06 stsp goto done;
1240 a440fac0 2018-09-06 stsp }
1241 a440fac0 2018-09-06 stsp break; /* EOF */
1242 a440fac0 2018-09-06 stsp }
1243 a440fac0 2018-09-06 stsp remain -= n;
1244 a440fac0 2018-09-06 stsp total += n;
1245 a440fac0 2018-09-06 stsp };
1246 a440fac0 2018-09-06 stsp
1247 a440fac0 2018-09-06 stsp done:
1248 a440fac0 2018-09-06 stsp if (err == NULL) {
1249 a440fac0 2018-09-06 stsp *outbuf = buf;
1250 a440fac0 2018-09-06 stsp *outlen = total;
1251 a440fac0 2018-09-06 stsp } else
1252 a440fac0 2018-09-06 stsp free(buf);
1253 ad242220 2018-09-08 stsp return err;
1254 a440fac0 2018-09-06 stsp }