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