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 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>
22 3d589bee 2022-06-25 stsp #include <sys/resource.h>
23 3d589bee 2022-06-25 stsp #include <sys/socket.h>
24 0a0a3048 2018-01-10 stsp
25 7e656b93 2018-03-17 stsp #include <fcntl.h>
26 a1fd68d8 2018-01-12 stsp #include <errno.h>
27 0a0a3048 2018-01-10 stsp #include <stdio.h>
28 a1fd68d8 2018-01-12 stsp #include <stdint.h>
29 0a0a3048 2018-01-10 stsp #include <stdlib.h>
30 0a0a3048 2018-01-10 stsp #include <string.h>
31 0a0a3048 2018-01-10 stsp #include <limits.h>
32 0a0a3048 2018-01-10 stsp #include <sha1.h>
33 69c6accf 2023-02-04 op #include <sha2.h>
34 0a0a3048 2018-01-10 stsp #include <endian.h>
35 81a12da5 2020-09-09 naddy #include <unistd.h>
36 a1fd68d8 2018-01-12 stsp #include <zlib.h>
37 876c234b 2018-09-10 stsp #include <imsg.h>
38 0a0a3048 2018-01-10 stsp
39 0a0a3048 2018-01-10 stsp #include "got_error.h"
40 a1fd68d8 2018-01-12 stsp #include "got_object.h"
41 324d37e7 2019-05-11 stsp #include "got_path.h"
42 0a0a3048 2018-01-10 stsp
43 1362b0e3 2023-02-04 op #include "got_lib_hash.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 dd88155e 2019-06-29 stsp #include "got_lib_object_parse.h"
49 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
50 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
51 79b11c62 2018-03-09 stsp
52 79b11c62 2018-03-09 stsp #ifndef nitems
53 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 79b11c62 2018-03-09 stsp #endif
55 1411938b 2018-02-12 stsp
56 a1fd68d8 2018-01-12 stsp #ifndef MIN
57 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 a1fd68d8 2018-01-12 stsp #endif
59 a1fd68d8 2018-01-12 stsp
60 0a0a3048 2018-01-10 stsp static const struct got_error *
61 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
62 0a0a3048 2018-01-10 stsp {
63 0a0a3048 2018-01-10 stsp int i;
64 0a0a3048 2018-01-10 stsp
65 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
66 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
67 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
68 0a0a3048 2018-01-10 stsp }
69 0a0a3048 2018-01-10 stsp
70 0a0a3048 2018-01-10 stsp return NULL;
71 0a0a3048 2018-01-10 stsp }
72 0a0a3048 2018-01-10 stsp
73 1510f469 2018-09-09 stsp const struct got_error *
74 c3564dfa 2021-07-15 stsp got_packidx_init_hdr(struct got_packidx *p, int verify, off_t packfile_size)
75 0a0a3048 2018-01-10 stsp {
76 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
77 817c5a18 2018-09-09 stsp struct got_packidx_v2_hdr *h;
78 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
79 3093e2d7 2023-02-04 op uint8_t hash[SHA1_DIGEST_LENGTH];
80 817c5a18 2018-09-09 stsp size_t nobj, len_fanout, len_ids, offset, remain;
81 817c5a18 2018-09-09 stsp ssize_t n;
82 5e6be232 2019-11-08 stsp int i;
83 0a0a3048 2018-01-10 stsp
84 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
85 0ebaf008 2018-01-10 stsp
86 57b35b75 2018-06-22 stsp h = &p->hdr;
87 57b35b75 2018-06-22 stsp offset = 0;
88 57b35b75 2018-06-22 stsp remain = p->len;
89 0a0a3048 2018-01-10 stsp
90 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->magic)) {
91 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
92 0a0a3048 2018-01-10 stsp goto done;
93 0a0a3048 2018-01-10 stsp }
94 fc79a48d 2018-07-09 stsp if (p->map)
95 fc79a48d 2018-07-09 stsp h->magic = (uint32_t *)(p->map + offset);
96 fc79a48d 2018-07-09 stsp else {
97 fc79a48d 2018-07-09 stsp h->magic = malloc(sizeof(*h->magic));
98 fc79a48d 2018-07-09 stsp if (h->magic == NULL) {
99 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
100 fc79a48d 2018-07-09 stsp goto done;
101 fc79a48d 2018-07-09 stsp }
102 fc79a48d 2018-07-09 stsp n = read(p->fd, h->magic, sizeof(*h->magic));
103 b1317e77 2019-09-22 stsp if (n < 0) {
104 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
105 b1317e77 2019-09-22 stsp goto done;
106 b1317e77 2019-09-22 stsp } else if (n != sizeof(*h->magic)) {
107 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
108 fc79a48d 2018-07-09 stsp goto done;
109 fc79a48d 2018-07-09 stsp }
110 fc79a48d 2018-07-09 stsp }
111 ac62b712 2021-03-30 stsp if (*h->magic != htobe32(GOT_PACKIDX_V2_MAGIC)) {
112 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
113 57b35b75 2018-06-22 stsp goto done;
114 57b35b75 2018-06-22 stsp }
115 57b35b75 2018-06-22 stsp offset += sizeof(*h->magic);
116 57b35b75 2018-06-22 stsp remain -= sizeof(*h->magic);
117 0a0a3048 2018-01-10 stsp
118 0cb74cf4 2018-07-08 stsp if (verify)
119 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
120 0ebaf008 2018-01-10 stsp
121 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->version)) {
122 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
123 0a0a3048 2018-01-10 stsp goto done;
124 0a0a3048 2018-01-10 stsp }
125 fc79a48d 2018-07-09 stsp if (p->map)
126 fc79a48d 2018-07-09 stsp h->version = (uint32_t *)(p->map + offset);
127 fc79a48d 2018-07-09 stsp else {
128 fc79a48d 2018-07-09 stsp h->version = malloc(sizeof(*h->version));
129 fc79a48d 2018-07-09 stsp if (h->version == NULL) {
130 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
131 fc79a48d 2018-07-09 stsp goto done;
132 fc79a48d 2018-07-09 stsp }
133 fc79a48d 2018-07-09 stsp n = read(p->fd, h->version, sizeof(*h->version));
134 c6368c2e 2019-10-11 stsp if (n < 0) {
135 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
136 c6368c2e 2019-10-11 stsp goto done;
137 c6368c2e 2019-10-11 stsp } else if (n != sizeof(*h->version)) {
138 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
139 fc79a48d 2018-07-09 stsp goto done;
140 fc79a48d 2018-07-09 stsp }
141 fc79a48d 2018-07-09 stsp }
142 ac62b712 2021-03-30 stsp if (*h->version != htobe32(GOT_PACKIDX_VERSION)) {
143 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
144 0a0a3048 2018-01-10 stsp goto done;
145 0a0a3048 2018-01-10 stsp }
146 57b35b75 2018-06-22 stsp offset += sizeof(*h->version);
147 57b35b75 2018-06-22 stsp remain -= sizeof(*h->version);
148 0a0a3048 2018-01-10 stsp
149 0cb74cf4 2018-07-08 stsp if (verify)
150 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
151 0ebaf008 2018-01-10 stsp
152 57b35b75 2018-06-22 stsp len_fanout =
153 57b35b75 2018-06-22 stsp sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
154 57b35b75 2018-06-22 stsp if (remain < len_fanout) {
155 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
156 0a0a3048 2018-01-10 stsp goto done;
157 0a0a3048 2018-01-10 stsp }
158 fc79a48d 2018-07-09 stsp if (p->map)
159 fc79a48d 2018-07-09 stsp h->fanout_table = (uint32_t *)(p->map + offset);
160 fc79a48d 2018-07-09 stsp else {
161 fc79a48d 2018-07-09 stsp h->fanout_table = malloc(len_fanout);
162 fc79a48d 2018-07-09 stsp if (h->fanout_table == NULL) {
163 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
164 fc79a48d 2018-07-09 stsp goto done;
165 fc79a48d 2018-07-09 stsp }
166 fc79a48d 2018-07-09 stsp n = read(p->fd, h->fanout_table, len_fanout);
167 c6368c2e 2019-10-11 stsp if (n < 0) {
168 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
169 c6368c2e 2019-10-11 stsp goto done;
170 c6368c2e 2019-10-11 stsp } else if (n != len_fanout) {
171 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
172 fc79a48d 2018-07-09 stsp goto done;
173 fc79a48d 2018-07-09 stsp }
174 fc79a48d 2018-07-09 stsp }
175 c8262310 2018-06-04 stsp err = verify_fanout_table(h->fanout_table);
176 0a0a3048 2018-01-10 stsp if (err)
177 0a0a3048 2018-01-10 stsp goto done;
178 0cb74cf4 2018-07-08 stsp if (verify)
179 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
180 57b35b75 2018-06-22 stsp offset += len_fanout;
181 57b35b75 2018-06-22 stsp remain -= len_fanout;
182 0a0a3048 2018-01-10 stsp
183 78fb0967 2020-09-09 naddy nobj = be32toh(h->fanout_table[0xff]);
184 57b35b75 2018-06-22 stsp len_ids = nobj * sizeof(*h->sorted_ids);
185 57b35b75 2018-06-22 stsp if (len_ids <= nobj || len_ids > remain) {
186 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
187 0a0a3048 2018-01-10 stsp goto done;
188 0a0a3048 2018-01-10 stsp }
189 fc79a48d 2018-07-09 stsp if (p->map)
190 fc79a48d 2018-07-09 stsp h->sorted_ids =
191 fc79a48d 2018-07-09 stsp (struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
192 fc79a48d 2018-07-09 stsp else {
193 fc79a48d 2018-07-09 stsp h->sorted_ids = malloc(len_ids);
194 fc79a48d 2018-07-09 stsp if (h->sorted_ids == NULL) {
195 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
196 fc79a48d 2018-07-09 stsp goto done;
197 fc79a48d 2018-07-09 stsp }
198 fc79a48d 2018-07-09 stsp n = read(p->fd, h->sorted_ids, len_ids);
199 faaa1c0f 2018-09-15 stsp if (n < 0)
200 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
201 faaa1c0f 2018-09-15 stsp else if (n != len_ids) {
202 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
203 fc79a48d 2018-07-09 stsp goto done;
204 fc79a48d 2018-07-09 stsp }
205 fc79a48d 2018-07-09 stsp }
206 0cb74cf4 2018-07-08 stsp if (verify)
207 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
208 57b35b75 2018-06-22 stsp offset += len_ids;
209 57b35b75 2018-06-22 stsp remain -= len_ids;
210 0a0a3048 2018-01-10 stsp
211 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->crc32)) {
212 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
213 0a0a3048 2018-01-10 stsp goto done;
214 0a0a3048 2018-01-10 stsp }
215 fc79a48d 2018-07-09 stsp if (p->map)
216 fc79a48d 2018-07-09 stsp h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
217 fc79a48d 2018-07-09 stsp else {
218 fc79a48d 2018-07-09 stsp h->crc32 = malloc(nobj * sizeof(*h->crc32));
219 fc79a48d 2018-07-09 stsp if (h->crc32 == NULL) {
220 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
221 fc79a48d 2018-07-09 stsp goto done;
222 fc79a48d 2018-07-09 stsp }
223 fc79a48d 2018-07-09 stsp n = read(p->fd, h->crc32, nobj * sizeof(*h->crc32));
224 faaa1c0f 2018-09-15 stsp if (n < 0)
225 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
226 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->crc32)) {
227 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
228 fc79a48d 2018-07-09 stsp goto done;
229 fc79a48d 2018-07-09 stsp }
230 fc79a48d 2018-07-09 stsp }
231 0cb74cf4 2018-07-08 stsp if (verify)
232 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
233 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->crc32);
234 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->crc32);
235 0ebaf008 2018-01-10 stsp
236 57b35b75 2018-06-22 stsp if (remain < nobj * sizeof(*h->offsets)) {
237 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
238 0a0a3048 2018-01-10 stsp goto done;
239 0a0a3048 2018-01-10 stsp }
240 fc79a48d 2018-07-09 stsp if (p->map)
241 fc79a48d 2018-07-09 stsp h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
242 fc79a48d 2018-07-09 stsp else {
243 fc79a48d 2018-07-09 stsp h->offsets = malloc(nobj * sizeof(*h->offsets));
244 fc79a48d 2018-07-09 stsp if (h->offsets == NULL) {
245 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
246 fc79a48d 2018-07-09 stsp goto done;
247 fc79a48d 2018-07-09 stsp }
248 fc79a48d 2018-07-09 stsp n = read(p->fd, h->offsets, nobj * sizeof(*h->offsets));
249 faaa1c0f 2018-09-15 stsp if (n < 0)
250 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
251 faaa1c0f 2018-09-15 stsp else if (n != nobj * sizeof(*h->offsets)) {
252 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
253 fc79a48d 2018-07-09 stsp goto done;
254 fc79a48d 2018-07-09 stsp }
255 fc79a48d 2018-07-09 stsp }
256 0cb74cf4 2018-07-08 stsp if (verify)
257 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t *)h->offsets,
258 0cb74cf4 2018-07-08 stsp nobj * sizeof(*h->offsets));
259 57b35b75 2018-06-22 stsp remain -= nobj * sizeof(*h->offsets);
260 57b35b75 2018-06-22 stsp offset += nobj * sizeof(*h->offsets);
261 0ebaf008 2018-01-10 stsp
262 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
263 c3564dfa 2021-07-15 stsp if (verify || packfile_size > 0x7fffffff) {
264 c3564dfa 2021-07-15 stsp for (i = 0; i < nobj; i++) {
265 c3564dfa 2021-07-15 stsp uint32_t o = h->offsets[i];
266 c3564dfa 2021-07-15 stsp if (o & htobe32(GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX))
267 c3564dfa 2021-07-15 stsp p->nlargeobj++;
268 c3564dfa 2021-07-15 stsp }
269 5e6be232 2019-11-08 stsp }
270 5e6be232 2019-11-08 stsp if (p->nlargeobj == 0)
271 0a0a3048 2018-01-10 stsp goto checksum;
272 c3564dfa 2021-07-15 stsp else if (packfile_size <= 0x7fffffff) {
273 c3564dfa 2021-07-15 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
274 c3564dfa 2021-07-15 stsp goto done;
275 c3564dfa 2021-07-15 stsp }
276 0a0a3048 2018-01-10 stsp
277 5e6be232 2019-11-08 stsp if (remain < p->nlargeobj * sizeof(*h->large_offsets)) {
278 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
279 0a0a3048 2018-01-10 stsp goto done;
280 0a0a3048 2018-01-10 stsp }
281 fc79a48d 2018-07-09 stsp if (p->map)
282 fc79a48d 2018-07-09 stsp h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
283 fc79a48d 2018-07-09 stsp else {
284 5e6be232 2019-11-08 stsp h->large_offsets = malloc(p->nlargeobj *
285 5e6be232 2019-11-08 stsp sizeof(*h->large_offsets));
286 de30857e 2019-08-23 stsp if (h->large_offsets == NULL) {
287 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
288 fc79a48d 2018-07-09 stsp goto done;
289 fc79a48d 2018-07-09 stsp }
290 fc79a48d 2018-07-09 stsp n = read(p->fd, h->large_offsets,
291 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
292 faaa1c0f 2018-09-15 stsp if (n < 0)
293 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
294 5e6be232 2019-11-08 stsp else if (n != p->nlargeobj * sizeof(*h->large_offsets)) {
295 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
296 fc79a48d 2018-07-09 stsp goto done;
297 fc79a48d 2018-07-09 stsp }
298 fc79a48d 2018-07-09 stsp }
299 0cb74cf4 2018-07-08 stsp if (verify)
300 0cb74cf4 2018-07-08 stsp SHA1Update(&ctx, (uint8_t*)h->large_offsets,
301 5e6be232 2019-11-08 stsp p->nlargeobj * sizeof(*h->large_offsets));
302 5e6be232 2019-11-08 stsp remain -= p->nlargeobj * sizeof(*h->large_offsets);
303 5e6be232 2019-11-08 stsp offset += p->nlargeobj * sizeof(*h->large_offsets);
304 0ebaf008 2018-01-10 stsp
305 0a0a3048 2018-01-10 stsp checksum:
306 57b35b75 2018-06-22 stsp if (remain < sizeof(*h->trailer)) {
307 57b35b75 2018-06-22 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
308 0a0a3048 2018-01-10 stsp goto done;
309 0a0a3048 2018-01-10 stsp }
310 fc79a48d 2018-07-09 stsp if (p->map)
311 fc79a48d 2018-07-09 stsp h->trailer =
312 fc79a48d 2018-07-09 stsp (struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
313 fc79a48d 2018-07-09 stsp else {
314 fc79a48d 2018-07-09 stsp h->trailer = malloc(sizeof(*h->trailer));
315 fc79a48d 2018-07-09 stsp if (h->trailer == NULL) {
316 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
317 fc79a48d 2018-07-09 stsp goto done;
318 fc79a48d 2018-07-09 stsp }
319 fc79a48d 2018-07-09 stsp n = read(p->fd, h->trailer, sizeof(*h->trailer));
320 faaa1c0f 2018-09-15 stsp if (n < 0)
321 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
322 faaa1c0f 2018-09-15 stsp else if (n != sizeof(*h->trailer)) {
323 fc79a48d 2018-07-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
324 fc79a48d 2018-07-09 stsp goto done;
325 fc79a48d 2018-07-09 stsp }
326 fc79a48d 2018-07-09 stsp }
327 0cb74cf4 2018-07-08 stsp if (verify) {
328 3093e2d7 2023-02-04 op SHA1Update(&ctx, h->trailer->packfile_hash, SHA1_DIGEST_LENGTH);
329 3093e2d7 2023-02-04 op SHA1Final(hash, &ctx);
330 3093e2d7 2023-02-04 op if (memcmp(h->trailer->packidx_hash, hash,
331 0cb74cf4 2018-07-08 stsp SHA1_DIGEST_LENGTH) != 0)
332 0cb74cf4 2018-07-08 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
333 0cb74cf4 2018-07-08 stsp }
334 0a0a3048 2018-01-10 stsp done:
335 817c5a18 2018-09-09 stsp return err;
336 817c5a18 2018-09-09 stsp }
337 817c5a18 2018-09-09 stsp
338 817c5a18 2018-09-09 stsp const struct got_error *
339 6d5a9006 2020-12-16 yzhong got_packidx_open(struct got_packidx **packidx,
340 6d5a9006 2020-12-16 yzhong int dir_fd, const char *relpath, int verify)
341 817c5a18 2018-09-09 stsp {
342 817c5a18 2018-09-09 stsp const struct got_error *err = NULL;
343 1124fe40 2021-07-07 stsp struct got_packidx *p = NULL;
344 1124fe40 2021-07-07 stsp char *pack_relpath;
345 c3564dfa 2021-07-15 stsp struct stat idx_sb, pack_sb;
346 817c5a18 2018-09-09 stsp
347 817c5a18 2018-09-09 stsp *packidx = NULL;
348 1124fe40 2021-07-07 stsp
349 1124fe40 2021-07-07 stsp err = got_packidx_get_packfile_path(&pack_relpath, relpath);
350 1124fe40 2021-07-07 stsp if (err)
351 1124fe40 2021-07-07 stsp return err;
352 1124fe40 2021-07-07 stsp
353 1124fe40 2021-07-07 stsp /*
354 1124fe40 2021-07-07 stsp * Ensure that a corresponding pack file exists.
355 1124fe40 2021-07-07 stsp * Some Git repositories have this problem. Git seems to ignore
356 1124fe40 2021-07-07 stsp * the existence of lonely pack index files but we do not.
357 1124fe40 2021-07-07 stsp */
358 c3564dfa 2021-07-15 stsp if (fstatat(dir_fd, pack_relpath, &pack_sb, 0) == -1) {
359 1124fe40 2021-07-07 stsp if (errno == ENOENT) {
360 1124fe40 2021-07-07 stsp err = got_error_fmt(GOT_ERR_LONELY_PACKIDX,
361 1124fe40 2021-07-07 stsp "%s", relpath);
362 1124fe40 2021-07-07 stsp } else
363 1124fe40 2021-07-07 stsp err = got_error_from_errno2("fstatat", pack_relpath);
364 1124fe40 2021-07-07 stsp goto done;
365 1124fe40 2021-07-07 stsp }
366 817c5a18 2018-09-09 stsp
367 817c5a18 2018-09-09 stsp p = calloc(1, sizeof(*p));
368 1124fe40 2021-07-07 stsp if (p == NULL) {
369 1124fe40 2021-07-07 stsp err = got_error_from_errno("calloc");
370 1124fe40 2021-07-07 stsp goto done;
371 1124fe40 2021-07-07 stsp }
372 817c5a18 2018-09-09 stsp
373 e7ae0baf 2021-12-31 stsp p->fd = openat(dir_fd, relpath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
374 6772cf22 2019-08-28 hiltjo if (p->fd == -1) {
375 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("openat", relpath);
376 1124fe40 2021-07-07 stsp goto done;
377 6772cf22 2019-08-28 hiltjo }
378 817c5a18 2018-09-09 stsp
379 c3564dfa 2021-07-15 stsp if (fstat(p->fd, &idx_sb) != 0) {
380 6d5a9006 2020-12-16 yzhong err = got_error_from_errno2("fstat", relpath);
381 1124fe40 2021-07-07 stsp goto done;
382 817c5a18 2018-09-09 stsp }
383 c3564dfa 2021-07-15 stsp p->len = idx_sb.st_size;
384 817c5a18 2018-09-09 stsp if (p->len < sizeof(p->hdr)) {
385 817c5a18 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
386 1124fe40 2021-07-07 stsp goto done;
387 817c5a18 2018-09-09 stsp }
388 817c5a18 2018-09-09 stsp
389 6d5a9006 2020-12-16 yzhong p->path_packidx = strdup(relpath);
390 817c5a18 2018-09-09 stsp if (p->path_packidx == NULL) {
391 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
392 817c5a18 2018-09-09 stsp goto done;
393 817c5a18 2018-09-09 stsp }
394 817c5a18 2018-09-09 stsp
395 817c5a18 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
396 1c28a361 2022-10-25 op if (p->len > 0 && p->len <= SIZE_MAX) {
397 1c28a361 2022-10-25 op p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
398 1c28a361 2022-10-25 op if (p->map == MAP_FAILED) {
399 1c28a361 2022-10-25 op if (errno != ENOMEM) {
400 1c28a361 2022-10-25 op err = got_error_from_errno("mmap");
401 1c28a361 2022-10-25 op goto done;
402 1c28a361 2022-10-25 op }
403 1c28a361 2022-10-25 op p->map = NULL; /* fall back to read(2) */
404 3a11398b 2019-02-21 stsp }
405 3a11398b 2019-02-21 stsp }
406 817c5a18 2018-09-09 stsp #endif
407 817c5a18 2018-09-09 stsp
408 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, verify, pack_sb.st_size);
409 817c5a18 2018-09-09 stsp done:
410 1124fe40 2021-07-07 stsp if (err) {
411 1124fe40 2021-07-07 stsp if (p)
412 1124fe40 2021-07-07 stsp got_packidx_close(p);
413 1124fe40 2021-07-07 stsp } else
414 0a0a3048 2018-01-10 stsp *packidx = p;
415 1124fe40 2021-07-07 stsp free(pack_relpath);
416 0a0a3048 2018-01-10 stsp return err;
417 0a0a3048 2018-01-10 stsp }
418 0a0a3048 2018-01-10 stsp
419 57b35b75 2018-06-22 stsp const struct got_error *
420 6fd11751 2018-06-04 stsp got_packidx_close(struct got_packidx *packidx)
421 0a0a3048 2018-01-10 stsp {
422 57b35b75 2018-06-22 stsp const struct got_error *err = NULL;
423 57b35b75 2018-06-22 stsp
424 6fd11751 2018-06-04 stsp free(packidx->path_packidx);
425 fc79a48d 2018-07-09 stsp if (packidx->map) {
426 57b35b75 2018-06-22 stsp if (munmap(packidx->map, packidx->len) == -1)
427 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
428 fc79a48d 2018-07-09 stsp } else {
429 fc79a48d 2018-07-09 stsp free(packidx->hdr.magic);
430 fc79a48d 2018-07-09 stsp free(packidx->hdr.version);
431 fc79a48d 2018-07-09 stsp free(packidx->hdr.fanout_table);
432 fc79a48d 2018-07-09 stsp free(packidx->hdr.sorted_ids);
433 fc79a48d 2018-07-09 stsp free(packidx->hdr.crc32);
434 fc79a48d 2018-07-09 stsp free(packidx->hdr.offsets);
435 fc79a48d 2018-07-09 stsp free(packidx->hdr.large_offsets);
436 fc79a48d 2018-07-09 stsp free(packidx->hdr.trailer);
437 57b35b75 2018-06-22 stsp }
438 08578a35 2021-01-22 stsp if (close(packidx->fd) == -1 && err == NULL)
439 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
440 67fd6849 2022-02-13 stsp free(packidx->sorted_offsets);
441 67fd6849 2022-02-13 stsp free(packidx->sorted_large_offsets);
442 0a0a3048 2018-01-10 stsp free(packidx);
443 57b35b75 2018-06-22 stsp
444 57b35b75 2018-06-22 stsp return err;
445 a1fd68d8 2018-01-12 stsp }
446 509c9973 2021-04-10 stsp
447 509c9973 2021-04-10 stsp const struct got_error *
448 aea75d87 2021-07-06 stsp got_packidx_get_packfile_path(char **path_packfile, const char *path_packidx)
449 509c9973 2021-04-10 stsp {
450 509c9973 2021-04-10 stsp size_t size;
451 509c9973 2021-04-10 stsp
452 509c9973 2021-04-10 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
453 aea75d87 2021-07-06 stsp size = strlen(path_packidx) + 2;
454 509c9973 2021-04-10 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
455 aea75d87 2021-07-06 stsp return got_error_path(path_packidx, GOT_ERR_BAD_PATH);
456 a1fd68d8 2018-01-12 stsp
457 509c9973 2021-04-10 stsp *path_packfile = malloc(size);
458 509c9973 2021-04-10 stsp if (*path_packfile == NULL)
459 509c9973 2021-04-10 stsp return got_error_from_errno("malloc");
460 509c9973 2021-04-10 stsp
461 509c9973 2021-04-10 stsp /* Copy up to and excluding ".idx". */
462 aea75d87 2021-07-06 stsp if (strlcpy(*path_packfile, path_packidx,
463 509c9973 2021-04-10 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
464 509c9973 2021-04-10 stsp return got_error(GOT_ERR_NO_SPACE);
465 509c9973 2021-04-10 stsp
466 509c9973 2021-04-10 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
467 509c9973 2021-04-10 stsp return got_error(GOT_ERR_NO_SPACE);
468 509c9973 2021-04-10 stsp
469 509c9973 2021-04-10 stsp return NULL;
470 509c9973 2021-04-10 stsp }
471 509c9973 2021-04-10 stsp
472 02828bfd 2021-06-22 stsp off_t
473 02828bfd 2021-06-22 stsp got_packidx_get_object_offset(struct got_packidx *packidx, int idx)
474 a1fd68d8 2018-01-12 stsp {
475 78fb0967 2020-09-09 naddy uint32_t offset = be32toh(packidx->hdr.offsets[idx]);
476 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
477 a1fd68d8 2018-01-12 stsp uint64_t loffset;
478 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
479 5e6be232 2019-11-08 stsp if (idx < 0 || idx >= packidx->nlargeobj ||
480 6fd11751 2018-06-04 stsp packidx->hdr.large_offsets == NULL)
481 a1fd68d8 2018-01-12 stsp return -1;
482 78fb0967 2020-09-09 naddy loffset = be64toh(packidx->hdr.large_offsets[idx]);
483 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
484 a1fd68d8 2018-01-12 stsp }
485 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
486 a1fd68d8 2018-01-12 stsp }
487 a1fd68d8 2018-01-12 stsp
488 1510f469 2018-09-09 stsp int
489 c0df5966 2021-12-31 stsp got_packidx_get_object_idx(struct got_packidx *packidx,
490 c0df5966 2021-12-31 stsp struct got_object_id *id)
491 a1fd68d8 2018-01-12 stsp {
492 3093e2d7 2023-02-04 op u_int8_t id0 = id->hash[0];
493 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
494 40aeb19c 2018-06-22 stsp int left = 0, right = totobj - 1;
495 a1fd68d8 2018-01-12 stsp
496 a1fd68d8 2018-01-12 stsp if (id0 > 0)
497 78fb0967 2020-09-09 naddy left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
498 a1fd68d8 2018-01-12 stsp
499 40aeb19c 2018-06-22 stsp while (left <= right) {
500 57b35b75 2018-06-22 stsp struct got_packidx_object_id *oid;
501 40aeb19c 2018-06-22 stsp int i, cmp;
502 a1fd68d8 2018-01-12 stsp
503 40aeb19c 2018-06-22 stsp i = ((left + right) / 2);
504 40aeb19c 2018-06-22 stsp oid = &packidx->hdr.sorted_ids[i];
505 3093e2d7 2023-02-04 op cmp = memcmp(id->hash, oid->hash, SHA1_DIGEST_LENGTH);
506 16dcbf91 2018-04-01 stsp if (cmp == 0)
507 6c00b545 2018-01-17 stsp return i;
508 40aeb19c 2018-06-22 stsp else if (cmp > 0)
509 40aeb19c 2018-06-22 stsp left = i + 1;
510 40aeb19c 2018-06-22 stsp else if (cmp < 0)
511 40aeb19c 2018-06-22 stsp right = i - 1;
512 a1fd68d8 2018-01-12 stsp }
513 a1fd68d8 2018-01-12 stsp
514 a1fd68d8 2018-01-12 stsp return -1;
515 67fd6849 2022-02-13 stsp }
516 67fd6849 2022-02-13 stsp
517 67fd6849 2022-02-13 stsp static int
518 67fd6849 2022-02-13 stsp offset_cmp(const void *pa, const void *pb)
519 67fd6849 2022-02-13 stsp {
520 67fd6849 2022-02-13 stsp const struct got_pack_offset_index *a, *b;
521 67fd6849 2022-02-13 stsp
522 67fd6849 2022-02-13 stsp a = (const struct got_pack_offset_index *)pa;
523 67fd6849 2022-02-13 stsp b = (const struct got_pack_offset_index *)pb;
524 67fd6849 2022-02-13 stsp
525 67fd6849 2022-02-13 stsp if (a->offset < b->offset)
526 67fd6849 2022-02-13 stsp return -1;
527 67fd6849 2022-02-13 stsp else if (a->offset > b->offset)
528 67fd6849 2022-02-13 stsp return 1;
529 67fd6849 2022-02-13 stsp
530 67fd6849 2022-02-13 stsp return 0;
531 876c234b 2018-09-10 stsp }
532 876c234b 2018-09-10 stsp
533 67fd6849 2022-02-13 stsp static int
534 67fd6849 2022-02-13 stsp large_offset_cmp(const void *pa, const void *pb)
535 67fd6849 2022-02-13 stsp {
536 67fd6849 2022-02-13 stsp const struct got_pack_large_offset_index *a, *b;
537 67fd6849 2022-02-13 stsp
538 67fd6849 2022-02-13 stsp a = (const struct got_pack_large_offset_index *)pa;
539 67fd6849 2022-02-13 stsp b = (const struct got_pack_large_offset_index *)pb;
540 67fd6849 2022-02-13 stsp
541 67fd6849 2022-02-13 stsp if (a->offset < b->offset)
542 67fd6849 2022-02-13 stsp return -1;
543 67fd6849 2022-02-13 stsp else if (a->offset > b->offset)
544 67fd6849 2022-02-13 stsp return 1;
545 67fd6849 2022-02-13 stsp
546 67fd6849 2022-02-13 stsp return 0;
547 67fd6849 2022-02-13 stsp }
548 67fd6849 2022-02-13 stsp
549 67fd6849 2022-02-13 stsp static const struct got_error *
550 67fd6849 2022-02-13 stsp build_offset_index(struct got_packidx *p)
551 67fd6849 2022-02-13 stsp {
552 67fd6849 2022-02-13 stsp uint32_t nobj = be32toh(p->hdr.fanout_table[0xff]);
553 67fd6849 2022-02-13 stsp unsigned int i, j, k;
554 67fd6849 2022-02-13 stsp
555 67fd6849 2022-02-13 stsp p->sorted_offsets = calloc(nobj - p->nlargeobj,
556 67fd6849 2022-02-13 stsp sizeof(p->sorted_offsets[0]));
557 67fd6849 2022-02-13 stsp if (p->sorted_offsets == NULL)
558 67fd6849 2022-02-13 stsp return got_error_from_errno("calloc");
559 67fd6849 2022-02-13 stsp
560 67fd6849 2022-02-13 stsp if (p->nlargeobj > 0) {
561 67fd6849 2022-02-13 stsp p->sorted_large_offsets = calloc(p->nlargeobj,
562 67fd6849 2022-02-13 stsp sizeof(p->sorted_large_offsets[0]));
563 67fd6849 2022-02-13 stsp if (p->sorted_large_offsets == NULL)
564 67fd6849 2022-02-13 stsp return got_error_from_errno("calloc");
565 67fd6849 2022-02-13 stsp }
566 67fd6849 2022-02-13 stsp
567 67fd6849 2022-02-13 stsp j = 0;
568 67fd6849 2022-02-13 stsp k = 0;
569 67fd6849 2022-02-13 stsp for (i = 0; i < nobj; i++) {
570 67fd6849 2022-02-13 stsp uint32_t offset = be32toh(p->hdr.offsets[i]);
571 67fd6849 2022-02-13 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
572 67fd6849 2022-02-13 stsp uint64_t loffset;
573 67fd6849 2022-02-13 stsp uint32_t idx;
574 67fd6849 2022-02-13 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
575 67fd6849 2022-02-13 stsp if (idx >= p->nlargeobj ||
576 67fd6849 2022-02-13 stsp p->nlargeobj == 0 ||
577 67fd6849 2022-02-13 stsp p->hdr.large_offsets == NULL)
578 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
579 67fd6849 2022-02-13 stsp loffset = be64toh(p->hdr.large_offsets[idx]);
580 67fd6849 2022-02-13 stsp p->sorted_large_offsets[j].offset = loffset;
581 67fd6849 2022-02-13 stsp p->sorted_large_offsets[j].idx = i;
582 67fd6849 2022-02-13 stsp j++;
583 67fd6849 2022-02-13 stsp } else {
584 67fd6849 2022-02-13 stsp p->sorted_offsets[k].offset = offset;
585 67fd6849 2022-02-13 stsp p->sorted_offsets[k].idx = i;
586 67fd6849 2022-02-13 stsp k++;
587 67fd6849 2022-02-13 stsp }
588 67fd6849 2022-02-13 stsp }
589 67fd6849 2022-02-13 stsp if (j != p->nlargeobj || k != nobj - p->nlargeobj)
590 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
591 67fd6849 2022-02-13 stsp
592 67fd6849 2022-02-13 stsp qsort(p->sorted_offsets, nobj - p->nlargeobj,
593 67fd6849 2022-02-13 stsp sizeof(p->sorted_offsets[0]), offset_cmp);
594 67fd6849 2022-02-13 stsp
595 67fd6849 2022-02-13 stsp if (p->sorted_large_offsets)
596 67fd6849 2022-02-13 stsp qsort(p->sorted_large_offsets, p->nlargeobj,
597 67fd6849 2022-02-13 stsp sizeof(p->sorted_large_offsets[0]), large_offset_cmp);
598 67fd6849 2022-02-13 stsp
599 67fd6849 2022-02-13 stsp return NULL;
600 67fd6849 2022-02-13 stsp }
601 67fd6849 2022-02-13 stsp
602 876c234b 2018-09-10 stsp const struct got_error *
603 67fd6849 2022-02-13 stsp got_packidx_get_offset_idx(int *idx, struct got_packidx *packidx, off_t offset)
604 67fd6849 2022-02-13 stsp {
605 67fd6849 2022-02-13 stsp const struct got_error *err;
606 67fd6849 2022-02-13 stsp uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
607 67fd6849 2022-02-13 stsp int i, left, right;
608 67fd6849 2022-02-13 stsp
609 67fd6849 2022-02-13 stsp *idx = -1;
610 67fd6849 2022-02-13 stsp
611 67fd6849 2022-02-13 stsp if (packidx->sorted_offsets == NULL) {
612 67fd6849 2022-02-13 stsp err = build_offset_index(packidx);
613 67fd6849 2022-02-13 stsp if (err)
614 67fd6849 2022-02-13 stsp return err;
615 67fd6849 2022-02-13 stsp }
616 67fd6849 2022-02-13 stsp
617 67fd6849 2022-02-13 stsp if (offset >= 0x7fffffff) {
618 67fd6849 2022-02-13 stsp uint64_t lo;
619 67fd6849 2022-02-13 stsp left = 0, right = packidx->nlargeobj - 1;
620 67fd6849 2022-02-13 stsp while (left <= right) {
621 67fd6849 2022-02-13 stsp i = ((left + right) / 2);
622 67fd6849 2022-02-13 stsp lo = packidx->sorted_large_offsets[i].offset;
623 67fd6849 2022-02-13 stsp if (lo == offset) {
624 67fd6849 2022-02-13 stsp *idx = packidx->sorted_large_offsets[i].idx;
625 67fd6849 2022-02-13 stsp break;
626 67fd6849 2022-02-13 stsp } else if (offset > lo)
627 67fd6849 2022-02-13 stsp left = i + 1;
628 67fd6849 2022-02-13 stsp else if (offset < lo)
629 67fd6849 2022-02-13 stsp right = i - 1;
630 67fd6849 2022-02-13 stsp }
631 67fd6849 2022-02-13 stsp } else {
632 67fd6849 2022-02-13 stsp uint32_t o;
633 67fd6849 2022-02-13 stsp left = 0, right = totobj - packidx->nlargeobj - 1;
634 67fd6849 2022-02-13 stsp while (left <= right) {
635 67fd6849 2022-02-13 stsp i = ((left + right) / 2);
636 67fd6849 2022-02-13 stsp o = packidx->sorted_offsets[i].offset;
637 67fd6849 2022-02-13 stsp if (o == offset) {
638 67fd6849 2022-02-13 stsp *idx = packidx->sorted_offsets[i].idx;
639 67fd6849 2022-02-13 stsp break;
640 67fd6849 2022-02-13 stsp } else if (offset > o)
641 67fd6849 2022-02-13 stsp left = i + 1;
642 67fd6849 2022-02-13 stsp else if (offset < o)
643 67fd6849 2022-02-13 stsp right = i - 1;
644 67fd6849 2022-02-13 stsp }
645 67fd6849 2022-02-13 stsp }
646 67fd6849 2022-02-13 stsp
647 67fd6849 2022-02-13 stsp return NULL;
648 67fd6849 2022-02-13 stsp }
649 67fd6849 2022-02-13 stsp
650 67fd6849 2022-02-13 stsp const struct got_error *
651 67fd6849 2022-02-13 stsp got_packidx_get_object_id(struct got_object_id *id,
652 67fd6849 2022-02-13 stsp struct got_packidx *packidx, int idx)
653 67fd6849 2022-02-13 stsp {
654 67fd6849 2022-02-13 stsp uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
655 67fd6849 2022-02-13 stsp struct got_packidx_object_id *oid;
656 67fd6849 2022-02-13 stsp
657 67fd6849 2022-02-13 stsp if (idx < 0 || idx >= totobj)
658 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_NO_OBJ);
659 67fd6849 2022-02-13 stsp
660 67fd6849 2022-02-13 stsp oid = &packidx->hdr.sorted_ids[idx];
661 3093e2d7 2023-02-04 op memcpy(id->hash, oid->hash, SHA1_DIGEST_LENGTH);
662 67fd6849 2022-02-13 stsp return NULL;
663 67fd6849 2022-02-13 stsp }
664 67fd6849 2022-02-13 stsp
665 67fd6849 2022-02-13 stsp const struct got_error *
666 dd88155e 2019-06-29 stsp got_packidx_match_id_str_prefix(struct got_object_id_queue *matched_ids,
667 4277420a 2019-06-29 stsp struct got_packidx *packidx, const char *id_str_prefix)
668 e09a504c 2019-06-28 stsp {
669 dd88155e 2019-06-29 stsp const struct got_error *err = NULL;
670 4277420a 2019-06-29 stsp u_int8_t id0;
671 78fb0967 2020-09-09 naddy uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
672 4277420a 2019-06-29 stsp char hex[3];
673 4277420a 2019-06-29 stsp size_t prefix_len = strlen(id_str_prefix);
674 e09a504c 2019-06-28 stsp struct got_packidx_object_id *oid;
675 404bde06 2022-01-03 stsp uint32_t i = 0;
676 e09a504c 2019-06-28 stsp
677 dbdddfee 2021-06-23 naddy STAILQ_INIT(matched_ids);
678 4277420a 2019-06-29 stsp
679 4277420a 2019-06-29 stsp if (prefix_len < 2)
680 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
681 4277420a 2019-06-29 stsp
682 4277420a 2019-06-29 stsp hex[0] = id_str_prefix[0];
683 4277420a 2019-06-29 stsp hex[1] = id_str_prefix[1];
684 4277420a 2019-06-29 stsp hex[2] = '\0';
685 4277420a 2019-06-29 stsp if (!got_parse_xdigit(&id0, hex))
686 6dd1ece6 2019-11-10 stsp return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
687 4277420a 2019-06-29 stsp
688 404bde06 2022-01-03 stsp if (id0 > 0)
689 404bde06 2022-01-03 stsp i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
690 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[i];
691 3093e2d7 2023-02-04 op while (i < totobj && oid->hash[0] == id0) {
692 4277420a 2019-06-29 stsp char id_str[SHA1_DIGEST_STRING_LENGTH];
693 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
694 4277420a 2019-06-29 stsp int cmp;
695 4277420a 2019-06-29 stsp
696 3093e2d7 2023-02-04 op if (!got_sha1_digest_to_str(oid->hash, id_str, sizeof(id_str)))
697 4277420a 2019-06-29 stsp return got_error(GOT_ERR_NO_SPACE);
698 4277420a 2019-06-29 stsp
699 4277420a 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, prefix_len);
700 4277420a 2019-06-29 stsp if (cmp < 0) {
701 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
702 4277420a 2019-06-29 stsp continue;
703 4277420a 2019-06-29 stsp } else if (cmp > 0)
704 e09a504c 2019-06-28 stsp break;
705 4277420a 2019-06-29 stsp
706 dd88155e 2019-06-29 stsp err = got_object_qid_alloc_partial(&qid);
707 dd88155e 2019-06-29 stsp if (err)
708 dd88155e 2019-06-29 stsp break;
709 3093e2d7 2023-02-04 op memcpy(qid->id.hash, oid->hash, SHA1_DIGEST_LENGTH);
710 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(matched_ids, qid, entry);
711 4277420a 2019-06-29 stsp
712 4277420a 2019-06-29 stsp oid = &packidx->hdr.sorted_ids[++i];
713 e09a504c 2019-06-28 stsp }
714 e09a504c 2019-06-28 stsp
715 0adc7bcc 2019-06-29 stsp if (err)
716 0adc7bcc 2019-06-29 stsp got_object_id_queue_free(matched_ids);
717 3d589bee 2022-06-25 stsp return err;
718 3d589bee 2022-06-25 stsp }
719 3d589bee 2022-06-25 stsp
720 3d589bee 2022-06-25 stsp static void
721 3d589bee 2022-06-25 stsp set_max_datasize(void)
722 3d589bee 2022-06-25 stsp {
723 3d589bee 2022-06-25 stsp struct rlimit rl;
724 3d589bee 2022-06-25 stsp
725 3d589bee 2022-06-25 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
726 3d589bee 2022-06-25 stsp return;
727 3d589bee 2022-06-25 stsp
728 3d589bee 2022-06-25 stsp rl.rlim_cur = rl.rlim_max;
729 3d589bee 2022-06-25 stsp setrlimit(RLIMIT_DATA, &rl);
730 3d589bee 2022-06-25 stsp }
731 3d589bee 2022-06-25 stsp
732 3d589bee 2022-06-25 stsp const struct got_error *
733 3d589bee 2022-06-25 stsp got_pack_start_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
734 3d589bee 2022-06-25 stsp {
735 3d589bee 2022-06-25 stsp const struct got_error *err = NULL;
736 3d589bee 2022-06-25 stsp int imsg_fds[2];
737 3d589bee 2022-06-25 stsp pid_t pid;
738 3d589bee 2022-06-25 stsp struct imsgbuf *ibuf;
739 3d589bee 2022-06-25 stsp
740 3d589bee 2022-06-25 stsp ibuf = calloc(1, sizeof(*ibuf));
741 3d589bee 2022-06-25 stsp if (ibuf == NULL)
742 3d589bee 2022-06-25 stsp return got_error_from_errno("calloc");
743 3d589bee 2022-06-25 stsp
744 3d589bee 2022-06-25 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
745 3d589bee 2022-06-25 stsp if (pack->privsep_child == NULL) {
746 3d589bee 2022-06-25 stsp err = got_error_from_errno("calloc");
747 3d589bee 2022-06-25 stsp free(ibuf);
748 3d589bee 2022-06-25 stsp return err;
749 3d589bee 2022-06-25 stsp }
750 3d589bee 2022-06-25 stsp pack->child_has_tempfiles = 0;
751 3d589bee 2022-06-25 stsp pack->child_has_delta_outfd = 0;
752 3d589bee 2022-06-25 stsp
753 3d589bee 2022-06-25 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
754 3d589bee 2022-06-25 stsp err = got_error_from_errno("socketpair");
755 3d589bee 2022-06-25 stsp goto done;
756 3d589bee 2022-06-25 stsp }
757 3d589bee 2022-06-25 stsp
758 3d589bee 2022-06-25 stsp pid = fork();
759 3d589bee 2022-06-25 stsp if (pid == -1) {
760 3d589bee 2022-06-25 stsp err = got_error_from_errno("fork");
761 3d589bee 2022-06-25 stsp goto done;
762 3d589bee 2022-06-25 stsp } else if (pid == 0) {
763 3d589bee 2022-06-25 stsp set_max_datasize();
764 3d589bee 2022-06-25 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
765 3d589bee 2022-06-25 stsp pack->path_packfile);
766 3d589bee 2022-06-25 stsp /* not reached */
767 3d589bee 2022-06-25 stsp }
768 3d589bee 2022-06-25 stsp
769 3d589bee 2022-06-25 stsp if (close(imsg_fds[1]) == -1)
770 3d589bee 2022-06-25 stsp return got_error_from_errno("close");
771 3d589bee 2022-06-25 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
772 3d589bee 2022-06-25 stsp pack->privsep_child->pid = pid;
773 3d589bee 2022-06-25 stsp imsg_init(ibuf, imsg_fds[0]);
774 3d589bee 2022-06-25 stsp pack->privsep_child->ibuf = ibuf;
775 3d589bee 2022-06-25 stsp
776 3d589bee 2022-06-25 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
777 3d589bee 2022-06-25 stsp if (err) {
778 3d589bee 2022-06-25 stsp const struct got_error *child_err;
779 3d589bee 2022-06-25 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
780 3d589bee 2022-06-25 stsp child_err = got_privsep_wait_for_child(
781 3d589bee 2022-06-25 stsp pack->privsep_child->pid);
782 3d589bee 2022-06-25 stsp if (child_err && err == NULL)
783 3d589bee 2022-06-25 stsp err = child_err;
784 3d589bee 2022-06-25 stsp }
785 3d589bee 2022-06-25 stsp done:
786 3d589bee 2022-06-25 stsp if (err) {
787 3d589bee 2022-06-25 stsp free(ibuf);
788 3d589bee 2022-06-25 stsp free(pack->privsep_child);
789 3d589bee 2022-06-25 stsp pack->privsep_child = NULL;
790 3d589bee 2022-06-25 stsp }
791 dd88155e 2019-06-29 stsp return err;
792 e09a504c 2019-06-28 stsp }
793 e09a504c 2019-06-28 stsp
794 b4f37570 2021-06-19 stsp static const struct got_error *
795 b4f37570 2021-06-19 stsp pack_stop_privsep_child(struct got_pack *pack)
796 876c234b 2018-09-10 stsp {
797 74ef8aae 2022-10-24 stsp const struct got_error *err = NULL, *close_err = NULL;
798 876c234b 2018-09-10 stsp
799 876c234b 2018-09-10 stsp if (pack->privsep_child == NULL)
800 876c234b 2018-09-10 stsp return NULL;
801 876c234b 2018-09-10 stsp
802 876c234b 2018-09-10 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
803 96732e0b 2018-11-11 stsp if (err)
804 96732e0b 2018-11-11 stsp return err;
805 74ef8aae 2022-10-24 stsp if (close(pack->privsep_child->imsg_fd) == -1)
806 74ef8aae 2022-10-24 stsp close_err = got_error_from_errno("close");
807 96732e0b 2018-11-11 stsp err = got_privsep_wait_for_child(pack->privsep_child->pid);
808 74ef8aae 2022-10-24 stsp if (close_err && err == NULL)
809 74ef8aae 2022-10-24 stsp err = close_err;
810 74ef8aae 2022-10-24 stsp imsg_clear(pack->privsep_child->ibuf);
811 cc2a8ef4 2021-06-19 tracey free(pack->privsep_child->ibuf);
812 876c234b 2018-09-10 stsp free(pack->privsep_child);
813 876c234b 2018-09-10 stsp pack->privsep_child = NULL;
814 876c234b 2018-09-10 stsp return err;
815 7e656b93 2018-03-17 stsp }
816 7e656b93 2018-03-17 stsp
817 d7464085 2018-07-09 stsp const struct got_error *
818 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
819 7e656b93 2018-03-17 stsp {
820 d7464085 2018-07-09 stsp const struct got_error *err = NULL;
821 d7464085 2018-07-09 stsp
822 b4f37570 2021-06-19 stsp err = pack_stop_privsep_child(pack);
823 876c234b 2018-09-10 stsp if (pack->map && munmap(pack->map, pack->filesize) == -1 && !err)
824 638f9024 2019-05-13 stsp err = got_error_from_errno("munmap");
825 08578a35 2021-01-22 stsp if (pack->fd != -1 && close(pack->fd) == -1 && err == NULL)
826 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
827 8b2180d4 2018-04-26 stsp pack->fd = -1;
828 4589e373 2018-03-17 stsp free(pack->path_packfile);
829 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
830 4589e373 2018-03-17 stsp pack->filesize = 0;
831 ab2f42e7 2019-11-10 stsp if (pack->delta_cache) {
832 ab2f42e7 2019-11-10 stsp got_delta_cache_free(pack->delta_cache);
833 ab2f42e7 2019-11-10 stsp pack->delta_cache = NULL;
834 ab2f42e7 2019-11-10 stsp }
835 7e656b93 2018-03-17 stsp
836 57160834 2022-05-31 stsp /*
837 57160834 2022-05-31 stsp * Leave accumfd and basefd alone. They are managed by the
838 57160834 2022-05-31 stsp * repository layer and can be reused.
839 57160834 2022-05-31 stsp */
840 57160834 2022-05-31 stsp
841 c7fe698a 2018-01-23 stsp return err;
842 c7fe698a 2018-01-23 stsp }
843 c7fe698a 2018-01-23 stsp
844 668a20f6 2020-03-18 stsp const struct got_error *
845 668a20f6 2020-03-18 stsp got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
846 d7464085 2018-07-09 stsp struct got_pack *pack, off_t offset)
847 a487c1d0 2018-01-14 stsp {
848 a487c1d0 2018-01-14 stsp uint8_t t = 0;
849 a487c1d0 2018-01-14 stsp uint64_t s = 0;
850 a487c1d0 2018-01-14 stsp uint8_t sizeN;
851 d7464085 2018-07-09 stsp size_t mapoff = 0;
852 a487c1d0 2018-01-14 stsp int i = 0;
853 d7464085 2018-07-09 stsp
854 d7464085 2018-07-09 stsp *len = 0;
855 d7464085 2018-07-09 stsp
856 d7464085 2018-07-09 stsp if (offset >= pack->filesize)
857 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
858 d7464085 2018-07-09 stsp
859 d7464085 2018-07-09 stsp if (pack->map) {
860 ad4cc361 2022-10-27 op if (offset > SIZE_MAX) {
861 ad4cc361 2022-10-27 op return got_error_fmt(GOT_ERR_PACK_OFFSET,
862 ad4cc361 2022-10-27 op "offset %lld overflows size_t",
863 ad4cc361 2022-10-27 op (long long)offset);
864 ad4cc361 2022-10-27 op }
865 ad4cc361 2022-10-27 op
866 d7464085 2018-07-09 stsp mapoff = (size_t)offset;
867 d7464085 2018-07-09 stsp } else {
868 d7464085 2018-07-09 stsp if (lseek(pack->fd, offset, SEEK_SET) == -1)
869 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
870 d7464085 2018-07-09 stsp }
871 a487c1d0 2018-01-14 stsp
872 a1fd68d8 2018-01-12 stsp do {
873 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
874 a487c1d0 2018-01-14 stsp if (i > 9)
875 09ee8ded 2022-10-20 stsp return got_error_fmt(GOT_ERR_OBJ_TOO_LARGE,
876 e082ed67 2022-10-24 naddy "packfile offset %lld", (long long)offset);
877 a1fd68d8 2018-01-12 stsp
878 d7464085 2018-07-09 stsp if (pack->map) {
879 3976db15 2022-01-10 stsp if (mapoff + sizeof(sizeN) >= pack->filesize)
880 3976db15 2022-01-10 stsp return got_error(GOT_ERR_BAD_PACKFILE);
881 d7464085 2018-07-09 stsp sizeN = *(pack->map + mapoff);
882 d7464085 2018-07-09 stsp mapoff += sizeof(sizeN);
883 d7464085 2018-07-09 stsp } else {
884 d7464085 2018-07-09 stsp ssize_t n = read(pack->fd, &sizeN, sizeof(sizeN));
885 d7464085 2018-07-09 stsp if (n < 0)
886 638f9024 2019-05-13 stsp return got_error_from_errno("read");
887 d7464085 2018-07-09 stsp if (n != sizeof(sizeN))
888 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
889 d7464085 2018-07-09 stsp }
890 d7464085 2018-07-09 stsp *len += sizeof(sizeN);
891 8251fdbc 2018-01-12 stsp
892 a1fd68d8 2018-01-12 stsp if (i == 0) {
893 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
894 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
895 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
896 a1fd68d8 2018-01-12 stsp } else {
897 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
898 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
899 a1fd68d8 2018-01-12 stsp }
900 a1fd68d8 2018-01-12 stsp i++;
901 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
902 a1fd68d8 2018-01-12 stsp
903 a487c1d0 2018-01-14 stsp *type = t;
904 a487c1d0 2018-01-14 stsp *size = s;
905 a487c1d0 2018-01-14 stsp return NULL;
906 0a0a3048 2018-01-10 stsp }
907 c54542a0 2018-01-13 stsp
908 a1fd68d8 2018-01-12 stsp static const struct got_error *
909 5f25cc85 2019-11-26 stsp open_plain_object(struct got_object **obj, struct got_object_id *id,
910 5f25cc85 2019-11-26 stsp uint8_t type, off_t offset, size_t size, int idx)
911 6ccb713b 2018-01-19 stsp {
912 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
913 6ccb713b 2018-01-19 stsp if (*obj == NULL)
914 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
915 6ccb713b 2018-01-19 stsp
916 6ccb713b 2018-01-19 stsp (*obj)->type = type;
917 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
918 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
919 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
920 6ccb713b 2018-01-19 stsp (*obj)->size = size;
921 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
922 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
923 b107e67f 2018-01-19 stsp
924 b107e67f 2018-01-19 stsp return NULL;
925 b107e67f 2018-01-19 stsp }
926 b107e67f 2018-01-19 stsp
927 b107e67f 2018-01-19 stsp static const struct got_error *
928 d7464085 2018-07-09 stsp parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
929 d7464085 2018-07-09 stsp off_t delta_offset)
930 b107e67f 2018-01-19 stsp {
931 b107e67f 2018-01-19 stsp int64_t o = 0;
932 b107e67f 2018-01-19 stsp uint8_t offN;
933 b107e67f 2018-01-19 stsp int i = 0;
934 b107e67f 2018-01-19 stsp
935 a0de39f3 2019-08-09 stsp *offset = 0;
936 d7464085 2018-07-09 stsp *len = 0;
937 d7464085 2018-07-09 stsp
938 b107e67f 2018-01-19 stsp do {
939 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
940 b107e67f 2018-01-19 stsp if (i > 8)
941 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
942 b107e67f 2018-01-19 stsp
943 d7464085 2018-07-09 stsp if (pack->map) {
944 d7464085 2018-07-09 stsp size_t mapoff;
945 ad4cc361 2022-10-27 op
946 ad4cc361 2022-10-27 op if (delta_offset + *len > SIZE_MAX) {
947 ad4cc361 2022-10-27 op return got_error_fmt(GOT_ERR_PACK_OFFSET,
948 ad4cc361 2022-10-27 op "mapoff %lld would overflow size_t",
949 ad4cc361 2022-10-27 op (long long)delta_offset + *len);
950 ad4cc361 2022-10-27 op }
951 ad4cc361 2022-10-27 op
952 d7464085 2018-07-09 stsp mapoff = (size_t)delta_offset + *len;
953 3976db15 2022-01-10 stsp if (mapoff + sizeof(offN) >= pack->filesize)
954 3976db15 2022-01-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
955 d7464085 2018-07-09 stsp offN = *(pack->map + mapoff);
956 d7464085 2018-07-09 stsp } else {
957 d7464085 2018-07-09 stsp ssize_t n;
958 d7464085 2018-07-09 stsp n = read(pack->fd, &offN, sizeof(offN));
959 d7464085 2018-07-09 stsp if (n < 0)
960 638f9024 2019-05-13 stsp return got_error_from_errno("read");
961 d7464085 2018-07-09 stsp if (n != sizeof(offN))
962 d7464085 2018-07-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
963 d7464085 2018-07-09 stsp }
964 d7464085 2018-07-09 stsp *len += sizeof(offN);
965 b107e67f 2018-01-19 stsp
966 b107e67f 2018-01-19 stsp if (i == 0)
967 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
968 b107e67f 2018-01-19 stsp else {
969 cecc778e 2018-01-23 stsp o++;
970 b107e67f 2018-01-19 stsp o <<= 7;
971 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
972 b107e67f 2018-01-19 stsp }
973 b107e67f 2018-01-19 stsp i++;
974 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
975 6ccb713b 2018-01-19 stsp
976 b107e67f 2018-01-19 stsp *offset = o;
977 6ccb713b 2018-01-19 stsp return NULL;
978 6ccb713b 2018-01-19 stsp }
979 6ccb713b 2018-01-19 stsp
980 668a20f6 2020-03-18 stsp const struct got_error *
981 c0df5966 2021-12-31 stsp got_pack_parse_offset_delta(off_t *base_offset, size_t *len,
982 24d916d2 2022-10-24 op struct got_pack *pack, off_t offset, size_t tslen)
983 b107e67f 2018-01-19 stsp {
984 96f5e8b3 2018-01-23 stsp const struct got_error *err;
985 b107e67f 2018-01-19 stsp int64_t negoffset;
986 b107e67f 2018-01-19 stsp size_t negofflen;
987 b107e67f 2018-01-19 stsp
988 d7464085 2018-07-09 stsp *len = 0;
989 d7464085 2018-07-09 stsp
990 d7464085 2018-07-09 stsp err = parse_negative_offset(&negoffset, &negofflen, pack,
991 d7464085 2018-07-09 stsp offset + tslen);
992 b107e67f 2018-01-19 stsp if (err)
993 b107e67f 2018-01-19 stsp return err;
994 b107e67f 2018-01-19 stsp
995 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
996 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
997 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
998 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
999 b107e67f 2018-01-19 stsp
1000 d7464085 2018-07-09 stsp *len = negofflen;
1001 96f5e8b3 2018-01-23 stsp return NULL;
1002 96f5e8b3 2018-01-23 stsp }
1003 96f5e8b3 2018-01-23 stsp
1004 0e22967e 2018-02-11 stsp static const struct got_error *
1005 42c69117 2019-11-10 stsp read_delta_data(uint8_t **delta_buf, size_t *delta_len,
1006 2d9e6abf 2022-05-04 stsp size_t *delta_compressed_len, size_t delta_data_offset,
1007 2d9e6abf 2022-05-04 stsp struct got_pack *pack)
1008 42c69117 2019-11-10 stsp {
1009 42c69117 2019-11-10 stsp const struct got_error *err = NULL;
1010 2d9e6abf 2022-05-04 stsp size_t consumed = 0;
1011 42c69117 2019-11-10 stsp
1012 42c69117 2019-11-10 stsp if (pack->map) {
1013 42c69117 2019-11-10 stsp if (delta_data_offset >= pack->filesize)
1014 42c69117 2019-11-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
1015 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(delta_buf, delta_len,
1016 2d9e6abf 2022-05-04 stsp &consumed, NULL, pack->map, delta_data_offset,
1017 2e5a6fad 2020-03-18 stsp pack->filesize - delta_data_offset);
1018 2d9e6abf 2022-05-04 stsp if (err)
1019 2d9e6abf 2022-05-04 stsp return err;
1020 42c69117 2019-11-10 stsp } else {
1021 42c69117 2019-11-10 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1)
1022 42c69117 2019-11-10 stsp return got_error_from_errno("lseek");
1023 2d9e6abf 2022-05-04 stsp err = got_inflate_to_mem_fd(delta_buf, delta_len,
1024 2d9e6abf 2022-05-04 stsp &consumed, NULL, 0, pack->fd);
1025 2d9e6abf 2022-05-04 stsp if (err)
1026 2d9e6abf 2022-05-04 stsp return err;
1027 42c69117 2019-11-10 stsp }
1028 2d9e6abf 2022-05-04 stsp
1029 2d9e6abf 2022-05-04 stsp if (delta_compressed_len)
1030 2d9e6abf 2022-05-04 stsp *delta_compressed_len = consumed;
1031 2d9e6abf 2022-05-04 stsp
1032 2d9e6abf 2022-05-04 stsp return NULL;
1033 42c69117 2019-11-10 stsp }
1034 a3500804 2018-01-23 stsp
1035 96f5e8b3 2018-01-23 stsp static const struct got_error *
1036 c336f889 2018-07-23 stsp add_delta(struct got_delta_chain *deltas, off_t delta_offset, size_t tslen,
1037 e5792992 2022-10-28 op int delta_type, size_t delta_size, off_t delta_data_offset)
1038 bdd2fbb3 2018-02-11 stsp {
1039 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
1040 bdd2fbb3 2018-02-11 stsp
1041 c336f889 2018-07-23 stsp delta = got_delta_open(delta_offset, tslen, delta_type, delta_size,
1042 42c69117 2019-11-10 stsp delta_data_offset);
1043 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
1044 638f9024 2019-05-13 stsp return got_error_from_errno("got_delta_open");
1045 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
1046 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
1047 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&deltas->entries, delta, entry);
1048 bdd2fbb3 2018-02-11 stsp return NULL;
1049 bdd2fbb3 2018-02-11 stsp }
1050 bdd2fbb3 2018-02-11 stsp
1051 bdd2fbb3 2018-02-11 stsp static const struct got_error *
1052 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
1053 c8ecd499 2018-09-09 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1054 c8ecd499 2018-09-09 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1055 a3500804 2018-01-23 stsp {
1056 a3500804 2018-01-23 stsp const struct got_error *err;
1057 c3703302 2018-01-23 stsp off_t base_offset;
1058 c3703302 2018-01-23 stsp uint8_t base_type;
1059 c3703302 2018-01-23 stsp uint64_t base_size;
1060 c3703302 2018-01-23 stsp size_t base_tslen;
1061 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
1062 42c69117 2019-11-10 stsp size_t consumed;
1063 a3500804 2018-01-23 stsp
1064 668a20f6 2020-03-18 stsp err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
1065 d7464085 2018-07-09 stsp delta_offset, tslen);
1066 a3500804 2018-01-23 stsp if (err)
1067 a3500804 2018-01-23 stsp return err;
1068 a3500804 2018-01-23 stsp
1069 d7464085 2018-07-09 stsp delta_data_offset = delta_offset + tslen + consumed;
1070 d7464085 2018-07-09 stsp if (delta_data_offset >= pack->filesize)
1071 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1072 a53d2f13 2018-03-17 stsp
1073 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1074 d7464085 2018-07-09 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1075 d7464085 2018-07-09 stsp if (delta_data_offset == -1)
1076 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1077 d7464085 2018-07-09 stsp }
1078 bdd2fbb3 2018-02-11 stsp
1079 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1080 e5792992 2022-10-28 op delta_data_offset);
1081 bdd2fbb3 2018-02-11 stsp if (err)
1082 1a35c1bc 2019-05-22 stsp return err;
1083 bdd2fbb3 2018-02-11 stsp
1084 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
1085 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
1086 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
1087 b107e67f 2018-01-19 stsp
1088 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1089 668a20f6 2020-03-18 stsp &base_tslen, pack, base_offset);
1090 b107e67f 2018-01-19 stsp if (err)
1091 1a35c1bc 2019-05-22 stsp return err;
1092 b107e67f 2018-01-19 stsp
1093 668a20f6 2020-03-18 stsp return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1094 b0e2201a 2018-06-22 stsp base_tslen, base_type, base_size, recursion - 1);
1095 c3703302 2018-01-23 stsp }
1096 c4330eff 2021-06-22 stsp
1097 c4330eff 2021-06-22 stsp const struct got_error *
1098 c4330eff 2021-06-22 stsp got_pack_parse_ref_delta(struct got_object_id *id,
1099 c4330eff 2021-06-22 stsp struct got_pack *pack, off_t delta_offset, int tslen)
1100 c4330eff 2021-06-22 stsp {
1101 c4330eff 2021-06-22 stsp if (pack->map) {
1102 ad4cc361 2022-10-27 op size_t mapoff;
1103 ad4cc361 2022-10-27 op
1104 ad4cc361 2022-10-27 op if (delta_offset + tslen > SIZE_MAX) {
1105 ad4cc361 2022-10-27 op return got_error_fmt(GOT_ERR_PACK_OFFSET,
1106 ad4cc361 2022-10-27 op "mapoff %lld would overflow size_t",
1107 ad4cc361 2022-10-27 op (long long)delta_offset + tslen);
1108 ad4cc361 2022-10-27 op }
1109 ad4cc361 2022-10-27 op
1110 ad4cc361 2022-10-27 op mapoff = delta_offset + tslen;
1111 3976db15 2022-01-10 stsp if (mapoff + sizeof(*id) >= pack->filesize)
1112 3976db15 2022-01-10 stsp return got_error(GOT_ERR_PACK_OFFSET);
1113 c4330eff 2021-06-22 stsp memcpy(id, pack->map + mapoff, sizeof(*id));
1114 c4330eff 2021-06-22 stsp } else {
1115 c4330eff 2021-06-22 stsp ssize_t n;
1116 c4330eff 2021-06-22 stsp n = read(pack->fd, id, sizeof(*id));
1117 c4330eff 2021-06-22 stsp if (n < 0)
1118 c4330eff 2021-06-22 stsp return got_error_from_errno("read");
1119 c4330eff 2021-06-22 stsp if (n != sizeof(*id))
1120 c4330eff 2021-06-22 stsp return got_error(GOT_ERR_BAD_PACKFILE);
1121 c4330eff 2021-06-22 stsp }
1122 c3703302 2018-01-23 stsp
1123 c4330eff 2021-06-22 stsp return NULL;
1124 c4330eff 2021-06-22 stsp }
1125 c4330eff 2021-06-22 stsp
1126 c3703302 2018-01-23 stsp static const struct got_error *
1127 c8ecd499 2018-09-09 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
1128 c8ecd499 2018-09-09 stsp struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
1129 c8ecd499 2018-09-09 stsp size_t delta_size, unsigned int recursion)
1130 c3703302 2018-01-23 stsp {
1131 6b9c9673 2018-01-23 stsp const struct got_error *err;
1132 6b9c9673 2018-01-23 stsp struct got_object_id id;
1133 6b9c9673 2018-01-23 stsp int idx;
1134 6b9c9673 2018-01-23 stsp off_t base_offset;
1135 6b9c9673 2018-01-23 stsp uint8_t base_type;
1136 6b9c9673 2018-01-23 stsp uint64_t base_size;
1137 6b9c9673 2018-01-23 stsp size_t base_tslen;
1138 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
1139 6b9c9673 2018-01-23 stsp
1140 42c69117 2019-11-10 stsp if (delta_offset + tslen >= pack->filesize)
1141 d7464085 2018-07-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1142 a53d2f13 2018-03-17 stsp
1143 c4330eff 2021-06-22 stsp err = got_pack_parse_ref_delta(&id, pack, delta_offset, tslen);
1144 c4330eff 2021-06-22 stsp if (err)
1145 c4330eff 2021-06-22 stsp return err;
1146 d7464085 2018-07-09 stsp if (pack->map) {
1147 c4330eff 2021-06-22 stsp delta_data_offset = delta_offset + tslen + sizeof(id);
1148 d7464085 2018-07-09 stsp } else {
1149 e40b19ed 2020-01-06 stsp delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
1150 e40b19ed 2020-01-06 stsp if (delta_data_offset == -1)
1151 e40b19ed 2020-01-06 stsp return got_error_from_errno("lseek");
1152 d7464085 2018-07-09 stsp }
1153 d7464085 2018-07-09 stsp
1154 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
1155 e5792992 2022-10-28 op delta_data_offset);
1156 bdd2fbb3 2018-02-11 stsp if (err)
1157 1a35c1bc 2019-05-22 stsp return err;
1158 bdd2fbb3 2018-02-11 stsp
1159 652b2703 2018-06-22 stsp /* Delta base must be in the same pack file. */
1160 1510f469 2018-09-09 stsp idx = got_packidx_get_object_idx(packidx, &id);
1161 1a35c1bc 2019-05-22 stsp if (idx == -1)
1162 668a20f6 2020-03-18 stsp return got_error(GOT_ERR_NO_OBJ);
1163 6b9c9673 2018-01-23 stsp
1164 02828bfd 2021-06-22 stsp base_offset = got_packidx_get_object_offset(packidx, idx);
1165 04aed155 2022-07-24 naddy if (base_offset == -1)
1166 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1167 6b9c9673 2018-01-23 stsp
1168 1a35c1bc 2019-05-22 stsp if (base_offset >= pack->filesize)
1169 1a35c1bc 2019-05-22 stsp return got_error(GOT_ERR_PACK_OFFSET);
1170 6b9c9673 2018-01-23 stsp
1171 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&base_type, &base_size,
1172 668a20f6 2020-03-18 stsp &base_tslen, pack, base_offset);
1173 6b9c9673 2018-01-23 stsp if (err)
1174 1a35c1bc 2019-05-22 stsp return err;
1175 6b9c9673 2018-01-23 stsp
1176 668a20f6 2020-03-18 stsp return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
1177 0c048b15 2018-04-27 stsp base_tslen, base_type, base_size, recursion - 1);
1178 6b9c9673 2018-01-23 stsp }
1179 6b9c9673 2018-01-23 stsp
1180 668a20f6 2020-03-18 stsp const struct got_error *
1181 668a20f6 2020-03-18 stsp got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
1182 668a20f6 2020-03-18 stsp struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
1183 668a20f6 2020-03-18 stsp size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
1184 6b9c9673 2018-01-23 stsp {
1185 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
1186 c3703302 2018-01-23 stsp
1187 5b7e13a7 2018-04-02 stsp if (--recursion == 0)
1188 5b7e13a7 2018-04-02 stsp return got_error(GOT_ERR_RECURSION);
1189 5b7e13a7 2018-04-02 stsp
1190 c3703302 2018-01-23 stsp switch (delta_type) {
1191 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
1192 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
1193 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
1194 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
1195 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
1196 c336f889 2018-07-23 stsp err = add_delta(deltas, delta_offset, tslen, delta_type,
1197 42c69117 2019-11-10 stsp delta_size, 0);
1198 4e8cda55 2018-01-19 stsp break;
1199 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1200 c8ecd499 2018-09-09 stsp err = resolve_offset_delta(deltas, packidx, pack,
1201 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1202 96f5e8b3 2018-01-23 stsp break;
1203 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
1204 c8ecd499 2018-09-09 stsp err = resolve_ref_delta(deltas, packidx, pack,
1205 b0e2201a 2018-06-22 stsp delta_offset, tslen, delta_type, delta_size, recursion - 1);
1206 6b9c9673 2018-01-23 stsp break;
1207 4e8cda55 2018-01-19 stsp default:
1208 4810de4a 2018-04-01 stsp return got_error(GOT_ERR_OBJ_TYPE);
1209 4e8cda55 2018-01-19 stsp }
1210 96f5e8b3 2018-01-23 stsp
1211 96f5e8b3 2018-01-23 stsp return err;
1212 96f5e8b3 2018-01-23 stsp }
1213 96f5e8b3 2018-01-23 stsp
1214 96f5e8b3 2018-01-23 stsp static const struct got_error *
1215 a98c62b1 2018-09-09 stsp open_delta_object(struct got_object **obj, struct got_packidx *packidx,
1216 a98c62b1 2018-09-09 stsp struct got_pack *pack, struct got_object_id *id, off_t offset,
1217 c59b3346 2018-09-11 stsp size_t tslen, int delta_type, size_t delta_size, int idx)
1218 96f5e8b3 2018-01-23 stsp {
1219 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
1220 96f5e8b3 2018-01-23 stsp int resolved_type;
1221 4e8cda55 2018-01-19 stsp
1222 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
1223 b107e67f 2018-01-19 stsp if (*obj == NULL)
1224 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1225 b107e67f 2018-01-19 stsp
1226 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
1227 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
1228 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
1229 106807b4 2018-09-15 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1230 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
1231 1a35c1bc 2019-05-22 stsp
1232 dbdddfee 2021-06-23 naddy STAILQ_INIT(&(*obj)->deltas.entries);
1233 1a35c1bc 2019-05-22 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
1234 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
1235 c59b3346 2018-09-11 stsp (*obj)->pack_idx = idx;
1236 96f5e8b3 2018-01-23 stsp
1237 668a20f6 2020-03-18 stsp err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
1238 668a20f6 2020-03-18 stsp offset, tslen, delta_type, delta_size,
1239 668a20f6 2020-03-18 stsp GOT_DELTA_CHAIN_RECURSION_MAX);
1240 96f5e8b3 2018-01-23 stsp if (err)
1241 96f5e8b3 2018-01-23 stsp goto done;
1242 96f5e8b3 2018-01-23 stsp
1243 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
1244 96f5e8b3 2018-01-23 stsp if (err)
1245 96f5e8b3 2018-01-23 stsp goto done;
1246 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
1247 96f5e8b3 2018-01-23 stsp done:
1248 96f5e8b3 2018-01-23 stsp if (err) {
1249 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
1250 96f5e8b3 2018-01-23 stsp *obj = NULL;
1251 96f5e8b3 2018-01-23 stsp }
1252 96f5e8b3 2018-01-23 stsp return err;
1253 b107e67f 2018-01-19 stsp }
1254 b107e67f 2018-01-19 stsp
1255 2090a03d 2018-09-09 stsp const struct got_error *
1256 2090a03d 2018-09-09 stsp got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
1257 6fd11751 2018-06-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1258 a1fd68d8 2018-01-12 stsp {
1259 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1260 a1fd68d8 2018-01-12 stsp off_t offset;
1261 6c00b545 2018-01-17 stsp uint8_t type;
1262 6c00b545 2018-01-17 stsp uint64_t size;
1263 3ee5fc21 2018-01-17 stsp size_t tslen;
1264 a1fd68d8 2018-01-12 stsp
1265 6c00b545 2018-01-17 stsp *obj = NULL;
1266 a1fd68d8 2018-01-12 stsp
1267 02828bfd 2021-06-22 stsp offset = got_packidx_get_object_offset(packidx, idx);
1268 04aed155 2022-07-24 naddy if (offset == -1)
1269 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1270 a1fd68d8 2018-01-12 stsp
1271 668a20f6 2020-03-18 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1272 668a20f6 2020-03-18 stsp pack, offset);
1273 a1fd68d8 2018-01-12 stsp if (err)
1274 35c73765 2018-09-09 stsp return err;
1275 a1fd68d8 2018-01-12 stsp
1276 6c00b545 2018-01-17 stsp switch (type) {
1277 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
1278 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
1279 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
1280 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
1281 5f25cc85 2019-11-26 stsp err = open_plain_object(obj, id, type, offset + tslen,
1282 5f25cc85 2019-11-26 stsp size, idx);
1283 6c00b545 2018-01-17 stsp break;
1284 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1285 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1286 a98c62b1 2018-09-09 stsp err = open_delta_object(obj, packidx, pack, id, offset,
1287 c59b3346 2018-09-11 stsp tslen, type, size, idx);
1288 b107e67f 2018-01-19 stsp break;
1289 6c00b545 2018-01-17 stsp default:
1290 4810de4a 2018-04-01 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1291 35c73765 2018-09-09 stsp break;
1292 35c73765 2018-09-09 stsp }
1293 35c73765 2018-09-09 stsp
1294 3ee5fc21 2018-01-17 stsp return err;
1295 3ee5fc21 2018-01-17 stsp }
1296 efd2a263 2018-01-19 stsp
1297 d582f26c 2020-03-18 stsp const struct got_error *
1298 c0df5966 2021-12-31 stsp got_pack_get_delta_chain_max_size(uint64_t *max_size,
1299 c0df5966 2021-12-31 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1300 22484865 2018-03-13 stsp {
1301 22484865 2018-03-13 stsp struct got_delta *delta;
1302 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1303 22484865 2018-03-13 stsp
1304 22484865 2018-03-13 stsp *max_size = 0;
1305 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1306 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1307 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1308 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1309 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1310 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1311 22484865 2018-03-13 stsp const struct got_error *err;
1312 aabb25f8 2022-10-17 stsp uint8_t *delta_buf = NULL;
1313 42c69117 2019-11-10 stsp size_t delta_len;
1314 ab2f42e7 2019-11-10 stsp int cached = 1;
1315 42c69117 2019-11-10 stsp
1316 aabb25f8 2022-10-17 stsp if (pack->delta_cache) {
1317 aabb25f8 2022-10-17 stsp got_delta_cache_get(&delta_buf, &delta_len,
1318 aabb25f8 2022-10-17 stsp pack->delta_cache, delta->data_offset);
1319 aabb25f8 2022-10-17 stsp }
1320 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1321 ab2f42e7 2019-11-10 stsp cached = 0;
1322 ab2f42e7 2019-11-10 stsp err = read_delta_data(&delta_buf, &delta_len,
1323 2d9e6abf 2022-05-04 stsp NULL, delta->data_offset, pack);
1324 ab2f42e7 2019-11-10 stsp if (err)
1325 ab2f42e7 2019-11-10 stsp return err;
1326 aabb25f8 2022-10-17 stsp }
1327 aabb25f8 2022-10-17 stsp if (pack->delta_cache && !cached) {
1328 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1329 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1330 ab2f42e7 2019-11-10 stsp if (err == NULL)
1331 ab2f42e7 2019-11-10 stsp cached = 1;
1332 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1333 ab2f42e7 2019-11-10 stsp free(delta_buf);
1334 ab2f42e7 2019-11-10 stsp return err;
1335 ab2f42e7 2019-11-10 stsp }
1336 ab2f42e7 2019-11-10 stsp }
1337 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1338 42c69117 2019-11-10 stsp delta_buf, delta_len);
1339 ab2f42e7 2019-11-10 stsp if (!cached)
1340 ab2f42e7 2019-11-10 stsp free(delta_buf);
1341 22484865 2018-03-13 stsp if (err)
1342 22484865 2018-03-13 stsp return err;
1343 22484865 2018-03-13 stsp } else
1344 22484865 2018-03-13 stsp base_size = delta->size;
1345 22484865 2018-03-13 stsp if (base_size > *max_size)
1346 22484865 2018-03-13 stsp *max_size = base_size;
1347 22484865 2018-03-13 stsp if (result_size > *max_size)
1348 22484865 2018-03-13 stsp *max_size = result_size;
1349 22484865 2018-03-13 stsp }
1350 bd1223b9 2018-03-14 stsp
1351 9feb4ff2 2018-03-14 stsp return NULL;
1352 ac544f8c 2019-01-13 stsp }
1353 ac544f8c 2019-01-13 stsp
1354 ac544f8c 2019-01-13 stsp const struct got_error *
1355 42c69117 2019-11-10 stsp got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
1356 42c69117 2019-11-10 stsp struct got_pack *pack)
1357 ac544f8c 2019-01-13 stsp {
1358 85a703fa 2019-01-13 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0)
1359 85a703fa 2019-01-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1360 85a703fa 2019-01-13 stsp
1361 d582f26c 2020-03-18 stsp return got_pack_get_delta_chain_max_size(size, &obj->deltas, pack);
1362 22484865 2018-03-13 stsp }
1363 22484865 2018-03-13 stsp
1364 668a20f6 2020-03-18 stsp const struct got_error *
1365 4788f1ce 2020-03-18 stsp got_pack_dump_delta_chain_to_file(size_t *result_size,
1366 4788f1ce 2020-03-18 stsp struct got_delta_chain *deltas, struct got_pack *pack, FILE *outfile,
1367 4788f1ce 2020-03-18 stsp FILE *base_file, FILE *accum_file)
1368 efd2a263 2018-01-19 stsp {
1369 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1370 6691714a 2018-01-23 stsp struct got_delta *delta;
1371 aabb25f8 2022-10-17 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1372 6395114c 2022-05-31 stsp size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1373 6395114c 2022-05-31 stsp /* We process small enough files entirely in memory for speed. */
1374 6395114c 2022-05-31 stsp const size_t max_bufsize = GOT_DELTA_RESULT_SIZE_CACHED_MAX;
1375 6395114c 2022-05-31 stsp uint64_t max_size = 0;
1376 6691714a 2018-01-23 stsp int n = 0;
1377 b29656e2 2018-03-16 stsp
1378 b29656e2 2018-03-16 stsp *result_size = 0;
1379 3ee5fc21 2018-01-17 stsp
1380 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&deltas->entries))
1381 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1382 efd2a263 2018-01-19 stsp
1383 6395114c 2022-05-31 stsp if (fseeko(base_file, 0L, SEEK_SET) == -1)
1384 6395114c 2022-05-31 stsp return got_error_from_errno("fseeko");
1385 6395114c 2022-05-31 stsp if (fseeko(accum_file, 0L, SEEK_SET) == -1)
1386 6395114c 2022-05-31 stsp return got_error_from_errno("fseeko");
1387 efd2a263 2018-01-19 stsp
1388 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1389 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1390 aabb25f8 2022-10-17 stsp uint8_t *delta_buf = NULL;
1391 6395114c 2022-05-31 stsp uint64_t base_size, result_size = 0;
1392 ab2f42e7 2019-11-10 stsp int cached = 1;
1393 a6b158cc 2018-02-11 stsp if (n == 0) {
1394 34fca9c3 2018-11-11 stsp size_t mapoff;
1395 0c048b15 2018-04-27 stsp off_t delta_data_offset;
1396 9db65d41 2018-03-14 stsp
1397 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1398 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1399 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1400 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1401 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1402 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1403 72eb3431 2018-04-01 stsp goto done;
1404 72eb3431 2018-04-01 stsp }
1405 72eb3431 2018-04-01 stsp
1406 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1407 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1408 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1409 0c048b15 2018-04-27 stsp goto done;
1410 0c048b15 2018-04-27 stsp }
1411 d7464085 2018-07-09 stsp if (pack->map == NULL) {
1412 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1413 d7464085 2018-07-09 stsp == -1) {
1414 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1415 d7464085 2018-07-09 stsp goto done;
1416 d7464085 2018-07-09 stsp }
1417 bdd2fbb3 2018-02-11 stsp }
1418 6395114c 2022-05-31 stsp if (delta->size > max_size)
1419 6395114c 2022-05-31 stsp max_size = delta->size;
1420 6395114c 2022-05-31 stsp if (max_size > max_bufsize) {
1421 d7464085 2018-07-09 stsp if (pack->map) {
1422 ad4cc361 2022-10-27 op if (delta_data_offset > SIZE_MAX) {
1423 ad4cc361 2022-10-27 op return got_error_fmt(
1424 ad4cc361 2022-10-27 op GOT_ERR_RANGE,
1425 ad4cc361 2022-10-27 op "delta offset %lld "
1426 ad4cc361 2022-10-27 op "overflows size_t",
1427 ad4cc361 2022-10-27 op (long long)
1428 ad4cc361 2022-10-27 op delta_data_offset);
1429 ad4cc361 2022-10-27 op }
1430 ad4cc361 2022-10-27 op
1431 ad4cc361 2022-10-27 op mapoff = delta_data_offset;
1432 d7464085 2018-07-09 stsp err = got_inflate_to_file_mmap(
1433 4788f1ce 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->map,
1434 4788f1ce 2020-03-18 stsp mapoff, pack->filesize - mapoff,
1435 4788f1ce 2020-03-18 stsp base_file);
1436 d7464085 2018-07-09 stsp } else
1437 34fca9c3 2018-11-11 stsp err = got_inflate_to_file_fd(
1438 4788f1ce 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->fd,
1439 4788f1ce 2020-03-18 stsp base_file);
1440 d7464085 2018-07-09 stsp } else {
1441 6395114c 2022-05-31 stsp accum_buf = malloc(max_size);
1442 6395114c 2022-05-31 stsp if (accum_buf == NULL) {
1443 6395114c 2022-05-31 stsp err = got_error_from_errno("malloc");
1444 6395114c 2022-05-31 stsp goto done;
1445 6395114c 2022-05-31 stsp }
1446 6395114c 2022-05-31 stsp accum_bufsz = max_size;
1447 d7464085 2018-07-09 stsp if (pack->map) {
1448 ad4cc361 2022-10-27 op if (delta_data_offset > SIZE_MAX) {
1449 ad4cc361 2022-10-27 op return got_error_fmt(
1450 ad4cc361 2022-10-27 op GOT_ERR_RANGE,
1451 ad4cc361 2022-10-27 op "delta offset %lld "
1452 ad4cc361 2022-10-27 op "overflows size_t",
1453 ad4cc361 2022-10-27 op (long long)
1454 ad4cc361 2022-10-27 op delta_data_offset);
1455 ad4cc361 2022-10-27 op }
1456 ad4cc361 2022-10-27 op
1457 ad4cc361 2022-10-27 op mapoff = delta_data_offset;
1458 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1459 2e5a6fad 2020-03-18 stsp &base_bufsz, NULL, NULL,
1460 2e5a6fad 2020-03-18 stsp pack->map, mapoff,
1461 d7464085 2018-07-09 stsp pack->filesize - mapoff);
1462 d7464085 2018-07-09 stsp } else
1463 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1464 55fdd257 2020-03-18 stsp &base_bufsz, NULL, NULL, max_size,
1465 55fdd257 2020-03-18 stsp pack->fd);
1466 8628c62d 2018-03-15 stsp }
1467 a6b158cc 2018-02-11 stsp if (err)
1468 a6b158cc 2018-02-11 stsp goto done;
1469 a6b158cc 2018-02-11 stsp n++;
1470 6395114c 2022-05-31 stsp if (base_buf == NULL)
1471 8628c62d 2018-03-15 stsp rewind(base_file);
1472 a6b158cc 2018-02-11 stsp continue;
1473 9db65d41 2018-03-14 stsp }
1474 885d3e02 2018-01-27 stsp
1475 aabb25f8 2022-10-17 stsp if (pack->delta_cache) {
1476 aabb25f8 2022-10-17 stsp got_delta_cache_get(&delta_buf, &delta_len,
1477 aabb25f8 2022-10-17 stsp pack->delta_cache, delta->data_offset);
1478 aabb25f8 2022-10-17 stsp }
1479 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1480 ab2f42e7 2019-11-10 stsp cached = 0;
1481 2d9e6abf 2022-05-04 stsp err = read_delta_data(&delta_buf, &delta_len, NULL,
1482 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1483 ab2f42e7 2019-11-10 stsp if (err)
1484 ab2f42e7 2019-11-10 stsp goto done;
1485 aabb25f8 2022-10-17 stsp }
1486 aabb25f8 2022-10-17 stsp if (pack->delta_cache && !cached) {
1487 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1488 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1489 ab2f42e7 2019-11-10 stsp if (err == NULL)
1490 ab2f42e7 2019-11-10 stsp cached = 1;
1491 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1492 ab2f42e7 2019-11-10 stsp free(delta_buf);
1493 ab2f42e7 2019-11-10 stsp goto done;
1494 ab2f42e7 2019-11-10 stsp }
1495 ab2f42e7 2019-11-10 stsp }
1496 6395114c 2022-05-31 stsp
1497 6395114c 2022-05-31 stsp err = got_delta_get_sizes(&base_size, &result_size,
1498 6395114c 2022-05-31 stsp delta_buf, delta_len);
1499 e62fc520 2022-11-08 stsp if (err) {
1500 e62fc520 2022-11-08 stsp if (!cached)
1501 e62fc520 2022-11-08 stsp free(delta_buf);
1502 6395114c 2022-05-31 stsp goto done;
1503 e62fc520 2022-11-08 stsp }
1504 6395114c 2022-05-31 stsp if (base_size > max_size)
1505 6395114c 2022-05-31 stsp max_size = base_size;
1506 6395114c 2022-05-31 stsp if (result_size > max_size)
1507 6395114c 2022-05-31 stsp max_size = result_size;
1508 6395114c 2022-05-31 stsp
1509 6395114c 2022-05-31 stsp if (base_buf && max_size > max_bufsize) {
1510 6395114c 2022-05-31 stsp /* Switch from buffers to temporary files. */
1511 6395114c 2022-05-31 stsp size_t w = fwrite(base_buf, 1, base_bufsz,
1512 6395114c 2022-05-31 stsp base_file);
1513 6395114c 2022-05-31 stsp if (w != base_bufsz) {
1514 6395114c 2022-05-31 stsp err = got_ferror(outfile, GOT_ERR_IO);
1515 e62fc520 2022-11-08 stsp if (!cached)
1516 e62fc520 2022-11-08 stsp free(delta_buf);
1517 6395114c 2022-05-31 stsp goto done;
1518 6395114c 2022-05-31 stsp }
1519 6395114c 2022-05-31 stsp free(base_buf);
1520 6395114c 2022-05-31 stsp base_buf = NULL;
1521 6395114c 2022-05-31 stsp free(accum_buf);
1522 6395114c 2022-05-31 stsp accum_buf = NULL;
1523 6395114c 2022-05-31 stsp }
1524 6395114c 2022-05-31 stsp
1525 6395114c 2022-05-31 stsp if (base_buf && max_size > base_bufsz) {
1526 6395114c 2022-05-31 stsp uint8_t *p = realloc(base_buf, max_size);
1527 6395114c 2022-05-31 stsp if (p == NULL) {
1528 6395114c 2022-05-31 stsp err = got_error_from_errno("realloc");
1529 e62fc520 2022-11-08 stsp if (!cached)
1530 e62fc520 2022-11-08 stsp free(delta_buf);
1531 6395114c 2022-05-31 stsp goto done;
1532 6395114c 2022-05-31 stsp }
1533 6395114c 2022-05-31 stsp base_buf = p;
1534 6395114c 2022-05-31 stsp base_bufsz = max_size;
1535 6395114c 2022-05-31 stsp }
1536 6395114c 2022-05-31 stsp
1537 6395114c 2022-05-31 stsp if (accum_buf && max_size > accum_bufsz) {
1538 6395114c 2022-05-31 stsp uint8_t *p = realloc(accum_buf, max_size);
1539 6395114c 2022-05-31 stsp if (p == NULL) {
1540 6395114c 2022-05-31 stsp err = got_error_from_errno("realloc");
1541 e62fc520 2022-11-08 stsp if (!cached)
1542 e62fc520 2022-11-08 stsp free(delta_buf);
1543 6395114c 2022-05-31 stsp goto done;
1544 6395114c 2022-05-31 stsp }
1545 6395114c 2022-05-31 stsp accum_buf = p;
1546 6395114c 2022-05-31 stsp accum_bufsz = max_size;
1547 6395114c 2022-05-31 stsp }
1548 6395114c 2022-05-31 stsp
1549 8628c62d 2018-03-15 stsp if (base_buf) {
1550 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1551 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1552 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1553 8628c62d 2018-03-15 stsp n++;
1554 8628c62d 2018-03-15 stsp } else {
1555 42c69117 2019-11-10 stsp err = got_delta_apply(base_file, delta_buf,
1556 42c69117 2019-11-10 stsp delta_len,
1557 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1558 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1559 b29656e2 2018-03-16 stsp &accum_size);
1560 8628c62d 2018-03-15 stsp }
1561 ab2f42e7 2019-11-10 stsp if (!cached)
1562 ab2f42e7 2019-11-10 stsp free(delta_buf);
1563 6691714a 2018-01-23 stsp if (err)
1564 6691714a 2018-01-23 stsp goto done;
1565 6691714a 2018-01-23 stsp
1566 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1567 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1568 8628c62d 2018-03-15 stsp if (base_buf) {
1569 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1570 6395114c 2022-05-31 stsp size_t tmp_size = accum_bufsz;
1571 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1572 6395114c 2022-05-31 stsp accum_bufsz = base_bufsz;
1573 8628c62d 2018-03-15 stsp base_buf = tmp;
1574 6395114c 2022-05-31 stsp base_bufsz = tmp_size;
1575 8628c62d 2018-03-15 stsp } else {
1576 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1577 8628c62d 2018-03-15 stsp accum_file = base_file;
1578 8628c62d 2018-03-15 stsp base_file = tmp;
1579 8628c62d 2018-03-15 stsp rewind(base_file);
1580 8628c62d 2018-03-15 stsp rewind(accum_file);
1581 8628c62d 2018-03-15 stsp }
1582 6691714a 2018-01-23 stsp }
1583 6691714a 2018-01-23 stsp }
1584 6691714a 2018-01-23 stsp
1585 6691714a 2018-01-23 stsp done:
1586 8628c62d 2018-03-15 stsp free(base_buf);
1587 8628c62d 2018-03-15 stsp if (accum_buf) {
1588 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1589 8628c62d 2018-03-15 stsp free(accum_buf);
1590 8628c62d 2018-03-15 stsp if (len != accum_size)
1591 673702af 2018-07-23 stsp err = got_ferror(outfile, GOT_ERR_IO);
1592 8628c62d 2018-03-15 stsp }
1593 6691714a 2018-01-23 stsp rewind(outfile);
1594 b29656e2 2018-03-16 stsp if (err == NULL)
1595 b29656e2 2018-03-16 stsp *result_size = accum_size;
1596 e0ab43e7 2018-03-16 stsp return err;
1597 e0ab43e7 2018-03-16 stsp }
1598 e0ab43e7 2018-03-16 stsp
1599 668a20f6 2020-03-18 stsp const struct got_error *
1600 668a20f6 2020-03-18 stsp got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1601 48095039 2018-09-09 stsp struct got_delta_chain *deltas, struct got_pack *pack)
1602 e0ab43e7 2018-03-16 stsp {
1603 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1604 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1605 aabb25f8 2022-10-17 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1606 f18c433a 2022-05-31 stsp size_t base_bufsz = 0, accum_bufsz = 0, accum_size = 0, delta_len;
1607 f18c433a 2022-05-31 stsp uint64_t max_size = 0;
1608 e0ab43e7 2018-03-16 stsp int n = 0;
1609 e0ab43e7 2018-03-16 stsp
1610 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1611 e0ab43e7 2018-03-16 stsp *outlen = 0;
1612 e0ab43e7 2018-03-16 stsp
1613 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&deltas->entries))
1614 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1615 e0ab43e7 2018-03-16 stsp
1616 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1617 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(delta, &deltas->entries, entry) {
1618 aabb25f8 2022-10-17 stsp uint8_t *delta_buf = NULL;
1619 f18c433a 2022-05-31 stsp uint64_t base_size, result_size = 0;
1620 ab2f42e7 2019-11-10 stsp int cached = 1;
1621 e0ab43e7 2018-03-16 stsp if (n == 0) {
1622 ad4cc361 2022-10-27 op off_t delta_data_offset;
1623 e0ab43e7 2018-03-16 stsp
1624 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1625 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1626 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1627 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1628 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1629 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1630 72eb3431 2018-04-01 stsp goto done;
1631 72eb3431 2018-04-01 stsp }
1632 72eb3431 2018-04-01 stsp
1633 0c048b15 2018-04-27 stsp delta_data_offset = delta->offset + delta->tslen;
1634 0c048b15 2018-04-27 stsp if (delta_data_offset >= pack->filesize) {
1635 0c048b15 2018-04-27 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1636 0c048b15 2018-04-27 stsp goto done;
1637 0c048b15 2018-04-27 stsp }
1638 f18c433a 2022-05-31 stsp
1639 f18c433a 2022-05-31 stsp if (delta->size > max_size)
1640 f18c433a 2022-05-31 stsp max_size = delta->size;
1641 f18c433a 2022-05-31 stsp
1642 d7464085 2018-07-09 stsp if (pack->map) {
1643 ad4cc361 2022-10-27 op size_t mapoff;
1644 ad4cc361 2022-10-27 op
1645 ad4cc361 2022-10-27 op if (delta_data_offset > SIZE_MAX) {
1646 ad4cc361 2022-10-27 op return got_error_fmt(GOT_ERR_RANGE,
1647 ad4cc361 2022-10-27 op "delta %lld offset would "
1648 ad4cc361 2022-10-27 op "overflow size_t",
1649 ad4cc361 2022-10-27 op (long long)delta_data_offset);
1650 ad4cc361 2022-10-27 op }
1651 ad4cc361 2022-10-27 op
1652 ad4cc361 2022-10-27 op mapoff = delta_data_offset;
1653 d7464085 2018-07-09 stsp err = got_inflate_to_mem_mmap(&base_buf,
1654 2e5a6fad 2020-03-18 stsp &base_bufsz, NULL, NULL, pack->map,
1655 2e5a6fad 2020-03-18 stsp mapoff, pack->filesize - mapoff);
1656 d7464085 2018-07-09 stsp } else {
1657 d7464085 2018-07-09 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET)
1658 d7464085 2018-07-09 stsp == -1) {
1659 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1660 d7464085 2018-07-09 stsp goto done;
1661 d7464085 2018-07-09 stsp }
1662 d7464085 2018-07-09 stsp err = got_inflate_to_mem_fd(&base_buf,
1663 55fdd257 2020-03-18 stsp &base_bufsz, NULL, NULL, max_size,
1664 55fdd257 2020-03-18 stsp pack->fd);
1665 e0ab43e7 2018-03-16 stsp }
1666 d7464085 2018-07-09 stsp if (err)
1667 d7464085 2018-07-09 stsp goto done;
1668 e0ab43e7 2018-03-16 stsp n++;
1669 e0ab43e7 2018-03-16 stsp continue;
1670 e0ab43e7 2018-03-16 stsp }
1671 e0ab43e7 2018-03-16 stsp
1672 aabb25f8 2022-10-17 stsp if (pack->delta_cache) {
1673 aabb25f8 2022-10-17 stsp got_delta_cache_get(&delta_buf, &delta_len,
1674 aabb25f8 2022-10-17 stsp pack->delta_cache, delta->data_offset);
1675 aabb25f8 2022-10-17 stsp }
1676 ab2f42e7 2019-11-10 stsp if (delta_buf == NULL) {
1677 ab2f42e7 2019-11-10 stsp cached = 0;
1678 2d9e6abf 2022-05-04 stsp err = read_delta_data(&delta_buf, &delta_len, NULL,
1679 ab2f42e7 2019-11-10 stsp delta->data_offset, pack);
1680 ab2f42e7 2019-11-10 stsp if (err)
1681 ab2f42e7 2019-11-10 stsp goto done;
1682 aabb25f8 2022-10-17 stsp }
1683 aabb25f8 2022-10-17 stsp if (pack->delta_cache && !cached) {
1684 ab2f42e7 2019-11-10 stsp err = got_delta_cache_add(pack->delta_cache,
1685 ab2f42e7 2019-11-10 stsp delta->data_offset, delta_buf, delta_len);
1686 ab2f42e7 2019-11-10 stsp if (err == NULL)
1687 ab2f42e7 2019-11-10 stsp cached = 1;
1688 ab2f42e7 2019-11-10 stsp else if (err->code != GOT_ERR_NO_SPACE) {
1689 ab2f42e7 2019-11-10 stsp free(delta_buf);
1690 ab2f42e7 2019-11-10 stsp goto done;
1691 ab2f42e7 2019-11-10 stsp }
1692 ab2f42e7 2019-11-10 stsp }
1693 f18c433a 2022-05-31 stsp
1694 f18c433a 2022-05-31 stsp err = got_delta_get_sizes(&base_size, &result_size,
1695 f18c433a 2022-05-31 stsp delta_buf, delta_len);
1696 e62fc520 2022-11-08 stsp if (err) {
1697 e62fc520 2022-11-08 stsp if (!cached)
1698 e62fc520 2022-11-08 stsp free(delta_buf);
1699 f18c433a 2022-05-31 stsp goto done;
1700 e62fc520 2022-11-08 stsp }
1701 f18c433a 2022-05-31 stsp if (base_size > max_size)
1702 f18c433a 2022-05-31 stsp max_size = base_size;
1703 f18c433a 2022-05-31 stsp if (result_size > max_size)
1704 f18c433a 2022-05-31 stsp max_size = result_size;
1705 f18c433a 2022-05-31 stsp
1706 f18c433a 2022-05-31 stsp if (max_size > base_bufsz) {
1707 f18c433a 2022-05-31 stsp uint8_t *p = realloc(base_buf, max_size);
1708 f18c433a 2022-05-31 stsp if (p == NULL) {
1709 f18c433a 2022-05-31 stsp err = got_error_from_errno("realloc");
1710 e62fc520 2022-11-08 stsp if (!cached)
1711 e62fc520 2022-11-08 stsp free(delta_buf);
1712 f18c433a 2022-05-31 stsp goto done;
1713 f18c433a 2022-05-31 stsp }
1714 f18c433a 2022-05-31 stsp base_buf = p;
1715 f18c433a 2022-05-31 stsp base_bufsz = max_size;
1716 f18c433a 2022-05-31 stsp }
1717 f18c433a 2022-05-31 stsp
1718 f18c433a 2022-05-31 stsp if (max_size > accum_bufsz) {
1719 f18c433a 2022-05-31 stsp uint8_t *p = realloc(accum_buf, max_size);
1720 f18c433a 2022-05-31 stsp if (p == NULL) {
1721 f18c433a 2022-05-31 stsp err = got_error_from_errno("realloc");
1722 e62fc520 2022-11-08 stsp if (!cached)
1723 e62fc520 2022-11-08 stsp free(delta_buf);
1724 f18c433a 2022-05-31 stsp goto done;
1725 f18c433a 2022-05-31 stsp }
1726 f18c433a 2022-05-31 stsp accum_buf = p;
1727 f18c433a 2022-05-31 stsp accum_bufsz = max_size;
1728 f18c433a 2022-05-31 stsp }
1729 f18c433a 2022-05-31 stsp
1730 34fca9c3 2018-11-11 stsp err = got_delta_apply_in_mem(base_buf, base_bufsz,
1731 42c69117 2019-11-10 stsp delta_buf, delta_len, accum_buf,
1732 34fca9c3 2018-11-11 stsp &accum_size, max_size);
1733 ab2f42e7 2019-11-10 stsp if (!cached)
1734 ab2f42e7 2019-11-10 stsp free(delta_buf);
1735 e0ab43e7 2018-03-16 stsp n++;
1736 e0ab43e7 2018-03-16 stsp if (err)
1737 e0ab43e7 2018-03-16 stsp goto done;
1738 e0ab43e7 2018-03-16 stsp
1739 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1740 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1741 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1742 f18c433a 2022-05-31 stsp size_t tmp_size = accum_bufsz;
1743 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1744 f18c433a 2022-05-31 stsp accum_bufsz = base_bufsz;
1745 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1746 f18c433a 2022-05-31 stsp base_bufsz = tmp_size;
1747 e0ab43e7 2018-03-16 stsp }
1748 e0ab43e7 2018-03-16 stsp }
1749 e0ab43e7 2018-03-16 stsp
1750 e0ab43e7 2018-03-16 stsp done:
1751 e0ab43e7 2018-03-16 stsp free(base_buf);
1752 e0ab43e7 2018-03-16 stsp if (err) {
1753 e0ab43e7 2018-03-16 stsp free(accum_buf);
1754 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1755 e0ab43e7 2018-03-16 stsp *outlen = 0;
1756 e0ab43e7 2018-03-16 stsp } else {
1757 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1758 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1759 e0ab43e7 2018-03-16 stsp }
1760 efd2a263 2018-01-19 stsp return err;
1761 efd2a263 2018-01-19 stsp }
1762 efd2a263 2018-01-19 stsp
1763 3ee5fc21 2018-01-17 stsp const struct got_error *
1764 24140570 2018-09-09 stsp got_packfile_extract_object(struct got_pack *pack, struct got_object *obj,
1765 3840f4c9 2018-09-12 stsp FILE *outfile, FILE *base_file, FILE *accum_file)
1766 3ee5fc21 2018-01-17 stsp {
1767 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1768 3ee5fc21 2018-01-17 stsp
1769 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1770 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1771 2ce68b2f 2018-09-09 stsp
1772 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1773 24140570 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1774 24140570 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1775 72eb3431 2018-04-01 stsp
1776 d7464085 2018-07-09 stsp if (pack->map) {
1777 ad4cc361 2022-10-27 op size_t mapoff;
1778 ad4cc361 2022-10-27 op
1779 ad4cc361 2022-10-27 op if (obj->pack_offset > SIZE_MAX) {
1780 ad4cc361 2022-10-27 op return got_error_fmt(GOT_ERR_RANGE,
1781 ad4cc361 2022-10-27 op "pack offset %lld would overflow size_t",
1782 ad4cc361 2022-10-27 op (long long)obj->pack_offset);
1783 ad4cc361 2022-10-27 op }
1784 ad4cc361 2022-10-27 op
1785 ad4cc361 2022-10-27 op mapoff = obj->pack_offset;
1786 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_mmap(&obj->size, NULL, NULL,
1787 4788f1ce 2020-03-18 stsp pack->map, mapoff, pack->filesize - mapoff,
1788 4788f1ce 2020-03-18 stsp outfile);
1789 d7464085 2018-07-09 stsp } else {
1790 24140570 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1791 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1792 4788f1ce 2020-03-18 stsp err = got_inflate_to_file_fd(&obj->size, NULL, NULL,
1793 4788f1ce 2020-03-18 stsp pack->fd, outfile);
1794 d7464085 2018-07-09 stsp }
1795 040bf4a1 2018-04-01 stsp } else
1796 4788f1ce 2020-03-18 stsp err = got_pack_dump_delta_chain_to_file(&obj->size,
1797 4788f1ce 2020-03-18 stsp &obj->deltas, pack, outfile, base_file, accum_file);
1798 24140570 2018-09-09 stsp
1799 a1fd68d8 2018-01-12 stsp return err;
1800 a1fd68d8 2018-01-12 stsp }
1801 e0ab43e7 2018-03-16 stsp
1802 e0ab43e7 2018-03-16 stsp const struct got_error *
1803 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1804 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_pack *pack)
1805 e0ab43e7 2018-03-16 stsp {
1806 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1807 e0ab43e7 2018-03-16 stsp
1808 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1809 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1810 72eb3431 2018-04-01 stsp
1811 48095039 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1812 7e212e3d 2018-09-09 stsp if (obj->pack_offset >= pack->filesize)
1813 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_PACK_OFFSET);
1814 d7464085 2018-07-09 stsp if (pack->map) {
1815 ad4cc361 2022-10-27 op size_t mapoff;
1816 ad4cc361 2022-10-27 op
1817 ad4cc361 2022-10-27 op if (obj->pack_offset > SIZE_MAX) {
1818 ad4cc361 2022-10-27 op return got_error_fmt(GOT_ERR_RANGE,
1819 ad4cc361 2022-10-27 op "pack offset %lld would overflow size_t",
1820 ad4cc361 2022-10-27 op (long long)obj->pack_offset);
1821 ad4cc361 2022-10-27 op }
1822 ad4cc361 2022-10-27 op
1823 ad4cc361 2022-10-27 op mapoff = obj->pack_offset;
1824 2e5a6fad 2020-03-18 stsp err = got_inflate_to_mem_mmap(buf, len, NULL, NULL,
1825 2e5a6fad 2020-03-18 stsp pack->map, mapoff, pack->filesize - mapoff);
1826 d7464085 2018-07-09 stsp } else {
1827 7e212e3d 2018-09-09 stsp if (lseek(pack->fd, obj->pack_offset, SEEK_SET) == -1)
1828 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1829 1e87a3c3 2020-03-18 stsp err = got_inflate_to_mem_fd(buf, len, NULL, NULL,
1830 55fdd257 2020-03-18 stsp obj->size, pack->fd);
1831 e0ab43e7 2018-03-16 stsp }
1832 e0ab43e7 2018-03-16 stsp } else
1833 668a20f6 2020-03-18 stsp err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
1834 668a20f6 2020-03-18 stsp pack);
1835 7e212e3d 2018-09-09 stsp
1836 e0ab43e7 2018-03-16 stsp return err;
1837 e0ab43e7 2018-03-16 stsp }
1838 67fd6849 2022-02-13 stsp
1839 2d9e6abf 2022-05-04 stsp static const struct got_error *
1840 2d9e6abf 2022-05-04 stsp read_raw_delta_data(uint8_t **delta_buf, size_t *delta_len,
1841 2d9e6abf 2022-05-04 stsp size_t *delta_len_compressed, uint64_t *base_size, uint64_t *result_size,
1842 2d9e6abf 2022-05-04 stsp off_t delta_data_offset, struct got_pack *pack, struct got_packidx *packidx)
1843 2d9e6abf 2022-05-04 stsp {
1844 2d9e6abf 2022-05-04 stsp const struct got_error *err = NULL;
1845 2d9e6abf 2022-05-04 stsp
1846 2d9e6abf 2022-05-04 stsp /* Validate decompression and obtain the decompressed size. */
1847 2d9e6abf 2022-05-04 stsp err = read_delta_data(delta_buf, delta_len, delta_len_compressed,
1848 2d9e6abf 2022-05-04 stsp delta_data_offset, pack);
1849 2d9e6abf 2022-05-04 stsp if (err)
1850 2d9e6abf 2022-05-04 stsp return err;
1851 2d9e6abf 2022-05-04 stsp
1852 2d9e6abf 2022-05-04 stsp /* Read delta base/result sizes from head of delta stream. */
1853 2d9e6abf 2022-05-04 stsp err = got_delta_get_sizes(base_size, result_size,
1854 2d9e6abf 2022-05-04 stsp *delta_buf, *delta_len);
1855 2d9e6abf 2022-05-04 stsp if (err)
1856 2d9e6abf 2022-05-04 stsp goto done;
1857 2d9e6abf 2022-05-04 stsp
1858 2d9e6abf 2022-05-04 stsp /* Discard decompressed delta and read it again in compressed form. */
1859 2d9e6abf 2022-05-04 stsp free(*delta_buf);
1860 2d9e6abf 2022-05-04 stsp *delta_buf = malloc(*delta_len_compressed);
1861 2d9e6abf 2022-05-04 stsp if (*delta_buf == NULL) {
1862 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("malloc");
1863 2d9e6abf 2022-05-04 stsp goto done;
1864 2d9e6abf 2022-05-04 stsp }
1865 2d9e6abf 2022-05-04 stsp if (pack->map) {
1866 82031ac8 2022-10-24 op if (delta_data_offset >= pack->filesize) {
1867 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_PACK_OFFSET);
1868 82031ac8 2022-10-24 op goto done;
1869 82031ac8 2022-10-24 op }
1870 2d9e6abf 2022-05-04 stsp memcpy(*delta_buf, pack->map + delta_data_offset,
1871 2d9e6abf 2022-05-04 stsp *delta_len_compressed);
1872 2d9e6abf 2022-05-04 stsp } else {
1873 2d9e6abf 2022-05-04 stsp ssize_t n;
1874 2d9e6abf 2022-05-04 stsp if (lseek(pack->fd, delta_data_offset, SEEK_SET) == -1) {
1875 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("lseek");
1876 2d9e6abf 2022-05-04 stsp goto done;
1877 2d9e6abf 2022-05-04 stsp }
1878 2d9e6abf 2022-05-04 stsp n = read(pack->fd, *delta_buf, *delta_len_compressed);
1879 2d9e6abf 2022-05-04 stsp if (n < 0) {
1880 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("read");
1881 2d9e6abf 2022-05-04 stsp goto done;
1882 2d9e6abf 2022-05-04 stsp } else if (n != *delta_len_compressed) {
1883 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_IO);
1884 2d9e6abf 2022-05-04 stsp goto done;
1885 2d9e6abf 2022-05-04 stsp }
1886 2d9e6abf 2022-05-04 stsp }
1887 2d9e6abf 2022-05-04 stsp done:
1888 2d9e6abf 2022-05-04 stsp if (err) {
1889 2d9e6abf 2022-05-04 stsp free(*delta_buf);
1890 2d9e6abf 2022-05-04 stsp *delta_buf = NULL;
1891 2d9e6abf 2022-05-04 stsp *delta_len = 0;
1892 2d9e6abf 2022-05-04 stsp *delta_len_compressed = 0;
1893 2d9e6abf 2022-05-04 stsp *base_size = 0;
1894 2d9e6abf 2022-05-04 stsp *result_size = 0;
1895 2d9e6abf 2022-05-04 stsp }
1896 2d9e6abf 2022-05-04 stsp return err;
1897 2d9e6abf 2022-05-04 stsp }
1898 2d9e6abf 2022-05-04 stsp
1899 67fd6849 2022-02-13 stsp const struct got_error *
1900 67fd6849 2022-02-13 stsp got_packfile_extract_raw_delta(uint8_t **delta_buf, size_t *delta_size,
1901 24b7de1c 2022-12-03 stsp size_t *delta_compressed_size, off_t *delta_offset,
1902 24b7de1c 2022-12-03 stsp off_t *delta_data_offset, off_t *base_offset,
1903 2d9e6abf 2022-05-04 stsp struct got_object_id *base_id, uint64_t *base_size, uint64_t *result_size,
1904 2d9e6abf 2022-05-04 stsp struct got_pack *pack, struct got_packidx *packidx, int idx)
1905 67fd6849 2022-02-13 stsp {
1906 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1907 67fd6849 2022-02-13 stsp off_t offset;
1908 67fd6849 2022-02-13 stsp uint8_t type;
1909 67fd6849 2022-02-13 stsp uint64_t size;
1910 67fd6849 2022-02-13 stsp size_t tslen, delta_hdrlen;
1911 67fd6849 2022-02-13 stsp
1912 67fd6849 2022-02-13 stsp *delta_buf = NULL;
1913 67fd6849 2022-02-13 stsp *delta_size = 0;
1914 2d9e6abf 2022-05-04 stsp *delta_compressed_size = 0;
1915 67fd6849 2022-02-13 stsp *delta_offset = 0;
1916 24b7de1c 2022-12-03 stsp *delta_data_offset = 0;
1917 67fd6849 2022-02-13 stsp *base_offset = 0;
1918 67fd6849 2022-02-13 stsp *base_size = 0;
1919 67fd6849 2022-02-13 stsp *result_size = 0;
1920 67fd6849 2022-02-13 stsp
1921 67fd6849 2022-02-13 stsp offset = got_packidx_get_object_offset(packidx, idx);
1922 04aed155 2022-07-24 naddy if (offset == -1)
1923 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
1924 67fd6849 2022-02-13 stsp
1925 67fd6849 2022-02-13 stsp if (offset >= pack->filesize)
1926 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PACK_OFFSET);
1927 67fd6849 2022-02-13 stsp
1928 67fd6849 2022-02-13 stsp err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
1929 67fd6849 2022-02-13 stsp pack, offset);
1930 67fd6849 2022-02-13 stsp if (err)
1931 67fd6849 2022-02-13 stsp return err;
1932 67fd6849 2022-02-13 stsp
1933 67fd6849 2022-02-13 stsp if (tslen + size < tslen || offset + size < size ||
1934 67fd6849 2022-02-13 stsp tslen + offset < tslen)
1935 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PACK_OFFSET);
1936 67fd6849 2022-02-13 stsp
1937 67fd6849 2022-02-13 stsp switch (type) {
1938 67fd6849 2022-02-13 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1939 67fd6849 2022-02-13 stsp err = got_pack_parse_offset_delta(base_offset, &delta_hdrlen,
1940 67fd6849 2022-02-13 stsp pack, offset, tslen);
1941 67fd6849 2022-02-13 stsp if (err)
1942 67fd6849 2022-02-13 stsp return err;
1943 67fd6849 2022-02-13 stsp break;
1944 67fd6849 2022-02-13 stsp case GOT_OBJ_TYPE_REF_DELTA:
1945 67fd6849 2022-02-13 stsp err = got_pack_parse_ref_delta(base_id, pack, offset, tslen);
1946 67fd6849 2022-02-13 stsp if (err)
1947 67fd6849 2022-02-13 stsp return err;
1948 67fd6849 2022-02-13 stsp delta_hdrlen = SHA1_DIGEST_LENGTH;
1949 67fd6849 2022-02-13 stsp break;
1950 67fd6849 2022-02-13 stsp default:
1951 67fd6849 2022-02-13 stsp return got_error_fmt(GOT_ERR_OBJ_TYPE,
1952 04aed155 2022-07-24 naddy "non-delta object type %d found at offset %lld",
1953 04aed155 2022-07-24 naddy type, (long long)offset);
1954 67fd6849 2022-02-13 stsp }
1955 67fd6849 2022-02-13 stsp
1956 67fd6849 2022-02-13 stsp if (tslen + delta_hdrlen < delta_hdrlen ||
1957 67fd6849 2022-02-13 stsp offset + delta_hdrlen < delta_hdrlen)
1958 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_DELTA);
1959 67fd6849 2022-02-13 stsp
1960 24b7de1c 2022-12-03 stsp *delta_data_offset = offset + tslen + delta_hdrlen;
1961 2d9e6abf 2022-05-04 stsp err = read_raw_delta_data(delta_buf, delta_size, delta_compressed_size,
1962 24b7de1c 2022-12-03 stsp base_size, result_size, *delta_data_offset, pack, packidx);
1963 67fd6849 2022-02-13 stsp if (err)
1964 67fd6849 2022-02-13 stsp return err;
1965 67fd6849 2022-02-13 stsp
1966 67fd6849 2022-02-13 stsp if (*delta_size != size) {
1967 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_BAD_DELTA);
1968 67fd6849 2022-02-13 stsp goto done;
1969 67fd6849 2022-02-13 stsp }
1970 67fd6849 2022-02-13 stsp
1971 67fd6849 2022-02-13 stsp *delta_offset = offset;
1972 67fd6849 2022-02-13 stsp done:
1973 67fd6849 2022-02-13 stsp if (err) {
1974 67fd6849 2022-02-13 stsp free(*delta_buf);
1975 67fd6849 2022-02-13 stsp *delta_buf = NULL;
1976 2d9e6abf 2022-05-04 stsp *delta_size = 0;
1977 2d9e6abf 2022-05-04 stsp *delta_compressed_size = 0;
1978 2d9e6abf 2022-05-04 stsp *delta_offset = 0;
1979 2d9e6abf 2022-05-04 stsp *base_offset = 0;
1980 2d9e6abf 2022-05-04 stsp *base_size = 0;
1981 2d9e6abf 2022-05-04 stsp *result_size = 0;
1982 67fd6849 2022-02-13 stsp }
1983 67fd6849 2022-02-13 stsp return err;
1984 67fd6849 2022-02-13 stsp }