Blame


1 0a0a3048 2018-01-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 0a0a3048 2018-01-10 stsp *
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.
7 0a0a3048 2018-01-10 stsp *
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.
15 0a0a3048 2018-01-10 stsp */
16 0a0a3048 2018-01-10 stsp
17 a1fd68d8 2018-01-12 stsp #include <sys/types.h>
18 0a0a3048 2018-01-10 stsp #include <sys/stat.h>
19 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 57b35b75 2018-06-22 stsp #include <sys/mman.h>
21 0a0a3048 2018-01-10 stsp
22 7e656b93 2018-03-17 stsp #include <fcntl.h>
23 a1fd68d8 2018-01-12 stsp #include <errno.h>
24 0a0a3048 2018-01-10 stsp #include <stdio.h>
25 a1fd68d8 2018-01-12 stsp #include <stdint.h>
26 0a0a3048 2018-01-10 stsp #include <stdlib.h>
27 0a0a3048 2018-01-10 stsp #include <string.h>
28 0a0a3048 2018-01-10 stsp #include <limits.h>
29 81a12da5 2020-09-09 naddy #include <unistd.h>
30 a1fd68d8 2018-01-12 stsp #include <zlib.h>
31 0a0a3048 2018-01-10 stsp
32 0a0a3048 2018-01-10 stsp #include "got_error.h"
33 a1fd68d8 2018-01-12 stsp #include "got_object.h"
34 511a516b 2018-05-19 stsp #include "got_opentemp.h"
35 324d37e7 2019-05-11 stsp #include "got_path.h"
36 0a0a3048 2018-01-10 stsp
37 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
39 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
40 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
42 dd88155e 2019-06-29 stsp #include "got_lib_object_parse.h"
43 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
44 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
45 79b11c62 2018-03-09 stsp
46 79b11c62 2018-03-09 stsp #ifndef nitems
47 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
48 79b11c62 2018-03-09 stsp #endif
49 1411938b 2018-02-12 stsp
50 a1fd68d8 2018-01-12 stsp #ifndef MIN
51 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 a1fd68d8 2018-01-12 stsp #endif
53 a1fd68d8 2018-01-12 stsp
54 0a0a3048 2018-01-10 stsp static const struct got_error *
55 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
56 0a0a3048 2018-01-10 stsp {
57 0a0a3048 2018-01-10 stsp int i;
58 0a0a3048 2018-01-10 stsp
59 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
60 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
61 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
62 0a0a3048 2018-01-10 stsp }
63 0a0a3048 2018-01-10 stsp
64 0a0a3048 2018-01-10 stsp return NULL;
65 0a0a3048 2018-01-10 stsp }
66 0a0a3048 2018-01-10 stsp
67 1510f469 2018-09-09 stsp const struct got_error *
68 c3564dfa 2021-07-15 stsp got_packidx_init_hdr(struct got_packidx *p, int verify, off_t packfile_size)
69 0a0a3048 2018-01-10 stsp {
70 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
71 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
72 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
73 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
74 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
75 817c5a18 2018-09-09 stsp ssize_t n;
76 5e6be232 2019-11-08 stsp int i;
77 0a0a3048 2018-01-10 stsp
78 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
79 0ebaf008 2018-01-10 stsp
80 57b35b75 2018-06-22 stsp h = &p->hdr;
81 57b35b75 2018-06-22 stsp offset = 0;
82 57b35b75 2018-06-22 stsp remain = p->len;
83 0a0a3048 2018-01-10 stsp
84 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
85 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
86 0a0a3048 2018-01-10 stsp goto done;
87 0a0a3048 2018-01-10 stsp }
88 fc79a48d 2018-07-09 stsp if (p->map)
89 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
90 fc79a48d 2018-07-09 stsp else {
91 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
92 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
93 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
94 fc79a48d 2018-07-09 stsp goto done;
95 fc79a48d 2018-07-09 stsp }
96 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
97 b1317e77 2019-09-22 stsp if (n < 0) {
98 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
99 b1317e77 2019-09-22 stsp goto done;
100 b1317e77 2019-09-22 stsp } else if (n != sizeof(*h->magic)) {
101 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
102 fc79a48d 2018-07-09 stsp goto done;
103 fc79a48d 2018-07-09 stsp }
104 fc79a48d 2018-07-09 stsp }
105 ac62b712 2021-03-30 stsp if (*h->magic != htobe32(GOT_PACKIDX_V2_MAGIC)) {
106 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
107 57b35b75 2018-06-22 stsp goto done;
108 57b35b75 2018-06-22 stsp }
109 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
110 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
111 0a0a3048 2018-01-10 stsp
112 0cb74cf4 2018-07-08 stsp if (verify)
113 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
114 0ebaf008 2018-01-10 stsp
115 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
116 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
117 0a0a3048 2018-01-10 stsp goto done;
118 0a0a3048 2018-01-10 stsp }
119 fc79a48d 2018-07-09 stsp if (p->map)
120 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
121 fc79a48d 2018-07-09 stsp else {
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");
125 fc79a48d 2018-07-09 stsp goto done;
126 fc79a48d 2018-07-09 stsp }
127 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
128 c6368c2e 2019-10-11 stsp if (n < 0) {
129 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
130 c6368c2e 2019-10-11 stsp goto done;
131 c6368c2e 2019-10-11 stsp } else if (n != sizeof(*h->version)) {
132 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
133 fc79a48d 2018-07-09 stsp goto done;
134 fc79a48d 2018-07-09 stsp }
135 fc79a48d 2018-07-09 stsp }
136 ac62b712 2021-03-30 stsp if (*h->version != htobe32(GOT_PACKIDX_VERSION)) {
137 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
138 0a0a3048 2018-01-10 stsp goto done;
139 0a0a3048 2018-01-10 stsp }
140 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
141 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
142 0a0a3048 2018-01-10 stsp
143 0cb74cf4 2018-07-08 stsp if (verify)
144 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
145 0ebaf008 2018-01-10 stsp
146 57b35b75 2018-06-22 stsp len_fanout =
147 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
148 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
149 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
150 0a0a3048 2018-01-10 stsp goto done;
151 0a0a3048 2018-01-10 stsp }
152 fc79a48d 2018-07-09 stsp if (p->map)
153 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
154 fc79a48d 2018-07-09 stsp else {
155 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
156 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
157 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
158 fc79a48d 2018-07-09 stsp goto done;
159 fc79a48d 2018-07-09 stsp }
160 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
161 c6368c2e 2019-10-11 stsp if (n < 0) {
162 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
163 c6368c2e 2019-10-11 stsp goto done;
164 c6368c2e 2019-10-11 stsp } else if (n != len_fanout) {
165 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
166 fc79a48d 2018-07-09 stsp goto done;
167 fc79a48d 2018-07-09 stsp }
168 fc79a48d 2018-07-09 stsp }
169 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
170 0a0a3048 2018-01-10 stsp if (err)
171 0a0a3048 2018-01-10 stsp goto done;
172 0cb74cf4 2018-07-08 stsp if (verify)
173 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
174 57b35b75 2018-06-22 stsp offset += len_fanout;
175 57b35b75 2018-06-22 stsp remain -= len_fanout;
176 0a0a3048 2018-01-10 stsp
177 78fb0967 2020-09-09 naddy nobj = be32toh(h->fanout_table[0xff]);
178 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
179 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
180 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
181 0a0a3048 2018-01-10 stsp goto done;
182 0a0a3048 2018-01-10 stsp }
183 fc79a48d 2018-07-09 stsp if (p->map)
184 fc79a48d 2018-07-09 stsp h->sorted_ids =
185 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
186 fc79a48d 2018-07-09 stsp else {
187 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
188 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
189 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
190 fc79a48d 2018-07-09 stsp goto done;
191 fc79a48d 2018-07-09 stsp }
192 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
193 faaa1c0f 2018-09-15 stsp if (n < 0)
194 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
195 faaa1c0f 2018-09-15 stsp else if (n != len_ids) {
196 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
197 fc79a48d 2018-07-09 stsp goto done;
198 fc79a48d 2018-07-09 stsp }
199 fc79a48d 2018-07-09 stsp }
200 0cb74cf4 2018-07-08 stsp if (verify)
201 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
202 57b35b75 2018-06-22 stsp offset += len_ids;
203 57b35b75 2018-06-22 stsp remain -= len_ids;
204 0a0a3048 2018-01-10 stsp
205 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
206 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
207 0a0a3048 2018-01-10 stsp goto done;
208 0a0a3048 2018-01-10 stsp }
209 fc79a48d 2018-07-09 stsp if (p->map)
210 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
211 fc79a48d 2018-07-09 stsp else {
212 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
213 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
214 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
215 fc79a48d 2018-07-09 stsp goto done;
216 fc79a48d 2018-07-09 stsp }
217 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
218 faaa1c0f 2018-09-15 stsp if (n < 0)
219 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
220 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->crc32)) {
221 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
222 fc79a48d 2018-07-09 stsp goto done;
223 fc79a48d 2018-07-09 stsp }
224 fc79a48d 2018-07-09 stsp }
225 0cb74cf4 2018-07-08 stsp if (verify)
226 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
227 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->crc32);
228 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->crc32);
229 0ebaf008 2018-01-10 stsp
230 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->offsets)) {
231 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
232 0a0a3048 2018-01-10 stsp goto done;
233 0a0a3048 2018-01-10 stsp }
234 fc79a48d 2018-07-09 stsp if (p->map)
235 fc79a48d 2018-07-09 stsp h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
236 fc79a48d 2018-07-09 stsp else {
237 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->offsets));
238 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
239 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
240 fc79a48d 2018-07-09 stsp goto done;
241 fc79a48d 2018-07-09 stsp }
242 fc79a48d 2018-07-09 stsp n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
243 faaa1c0f 2018-09-15 stsp if (n < 0)
244 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
245 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->offsets)) {
246 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
247 fc79a48d 2018-07-09 stsp goto done;
248 fc79a48d 2018-07-09 stsp }
249 fc79a48d 2018-07-09 stsp }
250 0cb74cf4 2018-07-08 stsp if (verify)
251 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
252 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
253 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
254 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
255 0ebaf008 2018-01-10 stsp
256 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
257 c3564dfa 2021-07-15 stsp if (verify || packfile_size > 0x7fffffff) {
258 c3564dfa 2021-07-15 stsp for (i = 0; i < nobj; i++) {
259 c3564dfa 2021-07-15 stsp uint32_t o = h->offsets[i];
260 c3564dfa 2021-07-15 stsp if (o & htobe32(GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX))
261 c3564dfa 2021-07-15 stsp p->nlargeobj++;
262 c3564dfa 2021-07-15 stsp }
263 5e6be232 2019-11-08 stsp }
264 5e6be232 2019-11-08 stsp if (p->nlargeobj == 0)
265 0a0a3048 2018-01-10 stsp goto checksum;
266 c3564dfa 2021-07-15 stsp else if (packfile_size <= 0x7fffffff) {
267 c3564dfa 2021-07-15 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
268 c3564dfa 2021-07-15 stsp goto done;
269 c3564dfa 2021-07-15 stsp }
270 0a0a3048 2018-01-10 stsp
271 5e6be232 2019-11-08 stsp if (remain < p->nlargeobj * sizeof(*h->large_offsets)) {
272 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
273 0a0a3048 2018-01-10 stsp goto done;
274 0a0a3048 2018-01-10 stsp }
275 fc79a48d 2018-07-09 stsp if (p->map)
276 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
277 fc79a48d 2018-07-09 stsp else {
278 5e6be232 2019-11-08 stsp h->large_offsets = malloc(p->nlargeobj *
279 5e6be232 2019-11-08 stsp sizeof(*h->large_offsets));
280 de30857e 2019-08-23 stsp if (h->large_offsets == NULL) {
281 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
282 fc79a48d 2018-07-09 stsp goto done;
283 fc79a48d 2018-07-09 stsp }
284 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
285 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
286 faaa1c0f 2018-09-15 stsp if (n < 0)
287 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
288 5e6be232 2019-11-08 stsp else if (n != p->nlargeobj * sizeof(*h->large_offsets)) {
289 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
290 fc79a48d 2018-07-09 stsp goto done;
291 fc79a48d 2018-07-09 stsp }
292 fc79a48d 2018-07-09 stsp }
293 0cb74cf4 2018-07-08 stsp if (verify)
294 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
295 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
296 5e6be232 2019-11-08 stsp remain -= p->nlargeobj * sizeof(*h->large_offsets);
297 5e6be232 2019-11-08 stsp offset += p->nlargeobj * sizeof(*h->large_offsets);
298 0ebaf008 2018-01-10 stsp
299 0a0a3048 2018-01-10 stsp checksum:
300 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
301 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
302 0a0a3048 2018-01-10 stsp goto done;
303 0a0a3048 2018-01-10 stsp }
304 fc79a48d 2018-07-09 stsp if (p->map)
305 fc79a48d 2018-07-09 stsp h->trailer =
306 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
307 fc79a48d 2018-07-09 stsp else {
308 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
309 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
310 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
311 fc79a48d 2018-07-09 stsp goto done;
312 fc79a48d 2018-07-09 stsp }
313 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
314 faaa1c0f 2018-09-15 stsp if (n < 0)
315 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
316 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->trailer)) {
317 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
318 fc79a48d 2018-07-09 stsp goto done;
319 fc79a48d 2018-07-09 stsp }
320 fc79a48d 2018-07-09 stsp }
321 0cb74cf4 2018-07-08 stsp if (verify) {
322 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
323 0cb74cf4 2018-07-08 stsp SHA1Final(sha1, &ctx);
324 0cb74cf4 2018-07-08 stsp if (memcmp(h->trailer->packidx_sha1, sha1,
325 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
326 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
327 0cb74cf4 2018-07-08 stsp }
328 0a0a3048 2018-01-10 stsp done:
329 817c5a18 2018-09-09 stsp return err;
330 817c5a18 2018-09-09 stsp }
331 817c5a18 2018-09-09 stsp
332 817c5a18 2018-09-09 stsp const struct got_error *
333 6d5a9006 2020-12-16 yzhong got_packidx_open(struct got_packidx **packidx,
334 6d5a9006 2020-12-16 yzhong int dir_fd, const char *relpath, int verify)
335 817c5a18 2018-09-09 stsp {
336 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
337 1124fe40 2021-07-07 stsp struct got_packidx *p = NULL;
338 1124fe40 2021-07-07 stsp char *pack_relpath;
339 c3564dfa 2021-07-15 stsp struct stat idx_sb, pack_sb;
340 817c5a18 2018-09-09 stsp
341 817c5a18 2018-09-09 stsp *packidx = NULL;
342 1124fe40 2021-07-07 stsp
343 1124fe40 2021-07-07 stsp err = got_packidx_get_packfile_path(&pack_relpath, relpath);
344 1124fe40 2021-07-07 stsp if (err)
345 1124fe40 2021-07-07 stsp return err;
346 1124fe40 2021-07-07 stsp
347 1124fe40 2021-07-07 stsp /*
348 1124fe40 2021-07-07 stsp * Ensure that a corresponding pack file exists.
349 1124fe40 2021-07-07 stsp * Some Git repositories have this problem. Git seems to ignore
350 1124fe40 2021-07-07 stsp * the existence of lonely pack index files but we do not.
351 1124fe40 2021-07-07 stsp */
352 c3564dfa 2021-07-15 stsp if (fstatat(dir_fd, pack_relpath, &pack_sb, 0) == -1) {
353 1124fe40 2021-07-07 stsp if (errno == ENOENT) {
354 1124fe40 2021-07-07 stsp err = got_error_fmt(GOT_ERR_LONELY_PACKIDX,
355 1124fe40 2021-07-07 stsp "%s", relpath);
356 1124fe40 2021-07-07 stsp } else
357 1124fe40 2021-07-07 stsp err = got_error_from_errno2("fstatat", pack_relpath);
358 1124fe40 2021-07-07 stsp goto done;
359 1124fe40 2021-07-07 stsp }
360 817c5a18 2018-09-09 stsp
361 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
362 1124fe40 2021-07-07 stsp if (p == NULL) {
363 1124fe40 2021-07-07 stsp err = got_error_from_errno("calloc");
364 1124fe40 2021-07-07 stsp goto done;
365 1124fe40 2021-07-07 stsp }
366 817c5a18 2018-09-09 stsp
367 fc63f50d 2021-12-31 thomas p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
368 6772cf22 2019-08-28 hiltjo if (p->fd == -1) {
369 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("openat", relpath);
370 1124fe40 2021-07-07 stsp goto done;
371 6772cf22 2019-08-28 hiltjo }
372 817c5a18 2018-09-09 stsp
373 c3564dfa 2021-07-15 stsp if (fstat(p->fd, &idx_sb) != 0) {
374 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("fstat", relpath);
375 1124fe40 2021-07-07 stsp goto done;
376 817c5a18 2018-09-09 stsp }
377 c3564dfa 2021-07-15 stsp p->len = idx_sb.st_size;
378 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
379 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
380 1124fe40 2021-07-07 stsp goto done;
381 817c5a18 2018-09-09 stsp }
382 817c5a18 2018-09-09 stsp
383 6d5a9006 2020-12-16 yzhong p->path_packidx = strdup(relpath);
384 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
385 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
386 817c5a18 2018-09-09 stsp goto done;
387 817c5a18 2018-09-09 stsp }
388 817c5a18 2018-09-09 stsp
389 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
390 817c5a18 2018-09-09 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
391 3a11398b 2019-02-21 stsp if (p->map == MAP_FAILED) {
392 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
393 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
394 3a11398b 2019-02-21 stsp goto done;
395 3a11398b 2019-02-21 stsp }
396 817c5a18 2018-09-09 stsp p->map = NULL; /* fall back to read(2) */
397 3a11398b 2019-02-21 stsp }
398 817c5a18 2018-09-09 stsp #endif
399 817c5a18 2018-09-09 stsp
400 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, verify, pack_sb.st_size);
401 817c5a18 2018-09-09 stsp done:
402 1124fe40 2021-07-07 stsp if (err) {
403 1124fe40 2021-07-07 stsp if (p)
404 1124fe40 2021-07-07 stsp got_packidx_close(p);
405 1124fe40 2021-07-07 stsp } else
406 0a0a3048 2018-01-10 stsp *packidx = p;
407 1124fe40 2021-07-07 stsp free(pack_relpath);
408 0a0a3048 2018-01-10 stsp return err;
409 0a0a3048 2018-01-10 stsp }
410 0a0a3048 2018-01-10 stsp
411 57b35b75 2018-06-22 stsp const struct got_error *
412 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
413 0a0a3048 2018-01-10 stsp {
414 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
415 57b35b75 2018-06-22 stsp
416 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
417 fc79a48d 2018-07-09 stsp if (packidx->map) {
418 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
419 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
420 fc79a48d 2018-07-09 stsp } else {
421 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
422 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
423 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
424 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
425 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
426 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
427 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
428 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
429 57b35b75 2018-06-22 stsp }
430 08578a35 2021-01-22 stsp if (close(packidx->fd) == -1 && err == NULL)
431 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
432 0a0a3048 2018-01-10 stsp free(packidx);
433 57b35b75 2018-06-22 stsp
434 57b35b75 2018-06-22 stsp return err;
435 a1fd68d8 2018-01-12 stsp }
436 509c9973 2021-04-10 stsp
437 509c9973 2021-04-10 stsp const struct got_error *
438 aea75d87 2021-07-06 stsp got_packidx_get_packfile_path(char **path_packfile, const char *path_packidx)
439 509c9973 2021-04-10 stsp {
440 509c9973 2021-04-10 stsp size_t size;
441 509c9973 2021-04-10 stsp
442 509c9973 2021-04-10 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
443 aea75d87 2021-07-06 stsp size = strlen(path_packidx) + 2;
444 509c9973 2021-04-10 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
445 aea75d87 2021-07-06 stsp return got_error_path(path_packidx, GOT_ERR_BAD_PATH);
446 a1fd68d8 2018-01-12 stsp
447 509c9973 2021-04-10 stsp *path_packfile = malloc(size);
448 509c9973 2021-04-10 stsp if (*path_packfile == NULL)
449 509c9973 2021-04-10 stsp return got_error_from_errno("malloc");
450 509c9973 2021-04-10 stsp
451 509c9973 2021-04-10 stsp /* Copy up to and excluding ".idx". */
452 aea75d87 2021-07-06 stsp if (strlcpy(*path_packfile, path_packidx,
453 509c9973 2021-04-10 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
454 509c9973 2021-04-10 stsp return got_error(GOT_ERR_NO_SPACE);
455 509c9973 2021-04-10 stsp
456 509c9973 2021-04-10 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
457 509c9973 2021-04-10 stsp return got_error(GOT_ERR_NO_SPACE);
458 509c9973 2021-04-10 stsp
459 509c9973 2021-04-10 stsp return NULL;
460 509c9973 2021-04-10 stsp }
461 509c9973 2021-04-10 stsp
462 02828bfd 2021-06-22 stsp off_t
463 02828bfd 2021-06-22 stsp got_packidx_get_object_offset(struct got_packidx *packidx, int idx)
464 a1fd68d8 2018-01-12 stsp {
465 78fb0967 2020-09-09 naddy uint32_t offset = be32toh(packidx->hdr.offsets[idx]);
466 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
467 a1fd68d8 2018-01-12 stsp uint64_t loffset;
468 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
469 5e6be232 2019-11-08 stsp if (idx < 0 || idx >= packidx->nlargeobj ||
470 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
471 a1fd68d8 2018-01-12 stsp return -1;
472 78fb0967 2020-09-09 naddy loffset = be64toh(packidx->hdr.large_offsets[idx]);
473 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
474 a1fd68d8 2018-01-12 stsp }
475 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
476 a1fd68d8 2018-01-12 stsp }
477 a1fd68d8 2018-01-12 stsp
478 1510f469 2018-09-09 stsp int
479 48b4f239 2021-12-31 thomas got_packidx_get_object_idx(struct got_packidx *packidx,
480 48b4f239 2021-12-31 thomas struct got_object_id *id)
481 a1fd68d8 2018-01-12 stsp {
482 00927983 2020-04-19 stsp u_int8_t id0 = id->sha1[0];
483 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
484 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
485 a1fd68d8 2018-01-12 stsp
486 a1fd68d8 2018-01-12 stsp if (id0 > 0)
487 78fb0967 2020-09-09 naddy left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
488 a1fd68d8 2018-01-12 stsp
489 40aeb19c 2018-06-22 stsp while (left <= right) {
490 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
491 40aeb19c 2018-06-22 stsp int i, cmp;
492 a1fd68d8 2018-01-12 stsp
493 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
494 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
495 00927983 2020-04-19 stsp cmp = memcmp(id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
496 16dcbf91 2018-04-01 stsp if (cmp == 0)
497 6c00b545 2018-01-17 stsp return i;
498 40aeb19c 2018-06-22 stsp else if (cmp > 0)
499 40aeb19c 2018-06-22 stsp left = i + 1;
500 40aeb19c 2018-06-22 stsp else if (cmp < 0)
501 40aeb19c 2018-06-22 stsp right = i - 1;
502 a1fd68d8 2018-01-12 stsp }
503 a1fd68d8 2018-01-12 stsp
504 a1fd68d8 2018-01-12 stsp return -1;
505 876c234b 2018-09-10 stsp }
506 876c234b 2018-09-10 stsp
507 876c234b 2018-09-10 stsp const struct got_error *
508 dd88155e 2019-06-29 stsp got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
509 4277420a 2019-06-29 stsp struct got_packidx *packidx, const char *id_str_prefix)
510 e09a504c 2019-06-28 stsp {
511 dd88155e 2019-06-29 stsp const struct got_error *err = NULL;
512 4277420a 2019-06-29 stsp u_int8_t id0;
513 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
514 4277420a 2019-06-29 stsp char hex[3];
515 4277420a 2019-06-29 stsp size_t prefix_len = strlen(id_str_prefix);
516 e09a504c 2019-06-28 stsp struct got_packidx_object_id *oid;
517 ec138a87 2022-01-03 thomas uint32_t i = 0;
518 e09a504c 2019-06-28 stsp
519 dbdddfee 2021-06-23 naddy STAILQ_INIT(matched_ids);
520 4277420a 2019-06-29 stsp
521 4277420a 2019-06-29 stsp if (prefix_len < 2)
522 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
523 4277420a 2019-06-29 stsp
524 4277420a 2019-06-29 stsp hex[0] = id_str_prefix[0];
525 4277420a 2019-06-29 stsp hex[1] = id_str_prefix[1];
526 4277420a 2019-06-29 stsp hex[2] = '\0';
527 4277420a 2019-06-29 stsp if (!got_parse_xdigit(&id0, hex))
528 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
529 4277420a 2019-06-29 stsp
530 ec138a87 2022-01-03 thomas if (id0 > 0)
531 ec138a87 2022-01-03 thomas i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
532 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[i];
533 4277420a 2019-06-29 stsp while (i < totobj && oid->sha1[0] == id0) {
534 4277420a 2019-06-29 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
535 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
536 4277420a 2019-06-29 stsp int cmp;
537 4277420a 2019-06-29 stsp
538 4277420a 2019-06-29 stsp if (!got_sha1_digest_to_str(oid->sha1, id_str, sizeof(id_str)))
539 4277420a 2019-06-29 stsp return got_error(GOT_ERR_NO_SPACE);
540 4277420a 2019-06-29 stsp
541 4277420a 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, prefix_len);
542 4277420a 2019-06-29 stsp if (cmp < 0) {
543 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
544 4277420a 2019-06-29 stsp continue;
545 4277420a 2019-06-29 stsp } else if (cmp > 0)
546 e09a504c 2019-06-28 stsp break;
547 4277420a 2019-06-29 stsp
548 dd88155e 2019-06-29 stsp err = got_object_qid_alloc_partial(&qid);
549 dd88155e 2019-06-29 stsp if (err)
550 dd88155e 2019-06-29 stsp break;
551 dd88155e 2019-06-29 stsp memcpy(qid->id->sha1, oid->sha1, SHA1_DIGEST_LENGTH);
552 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(matched_ids, qid, entry);
553 4277420a 2019-06-29 stsp
554 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
555 e09a504c 2019-06-28 stsp }
556 e09a504c 2019-06-28 stsp
557 0adc7bcc 2019-06-29 stsp if (err)
558 0adc7bcc 2019-06-29 stsp got_object_id_queue_free(matched_ids);
559 dd88155e 2019-06-29 stsp return err;
560 e09a504c 2019-06-28 stsp }
561 e09a504c 2019-06-28 stsp
562 b4f37570 2021-06-19 stsp static const struct got_error *
563 b4f37570 2021-06-19 stsp pack_stop_privsep_child(struct got_pack *pack)
564 876c234b 2018-09-10 stsp {
565 96732e0b 2018-11-11 stsp const struct got_error *err = NULL;
566 876c234b 2018-09-10 stsp
567 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL)
568 876c234b 2018-09-10 stsp return NULL;
569 876c234b 2018-09-10 stsp
570 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
571 96732e0b 2018-11-11 stsp if (err)
572 96732e0b 2018-11-11 stsp return err;
573 96732e0b 2018-11-11 stsp err = got_privsep_wait_for_child(pack->privsep_child->pid);
574 08578a35 2021-01-22 stsp if (close(pack->privsep_child->imsg_fd) == -1 && err == NULL)
575 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
576 cc2a8ef4 2021-06-19 tracey imsg_clear(pack->privsep_child->ibuf);
577 cc2a8ef4 2021-06-19 tracey free(pack->privsep_child->ibuf);
578 876c234b 2018-09-10 stsp free(pack->privsep_child);
579 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
580 876c234b 2018-09-10 stsp return err;
581 7e656b93 2018-03-17 stsp }
582 7e656b93 2018-03-17 stsp
583 d7464085 2018-07-09 stsp const struct got_error *
584 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
585 7e656b93 2018-03-17 stsp {
586 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
587 d7464085 2018-07-09 stsp
588 b4f37570 2021-06-19 stsp err = pack_stop_privsep_child(pack);
589 876c234b 2018-09-10 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
590 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
591 08578a35 2021-01-22 stsp if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
592 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
593 8b2180d4 2018-04-26 stsp pack->fd = -1;
594 4589e373 2018-03-17 stsp free(pack->path_packfile);
595 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
596 4589e373 2018-03-17 stsp pack->filesize = 0;
597 ab2f42e7 2019-11-10 stsp if (pack->delta_cache) {
598 ab2f42e7 2019-11-10 stsp got_delta_cache_free(pack->delta_cache);
599 ab2f42e7 2019-11-10 stsp pack->delta_cache = NULL;
600 ab2f42e7 2019-11-10 stsp }
601 7e656b93 2018-03-17 stsp
602 c7fe698a 2018-01-23 stsp return err;
603 c7fe698a 2018-01-23 stsp }
604 c7fe698a 2018-01-23 stsp
605 668a20f6 2020-03-18 stsp const struct got_error *
606 668a20f6 2020-03-18 stsp got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
607 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
608 a487c1d0 2018-01-14 stsp {
609 a487c1d0 2018-01-14 stsp uint8_t t = 0;
610 a487c1d0 2018-01-14 stsp uint64_t s = 0;
611 a487c1d0 2018-01-14 stsp uint8_t sizeN;
612 d7464085 2018-07-09 stsp size_t mapoff = 0;
613 a487c1d0 2018-01-14 stsp int i = 0;
614 d7464085 2018-07-09 stsp
615 d7464085 2018-07-09 stsp *len = 0;
616 d7464085 2018-07-09 stsp
617 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
618 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
619 d7464085 2018-07-09 stsp
620 d7464085 2018-07-09 stsp if (pack->map) {
621 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
622 d7464085 2018-07-09 stsp } else {
623 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
624 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
625 d7464085 2018-07-09 stsp }
626 a487c1d0 2018-01-14 stsp
627 a1fd68d8 2018-01-12 stsp do {
628 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
629 a487c1d0 2018-01-14 stsp if (i > 9)
630 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
631 a1fd68d8 2018-01-12 stsp
632 d7464085 2018-07-09 stsp if (pack->map) {
633 1944573a 2022-01-10 thomas if (mapoff + sizeof(sizeN) >= pack->filesize)
634 1944573a 2022-01-10 thomas return got_error(GOT_ERR_BAD_PACKFILE);
635 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
636 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
637 d7464085 2018-07-09 stsp } else {
638 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
639 d7464085 2018-07-09 stsp if (n < 0)
640 638f9024 2019-05-13 stsp return got_error_from_errno("read");
641 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
642 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
643 d7464085 2018-07-09 stsp }
644 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
645 8251fdbc 2018-01-12 stsp
646 a1fd68d8 2018-01-12 stsp if (i == 0) {
647 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
648 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
649 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
650 a1fd68d8 2018-01-12 stsp } else {
651 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
652 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
653 a1fd68d8 2018-01-12 stsp }
654 a1fd68d8 2018-01-12 stsp i++;
655 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
656 a1fd68d8 2018-01-12 stsp
657 a487c1d0 2018-01-14 stsp *type = t;
658 a487c1d0 2018-01-14 stsp *size = s;
659 a487c1d0 2018-01-14 stsp return NULL;
660 0a0a3048 2018-01-10 stsp }
661 c54542a0 2018-01-13 stsp
662 a1fd68d8 2018-01-12 stsp static const struct got_error *
663 5f25cc85 2019-11-26 stsp open_plain_object(struct got_object **obj, struct got_object_id *id,
664 5f25cc85 2019-11-26 stsp uint8_t type, off_t offset, size_t size, int idx)
665 6ccb713b 2018-01-19 stsp {
666 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
667 6ccb713b 2018-01-19 stsp if (*obj == NULL)
668 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
669 6ccb713b 2018-01-19 stsp
670 6ccb713b 2018-01-19 stsp (*obj)->type = type;
671 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
672 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
673 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
674 6ccb713b 2018-01-19 stsp (*obj)->size = size;
675 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
676 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
677 b107e67f 2018-01-19 stsp
678 b107e67f 2018-01-19 stsp return NULL;
679 b107e67f 2018-01-19 stsp }
680 b107e67f 2018-01-19 stsp
681 b107e67f 2018-01-19 stsp static const struct got_error *
682 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
683 d7464085 2018-07-09 stsp off_t delta_offset)
684 b107e67f 2018-01-19 stsp {
685 b107e67f 2018-01-19 stsp int64_t o = 0;
686 b107e67f 2018-01-19 stsp uint8_t offN;
687 b107e67f 2018-01-19 stsp int i = 0;
688 b107e67f 2018-01-19 stsp
689 a0de39f3 2019-08-09 stsp *offset = 0;
690 d7464085 2018-07-09 stsp *len = 0;
691 d7464085 2018-07-09 stsp
692 b107e67f 2018-01-19 stsp do {
693 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
694 b107e67f 2018-01-19 stsp if (i > 8)
695 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
696 b107e67f 2018-01-19 stsp
697 d7464085 2018-07-09 stsp if (pack->map) {
698 d7464085 2018-07-09 stsp size_t mapoff;
699 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
700 1944573a 2022-01-10 thomas if (mapoff + sizeof(offN) >= pack->filesize)
701 1944573a 2022-01-10 thomas return got_error(GOT_ERR_PACK_OFFSET);
702 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
703 d7464085 2018-07-09 stsp } else {
704 d7464085 2018-07-09 stsp ssize_t n;
705 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
706 d7464085 2018-07-09 stsp if (n < 0)
707 638f9024 2019-05-13 stsp return got_error_from_errno("read");
708 d7464085 2018-07-09 stsp if (n != sizeof(offN))
709 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
710 d7464085 2018-07-09 stsp }
711 d7464085 2018-07-09 stsp *len += sizeof(offN);
712 b107e67f 2018-01-19 stsp
713 b107e67f 2018-01-19 stsp if (i == 0)
714 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
715 b107e67f 2018-01-19 stsp else {
716 cecc778e 2018-01-23 stsp o++;
717 b107e67f 2018-01-19 stsp o <<= 7;
718 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
719 b107e67f 2018-01-19 stsp }
720 b107e67f 2018-01-19 stsp i++;
721 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
722 6ccb713b 2018-01-19 stsp
723 b107e67f 2018-01-19 stsp *offset = o;
724 6ccb713b 2018-01-19 stsp return NULL;
725 6ccb713b 2018-01-19 stsp }
726 6ccb713b 2018-01-19 stsp
727 668a20f6 2020-03-18 stsp const struct got_error *
728 48b4f239 2021-12-31 thomas got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
729 48b4f239 2021-12-31 thomas struct got_pack *pack, off_t offset, int tslen)
730 b107e67f 2018-01-19 stsp {
731 96f5e8b3 2018-01-23 stsp const struct got_error *err;
732 b107e67f 2018-01-19 stsp int64_t negoffset;
733 b107e67f 2018-01-19 stsp size_t negofflen;
734 b107e67f 2018-01-19 stsp
735 d7464085 2018-07-09 stsp *len = 0;
736 d7464085 2018-07-09 stsp
737 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
738 d7464085 2018-07-09 stsp offset + tslen);
739 b107e67f 2018-01-19 stsp if (err)
740 b107e67f 2018-01-19 stsp return err;
741 b107e67f 2018-01-19 stsp
742 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
743 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
744 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
745 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
746 b107e67f 2018-01-19 stsp
747 d7464085 2018-07-09 stsp *len = negofflen;
748 96f5e8b3 2018-01-23 stsp return NULL;
749 96f5e8b3 2018-01-23 stsp }
750 96f5e8b3 2018-01-23 stsp
751 0e22967e 2018-02-11 stsp static const struct got_error *
752 42c69117 2019-11-10 stsp read_delta_data(uint8_t **delta_buf, size_t *delta_len,
753 42c69117 2019-11-10 stsp size_t delta_data_offset, struct got_pack *pack)
754 42c69117 2019-11-10 stsp {
755 42c69117 2019-11-10 stsp const struct got_error *err = NULL;
756 42c69117 2019-11-10 stsp
757 42c69117 2019-11-10 stsp if (pack->map) {
758 42c69117 2019-11-10 stsp if (delta_data_offset >= pack->filesize)
759 42c69117 2019-11-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
760 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(delta_buf, delta_len,
761 2e5a6fad 2020-03-18 stsp NULL, NULL, pack->map, delta_data_offset,
762 2e5a6fad 2020-03-18 stsp pack->filesize - delta_data_offset);
763 42c69117 2019-11-10 stsp } else {
764 42c69117 2019-11-10 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
765 42c69117 2019-11-10 stsp return got_error_from_errno("lseek");
766 3ab5e33c 2020-03-18 stsp err = got_inflate_to_mem_fd(delta_buf, delta_len, NULL,
767 55fdd257 2020-03-18 stsp NULL, 0, pack->fd);
768 42c69117 2019-11-10 stsp }
769 42c69117 2019-11-10 stsp return err;
770 42c69117 2019-11-10 stsp }
771 a3500804 2018-01-23 stsp
772 96f5e8b3 2018-01-23 stsp static const struct got_error *
773 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
774 42c69117 2019-11-10 stsp int delta_type, size_t delta_size, size_t delta_data_offset)
775 bdd2fbb3 2018-02-11 stsp {
776 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
777 bdd2fbb3 2018-02-11 stsp
778 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
779 42c69117 2019-11-10 stsp delta_data_offset);
780 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
781 638f9024 2019-05-13 stsp return got_error_from_errno("got_delta_open");
782 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
783 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
784 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&deltas->entries, delta, entry);
785 bdd2fbb3 2018-02-11 stsp return NULL;
786 bdd2fbb3 2018-02-11 stsp }
787 bdd2fbb3 2018-02-11 stsp
788 bdd2fbb3 2018-02-11 stsp static const struct got_error *
789 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
790 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
791 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
792 bdd2fbb3 2018-02-11 stsp
793 a3500804 2018-01-23 stsp {
794 a3500804 2018-01-23 stsp const struct got_error *err;
795 c3703302 2018-01-23 stsp off_t base_offset;
796 c3703302 2018-01-23 stsp uint8_t base_type;
797 c3703302 2018-01-23 stsp uint64_t base_size;
798 c3703302 2018-01-23 stsp size_t base_tslen;
799 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
800 42c69117 2019-11-10 stsp size_t consumed;
801 a3500804 2018-01-23 stsp
802 668a20f6 2020-03-18 stsp err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
803 d7464085 2018-07-09 stsp delta_offset, tslen);
804 a3500804 2018-01-23 stsp if (err)
805 a3500804 2018-01-23 stsp return err;
806 a3500804 2018-01-23 stsp
807 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
808 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
809 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
810 a53d2f13 2018-03-17 stsp
811 d7464085 2018-07-09 stsp if (pack->map == NULL) {
812 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
813 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
814 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
815 d7464085 2018-07-09 stsp }
816 bdd2fbb3 2018-02-11 stsp
817 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
818 42c69117 2019-11-10 stsp delta_data_offset);
819 bdd2fbb3 2018-02-11 stsp if (err)
820 1a35c1bc 2019-05-22 stsp return err;
821 bdd2fbb3 2018-02-11 stsp
822 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
823 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
824 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
825 b107e67f 2018-01-19 stsp
826 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&base_type, &base_size,
827 668a20f6 2020-03-18 stsp &base_tslen, pack, base_offset);
828 b107e67f 2018-01-19 stsp if (err)
829 1a35c1bc 2019-05-22 stsp return err;
830 b107e67f 2018-01-19 stsp
831 668a20f6 2020-03-18 stsp return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
832 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
833 c3703302 2018-01-23 stsp }
834 c4330eff 2021-06-22 stsp
835 c4330eff 2021-06-22 stsp const struct got_error *
836 c4330eff 2021-06-22 stsp got_pack_parse_ref_delta(struct got_object_id *id,
837 c4330eff 2021-06-22 stsp struct got_pack *pack, off_t delta_offset, int tslen)
838 c4330eff 2021-06-22 stsp {
839 c4330eff 2021-06-22 stsp if (pack->map) {
840 c4330eff 2021-06-22 stsp size_t mapoff = delta_offset + tslen;
841 1944573a 2022-01-10 thomas if (mapoff + sizeof(*id) >= pack->filesize)
842 1944573a 2022-01-10 thomas return got_error(GOT_ERR_PACK_OFFSET);
843 c4330eff 2021-06-22 stsp memcpy(id, pack->map + mapoff, sizeof(*id));
844 c4330eff 2021-06-22 stsp } else {
845 c4330eff 2021-06-22 stsp ssize_t n;
846 c4330eff 2021-06-22 stsp n = read(pack->fd, id, sizeof(*id));
847 c4330eff 2021-06-22 stsp if (n < 0)
848 c4330eff 2021-06-22 stsp return got_error_from_errno("read");
849 c4330eff 2021-06-22 stsp if (n != sizeof(*id))
850 c4330eff 2021-06-22 stsp return got_error(GOT_ERR_BAD_PACKFILE);
851 c4330eff 2021-06-22 stsp }
852 c3703302 2018-01-23 stsp
853 c4330eff 2021-06-22 stsp return NULL;
854 c4330eff 2021-06-22 stsp }
855 c4330eff 2021-06-22 stsp
856 c3703302 2018-01-23 stsp static const struct got_error *
857 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
858 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
859 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
860 c3703302 2018-01-23 stsp {
861 6b9c9673 2018-01-23 stsp const struct got_error *err;
862 6b9c9673 2018-01-23 stsp struct got_object_id id;
863 6b9c9673 2018-01-23 stsp int idx;
864 6b9c9673 2018-01-23 stsp off_t base_offset;
865 6b9c9673 2018-01-23 stsp uint8_t base_type;
866 6b9c9673 2018-01-23 stsp uint64_t base_size;
867 6b9c9673 2018-01-23 stsp size_t base_tslen;
868 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
869 6b9c9673 2018-01-23 stsp
870 42c69117 2019-11-10 stsp if (delta_offset + tslen >= pack->filesize)
871 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
872 a53d2f13 2018-03-17 stsp
873 c4330eff 2021-06-22 stsp err = got_pack_parse_ref_delta(&id, pack, delta_offset, tslen);
874 c4330eff 2021-06-22 stsp if (err)
875 c4330eff 2021-06-22 stsp return err;
876 d7464085 2018-07-09 stsp if (pack->map) {
877 c4330eff 2021-06-22 stsp delta_data_offset = delta_offset + tslen + sizeof(id);
878 d7464085 2018-07-09 stsp } else {
879 e40b19ed 2020-01-06 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
880 e40b19ed 2020-01-06 stsp if (delta_data_offset == -1)
881 e40b19ed 2020-01-06 stsp return got_error_from_errno("lseek");
882 d7464085 2018-07-09 stsp }
883 d7464085 2018-07-09 stsp
884 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
885 42c69117 2019-11-10 stsp delta_data_offset);
886 bdd2fbb3 2018-02-11 stsp if (err)
887 1a35c1bc 2019-05-22 stsp return err;
888 bdd2fbb3 2018-02-11 stsp
889 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
890 1510f469 2018-09-09 stsp idx = got_packidx_get_object_idx(packidx, &id);
891 1a35c1bc 2019-05-22 stsp if (idx == -1)
892 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_NO_OBJ);
893 6b9c9673 2018-01-23 stsp
894 02828bfd 2021-06-22 stsp base_offset = got_packidx_get_object_offset(packidx, idx);
895 1a35c1bc 2019-05-22 stsp if (base_offset == (uint64_t)-1)
896 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_BAD_PACKIDX);
897 6b9c9673 2018-01-23 stsp
898 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
899 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
900 6b9c9673 2018-01-23 stsp
901 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&base_type, &base_size,
902 668a20f6 2020-03-18 stsp &base_tslen, pack, base_offset);
903 6b9c9673 2018-01-23 stsp if (err)
904 1a35c1bc 2019-05-22 stsp return err;
905 6b9c9673 2018-01-23 stsp
906 668a20f6 2020-03-18 stsp return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
907 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
908 6b9c9673 2018-01-23 stsp }
909 6b9c9673 2018-01-23 stsp
910 668a20f6 2020-03-18 stsp const struct got_error *
911 668a20f6 2020-03-18 stsp got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
912 668a20f6 2020-03-18 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
913 668a20f6 2020-03-18 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
914 6b9c9673 2018-01-23 stsp {
915 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
916 c3703302 2018-01-23 stsp
917 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
918 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
919 5b7e13a7 2018-04-02 stsp
920 c3703302 2018-01-23 stsp switch (delta_type) {
921 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
922 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
923 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
924 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
925 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
926 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
927 42c69117 2019-11-10 stsp delta_size, 0);
928 4e8cda55 2018-01-19 stsp break;
929 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
930 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
931 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
932 96f5e8b3 2018-01-23 stsp break;
933 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
934 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
935 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
936 6b9c9673 2018-01-23 stsp break;
937 4e8cda55 2018-01-19 stsp default:
938 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
939 4e8cda55 2018-01-19 stsp }
940 96f5e8b3 2018-01-23 stsp
941 96f5e8b3 2018-01-23 stsp return err;
942 96f5e8b3 2018-01-23 stsp }
943 96f5e8b3 2018-01-23 stsp
944 96f5e8b3 2018-01-23 stsp static const struct got_error *
945 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
946 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
947 c59b3346 2018-09-11 stsp size_t tslen, int delta_type, size_t delta_size, int idx)
948 96f5e8b3 2018-01-23 stsp {
949 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
950 96f5e8b3 2018-01-23 stsp int resolved_type;
951 4e8cda55 2018-01-19 stsp
952 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
953 b107e67f 2018-01-19 stsp if (*obj == NULL)
954 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
955 b107e67f 2018-01-19 stsp
956 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
957 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
958 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
959 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
960 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
961 1a35c1bc 2019-05-22 stsp
962 dbdddfee 2021-06-23 naddy STAILQ_INIT(&(*obj)->deltas.entries);
963 1a35c1bc 2019-05-22 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
964 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
965 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
966 96f5e8b3 2018-01-23 stsp
967 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
968 668a20f6 2020-03-18 stsp offset, tslen, delta_type, delta_size,
969 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
970 96f5e8b3 2018-01-23 stsp if (err)
971 96f5e8b3 2018-01-23 stsp goto done;
972 96f5e8b3 2018-01-23 stsp
973 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
974 96f5e8b3 2018-01-23 stsp if (err)
975 96f5e8b3 2018-01-23 stsp goto done;
976 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
977 96f5e8b3 2018-01-23 stsp done:
978 96f5e8b3 2018-01-23 stsp if (err) {
979 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
980 96f5e8b3 2018-01-23 stsp *obj = NULL;
981 96f5e8b3 2018-01-23 stsp }
982 96f5e8b3 2018-01-23 stsp return err;
983 b107e67f 2018-01-19 stsp }
984 b107e67f 2018-01-19 stsp
985 2090a03d 2018-09-09 stsp const struct got_error *
986 2090a03d 2018-09-09 stsp got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
987 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
988 a1fd68d8 2018-01-12 stsp {
989 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
990 a1fd68d8 2018-01-12 stsp off_t offset;
991 6c00b545 2018-01-17 stsp uint8_t type;
992 6c00b545 2018-01-17 stsp uint64_t size;
993 3ee5fc21 2018-01-17 stsp size_t tslen;
994 a1fd68d8 2018-01-12 stsp
995 6c00b545 2018-01-17 stsp *obj = NULL;
996 a1fd68d8 2018-01-12 stsp
997 02828bfd 2021-06-22 stsp offset = got_packidx_get_object_offset(packidx, idx);
998 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
999 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1000 a1fd68d8 2018-01-12 stsp
1001 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1002 668a20f6 2020-03-18 stsp pack, offset);
1003 a1fd68d8 2018-01-12 stsp if (err)
1004 35c73765 2018-09-09 stsp return err;
1005 a1fd68d8 2018-01-12 stsp
1006 6c00b545 2018-01-17 stsp switch (type) {
1007 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1008 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1009 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1010 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1011 5f25cc85 2019-11-26 stsp err = open_plain_object(obj, id, type, offset + tslen,
1012 5f25cc85 2019-11-26 stsp size, idx);
1013 6c00b545 2018-01-17 stsp break;
1014 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1015 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1016 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
1017 c59b3346 2018-09-11 stsp tslen, type, size, idx);
1018 b107e67f 2018-01-19 stsp break;
1019 6c00b545 2018-01-17 stsp default:
1020 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1021 35c73765 2018-09-09 stsp break;
1022 35c73765 2018-09-09 stsp }
1023 35c73765 2018-09-09 stsp
1024 3ee5fc21 2018-01-17 stsp return err;
1025 3ee5fc21 2018-01-17 stsp }
1026 efd2a263 2018-01-19 stsp
1027 d582f26c 2020-03-18 stsp const struct got_error *
1028 48b4f239 2021-12-31 thomas got_pack_get_delta_chain_max_size(uint64_t *max_size,
1029 48b4f239 2021-12-31 thomas struct got_delta_chain *deltas, struct got_pack *pack)
1030 22484865 2018-03-13 stsp {
1031 22484865 2018-03-13 stsp struct got_delta *delta;
1032 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1033 22484865 2018-03-13 stsp
1034 22484865 2018-03-13 stsp *max_size = 0;
1035 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1036 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1037 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1038 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1039 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1040 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1041 22484865 2018-03-13 stsp const struct got_error *err;
1042 42c69117 2019-11-10 stsp uint8_t *delta_buf;
1043 42c69117 2019-11-10 stsp size_t delta_len;
1044 ab2f42e7 2019-11-10 stsp int cached = 1;
1045 42c69117 2019-11-10 stsp
1046 ab2f42e7 2019-11-10 stsp got_delta_cache_get(&delta_buf, &delta_len,
1047 ab2f42e7 2019-11-10 stsp pack->delta_cache, delta->data_offset);
1048 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1049 ab2f42e7 2019-11-10 stsp cached = 0;
1050 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
1051 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1052 ab2f42e7 2019-11-10 stsp if (err)
1053 ab2f42e7 2019-11-10 stsp return err;
1054 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1055 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1056 ab2f42e7 2019-11-10 stsp if (err == NULL)
1057 ab2f42e7 2019-11-10 stsp cached = 1;
1058 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1059 ab2f42e7 2019-11-10 stsp free(delta_buf);
1060 ab2f42e7 2019-11-10 stsp return err;
1061 ab2f42e7 2019-11-10 stsp }
1062 ab2f42e7 2019-11-10 stsp }
1063 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1064 42c69117 2019-11-10 stsp delta_buf, delta_len);
1065 ab2f42e7 2019-11-10 stsp if (!cached)
1066 ab2f42e7 2019-11-10 stsp free(delta_buf);
1067 22484865 2018-03-13 stsp if (err)
1068 22484865 2018-03-13 stsp return err;
1069 22484865 2018-03-13 stsp } else
1070 22484865 2018-03-13 stsp base_size = delta->size;
1071 22484865 2018-03-13 stsp if (base_size > *max_size)
1072 22484865 2018-03-13 stsp *max_size = base_size;
1073 22484865 2018-03-13 stsp if (result_size > *max_size)
1074 22484865 2018-03-13 stsp *max_size = result_size;
1075 22484865 2018-03-13 stsp }
1076 bd1223b9 2018-03-14 stsp
1077 9feb4ff2 2018-03-14 stsp return NULL;
1078 ac544f8c 2019-01-13 stsp }
1079 ac544f8c 2019-01-13 stsp
1080 ac544f8c 2019-01-13 stsp const struct got_error *
1081 42c69117 2019-11-10 stsp got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1082 42c69117 2019-11-10 stsp struct got_pack *pack)
1083 ac544f8c 2019-01-13 stsp {
1084 85a703fa 2019-01-13 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1085 85a703fa 2019-01-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1086 85a703fa 2019-01-13 stsp
1087 d582f26c 2020-03-18 stsp return got_pack_get_delta_chain_max_size(size, &obj->deltas, pack);
1088 22484865 2018-03-13 stsp }
1089 22484865 2018-03-13 stsp
1090 668a20f6 2020-03-18 stsp const struct got_error *
1091 4788f1ce 2020-03-18 stsp got_pack_dump_delta_chain_to_file(size_t *result_size,
1092 4788f1ce 2020-03-18 stsp struct got_delta_chain *deltas, struct got_pack *pack, FILE *outfile,
1093 4788f1ce 2020-03-18 stsp FILE *base_file, FILE *accum_file)
1094 efd2a263 2018-01-19 stsp {
1095 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1096 6691714a 2018-01-23 stsp struct got_delta *delta;
1097 42c69117 2019-11-10 stsp uint8_t *base_buf = NULL, *accum_buf = NULL, *delta_buf;
1098 42c69117 2019-11-10 stsp size_t base_bufsz = 0, accum_size = 0, delta_len;
1099 22484865 2018-03-13 stsp uint64_t max_size;
1100 6691714a 2018-01-23 stsp int n = 0;
1101 b29656e2 2018-03-16 stsp
1102 b29656e2 2018-03-16 stsp *result_size = 0;
1103 3ee5fc21 2018-01-17 stsp
1104 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&deltas->entries))
1105 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1106 efd2a263 2018-01-19 stsp
1107 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1108 d582f26c 2020-03-18 stsp err = got_pack_get_delta_chain_max_size(&max_size, deltas, pack);
1109 22484865 2018-03-13 stsp if (err)
1110 22484865 2018-03-13 stsp return err;
1111 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1112 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1113 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1114 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1115 3840f4c9 2018-09-12 stsp base_file = NULL;
1116 3840f4c9 2018-09-12 stsp accum_file = NULL;
1117 b490237f 2022-01-12 thomas } else {
1118 b490237f 2022-01-12 thomas if (fseeko(base_file, 0L, SEEK_SET) == -1)
1119 b490237f 2022-01-12 thomas return got_error_from_errno("fseeko");
1120 b490237f 2022-01-12 thomas if (fseeko(accum_file, 0L, SEEK_SET) == -1)
1121 b490237f 2022-01-12 thomas return got_error_from_errno("fseeko");
1122 6691714a 2018-01-23 stsp }
1123 efd2a263 2018-01-19 stsp
1124 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1125 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1126 ab2f42e7 2019-11-10 stsp int cached = 1;
1127 a6b158cc 2018-02-11 stsp if (n == 0) {
1128 34fca9c3 2018-11-11 stsp size_t mapoff;
1129 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1130 9db65d41 2018-03-14 stsp
1131 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1132 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1133 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1134 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1135 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1136 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1137 72eb3431 2018-04-01 stsp goto done;
1138 72eb3431 2018-04-01 stsp }
1139 72eb3431 2018-04-01 stsp
1140 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1141 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1142 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1143 0c048b15 2018-04-27 stsp goto done;
1144 0c048b15 2018-04-27 stsp }
1145 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1146 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1147 d7464085 2018-07-09 stsp == -1) {
1148 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1149 d7464085 2018-07-09 stsp goto done;
1150 d7464085 2018-07-09 stsp }
1151 bdd2fbb3 2018-02-11 stsp }
1152 d7464085 2018-07-09 stsp if (base_file) {
1153 d7464085 2018-07-09 stsp if (pack->map) {
1154 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1155 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1156 4788f1ce 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->map,
1157 4788f1ce 2020-03-18 stsp mapoff, pack->filesize - mapoff,
1158 4788f1ce 2020-03-18 stsp base_file);
1159 d7464085 2018-07-09 stsp } else
1160 34fca9c3 2018-11-11 stsp err = got_inflate_to_file_fd(
1161 4788f1ce 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->fd,
1162 4788f1ce 2020-03-18 stsp base_file);
1163 d7464085 2018-07-09 stsp } else {
1164 d7464085 2018-07-09 stsp if (pack->map) {
1165 d7464085 2018-07-09 stsp mapoff = (size_t)delta_data_offset;
1166 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1167 2e5a6fad 2020-03-18 stsp &base_bufsz, NULL, NULL,
1168 2e5a6fad 2020-03-18 stsp pack->map, mapoff,
1169 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1170 d7464085 2018-07-09 stsp } else
1171 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1172 55fdd257 2020-03-18 stsp &base_bufsz, NULL, NULL, max_size,
1173 55fdd257 2020-03-18 stsp pack->fd);
1174 8628c62d 2018-03-15 stsp }
1175 a6b158cc 2018-02-11 stsp if (err)
1176 a6b158cc 2018-02-11 stsp goto done;
1177 a6b158cc 2018-02-11 stsp n++;
1178 8628c62d 2018-03-15 stsp if (base_file)
1179 8628c62d 2018-03-15 stsp rewind(base_file);
1180 a6b158cc 2018-02-11 stsp continue;
1181 9db65d41 2018-03-14 stsp }
1182 885d3e02 2018-01-27 stsp
1183 ab2f42e7 2019-11-10 stsp got_delta_cache_get(&delta_buf, &delta_len,
1184 ab2f42e7 2019-11-10 stsp pack->delta_cache, delta->data_offset);
1185 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1186 ab2f42e7 2019-11-10 stsp cached = 0;
1187 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
1188 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1189 ab2f42e7 2019-11-10 stsp if (err)
1190 ab2f42e7 2019-11-10 stsp goto done;
1191 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1192 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1193 ab2f42e7 2019-11-10 stsp if (err == NULL)
1194 ab2f42e7 2019-11-10 stsp cached = 1;
1195 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1196 ab2f42e7 2019-11-10 stsp free(delta_buf);
1197 ab2f42e7 2019-11-10 stsp goto done;
1198 ab2f42e7 2019-11-10 stsp }
1199 ab2f42e7 2019-11-10 stsp }
1200 8628c62d 2018-03-15 stsp if (base_buf) {
1201 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1202 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1203 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1204 8628c62d 2018-03-15 stsp n++;
1205 8628c62d 2018-03-15 stsp } else {
1206 42c69117 2019-11-10 stsp err = got_delta_apply(base_file, delta_buf,
1207 42c69117 2019-11-10 stsp delta_len,
1208 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1209 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1210 b29656e2 2018-03-16 stsp &accum_size);
1211 8628c62d 2018-03-15 stsp }
1212 ab2f42e7 2019-11-10 stsp if (!cached)
1213 ab2f42e7 2019-11-10 stsp free(delta_buf);
1214 6691714a 2018-01-23 stsp if (err)
1215 6691714a 2018-01-23 stsp goto done;
1216 6691714a 2018-01-23 stsp
1217 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1218 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1219 8628c62d 2018-03-15 stsp if (base_buf) {
1220 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1221 34fca9c3 2018-11-11 stsp /*
1222 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation
1223 34fca9c3 2018-11-11 stsp * buffer. Ensure it can hold the largest
1224 34fca9c3 2018-11-11 stsp * result in the delta chain. The initial
1225 34fca9c3 2018-11-11 stsp * allocation might have been smaller.
1226 34fca9c3 2018-11-11 stsp */
1227 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1228 34fca9c3 2018-11-11 stsp uint8_t *p;
1229 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1230 34fca9c3 2018-11-11 stsp if (p == NULL) {
1231 638f9024 2019-05-13 stsp err = got_error_from_errno(
1232 230a42bd 2019-05-11 jcs "reallocarray");
1233 34fca9c3 2018-11-11 stsp goto done;
1234 34fca9c3 2018-11-11 stsp }
1235 34fca9c3 2018-11-11 stsp base_buf = p;
1236 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1237 34fca9c3 2018-11-11 stsp }
1238 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1239 8628c62d 2018-03-15 stsp base_buf = tmp;
1240 8628c62d 2018-03-15 stsp } else {
1241 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1242 8628c62d 2018-03-15 stsp accum_file = base_file;
1243 8628c62d 2018-03-15 stsp base_file = tmp;
1244 8628c62d 2018-03-15 stsp rewind(base_file);
1245 8628c62d 2018-03-15 stsp rewind(accum_file);
1246 8628c62d 2018-03-15 stsp }
1247 6691714a 2018-01-23 stsp }
1248 6691714a 2018-01-23 stsp }
1249 6691714a 2018-01-23 stsp
1250 6691714a 2018-01-23 stsp done:
1251 8628c62d 2018-03-15 stsp free(base_buf);
1252 8628c62d 2018-03-15 stsp if (accum_buf) {
1253 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1254 8628c62d 2018-03-15 stsp free(accum_buf);
1255 8628c62d 2018-03-15 stsp if (len != accum_size)
1256 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1257 8628c62d 2018-03-15 stsp }
1258 6691714a 2018-01-23 stsp rewind(outfile);
1259 b29656e2 2018-03-16 stsp if (err == NULL)
1260 b29656e2 2018-03-16 stsp *result_size = accum_size;
1261 e0ab43e7 2018-03-16 stsp return err;
1262 e0ab43e7 2018-03-16 stsp }
1263 e0ab43e7 2018-03-16 stsp
1264 668a20f6 2020-03-18 stsp const struct got_error *
1265 668a20f6 2020-03-18 stsp got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1266 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1267 e0ab43e7 2018-03-16 stsp {
1268 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1269 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1270 42c69117 2019-11-10 stsp uint8_t *base_buf = NULL, *accum_buf = NULL, *delta_buf;
1271 42c69117 2019-11-10 stsp size_t base_bufsz = 0, accum_size = 0, delta_len;
1272 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1273 e0ab43e7 2018-03-16 stsp int n = 0;
1274 e0ab43e7 2018-03-16 stsp
1275 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1276 e0ab43e7 2018-03-16 stsp *outlen = 0;
1277 e0ab43e7 2018-03-16 stsp
1278 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&deltas->entries))
1279 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1280 e0ab43e7 2018-03-16 stsp
1281 d582f26c 2020-03-18 stsp err = got_pack_get_delta_chain_max_size(&max_size, deltas, pack);
1282 e0ab43e7 2018-03-16 stsp if (err)
1283 e0ab43e7 2018-03-16 stsp return err;
1284 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1285 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1286 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
1287 e0ab43e7 2018-03-16 stsp
1288 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1289 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1290 ab2f42e7 2019-11-10 stsp int cached = 1;
1291 e0ab43e7 2018-03-16 stsp if (n == 0) {
1292 0c048b15 2018-04-27 stsp size_t delta_data_offset;
1293 e0ab43e7 2018-03-16 stsp
1294 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1295 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1296 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1297 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1298 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1299 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1300 72eb3431 2018-04-01 stsp goto done;
1301 72eb3431 2018-04-01 stsp }
1302 72eb3431 2018-04-01 stsp
1303 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1304 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1305 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1306 0c048b15 2018-04-27 stsp goto done;
1307 0c048b15 2018-04-27 stsp }
1308 d7464085 2018-07-09 stsp if (pack->map) {
1309 d7464085 2018-07-09 stsp size_t mapoff = (size_t)delta_data_offset;
1310 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1311 2e5a6fad 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->map,
1312 2e5a6fad 2020-03-18 stsp mapoff, pack->filesize - mapoff);
1313 d7464085 2018-07-09 stsp } else {
1314 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1315 d7464085 2018-07-09 stsp == -1) {
1316 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1317 d7464085 2018-07-09 stsp goto done;
1318 d7464085 2018-07-09 stsp }
1319 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1320 55fdd257 2020-03-18 stsp &base_bufsz, NULL, NULL, max_size,
1321 55fdd257 2020-03-18 stsp pack->fd);
1322 e0ab43e7 2018-03-16 stsp }
1323 d7464085 2018-07-09 stsp if (err)
1324 d7464085 2018-07-09 stsp goto done;
1325 e0ab43e7 2018-03-16 stsp n++;
1326 e0ab43e7 2018-03-16 stsp continue;
1327 e0ab43e7 2018-03-16 stsp }
1328 e0ab43e7 2018-03-16 stsp
1329 ab2f42e7 2019-11-10 stsp got_delta_cache_get(&delta_buf, &delta_len,
1330 ab2f42e7 2019-11-10 stsp pack->delta_cache, delta->data_offset);
1331 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1332 ab2f42e7 2019-11-10 stsp cached = 0;
1333 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
1334 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1335 ab2f42e7 2019-11-10 stsp if (err)
1336 ab2f42e7 2019-11-10 stsp goto done;
1337 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1338 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1339 ab2f42e7 2019-11-10 stsp if (err == NULL)
1340 ab2f42e7 2019-11-10 stsp cached = 1;
1341 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1342 ab2f42e7 2019-11-10 stsp free(delta_buf);
1343 ab2f42e7 2019-11-10 stsp goto done;
1344 ab2f42e7 2019-11-10 stsp }
1345 ab2f42e7 2019-11-10 stsp }
1346 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1347 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1348 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1349 ab2f42e7 2019-11-10 stsp if (!cached)
1350 ab2f42e7 2019-11-10 stsp free(delta_buf);
1351 e0ab43e7 2018-03-16 stsp n++;
1352 e0ab43e7 2018-03-16 stsp if (err)
1353 e0ab43e7 2018-03-16 stsp goto done;
1354 e0ab43e7 2018-03-16 stsp
1355 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1356 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1357 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1358 34fca9c3 2018-11-11 stsp /*
1359 34fca9c3 2018-11-11 stsp * Base buffer switches roles with accumulation buffer.
1360 34fca9c3 2018-11-11 stsp * Ensure it can hold the largest result in the delta
1361 34fca9c3 2018-11-11 stsp * chain. Initial allocation might have been smaller.
1362 34fca9c3 2018-11-11 stsp */
1363 34fca9c3 2018-11-11 stsp if (base_bufsz < max_size) {
1364 34fca9c3 2018-11-11 stsp uint8_t *p;
1365 34fca9c3 2018-11-11 stsp p = reallocarray(base_buf, 1, max_size);
1366 34fca9c3 2018-11-11 stsp if (p == NULL) {
1367 638f9024 2019-05-13 stsp err = got_error_from_errno(
1368 230a42bd 2019-05-11 jcs "reallocarray");
1369 34fca9c3 2018-11-11 stsp goto done;
1370 34fca9c3 2018-11-11 stsp }
1371 34fca9c3 2018-11-11 stsp base_buf = p;
1372 34fca9c3 2018-11-11 stsp base_bufsz = max_size;
1373 34fca9c3 2018-11-11 stsp }
1374 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1375 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1376 e0ab43e7 2018-03-16 stsp }
1377 e0ab43e7 2018-03-16 stsp }
1378 e0ab43e7 2018-03-16 stsp
1379 e0ab43e7 2018-03-16 stsp done:
1380 e0ab43e7 2018-03-16 stsp free(base_buf);
1381 e0ab43e7 2018-03-16 stsp if (err) {
1382 e0ab43e7 2018-03-16 stsp free(accum_buf);
1383 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1384 e0ab43e7 2018-03-16 stsp *outlen = 0;
1385 e0ab43e7 2018-03-16 stsp } else {
1386 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1387 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1388 e0ab43e7 2018-03-16 stsp }
1389 efd2a263 2018-01-19 stsp return err;
1390 efd2a263 2018-01-19 stsp }
1391 efd2a263 2018-01-19 stsp
1392 3ee5fc21 2018-01-17 stsp const struct got_error *
1393 24140570 2018-09-09 stsp got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1394 3840f4c9 2018-09-12 stsp FILE *outfile, FILE *base_file, FILE *accum_file)
1395 3ee5fc21 2018-01-17 stsp {
1396 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1397 3ee5fc21 2018-01-17 stsp
1398 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1399 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1400 2ce68b2f 2018-09-09 stsp
1401 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1402 24140570 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1403 24140570 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1404 72eb3431 2018-04-01 stsp
1405 d7464085 2018-07-09 stsp if (pack->map) {
1406 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1407 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_mmap(&obj->size, NULL, NULL,
1408 4788f1ce 2020-03-18 stsp pack->map, mapoff, pack->filesize - mapoff,
1409 4788f1ce 2020-03-18 stsp outfile);
1410 d7464085 2018-07-09 stsp } else {
1411 24140570 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1412 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1413 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_fd(&obj->size, NULL, NULL,
1414 4788f1ce 2020-03-18 stsp pack->fd, outfile);
1415 d7464085 2018-07-09 stsp }
1416 040bf4a1 2018-04-01 stsp } else
1417 4788f1ce 2020-03-18 stsp err = got_pack_dump_delta_chain_to_file(&obj->size,
1418 4788f1ce 2020-03-18 stsp &obj->deltas, pack, outfile, base_file, accum_file);
1419 24140570 2018-09-09 stsp
1420 a1fd68d8 2018-01-12 stsp return err;
1421 a1fd68d8 2018-01-12 stsp }
1422 e0ab43e7 2018-03-16 stsp
1423 e0ab43e7 2018-03-16 stsp const struct got_error *
1424 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1425 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_pack *pack)
1426 e0ab43e7 2018-03-16 stsp {
1427 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1428 e0ab43e7 2018-03-16 stsp
1429 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1430 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1431 72eb3431 2018-04-01 stsp
1432 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1433 7e212e3d 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1434 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1435 d7464085 2018-07-09 stsp if (pack->map) {
1436 d7464085 2018-07-09 stsp size_t mapoff = (size_t)obj->pack_offset;
1437 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
1438 2e5a6fad 2020-03-18 stsp pack->map, mapoff, pack->filesize - mapoff);
1439 d7464085 2018-07-09 stsp } else {
1440 7e212e3d 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1441 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1442 1e87a3c3 2020-03-18 stsp err = got_inflate_to_mem_fd(buf, len, NULL, NULL,
1443 55fdd257 2020-03-18 stsp obj->size, pack->fd);
1444 e0ab43e7 2018-03-16 stsp }
1445 e0ab43e7 2018-03-16 stsp } else
1446 668a20f6 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
1447 668a20f6 2020-03-18 stsp pack);
1448 7e212e3d 2018-09-09 stsp
1449 e0ab43e7 2018-03-16 stsp return err;
1450 e0ab43e7 2018-03-16 stsp }