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 abort();
288 13b2bc37 2022-10-23 stsp close(fd);
289 13b2bc37 2022-10-23 stsp goto done;
290 13b2bc37 2022-10-23 stsp }
291 13b2bc37 2022-10-23 stsp err = got_inflate_to_fd(&len, f, &csum, outfd);
292 13b2bc37 2022-10-23 stsp }
293 13b2bc37 2022-10-23 stsp if (err)
294 13b2bc37 2022-10-23 stsp goto done;
295 13b2bc37 2022-10-23 stsp
296 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
297 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
298 13b2bc37 2022-10-23 stsp goto done;
299 13b2bc37 2022-10-23 stsp }
300 13b2bc37 2022-10-23 stsp
301 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
302 ae25a666 2023-02-23 op if (got_object_id_cmp(expected_id, &id) != 0) {
303 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
304 13b2bc37 2022-10-23 stsp goto done;
305 13b2bc37 2022-10-23 stsp }
306 13b2bc37 2022-10-23 stsp
307 13b2bc37 2022-10-23 stsp *size = obj->size;
308 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
309 13b2bc37 2022-10-23 stsp done:
310 13b2bc37 2022-10-23 stsp got_object_close(obj);
311 13b2bc37 2022-10-23 stsp if (f && fclose(f) == EOF && err == NULL)
312 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
313 13b2bc37 2022-10-23 stsp return err;
314 13b2bc37 2022-10-23 stsp }
315 13b2bc37 2022-10-23 stsp
316 a440fac0 2018-09-06 stsp struct got_commit_object *
317 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
318 a440fac0 2018-09-06 stsp {
319 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
320 a440fac0 2018-09-06 stsp
321 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
322 a440fac0 2018-09-06 stsp if (commit == NULL)
323 a440fac0 2018-09-06 stsp return NULL;
324 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
325 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
326 a440fac0 2018-09-06 stsp free(commit);
327 a440fac0 2018-09-06 stsp return NULL;
328 a440fac0 2018-09-06 stsp }
329 a440fac0 2018-09-06 stsp
330 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commit->parent_ids);
331 a440fac0 2018-09-06 stsp
332 a440fac0 2018-09-06 stsp return commit;
333 a440fac0 2018-09-06 stsp }
334 a440fac0 2018-09-06 stsp
335 a440fac0 2018-09-06 stsp const struct got_error *
336 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
337 a440fac0 2018-09-06 stsp const char *id_str)
338 a440fac0 2018-09-06 stsp {
339 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
340 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
341 a440fac0 2018-09-06 stsp
342 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
343 5df4932d 2018-11-05 stsp if (err)
344 7762fe12 2018-11-05 stsp return err;
345 a440fac0 2018-09-06 stsp
346 87a3ab84 2023-02-23 op if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
347 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
348 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
349 a440fac0 2018-09-06 stsp return err;
350 a440fac0 2018-09-06 stsp }
351 a440fac0 2018-09-06 stsp
352 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
353 a440fac0 2018-09-06 stsp commit->nparents++;
354 a440fac0 2018-09-06 stsp
355 a440fac0 2018-09-06 stsp return NULL;
356 a440fac0 2018-09-06 stsp }
357 a440fac0 2018-09-06 stsp
358 a440fac0 2018-09-06 stsp static const struct got_error *
359 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
360 a440fac0 2018-09-06 stsp {
361 a440fac0 2018-09-06 stsp int sign = 1;
362 a440fac0 2018-09-06 stsp const char *p = tzstr;
363 a440fac0 2018-09-06 stsp time_t h, m;
364 a440fac0 2018-09-06 stsp
365 a440fac0 2018-09-06 stsp *gmtoff = 0;
366 a440fac0 2018-09-06 stsp
367 a440fac0 2018-09-06 stsp if (*p == '-')
368 a440fac0 2018-09-06 stsp sign = -1;
369 a440fac0 2018-09-06 stsp else if (*p != '+')
370 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
371 a440fac0 2018-09-06 stsp p++;
372 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
373 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
374 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
375 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
376 a440fac0 2018-09-06 stsp
377 a440fac0 2018-09-06 stsp p += 2;
378 99fd9ff4 2022-11-17 op if (!isdigit((unsigned char)*p) &&
379 99fd9ff4 2022-11-17 op !isdigit((unsigned char)*(p + 1)))
380 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
381 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
382 a440fac0 2018-09-06 stsp
383 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
384 a440fac0 2018-09-06 stsp return NULL;
385 a440fac0 2018-09-06 stsp }
386 a440fac0 2018-09-06 stsp
387 a440fac0 2018-09-06 stsp static const struct got_error *
388 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
389 a440fac0 2018-09-06 stsp {
390 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
391 a440fac0 2018-09-06 stsp const char *errstr;
392 a440fac0 2018-09-06 stsp char *space, *tzstr;
393 a440fac0 2018-09-06 stsp
394 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
395 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
396 a440fac0 2018-09-06 stsp if (space == NULL)
397 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
398 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
399 a440fac0 2018-09-06 stsp if (tzstr == NULL)
400 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
401 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
402 a440fac0 2018-09-06 stsp free(tzstr);
403 5e91dae4 2022-08-30 stsp if (err) {
404 9dbd8627 2021-02-04 stsp if (err->code != GOT_ERR_BAD_OBJ_DATA)
405 9dbd8627 2021-02-04 stsp return err;
406 9dbd8627 2021-02-04 stsp /* Old versions of Git omitted the timestamp. */
407 9dbd8627 2021-02-04 stsp *time = 0;
408 9dbd8627 2021-02-04 stsp *gmtoff = 0;
409 9dbd8627 2021-02-04 stsp return NULL;
410 9dbd8627 2021-02-04 stsp }
411 a440fac0 2018-09-06 stsp *space = '\0';
412 a440fac0 2018-09-06 stsp
413 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
414 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
415 a440fac0 2018-09-06 stsp if (space == NULL)
416 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
417 a440fac0 2018-09-06 stsp
418 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
419 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
420 a440fac0 2018-09-06 stsp if (errstr)
421 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
422 a440fac0 2018-09-06 stsp
423 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
424 a440fac0 2018-09-06 stsp *space = '\0';
425 a440fac0 2018-09-06 stsp
426 a440fac0 2018-09-06 stsp return NULL;
427 a440fac0 2018-09-06 stsp }
428 a440fac0 2018-09-06 stsp
429 03fa71c8 2018-09-06 stsp void
430 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
431 03fa71c8 2018-09-06 stsp {
432 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
433 03fa71c8 2018-09-06 stsp commit->refcnt--;
434 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
435 03fa71c8 2018-09-06 stsp return;
436 03fa71c8 2018-09-06 stsp }
437 03fa71c8 2018-09-06 stsp
438 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
439 03fa71c8 2018-09-06 stsp free(commit->tree_id);
440 03fa71c8 2018-09-06 stsp free(commit->author);
441 03fa71c8 2018-09-06 stsp free(commit->committer);
442 03fa71c8 2018-09-06 stsp free(commit->logmsg);
443 03fa71c8 2018-09-06 stsp free(commit);
444 45d799e2 2018-12-23 stsp }
445 45d799e2 2018-12-23 stsp
446 45d799e2 2018-12-23 stsp struct got_object_id *
447 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
448 45d799e2 2018-12-23 stsp {
449 45d799e2 2018-12-23 stsp return commit->tree_id;
450 45d799e2 2018-12-23 stsp }
451 45d799e2 2018-12-23 stsp
452 45d799e2 2018-12-23 stsp int
453 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
454 45d799e2 2018-12-23 stsp {
455 45d799e2 2018-12-23 stsp return commit->nparents;
456 03fa71c8 2018-09-06 stsp }
457 03fa71c8 2018-09-06 stsp
458 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
459 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
460 45d799e2 2018-12-23 stsp {
461 45d799e2 2018-12-23 stsp return &commit->parent_ids;
462 45d799e2 2018-12-23 stsp }
463 45d799e2 2018-12-23 stsp
464 45d799e2 2018-12-23 stsp const char *
465 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
466 45d799e2 2018-12-23 stsp {
467 45d799e2 2018-12-23 stsp return commit->author;
468 45d799e2 2018-12-23 stsp }
469 45d799e2 2018-12-23 stsp
470 45d799e2 2018-12-23 stsp time_t
471 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
472 45d799e2 2018-12-23 stsp {
473 45d799e2 2018-12-23 stsp return commit->author_time;
474 45d799e2 2018-12-23 stsp }
475 45d799e2 2018-12-23 stsp
476 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
477 45d799e2 2018-12-23 stsp {
478 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
479 45d799e2 2018-12-23 stsp }
480 45d799e2 2018-12-23 stsp
481 45d799e2 2018-12-23 stsp const char *
482 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
483 45d799e2 2018-12-23 stsp {
484 45d799e2 2018-12-23 stsp return commit->committer;
485 45d799e2 2018-12-23 stsp }
486 45d799e2 2018-12-23 stsp
487 45d799e2 2018-12-23 stsp time_t
488 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
489 45d799e2 2018-12-23 stsp {
490 45d799e2 2018-12-23 stsp return commit->committer_time;
491 45d799e2 2018-12-23 stsp }
492 45d799e2 2018-12-23 stsp
493 45d799e2 2018-12-23 stsp time_t
494 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
495 45d799e2 2018-12-23 stsp {
496 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
497 45d799e2 2018-12-23 stsp }
498 5943eee2 2019-08-13 stsp
499 5943eee2 2019-08-13 stsp const struct got_error *
500 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
501 45d799e2 2018-12-23 stsp {
502 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
503 b9c41b54 2021-08-03 stsp const char *src;
504 b9c41b54 2021-08-03 stsp char *dst;
505 5943eee2 2019-08-13 stsp size_t len;
506 5943eee2 2019-08-13 stsp
507 b9c41b54 2021-08-03 stsp len = strlen(commit->logmsg);
508 b9c41b54 2021-08-03 stsp *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
509 b9c41b54 2021-08-03 stsp if (*logmsg == NULL)
510 b9c41b54 2021-08-03 stsp return got_error_from_errno("malloc");
511 5943eee2 2019-08-13 stsp
512 b9c41b54 2021-08-03 stsp /*
513 b9c41b54 2021-08-03 stsp * Strip out unusual headers. Headers are separated from the commit
514 b9c41b54 2021-08-03 stsp * message body by a single empty line.
515 b9c41b54 2021-08-03 stsp */
516 b9c41b54 2021-08-03 stsp src = commit->logmsg;
517 b9c41b54 2021-08-03 stsp dst = *logmsg;
518 b9c41b54 2021-08-03 stsp while (*src != '\0' && *src != '\n') {
519 b9c41b54 2021-08-03 stsp int copy_header = 1, eol = 0;
520 b9c41b54 2021-08-03 stsp if (strncmp(src, GOT_COMMIT_LABEL_TREE,
521 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
522 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
523 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
524 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_PARENT,
525 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
526 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
527 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
528 b9c41b54 2021-08-03 stsp copy_header = 0;
529 13555e04 2019-09-28 semarie
530 b9c41b54 2021-08-03 stsp while (*src != '\0' && !eol) {
531 b9c41b54 2021-08-03 stsp if (copy_header) {
532 b9c41b54 2021-08-03 stsp *dst = *src;
533 b9c41b54 2021-08-03 stsp dst++;
534 b9c41b54 2021-08-03 stsp }
535 b9c41b54 2021-08-03 stsp if (*src == '\n')
536 b9c41b54 2021-08-03 stsp eol = 1;
537 b9c41b54 2021-08-03 stsp src++;
538 5943eee2 2019-08-13 stsp }
539 b9c41b54 2021-08-03 stsp }
540 b9c41b54 2021-08-03 stsp *dst = '\0';
541 13555e04 2019-09-28 semarie
542 b9c41b54 2021-08-03 stsp if (strlcat(*logmsg, src, len + 1) >= len + 1) {
543 b9c41b54 2021-08-03 stsp err = got_error(GOT_ERR_NO_SPACE);
544 b9c41b54 2021-08-03 stsp goto done;
545 ef744db3 2020-08-27 stsp }
546 ef744db3 2020-08-27 stsp
547 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
548 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
549 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
550 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
551 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
552 5943eee2 2019-08-13 stsp len--;
553 5943eee2 2019-08-13 stsp }
554 b9c41b54 2021-08-03 stsp
555 b9c41b54 2021-08-03 stsp /* Append a trailing newline if missing. */
556 b9c41b54 2021-08-03 stsp if (len > 0 && (*logmsg)[len - 1] != '\n') {
557 b9c41b54 2021-08-03 stsp (*logmsg)[len] = '\n';
558 b9c41b54 2021-08-03 stsp (*logmsg)[len + 1] = '\0';
559 b9c41b54 2021-08-03 stsp }
560 5943eee2 2019-08-13 stsp done:
561 5943eee2 2019-08-13 stsp if (err) {
562 5943eee2 2019-08-13 stsp free(*logmsg);
563 5943eee2 2019-08-13 stsp *logmsg = NULL;
564 5943eee2 2019-08-13 stsp }
565 5943eee2 2019-08-13 stsp return err;
566 24ea5512 2019-08-22 stsp }
567 24ea5512 2019-08-22 stsp
568 24ea5512 2019-08-22 stsp const char *
569 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
570 24ea5512 2019-08-22 stsp {
571 24ea5512 2019-08-22 stsp return commit->logmsg;
572 45d799e2 2018-12-23 stsp }
573 45d799e2 2018-12-23 stsp
574 a440fac0 2018-09-06 stsp const struct got_error *
575 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
576 5e0b25c4 2018-12-24 stsp size_t len)
577 a440fac0 2018-09-06 stsp {
578 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
579 87a3ab84 2023-02-23 op enum got_hash_algorithm algo = GOT_HASH_SHA1;
580 a440fac0 2018-09-06 stsp char *s = buf;
581 ff2a4428 2019-03-19 stsp size_t label_len;
582 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
583 4793d91b 2019-09-22 stsp
584 4793d91b 2019-09-22 stsp if (remain == 0)
585 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
586 230a42bd 2019-05-11 jcs
587 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
588 a440fac0 2018-09-06 stsp if (*commit == NULL)
589 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
590 a440fac0 2018-09-06 stsp
591 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
592 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
593 ff2a4428 2019-03-19 stsp remain -= label_len;
594 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
595 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 a440fac0 2018-09-06 stsp goto done;
597 a440fac0 2018-09-06 stsp }
598 ff2a4428 2019-03-19 stsp s += label_len;
599 87a3ab84 2023-02-23 op if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
600 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 a440fac0 2018-09-06 stsp goto done;
602 a440fac0 2018-09-06 stsp }
603 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
604 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
605 a440fac0 2018-09-06 stsp } else {
606 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 a440fac0 2018-09-06 stsp goto done;
608 a440fac0 2018-09-06 stsp }
609 a440fac0 2018-09-06 stsp
610 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
611 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
612 ff2a4428 2019-03-19 stsp remain -= label_len;
613 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
614 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 a440fac0 2018-09-06 stsp goto done;
616 a440fac0 2018-09-06 stsp }
617 ff2a4428 2019-03-19 stsp s += label_len;
618 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
619 a440fac0 2018-09-06 stsp if (err)
620 a440fac0 2018-09-06 stsp goto done;
621 a440fac0 2018-09-06 stsp
622 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
623 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
624 a440fac0 2018-09-06 stsp }
625 a440fac0 2018-09-06 stsp
626 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
627 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
628 a440fac0 2018-09-06 stsp char *p;
629 a440fac0 2018-09-06 stsp size_t slen;
630 a440fac0 2018-09-06 stsp
631 ff2a4428 2019-03-19 stsp remain -= label_len;
632 a440fac0 2018-09-06 stsp if (remain <= 0) {
633 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 a440fac0 2018-09-06 stsp goto done;
635 a440fac0 2018-09-06 stsp }
636 ff2a4428 2019-03-19 stsp s += label_len;
637 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
638 a440fac0 2018-09-06 stsp if (p == NULL) {
639 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 a440fac0 2018-09-06 stsp goto done;
641 a440fac0 2018-09-06 stsp }
642 a440fac0 2018-09-06 stsp *p = '\0';
643 a440fac0 2018-09-06 stsp slen = strlen(s);
644 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
645 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
646 a440fac0 2018-09-06 stsp if (err)
647 a440fac0 2018-09-06 stsp goto done;
648 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
649 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
650 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
651 a440fac0 2018-09-06 stsp goto done;
652 a440fac0 2018-09-06 stsp }
653 a440fac0 2018-09-06 stsp s += slen + 1;
654 a440fac0 2018-09-06 stsp remain -= slen + 1;
655 a440fac0 2018-09-06 stsp }
656 a440fac0 2018-09-06 stsp
657 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
658 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
659 a440fac0 2018-09-06 stsp char *p;
660 a440fac0 2018-09-06 stsp size_t slen;
661 a440fac0 2018-09-06 stsp
662 ff2a4428 2019-03-19 stsp remain -= label_len;
663 a440fac0 2018-09-06 stsp if (remain <= 0) {
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 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
669 a440fac0 2018-09-06 stsp if (p == NULL) {
670 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
671 a440fac0 2018-09-06 stsp goto done;
672 a440fac0 2018-09-06 stsp }
673 a440fac0 2018-09-06 stsp *p = '\0';
674 a440fac0 2018-09-06 stsp slen = strlen(s);
675 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
676 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
677 a440fac0 2018-09-06 stsp if (err)
678 a440fac0 2018-09-06 stsp goto done;
679 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
680 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
681 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
682 a440fac0 2018-09-06 stsp goto done;
683 a440fac0 2018-09-06 stsp }
684 a440fac0 2018-09-06 stsp s += slen + 1;
685 a440fac0 2018-09-06 stsp remain -= slen + 1;
686 a440fac0 2018-09-06 stsp }
687 a440fac0 2018-09-06 stsp
688 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
689 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
690 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
691 a440fac0 2018-09-06 stsp goto done;
692 a440fac0 2018-09-06 stsp }
693 a440fac0 2018-09-06 stsp done:
694 a440fac0 2018-09-06 stsp if (err) {
695 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
696 a440fac0 2018-09-06 stsp *commit = NULL;
697 a440fac0 2018-09-06 stsp }
698 a440fac0 2018-09-06 stsp return err;
699 ed175427 2019-05-09 stsp }
700 13b2bc37 2022-10-23 stsp
701 13b2bc37 2022-10-23 stsp const struct got_error *
702 13b2bc37 2022-10-23 stsp got_object_read_commit(struct got_commit_object **commit, int fd,
703 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
704 13b2bc37 2022-10-23 stsp {
705 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
706 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
707 13b2bc37 2022-10-23 stsp size_t len;
708 13b2bc37 2022-10-23 stsp uint8_t *p;
709 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
710 ae25a666 2023-02-23 op struct got_hash ctx;
711 13b2bc37 2022-10-23 stsp struct got_object_id id;
712 ed175427 2019-05-09 stsp
713 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
714 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
715 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
716 13b2bc37 2022-10-23 stsp
717 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
718 13b2bc37 2022-10-23 stsp if (err)
719 13b2bc37 2022-10-23 stsp return err;
720 13b2bc37 2022-10-23 stsp
721 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
722 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
723 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
724 13b2bc37 2022-10-23 stsp goto done;
725 13b2bc37 2022-10-23 stsp }
726 13b2bc37 2022-10-23 stsp
727 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
728 13b2bc37 2022-10-23 stsp if (err)
729 13b2bc37 2022-10-23 stsp goto done;
730 13b2bc37 2022-10-23 stsp
731 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
732 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
733 13b2bc37 2022-10-23 stsp goto done;
734 13b2bc37 2022-10-23 stsp }
735 13b2bc37 2022-10-23 stsp
736 13b2bc37 2022-10-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
737 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
738 13b2bc37 2022-10-23 stsp goto done;
739 13b2bc37 2022-10-23 stsp }
740 13b2bc37 2022-10-23 stsp
741 13b2bc37 2022-10-23 stsp /* Skip object header. */
742 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
743 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, p + obj->hdrlen, len);
744 13b2bc37 2022-10-23 stsp done:
745 13b2bc37 2022-10-23 stsp free(p);
746 13b2bc37 2022-10-23 stsp if (obj)
747 13b2bc37 2022-10-23 stsp got_object_close(obj);
748 13b2bc37 2022-10-23 stsp return err;
749 13b2bc37 2022-10-23 stsp }
750 13b2bc37 2022-10-23 stsp
751 ed175427 2019-05-09 stsp void
752 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
753 ed175427 2019-05-09 stsp {
754 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
755 03fa71c8 2018-09-06 stsp tree->refcnt--;
756 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
757 03fa71c8 2018-09-06 stsp return;
758 03fa71c8 2018-09-06 stsp }
759 03fa71c8 2018-09-06 stsp
760 56e0773d 2019-11-28 stsp free(tree->entries);
761 03fa71c8 2018-09-06 stsp free(tree);
762 03fa71c8 2018-09-06 stsp }
763 03fa71c8 2018-09-06 stsp
764 cdfe5501 2023-04-24 stsp const struct got_error *
765 cdfe5501 2023-04-24 stsp got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
766 cdfe5501 2023-04-24 stsp char *buf, size_t maxlen)
767 a440fac0 2018-09-06 stsp {
768 8914529d 2019-04-13 stsp char *p, *space;
769 a0de39f3 2019-08-09 stsp
770 a0de39f3 2019-08-09 stsp *elen = 0;
771 a440fac0 2018-09-06 stsp
772 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
773 9985f404 2022-05-19 stsp if (*elen > maxlen)
774 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
775 a440fac0 2018-09-06 stsp
776 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
777 9985f404 2022-05-19 stsp if (space == NULL || space <= buf)
778 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
779 9985f404 2022-05-19 stsp
780 9985f404 2022-05-19 stsp pte->mode = 0;
781 8914529d 2019-04-13 stsp p = buf;
782 8914529d 2019-04-13 stsp while (p < space) {
783 00d10bca 2023-01-19 mark if (*p < '0' || *p > '7')
784 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
785 9985f404 2022-05-19 stsp pte->mode <<= 3;
786 9985f404 2022-05-19 stsp pte->mode |= *p - '0';
787 a440fac0 2018-09-06 stsp p++;
788 a440fac0 2018-09-06 stsp }
789 a440fac0 2018-09-06 stsp
790 9985f404 2022-05-19 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
791 9985f404 2022-05-19 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
792 9985f404 2022-05-19 stsp
793 9985f404 2022-05-19 stsp pte->name = space + 1;
794 9985f404 2022-05-19 stsp pte->namelen = strlen(pte->name);
795 68bf1b1e 2018-11-07 stsp buf += *elen;
796 9985f404 2022-05-19 stsp pte->id = buf;
797 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
798 9985f404 2022-05-19 stsp return NULL;
799 a440fac0 2018-09-06 stsp }
800 a440fac0 2018-09-06 stsp
801 9985f404 2022-05-19 stsp static int
802 9985f404 2022-05-19 stsp pte_cmp(const void *pa, const void *pb)
803 9985f404 2022-05-19 stsp {
804 9985f404 2022-05-19 stsp const struct got_parsed_tree_entry *a = pa, *b = pb;
805 9985f404 2022-05-19 stsp
806 9985f404 2022-05-19 stsp return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
807 9985f404 2022-05-19 stsp }
808 9985f404 2022-05-19 stsp
809 a440fac0 2018-09-06 stsp const struct got_error *
810 d294b1dc 2022-10-18 stsp got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
811 d294b1dc 2022-10-18 stsp size_t *nentries_alloc, uint8_t *buf, size_t len)
812 a440fac0 2018-09-06 stsp {
813 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
814 d294b1dc 2022-10-18 stsp size_t remain = len;
815 9985f404 2022-05-19 stsp const size_t nalloc = 16;
816 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *pte;
817 9985f404 2022-05-19 stsp int i;
818 f5d3d7af 2019-02-05 stsp
819 3022d272 2019-11-14 stsp *nentries = 0;
820 db1d3576 2019-10-04 stsp if (remain == 0)
821 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
822 db1d3576 2019-10-04 stsp
823 a440fac0 2018-09-06 stsp while (remain > 0) {
824 a440fac0 2018-09-06 stsp size_t elen;
825 a440fac0 2018-09-06 stsp
826 d294b1dc 2022-10-18 stsp if (*nentries >= *nentries_alloc) {
827 d294b1dc 2022-10-18 stsp pte = recallocarray(*entries, *nentries_alloc,
828 d294b1dc 2022-10-18 stsp *nentries_alloc + nalloc, sizeof(**entries));
829 9985f404 2022-05-19 stsp if (pte == NULL) {
830 9985f404 2022-05-19 stsp err = got_error_from_errno("recallocarray");
831 9985f404 2022-05-19 stsp goto done;
832 9985f404 2022-05-19 stsp }
833 9985f404 2022-05-19 stsp *entries = pte;
834 d294b1dc 2022-10-18 stsp *nentries_alloc += nalloc;
835 9985f404 2022-05-19 stsp }
836 9985f404 2022-05-19 stsp
837 9985f404 2022-05-19 stsp pte = &(*entries)[*nentries];
838 cdfe5501 2023-04-24 stsp err = got_object_parse_tree_entry(pte, &elen, buf, remain);
839 a440fac0 2018-09-06 stsp if (err)
840 f5d3d7af 2019-02-05 stsp goto done;
841 a440fac0 2018-09-06 stsp buf += elen;
842 a440fac0 2018-09-06 stsp remain -= elen;
843 3022d272 2019-11-14 stsp (*nentries)++;
844 a440fac0 2018-09-06 stsp }
845 a440fac0 2018-09-06 stsp
846 a440fac0 2018-09-06 stsp if (remain != 0) {
847 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 f5d3d7af 2019-02-05 stsp goto done;
849 a440fac0 2018-09-06 stsp }
850 9985f404 2022-05-19 stsp
851 9985f404 2022-05-19 stsp if (*nentries > 1) {
852 9985f404 2022-05-19 stsp mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
853 9985f404 2022-05-19 stsp
854 9985f404 2022-05-19 stsp for (i = 0; i < *nentries - 1; i++) {
855 9985f404 2022-05-19 stsp struct got_parsed_tree_entry *prev = &(*entries)[i];
856 9985f404 2022-05-19 stsp pte = &(*entries)[i + 1];
857 9985f404 2022-05-19 stsp if (got_path_cmp(prev->name, pte->name,
858 9985f404 2022-05-19 stsp prev->namelen, pte->namelen) == 0) {
859 9985f404 2022-05-19 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
860 9985f404 2022-05-19 stsp break;
861 9985f404 2022-05-19 stsp }
862 9985f404 2022-05-19 stsp }
863 9985f404 2022-05-19 stsp }
864 3022d272 2019-11-14 stsp done:
865 d294b1dc 2022-10-18 stsp if (err)
866 3022d272 2019-11-14 stsp *nentries = 0;
867 f5d3d7af 2019-02-05 stsp return err;
868 b64b1f95 2020-01-06 stsp }
869 b64b1f95 2020-01-06 stsp
870 13b2bc37 2022-10-23 stsp const struct got_error *
871 13b2bc37 2022-10-23 stsp got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
872 13b2bc37 2022-10-23 stsp size_t *nentries_alloc, uint8_t **p, int fd,
873 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id)
874 13b2bc37 2022-10-23 stsp {
875 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
876 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
877 13b2bc37 2022-10-23 stsp size_t len;
878 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
879 ae25a666 2023-02-23 op struct got_hash ctx;
880 13b2bc37 2022-10-23 stsp struct got_object_id id;
881 13b2bc37 2022-10-23 stsp
882 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
883 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
884 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
885 13b2bc37 2022-10-23 stsp
886 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
887 13b2bc37 2022-10-23 stsp if (err)
888 13b2bc37 2022-10-23 stsp return err;
889 13b2bc37 2022-10-23 stsp
890 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
891 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
892 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
893 13b2bc37 2022-10-23 stsp goto done;
894 13b2bc37 2022-10-23 stsp }
895 13b2bc37 2022-10-23 stsp
896 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, *p, len);
897 13b2bc37 2022-10-23 stsp if (err)
898 13b2bc37 2022-10-23 stsp goto done;
899 13b2bc37 2022-10-23 stsp
900 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
901 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
902 13b2bc37 2022-10-23 stsp goto done;
903 13b2bc37 2022-10-23 stsp }
904 13b2bc37 2022-10-23 stsp
905 13b2bc37 2022-10-23 stsp /* Skip object header. */
906 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
907 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(entries, nentries, nentries_alloc,
908 13b2bc37 2022-10-23 stsp *p + obj->hdrlen, len);
909 13b2bc37 2022-10-23 stsp done:
910 13b2bc37 2022-10-23 stsp if (obj)
911 13b2bc37 2022-10-23 stsp got_object_close(obj);
912 13b2bc37 2022-10-23 stsp return err;
913 13b2bc37 2022-10-23 stsp }
914 13b2bc37 2022-10-23 stsp
915 b64b1f95 2020-01-06 stsp void
916 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
917 f4a881ce 2018-11-17 stsp {
918 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
919 ca0d469c 2019-08-13 stsp tag->refcnt--;
920 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
921 ca0d469c 2019-08-13 stsp return;
922 ca0d469c 2019-08-13 stsp }
923 ca0d469c 2019-08-13 stsp
924 f4a881ce 2018-11-17 stsp free(tag->tag);
925 f4a881ce 2018-11-17 stsp free(tag->tagger);
926 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
927 f4a881ce 2018-11-17 stsp free(tag);
928 f4a881ce 2018-11-17 stsp }
929 f4a881ce 2018-11-17 stsp
930 ad242220 2018-09-08 stsp const struct got_error *
931 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
932 f4a881ce 2018-11-17 stsp {
933 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
934 87a3ab84 2023-02-23 op enum got_hash_algorithm algo = GOT_HASH_SHA1;
935 f4a881ce 2018-11-17 stsp size_t remain = len;
936 f4a881ce 2018-11-17 stsp char *s = buf;
937 ff2a4428 2019-03-19 stsp size_t label_len;
938 4793d91b 2019-09-22 stsp
939 4793d91b 2019-09-22 stsp if (remain == 0)
940 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
941 f4a881ce 2018-11-17 stsp
942 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
943 f4a881ce 2018-11-17 stsp if (*tag == NULL)
944 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
945 f4a881ce 2018-11-17 stsp
946 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
947 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
948 ff2a4428 2019-03-19 stsp remain -= label_len;
949 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
950 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
951 f4a881ce 2018-11-17 stsp goto done;
952 f4a881ce 2018-11-17 stsp }
953 ff2a4428 2019-03-19 stsp s += label_len;
954 87a3ab84 2023-02-23 op if (!got_parse_object_id(&(*tag)->id, s, algo)) {
955 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
956 f4a881ce 2018-11-17 stsp goto done;
957 f4a881ce 2018-11-17 stsp }
958 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
959 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
960 f4a881ce 2018-11-17 stsp } else {
961 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
962 f4a881ce 2018-11-17 stsp goto done;
963 f4a881ce 2018-11-17 stsp }
964 f4a881ce 2018-11-17 stsp
965 f4a881ce 2018-11-17 stsp if (remain <= 0) {
966 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
967 f4a881ce 2018-11-17 stsp goto done;
968 f4a881ce 2018-11-17 stsp }
969 f4a881ce 2018-11-17 stsp
970 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
971 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
972 ff2a4428 2019-03-19 stsp remain -= label_len;
973 f4a881ce 2018-11-17 stsp if (remain <= 0) {
974 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
975 f4a881ce 2018-11-17 stsp goto done;
976 f4a881ce 2018-11-17 stsp }
977 ff2a4428 2019-03-19 stsp s += label_len;
978 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
979 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
980 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
981 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
982 ff2a4428 2019-03-19 stsp s += label_len;
983 ff2a4428 2019-03-19 stsp remain -= label_len;
984 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
985 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
986 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
987 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
988 ff2a4428 2019-03-19 stsp s += label_len;
989 ff2a4428 2019-03-19 stsp remain -= label_len;
990 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
991 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
992 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
993 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
994 ff2a4428 2019-03-19 stsp s += label_len;
995 ff2a4428 2019-03-19 stsp remain -= label_len;
996 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
997 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
998 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
999 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
1000 ff2a4428 2019-03-19 stsp s += label_len;
1001 ff2a4428 2019-03-19 stsp remain -= label_len;
1002 f4a881ce 2018-11-17 stsp } else {
1003 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1004 f4a881ce 2018-11-17 stsp goto done;
1005 f4a881ce 2018-11-17 stsp }
1006 f4a881ce 2018-11-17 stsp
1007 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
1008 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1009 f4a881ce 2018-11-17 stsp goto done;
1010 f4a881ce 2018-11-17 stsp }
1011 f4a881ce 2018-11-17 stsp s++;
1012 f4a881ce 2018-11-17 stsp remain--;
1013 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1014 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1015 f4a881ce 2018-11-17 stsp goto done;
1016 f4a881ce 2018-11-17 stsp }
1017 f4a881ce 2018-11-17 stsp } else {
1018 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1019 f4a881ce 2018-11-17 stsp goto done;
1020 f4a881ce 2018-11-17 stsp }
1021 f4a881ce 2018-11-17 stsp
1022 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
1023 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1024 f4a881ce 2018-11-17 stsp char *p;
1025 f4a881ce 2018-11-17 stsp size_t slen;
1026 ff2a4428 2019-03-19 stsp remain -= label_len;
1027 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1028 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1029 f4a881ce 2018-11-17 stsp goto done;
1030 f4a881ce 2018-11-17 stsp }
1031 ff2a4428 2019-03-19 stsp s += label_len;
1032 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1033 f4a881ce 2018-11-17 stsp if (p == NULL) {
1034 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1035 f4a881ce 2018-11-17 stsp goto done;
1036 f4a881ce 2018-11-17 stsp }
1037 f4a881ce 2018-11-17 stsp *p = '\0';
1038 f4a881ce 2018-11-17 stsp slen = strlen(s);
1039 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
1040 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1041 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1042 f4a881ce 2018-11-17 stsp goto done;
1043 f4a881ce 2018-11-17 stsp }
1044 f4a881ce 2018-11-17 stsp s += slen + 1;
1045 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1046 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1047 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1048 f4a881ce 2018-11-17 stsp goto done;
1049 f4a881ce 2018-11-17 stsp }
1050 f4a881ce 2018-11-17 stsp } else {
1051 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1052 f4a881ce 2018-11-17 stsp goto done;
1053 f4a881ce 2018-11-17 stsp }
1054 f4a881ce 2018-11-17 stsp
1055 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
1056 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1057 f4a881ce 2018-11-17 stsp char *p;
1058 f4a881ce 2018-11-17 stsp size_t slen;
1059 f4a881ce 2018-11-17 stsp
1060 ff2a4428 2019-03-19 stsp remain -= label_len;
1061 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1062 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1063 f4a881ce 2018-11-17 stsp goto done;
1064 f4a881ce 2018-11-17 stsp }
1065 ff2a4428 2019-03-19 stsp s += label_len;
1066 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1067 f4a881ce 2018-11-17 stsp if (p == NULL) {
1068 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 f4a881ce 2018-11-17 stsp goto done;
1070 f4a881ce 2018-11-17 stsp }
1071 f4a881ce 2018-11-17 stsp *p = '\0';
1072 f4a881ce 2018-11-17 stsp slen = strlen(s);
1073 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
1074 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
1075 f4a881ce 2018-11-17 stsp if (err)
1076 f4a881ce 2018-11-17 stsp goto done;
1077 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
1078 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1079 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1080 f4a881ce 2018-11-17 stsp goto done;
1081 f4a881ce 2018-11-17 stsp }
1082 f4a881ce 2018-11-17 stsp s += slen + 1;
1083 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1084 5a8b373c 2020-12-18 stsp if (remain < 0) {
1085 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1086 f4a881ce 2018-11-17 stsp goto done;
1087 f4a881ce 2018-11-17 stsp }
1088 f4a881ce 2018-11-17 stsp } else {
1089 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
1090 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
1091 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
1092 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1093 e0e55b50 2019-02-01 stsp goto done;
1094 e0e55b50 2019-02-01 stsp }
1095 f4a881ce 2018-11-17 stsp }
1096 f4a881ce 2018-11-17 stsp
1097 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
1098 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1099 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1100 f4a881ce 2018-11-17 stsp goto done;
1101 f4a881ce 2018-11-17 stsp }
1102 f4a881ce 2018-11-17 stsp done:
1103 f4a881ce 2018-11-17 stsp if (err) {
1104 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
1105 f4a881ce 2018-11-17 stsp *tag = NULL;
1106 13b2bc37 2022-10-23 stsp }
1107 13b2bc37 2022-10-23 stsp return err;
1108 13b2bc37 2022-10-23 stsp }
1109 13b2bc37 2022-10-23 stsp
1110 13b2bc37 2022-10-23 stsp const struct got_error *
1111 758dc042 2022-11-06 stsp got_object_read_tag(struct got_tag_object **tag, int fd,
1112 13b2bc37 2022-10-23 stsp struct got_object_id *expected_id, size_t expected_size)
1113 13b2bc37 2022-10-23 stsp {
1114 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1115 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
1116 13b2bc37 2022-10-23 stsp size_t len;
1117 13b2bc37 2022-10-23 stsp uint8_t *p;
1118 13b2bc37 2022-10-23 stsp struct got_inflate_checksum csum;
1119 ae25a666 2023-02-23 op struct got_hash ctx;
1120 13b2bc37 2022-10-23 stsp struct got_object_id id;
1121 13b2bc37 2022-10-23 stsp
1122 ae25a666 2023-02-23 op got_hash_init(&ctx, GOT_HASH_SHA1);
1123 13b2bc37 2022-10-23 stsp memset(&csum, 0, sizeof(csum));
1124 ae25a666 2023-02-23 op csum.output_ctx = &ctx;
1125 13b2bc37 2022-10-23 stsp
1126 13b2bc37 2022-10-23 stsp err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1127 13b2bc37 2022-10-23 stsp expected_size, fd);
1128 13b2bc37 2022-10-23 stsp if (err)
1129 13b2bc37 2022-10-23 stsp return err;
1130 13b2bc37 2022-10-23 stsp
1131 ae25a666 2023-02-23 op got_hash_final_object_id(&ctx, &id);
1132 7f959095 2023-02-02 op if (got_object_id_cmp(expected_id, &id) != 0) {
1133 3c23f6cd 2023-02-04 op err = got_error_checksum(expected_id);
1134 13b2bc37 2022-10-23 stsp goto done;
1135 f4a881ce 2018-11-17 stsp }
1136 13b2bc37 2022-10-23 stsp
1137 13b2bc37 2022-10-23 stsp err = got_object_parse_header(&obj, p, len);
1138 13b2bc37 2022-10-23 stsp if (err)
1139 13b2bc37 2022-10-23 stsp goto done;
1140 13b2bc37 2022-10-23 stsp
1141 13b2bc37 2022-10-23 stsp if (len < obj->hdrlen + obj->size) {
1142 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1143 13b2bc37 2022-10-23 stsp goto done;
1144 13b2bc37 2022-10-23 stsp }
1145 13b2bc37 2022-10-23 stsp
1146 13b2bc37 2022-10-23 stsp /* Skip object header. */
1147 13b2bc37 2022-10-23 stsp len -= obj->hdrlen;
1148 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1149 13b2bc37 2022-10-23 stsp done:
1150 13b2bc37 2022-10-23 stsp free(p);
1151 13b2bc37 2022-10-23 stsp if (obj)
1152 13b2bc37 2022-10-23 stsp got_object_close(obj);
1153 f4a881ce 2018-11-17 stsp return err;
1154 f4a881ce 2018-11-17 stsp }