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