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