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 off_t offset;
20 size_t tslen;
21 int type;
22 size_t size;
23 off_t data_offset;
24 uint8_t *delta_buf;
25 size_t delta_len;
26 };
28 struct got_delta_chain {
29 int nentries;
30 SIMPLEQ_HEAD(, got_delta) entries;
31 };
33 #define GOT_DELTA_CHAIN_RECURSION_MAX 500
35 struct got_delta *got_delta_open(off_t, size_t, int, size_t, off_t,
36 uint8_t *, size_t);
37 void got_delta_close(struct got_delta *);
38 const struct got_error *got_delta_chain_get_base_type(int *,
39 struct got_delta_chain *);
40 const struct got_error *got_delta_get_sizes(uint64_t *, uint64_t *,
41 const uint8_t *, size_t);
42 const struct got_error *got_delta_apply_in_mem(uint8_t *, size_t,
43 const uint8_t *, size_t, uint8_t *, size_t *, size_t);
44 const struct got_error *got_delta_apply(FILE *, const uint8_t *, size_t,
45 FILE *, size_t *);
47 /*
48 * The amount of result data we may keep in RAM while applying deltas.
49 * Data larger than this is written to disk during delta application (slow).
50 */
51 #define GOT_DELTA_RESULT_SIZE_CACHED_MAX (4 * 1024 * 1024) /* bytes */
53 /*
54 * Definitions for delta data streams.
55 */
57 #define GOT_DELTA_STREAM_LENGTH_MIN 4 /* bytes */
59 /*
60 * A delta stream begins with two size fields. The first specifies the
61 * size of the delta base, and the second describes the expected size of
62 * the data which results from applying the delta to the delta base.
63 *
64 * Each size field uses a variable length encoding:
65 * size0...sizeN form a 7+7+7+...+7 bit integer, where size0 is the
66 * least significant part and sizeN is the most significant part.
67 * If the MSB of a size byte is set, an additional size byte follows.
68 */
69 #define GOT_DELTA_SIZE_VAL_MASK 0x7f
70 #define GOT_DELTA_SIZE_SHIFT 7
71 #define GOT_DELTA_SIZE_MORE 0x80
73 /*
74 * The rest of the delta stream contains copy instructions.
75 *
76 * A base copy instruction copies N bytes starting at offset X from the delta
77 * base to the output. Base copy instructions begin with a byte which has its
78 * MSB set. The remaining bits of this byte describe how many offset and
79 * length value bytes follow.
80 * The offset X is encoded in 1 to 4 bytes, and the length N is encoded in
81 * 1 to 3 bytes. For both values, the first byte contributes the least
82 * significant part and the last byte which is present contributes the
83 * most significant part.
84 * If the offset value is omitted, an offset of zero is implied.
85 * If the length value is omitted, a default length of 65536 bytes is implied.
86 *
87 * An inline copy instruction copies data from the delta stream to the output.
88 * Such instructions begin with one byte which does not have the MSB set
89 * and which specifies the length of the inline data which follows (i.e.
90 * at most 127 bytes). A length value of zero is invalid.
91 */
93 #define GOT_DELTA_BASE_COPY 0x80
95 #define GOT_DELTA_COPY_OFF1 0x01 /* byte 1 of offset is present */
96 #define GOT_DELTA_COPY_OFF2 0x02 /* byte 2 of offset is present */
97 #define GOT_DELTA_COPY_OFF3 0x04 /* byte 3 of offset is present */
98 #define GOT_DELTA_COPY_OFF4 0x08 /* byte 4 of offset is present */
100 #define GOT_DELTA_COPY_LEN1 0x10 /* byte 1 of length is present */
101 #define GOT_DELTA_COPY_LEN2 0x20 /* byte 2 of length is present */
102 #define GOT_DELTA_COPY_LEN3 0x40 /* byte 3 of length is present */
104 #define GOT_DELTA_COPY_DEFAULT_OFF 0x0 /* default offset if omitted */
105 #define GOT_DELTA_COPY_DEFAULT_LEN 0x10000 /* default length if omitted */