1/*
2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
4 */
5
6#include "git-compat-util.h"
7#include "sliding_window.h"
8#include "line_buffer.h"
9#include "svndiff.h"
10
11/*
12 * svndiff0 applier
13 *
14 * See http://svn.apache.org/repos/asf/subversion/trunk/notes/svndiff.
15 *
16 * svndiff0 ::= 'SVN\0' window*
17 * window ::= int int int int int instructions inline_data;
18 * instructions ::= instruction*;
19 * instruction ::= view_selector int int
20 * | copyfrom_data int
21 * | packed_view_selector int
22 * | packed_copyfrom_data
23 * ;
24 * view_selector ::= copyfrom_source
25 * | copyfrom_target
26 * ;
27 * copyfrom_source ::= # binary 00 000000;
28 * copyfrom_target ::= # binary 01 000000;
29 * copyfrom_data ::= # binary 10 000000;
30 * packed_view_selector ::= # view_selector OR-ed with 6 bit value;
31 * packed_copyfrom_data ::= # copyfrom_data OR-ed with 6 bit value;
32 * int ::= highdigit* lowdigit;
33 * highdigit ::= # binary 1000 0000 OR-ed with 7 bit value;
34 * lowdigit ::= # 7 bit value;
35 */
36
37#define INSN_MASK 0xc0
38#define INSN_COPYFROM_SOURCE 0x00
39#define INSN_COPYFROM_TARGET 0x40
40#define INSN_COPYFROM_DATA 0x80
41#define OPERAND_MASK 0x3f
42
43#define VLI_CONTINUE 0x80
44#define VLI_DIGIT_MASK 0x7f
45#define VLI_BITS_PER_DIGIT 7
46
47struct window {
48 struct sliding_view *in;
49 struct strbuf out;
50 struct strbuf instructions;
51 struct strbuf data;
52};
53
54#define WINDOW_INIT(w) { (w), STRBUF_INIT, STRBUF_INIT, STRBUF_INIT }
55
56static void window_release(struct window *ctx)
57{
58 strbuf_release(&ctx->out);
59 strbuf_release(&ctx->instructions);
60 strbuf_release(&ctx->data);
61}
62
63static int write_strbuf(struct strbuf *sb, FILE *out)
64{
65 if (fwrite(sb->buf, 1, sb->len, out) == sb->len) /* Success. */
66 return 0;
67 return error("cannot write delta postimage: %s", strerror(errno));
68}
69
70static int error_short_read(struct line_buffer *input)
71{
72 if (buffer_ferror(input))
73 return error("error reading delta: %s", strerror(errno));
74 return error("invalid delta: unexpected end of file");
75}
76
77static int read_chunk(struct line_buffer *delta, off_t *delta_len,
78 struct strbuf *buf, size_t len)
79{
80 strbuf_reset(buf);
81 if (len > *delta_len ||
82 buffer_read_binary(delta, buf, len) != len)
83 return error_short_read(delta);
84 *delta_len -= buf->len;
85 return 0;
86}
87
88static int read_magic(struct line_buffer *in, off_t *len)
89{
90 static const char magic[] = {'S', 'V', 'N', '\0'};
91 struct strbuf sb = STRBUF_INIT;
92
93 if (read_chunk(in, len, &sb, sizeof(magic))) {
94 strbuf_release(&sb);
95 return -1;
96 }
97 if (memcmp(sb.buf, magic, sizeof(magic))) {
98 strbuf_release(&sb);
99 return error("invalid delta: unrecognized file type");
100 }
101 strbuf_release(&sb);
102 return 0;
103}
104
105static int read_int(struct line_buffer *in, uintmax_t *result, off_t *len)
106{
107 uintmax_t rv = 0;
108 off_t sz;
109 for (sz = *len; sz; sz--) {
110 const int ch = buffer_read_char(in);
111 if (ch == EOF)
112 break;
113
114 rv <<= VLI_BITS_PER_DIGIT;
115 rv += (ch & VLI_DIGIT_MASK);
116 if (ch & VLI_CONTINUE)
117 continue;
118
119 *result = rv;
120 *len = sz - 1;
121 return 0;
122 }
123 return error_short_read(in);
124}
125
126static int parse_int(const char **buf, size_t *result, const char *end)
127{
128 size_t rv = 0;
129 const char *pos;
130 for (pos = *buf; pos != end; pos++) {
131 unsigned char ch = *pos;
132
133 rv <<= VLI_BITS_PER_DIGIT;
134 rv += (ch & VLI_DIGIT_MASK);
135 if (ch & VLI_CONTINUE)
136 continue;
137
138 *result = rv;
139 *buf = pos + 1;
140 return 0;
141 }
142 return error("invalid delta: unexpected end of instructions section");
143}
144
145static int read_offset(struct line_buffer *in, off_t *result, off_t *len)
146{
147 uintmax_t val;
148 if (read_int(in, &val, len))
149 return -1;
150 if (val > maximum_signed_value_of_type(off_t))
151 return error("unrepresentable offset in delta: %"PRIuMAX"", val);
152 *result = val;
153 return 0;
154}
155
156static int read_length(struct line_buffer *in, size_t *result, off_t *len)
157{
158 uintmax_t val;
159 if (read_int(in, &val, len))
160 return -1;
161 if (val > SIZE_MAX)
162 return error("unrepresentable length in delta: %"PRIuMAX"", val);
163 *result = val;
164 return 0;
165}
166
167static int copyfrom_source(struct window *ctx, const char **instructions,
168 size_t nbytes, const char *insns_end)
169{
170 size_t offset;
171 if (parse_int(instructions, &offset, insns_end))
172 return -1;
173 if (unsigned_add_overflows(offset, nbytes) ||
174 offset + nbytes > ctx->in->width)
175 return error("invalid delta: copies source data outside view");
176 strbuf_add(&ctx->out, ctx->in->buf.buf + offset, nbytes);
177 return 0;
178}
179
180static int copyfrom_target(struct window *ctx, const char **instructions,
181 size_t nbytes, const char *instructions_end)
182{
183 size_t offset;
184 if (parse_int(instructions, &offset, instructions_end))
185 return -1;
186 if (offset >= ctx->out.len)
187 return error("invalid delta: copies from the future");
188 for (; nbytes > 0; nbytes--)
189 strbuf_addch(&ctx->out, ctx->out.buf[offset++]);
190 return 0;
191}
192
193static int copyfrom_data(struct window *ctx, size_t *data_pos, size_t nbytes)
194{
195 const size_t pos = *data_pos;
196 if (unsigned_add_overflows(pos, nbytes) ||
197 pos + nbytes > ctx->data.len)
198 return error("invalid delta: copies unavailable inline data");
199 strbuf_add(&ctx->out, ctx->data.buf + pos, nbytes);
200 *data_pos += nbytes;
201 return 0;
202}
203
204static int parse_first_operand(const char **buf, size_t *out, const char *end)
205{
206 size_t result = (unsigned char) *(*buf)++ & OPERAND_MASK;
207 if (result) { /* immediate operand */
208 *out = result;
209 return 0;
210 }
211 return parse_int(buf, out, end);
212}
213
214static int execute_one_instruction(struct window *ctx,
215 const char **instructions, size_t *data_pos)
216{
217 unsigned int instruction;
218 const char *insns_end = ctx->instructions.buf + ctx->instructions.len;
219 size_t nbytes;
220 assert(ctx);
221 assert(instructions && *instructions);
222 assert(data_pos);
223
224 instruction = (unsigned char) **instructions;
225 if (parse_first_operand(instructions, &nbytes, insns_end))
226 return -1;
227 switch (instruction & INSN_MASK) {
228 case INSN_COPYFROM_SOURCE:
229 return copyfrom_source(ctx, instructions, nbytes, insns_end);
230 case INSN_COPYFROM_TARGET:
231 return copyfrom_target(ctx, instructions, nbytes, insns_end);
232 case INSN_COPYFROM_DATA:
233 return copyfrom_data(ctx, data_pos, nbytes);
234 default:
235 return error("invalid delta: unrecognized instruction");
236 }
237}
238
239static int apply_window_in_core(struct window *ctx)
240{
241 const char *instructions;
242 size_t data_pos = 0;
243
244 /*
245 * Fill ctx->out.buf using data from the source, target,
246 * and inline data views.
247 */
248 for (instructions = ctx->instructions.buf;
249 instructions != ctx->instructions.buf + ctx->instructions.len;
250 )
251 if (execute_one_instruction(ctx, &instructions, &data_pos))
252 return -1;
253 if (data_pos != ctx->data.len)
254 return error("invalid delta: does not copy all inline data");
255 return 0;
256}
257
258static int apply_one_window(struct line_buffer *delta, off_t *delta_len,
259 struct sliding_view *preimage, FILE *out)
260{
261 struct window ctx = WINDOW_INIT(preimage);
262 size_t out_len;
263 size_t instructions_len;
264 size_t data_len;
265 assert(delta_len);
266
267 /* "source view" offset and length already handled; */
268 if (read_length(delta, &out_len, delta_len) ||
269 read_length(delta, &instructions_len, delta_len) ||
270 read_length(delta, &data_len, delta_len) ||
271 read_chunk(delta, delta_len, &ctx.instructions, instructions_len) ||
272 read_chunk(delta, delta_len, &ctx.data, data_len))
273 goto error_out;
274 strbuf_grow(&ctx.out, out_len);
275 if (apply_window_in_core(&ctx))
276 goto error_out;
277 if (ctx.out.len != out_len) {
278 error("invalid delta: incorrect postimage length");
279 goto error_out;
280 }
281 if (write_strbuf(&ctx.out, out))
282 goto error_out;
283 window_release(&ctx);
284 return 0;
285error_out:
286 window_release(&ctx);
287 return -1;
288}
289
290int svndiff0_apply(struct line_buffer *delta, off_t delta_len,
291 struct sliding_view *preimage, FILE *postimage)
292{
293 assert(delta && preimage && postimage);
294
295 if (read_magic(delta, &delta_len))
296 return -1;
297 while (delta_len) { /* For each window: */
298 off_t pre_off = pre_off; /* stupid GCC... */
299 size_t pre_len;
300
301 if (read_offset(delta, &pre_off, &delta_len) ||
302 read_length(delta, &pre_len, &delta_len) ||
303 move_window(preimage, pre_off, pre_len) ||
304 apply_one_window(delta, &delta_len, preimage, postimage))
305 return -1;
306 }
307 return 0;
308}