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