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