Blame


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