2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 0a0a3048 2018-01-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 0a0a3048 2018-01-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 0a0a3048 2018-01-10 stsp * copyright notice and this permission notice appear in all copies.
8 0a0a3048 2018-01-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0a0a3048 2018-01-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0a0a3048 2018-01-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0a0a3048 2018-01-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0a0a3048 2018-01-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0a0a3048 2018-01-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0a0a3048 2018-01-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a1fd68d8 2018-01-12 stsp #include <sys/types.h>
18 0a0a3048 2018-01-10 stsp #include <sys/stat.h>
19 a1fd68d8 2018-01-12 stsp #include <sys/queue.h>
20 876c234b 2018-09-10 stsp #include <sys/uio.h>
21 57b35b75 2018-06-22 stsp #include <sys/mman.h>
23 7e656b93 2018-03-17 stsp #include <fcntl.h>
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 0a0a3048 2018-01-10 stsp #include <stdio.h>
26 a1fd68d8 2018-01-12 stsp #include <stdint.h>
27 0a0a3048 2018-01-10 stsp #include <stdlib.h>
28 0a0a3048 2018-01-10 stsp #include <string.h>
29 0a0a3048 2018-01-10 stsp #include <limits.h>
30 0a0a3048 2018-01-10 stsp #include <sha1.h>
31 0a0a3048 2018-01-10 stsp #include <endian.h>
32 a1fd68d8 2018-01-12 stsp #include <zlib.h>
33 876c234b 2018-09-10 stsp #include <imsg.h>
35 0a0a3048 2018-01-10 stsp #include "got_error.h"
36 a1fd68d8 2018-01-12 stsp #include "got_object.h"
37 511a516b 2018-05-19 stsp #include "got_opentemp.h"
38 324d37e7 2019-05-11 stsp #include "got_path.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
42 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
44 dd88155e 2019-06-29 stsp #include "got_lib_object_parse.h"
45 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
46 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
48 79b11c62 2018-03-09 stsp #ifndef nitems
49 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
53 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 0a0a3048 2018-01-10 stsp static const struct got_error *
57 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
61 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
62 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
63 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
66 0a0a3048 2018-01-10 stsp return NULL;
69 1510f469 2018-09-09 stsp const struct got_error *
70 817c5a18 2018-09-09 stsp got_packidx_init_hdr(struct got_packidx *p, int verify)
72 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
73 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
74 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
75 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
76 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
79 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
81 57b35b75 2018-06-22 stsp h = &p->hdr;
83 57b35b75 2018-06-22 stsp remain = p->len;
85 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
86 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
90 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
92 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
93 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
94 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
97 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
99 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
100 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->magic)) {
101 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
105 57b35b75 2018-06-22 stsp if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
106 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
109 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
110 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
112 0cb74cf4 2018-07-08 stsp if (verify)
113 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
115 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
116 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
119 fc79a48d 2018-07-09 stsp if (p->map)
120 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
122 fc79a48d 2018-07-09 stsp h->version = malloc(sizeof(*h->version));
123 fc79a48d 2018-07-09 stsp if (h->version == NULL) {
124 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
127 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
129 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
130 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->version)) {
131 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
135 57b35b75 2018-06-22 stsp if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
136 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
139 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
140 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
142 0cb74cf4 2018-07-08 stsp if (verify)
143 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
145 57b35b75 2018-06-22 stsp len_fanout =
146 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
147 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
148 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
151 fc79a48d 2018-07-09 stsp if (p->map)
152 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
154 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
155 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
156 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
159 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
161 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
162 faaa1c0f 2018-09-15 stsp else if (n != len_fanout) {
163 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
167 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
170 0cb74cf4 2018-07-08 stsp if (verify)
171 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
172 57b35b75 2018-06-22 stsp offset += len_fanout;
173 57b35b75 2018-06-22 stsp remain -= len_fanout;
175 c8262310 2018-06-04 stsp nobj = betoh32(h->fanout_table[0xff]);
176 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
177 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
178 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
181 fc79a48d 2018-07-09 stsp if (p->map)
182 fc79a48d 2018-07-09 stsp h->sorted_ids =
183 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
185 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
186 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
187 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
190 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
192 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
193 faaa1c0f 2018-09-15 stsp else if (n != len_ids) {
194 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
198 0cb74cf4 2018-07-08 stsp if (verify)
199 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
200 57b35b75 2018-06-22 stsp offset += len_ids;
201 57b35b75 2018-06-22 stsp remain -= len_ids;
203 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
204 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
207 fc79a48d 2018-07-09 stsp if (p->map)
208 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
210 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
211 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
212 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
215 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
217 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
218 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->crc32)) {
219 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
223 0cb74cf4 2018-07-08 stsp if (verify)
224 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
225 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->crc32);
226 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->crc32);
228 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->offsets)) {
229 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
232 fc79a48d 2018-07-09 stsp if (p->map)
233 fc79a48d 2018-07-09 stsp h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
235 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->offsets));
236 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
237 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
240 fc79a48d 2018-07-09 stsp n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
242 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
243 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->offsets)) {
244 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
248 0cb74cf4 2018-07-08 stsp if (verify)
249 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
250 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
251 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
252 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
254 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
255 57b35b75 2018-06-22 stsp if (p->len <= 0x80000000)
256 0a0a3048 2018-01-10 stsp goto checksum;
258 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->large_offsets)) {
259 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
262 fc79a48d 2018-07-09 stsp if (p->map)
263 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
265 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->large_offsets));
266 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
267 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
270 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
271 fc79a48d 2018-07-09 stsp nobj * sizeof(*h->large_offsets));
273 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
274 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->large_offsets)) {
275 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
279 0cb74cf4 2018-07-08 stsp if (verify)
280 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
281 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->large_offsets));
282 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->large_offsets);
283 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->large_offsets);
286 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
287 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
290 fc79a48d 2018-07-09 stsp if (p->map)
291 fc79a48d 2018-07-09 stsp h->trailer =
292 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
294 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
295 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
296 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
299 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
301 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
302 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->trailer)) {
303 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
307 0cb74cf4 2018-07-08 stsp if (verify) {
308 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
309 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
310 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
311 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
312 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
315 817c5a18 2018-09-09 stsp return err;
318 817c5a18 2018-09-09 stsp const struct got_error *
319 817c5a18 2018-09-09 stsp got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
321 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
322 817c5a18 2018-09-09 stsp struct got_packidx *p;
323 57377f07 2019-05-23 stsp struct stat sb;
325 817c5a18 2018-09-09 stsp *packidx = NULL;
327 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
328 817c5a18 2018-09-09 stsp if (p == NULL)
329 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
331 a5b57ccf 2019-04-11 stsp p->fd = open(path, O_RDONLY | O_NOFOLLOW);
332 817c5a18 2018-09-09 stsp if (p->fd == -1)
333 638f9024 2019-05-13 stsp return got_error_from_errno2("open", path);
335 57377f07 2019-05-23 stsp if (fstat(p->fd, &sb) != 0) {
336 57377f07 2019-05-23 stsp err = got_error_from_errno2("fstat", path);
337 817c5a18 2018-09-09 stsp close(p->fd);
339 817c5a18 2018-09-09 stsp return err;
341 57377f07 2019-05-23 stsp p->len = sb.st_size;
342 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
343 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
344 817c5a18 2018-09-09 stsp close(p->fd);
346 817c5a18 2018-09-09 stsp return err;
349 817c5a18 2018-09-09 stsp p->path_packidx = strdup(path);
350 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
351 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
355 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
356 817c5a18 2018-09-09 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
357 3a11398b 2019-02-21 stsp if (p->map == MAP_FAILED) {
358 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
359 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
362 817c5a18 2018-09-09 stsp p->map = NULL; /* fall back to read(2) */
366 817c5a18 2018-09-09 stsp err = got_packidx_init_hdr(p, verify);
369 0a0a3048 2018-01-10 stsp got_packidx_close(p);
371 0a0a3048 2018-01-10 stsp *packidx = p;
373 0a0a3048 2018-01-10 stsp return err;
376 57b35b75 2018-06-22 stsp const struct got_error *
377 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
379 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
381 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
382 fc79a48d 2018-07-09 stsp if (packidx->map) {
383 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
384 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
386 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
387 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
388 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
389 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
390 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
391 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
392 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
393 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
395 3a6ce05a 2019-02-11 stsp if (close(packidx->fd) != 0 && err == NULL)
396 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
397 0a0a3048 2018-01-10 stsp free(packidx);
399 57b35b75 2018-06-22 stsp return err;
402 a1fd68d8 2018-01-12 stsp static off_t
403 6fd11751 2018-06-04 stsp get_object_offset(struct got_packidx *packidx, int idx)
405 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
406 6fd11751 2018-06-04 stsp uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
407 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
408 a1fd68d8 2018-01-12 stsp uint64_t loffset;
409 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
410 6fd11751 2018-06-04 stsp if (idx < 0 || idx > totobj ||
411 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
413 6fd11751 2018-06-04 stsp loffset = betoh64(packidx->hdr.large_offsets[idx]);
414 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
416 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
420 1510f469 2018-09-09 stsp got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
422 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
423 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
424 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
426 a1fd68d8 2018-01-12 stsp if (id0 > 0)
427 40aeb19c 2018-06-22 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
429 40aeb19c 2018-06-22 stsp while (left <= right) {
430 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
431 40aeb19c 2018-06-22 stsp int i, cmp;
433 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
434 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
435 57b35b75 2018-06-22 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
436 16dcbf91 2018-04-01 stsp if (cmp == 0)
438 40aeb19c 2018-06-22 stsp else if (cmp > 0)
439 40aeb19c 2018-06-22 stsp left = i + 1;
440 40aeb19c 2018-06-22 stsp else if (cmp < 0)
441 40aeb19c 2018-06-22 stsp right = i - 1;
447 876c234b 2018-09-10 stsp const struct got_error *
448 dd88155e 2019-06-29 stsp got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
449 4277420a 2019-06-29 stsp struct got_packidx *packidx, const char *id_str_prefix)
451 dd88155e 2019-06-29 stsp const struct got_error *err = NULL;
452 4277420a 2019-06-29 stsp u_int8_t id0;
453 4277420a 2019-06-29 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
454 4277420a 2019-06-29 stsp char hex[3];
455 4277420a 2019-06-29 stsp size_t prefix_len = strlen(id_str_prefix);
456 e09a504c 2019-06-28 stsp struct got_packidx_object_id *oid;
459 dd88155e 2019-06-29 stsp SIMPLEQ_INIT(matched_ids);
461 4277420a 2019-06-29 stsp if (prefix_len < 2)
462 4277420a 2019-06-29 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
464 4277420a 2019-06-29 stsp hex[0] = id_str_prefix[0];
465 4277420a 2019-06-29 stsp hex[1] = id_str_prefix[1];
466 4277420a 2019-06-29 stsp hex[2] = '\0';
467 4277420a 2019-06-29 stsp if (!got_parse_xdigit(&id0, hex))
468 4277420a 2019-06-29 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
470 4277420a 2019-06-29 stsp i = betoh32(packidx->hdr.fanout_table[id0 - 1]);
471 4277420a 2019-06-29 stsp if (i == 0)
472 4277420a 2019-06-29 stsp return NULL;
474 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[i];
475 4277420a 2019-06-29 stsp while (i < totobj && oid->sha1[0] == id0) {
476 4277420a 2019-06-29 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
477 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
480 4277420a 2019-06-29 stsp if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
481 4277420a 2019-06-29 stsp return got_error(GOT_ERR_NO_SPACE);
483 4277420a 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, prefix_len);
484 4277420a 2019-06-29 stsp if (cmp < 0) {
485 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
487 4277420a 2019-06-29 stsp } else if (cmp > 0)
490 dd88155e 2019-06-29 stsp err = got_object_qid_alloc_partial(&qid);
493 dd88155e 2019-06-29 stsp memcpy(qid->id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
494 dd88155e 2019-06-29 stsp SIMPLEQ_INSERT_TAIL(matched_ids, qid, entry);
496 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
500 0adc7bcc 2019-06-29 stsp got_object_id_queue_free(matched_ids);
501 dd88155e 2019-06-29 stsp return err;
504 e09a504c 2019-06-28 stsp const struct got_error *
505 876c234b 2018-09-10 stsp got_pack_stop_privsep_child(struct got_pack *pack)
507 96732e0b 2018-11-11 stsp const struct got_error *err = NULL;
509 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL)
510 876c234b 2018-09-10 stsp return NULL;
512 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
514 96732e0b 2018-11-11 stsp return err;
515 96732e0b 2018-11-11 stsp err = got_privsep_wait_for_child(pack->privsep_child->pid);
516 0dd5271b 2019-05-10 stsp if (close(pack->privsep_child->imsg_fd) != 0 && err == NULL)
517 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
518 876c234b 2018-09-10 stsp free(pack->privsep_child);
519 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
520 876c234b 2018-09-10 stsp return err;
523 d7464085 2018-07-09 stsp const struct got_error *
524 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
526 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
528 876c234b 2018-09-10 stsp err = got_pack_stop_privsep_child(pack);
529 876c234b 2018-09-10 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
530 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
531 3a6ce05a 2019-02-11 stsp if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
532 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
533 8b2180d4 2018-04-26 stsp pack->fd = -1;
534 4589e373 2018-03-17 stsp free(pack->path_packfile);
535 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
536 4589e373 2018-03-17 stsp pack->filesize = 0;
538 c7fe698a 2018-01-23 stsp return err;
541 c7fe698a 2018-01-23 stsp static const struct got_error *
542 d7464085 2018-07-09 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
543 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
545 a487c1d0 2018-01-14 stsp uint8_t t = 0;
546 a487c1d0 2018-01-14 stsp uint64_t s = 0;
547 a487c1d0 2018-01-14 stsp uint8_t sizeN;
548 d7464085 2018-07-09 stsp size_t mapoff = 0;
553 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
554 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
556 d7464085 2018-07-09 stsp if (pack->map) {
557 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
559 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
560 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
564 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
566 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
568 d7464085 2018-07-09 stsp if (pack->map) {
569 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
570 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
572 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
574 638f9024 2019-05-13 stsp return got_error_from_errno("read");
575 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
576 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
578 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
580 a1fd68d8 2018-01-12 stsp if (i == 0) {
581 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
582 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
583 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
585 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
586 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
589 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
593 a487c1d0 2018-01-14 stsp return NULL;
596 a1fd68d8 2018-01-12 stsp static const struct got_error *
597 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
598 c59b3346 2018-09-11 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size, int idx)
600 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
601 6ccb713b 2018-01-19 stsp if (*obj == NULL)
602 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
604 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
605 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
606 638f9024 2019-05-13 stsp const struct got_error *err = got_error_from_errno("strdup");
607 6ccb713b 2018-01-19 stsp free(*obj);
608 6ccb713b 2018-01-19 stsp *obj = NULL;
609 0a585a0d 2018-03-17 stsp return err;
612 6ccb713b 2018-01-19 stsp (*obj)->type = type;
613 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
614 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
615 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
616 6ccb713b 2018-01-19 stsp (*obj)->size = size;
617 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
618 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
620 b107e67f 2018-01-19 stsp return NULL;
623 b107e67f 2018-01-19 stsp static const struct got_error *
624 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
625 d7464085 2018-07-09 stsp off_t delta_offset)
627 b107e67f 2018-01-19 stsp int64_t o = 0;
628 b107e67f 2018-01-19 stsp uint8_t offN;
634 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
636 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
638 d7464085 2018-07-09 stsp if (pack->map) {
639 d7464085 2018-07-09 stsp size_t mapoff;
640 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
641 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
642 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
643 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
646 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
648 638f9024 2019-05-13 stsp return got_error_from_errno("read");
649 d7464085 2018-07-09 stsp if (n != sizeof(offN))
650 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
652 d7464085 2018-07-09 stsp *len += sizeof(offN);
654 b107e67f 2018-01-19 stsp if (i == 0)
655 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
659 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
662 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
664 b107e67f 2018-01-19 stsp *offset = o;
665 6ccb713b 2018-01-19 stsp return NULL;
668 6ccb713b 2018-01-19 stsp static const struct got_error *
669 d7464085 2018-07-09 stsp parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
670 d7464085 2018-07-09 stsp off_t offset, int tslen)
672 96f5e8b3 2018-01-23 stsp const struct got_error *err;
673 b107e67f 2018-01-19 stsp int64_t negoffset;
674 b107e67f 2018-01-19 stsp size_t negofflen;
678 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
679 d7464085 2018-07-09 stsp offset + tslen);
681 b107e67f 2018-01-19 stsp return err;
683 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
684 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
685 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
686 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
688 d7464085 2018-07-09 stsp *len = negofflen;
689 96f5e8b3 2018-01-23 stsp return NULL;
692 0e22967e 2018-02-11 stsp static const struct got_error *
693 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
694 c8ecd499 2018-09-09 stsp struct got_pack *, off_t, size_t, int, size_t, unsigned int);
696 96f5e8b3 2018-01-23 stsp static const struct got_error *
697 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
698 c336f889 2018-07-23 stsp int delta_type, size_t delta_size, size_t delta_data_offset,
699 c336f889 2018-07-23 stsp uint8_t *delta_buf, size_t delta_len)
701 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
703 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
704 c336f889 2018-07-23 stsp delta_data_offset, delta_buf,
705 a53d2f13 2018-03-17 stsp delta_len);
706 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
707 638f9024 2019-05-13 stsp return got_error_from_errno("got_delta_open");
708 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
709 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
710 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
711 bdd2fbb3 2018-02-11 stsp return NULL;
714 bdd2fbb3 2018-02-11 stsp static const struct got_error *
715 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
716 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
717 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
720 a3500804 2018-01-23 stsp const struct got_error *err;
721 c3703302 2018-01-23 stsp off_t base_offset;
722 c3703302 2018-01-23 stsp uint8_t base_type;
723 c3703302 2018-01-23 stsp uint64_t base_size;
724 c3703302 2018-01-23 stsp size_t base_tslen;
725 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
726 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
727 d7464085 2018-07-09 stsp size_t delta_len, consumed;
729 d7464085 2018-07-09 stsp err = parse_offset_delta(&base_offset, &consumed, pack,
730 d7464085 2018-07-09 stsp delta_offset, tslen);
732 a3500804 2018-01-23 stsp return err;
734 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
735 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
736 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
738 d7464085 2018-07-09 stsp if (pack->map == NULL) {
739 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
740 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
741 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
744 d7464085 2018-07-09 stsp if (pack->map) {
745 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
746 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
747 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
749 d7464085 2018-07-09 stsp return err;
752 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
754 d7464085 2018-07-09 stsp return err;
757 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
758 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
760 1a35c1bc 2019-05-22 stsp return err;
762 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
763 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
764 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
766 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
767 d7464085 2018-07-09 stsp pack, base_offset);
769 1a35c1bc 2019-05-22 stsp return err;
771 1a35c1bc 2019-05-22 stsp return resolve_delta_chain(deltas, packidx, pack, base_offset,
772 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
775 c3703302 2018-01-23 stsp static const struct got_error *
776 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
777 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
778 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
780 6b9c9673 2018-01-23 stsp const struct got_error *err;
781 6b9c9673 2018-01-23 stsp struct got_object_id id;
783 6b9c9673 2018-01-23 stsp off_t base_offset;
784 6b9c9673 2018-01-23 stsp uint8_t base_type;
785 6b9c9673 2018-01-23 stsp uint64_t base_size;
786 6b9c9673 2018-01-23 stsp size_t base_tslen;
787 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
788 1a35c1bc 2019-05-22 stsp uint8_t *delta_buf;
789 a53d2f13 2018-03-17 stsp size_t delta_len;
791 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
792 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
793 45202a8f 2018-07-11 stsp delta_data_offset = delta_offset + tslen;
794 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
795 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
797 d7464085 2018-07-09 stsp if (pack->map == NULL) {
798 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
799 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
800 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
803 d7464085 2018-07-09 stsp if (pack->map) {
804 45202a8f 2018-07-11 stsp size_t mapoff = (size_t)delta_data_offset;
805 d7464085 2018-07-09 stsp memcpy(&id, pack->map + mapoff, sizeof(id));
806 d7464085 2018-07-09 stsp mapoff += sizeof(id);
807 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
808 45202a8f 2018-07-11 stsp mapoff, pack->filesize - mapoff);
810 1a35c1bc 2019-05-22 stsp return err;
812 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &id, sizeof(id));
814 638f9024 2019-05-13 stsp return got_error_from_errno("read");
815 d7464085 2018-07-09 stsp if (n != sizeof(id))
816 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
817 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
819 1a35c1bc 2019-05-22 stsp return err;
822 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
823 c336f889 2018-07-23 stsp delta_data_offset, delta_buf, delta_len);
825 1a35c1bc 2019-05-22 stsp return err;
827 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
828 1510f469 2018-09-09 stsp idx = got_packidx_get_object_idx(packidx, &id);
829 1a35c1bc 2019-05-22 stsp if (idx == -1)
830 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_BAD_PACKFILE);
832 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
833 1a35c1bc 2019-05-22 stsp if (base_offset == (uint64_t)-1)
834 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_BAD_PACKIDX);
836 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
837 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
839 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
840 d7464085 2018-07-09 stsp pack, base_offset);
842 1a35c1bc 2019-05-22 stsp return err;
844 1a35c1bc 2019-05-22 stsp return resolve_delta_chain(deltas, packidx, pack, base_offset,
845 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
848 6b9c9673 2018-01-23 stsp static const struct got_error *
849 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
850 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
851 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
853 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
855 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
856 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
858 c3703302 2018-01-23 stsp switch (delta_type) {
859 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
860 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
861 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
862 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
863 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
864 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
865 c336f889 2018-07-23 stsp delta_size, 0, NULL, 0);
867 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
868 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
869 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
871 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
872 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
873 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
876 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
879 96f5e8b3 2018-01-23 stsp return err;
882 96f5e8b3 2018-01-23 stsp static const struct got_error *
883 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
884 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
885 c59b3346 2018-09-11 stsp size_t tslen, int delta_type, size_t delta_size, int idx)
887 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
888 96f5e8b3 2018-01-23 stsp int resolved_type;
890 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
891 b107e67f 2018-01-19 stsp if (*obj == NULL)
892 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
894 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
895 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
896 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
897 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
898 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
900 1a35c1bc 2019-05-22 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
901 1a35c1bc 2019-05-22 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
903 b0e2201a 2018-06-22 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
904 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
905 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
908 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
909 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
911 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
912 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
916 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
919 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
922 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
923 96f5e8b3 2018-01-23 stsp *obj = NULL;
925 96f5e8b3 2018-01-23 stsp return err;
928 2090a03d 2018-09-09 stsp const struct got_error *
929 2090a03d 2018-09-09 stsp got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
930 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
932 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
933 a1fd68d8 2018-01-12 stsp off_t offset;
934 6c00b545 2018-01-17 stsp uint8_t type;
935 6c00b545 2018-01-17 stsp uint64_t size;
936 3ee5fc21 2018-01-17 stsp size_t tslen;
938 6c00b545 2018-01-17 stsp *obj = NULL;
940 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
941 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
942 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
944 d7464085 2018-07-09 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
946 35c73765 2018-09-09 stsp return err;
948 6c00b545 2018-01-17 stsp switch (type) {
949 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
950 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
951 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
952 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
953 35c73765 2018-09-09 stsp err = open_plain_object(obj, pack->path_packfile, id, type,
954 c59b3346 2018-09-11 stsp offset + tslen, size, idx);
956 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
957 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
958 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
959 c59b3346 2018-09-11 stsp tslen, type, size, idx);
962 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
966 3ee5fc21 2018-01-17 stsp return err;
969 efd2a263 2018-01-19 stsp static const struct got_error *
970 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
972 22484865 2018-03-13 stsp struct got_delta *delta;
973 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
975 22484865 2018-03-13 stsp *max_size = 0;
976 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
977 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
978 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
979 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
980 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
981 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
982 22484865 2018-03-13 stsp const struct got_error *err;
983 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
984 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
986 22484865 2018-03-13 stsp return err;
988 22484865 2018-03-13 stsp base_size = delta->size;
989 22484865 2018-03-13 stsp if (base_size > *max_size)
990 22484865 2018-03-13 stsp *max_size = base_size;
991 22484865 2018-03-13 stsp if (result_size > *max_size)
992 22484865 2018-03-13 stsp *max_size = result_size;
995 9feb4ff2 2018-03-14 stsp return NULL;
998 ac544f8c 2019-01-13 stsp const struct got_error *
999 85a703fa 2019-01-13 stsp got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj)
1001 85a703fa 2019-01-13 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1002 85a703fa 2019-01-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1004 ac544f8c 2019-01-13 stsp return get_delta_chain_max_size(size, &obj->deltas);
1007 22484865 2018-03-13 stsp static const struct got_error *
1008 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1009 3840f4c9 2018-09-12 stsp struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
1011 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1012 6691714a 2018-01-23 stsp struct got_delta *delta;
1013 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1014 34fca9c3 2018-11-11 stsp size_t base_bufsz = 0, accum_size = 0;
1015 22484865 2018-03-13 stsp uint64_t max_size;
1016 6691714a 2018-01-23 stsp int n = 0;
1018 b29656e2 2018-03-16 stsp *result_size = 0;
1020 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1021 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1023 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1024 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1026 22484865 2018-03-13 stsp return err;
1027 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1028 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1029 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1030 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1031 3840f4c9 2018-09-12 stsp base_file = NULL;
1032 3840f4c9 2018-09-12 stsp accum_file = NULL;
1035 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1036 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1037 a6b158cc 2018-02-11 stsp if (n == 0) {
1038 34fca9c3 2018-11-11 stsp size_t mapoff;
1039 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1041 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1042 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1043 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1044 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1045 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1046 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1047 72eb3431 2018-04-01 stsp goto done;
1050 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1051 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1052 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1053 0c048b15 2018-04-27 stsp goto done;
1055 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1056 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1058 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1059 d7464085 2018-07-09 stsp goto done;
1062 d7464085 2018-07-09 stsp if (base_file) {
1063 d7464085 2018-07-09 stsp if (pack->map) {
1064 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1065 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1066 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1067 d7464085 2018-07-09 stsp pack->filesize - mapoff, base_file);
1069 34fca9c3 2018-11-11 stsp err = got_inflate_to_file_fd(
1070 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd, base_file);
1072 d7464085 2018-07-09 stsp if (pack->map) {
1073 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1074 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1075 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1076 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1078 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1079 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd);
1082 a6b158cc 2018-02-11 stsp goto done;
1084 8628c62d 2018-03-15 stsp if (base_file)
1085 8628c62d 2018-03-15 stsp rewind(base_file);
1089 8628c62d 2018-03-15 stsp if (base_buf) {
1090 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1091 34fca9c3 2018-11-11 stsp delta->delta_buf, delta->delta_len, accum_buf,
1092 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1095 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1096 a53d2f13 2018-03-17 stsp delta->delta_len,
1097 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1098 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1099 b29656e2 2018-03-16 stsp &accum_size);
1102 6691714a 2018-01-23 stsp goto done;
1104 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1105 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1106 8628c62d 2018-03-15 stsp if (base_buf) {
1107 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1109 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation
1110 34fca9c3 2018-11-11 stsp * buffer. Ensure it can hold the largest
1111 34fca9c3 2018-11-11 stsp * result in the delta chain. The initial
1112 34fca9c3 2018-11-11 stsp * allocation might have been smaller.
1114 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1115 34fca9c3 2018-11-11 stsp uint8_t *p;
1116 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1117 34fca9c3 2018-11-11 stsp if (p == NULL) {
1118 638f9024 2019-05-13 stsp err = got_error_from_errno(
1119 230a42bd 2019-05-11 jcs "reallocarray");
1120 34fca9c3 2018-11-11 stsp goto done;
1122 34fca9c3 2018-11-11 stsp base_buf = p;
1123 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1125 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1126 8628c62d 2018-03-15 stsp base_buf = tmp;
1128 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1129 8628c62d 2018-03-15 stsp accum_file = base_file;
1130 8628c62d 2018-03-15 stsp base_file = tmp;
1131 8628c62d 2018-03-15 stsp rewind(base_file);
1132 8628c62d 2018-03-15 stsp rewind(accum_file);
1138 8628c62d 2018-03-15 stsp free(base_buf);
1139 8628c62d 2018-03-15 stsp if (accum_buf) {
1140 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1141 8628c62d 2018-03-15 stsp free(accum_buf);
1142 8628c62d 2018-03-15 stsp if (len != accum_size)
1143 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1145 6691714a 2018-01-23 stsp rewind(outfile);
1146 b29656e2 2018-03-16 stsp if (err == NULL)
1147 b29656e2 2018-03-16 stsp *result_size = accum_size;
1148 e0ab43e7 2018-03-16 stsp return err;
1151 e0ab43e7 2018-03-16 stsp static const struct got_error *
1152 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1153 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1155 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1156 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1157 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1158 34fca9c3 2018-11-11 stsp size_t base_bufsz = 0, accum_size = 0;
1159 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1160 e0ab43e7 2018-03-16 stsp int n = 0;
1162 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1163 e0ab43e7 2018-03-16 stsp *outlen = 0;
1165 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1166 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1168 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1170 e0ab43e7 2018-03-16 stsp return err;
1171 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1172 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1173 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1175 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1176 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1177 e0ab43e7 2018-03-16 stsp if (n == 0) {
1178 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1180 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1181 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1182 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1183 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1184 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1185 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1186 72eb3431 2018-04-01 stsp goto done;
1189 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1190 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1191 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1192 0c048b15 2018-04-27 stsp goto done;
1194 d7464085 2018-07-09 stsp if (pack->map) {
1195 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1196 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1197 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1198 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1200 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1202 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1203 d7464085 2018-07-09 stsp goto done;
1205 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1206 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd);
1209 d7464085 2018-07-09 stsp goto done;
1214 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1215 34fca9c3 2018-11-11 stsp delta->delta_buf, delta->delta_len, accum_buf,
1216 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1219 e0ab43e7 2018-03-16 stsp goto done;
1221 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1222 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1223 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1225 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation buffer.
1226 34fca9c3 2018-11-11 stsp * Ensure it can hold the largest result in the delta
1227 34fca9c3 2018-11-11 stsp * chain. Initial allocation might have been smaller.
1229 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1230 34fca9c3 2018-11-11 stsp uint8_t *p;
1231 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1232 34fca9c3 2018-11-11 stsp if (p == NULL) {
1233 638f9024 2019-05-13 stsp err = got_error_from_errno(
1234 230a42bd 2019-05-11 jcs "reallocarray");
1235 34fca9c3 2018-11-11 stsp goto done;
1237 34fca9c3 2018-11-11 stsp base_buf = p;
1238 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1240 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1241 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1246 e0ab43e7 2018-03-16 stsp free(base_buf);
1247 e0ab43e7 2018-03-16 stsp if (err) {
1248 e0ab43e7 2018-03-16 stsp free(accum_buf);
1249 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1250 e0ab43e7 2018-03-16 stsp *outlen = 0;
1252 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1253 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1255 efd2a263 2018-01-19 stsp return err;
1258 3ee5fc21 2018-01-17 stsp const struct got_error *
1259 24140570 2018-09-09 stsp got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1260 3840f4c9 2018-09-12 stsp FILE *outfile, FILE *base_file, FILE *accum_file)
1262 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1264 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1265 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1267 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1268 24140570 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1269 24140570 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1271 d7464085 2018-07-09 stsp if (pack->map) {
1272 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1273 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(&obj->size, pack->map,
1274 24140570 2018-09-09 stsp mapoff, pack->filesize - mapoff, outfile);
1276 24140570 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1277 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1278 24140570 2018-09-09 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd,
1282 2ce68b2f 2018-09-09 stsp err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1283 3840f4c9 2018-09-12 stsp outfile, base_file, accum_file);
1285 a1fd68d8 2018-01-12 stsp return err;
1288 e0ab43e7 2018-03-16 stsp const struct got_error *
1289 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1290 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_pack *pack)
1292 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1294 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1295 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1297 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1298 7e212e3d 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1299 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1300 d7464085 2018-07-09 stsp if (pack->map) {
1301 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1302 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(buf, len, pack->map,
1303 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
1305 7e212e3d 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1306 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1307 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1310 48095039 2018-09-09 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
1312 e0ab43e7 2018-03-16 stsp return err;