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