Blob


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