Blame


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