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