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 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
43 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
45 dd88155e 2019-06-29 stsp #include "got_lib_object_parse.h"
46 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
47 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
49 79b11c62 2018-03-09 stsp #ifndef nitems
50 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 0a0a3048 2018-01-10 stsp static const struct got_error *
58 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
62 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
63 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
64 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
67 0a0a3048 2018-01-10 stsp return NULL;
70 1510f469 2018-09-09 stsp const struct got_error *
71 817c5a18 2018-09-09 stsp got_packidx_init_hdr(struct got_packidx *p, int verify)
73 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
74 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
75 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
76 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
77 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
81 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
83 57b35b75 2018-06-22 stsp h = &p->hdr;
85 57b35b75 2018-06-22 stsp remain = p->len;
87 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
88 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
92 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
94 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
95 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
99 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
100 b1317e77 2019-09-22 stsp if (n < 0) {
101 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
103 b1317e77 2019-09-22 stsp } else if (n != sizeof(*h->magic)) {
104 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
108 57b35b75 2018-06-22 stsp if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
109 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
112 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
113 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
115 0cb74cf4 2018-07-08 stsp if (verify)
116 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
118 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
119 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
122 fc79a48d 2018-07-09 stsp if (p->map)
123 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
125 fc79a48d 2018-07-09 stsp h->version = malloc(sizeof(*h->version));
126 fc79a48d 2018-07-09 stsp if (h->version == NULL) {
127 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
130 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
131 c6368c2e 2019-10-11 stsp if (n < 0) {
132 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
134 c6368c2e 2019-10-11 stsp } else if (n != sizeof(*h->version)) {
135 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
139 57b35b75 2018-06-22 stsp if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
140 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
143 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
144 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
146 0cb74cf4 2018-07-08 stsp if (verify)
147 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
149 57b35b75 2018-06-22 stsp len_fanout =
150 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
151 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
152 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
155 fc79a48d 2018-07-09 stsp if (p->map)
156 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
158 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
159 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
160 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
163 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
164 c6368c2e 2019-10-11 stsp if (n < 0) {
165 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
167 c6368c2e 2019-10-11 stsp } else if (n != len_fanout) {
168 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
172 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
175 0cb74cf4 2018-07-08 stsp if (verify)
176 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
177 57b35b75 2018-06-22 stsp offset += len_fanout;
178 57b35b75 2018-06-22 stsp remain -= len_fanout;
180 c8262310 2018-06-04 stsp nobj = betoh32(h->fanout_table[0xff]);
181 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
182 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
183 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
186 fc79a48d 2018-07-09 stsp if (p->map)
187 fc79a48d 2018-07-09 stsp h->sorted_ids =
188 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
190 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
191 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
192 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
195 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
197 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
198 faaa1c0f 2018-09-15 stsp else if (n != len_ids) {
199 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
203 0cb74cf4 2018-07-08 stsp if (verify)
204 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
205 57b35b75 2018-06-22 stsp offset += len_ids;
206 57b35b75 2018-06-22 stsp remain -= len_ids;
208 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
209 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
212 fc79a48d 2018-07-09 stsp if (p->map)
213 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
215 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
216 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
217 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
220 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
222 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
223 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->crc32)) {
224 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
228 0cb74cf4 2018-07-08 stsp if (verify)
229 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
230 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->crc32);
231 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->crc32);
233 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->offsets)) {
234 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
237 fc79a48d 2018-07-09 stsp if (p->map)
238 fc79a48d 2018-07-09 stsp h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
240 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->offsets));
241 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
242 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
245 fc79a48d 2018-07-09 stsp n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
247 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
248 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->offsets)) {
249 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
253 0cb74cf4 2018-07-08 stsp if (verify)
254 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
255 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
256 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
257 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
259 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
260 5e6be232 2019-11-08 stsp for (i = 0; i < nobj; i++) {
261 5e6be232 2019-11-08 stsp uint32_t o = betoh32(h->offsets[i]);
262 5e6be232 2019-11-08 stsp if (o & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
263 5e6be232 2019-11-08 stsp p->nlargeobj++;
265 5e6be232 2019-11-08 stsp if (p->nlargeobj == 0)
266 0a0a3048 2018-01-10 stsp goto checksum;
268 5e6be232 2019-11-08 stsp if (remain < p->nlargeobj * sizeof(*h->large_offsets)) {
269 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
272 fc79a48d 2018-07-09 stsp if (p->map)
273 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
275 5e6be232 2019-11-08 stsp h->large_offsets = malloc(p->nlargeobj *
276 5e6be232 2019-11-08 stsp sizeof(*h->large_offsets));
277 de30857e 2019-08-23 stsp if (h->large_offsets == NULL) {
278 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
281 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
282 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
284 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
285 5e6be232 2019-11-08 stsp else if (n != p->nlargeobj * sizeof(*h->large_offsets)) {
286 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
290 0cb74cf4 2018-07-08 stsp if (verify)
291 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
292 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
293 5e6be232 2019-11-08 stsp remain -= p->nlargeobj * sizeof(*h->large_offsets);
294 5e6be232 2019-11-08 stsp offset += p->nlargeobj * sizeof(*h->large_offsets);
297 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
298 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
301 fc79a48d 2018-07-09 stsp if (p->map)
302 fc79a48d 2018-07-09 stsp h->trailer =
303 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
305 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
306 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
307 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
310 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
312 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
313 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->trailer)) {
314 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
318 0cb74cf4 2018-07-08 stsp if (verify) {
319 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
320 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
321 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
322 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
323 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
326 817c5a18 2018-09-09 stsp return err;
329 817c5a18 2018-09-09 stsp const struct got_error *
330 817c5a18 2018-09-09 stsp got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
332 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
333 817c5a18 2018-09-09 stsp struct got_packidx *p;
334 57377f07 2019-05-23 stsp struct stat sb;
336 817c5a18 2018-09-09 stsp *packidx = NULL;
338 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
339 817c5a18 2018-09-09 stsp if (p == NULL)
340 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
342 a5b57ccf 2019-04-11 stsp p->fd = open(path, O_RDONLY | O_NOFOLLOW);
343 6772cf22 2019-08-28 hiltjo if (p->fd == -1) {
344 6772cf22 2019-08-28 hiltjo err = got_error_from_errno2("open", path);
346 6772cf22 2019-08-28 hiltjo return err;
349 57377f07 2019-05-23 stsp if (fstat(p->fd, &sb) != 0) {
350 57377f07 2019-05-23 stsp err = got_error_from_errno2("fstat", path);
351 817c5a18 2018-09-09 stsp close(p->fd);
353 817c5a18 2018-09-09 stsp return err;
355 57377f07 2019-05-23 stsp p->len = sb.st_size;
356 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
357 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
358 817c5a18 2018-09-09 stsp close(p->fd);
360 817c5a18 2018-09-09 stsp return err;
363 817c5a18 2018-09-09 stsp p->path_packidx = strdup(path);
364 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
365 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
369 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
370 817c5a18 2018-09-09 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
371 3a11398b 2019-02-21 stsp if (p->map == MAP_FAILED) {
372 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
373 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
376 817c5a18 2018-09-09 stsp p->map = NULL; /* fall back to read(2) */
380 817c5a18 2018-09-09 stsp err = got_packidx_init_hdr(p, verify);
383 0a0a3048 2018-01-10 stsp got_packidx_close(p);
385 0a0a3048 2018-01-10 stsp *packidx = p;
387 0a0a3048 2018-01-10 stsp return err;
390 57b35b75 2018-06-22 stsp const struct got_error *
391 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
393 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
395 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
396 fc79a48d 2018-07-09 stsp if (packidx->map) {
397 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
398 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
400 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
401 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
402 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
403 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
404 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
405 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
406 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
407 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
409 3a6ce05a 2019-02-11 stsp if (close(packidx->fd) != 0 && err == NULL)
410 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
411 0a0a3048 2018-01-10 stsp free(packidx);
413 57b35b75 2018-06-22 stsp return err;
416 a1fd68d8 2018-01-12 stsp static off_t
417 6fd11751 2018-06-04 stsp get_object_offset(struct got_packidx *packidx, int idx)
419 6fd11751 2018-06-04 stsp uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
420 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
421 a1fd68d8 2018-01-12 stsp uint64_t loffset;
422 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
423 5e6be232 2019-11-08 stsp if (idx < 0 || idx >= packidx->nlargeobj ||
424 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
426 6fd11751 2018-06-04 stsp loffset = betoh64(packidx->hdr.large_offsets[idx]);
427 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
429 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
433 1510f469 2018-09-09 stsp got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
435 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
436 6fd11751 2018-06-04 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
437 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
439 a1fd68d8 2018-01-12 stsp if (id0 > 0)
440 40aeb19c 2018-06-22 stsp left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
442 40aeb19c 2018-06-22 stsp while (left <= right) {
443 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
444 40aeb19c 2018-06-22 stsp int i, cmp;
446 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
447 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
448 57b35b75 2018-06-22 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
449 16dcbf91 2018-04-01 stsp if (cmp == 0)
451 40aeb19c 2018-06-22 stsp else if (cmp > 0)
452 40aeb19c 2018-06-22 stsp left = i + 1;
453 40aeb19c 2018-06-22 stsp else if (cmp < 0)
454 40aeb19c 2018-06-22 stsp right = i - 1;
460 876c234b 2018-09-10 stsp const struct got_error *
461 dd88155e 2019-06-29 stsp got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
462 4277420a 2019-06-29 stsp struct got_packidx *packidx, const char *id_str_prefix)
464 dd88155e 2019-06-29 stsp const struct got_error *err = NULL;
465 4277420a 2019-06-29 stsp u_int8_t id0;
466 4277420a 2019-06-29 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
467 4277420a 2019-06-29 stsp char hex[3];
468 4277420a 2019-06-29 stsp size_t prefix_len = strlen(id_str_prefix);
469 e09a504c 2019-06-28 stsp struct got_packidx_object_id *oid;
472 dd88155e 2019-06-29 stsp SIMPLEQ_INIT(matched_ids);
474 4277420a 2019-06-29 stsp if (prefix_len < 2)
475 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
477 4277420a 2019-06-29 stsp hex[0] = id_str_prefix[0];
478 4277420a 2019-06-29 stsp hex[1] = id_str_prefix[1];
479 4277420a 2019-06-29 stsp hex[2] = '\0';
480 4277420a 2019-06-29 stsp if (!got_parse_xdigit(&id0, hex))
481 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
483 4277420a 2019-06-29 stsp i = betoh32(packidx->hdr.fanout_table[id0 - 1]);
484 4277420a 2019-06-29 stsp if (i == 0)
485 4277420a 2019-06-29 stsp return NULL;
487 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[i];
488 4277420a 2019-06-29 stsp while (i < totobj && oid->sha1[0] == id0) {
489 4277420a 2019-06-29 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
490 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
493 4277420a 2019-06-29 stsp if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
494 4277420a 2019-06-29 stsp return got_error(GOT_ERR_NO_SPACE);
496 4277420a 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, prefix_len);
497 4277420a 2019-06-29 stsp if (cmp < 0) {
498 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
500 4277420a 2019-06-29 stsp } else if (cmp > 0)
503 dd88155e 2019-06-29 stsp err = got_object_qid_alloc_partial(&qid);
506 dd88155e 2019-06-29 stsp memcpy(qid->id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
507 dd88155e 2019-06-29 stsp SIMPLEQ_INSERT_TAIL(matched_ids, qid, entry);
509 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
513 0adc7bcc 2019-06-29 stsp got_object_id_queue_free(matched_ids);
514 dd88155e 2019-06-29 stsp return err;
517 e09a504c 2019-06-28 stsp const struct got_error *
518 876c234b 2018-09-10 stsp got_pack_stop_privsep_child(struct got_pack *pack)
520 96732e0b 2018-11-11 stsp const struct got_error *err = NULL;
522 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL)
523 876c234b 2018-09-10 stsp return NULL;
525 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
527 96732e0b 2018-11-11 stsp return err;
528 96732e0b 2018-11-11 stsp err = got_privsep_wait_for_child(pack->privsep_child->pid);
529 0dd5271b 2019-05-10 stsp if (close(pack->privsep_child->imsg_fd) != 0 && err == NULL)
530 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
531 876c234b 2018-09-10 stsp free(pack->privsep_child);
532 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
533 876c234b 2018-09-10 stsp return err;
536 d7464085 2018-07-09 stsp const struct got_error *
537 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
539 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
541 876c234b 2018-09-10 stsp err = got_pack_stop_privsep_child(pack);
542 876c234b 2018-09-10 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
543 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
544 3a6ce05a 2019-02-11 stsp if (pack->fd != -1 && close(pack->fd) != 0 && err == NULL)
545 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
546 8b2180d4 2018-04-26 stsp pack->fd = -1;
547 4589e373 2018-03-17 stsp free(pack->path_packfile);
548 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
549 4589e373 2018-03-17 stsp pack->filesize = 0;
550 ab2f42e7 2019-11-10 stsp if (pack->delta_cache) {
551 ab2f42e7 2019-11-10 stsp got_delta_cache_free(pack->delta_cache);
552 ab2f42e7 2019-11-10 stsp pack->delta_cache = NULL;
555 c7fe698a 2018-01-23 stsp return err;
558 c7fe698a 2018-01-23 stsp static const struct got_error *
559 d7464085 2018-07-09 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
560 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
562 a487c1d0 2018-01-14 stsp uint8_t t = 0;
563 a487c1d0 2018-01-14 stsp uint64_t s = 0;
564 a487c1d0 2018-01-14 stsp uint8_t sizeN;
565 d7464085 2018-07-09 stsp size_t mapoff = 0;
570 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
571 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
573 d7464085 2018-07-09 stsp if (pack->map) {
574 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
576 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
577 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
581 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
583 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
585 d7464085 2018-07-09 stsp if (pack->map) {
586 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
587 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
589 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
591 638f9024 2019-05-13 stsp return got_error_from_errno("read");
592 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
593 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
595 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
597 a1fd68d8 2018-01-12 stsp if (i == 0) {
598 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
599 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
600 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
602 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
603 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
606 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
610 a487c1d0 2018-01-14 stsp return NULL;
613 a1fd68d8 2018-01-12 stsp static const struct got_error *
614 5f25cc85 2019-11-26 stsp open_plain_object(struct got_object **obj, struct got_object_id *id,
615 5f25cc85 2019-11-26 stsp uint8_t type, off_t offset, size_t size, int idx)
617 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
618 6ccb713b 2018-01-19 stsp if (*obj == NULL)
619 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
621 6ccb713b 2018-01-19 stsp (*obj)->type = type;
622 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
623 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
624 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
625 6ccb713b 2018-01-19 stsp (*obj)->size = size;
626 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
627 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
629 b107e67f 2018-01-19 stsp return NULL;
632 b107e67f 2018-01-19 stsp static const struct got_error *
633 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
634 d7464085 2018-07-09 stsp off_t delta_offset)
636 b107e67f 2018-01-19 stsp int64_t o = 0;
637 b107e67f 2018-01-19 stsp uint8_t offN;
640 a0de39f3 2019-08-09 stsp *offset = 0;
644 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
646 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
648 d7464085 2018-07-09 stsp if (pack->map) {
649 d7464085 2018-07-09 stsp size_t mapoff;
650 d7464085 2018-07-09 stsp if (delta_offset >= pack->filesize)
651 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
652 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
653 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
656 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
658 638f9024 2019-05-13 stsp return got_error_from_errno("read");
659 d7464085 2018-07-09 stsp if (n != sizeof(offN))
660 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
662 d7464085 2018-07-09 stsp *len += sizeof(offN);
664 b107e67f 2018-01-19 stsp if (i == 0)
665 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
669 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
672 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
674 b107e67f 2018-01-19 stsp *offset = o;
675 6ccb713b 2018-01-19 stsp return NULL;
678 6ccb713b 2018-01-19 stsp static const struct got_error *
679 d7464085 2018-07-09 stsp parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
680 d7464085 2018-07-09 stsp off_t offset, int tslen)
682 96f5e8b3 2018-01-23 stsp const struct got_error *err;
683 b107e67f 2018-01-19 stsp int64_t negoffset;
684 b107e67f 2018-01-19 stsp size_t negofflen;
688 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
689 d7464085 2018-07-09 stsp offset + tslen);
691 b107e67f 2018-01-19 stsp return err;
693 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
694 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
695 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
696 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
698 d7464085 2018-07-09 stsp *len = negofflen;
699 96f5e8b3 2018-01-23 stsp return NULL;
702 0e22967e 2018-02-11 stsp static const struct got_error *
703 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
704 c8ecd499 2018-09-09 stsp struct got_pack *, off_t, size_t, int, size_t, unsigned int);
706 42c69117 2019-11-10 stsp static const struct got_error *
707 42c69117 2019-11-10 stsp read_delta_data(uint8_t **delta_buf, size_t *delta_len,
708 42c69117 2019-11-10 stsp size_t delta_data_offset, struct got_pack *pack)
710 42c69117 2019-11-10 stsp const struct got_error *err = NULL;
712 42c69117 2019-11-10 stsp if (pack->map) {
713 42c69117 2019-11-10 stsp if (delta_data_offset >= pack->filesize)
714 42c69117 2019-11-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
715 42c69117 2019-11-10 stsp err = got_inflate_to_mem_mmap(delta_buf, delta_len, pack->map,
716 42c69117 2019-11-10 stsp delta_data_offset, pack->filesize - delta_data_offset);
718 42c69117 2019-11-10 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
719 42c69117 2019-11-10 stsp return got_error_from_errno("lseek");
720 42c69117 2019-11-10 stsp err = got_inflate_to_mem_fd(delta_buf, delta_len, pack->fd);
722 42c69117 2019-11-10 stsp return err;
725 96f5e8b3 2018-01-23 stsp static const struct got_error *
726 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
727 42c69117 2019-11-10 stsp int delta_type, size_t delta_size, size_t delta_data_offset)
729 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
731 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
732 42c69117 2019-11-10 stsp delta_data_offset);
733 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
734 638f9024 2019-05-13 stsp return got_error_from_errno("got_delta_open");
735 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
736 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
737 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
738 bdd2fbb3 2018-02-11 stsp return NULL;
741 bdd2fbb3 2018-02-11 stsp static const struct got_error *
742 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
743 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
744 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
747 a3500804 2018-01-23 stsp const struct got_error *err;
748 c3703302 2018-01-23 stsp off_t base_offset;
749 c3703302 2018-01-23 stsp uint8_t base_type;
750 c3703302 2018-01-23 stsp uint64_t base_size;
751 c3703302 2018-01-23 stsp size_t base_tslen;
752 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
753 42c69117 2019-11-10 stsp size_t consumed;
755 d7464085 2018-07-09 stsp err = parse_offset_delta(&base_offset, &consumed, pack,
756 d7464085 2018-07-09 stsp delta_offset, tslen);
758 a3500804 2018-01-23 stsp return err;
760 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
761 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
762 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
764 d7464085 2018-07-09 stsp if (pack->map == NULL) {
765 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
766 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
767 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
770 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
771 42c69117 2019-11-10 stsp delta_data_offset);
773 1a35c1bc 2019-05-22 stsp return err;
775 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
776 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
777 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
779 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
780 d7464085 2018-07-09 stsp pack, base_offset);
782 1a35c1bc 2019-05-22 stsp return err;
784 1a35c1bc 2019-05-22 stsp return resolve_delta_chain(deltas, packidx, pack, base_offset,
785 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
788 c3703302 2018-01-23 stsp static const struct got_error *
789 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
790 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
791 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
793 6b9c9673 2018-01-23 stsp const struct got_error *err;
794 6b9c9673 2018-01-23 stsp struct got_object_id id;
796 6b9c9673 2018-01-23 stsp off_t base_offset;
797 6b9c9673 2018-01-23 stsp uint8_t base_type;
798 6b9c9673 2018-01-23 stsp uint64_t base_size;
799 6b9c9673 2018-01-23 stsp size_t base_tslen;
800 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
801 1a35c1bc 2019-05-22 stsp uint8_t *delta_buf;
802 a53d2f13 2018-03-17 stsp size_t delta_len;
804 42c69117 2019-11-10 stsp if (delta_offset + tslen >= pack->filesize)
805 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
807 d7464085 2018-07-09 stsp if (pack->map == NULL) {
810 d7464085 2018-07-09 stsp if (pack->map) {
811 42c69117 2019-11-10 stsp size_t mapoff = delta_offset + tslen;
812 d7464085 2018-07-09 stsp memcpy(&id, pack->map + mapoff, sizeof(id));
813 d7464085 2018-07-09 stsp mapoff += sizeof(id);
814 42c69117 2019-11-10 stsp delta_data_offset = (off_t)mapoff;
815 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&delta_buf, &delta_len, pack->map,
816 45202a8f 2018-07-11 stsp mapoff, pack->filesize - mapoff);
818 1a35c1bc 2019-05-22 stsp return err;
821 42c69117 2019-11-10 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
822 42c69117 2019-11-10 stsp if (delta_data_offset == -1)
823 42c69117 2019-11-10 stsp return got_error_from_errno("lseek");
824 42c69117 2019-11-10 stsp n = read(pack->fd, &id, sizeof(id));
826 638f9024 2019-05-13 stsp return got_error_from_errno("read");
827 d7464085 2018-07-09 stsp if (n != sizeof(id))
828 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
829 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&delta_buf, &delta_len, pack->fd);
831 1a35c1bc 2019-05-22 stsp return err;
834 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
835 42c69117 2019-11-10 stsp delta_data_offset);
837 1a35c1bc 2019-05-22 stsp return err;
839 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
840 1510f469 2018-09-09 stsp idx = got_packidx_get_object_idx(packidx, &id);
841 1a35c1bc 2019-05-22 stsp if (idx == -1)
842 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_BAD_PACKFILE);
844 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
845 1a35c1bc 2019-05-22 stsp if (base_offset == (uint64_t)-1)
846 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_BAD_PACKIDX);
848 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
849 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
851 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
852 d7464085 2018-07-09 stsp pack, base_offset);
854 1a35c1bc 2019-05-22 stsp return err;
856 1a35c1bc 2019-05-22 stsp return resolve_delta_chain(deltas, packidx, pack, base_offset,
857 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
860 6b9c9673 2018-01-23 stsp static const struct got_error *
861 c8ecd499 2018-09-09 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
862 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
863 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
865 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
867 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
868 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
870 c3703302 2018-01-23 stsp switch (delta_type) {
871 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
872 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
873 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
874 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
875 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
876 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
877 42c69117 2019-11-10 stsp delta_size, 0);
879 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
880 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
881 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
883 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
884 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
885 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
888 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
891 96f5e8b3 2018-01-23 stsp return err;
894 96f5e8b3 2018-01-23 stsp static const struct got_error *
895 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
896 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
897 c59b3346 2018-09-11 stsp size_t tslen, int delta_type, size_t delta_size, int idx)
899 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
900 96f5e8b3 2018-01-23 stsp int resolved_type;
902 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
903 b107e67f 2018-01-19 stsp if (*obj == NULL)
904 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
906 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
907 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
908 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
909 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
910 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
912 1a35c1bc 2019-05-22 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
913 1a35c1bc 2019-05-22 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
914 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
915 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
917 c8ecd499 2018-09-09 stsp err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
918 b0e2201a 2018-06-22 stsp tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
922 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
925 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
928 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
929 96f5e8b3 2018-01-23 stsp *obj = NULL;
931 96f5e8b3 2018-01-23 stsp return err;
934 2090a03d 2018-09-09 stsp const struct got_error *
935 2090a03d 2018-09-09 stsp got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
936 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
938 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
939 a1fd68d8 2018-01-12 stsp off_t offset;
940 6c00b545 2018-01-17 stsp uint8_t type;
941 6c00b545 2018-01-17 stsp uint64_t size;
942 3ee5fc21 2018-01-17 stsp size_t tslen;
944 6c00b545 2018-01-17 stsp *obj = NULL;
946 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
947 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
948 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
950 d7464085 2018-07-09 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
952 35c73765 2018-09-09 stsp return err;
954 6c00b545 2018-01-17 stsp switch (type) {
955 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
956 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
957 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
958 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
959 5f25cc85 2019-11-26 stsp err = open_plain_object(obj, id, type, offset + tslen,
960 5f25cc85 2019-11-26 stsp size, idx);
962 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
963 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
964 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
965 c59b3346 2018-09-11 stsp tslen, type, size, idx);
968 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
972 3ee5fc21 2018-01-17 stsp return err;
975 efd2a263 2018-01-19 stsp static const struct got_error *
976 42c69117 2019-11-10 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas,
977 42c69117 2019-11-10 stsp struct got_pack *pack)
979 22484865 2018-03-13 stsp struct got_delta *delta;
980 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
982 22484865 2018-03-13 stsp *max_size = 0;
983 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
984 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
985 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
986 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
987 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
988 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
989 22484865 2018-03-13 stsp const struct got_error *err;
990 42c69117 2019-11-10 stsp uint8_t *delta_buf;
991 42c69117 2019-11-10 stsp size_t delta_len;
992 ab2f42e7 2019-11-10 stsp int cached = 1;
994 ab2f42e7 2019-11-10 stsp got_delta_cache_get(&delta_buf, &delta_len,
995 ab2f42e7 2019-11-10 stsp pack->delta_cache, delta->data_offset);
996 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
997 ab2f42e7 2019-11-10 stsp cached = 0;
998 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
999 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1001 ab2f42e7 2019-11-10 stsp return err;
1002 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1003 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1004 ab2f42e7 2019-11-10 stsp if (err == NULL)
1005 ab2f42e7 2019-11-10 stsp cached = 1;
1006 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1007 ab2f42e7 2019-11-10 stsp free(delta_buf);
1008 ab2f42e7 2019-11-10 stsp return err;
1011 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1012 42c69117 2019-11-10 stsp delta_buf, delta_len);
1013 ab2f42e7 2019-11-10 stsp if (!cached)
1014 ab2f42e7 2019-11-10 stsp free(delta_buf);
1016 22484865 2018-03-13 stsp return err;
1018 22484865 2018-03-13 stsp base_size = delta->size;
1019 22484865 2018-03-13 stsp if (base_size > *max_size)
1020 22484865 2018-03-13 stsp *max_size = base_size;
1021 22484865 2018-03-13 stsp if (result_size > *max_size)
1022 22484865 2018-03-13 stsp *max_size = result_size;
1025 9feb4ff2 2018-03-14 stsp return NULL;
1028 ac544f8c 2019-01-13 stsp const struct got_error *
1029 42c69117 2019-11-10 stsp got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1030 42c69117 2019-11-10 stsp struct got_pack *pack)
1032 85a703fa 2019-01-13 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1033 85a703fa 2019-01-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1035 42c69117 2019-11-10 stsp return get_delta_chain_max_size(size, &obj->deltas, pack);
1038 22484865 2018-03-13 stsp static const struct got_error *
1039 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1040 3840f4c9 2018-09-12 stsp struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
1042 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1043 6691714a 2018-01-23 stsp struct got_delta *delta;
1044 42c69117 2019-11-10 stsp uint8_t *base_buf = NULL, *accum_buf = NULL, *delta_buf;
1045 42c69117 2019-11-10 stsp size_t base_bufsz = 0, accum_size = 0, delta_len;
1046 22484865 2018-03-13 stsp uint64_t max_size;
1047 6691714a 2018-01-23 stsp int n = 0;
1049 b29656e2 2018-03-16 stsp *result_size = 0;
1051 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1052 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1054 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1055 42c69117 2019-11-10 stsp err = get_delta_chain_max_size(&max_size, deltas, pack);
1057 22484865 2018-03-13 stsp return err;
1058 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1059 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1060 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1061 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1062 3840f4c9 2018-09-12 stsp base_file = NULL;
1063 3840f4c9 2018-09-12 stsp accum_file = NULL;
1066 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1067 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1068 ab2f42e7 2019-11-10 stsp int cached = 1;
1069 a6b158cc 2018-02-11 stsp if (n == 0) {
1070 34fca9c3 2018-11-11 stsp size_t mapoff;
1071 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1073 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1074 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1075 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1076 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1077 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1078 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1079 72eb3431 2018-04-01 stsp goto done;
1082 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1083 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1084 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1085 0c048b15 2018-04-27 stsp goto done;
1087 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1088 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1090 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1091 d7464085 2018-07-09 stsp goto done;
1094 d7464085 2018-07-09 stsp if (base_file) {
1095 d7464085 2018-07-09 stsp if (pack->map) {
1096 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1097 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1098 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1099 d7464085 2018-07-09 stsp pack->filesize - mapoff, base_file);
1101 34fca9c3 2018-11-11 stsp err = got_inflate_to_file_fd(
1102 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd, base_file);
1104 d7464085 2018-07-09 stsp if (pack->map) {
1105 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1106 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1107 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1108 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1110 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1111 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd);
1114 a6b158cc 2018-02-11 stsp goto done;
1116 8628c62d 2018-03-15 stsp if (base_file)
1117 8628c62d 2018-03-15 stsp rewind(base_file);
1121 ab2f42e7 2019-11-10 stsp got_delta_cache_get(&delta_buf, &delta_len,
1122 ab2f42e7 2019-11-10 stsp pack->delta_cache, delta->data_offset);
1123 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1124 ab2f42e7 2019-11-10 stsp cached = 0;
1125 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
1126 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1128 ab2f42e7 2019-11-10 stsp goto done;
1129 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1130 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1131 ab2f42e7 2019-11-10 stsp if (err == NULL)
1132 ab2f42e7 2019-11-10 stsp cached = 1;
1133 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1134 ab2f42e7 2019-11-10 stsp free(delta_buf);
1135 ab2f42e7 2019-11-10 stsp goto done;
1138 8628c62d 2018-03-15 stsp if (base_buf) {
1139 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1140 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1141 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1144 42c69117 2019-11-10 stsp err = got_delta_apply(base_file, delta_buf,
1145 42c69117 2019-11-10 stsp delta_len,
1146 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1147 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1148 b29656e2 2018-03-16 stsp &accum_size);
1150 ab2f42e7 2019-11-10 stsp if (!cached)
1151 ab2f42e7 2019-11-10 stsp free(delta_buf);
1153 6691714a 2018-01-23 stsp goto done;
1155 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1156 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1157 8628c62d 2018-03-15 stsp if (base_buf) {
1158 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1160 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation
1161 34fca9c3 2018-11-11 stsp * buffer. Ensure it can hold the largest
1162 34fca9c3 2018-11-11 stsp * result in the delta chain. The initial
1163 34fca9c3 2018-11-11 stsp * allocation might have been smaller.
1165 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1166 34fca9c3 2018-11-11 stsp uint8_t *p;
1167 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1168 34fca9c3 2018-11-11 stsp if (p == NULL) {
1169 638f9024 2019-05-13 stsp err = got_error_from_errno(
1170 230a42bd 2019-05-11 jcs "reallocarray");
1171 34fca9c3 2018-11-11 stsp goto done;
1173 34fca9c3 2018-11-11 stsp base_buf = p;
1174 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1176 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1177 8628c62d 2018-03-15 stsp base_buf = tmp;
1179 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1180 8628c62d 2018-03-15 stsp accum_file = base_file;
1181 8628c62d 2018-03-15 stsp base_file = tmp;
1182 8628c62d 2018-03-15 stsp rewind(base_file);
1183 8628c62d 2018-03-15 stsp rewind(accum_file);
1189 8628c62d 2018-03-15 stsp free(base_buf);
1190 8628c62d 2018-03-15 stsp if (accum_buf) {
1191 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1192 8628c62d 2018-03-15 stsp free(accum_buf);
1193 8628c62d 2018-03-15 stsp if (len != accum_size)
1194 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1196 6691714a 2018-01-23 stsp rewind(outfile);
1197 b29656e2 2018-03-16 stsp if (err == NULL)
1198 b29656e2 2018-03-16 stsp *result_size = accum_size;
1199 e0ab43e7 2018-03-16 stsp return err;
1202 e0ab43e7 2018-03-16 stsp static const struct got_error *
1203 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1204 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1206 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1207 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1208 42c69117 2019-11-10 stsp uint8_t *base_buf = NULL, *accum_buf = NULL, *delta_buf;
1209 42c69117 2019-11-10 stsp size_t base_bufsz = 0, accum_size = 0, delta_len;
1210 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1211 e0ab43e7 2018-03-16 stsp int n = 0;
1213 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1214 e0ab43e7 2018-03-16 stsp *outlen = 0;
1216 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1217 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1219 42c69117 2019-11-10 stsp err = get_delta_chain_max_size(&max_size, deltas, pack);
1221 e0ab43e7 2018-03-16 stsp return err;
1222 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1223 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1224 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1226 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1227 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1228 ab2f42e7 2019-11-10 stsp int cached = 1;
1229 e0ab43e7 2018-03-16 stsp if (n == 0) {
1230 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1232 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1233 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1234 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1235 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1236 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1237 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1238 72eb3431 2018-04-01 stsp goto done;
1241 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1242 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1243 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1244 0c048b15 2018-04-27 stsp goto done;
1246 d7464085 2018-07-09 stsp if (pack->map) {
1247 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1248 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1249 34fca9c3 2018-11-11 stsp &base_bufsz, pack->map, mapoff,
1250 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1252 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1254 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1255 d7464085 2018-07-09 stsp goto done;
1257 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1258 34fca9c3 2018-11-11 stsp &base_bufsz, pack->fd);
1261 d7464085 2018-07-09 stsp goto done;
1266 ab2f42e7 2019-11-10 stsp got_delta_cache_get(&delta_buf, &delta_len,
1267 ab2f42e7 2019-11-10 stsp pack->delta_cache, delta->data_offset);
1268 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1269 ab2f42e7 2019-11-10 stsp cached = 0;
1270 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
1271 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1273 ab2f42e7 2019-11-10 stsp goto done;
1274 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1275 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1276 ab2f42e7 2019-11-10 stsp if (err == NULL)
1277 ab2f42e7 2019-11-10 stsp cached = 1;
1278 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1279 ab2f42e7 2019-11-10 stsp free(delta_buf);
1280 ab2f42e7 2019-11-10 stsp goto done;
1283 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1284 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1285 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1286 ab2f42e7 2019-11-10 stsp if (!cached)
1287 ab2f42e7 2019-11-10 stsp free(delta_buf);
1290 e0ab43e7 2018-03-16 stsp goto done;
1292 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1293 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1294 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1296 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation buffer.
1297 34fca9c3 2018-11-11 stsp * Ensure it can hold the largest result in the delta
1298 34fca9c3 2018-11-11 stsp * chain. Initial allocation might have been smaller.
1300 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1301 34fca9c3 2018-11-11 stsp uint8_t *p;
1302 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1303 34fca9c3 2018-11-11 stsp if (p == NULL) {
1304 638f9024 2019-05-13 stsp err = got_error_from_errno(
1305 230a42bd 2019-05-11 jcs "reallocarray");
1306 34fca9c3 2018-11-11 stsp goto done;
1308 34fca9c3 2018-11-11 stsp base_buf = p;
1309 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1311 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1312 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1317 e0ab43e7 2018-03-16 stsp free(base_buf);
1318 e0ab43e7 2018-03-16 stsp if (err) {
1319 e0ab43e7 2018-03-16 stsp free(accum_buf);
1320 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1321 e0ab43e7 2018-03-16 stsp *outlen = 0;
1323 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1324 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1326 efd2a263 2018-01-19 stsp return err;
1329 3ee5fc21 2018-01-17 stsp const struct got_error *
1330 24140570 2018-09-09 stsp got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1331 3840f4c9 2018-09-12 stsp FILE *outfile, FILE *base_file, FILE *accum_file)
1333 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1335 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1336 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1338 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1339 24140570 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1340 24140570 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1342 d7464085 2018-07-09 stsp if (pack->map) {
1343 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1344 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(&obj->size, pack->map,
1345 24140570 2018-09-09 stsp mapoff, pack->filesize - mapoff, outfile);
1347 24140570 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1348 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1349 24140570 2018-09-09 stsp err = got_inflate_to_file_fd(&obj->size, pack->fd,
1353 2ce68b2f 2018-09-09 stsp err = dump_delta_chain_to_file(&obj->size, &obj->deltas, pack,
1354 3840f4c9 2018-09-12 stsp outfile, base_file, accum_file);
1356 a1fd68d8 2018-01-12 stsp return err;
1359 e0ab43e7 2018-03-16 stsp const struct got_error *
1360 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1361 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_pack *pack)
1363 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1365 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1366 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1368 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1369 7e212e3d 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1370 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1371 d7464085 2018-07-09 stsp if (pack->map) {
1372 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1373 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(buf, len, pack->map,
1374 d7464085 2018-07-09 stsp mapoff, pack->filesize - mapoff);
1376 7e212e3d 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1377 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1378 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(buf, len, pack->fd);
1381 48095039 2018-09-09 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
1383 e0ab43e7 2018-03-16 stsp return err;