2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
25 #include "got_error.h"
26 #include "got_repository.h"
27 #include "got_object.h"
33 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 got_delta_open(const char *path_packfile, int type, off_t offset,
40 struct got_delta *delta;
42 delta = calloc(1, sizeof(*delta));
46 delta->path_packfile = strdup(path_packfile);
47 if (delta->path_packfile == NULL) {
52 delta->offset = offset;
58 got_delta_close(struct got_delta *delta)
60 free(delta->path_packfile);
65 const struct got_error *
66 got_delta_chain_get_base_type(int *type, struct got_delta_chain *deltas)
68 struct got_delta *delta;
70 /* The first delta in the chain should represent the base object. */
71 delta = SIMPLEQ_FIRST(&deltas->entries);
72 if (delta->type == GOT_OBJ_TYPE_COMMIT ||
73 delta->type == GOT_OBJ_TYPE_TREE ||
74 delta->type == GOT_OBJ_TYPE_BLOB ||
75 delta->type == GOT_OBJ_TYPE_TAG) {
80 return got_error(GOT_ERR_BAD_DELTA_CHAIN);
83 /* Fetch another (required) byte from the delta stream. */
84 static const struct got_error *
85 next_delta_byte(const uint8_t **p, size_t *remain)
88 return got_error(GOT_ERR_BAD_DELTA);
93 static const struct got_error *
94 parse_size(uint64_t *size, const uint8_t **p, size_t *remain)
96 const struct got_error *err = NULL;
101 /* We do not support size values which don't fit in 64 bit. */
103 return got_error(GOT_ERR_NO_SPACE);
106 *size = ((**p) & GOT_DELTA_SIZE_VAL_MASK);
108 size_t shift = GOT_DELTA_SIZE_SHIFT * i;
109 *size |= (((**p) & GOT_DELTA_SIZE_VAL_MASK) << shift);
112 if (((**p) & GOT_DELTA_SIZE_MORE) == 0)
115 err = next_delta_byte(p, remain);
116 } while (err == NULL);
121 static const struct got_error *
122 parse_opcode(off_t *offset, size_t *len, const uint8_t **p, size_t *remain)
124 const struct got_error *err = NULL;
127 uint8_t opcode = **p;
129 if (opcode & GOT_DELTA_COPY_OFF1) {
130 err = next_delta_byte(p, remain);
135 if (opcode & GOT_DELTA_COPY_OFF2) {
136 err = next_delta_byte(p, remain);
139 o |= ((off_t)(**p)) << 8;
141 if (opcode & GOT_DELTA_COPY_OFF3) {
142 err = next_delta_byte(p, remain);
145 o |= ((off_t)(**p)) << 16;
147 if (opcode & GOT_DELTA_COPY_OFF4) {
148 err = next_delta_byte(p, remain);
151 o |= ((off_t)(**p)) << 24;
154 if (opcode & GOT_DELTA_COPY_LEN1) {
155 err = next_delta_byte(p, remain);
160 if (opcode & GOT_DELTA_COPY_LEN2) {
161 err = next_delta_byte(p, remain);
164 l |= ((off_t)(**p)) << 8;
166 if (opcode & GOT_DELTA_COPY_LEN3) {
167 err = next_delta_byte(p, remain);
170 l |= ((off_t)(**p)) << 16;
174 o = GOT_DELTA_COPY_DEFAULT_OFF;
176 l = GOT_DELTA_COPY_DEFAULT_LEN;
183 static const struct got_error *
184 copy_from_base(FILE *base_file, off_t offset, size_t size, FILE *outfile)
186 if (fseeko(base_file, offset, SEEK_SET) != 0)
187 return got_error_from_errno();
191 size_t len = MIN(size, sizeof(data));
194 n = fread(data, len, 1, base_file);
196 return got_ferror(base_file, GOT_ERR_IO);
198 n = fwrite(data, len, 1, outfile);
200 return got_ferror(outfile, GOT_ERR_IO);
208 static const struct got_error *
209 copy_from_delta(const uint8_t **p, size_t *remain, size_t len, FILE *outfile)
214 return got_error(GOT_ERR_BAD_DELTA);
216 n = fwrite(*p, len, 1, outfile);
218 return got_ferror(outfile, GOT_ERR_IO);
225 const struct got_error *
226 got_delta_apply(FILE *base_compressed, const uint8_t *delta_buf,
227 size_t delta_len, FILE *outfile)
229 const struct got_error *err = NULL;
230 FILE *base_file = NULL;
231 uint64_t base_size, result_size;
232 size_t remain, outsize = 0;
235 if (delta_len < GOT_DELTA_STREAM_LENGTH_MIN)
236 return got_error(GOT_ERR_BAD_DELTA);
241 /* Read the two size fields at the beginning of the stream. */
242 err = parse_size(&base_size, &p, &remain);
245 err = next_delta_byte(&p, &remain);
248 err = parse_size(&result_size, &p, &remain);
252 /* Decode and execute copy instructions from the delta stream. */
253 err = next_delta_byte(&p, &remain);
254 while (err == NULL) {
255 if (*p & GOT_DELTA_BASE_COPY) {
258 err = parse_opcode(&offset, &len, &p, &remain);
261 if (base_file == NULL) {
262 size_t inflated_size;
263 err = got_inflate_to_tempfile(&base_file,
264 &inflated_size, base_compressed);
267 if (inflated_size != base_size) {
268 err = got_error(GOT_ERR_BAD_DELTA);
272 err = copy_from_base(base_file, offset, len, outfile);
276 size_t len = (size_t)*p;
278 err = got_error(GOT_ERR_BAD_DELTA);
281 err = next_delta_byte(&p, &remain);
284 err = copy_from_delta(&p, &remain, len, outfile);
292 /* Fetch the next instruction. */
298 if (outsize != result_size)
299 err = got_error(GOT_ERR_BAD_DELTA);