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