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>
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"
29 32cb896c 2018-03-11 stsp #include "got_delta_lib.h"
30 32cb896c 2018-03-11 stsp #include "got_path_lib.h"
31 32cb896c 2018-03-11 stsp #include "got_zbuf_lib.h"
34 885d3e02 2018-01-27 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
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 bdd2fbb3 2018-02-11 stsp int type, size_t size, off_t data_offset)
41 c3703302 2018-01-23 stsp struct got_delta *delta;
43 c3703302 2018-01-23 stsp delta = calloc(1, sizeof(*delta));
44 c3703302 2018-01-23 stsp if (delta == NULL)
45 96f5e8b3 2018-01-23 stsp return NULL;
47 c3703302 2018-01-23 stsp delta->type = type;
48 c3703302 2018-01-23 stsp delta->offset = offset;
49 0e22967e 2018-02-11 stsp delta->tslen = tslen;
50 c3703302 2018-01-23 stsp delta->size = size;
51 bdd2fbb3 2018-02-11 stsp delta->data_offset = data_offset;
52 c3703302 2018-01-23 stsp return delta;
56 c3703302 2018-01-23 stsp got_delta_close(struct got_delta *delta)
58 c3703302 2018-01-23 stsp free(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 6691714a 2018-01-23 stsp delta = SIMPLEQ_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 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
86 885d3e02 2018-01-27 stsp return NULL;
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)
92 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
97 885d3e02 2018-01-27 stsp /* We do not support size values which don't fit in 64 bit. */
99 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_NO_SPACE);
101 885d3e02 2018-01-27 stsp if (i == 0)
102 885d3e02 2018-01-27 stsp *size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
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);
108 885d3e02 2018-01-27 stsp if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
111 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
112 885d3e02 2018-01-27 stsp } while (err == NULL);
114 885d3e02 2018-01-27 stsp return err;
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)
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;
125 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF1) {
126 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
128 885d3e02 2018-01-27 stsp return err;
129 885d3e02 2018-01-27 stsp o = (off_t)(**p);
131 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF2) {
132 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
134 885d3e02 2018-01-27 stsp return err;
135 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 8;
137 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF3) {
138 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
140 885d3e02 2018-01-27 stsp return err;
141 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 16;
143 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF4) {
144 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
146 885d3e02 2018-01-27 stsp return err;
147 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 24;
150 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN1) {
151 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
153 885d3e02 2018-01-27 stsp return err;
154 885d3e02 2018-01-27 stsp l = (off_t)(**p);
156 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN2) {
157 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
159 885d3e02 2018-01-27 stsp return err;
160 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 8;
162 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN3) {
163 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
165 885d3e02 2018-01-27 stsp return err;
166 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 16;
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;
174 885d3e02 2018-01-27 stsp *offset = o;
176 885d3e02 2018-01-27 stsp return NULL;
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)
182 885d3e02 2018-01-27 stsp if (fseeko(base_file, offset, SEEK_SET) != 0)
183 885d3e02 2018-01-27 stsp return got_error_from_errno();
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));
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);
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);
198 885d3e02 2018-01-27 stsp size -= len;
201 885d3e02 2018-01-27 stsp return NULL;
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)
209 885d3e02 2018-01-27 stsp if (*remain < len)
210 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
212 885d3e02 2018-01-27 stsp n = fwrite(*p, len, 1, outfile);
213 885d3e02 2018-01-27 stsp if (n != 1)
214 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
217 885d3e02 2018-01-27 stsp *remain -= len;
218 885d3e02 2018-01-27 stsp return NULL;
221 22484865 2018-03-13 stsp static const struct got_error *
222 22484865 2018-03-13 stsp parse_delta_sizes(uint64_t *base_size, uint64_t *result_size,
223 22484865 2018-03-13 stsp const uint8_t **p, size_t *remain)
225 22484865 2018-03-13 stsp const struct got_error *err;
227 22484865 2018-03-13 stsp /* Read the two size fields at the beginning of the stream. */
228 22484865 2018-03-13 stsp err = parse_size(base_size, p, remain);
230 22484865 2018-03-13 stsp return err;
231 22484865 2018-03-13 stsp err = next_delta_byte(p, remain);
233 22484865 2018-03-13 stsp return err;
234 22484865 2018-03-13 stsp err = parse_size(result_size, p, remain);
236 22484865 2018-03-13 stsp return err;
238 22484865 2018-03-13 stsp return NULL;
241 885d3e02 2018-01-27 stsp const struct got_error *
242 22484865 2018-03-13 stsp got_delta_get_sizes(uint64_t *base_size, uint64_t *result_size,
243 22484865 2018-03-13 stsp const uint8_t *delta_buf, size_t delta_len)
245 22484865 2018-03-13 stsp size_t remain;
246 22484865 2018-03-13 stsp const uint8_t *p;
248 22484865 2018-03-13 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
249 22484865 2018-03-13 stsp return got_error(GOT_ERR_BAD_DELTA);
251 22484865 2018-03-13 stsp p = delta_buf;
252 22484865 2018-03-13 stsp remain = delta_len;
253 22484865 2018-03-13 stsp return parse_delta_sizes(base_size, result_size, &p, &remain);
256 8628c62d 2018-03-15 stsp const struct got_error *
257 8628c62d 2018-03-15 stsp got_delta_apply_in_mem(uint8_t *base_buf, const uint8_t *delta_buf,
258 8628c62d 2018-03-15 stsp size_t delta_len, uint8_t *outbuf, size_t *outsize)
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;
265 8628c62d 2018-03-15 stsp *outsize= 0;
267 8628c62d 2018-03-15 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
268 8628c62d 2018-03-15 stsp return got_error(GOT_ERR_BAD_DELTA);
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);
274 8628c62d 2018-03-15 stsp return err;
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);
285 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, base_buf + offset, len);
286 8628c62d 2018-03-15 stsp if (err == NULL) {
287 8628c62d 2018-03-15 stsp *outsize += len;
288 8628c62d 2018-03-15 stsp if (remain > 0) {
294 8628c62d 2018-03-15 stsp size_t len = (size_t)*p;
295 8628c62d 2018-03-15 stsp if (len == 0) {
296 8628c62d 2018-03-15 stsp err = got_error(GOT_ERR_BAD_DELTA);
299 8628c62d 2018-03-15 stsp err = next_delta_byte(&p, &remain);
302 8628c62d 2018-03-15 stsp if (remain < len)
303 8628c62d 2018-03-15 stsp return got_error(GOT_ERR_BAD_DELTA);
304 8628c62d 2018-03-15 stsp memcpy(outbuf + *outsize, p, len);
306 8628c62d 2018-03-15 stsp remain -= len;
307 8628c62d 2018-03-15 stsp *outsize += len;
311 8628c62d 2018-03-15 stsp if (*outsize != result_size)
312 8628c62d 2018-03-15 stsp err = got_error(GOT_ERR_BAD_DELTA);
313 8628c62d 2018-03-15 stsp return err;
316 22484865 2018-03-13 stsp const struct got_error *
317 0e22967e 2018-02-11 stsp got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
318 885d3e02 2018-01-27 stsp size_t delta_len, FILE *outfile)
320 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
321 885d3e02 2018-01-27 stsp uint64_t base_size, result_size;
322 885d3e02 2018-01-27 stsp size_t remain, outsize = 0;
323 885d3e02 2018-01-27 stsp const uint8_t *p;
324 39ff877f 2018-03-13 stsp FILE *memstream = NULL;
325 39ff877f 2018-03-13 stsp char *memstream_buf = NULL;
326 39ff877f 2018-03-13 stsp size_t memstream_size = 0;
328 885d3e02 2018-01-27 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
329 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
331 885d3e02 2018-01-27 stsp p = delta_buf;
332 885d3e02 2018-01-27 stsp remain = delta_len;
333 22484865 2018-03-13 stsp err = parse_delta_sizes(&base_size, &result_size, &p, &remain);
335 885d3e02 2018-01-27 stsp return err;
337 39ff877f 2018-03-13 stsp if (result_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
338 39ff877f 2018-03-13 stsp memstream = open_memstream(&memstream_buf, &memstream_size);
340 885d3e02 2018-01-27 stsp /* Decode and execute copy instructions from the delta stream. */
341 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
342 06e5fc98 2018-02-11 stsp while (err == NULL && remain > 0) {
343 824801e7 2018-01-27 stsp if (*p & GOT_DELTA_BASE_COPY) {
344 885d3e02 2018-01-27 stsp off_t offset = 0;
345 885d3e02 2018-01-27 stsp size_t len = 0;
346 885d3e02 2018-01-27 stsp err = parse_opcode(&offset, &len, &p, &remain);
349 39ff877f 2018-03-13 stsp err = copy_from_base(base_file, offset, len,
350 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
351 06e5fc98 2018-02-11 stsp if (err == NULL) {
352 885d3e02 2018-01-27 stsp outsize += len;
353 06e5fc98 2018-02-11 stsp if (remain > 0) {
359 885d3e02 2018-01-27 stsp size_t len = (size_t)*p;
360 885d3e02 2018-01-27 stsp if (len == 0) {
361 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
364 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
367 39ff877f 2018-03-13 stsp err = copy_from_delta(&p, &remain, len,
368 39ff877f 2018-03-13 stsp memstream ? memstream : outfile);
369 885d3e02 2018-01-27 stsp if (err == NULL)
370 885d3e02 2018-01-27 stsp outsize += len;
374 885d3e02 2018-01-27 stsp if (outsize != result_size)
375 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
377 39ff877f 2018-03-13 stsp if (memstream != NULL) {
378 39ff877f 2018-03-13 stsp fclose(memstream);
379 39ff877f 2018-03-13 stsp if (err == NULL) {
381 39ff877f 2018-03-13 stsp n = fwrite(memstream_buf, 1, memstream_size, outfile);
382 39ff877f 2018-03-13 stsp if (n != memstream_size)
383 39ff877f 2018-03-13 stsp err = got_ferror(outfile, GOT_ERR_IO);
385 39ff877f 2018-03-13 stsp free(memstream_buf);
387 885d3e02 2018-01-27 stsp if (err == NULL)
388 885d3e02 2018-01-27 stsp rewind(outfile);
389 885d3e02 2018-01-27 stsp return err;