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