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