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