2 efd2a263 2018-01-19 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 efd2a263 2018-01-19 stsp #include <sys/queue.h>
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 788c352e 2018-06-16 stsp #include <time.h>
26 efd2a263 2018-01-19 stsp #include "got_error.h"
27 efd2a263 2018-01-19 stsp #include "got_repository.h"
28 efd2a263 2018-01-19 stsp #include "got_object.h"
30 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
31 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
32 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
35 885d3e02 2018-01-27 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 c3703302 2018-01-23 stsp struct got_delta *
39 c336f889 2018-07-23 stsp got_delta_open(off_t offset, size_t tslen, int type, size_t size,
40 c336f889 2018-07-23 stsp off_t data_offset, uint8_t *delta_buf, size_t delta_len)
42 c3703302 2018-01-23 stsp struct got_delta *delta;
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;
48 c3703302 2018-01-23 stsp delta->type = type;
49 c3703302 2018-01-23 stsp delta->offset = offset;
50 0e22967e 2018-02-11 stsp delta->tslen = tslen;
51 c3703302 2018-01-23 stsp delta->size = size;
52 bdd2fbb3 2018-02-11 stsp delta->data_offset = data_offset;
53 a53d2f13 2018-03-17 stsp delta->delta_buf = delta_buf;
54 a53d2f13 2018-03-17 stsp delta->delta_len = delta_len;
55 c3703302 2018-01-23 stsp return delta;
59 c3703302 2018-01-23 stsp got_delta_close(struct got_delta *delta)
61 a53d2f13 2018-03-17 stsp free(delta->delta_buf);
62 c3703302 2018-01-23 stsp free(delta);
65 efd2a263 2018-01-19 stsp const struct got_error *
66 96f5e8b3 2018-01-23 stsp got_delta_chain_get_base_type(int *type, struct got_delta_chain *deltas)
68 c3703302 2018-01-23 stsp struct got_delta *delta;
70 6691714a 2018-01-23 stsp /* The first delta in the chain should represent the base object. */
71 6691714a 2018-01-23 stsp delta = SIMPLEQ_FIRST(&deltas->entries);
72 6691714a 2018-01-23 stsp if (delta->type == GOT_OBJ_TYPE_COMMIT ||
73 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TREE ||
74 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_BLOB ||
75 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TAG) {
76 6691714a 2018-01-23 stsp *type = delta->type;
77 6691714a 2018-01-23 stsp return NULL;
80 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
83 885d3e02 2018-01-27 stsp /* Fetch another (required) byte from the delta stream. */
84 885d3e02 2018-01-27 stsp static const struct got_error *
85 885d3e02 2018-01-27 stsp next_delta_byte(const uint8_t **p, size_t *remain)
87 885d3e02 2018-01-27 stsp if (--(*remain) == 0)
88 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
90 885d3e02 2018-01-27 stsp return NULL;
93 885d3e02 2018-01-27 stsp static const struct got_error *
94 885d3e02 2018-01-27 stsp parse_size(uint64_t *size, const uint8_t **p, size_t *remain)
96 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
101 885d3e02 2018-01-27 stsp /* We do not support size values which don't fit in 64 bit. */
103 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_NO_SPACE);
105 885d3e02 2018-01-27 stsp if (i == 0)
106 885d3e02 2018-01-27 stsp *size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
108 885d3e02 2018-01-27 stsp size_t shift = GOT_DELTA_SIZE_SHIFT * i;
109 885d3e02 2018-01-27 stsp *size |= (((**p) & GOT_DELTA_SIZE_VAL_MASK) << shift);
112 885d3e02 2018-01-27 stsp if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
115 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
116 885d3e02 2018-01-27 stsp } while (err == NULL);
118 885d3e02 2018-01-27 stsp return err;
121 885d3e02 2018-01-27 stsp static const struct got_error *
122 885d3e02 2018-01-27 stsp parse_opcode(off_t *offset, size_t *len, const uint8_t **p, size_t *remain)
124 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
125 885d3e02 2018-01-27 stsp off_t o = 0;
126 885d3e02 2018-01-27 stsp size_t l = 0;
127 885d3e02 2018-01-27 stsp uint8_t opcode = **p;
129 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF1) {
130 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
132 885d3e02 2018-01-27 stsp return err;
133 885d3e02 2018-01-27 stsp o = (off_t)(**p);
135 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF2) {
136 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
138 885d3e02 2018-01-27 stsp return err;
139 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 8;
141 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF3) {
142 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
144 885d3e02 2018-01-27 stsp return err;
145 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 16;
147 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF4) {
148 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
150 885d3e02 2018-01-27 stsp return err;
151 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 24;
154 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN1) {
155 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
157 885d3e02 2018-01-27 stsp return err;
158 885d3e02 2018-01-27 stsp l = (off_t)(**p);
160 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN2) {
161 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
163 885d3e02 2018-01-27 stsp return err;
164 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 8;
166 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN3) {
167 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
169 885d3e02 2018-01-27 stsp return err;
170 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 16;
173 885d3e02 2018-01-27 stsp if (o == 0)
174 885d3e02 2018-01-27 stsp o = GOT_DELTA_COPY_DEFAULT_OFF;
175 885d3e02 2018-01-27 stsp if (l == 0)
176 885d3e02 2018-01-27 stsp l = GOT_DELTA_COPY_DEFAULT_LEN;
178 885d3e02 2018-01-27 stsp *offset = o;
180 885d3e02 2018-01-27 stsp return NULL;
183 885d3e02 2018-01-27 stsp static const struct got_error *
184 885d3e02 2018-01-27 stsp copy_from_base(FILE *base_file, off_t offset, size_t size, FILE *outfile)
186 885d3e02 2018-01-27 stsp if (fseeko(base_file, offset, SEEK_SET) != 0)
187 885d3e02 2018-01-27 stsp return got_error_from_errno();
189 885d3e02 2018-01-27 stsp while (size > 0) {
190 885d3e02 2018-01-27 stsp uint8_t data[2048];
191 885d3e02 2018-01-27 stsp size_t len = MIN(size, sizeof(data));
194 885d3e02 2018-01-27 stsp n = fread(data, len, 1, base_file);
195 885d3e02 2018-01-27 stsp if (n != 1)
196 885d3e02 2018-01-27 stsp return got_ferror(base_file, GOT_ERR_IO);
198 885d3e02 2018-01-27 stsp n = fwrite(data, len, 1, outfile);
199 885d3e02 2018-01-27 stsp if (n != 1)
200 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
202 885d3e02 2018-01-27 stsp size -= len;
205 885d3e02 2018-01-27 stsp return NULL;
208 885d3e02 2018-01-27 stsp static const struct got_error *
209 885d3e02 2018-01-27 stsp copy_from_delta(const uint8_t **p, size_t *remain, size_t len, FILE *outfile)
213 885d3e02 2018-01-27 stsp if (*remain < len)
214 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
216 885d3e02 2018-01-27 stsp n = fwrite(*p, len, 1, outfile);
217 885d3e02 2018-01-27 stsp if (n != 1)
218 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
221 885d3e02 2018-01-27 stsp *remain -= len;
222 885d3e02 2018-01-27 stsp return NULL;
225 22484865 2018-03-13 stsp static const struct got_error *
226 22484865 2018-03-13 stsp parse_delta_sizes(uint64_t *base_size, uint64_t *result_size,
227 22484865 2018-03-13 stsp const uint8_t **p, size_t *remain)
229 22484865 2018-03-13 stsp const struct got_error *err;
231 22484865 2018-03-13 stsp /* Read the two size fields at the beginning of the stream. */
232 22484865 2018-03-13 stsp err = parse_size(base_size, p, remain);
234 22484865 2018-03-13 stsp return err;
235 22484865 2018-03-13 stsp err = next_delta_byte(p, remain);
237 22484865 2018-03-13 stsp return err;
238 22484865 2018-03-13 stsp err = parse_size(result_size, p, remain);
240 22484865 2018-03-13 stsp return err;
242 22484865 2018-03-13 stsp return NULL;
245 885d3e02 2018-01-27 stsp const struct got_error *
246 22484865 2018-03-13 stsp got_delta_get_sizes(uint64_t *base_size, uint64_t *result_size,
247 22484865 2018-03-13 stsp const uint8_t *delta_buf, size_t delta_len)
249 22484865 2018-03-13 stsp size_t remain;
250 22484865 2018-03-13 stsp const uint8_t *p;
252 22484865 2018-03-13 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
253 22484865 2018-03-13 stsp return got_error(GOT_ERR_BAD_DELTA);
255 22484865 2018-03-13 stsp p = delta_buf;
256 22484865 2018-03-13 stsp remain = delta_len;
257 22484865 2018-03-13 stsp return parse_delta_sizes(base_size, result_size, &p, &remain);
260 8628c62d 2018-03-15 stsp const struct got_error *
261 8628c62d 2018-03-15 stsp got_delta_apply_in_mem(uint8_t *base_buf, const uint8_t *delta_buf,
262 8628c62d 2018-03-15 stsp size_t delta_len, uint8_t *outbuf, size_t *outsize)
264 8628c62d 2018-03-15 stsp const struct got_error *err = NULL;
265 8628c62d 2018-03-15 stsp uint64_t base_size, result_size;
266 8628c62d 2018-03-15 stsp size_t remain;
267 8628c62d 2018-03-15 stsp const uint8_t *p;
269 8628c62d 2018-03-15 stsp *outsize= 0;
271 8628c62d 2018-03-15 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
272 8628c62d 2018-03-15 stsp return got_error(GOT_ERR_BAD_DELTA);
274 8628c62d 2018-03-15 stsp p = delta_buf;
275 8628c62d 2018-03-15 stsp remain = delta_len;
276 8628c62d 2018-03-15 stsp err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
278 8628c62d 2018-03-15 stsp return err;
280 8628c62d 2018-03-15 stsp /* Decode and execute copy instructions from the delta stream. */
281 8628c62d 2018-03-15 stsp err = next_delta_byte(&p, &remain);
282 8628c62d 2018-03-15 stsp while (err == NULL && remain > 0) {
283 8628c62d 2018-03-15 stsp if (*p & GOT_DELTA_BASE_COPY) {
284 8628c62d 2018-03-15 stsp off_t offset = 0;
285 8628c62d 2018-03-15 stsp size_t len = 0;
286 8628c62d 2018-03-15 stsp err = parse_opcode(&offset, &len, &p, &remain);
289 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, base_buf + offset, len);
290 8628c62d 2018-03-15 stsp if (err == NULL) {
291 8628c62d 2018-03-15 stsp *outsize += len;
292 8628c62d 2018-03-15 stsp if (remain > 0) {
298 8628c62d 2018-03-15 stsp size_t len = (size_t)*p;
299 8628c62d 2018-03-15 stsp if (len == 0) {
300 8628c62d 2018-03-15 stsp err = got_error(GOT_ERR_BAD_DELTA);
303 8628c62d 2018-03-15 stsp err = next_delta_byte(&p, &remain);
306 8628c62d 2018-03-15 stsp if (remain < len)
307 8628c62d 2018-03-15 stsp return got_error(GOT_ERR_BAD_DELTA);
308 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, p, len);
310 8628c62d 2018-03-15 stsp remain -= len;
311 8628c62d 2018-03-15 stsp *outsize += len;
315 8628c62d 2018-03-15 stsp if (*outsize != result_size)
316 8628c62d 2018-03-15 stsp err = got_error(GOT_ERR_BAD_DELTA);
317 8628c62d 2018-03-15 stsp return err;
320 22484865 2018-03-13 stsp const struct got_error *
321 0e22967e 2018-02-11 stsp got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
322 b29656e2 2018-03-16 stsp size_t delta_len, FILE *outfile, size_t *outsize)
324 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
325 885d3e02 2018-01-27 stsp uint64_t base_size, result_size;
326 b29656e2 2018-03-16 stsp size_t remain = 0;
327 885d3e02 2018-01-27 stsp const uint8_t *p;
328 39ff877f 2018-03-13 stsp FILE *memstream = NULL;
329 39ff877f 2018-03-13 stsp char *memstream_buf = NULL;
330 39ff877f 2018-03-13 stsp size_t memstream_size = 0;
332 b29656e2 2018-03-16 stsp *outsize = 0;
334 885d3e02 2018-01-27 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
335 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
337 885d3e02 2018-01-27 stsp p = delta_buf;
338 885d3e02 2018-01-27 stsp remain = delta_len;
339 22484865 2018-03-13 stsp err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
341 885d3e02 2018-01-27 stsp return err;
343 39ff877f 2018-03-13 stsp if (result_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
344 39ff877f 2018-03-13 stsp memstream = open_memstream(&memstream_buf, &memstream_size);
346 885d3e02 2018-01-27 stsp /* Decode and execute copy instructions from the delta stream. */
347 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
348 06e5fc98 2018-02-11 stsp while (err == NULL && remain > 0) {
349 824801e7 2018-01-27 stsp if (*p & GOT_DELTA_BASE_COPY) {
350 885d3e02 2018-01-27 stsp off_t offset = 0;
351 885d3e02 2018-01-27 stsp size_t len = 0;
352 885d3e02 2018-01-27 stsp err = parse_opcode(&offset, &len, &p, &remain);
355 39ff877f 2018-03-13 stsp err = copy_from_base(base_file, offset, len,
356 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
357 06e5fc98 2018-02-11 stsp if (err == NULL) {
358 b29656e2 2018-03-16 stsp *outsize += len;
359 06e5fc98 2018-02-11 stsp if (remain > 0) {
365 885d3e02 2018-01-27 stsp size_t len = (size_t)*p;
366 885d3e02 2018-01-27 stsp if (len == 0) {
367 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
370 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
373 39ff877f 2018-03-13 stsp err = copy_from_delta(&p, &remain, len,
374 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
375 885d3e02 2018-01-27 stsp if (err == NULL)
376 b29656e2 2018-03-16 stsp *outsize += len;
380 b29656e2 2018-03-16 stsp if (*outsize != result_size)
381 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
383 39ff877f 2018-03-13 stsp if (memstream != NULL) {
384 39ff877f 2018-03-13 stsp fclose(memstream);
385 39ff877f 2018-03-13 stsp if (err == NULL) {
387 39ff877f 2018-03-13 stsp n = fwrite(memstream_buf, 1, memstream_size, outfile);
388 39ff877f 2018-03-13 stsp if (n != memstream_size)
389 39ff877f 2018-03-13 stsp err = got_ferror(outfile, GOT_ERR_IO);
391 39ff877f 2018-03-13 stsp free(memstream_buf);
393 885d3e02 2018-01-27 stsp if (err == NULL)
394 885d3e02 2018-01-27 stsp rewind(outfile);
395 885d3e02 2018-01-27 stsp return err;