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