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