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