2 * Licensed under a two-clause BSD-style license.
3 * See LICENSE for details.
6 #include "git-compat-util.h"
7 #include "sliding_window.h"
8 #include "line_buffer.h"
14 * See http://svn.apache.org/repos/asf/subversion/trunk/notes/svndiff.
16 * svndiff0 ::= 'SVN\0' window*
17 * window ::= int int int int int instructions inline_data;
18 * instructions ::= instruction*;
19 * instruction ::= view_selector int int
21 * | packed_view_selector int
22 * | packed_copyfrom_data
24 * view_selector ::= copyfrom_source
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;
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
43 #define VLI_CONTINUE 0x80
44 #define VLI_DIGIT_MASK 0x7f
45 #define VLI_BITS_PER_DIGIT 7
48 struct sliding_view *in;
50 struct strbuf instructions;
54 #define WINDOW_INIT(w) { (w), STRBUF_INIT, STRBUF_INIT, STRBUF_INIT }
56 static void window_release(struct window *ctx)
58 strbuf_release(&ctx->out);
59 strbuf_release(&ctx->instructions);
60 strbuf_release(&ctx->data);
63 static int write_strbuf(struct strbuf *sb, FILE *out)
65 if (fwrite(sb->buf, 1, sb->len, out) == sb->len) /* Success. */
67 return error("cannot write delta postimage: %s", strerror(errno));
70 static int error_short_read(struct line_buffer *input)
72 if (buffer_ferror(input))
73 return error("error reading delta: %s", strerror(errno));
74 return error("invalid delta: unexpected end of file");
77 static int read_chunk(struct line_buffer *delta, off_t *delta_len,
78 struct strbuf *buf, size_t len)
81 if (len > *delta_len ||
82 buffer_read_binary(delta, buf, len) != len)
83 return error_short_read(delta);
84 *delta_len -= buf->len;
88 static int read_magic(struct line_buffer *in, off_t *len)
90 static const char magic[] = {'S', 'V', 'N', '\0'};
91 struct strbuf sb = STRBUF_INIT;
93 if (read_chunk(in, len, &sb, sizeof(magic))) {
97 if (memcmp(sb.buf, magic, sizeof(magic))) {
99 return error("invalid delta: unrecognized file type");
105 static int read_int(struct line_buffer *in, uintmax_t *result, off_t *len)
109 for (sz = *len; sz; sz--) {
110 const int ch = buffer_read_char(in);
114 rv <<= VLI_BITS_PER_DIGIT;
115 rv += (ch & VLI_DIGIT_MASK);
116 if (ch & VLI_CONTINUE)
123 return error_short_read(in);
126 static int parse_int(const char **buf, size_t *result, const char *end)
130 for (pos = *buf; pos != end; pos++) {
131 unsigned char ch = *pos;
133 rv <<= VLI_BITS_PER_DIGIT;
134 rv += (ch & VLI_DIGIT_MASK);
135 if (ch & VLI_CONTINUE)
142 return error("invalid delta: unexpected end of instructions section");
145 static int read_offset(struct line_buffer *in, off_t *result, off_t *len)
148 if (read_int(in, &val, len))
150 if (val > maximum_signed_value_of_type(off_t))
151 return error("unrepresentable offset in delta: %"PRIuMAX"", val);
156 static int read_length(struct line_buffer *in, size_t *result, off_t *len)
159 if (read_int(in, &val, len))
162 return error("unrepresentable length in delta: %"PRIuMAX"", val);
167 static int copyfrom_source(struct window *ctx, const char **instructions,
168 size_t nbytes, const char *insns_end)
171 if (parse_int(instructions, &offset, insns_end))
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);
180 static int copyfrom_target(struct window *ctx, const char **instructions,
181 size_t nbytes, const char *instructions_end)
184 if (parse_int(instructions, &offset, instructions_end))
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++]);
193 static int copyfrom_data(struct window *ctx, size_t *data_pos, size_t nbytes)
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);
204 static int parse_first_operand(const char **buf, size_t *out, const char *end)
206 size_t result = (unsigned char) *(*buf)++ & OPERAND_MASK;
207 if (result) { /* immediate operand */
211 return parse_int(buf, out, end);
214 static int execute_one_instruction(struct window *ctx,
215 const char **instructions, size_t *data_pos)
217 unsigned int instruction;
218 const char *insns_end = ctx->instructions.buf + ctx->instructions.len;
221 assert(instructions && *instructions);
224 instruction = (unsigned char) **instructions;
225 if (parse_first_operand(instructions, &nbytes, insns_end))
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);
235 return error("invalid delta: unrecognized instruction");
239 static int apply_window_in_core(struct window *ctx)
241 const char *instructions;
245 * Fill ctx->out.buf using data from the source, target,
246 * and inline data views.
248 for (instructions = ctx->instructions.buf;
249 instructions != ctx->instructions.buf + ctx->instructions.len;
251 if (execute_one_instruction(ctx, &instructions, &data_pos))
253 if (data_pos != ctx->data.len)
254 return error("invalid delta: does not copy all inline data");
258 static int apply_one_window(struct line_buffer *delta, off_t *delta_len,
259 struct sliding_view *preimage, FILE *out)
261 struct window ctx = WINDOW_INIT(preimage);
263 size_t instructions_len;
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))
274 strbuf_grow(&ctx.out, out_len);
275 if (apply_window_in_core(&ctx))
277 if (ctx.out.len != out_len) {
278 error("invalid delta: incorrect postimage length");
281 if (write_strbuf(&ctx.out, out))
283 window_release(&ctx);
286 window_release(&ctx);
290 int svndiff0_apply(struct line_buffer *delta, off_t delta_len,
291 struct sliding_view *preimage, FILE *postimage)
293 assert(delta && preimage && postimage);
295 if (read_magic(delta, &delta_len))
297 while (delta_len) { /* For each window: */
298 off_t pre_off = pre_off; /* stupid GCC... */
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))