credential: fix wrong double password prompt
[git] / vcs-svn / svndiff.c
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
47 struct 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
56 static void window_release(struct window *ctx)
57 {
58         strbuf_release(&ctx->out);
59         strbuf_release(&ctx->instructions);
60         strbuf_release(&ctx->data);
61 }
62
63 static 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         error("cannot write delta postimage: %s", strerror(errno));
68         return -1;
69 }
70
71 static int error_short_read(struct line_buffer *input)
72 {
73         if (buffer_ferror(input))
74                 error("error reading delta: %s", strerror(errno));
75         else
76                 error("invalid delta: unexpected end of file");
77         return -1;
78 }
79
80 static int read_chunk(struct line_buffer *delta, off_t *delta_len,
81                       struct strbuf *buf, size_t len)
82 {
83         assert(*delta_len >= 0);
84         strbuf_reset(buf);
85         if (len > (uintmax_t) *delta_len ||
86             buffer_read_binary(delta, buf, len) != len)
87                 return error_short_read(delta);
88         *delta_len -= buf->len;
89         return 0;
90 }
91
92 static int read_magic(struct line_buffer *in, off_t *len)
93 {
94         static const char magic[] = {'S', 'V', 'N', '\0'};
95         struct strbuf sb = STRBUF_INIT;
96
97         if (read_chunk(in, len, &sb, sizeof(magic))) {
98                 strbuf_release(&sb);
99                 return -1;
100         }
101         if (memcmp(sb.buf, magic, sizeof(magic))) {
102                 strbuf_release(&sb);
103                 error("invalid delta: unrecognized file type");
104                 return -1;
105         }
106         strbuf_release(&sb);
107         return 0;
108 }
109
110 static int read_int(struct line_buffer *in, uintmax_t *result, off_t *len)
111 {
112         uintmax_t rv = 0;
113         off_t sz;
114         for (sz = *len; sz; sz--) {
115                 const int ch = buffer_read_char(in);
116                 if (ch == EOF)
117                         break;
118
119                 rv <<= VLI_BITS_PER_DIGIT;
120                 rv += (ch & VLI_DIGIT_MASK);
121                 if (ch & VLI_CONTINUE)
122                         continue;
123
124                 *result = rv;
125                 *len = sz - 1;
126                 return 0;
127         }
128         return error_short_read(in);
129 }
130
131 static int parse_int(const char **buf, size_t *result, const char *end)
132 {
133         size_t rv = 0;
134         const char *pos;
135         for (pos = *buf; pos != end; pos++) {
136                 unsigned char ch = *pos;
137
138                 rv <<= VLI_BITS_PER_DIGIT;
139                 rv += (ch & VLI_DIGIT_MASK);
140                 if (ch & VLI_CONTINUE)
141                         continue;
142
143                 *result = rv;
144                 *buf = pos + 1;
145                 return 0;
146         }
147         error("invalid delta: unexpected end of instructions section");
148         return -1;
149 }
150
151 static int read_offset(struct line_buffer *in, off_t *result, off_t *len)
152 {
153         uintmax_t val;
154         if (read_int(in, &val, len))
155                 return -1;
156         if (val > maximum_signed_value_of_type(off_t))
157                 return error("unrepresentable offset in delta: %"PRIuMAX"", val);
158         *result = val;
159         return 0;
160 }
161
162 static int read_length(struct line_buffer *in, size_t *result, off_t *len)
163 {
164         uintmax_t val;
165         if (read_int(in, &val, len))
166                 return -1;
167         if (val > SIZE_MAX)
168                 return error("unrepresentable length in delta: %"PRIuMAX"", val);
169         *result = val;
170         return 0;
171 }
172
173 static int copyfrom_source(struct window *ctx, const char **instructions,
174                            size_t nbytes, const char *insns_end)
175 {
176         size_t offset;
177         if (parse_int(instructions, &offset, insns_end))
178                 return -1;
179         if (unsigned_add_overflows(offset, nbytes) ||
180             offset + nbytes > ctx->in->width)
181                 return error("invalid delta: copies source data outside view");
182         strbuf_add(&ctx->out, ctx->in->buf.buf + offset, nbytes);
183         return 0;
184 }
185
186 static int copyfrom_target(struct window *ctx, const char **instructions,
187                            size_t nbytes, const char *instructions_end)
188 {
189         size_t offset;
190         if (parse_int(instructions, &offset, instructions_end))
191                 return -1;
192         if (offset >= ctx->out.len)
193                 return error("invalid delta: copies from the future");
194         for (; nbytes > 0; nbytes--)
195                 strbuf_addch(&ctx->out, ctx->out.buf[offset++]);
196         return 0;
197 }
198
199 static int copyfrom_data(struct window *ctx, size_t *data_pos, size_t nbytes)
200 {
201         const size_t pos = *data_pos;
202         if (unsigned_add_overflows(pos, nbytes) ||
203             pos + nbytes > ctx->data.len)
204                 return error("invalid delta: copies unavailable inline data");
205         strbuf_add(&ctx->out, ctx->data.buf + pos, nbytes);
206         *data_pos += nbytes;
207         return 0;
208 }
209
210 static int parse_first_operand(const char **buf, size_t *out, const char *end)
211 {
212         size_t result = (unsigned char) *(*buf)++ & OPERAND_MASK;
213         if (result) {   /* immediate operand */
214                 *out = result;
215                 return 0;
216         }
217         return parse_int(buf, out, end);
218 }
219
220 static int execute_one_instruction(struct window *ctx,
221                                 const char **instructions, size_t *data_pos)
222 {
223         unsigned int instruction;
224         const char *insns_end = ctx->instructions.buf + ctx->instructions.len;
225         size_t nbytes;
226         assert(ctx);
227         assert(instructions && *instructions);
228         assert(data_pos);
229
230         instruction = (unsigned char) **instructions;
231         if (parse_first_operand(instructions, &nbytes, insns_end))
232                 return -1;
233         switch (instruction & INSN_MASK) {
234         case INSN_COPYFROM_SOURCE:
235                 return copyfrom_source(ctx, instructions, nbytes, insns_end);
236         case INSN_COPYFROM_TARGET:
237                 return copyfrom_target(ctx, instructions, nbytes, insns_end);
238         case INSN_COPYFROM_DATA:
239                 return copyfrom_data(ctx, data_pos, nbytes);
240         default:
241                 return error("invalid delta: unrecognized instruction");
242         }
243 }
244
245 static int apply_window_in_core(struct window *ctx)
246 {
247         const char *instructions;
248         size_t data_pos = 0;
249
250         /*
251          * Fill ctx->out.buf using data from the source, target,
252          * and inline data views.
253          */
254         for (instructions = ctx->instructions.buf;
255              instructions != ctx->instructions.buf + ctx->instructions.len;
256              )
257                 if (execute_one_instruction(ctx, &instructions, &data_pos))
258                         return -1;
259         if (data_pos != ctx->data.len)
260                 return error("invalid delta: does not copy all inline data");
261         return 0;
262 }
263
264 static int apply_one_window(struct line_buffer *delta, off_t *delta_len,
265                             struct sliding_view *preimage, FILE *out)
266 {
267         int rv = -1;
268         struct window ctx = WINDOW_INIT(preimage);
269         size_t out_len;
270         size_t instructions_len;
271         size_t data_len;
272         assert(delta_len);
273
274         /* "source view" offset and length already handled; */
275         if (read_length(delta, &out_len, delta_len) ||
276             read_length(delta, &instructions_len, delta_len) ||
277             read_length(delta, &data_len, delta_len) ||
278             read_chunk(delta, delta_len, &ctx.instructions, instructions_len) ||
279             read_chunk(delta, delta_len, &ctx.data, data_len))
280                 goto error_out;
281         strbuf_grow(&ctx.out, out_len);
282         if (apply_window_in_core(&ctx))
283                 goto error_out;
284         if (ctx.out.len != out_len) {
285                 rv = error("invalid delta: incorrect postimage length");
286                 goto error_out;
287         }
288         if (write_strbuf(&ctx.out, out))
289                 goto error_out;
290         rv = 0;
291 error_out:
292         window_release(&ctx);
293         return rv;
294 }
295
296 int svndiff0_apply(struct line_buffer *delta, off_t delta_len,
297                         struct sliding_view *preimage, FILE *postimage)
298 {
299         assert(delta && preimage && postimage && delta_len >= 0);
300
301         if (read_magic(delta, &delta_len))
302                 return -1;
303         while (delta_len) {     /* For each window: */
304                 off_t pre_off = -1;
305                 size_t pre_len;
306
307                 if (read_offset(delta, &pre_off, &delta_len) ||
308                     read_length(delta, &pre_len, &delta_len) ||
309                     move_window(preimage, pre_off, pre_len) ||
310                     apply_one_window(delta, &delta_len, preimage, postimage))
311                         return -1;
312         }
313         return 0;
314 }