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 c3703302 2018-01-23 stsp struct got_delta {
18 c3703302 2018-01-23 stsp SIMPLEQ_ENTRY(got_delta) entry;
19 96f5e8b3 2018-01-23 stsp char *path_packfile;
20 96f5e8b3 2018-01-23 stsp off_t offset;
21 96f5e8b3 2018-01-23 stsp int type;
22 5a2e13f7 2018-01-23 stsp size_t size;
23 96f5e8b3 2018-01-23 stsp };
24 96f5e8b3 2018-01-23 stsp
25 96f5e8b3 2018-01-23 stsp struct got_delta_chain {
26 96f5e8b3 2018-01-23 stsp int nentries;
27 c3703302 2018-01-23 stsp SIMPLEQ_HEAD(, got_delta) entries;
28 96f5e8b3 2018-01-23 stsp };
29 96f5e8b3 2018-01-23 stsp
30 c3703302 2018-01-23 stsp struct got_delta *got_delta_open(const char *, int, off_t, size_t);
31 c3703302 2018-01-23 stsp void got_delta_close(struct got_delta *);
32 96f5e8b3 2018-01-23 stsp const struct got_error *got_delta_chain_get_base_type(int *,
33 6691714a 2018-01-23 stsp struct got_delta_chain *);
34 885d3e02 2018-01-27 stsp const struct got_error *got_delta_apply(FILE *, const uint8_t *, size_t,
35 efd2a263 2018-01-19 stsp FILE *);
36 96a1d717 2018-01-24 stsp
37 96a1d717 2018-01-24 stsp /*
38 96a1d717 2018-01-24 stsp * Definitions for delta data streams.
39 96a1d717 2018-01-24 stsp */
40 96a1d717 2018-01-24 stsp
41 96a1d717 2018-01-24 stsp #define GOT_DELTA_STREAM_LENGTH_MIN 4 /* bytes */
42 96a1d717 2018-01-24 stsp
43 96a1d717 2018-01-24 stsp /*
44 96a1d717 2018-01-24 stsp * A delta stream begins with two size fields. The first specifies the
45 96a1d717 2018-01-24 stsp * size of the delta base, and the second describes the expected size of
46 96a1d717 2018-01-24 stsp * the data which results from combining the delta base and the delta.
47 96a1d717 2018-01-24 stsp *
48 96a1d717 2018-01-24 stsp * Each size field uses a variable length encoding:
49 96a1d717 2018-01-24 stsp * size0...sizeN form a 7+7+7+...+7 bit integer, where size0 is the
50 96a1d717 2018-01-24 stsp * least significant part and sizeN is the most significant part.
51 96a1d717 2018-01-24 stsp * If the MSB of a size byte is set, an additional size byte follows.
52 96a1d717 2018-01-24 stsp */
53 96a1d717 2018-01-24 stsp #define GOT_DELTA_SIZE_VAL_MASK 0x7f
54 96a1d717 2018-01-24 stsp #define GOT_DELTA_SIZE_SHIFT 7
55 96a1d717 2018-01-24 stsp #define GOT_DELTA_SIZE_MORE 0x80
56 96a1d717 2018-01-24 stsp
57 96a1d717 2018-01-24 stsp /*
58 824801e7 2018-01-27 stsp * The rest of the delta stream contains copy instructions.
59 96a1d717 2018-01-24 stsp *
60 824801e7 2018-01-27 stsp * A base copy instruction tells the delta combiner to copy N bytes starting
61 824801e7 2018-01-27 stsp * at offset X from the delta base to the output. Base copy instructions begin
62 824801e7 2018-01-27 stsp * with a byte which has its MSB set. The remaining bits of this byte describe
63 824801e7 2018-01-27 stsp * how many offset and length value bytes follow.
64 96a1d717 2018-01-24 stsp * The offset X is encoded in 1 to 4 bytes, and the length N is encoded in
65 96a1d717 2018-01-24 stsp * 1 to 3 bytes. For both values, the first byte contributes the least
66 96a1d717 2018-01-24 stsp * significant part and the last byte which is present contributes the
67 96a1d717 2018-01-24 stsp * most significant part.
68 96a1d717 2018-01-24 stsp * If the offset value is omitted, an offset of zero is implied.
69 96a1d717 2018-01-24 stsp * If the length value is omitted, a default length of 65536 bytes is implied.
70 96a1d717 2018-01-24 stsp *
71 824801e7 2018-01-27 stsp * An inline copy instruction tells the delta combiner to copy data from
72 824801e7 2018-01-27 stsp * the delta stream to the output.
73 824801e7 2018-01-27 stsp * Such instructions begin with one byte which does not have the MSB set
74 824801e7 2018-01-27 stsp * and which specifies the length of the inline data which follows (i.e.
75 96a1d717 2018-01-24 stsp * at most 127 bytes). A length value of zero is invalid.
76 96a1d717 2018-01-24 stsp */
77 96a1d717 2018-01-24 stsp
78 824801e7 2018-01-27 stsp #define GOT_DELTA_BASE_COPY 0x80
79 96a1d717 2018-01-24 stsp
80 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF1 0x01 /* byte 1 of offset is present */
81 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF2 0x02 /* byte 2 of offset is present */
82 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF3 0x04 /* byte 3 of offset is present */
83 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF4 0x08 /* byte 4 of offset is present */
84 96a1d717 2018-01-24 stsp
85 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_LEN1 0x10 /* byte 1 of length is present */
86 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_LEN2 0x20 /* byte 2 of length is present */
87 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_LEN3 0x40 /* byte 3 of length is present */
88 96a1d717 2018-01-24 stsp
89 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_DEFAULT_OFF 0x0 /* default offset if omitted */
90 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_DEFAULT_LEN 0x10000 /* default length if omitted */