Blame


1 32cb896c 2018-03-11 stsp /*
2 32cb896c 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 32cb896c 2018-03-11 stsp *
4 32cb896c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 32cb896c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 32cb896c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
7 32cb896c 2018-03-11 stsp *
8 32cb896c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 32cb896c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 32cb896c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 32cb896c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 32cb896c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 32cb896c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 32cb896c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 32cb896c 2018-03-11 stsp */
16 32cb896c 2018-03-11 stsp
17 32cb896c 2018-03-11 stsp struct got_delta {
18 32cb896c 2018-03-11 stsp SIMPLEQ_ENTRY(got_delta) entry;
19 32cb896c 2018-03-11 stsp char *path_packfile;
20 32cb896c 2018-03-11 stsp off_t offset;
21 32cb896c 2018-03-11 stsp size_t tslen;
22 32cb896c 2018-03-11 stsp int type;
23 32cb896c 2018-03-11 stsp size_t size;
24 32cb896c 2018-03-11 stsp off_t data_offset;
25 32cb896c 2018-03-11 stsp };
26 32cb896c 2018-03-11 stsp
27 32cb896c 2018-03-11 stsp struct got_delta_chain {
28 32cb896c 2018-03-11 stsp int nentries;
29 32cb896c 2018-03-11 stsp SIMPLEQ_HEAD(, got_delta) entries;
30 32cb896c 2018-03-11 stsp };
31 32cb896c 2018-03-11 stsp
32 32cb896c 2018-03-11 stsp struct got_delta *got_delta_open(const char *, off_t, size_t, int, size_t,
33 32cb896c 2018-03-11 stsp off_t);
34 32cb896c 2018-03-11 stsp void got_delta_close(struct got_delta *);
35 32cb896c 2018-03-11 stsp const struct got_error *got_delta_chain_get_base_type(int *,
36 32cb896c 2018-03-11 stsp struct got_delta_chain *);
37 32cb896c 2018-03-11 stsp const struct got_error *got_delta_apply(FILE *, const uint8_t *, size_t,
38 32cb896c 2018-03-11 stsp FILE *);
39 32cb896c 2018-03-11 stsp
40 32cb896c 2018-03-11 stsp /*
41 32cb896c 2018-03-11 stsp * Definitions for delta data streams.
42 32cb896c 2018-03-11 stsp */
43 32cb896c 2018-03-11 stsp
44 32cb896c 2018-03-11 stsp #define GOT_DELTA_STREAM_LENGTH_MIN 4 /* bytes */
45 32cb896c 2018-03-11 stsp
46 32cb896c 2018-03-11 stsp /*
47 32cb896c 2018-03-11 stsp * A delta stream begins with two size fields. The first specifies the
48 32cb896c 2018-03-11 stsp * size of the delta base, and the second describes the expected size of
49 32cb896c 2018-03-11 stsp * the data which results from applying the delta to the delta base.
50 32cb896c 2018-03-11 stsp *
51 32cb896c 2018-03-11 stsp * Each size field uses a variable length encoding:
52 32cb896c 2018-03-11 stsp * size0...sizeN form a 7+7+7+...+7 bit integer, where size0 is the
53 32cb896c 2018-03-11 stsp * least significant part and sizeN is the most significant part.
54 32cb896c 2018-03-11 stsp * If the MSB of a size byte is set, an additional size byte follows.
55 32cb896c 2018-03-11 stsp */
56 32cb896c 2018-03-11 stsp #define GOT_DELTA_SIZE_VAL_MASK 0x7f
57 32cb896c 2018-03-11 stsp #define GOT_DELTA_SIZE_SHIFT 7
58 32cb896c 2018-03-11 stsp #define GOT_DELTA_SIZE_MORE 0x80
59 32cb896c 2018-03-11 stsp
60 32cb896c 2018-03-11 stsp /*
61 32cb896c 2018-03-11 stsp * The rest of the delta stream contains copy instructions.
62 32cb896c 2018-03-11 stsp *
63 32cb896c 2018-03-11 stsp * A base copy instruction copies N bytes starting at offset X from the delta
64 32cb896c 2018-03-11 stsp * base to the output. Base copy instructions begin with a byte which has its
65 32cb896c 2018-03-11 stsp * MSB set. The remaining bits of this byte describe how many offset and
66 32cb896c 2018-03-11 stsp * length value bytes follow.
67 32cb896c 2018-03-11 stsp * The offset X is encoded in 1 to 4 bytes, and the length N is encoded in
68 32cb896c 2018-03-11 stsp * 1 to 3 bytes. For both values, the first byte contributes the least
69 32cb896c 2018-03-11 stsp * significant part and the last byte which is present contributes the
70 32cb896c 2018-03-11 stsp * most significant part.
71 32cb896c 2018-03-11 stsp * If the offset value is omitted, an offset of zero is implied.
72 32cb896c 2018-03-11 stsp * If the length value is omitted, a default length of 65536 bytes is implied.
73 32cb896c 2018-03-11 stsp *
74 32cb896c 2018-03-11 stsp * An inline copy instruction copies data from the delta stream to the output.
75 32cb896c 2018-03-11 stsp * Such instructions begin with one byte which does not have the MSB set
76 32cb896c 2018-03-11 stsp * and which specifies the length of the inline data which follows (i.e.
77 32cb896c 2018-03-11 stsp * at most 127 bytes). A length value of zero is invalid.
78 32cb896c 2018-03-11 stsp */
79 32cb896c 2018-03-11 stsp
80 32cb896c 2018-03-11 stsp #define GOT_DELTA_BASE_COPY 0x80
81 32cb896c 2018-03-11 stsp
82 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_OFF1 0x01 /* byte 1 of offset is present */
83 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_OFF2 0x02 /* byte 2 of offset is present */
84 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_OFF3 0x04 /* byte 3 of offset is present */
85 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_OFF4 0x08 /* byte 4 of offset is present */
86 32cb896c 2018-03-11 stsp
87 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_LEN1 0x10 /* byte 1 of length is present */
88 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_LEN2 0x20 /* byte 2 of length is present */
89 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_LEN3 0x40 /* byte 3 of length is present */
90 32cb896c 2018-03-11 stsp
91 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_DEFAULT_OFF 0x0 /* default offset if omitted */
92 32cb896c 2018-03-11 stsp #define GOT_DELTA_COPY_DEFAULT_LEN 0x10000 /* default length if omitted */