Blame


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