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