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 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 efd2a263 2018-01-19 stsp #include <sha1.h>
26 5822e79e 2023-02-23 op #include <sha2.h>
27 788c352e 2018-06-16 stsp #include <time.h>
28 15a94983 2018-12-23 stsp #include <zlib.h>
30 efd2a263 2018-01-19 stsp #include "got_error.h"
31 efd2a263 2018-01-19 stsp #include "got_repository.h"
32 efd2a263 2018-01-19 stsp #include "got_object.h"
33 324d37e7 2019-05-11 stsp #include "got_path.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
36 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
37 15a94983 2018-12-23 stsp #include "got_lib_object.h"
40 885d3e02 2018-01-27 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 c3703302 2018-01-23 stsp struct got_delta *
44 c336f889 2018-07-23 stsp got_delta_open(off_t offset, size_t tslen, int type, size_t size,
45 42c69117 2019-11-10 stsp off_t data_offset)
47 c3703302 2018-01-23 stsp struct got_delta *delta;
49 14118581 2018-11-05 stsp delta = malloc(sizeof(*delta));
50 c3703302 2018-01-23 stsp if (delta == NULL)
51 96f5e8b3 2018-01-23 stsp return NULL;
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 c3703302 2018-01-23 stsp return delta;
61 efd2a263 2018-01-19 stsp const struct got_error *
62 96f5e8b3 2018-01-23 stsp got_delta_chain_get_base_type(int *type, struct got_delta_chain *deltas)
64 c3703302 2018-01-23 stsp struct got_delta *delta;
66 6691714a 2018-01-23 stsp /* The first delta in the chain should represent the base object. */
67 dbdddfee 2021-06-23 naddy delta = STAILQ_FIRST(&deltas->entries);
68 6691714a 2018-01-23 stsp if (delta->type == GOT_OBJ_TYPE_COMMIT ||
69 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TREE ||
70 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_BLOB ||
71 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TAG) {
72 6691714a 2018-01-23 stsp *type = delta->type;
73 6691714a 2018-01-23 stsp return NULL;
76 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
79 885d3e02 2018-01-27 stsp /* Fetch another (required) byte from the delta stream. */
80 885d3e02 2018-01-27 stsp static const struct got_error *
81 885d3e02 2018-01-27 stsp next_delta_byte(const uint8_t **p, size_t *remain)
83 885d3e02 2018-01-27 stsp if (--(*remain) == 0)
84 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
85 9069347b 2021-05-20 stsp "delta data truncated");
87 885d3e02 2018-01-27 stsp return NULL;
90 885d3e02 2018-01-27 stsp static const struct got_error *
91 885d3e02 2018-01-27 stsp parse_size(uint64_t *size, const uint8_t **p, size_t *remain)
93 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
98 885d3e02 2018-01-27 stsp /* We do not support size values which don't fit in 64 bit. */
100 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_NO_SPACE);
102 885d3e02 2018-01-27 stsp if (i == 0)
103 885d3e02 2018-01-27 stsp *size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
105 885d3e02 2018-01-27 stsp size_t shift = GOT_DELTA_SIZE_SHIFT * i;
106 885d3e02 2018-01-27 stsp *size |= (((**p) & GOT_DELTA_SIZE_VAL_MASK) << shift);
109 885d3e02 2018-01-27 stsp if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
112 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
113 885d3e02 2018-01-27 stsp } while (err == NULL);
115 885d3e02 2018-01-27 stsp return err;
118 885d3e02 2018-01-27 stsp static const struct got_error *
119 885d3e02 2018-01-27 stsp parse_opcode(off_t *offset, size_t *len, const uint8_t **p, size_t *remain)
121 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
122 885d3e02 2018-01-27 stsp off_t o = 0;
123 885d3e02 2018-01-27 stsp size_t l = 0;
124 885d3e02 2018-01-27 stsp uint8_t opcode = **p;
126 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF1) {
127 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
129 885d3e02 2018-01-27 stsp return err;
130 885d3e02 2018-01-27 stsp o = (off_t)(**p);
132 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF2) {
133 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
135 885d3e02 2018-01-27 stsp return err;
136 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 8;
138 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF3) {
139 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
141 885d3e02 2018-01-27 stsp return err;
142 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 16;
144 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF4) {
145 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
147 885d3e02 2018-01-27 stsp return err;
148 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 24;
151 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN1) {
152 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
154 885d3e02 2018-01-27 stsp return err;
155 885d3e02 2018-01-27 stsp l = (off_t)(**p);
157 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN2) {
158 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
160 885d3e02 2018-01-27 stsp return err;
161 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 8;
163 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN3) {
164 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
166 885d3e02 2018-01-27 stsp return err;
167 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 16;
170 885d3e02 2018-01-27 stsp if (o == 0)
171 885d3e02 2018-01-27 stsp o = GOT_DELTA_COPY_DEFAULT_OFF;
172 885d3e02 2018-01-27 stsp if (l == 0)
173 885d3e02 2018-01-27 stsp l = GOT_DELTA_COPY_DEFAULT_LEN;
175 885d3e02 2018-01-27 stsp *offset = o;
177 885d3e02 2018-01-27 stsp return NULL;
180 885d3e02 2018-01-27 stsp static const struct got_error *
181 885d3e02 2018-01-27 stsp copy_from_base(FILE *base_file, off_t offset, size_t size, FILE *outfile)
183 885d3e02 2018-01-27 stsp if (fseeko(base_file, offset, SEEK_SET) != 0)
184 638f9024 2019-05-13 stsp return got_error_from_errno("fseeko");
186 885d3e02 2018-01-27 stsp while (size > 0) {
187 885d3e02 2018-01-27 stsp uint8_t data[2048];
188 885d3e02 2018-01-27 stsp size_t len = MIN(size, sizeof(data));
191 885d3e02 2018-01-27 stsp n = fread(data, len, 1, base_file);
192 885d3e02 2018-01-27 stsp if (n != 1)
193 885d3e02 2018-01-27 stsp return got_ferror(base_file, GOT_ERR_IO);
195 885d3e02 2018-01-27 stsp n = fwrite(data, len, 1, outfile);
196 885d3e02 2018-01-27 stsp if (n != 1)
197 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
199 885d3e02 2018-01-27 stsp size -= len;
202 885d3e02 2018-01-27 stsp return NULL;
205 885d3e02 2018-01-27 stsp static const struct got_error *
206 885d3e02 2018-01-27 stsp copy_from_delta(const uint8_t **p, size_t *remain, size_t len, FILE *outfile)
210 885d3e02 2018-01-27 stsp if (*remain < len)
211 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
212 9069347b 2021-05-20 stsp "copy from beyond end of delta data");
214 885d3e02 2018-01-27 stsp n = fwrite(*p, len, 1, outfile);
215 885d3e02 2018-01-27 stsp if (n != 1)
216 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
219 885d3e02 2018-01-27 stsp *remain -= len;
220 885d3e02 2018-01-27 stsp return NULL;
223 22484865 2018-03-13 stsp static const struct got_error *
224 22484865 2018-03-13 stsp parse_delta_sizes(uint64_t *base_size, uint64_t *result_size,
225 22484865 2018-03-13 stsp const uint8_t **p, size_t *remain)
227 22484865 2018-03-13 stsp const struct got_error *err;
229 22484865 2018-03-13 stsp /* Read the two size fields at the beginning of the stream. */
230 22484865 2018-03-13 stsp err = parse_size(base_size, p, remain);
232 22484865 2018-03-13 stsp return err;
233 22484865 2018-03-13 stsp err = next_delta_byte(p, remain);
235 22484865 2018-03-13 stsp return err;
236 22484865 2018-03-13 stsp err = parse_size(result_size, p, remain);
238 22484865 2018-03-13 stsp return err;
240 22484865 2018-03-13 stsp return NULL;
243 885d3e02 2018-01-27 stsp const struct got_error *
244 22484865 2018-03-13 stsp got_delta_get_sizes(uint64_t *base_size, uint64_t *result_size,
245 22484865 2018-03-13 stsp const uint8_t *delta_buf, size_t delta_len)
247 22484865 2018-03-13 stsp size_t remain;
248 22484865 2018-03-13 stsp const uint8_t *p;
250 22484865 2018-03-13 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
251 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA, "delta too small");
253 22484865 2018-03-13 stsp p = delta_buf;
254 22484865 2018-03-13 stsp remain = delta_len;
255 22484865 2018-03-13 stsp return parse_delta_sizes(base_size, result_size, &p, &remain);
258 8628c62d 2018-03-15 stsp const struct got_error *
259 34fca9c3 2018-11-11 stsp got_delta_apply_in_mem(uint8_t *base_buf, size_t base_bufsz,
260 34fca9c3 2018-11-11 stsp const uint8_t *delta_buf, size_t delta_len, uint8_t *outbuf,
261 34fca9c3 2018-11-11 stsp size_t *outsize, size_t maxoutsize)
263 8628c62d 2018-03-15 stsp const struct got_error *err = NULL;
264 8628c62d 2018-03-15 stsp uint64_t base_size, result_size;
265 8628c62d 2018-03-15 stsp size_t remain;
266 8628c62d 2018-03-15 stsp const uint8_t *p;
268 8628c62d 2018-03-15 stsp *outsize= 0;
270 8628c62d 2018-03-15 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
271 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA, "delta too small");
273 8628c62d 2018-03-15 stsp p = delta_buf;
274 8628c62d 2018-03-15 stsp remain = delta_len;
275 8628c62d 2018-03-15 stsp err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
277 8628c62d 2018-03-15 stsp return err;
279 8628c62d 2018-03-15 stsp /* Decode and execute copy instructions from the delta stream. */
280 8628c62d 2018-03-15 stsp err = next_delta_byte(&p, &remain);
281 8628c62d 2018-03-15 stsp while (err == NULL && remain > 0) {
282 8628c62d 2018-03-15 stsp if (*p & GOT_DELTA_BASE_COPY) {
283 8628c62d 2018-03-15 stsp off_t offset = 0;
284 8628c62d 2018-03-15 stsp size_t len = 0;
285 8628c62d 2018-03-15 stsp err = parse_opcode(&offset, &len, &p, &remain);
288 b2f7af54 2018-11-11 stsp if (SIZE_MAX - offset < len || offset + len < 0 ||
289 b2f7af54 2018-11-11 stsp base_bufsz < offset + len ||
290 34fca9c3 2018-11-11 stsp *outsize + len > maxoutsize)
291 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
292 9069347b 2021-05-20 stsp "bad delta copy length");
293 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, base_buf + offset, len);
294 8628c62d 2018-03-15 stsp if (err == NULL) {
295 8628c62d 2018-03-15 stsp *outsize += len;
296 8628c62d 2018-03-15 stsp if (remain > 0) {
302 8628c62d 2018-03-15 stsp size_t len = (size_t)*p;
303 8628c62d 2018-03-15 stsp if (len == 0) {
304 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
305 9069347b 2021-05-20 stsp "zero length delta");
308 8628c62d 2018-03-15 stsp err = next_delta_byte(&p, &remain);
311 b2f7af54 2018-11-11 stsp if (remain < len || SIZE_MAX - *outsize < len ||
312 b2f7af54 2018-11-11 stsp *outsize + len > maxoutsize)
313 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA,
314 9069347b 2021-05-20 stsp "bad delta copy length");
315 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, p, len);
317 8628c62d 2018-03-15 stsp remain -= len;
318 8628c62d 2018-03-15 stsp *outsize += len;
322 8628c62d 2018-03-15 stsp if (*outsize != result_size)
323 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
324 9069347b 2021-05-20 stsp "delta application result size mismatch");
325 8628c62d 2018-03-15 stsp return err;
328 22484865 2018-03-13 stsp const struct got_error *
329 0e22967e 2018-02-11 stsp got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
330 b29656e2 2018-03-16 stsp size_t delta_len, FILE *outfile, size_t *outsize)
332 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
333 885d3e02 2018-01-27 stsp uint64_t base_size, result_size;
334 b29656e2 2018-03-16 stsp size_t remain = 0;
335 885d3e02 2018-01-27 stsp const uint8_t *p;
336 39ff877f 2018-03-13 stsp FILE *memstream = NULL;
337 39ff877f 2018-03-13 stsp char *memstream_buf = NULL;
338 39ff877f 2018-03-13 stsp size_t memstream_size = 0;
340 b29656e2 2018-03-16 stsp *outsize = 0;
342 885d3e02 2018-01-27 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
343 9069347b 2021-05-20 stsp return got_error_msg(GOT_ERR_BAD_DELTA, "delta too small");
345 885d3e02 2018-01-27 stsp p = delta_buf;
346 885d3e02 2018-01-27 stsp remain = delta_len;
347 22484865 2018-03-13 stsp err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
349 885d3e02 2018-01-27 stsp return err;
351 39ff877f 2018-03-13 stsp if (result_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
352 39ff877f 2018-03-13 stsp memstream = open_memstream(&memstream_buf, &memstream_size);
354 885d3e02 2018-01-27 stsp /* Decode and execute copy instructions from the delta stream. */
355 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
356 06e5fc98 2018-02-11 stsp while (err == NULL && remain > 0) {
357 824801e7 2018-01-27 stsp if (*p & GOT_DELTA_BASE_COPY) {
358 885d3e02 2018-01-27 stsp off_t offset = 0;
359 885d3e02 2018-01-27 stsp size_t len = 0;
360 885d3e02 2018-01-27 stsp err = parse_opcode(&offset, &len, &p, &remain);
363 39ff877f 2018-03-13 stsp err = copy_from_base(base_file, offset, len,
364 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
365 06e5fc98 2018-02-11 stsp if (err == NULL) {
366 b29656e2 2018-03-16 stsp *outsize += len;
367 06e5fc98 2018-02-11 stsp if (remain > 0) {
373 885d3e02 2018-01-27 stsp size_t len = (size_t)*p;
374 885d3e02 2018-01-27 stsp if (len == 0) {
375 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
376 9069347b 2021-05-20 stsp "zero length delta");
379 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
382 39ff877f 2018-03-13 stsp err = copy_from_delta(&p, &remain, len,
383 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
384 885d3e02 2018-01-27 stsp if (err == NULL)
385 b29656e2 2018-03-16 stsp *outsize += len;
389 b29656e2 2018-03-16 stsp if (*outsize != result_size)
390 9069347b 2021-05-20 stsp err = got_error_msg(GOT_ERR_BAD_DELTA,
391 9069347b 2021-05-20 stsp "delta application result size mismatch");
393 39ff877f 2018-03-13 stsp if (memstream != NULL) {
394 56b63ca4 2021-01-22 stsp if (fclose(memstream) == EOF)
395 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
396 39ff877f 2018-03-13 stsp if (err == NULL) {
398 39ff877f 2018-03-13 stsp n = fwrite(memstream_buf, 1, memstream_size, outfile);
399 39ff877f 2018-03-13 stsp if (n != memstream_size)
400 39ff877f 2018-03-13 stsp err = got_ferror(outfile, GOT_ERR_IO);
402 39ff877f 2018-03-13 stsp free(memstream_buf);
404 885d3e02 2018-01-27 stsp if (err == NULL)
405 885d3e02 2018-01-27 stsp rewind(outfile);
406 885d3e02 2018-01-27 stsp return err;