Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * patch-delta.c: |
3 | | * recreate a buffer from a source and the delta produced by diff-delta.c |
4 | | * |
5 | | * (C) 2005 Nicolas Pitre <nico@fluxnic.net> |
6 | | * |
7 | | * This code is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License version 2 as |
9 | | * published by the Free Software Foundation. |
10 | | */ |
11 | | |
12 | | #include "git-compat-util.h" |
13 | | #include "delta.h" |
14 | | |
15 | | void *patch_delta(const void *src_buf, unsigned long src_size, |
16 | | const void *delta_buf, unsigned long delta_size, |
17 | | unsigned long *dst_size) |
18 | 0 | { |
19 | 0 | const unsigned char *data, *top; |
20 | 0 | unsigned char *dst_buf, *out, cmd; |
21 | 0 | unsigned long size; |
22 | |
|
23 | 0 | if (delta_size < DELTA_SIZE_MIN) |
24 | 0 | return NULL; |
25 | | |
26 | 0 | data = delta_buf; |
27 | 0 | top = (const unsigned char *) delta_buf + delta_size; |
28 | | |
29 | | /* make sure the orig file size matches what we expect */ |
30 | 0 | size = get_delta_hdr_size(&data, top); |
31 | 0 | if (size != src_size) |
32 | 0 | return NULL; |
33 | | |
34 | | /* now the result size */ |
35 | 0 | size = get_delta_hdr_size(&data, top); |
36 | 0 | dst_buf = xmallocz(size); |
37 | |
|
38 | 0 | out = dst_buf; |
39 | 0 | while (data < top) { |
40 | 0 | cmd = *data++; |
41 | 0 | if (cmd & 0x80) { |
42 | 0 | unsigned long cp_off = 0, cp_size = 0; |
43 | 0 | #define PARSE_CP_PARAM(bit, var, shift) do { \ |
44 | 0 | if (cmd & (bit)) { \ |
45 | 0 | if (data >= top) \ |
46 | 0 | goto bad_length; \ |
47 | 0 | var |= ((unsigned) *data++ << (shift)); \ |
48 | 0 | } } while (0) |
49 | 0 | PARSE_CP_PARAM(0x01, cp_off, 0); |
50 | 0 | PARSE_CP_PARAM(0x02, cp_off, 8); |
51 | 0 | PARSE_CP_PARAM(0x04, cp_off, 16); |
52 | 0 | PARSE_CP_PARAM(0x08, cp_off, 24); |
53 | 0 | PARSE_CP_PARAM(0x10, cp_size, 0); |
54 | 0 | PARSE_CP_PARAM(0x20, cp_size, 8); |
55 | 0 | PARSE_CP_PARAM(0x40, cp_size, 16); |
56 | 0 | #undef PARSE_CP_PARAM |
57 | 0 | if (cp_size == 0) cp_size = 0x10000; |
58 | 0 | if (unsigned_add_overflows(cp_off, cp_size) || |
59 | 0 | cp_off + cp_size > src_size || |
60 | 0 | cp_size > size) |
61 | 0 | goto bad_length; |
62 | 0 | memcpy(out, (char *) src_buf + cp_off, cp_size); |
63 | 0 | out += cp_size; |
64 | 0 | size -= cp_size; |
65 | 0 | } else if (cmd) { |
66 | 0 | if (cmd > size || cmd > top - data) |
67 | 0 | goto bad_length; |
68 | 0 | memcpy(out, data, cmd); |
69 | 0 | out += cmd; |
70 | 0 | data += cmd; |
71 | 0 | size -= cmd; |
72 | 0 | } else { |
73 | | /* |
74 | | * cmd == 0 is reserved for future encoding |
75 | | * extensions. In the mean time we must fail when |
76 | | * encountering them (might be data corruption). |
77 | | */ |
78 | 0 | error("unexpected delta opcode 0"); |
79 | 0 | goto bad; |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | /* sanity check */ |
84 | 0 | if (data != top || size != 0) { |
85 | 0 | bad_length: |
86 | 0 | error("delta replay has gone wild"); |
87 | 0 | bad: |
88 | 0 | free(dst_buf); |
89 | 0 | return NULL; |
90 | 0 | } |
91 | | |
92 | 0 | *dst_size = out - dst_buf; |
93 | 0 | return dst_buf; |
94 | 0 | } |