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