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->path_packfile = strdup(path_packfile);
48 c3703302 2018-01-23 stsp if (delta->path_packfile == NULL) {
49 c3703302 2018-01-23 stsp free(delta);
50 96f5e8b3 2018-01-23 stsp return NULL;
52 c3703302 2018-01-23 stsp delta->type = type;
53 c3703302 2018-01-23 stsp delta->offset = offset;
54 0e22967e 2018-02-11 stsp delta->tslen = tslen;
55 c3703302 2018-01-23 stsp delta->size = size;
56 bdd2fbb3 2018-02-11 stsp delta->data_offset = data_offset;
57 c3703302 2018-01-23 stsp return delta;
61 c3703302 2018-01-23 stsp got_delta_close(struct got_delta *delta)
63 c3703302 2018-01-23 stsp free(delta->path_packfile);
64 c3703302 2018-01-23 stsp free(delta);
68 efd2a263 2018-01-19 stsp const struct got_error *
69 96f5e8b3 2018-01-23 stsp got_delta_chain_get_base_type(int *type, struct got_delta_chain *deltas)
71 c3703302 2018-01-23 stsp struct got_delta *delta;
73 6691714a 2018-01-23 stsp /* The first delta in the chain should represent the base object. */
74 6691714a 2018-01-23 stsp delta = SIMPLEQ_FIRST(&deltas->entries);
75 6691714a 2018-01-23 stsp if (delta->type == GOT_OBJ_TYPE_COMMIT ||
76 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TREE ||
77 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_BLOB ||
78 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TAG) {
79 6691714a 2018-01-23 stsp *type = delta->type;
80 6691714a 2018-01-23 stsp return NULL;
83 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
86 885d3e02 2018-01-27 stsp /* Fetch another (required) byte from the delta stream. */
87 885d3e02 2018-01-27 stsp static const struct got_error *
88 885d3e02 2018-01-27 stsp next_delta_byte(const uint8_t **p, size_t *remain)
90 885d3e02 2018-01-27 stsp if (--(*remain) == 0)
91 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
93 885d3e02 2018-01-27 stsp return NULL;
96 885d3e02 2018-01-27 stsp static const struct got_error *
97 885d3e02 2018-01-27 stsp parse_size(uint64_t *size, const uint8_t **p, size_t *remain)
99 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
104 885d3e02 2018-01-27 stsp /* We do not support size values which don't fit in 64 bit. */
106 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_NO_SPACE);
108 885d3e02 2018-01-27 stsp if (i == 0)
109 885d3e02 2018-01-27 stsp *size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
111 885d3e02 2018-01-27 stsp size_t shift = GOT_DELTA_SIZE_SHIFT * i;
112 885d3e02 2018-01-27 stsp *size |= (((**p) & GOT_DELTA_SIZE_VAL_MASK) << shift);
115 885d3e02 2018-01-27 stsp if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
118 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
119 885d3e02 2018-01-27 stsp } while (err == NULL);
121 885d3e02 2018-01-27 stsp return err;
124 885d3e02 2018-01-27 stsp static const struct got_error *
125 885d3e02 2018-01-27 stsp parse_opcode(off_t *offset, size_t *len, const uint8_t **p, size_t *remain)
127 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
128 885d3e02 2018-01-27 stsp off_t o = 0;
129 885d3e02 2018-01-27 stsp size_t l = 0;
130 885d3e02 2018-01-27 stsp uint8_t opcode = **p;
132 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF1) {
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);
138 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF2) {
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)) << 8;
144 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF3) {
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)) << 16;
150 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF4) {
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 o |= ((off_t)(**p)) << 24;
157 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN1) {
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);
163 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN2) {
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)) << 8;
169 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN3) {
170 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
172 885d3e02 2018-01-27 stsp return err;
173 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 16;
176 885d3e02 2018-01-27 stsp if (o == 0)
177 885d3e02 2018-01-27 stsp o = GOT_DELTA_COPY_DEFAULT_OFF;
178 885d3e02 2018-01-27 stsp if (l == 0)
179 885d3e02 2018-01-27 stsp l = GOT_DELTA_COPY_DEFAULT_LEN;
181 885d3e02 2018-01-27 stsp *offset = o;
183 885d3e02 2018-01-27 stsp return NULL;
186 885d3e02 2018-01-27 stsp static const struct got_error *
187 885d3e02 2018-01-27 stsp copy_from_base(FILE *base_file, off_t offset, size_t size, FILE *outfile)
189 885d3e02 2018-01-27 stsp if (fseeko(base_file, offset, SEEK_SET) != 0)
190 885d3e02 2018-01-27 stsp return got_error_from_errno();
192 885d3e02 2018-01-27 stsp while (size > 0) {
193 885d3e02 2018-01-27 stsp uint8_t data[2048];
194 885d3e02 2018-01-27 stsp size_t len = MIN(size, sizeof(data));
197 885d3e02 2018-01-27 stsp n = fread(data, len, 1, base_file);
198 885d3e02 2018-01-27 stsp if (n != 1)
199 885d3e02 2018-01-27 stsp return got_ferror(base_file, GOT_ERR_IO);
201 885d3e02 2018-01-27 stsp n = fwrite(data, len, 1, outfile);
202 885d3e02 2018-01-27 stsp if (n != 1)
203 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
205 885d3e02 2018-01-27 stsp size -= len;
208 885d3e02 2018-01-27 stsp return NULL;
211 885d3e02 2018-01-27 stsp static const struct got_error *
212 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 if (*remain < len)
217 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
219 885d3e02 2018-01-27 stsp n = fwrite(*p, len, 1, outfile);
220 885d3e02 2018-01-27 stsp if (n != 1)
221 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
224 885d3e02 2018-01-27 stsp *remain -= len;
225 885d3e02 2018-01-27 stsp return NULL;
228 885d3e02 2018-01-27 stsp const struct got_error *
229 0e22967e 2018-02-11 stsp got_delta_apply(FILE *base_file, const uint8_t *delta_buf,
230 885d3e02 2018-01-27 stsp size_t delta_len, FILE *outfile)
232 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
233 885d3e02 2018-01-27 stsp uint64_t base_size, result_size;
234 885d3e02 2018-01-27 stsp size_t remain, outsize = 0;
235 885d3e02 2018-01-27 stsp const uint8_t *p;
237 885d3e02 2018-01-27 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
238 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
240 885d3e02 2018-01-27 stsp p = delta_buf;
241 885d3e02 2018-01-27 stsp remain = delta_len;
243 885d3e02 2018-01-27 stsp /* Read the two size fields at the beginning of the stream. */
244 885d3e02 2018-01-27 stsp err = parse_size(&base_size, &p, &remain);
246 885d3e02 2018-01-27 stsp return err;
247 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
249 885d3e02 2018-01-27 stsp return err;
250 885d3e02 2018-01-27 stsp err = parse_size(&result_size, &p, &remain);
252 885d3e02 2018-01-27 stsp return err;
254 885d3e02 2018-01-27 stsp /* Decode and execute copy instructions from the delta stream. */
255 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
256 06e5fc98 2018-02-11 stsp while (err == NULL && remain > 0) {
257 824801e7 2018-01-27 stsp if (*p & GOT_DELTA_BASE_COPY) {
258 885d3e02 2018-01-27 stsp off_t offset = 0;
259 885d3e02 2018-01-27 stsp size_t len = 0;
260 885d3e02 2018-01-27 stsp err = parse_opcode(&offset, &len, &p, &remain);
263 885d3e02 2018-01-27 stsp err = copy_from_base(base_file, offset, len, outfile);
264 06e5fc98 2018-02-11 stsp if (err == NULL) {
265 885d3e02 2018-01-27 stsp outsize += len;
266 06e5fc98 2018-02-11 stsp if (remain > 0) {
272 885d3e02 2018-01-27 stsp size_t len = (size_t)*p;
273 885d3e02 2018-01-27 stsp if (len == 0) {
274 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
277 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
280 885d3e02 2018-01-27 stsp err = copy_from_delta(&p, &remain, len, outfile);
281 885d3e02 2018-01-27 stsp if (err == NULL)
282 885d3e02 2018-01-27 stsp outsize += len;
286 885d3e02 2018-01-27 stsp if (outsize != result_size)
287 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
289 885d3e02 2018-01-27 stsp if (err == NULL)
290 885d3e02 2018-01-27 stsp rewind(outfile);
291 885d3e02 2018-01-27 stsp return err;