Blame


1 efd2a263 2018-01-19 stsp /*
2 efd2a263 2018-01-19 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 efd2a263 2018-01-19 stsp *
4 efd2a263 2018-01-19 stsp * Permission to use, copy, modify, and distribute this software for any
5 efd2a263 2018-01-19 stsp * purpose with or without fee is hereby granted, provided that the above
6 efd2a263 2018-01-19 stsp * copyright notice and this permission notice appear in all copies.
7 efd2a263 2018-01-19 stsp *
8 efd2a263 2018-01-19 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 efd2a263 2018-01-19 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 efd2a263 2018-01-19 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 efd2a263 2018-01-19 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 efd2a263 2018-01-19 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 efd2a263 2018-01-19 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 efd2a263 2018-01-19 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 efd2a263 2018-01-19 stsp */
16 efd2a263 2018-01-19 stsp
17 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
18 efd2a263 2018-01-19 stsp
19 efd2a263 2018-01-19 stsp #include <stdio.h>
20 96f5e8b3 2018-01-23 stsp #include <stdlib.h>
21 b2f7af54 2018-11-11 stsp #include <stdint.h>
22 96f5e8b3 2018-01-23 stsp #include <string.h>
23 56e0773d 2019-11-28 stsp #include <limits.h>
24 efd2a263 2018-01-19 stsp #include <zlib.h>
25 788c352e 2018-06-16 stsp #include <time.h>
26 15a94983 2018-12-23 stsp #include <zlib.h>
27 efd2a263 2018-01-19 stsp
28 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
29 efd2a263 2018-01-19 stsp #include "got_error.h"
30 efd2a263 2018-01-19 stsp #include "got_repository.h"
31 efd2a263 2018-01-19 stsp #include "got_object.h"
32 324d37e7 2019-05-11 stsp #include "got_path.h"
33 efd2a263 2018-01-19 stsp
34 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
35 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
36 15a94983 2018-12-23 stsp #include "got_lib_object.h"
37 efd2a263 2018-01-19 stsp
38 885d3e02 2018-01-27 stsp #ifndef MIN
39 885d3e02 2018-01-27 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 885d3e02 2018-01-27 stsp #endif
41 885d3e02 2018-01-27 stsp
42 c3703302 2018-01-23 stsp struct got_delta *
43 c336f889 2018-07-23 stsp got_delta_open(off_t offset, size_t tslen, int type, size_t size,
44 42c69117 2019-11-10 stsp off_t data_offset)
45 96f5e8b3 2018-01-23 stsp {
46 c3703302 2018-01-23 stsp struct got_delta *delta;
47 96f5e8b3 2018-01-23 stsp
48 14118581 2018-11-05 stsp delta = malloc(sizeof(*delta));
49 c3703302 2018-01-23 stsp if (delta == NULL)
50 96f5e8b3 2018-01-23 stsp return NULL;
51 96f5e8b3 2018-01-23 stsp
52 c3703302 2018-01-23 stsp delta->type = type;
53 c3703302 2018-01-23 stsp delta->offset = offset;
54 0e22967e 2018-02-11 stsp delta->tslen = tslen;
55 c3703302 2018-01-23 stsp delta->size = size;
56 bdd2fbb3 2018-02-11 stsp delta->data_offset = data_offset;
57 c3703302 2018-01-23 stsp return delta;
58 96f5e8b3 2018-01-23 stsp }
59 96f5e8b3 2018-01-23 stsp
60 efd2a263 2018-01-19 stsp const struct got_error *
61 96f5e8b3 2018-01-23 stsp got_delta_chain_get_base_type(int *type, struct got_delta_chain *deltas)
62 96f5e8b3 2018-01-23 stsp {
63 c3703302 2018-01-23 stsp struct got_delta *delta;
64 96f5e8b3 2018-01-23 stsp
65 6691714a 2018-01-23 stsp /* The first delta in the chain should represent the base object. */
66 dbdddfee 2021-06-23 naddy delta = STAILQ_FIRST(&deltas->entries);
67 6691714a 2018-01-23 stsp if (delta->type == GOT_OBJ_TYPE_COMMIT ||
68 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TREE ||
69 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_BLOB ||
70 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TAG) {
71 6691714a 2018-01-23 stsp *type = delta->type;
72 6691714a 2018-01-23 stsp return NULL;
73 96f5e8b3 2018-01-23 stsp }
74 96f5e8b3 2018-01-23 stsp
75 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
76 96f5e8b3 2018-01-23 stsp }
77 96f5e8b3 2018-01-23 stsp
78 885d3e02 2018-01-27 stsp /* Fetch another (required) byte from the delta stream. */
79 885d3e02 2018-01-27 stsp static const struct got_error *
80 885d3e02 2018-01-27 stsp next_delta_byte(const uint8_t **p, size_t *remain)
81 efd2a263 2018-01-19 stsp {
82 885d3e02 2018-01-27 stsp if (--(*remain) == 0)
83 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
84 9069347b 2021-05-20 stsp "delta data truncated");
85 885d3e02 2018-01-27 stsp (*p)++;
86 885d3e02 2018-01-27 stsp return NULL;
87 efd2a263 2018-01-19 stsp }
88 885d3e02 2018-01-27 stsp
89 885d3e02 2018-01-27 stsp static const struct got_error *
90 885d3e02 2018-01-27 stsp parse_size(uint64_t *size, const uint8_t **p, size_t *remain)
91 885d3e02 2018-01-27 stsp {
92 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
93 885d3e02 2018-01-27 stsp int i = 0;
94 885d3e02 2018-01-27 stsp
95 885d3e02 2018-01-27 stsp *size = 0;
96 885d3e02 2018-01-27 stsp do {
97 885d3e02 2018-01-27 stsp /* We do not support size values which don't fit in 64 bit. */
98 885d3e02 2018-01-27 stsp if (i > 9)
99 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_NO_SPACE);
100 885d3e02 2018-01-27 stsp
101 885d3e02 2018-01-27 stsp if (i == 0)
102 885d3e02 2018-01-27 stsp *size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
103 885d3e02 2018-01-27 stsp else {
104 885d3e02 2018-01-27 stsp size_t shift = GOT_DELTA_SIZE_SHIFT * i;
105 885d3e02 2018-01-27 stsp *size |= (((**p) & GOT_DELTA_SIZE_VAL_MASK) << shift);
106 885d3e02 2018-01-27 stsp }
107 885d3e02 2018-01-27 stsp
108 885d3e02 2018-01-27 stsp if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
109 885d3e02 2018-01-27 stsp break;
110 885d3e02 2018-01-27 stsp i++;
111 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
112 885d3e02 2018-01-27 stsp } while (err == NULL);
113 885d3e02 2018-01-27 stsp
114 885d3e02 2018-01-27 stsp return err;
115 885d3e02 2018-01-27 stsp }
116 885d3e02 2018-01-27 stsp
117 885d3e02 2018-01-27 stsp static const struct got_error *
118 885d3e02 2018-01-27 stsp parse_opcode(off_t *offset, size_t *len, const uint8_t **p, size_t *remain)
119 885d3e02 2018-01-27 stsp {
120 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
121 885d3e02 2018-01-27 stsp off_t o = 0;
122 885d3e02 2018-01-27 stsp size_t l = 0;
123 885d3e02 2018-01-27 stsp uint8_t opcode = **p;
124 885d3e02 2018-01-27 stsp
125 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF1) {
126 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
127 885d3e02 2018-01-27 stsp if (err)
128 885d3e02 2018-01-27 stsp return err;
129 885d3e02 2018-01-27 stsp o = (off_t)(**p);
130 885d3e02 2018-01-27 stsp }
131 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF2) {
132 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
133 885d3e02 2018-01-27 stsp if (err)
134 885d3e02 2018-01-27 stsp return err;
135 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 8;
136 885d3e02 2018-01-27 stsp }
137 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF3) {
138 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
139 885d3e02 2018-01-27 stsp if (err)
140 885d3e02 2018-01-27 stsp return err;
141 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 16;
142 885d3e02 2018-01-27 stsp }
143 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF4) {
144 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
145 885d3e02 2018-01-27 stsp if (err)
146 885d3e02 2018-01-27 stsp return err;
147 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 24;
148 885d3e02 2018-01-27 stsp }
149 885d3e02 2018-01-27 stsp
150 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN1) {
151 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
152 885d3e02 2018-01-27 stsp if (err)
153 885d3e02 2018-01-27 stsp return err;
154 885d3e02 2018-01-27 stsp l = (off_t)(**p);
155 885d3e02 2018-01-27 stsp }
156 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN2) {
157 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
158 885d3e02 2018-01-27 stsp if (err)
159 885d3e02 2018-01-27 stsp return err;
160 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 8;
161 885d3e02 2018-01-27 stsp }
162 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN3) {
163 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
164 885d3e02 2018-01-27 stsp if (err)
165 885d3e02 2018-01-27 stsp return err;
166 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 16;
167 885d3e02 2018-01-27 stsp }
168 885d3e02 2018-01-27 stsp
169 885d3e02 2018-01-27 stsp if (o == 0)
170 885d3e02 2018-01-27 stsp o = GOT_DELTA_COPY_DEFAULT_OFF;
171 885d3e02 2018-01-27 stsp if (l == 0)
172 885d3e02 2018-01-27 stsp l = GOT_DELTA_COPY_DEFAULT_LEN;
173 885d3e02 2018-01-27 stsp
174 885d3e02 2018-01-27 stsp *offset = o;
175 885d3e02 2018-01-27 stsp *len = l;
176 885d3e02 2018-01-27 stsp return NULL;
177 885d3e02 2018-01-27 stsp }
178 885d3e02 2018-01-27 stsp
179 885d3e02 2018-01-27 stsp static const struct got_error *
180 885d3e02 2018-01-27 stsp copy_from_base(FILE *base_file, off_t offset, size_t size, FILE *outfile)
181 885d3e02 2018-01-27 stsp {
182 885d3e02 2018-01-27 stsp if (fseeko(base_file, offset, SEEK_SET) != 0)
183 638f9024 2019-05-13 stsp return got_error_from_errno("fseeko");
184 885d3e02 2018-01-27 stsp
185 885d3e02 2018-01-27 stsp while (size > 0) {
186 885d3e02 2018-01-27 stsp uint8_t data[2048];
187 885d3e02 2018-01-27 stsp size_t len = MIN(size, sizeof(data));
188 885d3e02 2018-01-27 stsp size_t n;
189 885d3e02 2018-01-27 stsp
190 885d3e02 2018-01-27 stsp n = fread(data, len, 1, base_file);
191 885d3e02 2018-01-27 stsp if (n != 1)
192 885d3e02 2018-01-27 stsp return got_ferror(base_file, GOT_ERR_IO);
193 885d3e02 2018-01-27 stsp
194 885d3e02 2018-01-27 stsp n = fwrite(data, len, 1, outfile);
195 885d3e02 2018-01-27 stsp if (n != 1)
196 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
197 885d3e02 2018-01-27 stsp
198 885d3e02 2018-01-27 stsp size -= len;
199 885d3e02 2018-01-27 stsp }
200 885d3e02 2018-01-27 stsp
201 885d3e02 2018-01-27 stsp return NULL;
202 885d3e02 2018-01-27 stsp }
203 885d3e02 2018-01-27 stsp
204 885d3e02 2018-01-27 stsp static const struct got_error *
205 885d3e02 2018-01-27 stsp copy_from_delta(const uint8_t **p, size_t *remain, size_t len, FILE *outfile)
206 885d3e02 2018-01-27 stsp {
207 885d3e02 2018-01-27 stsp size_t n;
208 885d3e02 2018-01-27 stsp
209 885d3e02 2018-01-27 stsp if (*remain < len)
210 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
211 9069347b 2021-05-20 stsp "copy from beyond end of delta data");
212 885d3e02 2018-01-27 stsp
213 885d3e02 2018-01-27 stsp n = fwrite(*p, len, 1, outfile);
214 885d3e02 2018-01-27 stsp if (n != 1)
215 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
216 885d3e02 2018-01-27 stsp
217 885d3e02 2018-01-27 stsp *p += len;
218 885d3e02 2018-01-27 stsp *remain -= len;
219 885d3e02 2018-01-27 stsp return NULL;
220 885d3e02 2018-01-27 stsp }
221 885d3e02 2018-01-27 stsp
222 22484865 2018-03-13 stsp static const struct got_error *
223 22484865 2018-03-13 stsp parse_delta_sizes(uint64_t *base_size, uint64_t *result_size,
224 22484865 2018-03-13 stsp const uint8_t **p, size_t *remain)
225 22484865 2018-03-13 stsp {
226 22484865 2018-03-13 stsp const struct got_error *err;
227 22484865 2018-03-13 stsp
228 22484865 2018-03-13 stsp /* Read the two size fields at the beginning of the stream. */
229 22484865 2018-03-13 stsp err = parse_size(base_size, p, remain);
230 22484865 2018-03-13 stsp if (err)
231 22484865 2018-03-13 stsp return err;
232 22484865 2018-03-13 stsp err = next_delta_byte(p, remain);
233 22484865 2018-03-13 stsp if (err)
234 22484865 2018-03-13 stsp return err;
235 22484865 2018-03-13 stsp err = parse_size(result_size, p, remain);
236 22484865 2018-03-13 stsp if (err)
237 22484865 2018-03-13 stsp return err;
238 22484865 2018-03-13 stsp
239 22484865 2018-03-13 stsp return NULL;
240 22484865 2018-03-13 stsp }
241 22484865 2018-03-13 stsp
242 885d3e02 2018-01-27 stsp const struct got_error *
243 22484865 2018-03-13 stsp got_delta_get_sizes(uint64_t *base_size, uint64_t *result_size,
244 22484865 2018-03-13 stsp const uint8_t *delta_buf, size_t delta_len)
245 22484865 2018-03-13 stsp {
246 22484865 2018-03-13 stsp size_t remain;
247 22484865 2018-03-13 stsp const uint8_t *p;
248 22484865 2018-03-13 stsp
249 22484865 2018-03-13 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
250 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA, "delta too small");
251 22484865 2018-03-13 stsp
252 22484865 2018-03-13 stsp p = delta_buf;
253 22484865 2018-03-13 stsp remain = delta_len;
254 22484865 2018-03-13 stsp return parse_delta_sizes(base_size, result_size, &p, &remain);
255 8628c62d 2018-03-15 stsp }
256 8628c62d 2018-03-15 stsp
257 8628c62d 2018-03-15 stsp const struct got_error *
258 34fca9c3 2018-11-11 stsp got_delta_apply_in_mem(uint8_t *base_buf, size_t base_bufsz,
259 34fca9c3 2018-11-11 stsp const uint8_t *delta_buf, size_t delta_len, uint8_t *outbuf,
260 34fca9c3 2018-11-11 stsp size_t *outsize, size_t maxoutsize)
261 8628c62d 2018-03-15 stsp {
262 8628c62d 2018-03-15 stsp const struct got_error *err = NULL;
263 8628c62d 2018-03-15 stsp uint64_t base_size, result_size;
264 8628c62d 2018-03-15 stsp size_t remain;
265 8628c62d 2018-03-15 stsp const uint8_t *p;
266 8628c62d 2018-03-15 stsp
267 8628c62d 2018-03-15 stsp *outsize= 0;
268 8628c62d 2018-03-15 stsp
269 8628c62d 2018-03-15 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
270 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA, "delta too small");
271 8628c62d 2018-03-15 stsp
272 8628c62d 2018-03-15 stsp p = delta_buf;
273 8628c62d 2018-03-15 stsp remain = delta_len;
274 8628c62d 2018-03-15 stsp err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
275 8628c62d 2018-03-15 stsp if (err)
276 8628c62d 2018-03-15 stsp return err;
277 8628c62d 2018-03-15 stsp
278 8628c62d 2018-03-15 stsp /* Decode and execute copy instructions from the delta stream. */
279 8628c62d 2018-03-15 stsp err = next_delta_byte(&p, &remain);
280 8628c62d 2018-03-15 stsp while (err == NULL && remain > 0) {
281 8628c62d 2018-03-15 stsp if (*p & GOT_DELTA_BASE_COPY) {
282 8628c62d 2018-03-15 stsp off_t offset = 0;
283 8628c62d 2018-03-15 stsp size_t len = 0;
284 8628c62d 2018-03-15 stsp err = parse_opcode(&offset, &len, &p, &remain);
285 8628c62d 2018-03-15 stsp if (err)
286 8628c62d 2018-03-15 stsp break;
287 b2f7af54 2018-11-11 stsp if (SIZE_MAX - offset < len || offset + len < 0 ||
288 b2f7af54 2018-11-11 stsp base_bufsz < offset + len ||
289 34fca9c3 2018-11-11 stsp *outsize + len > maxoutsize)
290 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
291 9069347b 2021-05-20 stsp "bad delta copy length");
292 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, base_buf + offset, len);
293 8628c62d 2018-03-15 stsp if (err == NULL) {
294 8628c62d 2018-03-15 stsp *outsize += len;
295 8628c62d 2018-03-15 stsp if (remain > 0) {
296 8628c62d 2018-03-15 stsp p++;
297 8628c62d 2018-03-15 stsp remain--;
298 8628c62d 2018-03-15 stsp }
299 8628c62d 2018-03-15 stsp }
300 8628c62d 2018-03-15 stsp } else {
301 8628c62d 2018-03-15 stsp size_t len = (size_t)*p;
302 8628c62d 2018-03-15 stsp if (len == 0) {
303 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
304 9069347b 2021-05-20 stsp "zero length delta");
305 8628c62d 2018-03-15 stsp break;
306 8628c62d 2018-03-15 stsp }
307 8628c62d 2018-03-15 stsp err = next_delta_byte(&p, &remain);
308 8628c62d 2018-03-15 stsp if (err)
309 8628c62d 2018-03-15 stsp break;
310 b2f7af54 2018-11-11 stsp if (remain < len || SIZE_MAX - *outsize < len ||
311 b2f7af54 2018-11-11 stsp *outsize + len > maxoutsize)
312 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
313 9069347b 2021-05-20 stsp "bad delta copy length");
314 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, p, len);
315 8628c62d 2018-03-15 stsp p += len;
316 8628c62d 2018-03-15 stsp remain -= len;
317 8628c62d 2018-03-15 stsp *outsize += len;
318 8628c62d 2018-03-15 stsp }
319 8628c62d 2018-03-15 stsp }
320 8628c62d 2018-03-15 stsp
321 8628c62d 2018-03-15 stsp if (*outsize != result_size)
322 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
323 9069347b 2021-05-20 stsp "delta application result size mismatch");
324 8628c62d 2018-03-15 stsp return err;
325 22484865 2018-03-13 stsp }
326 22484865 2018-03-13 stsp
327 22484865 2018-03-13 stsp const struct got_error *
328 0e22967e 2018-02-11 stsp got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
329 b29656e2 2018-03-16 stsp size_t delta_len, FILE *outfile, size_t *outsize)
330 885d3e02 2018-01-27 stsp {
331 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
332 885d3e02 2018-01-27 stsp uint64_t base_size, result_size;
333 b29656e2 2018-03-16 stsp size_t remain = 0;
334 885d3e02 2018-01-27 stsp const uint8_t *p;
335 39ff877f 2018-03-13 stsp FILE *memstream = NULL;
336 39ff877f 2018-03-13 stsp char *memstream_buf = NULL;
337 39ff877f 2018-03-13 stsp size_t memstream_size = 0;
338 b29656e2 2018-03-16 stsp
339 b29656e2 2018-03-16 stsp *outsize = 0;
340 885d3e02 2018-01-27 stsp
341 885d3e02 2018-01-27 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
342 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA, "delta too small");
343 885d3e02 2018-01-27 stsp
344 885d3e02 2018-01-27 stsp p = delta_buf;
345 885d3e02 2018-01-27 stsp remain = delta_len;
346 22484865 2018-03-13 stsp err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
347 885d3e02 2018-01-27 stsp if (err)
348 885d3e02 2018-01-27 stsp return err;
349 885d3e02 2018-01-27 stsp
350 39ff877f 2018-03-13 stsp if (result_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
351 39ff877f 2018-03-13 stsp memstream = open_memstream(&memstream_buf, &memstream_size);
352 39ff877f 2018-03-13 stsp
353 885d3e02 2018-01-27 stsp /* Decode and execute copy instructions from the delta stream. */
354 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
355 06e5fc98 2018-02-11 stsp while (err == NULL && remain > 0) {
356 824801e7 2018-01-27 stsp if (*p & GOT_DELTA_BASE_COPY) {
357 885d3e02 2018-01-27 stsp off_t offset = 0;
358 885d3e02 2018-01-27 stsp size_t len = 0;
359 885d3e02 2018-01-27 stsp err = parse_opcode(&offset, &len, &p, &remain);
360 885d3e02 2018-01-27 stsp if (err)
361 885d3e02 2018-01-27 stsp break;
362 39ff877f 2018-03-13 stsp err = copy_from_base(base_file, offset, len,
363 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
364 06e5fc98 2018-02-11 stsp if (err == NULL) {
365 b29656e2 2018-03-16 stsp *outsize += len;
366 06e5fc98 2018-02-11 stsp if (remain > 0) {
367 06e5fc98 2018-02-11 stsp p++;
368 06e5fc98 2018-02-11 stsp remain--;
369 06e5fc98 2018-02-11 stsp }
370 06e5fc98 2018-02-11 stsp }
371 885d3e02 2018-01-27 stsp } else {
372 885d3e02 2018-01-27 stsp size_t len = (size_t)*p;
373 885d3e02 2018-01-27 stsp if (len == 0) {
374 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
375 9069347b 2021-05-20 stsp "zero length delta");
376 885d3e02 2018-01-27 stsp break;
377 885d3e02 2018-01-27 stsp }
378 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
379 885d3e02 2018-01-27 stsp if (err)
380 06e5fc98 2018-02-11 stsp break;
381 39ff877f 2018-03-13 stsp err = copy_from_delta(&p, &remain, len,
382 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
383 885d3e02 2018-01-27 stsp if (err == NULL)
384 b29656e2 2018-03-16 stsp *outsize += len;
385 885d3e02 2018-01-27 stsp }
386 885d3e02 2018-01-27 stsp }
387 885d3e02 2018-01-27 stsp
388 b29656e2 2018-03-16 stsp if (*outsize != result_size)
389 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
390 9069347b 2021-05-20 stsp "delta application result size mismatch");
391 885d3e02 2018-01-27 stsp
392 39ff877f 2018-03-13 stsp if (memstream != NULL) {
393 56b63ca4 2021-01-22 stsp if (fclose(memstream) == EOF)
394 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
395 39ff877f 2018-03-13 stsp if (err == NULL) {
396 39ff877f 2018-03-13 stsp size_t n;
397 39ff877f 2018-03-13 stsp n = fwrite(memstream_buf, 1, memstream_size, outfile);
398 39ff877f 2018-03-13 stsp if (n != memstream_size)
399 39ff877f 2018-03-13 stsp err = got_ferror(outfile, GOT_ERR_IO);
400 39ff877f 2018-03-13 stsp }
401 39ff877f 2018-03-13 stsp free(memstream_buf);
402 39ff877f 2018-03-13 stsp }
403 885d3e02 2018-01-27 stsp if (err == NULL)
404 885d3e02 2018-01-27 stsp rewind(outfile);
405 885d3e02 2018-01-27 stsp return err;
406 885d3e02 2018-01-27 stsp }