Blame


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