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 6691714a 2018-01-23 stsp const struct got_error *got_delta_apply(struct got_delta *, FILE *, FILE *,
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 96a1d717 2018-01-24 stsp * A delta stream contains copy opcodes and verbatim data.
59 96a1d717 2018-01-24 stsp *
60 96a1d717 2018-01-24 stsp * A copy opcode instructs the delta combiner to copy N bytes starting at
61 96a1d717 2018-01-24 stsp * offset X from the delta base to the output. Copy opcodes begin with a
62 96a1d717 2018-01-24 stsp * byte which has its MSB set. The remaining bits of this byte describe how
63 96a1d717 2018-01-24 stsp * 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 96a1d717 2018-01-24 stsp * Verbatim data is copied from the delta stream to the output.
72 96a1d717 2018-01-24 stsp * Verbatim data is preceded by one byte which does not have the MSB set
73 96a1d717 2018-01-24 stsp * and which specifies the length of the verbatim data which follows (i.e.
74 96a1d717 2018-01-24 stsp * at most 127 bytes). A length value of zero is invalid.
75 96a1d717 2018-01-24 stsp */
76 96a1d717 2018-01-24 stsp
77 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OPCODE 0x80
78 96a1d717 2018-01-24 stsp
79 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF1 0x01 /* byte 1 of offset is present */
80 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF2 0x02 /* byte 2 of offset is present */
81 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF3 0x04 /* byte 3 of offset is present */
82 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_OFF4 0x08 /* byte 4 of offset is present */
83 96a1d717 2018-01-24 stsp
84 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_LEN1 0x10 /* byte 1 of length is present */
85 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_LEN2 0x20 /* byte 2 of length is present */
86 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_LEN3 0x40 /* byte 3 of length is present */
87 96a1d717 2018-01-24 stsp
88 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_DEFAULT_OFF 0x0 /* default offset if omitted */
89 96a1d717 2018-01-24 stsp #define GOT_DELTA_COPY_DEFAULT_LEN 0x10000 /* default length if omitted */