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