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 efd2a263 2018-01-19 stsp #include "delta.h"
30 3606d7fc 2018-02-11 stsp #include "path.h"
31 885d3e02 2018-01-27 stsp #include "zb.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 c3703302 2018-01-23 stsp got_delta_open(const char *path_packfile, int type, off_t offset,
39 5a2e13f7 2018-01-23 stsp size_t size)
40 96f5e8b3 2018-01-23 stsp {
41 c3703302 2018-01-23 stsp struct got_delta *delta;
42 96f5e8b3 2018-01-23 stsp
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;
46 96f5e8b3 2018-01-23 stsp
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;
51 96f5e8b3 2018-01-23 stsp }
52 c3703302 2018-01-23 stsp delta->type = type;
53 c3703302 2018-01-23 stsp delta->offset = offset;
54 c3703302 2018-01-23 stsp delta->size = size;
55 c3703302 2018-01-23 stsp return delta;
56 96f5e8b3 2018-01-23 stsp }
57 96f5e8b3 2018-01-23 stsp
58 96f5e8b3 2018-01-23 stsp void
59 c3703302 2018-01-23 stsp got_delta_close(struct got_delta *delta)
60 96f5e8b3 2018-01-23 stsp {
61 c3703302 2018-01-23 stsp free(delta->path_packfile);
62 c3703302 2018-01-23 stsp free(delta);
63 96f5e8b3 2018-01-23 stsp
64 96f5e8b3 2018-01-23 stsp }
65 96f5e8b3 2018-01-23 stsp
66 efd2a263 2018-01-19 stsp const struct got_error *
67 96f5e8b3 2018-01-23 stsp got_delta_chain_get_base_type(int *type, struct got_delta_chain *deltas)
68 96f5e8b3 2018-01-23 stsp {
69 c3703302 2018-01-23 stsp struct got_delta *delta;
70 96f5e8b3 2018-01-23 stsp
71 6691714a 2018-01-23 stsp /* The first delta in the chain should represent the base object. */
72 6691714a 2018-01-23 stsp delta = SIMPLEQ_FIRST(&deltas->entries);
73 6691714a 2018-01-23 stsp if (delta->type == GOT_OBJ_TYPE_COMMIT ||
74 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TREE ||
75 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_BLOB ||
76 6691714a 2018-01-23 stsp delta->type == GOT_OBJ_TYPE_TAG) {
77 6691714a 2018-01-23 stsp *type = delta->type;
78 6691714a 2018-01-23 stsp return NULL;
79 96f5e8b3 2018-01-23 stsp }
80 96f5e8b3 2018-01-23 stsp
81 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
82 96f5e8b3 2018-01-23 stsp }
83 96f5e8b3 2018-01-23 stsp
84 885d3e02 2018-01-27 stsp /* Fetch another (required) byte from the delta stream. */
85 885d3e02 2018-01-27 stsp static const struct got_error *
86 885d3e02 2018-01-27 stsp next_delta_byte(const uint8_t **p, size_t *remain)
87 efd2a263 2018-01-19 stsp {
88 885d3e02 2018-01-27 stsp if (--(*remain) == 0)
89 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
90 885d3e02 2018-01-27 stsp (*p)++;
91 885d3e02 2018-01-27 stsp return NULL;
92 efd2a263 2018-01-19 stsp }
93 885d3e02 2018-01-27 stsp
94 885d3e02 2018-01-27 stsp static const struct got_error *
95 885d3e02 2018-01-27 stsp parse_size(uint64_t *size, const uint8_t **p, size_t *remain)
96 885d3e02 2018-01-27 stsp {
97 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
98 885d3e02 2018-01-27 stsp int i = 0;
99 885d3e02 2018-01-27 stsp
100 885d3e02 2018-01-27 stsp *size = 0;
101 885d3e02 2018-01-27 stsp do {
102 885d3e02 2018-01-27 stsp /* We do not support size values which don't fit in 64 bit. */
103 885d3e02 2018-01-27 stsp if (i > 9)
104 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_NO_SPACE);
105 885d3e02 2018-01-27 stsp
106 885d3e02 2018-01-27 stsp if (i == 0)
107 885d3e02 2018-01-27 stsp *size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
108 885d3e02 2018-01-27 stsp else {
109 885d3e02 2018-01-27 stsp size_t shift = GOT_DELTA_SIZE_SHIFT * i;
110 885d3e02 2018-01-27 stsp *size |= (((**p) & GOT_DELTA_SIZE_VAL_MASK) << shift);
111 885d3e02 2018-01-27 stsp }
112 885d3e02 2018-01-27 stsp
113 885d3e02 2018-01-27 stsp if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
114 885d3e02 2018-01-27 stsp break;
115 885d3e02 2018-01-27 stsp i++;
116 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
117 885d3e02 2018-01-27 stsp } while (err == NULL);
118 885d3e02 2018-01-27 stsp
119 885d3e02 2018-01-27 stsp return err;
120 885d3e02 2018-01-27 stsp }
121 885d3e02 2018-01-27 stsp
122 885d3e02 2018-01-27 stsp static const struct got_error *
123 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 {
125 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
126 885d3e02 2018-01-27 stsp off_t o = 0;
127 885d3e02 2018-01-27 stsp size_t l = 0;
128 885d3e02 2018-01-27 stsp uint8_t opcode = **p;
129 885d3e02 2018-01-27 stsp
130 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF1) {
131 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
132 885d3e02 2018-01-27 stsp if (err)
133 885d3e02 2018-01-27 stsp return err;
134 885d3e02 2018-01-27 stsp o = (off_t)(**p);
135 885d3e02 2018-01-27 stsp }
136 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF2) {
137 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
138 885d3e02 2018-01-27 stsp if (err)
139 885d3e02 2018-01-27 stsp return err;
140 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 8;
141 885d3e02 2018-01-27 stsp }
142 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF3) {
143 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
144 885d3e02 2018-01-27 stsp if (err)
145 885d3e02 2018-01-27 stsp return err;
146 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 16;
147 885d3e02 2018-01-27 stsp }
148 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_OFF4) {
149 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
150 885d3e02 2018-01-27 stsp if (err)
151 885d3e02 2018-01-27 stsp return err;
152 885d3e02 2018-01-27 stsp o |= ((off_t)(**p)) << 24;
153 885d3e02 2018-01-27 stsp }
154 885d3e02 2018-01-27 stsp
155 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN1) {
156 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
157 885d3e02 2018-01-27 stsp if (err)
158 885d3e02 2018-01-27 stsp return err;
159 885d3e02 2018-01-27 stsp l = (off_t)(**p);
160 885d3e02 2018-01-27 stsp }
161 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN2) {
162 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
163 885d3e02 2018-01-27 stsp if (err)
164 885d3e02 2018-01-27 stsp return err;
165 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 8;
166 885d3e02 2018-01-27 stsp }
167 885d3e02 2018-01-27 stsp if (opcode & GOT_DELTA_COPY_LEN3) {
168 885d3e02 2018-01-27 stsp err = next_delta_byte(p, remain);
169 885d3e02 2018-01-27 stsp if (err)
170 885d3e02 2018-01-27 stsp return err;
171 885d3e02 2018-01-27 stsp l |= ((off_t)(**p)) << 16;
172 885d3e02 2018-01-27 stsp }
173 885d3e02 2018-01-27 stsp
174 885d3e02 2018-01-27 stsp if (o == 0)
175 885d3e02 2018-01-27 stsp o = GOT_DELTA_COPY_DEFAULT_OFF;
176 885d3e02 2018-01-27 stsp if (l == 0)
177 885d3e02 2018-01-27 stsp l = GOT_DELTA_COPY_DEFAULT_LEN;
178 885d3e02 2018-01-27 stsp
179 885d3e02 2018-01-27 stsp *offset = o;
180 885d3e02 2018-01-27 stsp *len = l;
181 885d3e02 2018-01-27 stsp return NULL;
182 885d3e02 2018-01-27 stsp }
183 885d3e02 2018-01-27 stsp
184 885d3e02 2018-01-27 stsp static const struct got_error *
185 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 {
187 885d3e02 2018-01-27 stsp if (fseeko(base_file, offset, SEEK_SET) != 0)
188 885d3e02 2018-01-27 stsp return got_error_from_errno();
189 885d3e02 2018-01-27 stsp
190 885d3e02 2018-01-27 stsp while (size > 0) {
191 885d3e02 2018-01-27 stsp uint8_t data[2048];
192 885d3e02 2018-01-27 stsp size_t len = MIN(size, sizeof(data));
193 885d3e02 2018-01-27 stsp size_t n;
194 885d3e02 2018-01-27 stsp
195 885d3e02 2018-01-27 stsp n = fread(data, len, 1, base_file);
196 885d3e02 2018-01-27 stsp if (n != 1)
197 885d3e02 2018-01-27 stsp return got_ferror(base_file, GOT_ERR_IO);
198 885d3e02 2018-01-27 stsp
199 885d3e02 2018-01-27 stsp n = fwrite(data, len, 1, outfile);
200 885d3e02 2018-01-27 stsp if (n != 1)
201 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
202 885d3e02 2018-01-27 stsp
203 885d3e02 2018-01-27 stsp size -= len;
204 885d3e02 2018-01-27 stsp }
205 885d3e02 2018-01-27 stsp
206 885d3e02 2018-01-27 stsp return NULL;
207 885d3e02 2018-01-27 stsp }
208 885d3e02 2018-01-27 stsp
209 885d3e02 2018-01-27 stsp static const struct got_error *
210 885d3e02 2018-01-27 stsp copy_from_delta(const uint8_t **p, size_t *remain, size_t len, FILE *outfile)
211 885d3e02 2018-01-27 stsp {
212 885d3e02 2018-01-27 stsp size_t n;
213 885d3e02 2018-01-27 stsp
214 885d3e02 2018-01-27 stsp if (*remain < len)
215 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
216 885d3e02 2018-01-27 stsp
217 885d3e02 2018-01-27 stsp n = fwrite(*p, len, 1, outfile);
218 885d3e02 2018-01-27 stsp if (n != 1)
219 885d3e02 2018-01-27 stsp return got_ferror(outfile, GOT_ERR_IO);
220 885d3e02 2018-01-27 stsp
221 885d3e02 2018-01-27 stsp *p += len;
222 885d3e02 2018-01-27 stsp *remain -= len;
223 885d3e02 2018-01-27 stsp return NULL;
224 885d3e02 2018-01-27 stsp }
225 885d3e02 2018-01-27 stsp
226 885d3e02 2018-01-27 stsp const struct got_error *
227 885d3e02 2018-01-27 stsp got_delta_apply(FILE *base_compressed, const uint8_t *delta_buf,
228 885d3e02 2018-01-27 stsp size_t delta_len, FILE *outfile)
229 885d3e02 2018-01-27 stsp {
230 885d3e02 2018-01-27 stsp const struct got_error *err = NULL;
231 885d3e02 2018-01-27 stsp FILE *base_file = NULL;
232 885d3e02 2018-01-27 stsp uint64_t base_size, result_size;
233 885d3e02 2018-01-27 stsp size_t remain, outsize = 0;
234 885d3e02 2018-01-27 stsp const uint8_t *p;
235 885d3e02 2018-01-27 stsp
236 885d3e02 2018-01-27 stsp if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
237 885d3e02 2018-01-27 stsp return got_error(GOT_ERR_BAD_DELTA);
238 885d3e02 2018-01-27 stsp
239 885d3e02 2018-01-27 stsp p = delta_buf;
240 885d3e02 2018-01-27 stsp remain = delta_len;
241 885d3e02 2018-01-27 stsp
242 885d3e02 2018-01-27 stsp /* Read the two size fields at the beginning of the stream. */
243 885d3e02 2018-01-27 stsp err = parse_size(&base_size, &p, &remain);
244 885d3e02 2018-01-27 stsp if (err)
245 885d3e02 2018-01-27 stsp return err;
246 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
247 885d3e02 2018-01-27 stsp if (err)
248 885d3e02 2018-01-27 stsp return err;
249 885d3e02 2018-01-27 stsp err = parse_size(&result_size, &p, &remain);
250 885d3e02 2018-01-27 stsp if (err)
251 885d3e02 2018-01-27 stsp return err;
252 885d3e02 2018-01-27 stsp
253 885d3e02 2018-01-27 stsp /* Decode and execute copy instructions from the delta stream. */
254 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
255 885d3e02 2018-01-27 stsp while (err == NULL) {
256 824801e7 2018-01-27 stsp if (*p & GOT_DELTA_BASE_COPY) {
257 885d3e02 2018-01-27 stsp off_t offset = 0;
258 885d3e02 2018-01-27 stsp size_t len = 0;
259 885d3e02 2018-01-27 stsp err = parse_opcode(&offset, &len, &p, &remain);
260 885d3e02 2018-01-27 stsp if (err)
261 885d3e02 2018-01-27 stsp break;
262 885d3e02 2018-01-27 stsp if (base_file == NULL) {
263 885d3e02 2018-01-27 stsp size_t inflated_size;
264 3606d7fc 2018-02-11 stsp base_file = got_opentemp();
265 3606d7fc 2018-02-11 stsp if (base_file == NULL) {
266 3606d7fc 2018-02-11 stsp err = got_error_from_errno();
267 3606d7fc 2018-02-11 stsp break;
268 3606d7fc 2018-02-11 stsp }
269 3606d7fc 2018-02-11 stsp err = got_inflate_to_file(&inflated_size,
270 3606d7fc 2018-02-11 stsp base_compressed, base_file);
271 885d3e02 2018-01-27 stsp if (err)
272 885d3e02 2018-01-27 stsp break;
273 885d3e02 2018-01-27 stsp if (inflated_size != base_size) {
274 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
275 885d3e02 2018-01-27 stsp break;
276 885d3e02 2018-01-27 stsp }
277 885d3e02 2018-01-27 stsp }
278 885d3e02 2018-01-27 stsp err = copy_from_base(base_file, offset, len, outfile);
279 885d3e02 2018-01-27 stsp if (err == NULL)
280 885d3e02 2018-01-27 stsp outsize += len;
281 885d3e02 2018-01-27 stsp } else {
282 885d3e02 2018-01-27 stsp size_t len = (size_t)*p;
283 885d3e02 2018-01-27 stsp if (len == 0) {
284 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
285 885d3e02 2018-01-27 stsp break;
286 885d3e02 2018-01-27 stsp }
287 885d3e02 2018-01-27 stsp err = next_delta_byte(&p, &remain);
288 885d3e02 2018-01-27 stsp if (err)
289 885d3e02 2018-01-27 stsp return err;
290 885d3e02 2018-01-27 stsp err = copy_from_delta(&p, &remain, len, outfile);
291 885d3e02 2018-01-27 stsp if (err == NULL)
292 885d3e02 2018-01-27 stsp outsize += len;
293 885d3e02 2018-01-27 stsp }
294 885d3e02 2018-01-27 stsp
295 885d3e02 2018-01-27 stsp if (err == NULL) {
296 885d3e02 2018-01-27 stsp if (remain == 0)
297 885d3e02 2018-01-27 stsp break;
298 885d3e02 2018-01-27 stsp /* Fetch the next instruction. */
299 885d3e02 2018-01-27 stsp p++;
300 885d3e02 2018-01-27 stsp remain--;
301 885d3e02 2018-01-27 stsp }
302 885d3e02 2018-01-27 stsp }
303 885d3e02 2018-01-27 stsp
304 885d3e02 2018-01-27 stsp if (outsize != result_size)
305 885d3e02 2018-01-27 stsp err = got_error(GOT_ERR_BAD_DELTA);
306 885d3e02 2018-01-27 stsp
307 885d3e02 2018-01-27 stsp if (base_file)
308 885d3e02 2018-01-27 stsp fclose(base_file);
309 885d3e02 2018-01-27 stsp if (err == NULL)
310 885d3e02 2018-01-27 stsp rewind(outfile);
311 885d3e02 2018-01-27 stsp return err;
312 885d3e02 2018-01-27 stsp }