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 * int ::= highdigit* lowdigit;
19 * highdigit ::= # binary 1000 0000 OR-ed with 7 bit value;
20 * lowdigit ::= # 7 bit value;
23 #define VLI_CONTINUE 0x80
24 #define VLI_DIGIT_MASK 0x7f
25 #define VLI_BITS_PER_DIGIT 7
31 #define WINDOW_INIT { STRBUF_INIT }
33 static void window_release(struct window
*ctx
)
35 strbuf_release(&ctx
->data
);
38 static int error_short_read(struct line_buffer
*input
)
40 if (buffer_ferror(input
))
41 return error("error reading delta: %s", strerror(errno
));
42 return error("invalid delta: unexpected end of file");
45 static int read_chunk(struct line_buffer
*delta
, off_t
*delta_len
,
46 struct strbuf
*buf
, size_t len
)
49 if (len
> *delta_len
||
50 buffer_read_binary(delta
, buf
, len
) != len
)
51 return error_short_read(delta
);
52 *delta_len
-= buf
->len
;
56 static int read_magic(struct line_buffer
*in
, off_t
*len
)
58 static const char magic
[] = {'S', 'V', 'N', '\0'};
59 struct strbuf sb
= STRBUF_INIT
;
61 if (read_chunk(in
, len
, &sb
, sizeof(magic
))) {
65 if (memcmp(sb
.buf
, magic
, sizeof(magic
))) {
67 return error("invalid delta: unrecognized file type");
73 static int read_int(struct line_buffer
*in
, uintmax_t *result
, off_t
*len
)
77 for (sz
= *len
; sz
; sz
--) {
78 const int ch
= buffer_read_char(in
);
82 rv
<<= VLI_BITS_PER_DIGIT
;
83 rv
+= (ch
& VLI_DIGIT_MASK
);
84 if (ch
& VLI_CONTINUE
)
91 return error_short_read(in
);
94 static int read_offset(struct line_buffer
*in
, off_t
*result
, off_t
*len
)
97 if (read_int(in
, &val
, len
))
99 if (val
> maximum_signed_value_of_type(off_t
))
100 return error("unrepresentable offset in delta: %"PRIuMAX
"", val
);
105 static int read_length(struct line_buffer
*in
, size_t *result
, off_t
*len
)
108 if (read_int(in
, &val
, len
))
111 return error("unrepresentable length in delta: %"PRIuMAX
"", val
);
116 static int apply_one_window(struct line_buffer
*delta
, off_t
*delta_len
)
118 struct window ctx
= WINDOW_INIT
;
120 size_t instructions_len
;
124 /* "source view" offset and length already handled; */
125 if (read_length(delta
, &out_len
, delta_len
) ||
126 read_length(delta
, &instructions_len
, delta_len
) ||
127 read_length(delta
, &data_len
, delta_len
))
129 if (instructions_len
) {
130 error("What do you think I am? A delta applier?");
133 if (read_chunk(delta
, delta_len
, &ctx
.data
, data_len
))
135 window_release(&ctx
);
138 window_release(&ctx
);
142 int svndiff0_apply(struct line_buffer
*delta
, off_t delta_len
,
143 struct sliding_view
*preimage
, FILE *postimage
)
145 assert(delta
&& preimage
&& postimage
);
147 if (read_magic(delta
, &delta_len
))
149 while (delta_len
) { /* For each window: */
153 if (read_offset(delta
, &pre_off
, &delta_len
) ||
154 read_length(delta
, &pre_len
, &delta_len
) ||
155 move_window(preimage
, pre_off
, pre_len
) ||
156 apply_one_window(delta
, &delta_len
))