]>
Commit | Line | Data |
---|---|---|
a310d434 NP |
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@cam.org> | |
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 | ||
57b73150 | 12 | #include "git-compat-util.h" |
a310d434 NP |
13 | #include "delta.h" |
14 | ||
08abe669 NP |
15 | void *patch_delta(const void *src_buf, unsigned long src_size, |
16 | const void *delta_buf, unsigned long delta_size, | |
a310d434 NP |
17 | unsigned long *dst_size) |
18 | { | |
19 | const unsigned char *data, *top; | |
20 | unsigned char *dst_buf, *out, cmd; | |
21 | unsigned long size; | |
a310d434 | 22 | |
dcde55bc | 23 | if (delta_size < DELTA_SIZE_MIN) |
a310d434 NP |
24 | return NULL; |
25 | ||
26 | data = delta_buf; | |
1d7f171c | 27 | top = (const unsigned char *) delta_buf + delta_size; |
a310d434 NP |
28 | |
29 | /* make sure the orig file size matches what we expect */ | |
8960844a | 30 | size = get_delta_hdr_size(&data, top); |
a310d434 NP |
31 | if (size != src_size) |
32 | return NULL; | |
33 | ||
34 | /* now the result size */ | |
8960844a | 35 | size = get_delta_hdr_size(&data, top); |
57b73150 | 36 | dst_buf = xmalloc(size + 1); |
ce726ec8 | 37 | dst_buf[size] = 0; |
a310d434 NP |
38 | |
39 | out = dst_buf; | |
40 | while (data < top) { | |
41 | cmd = *data++; | |
42 | if (cmd & 0x80) { | |
43 | unsigned long cp_off = 0, cp_size = 0; | |
a310d434 NP |
44 | if (cmd & 0x01) cp_off = *data++; |
45 | if (cmd & 0x02) cp_off |= (*data++ << 8); | |
46 | if (cmd & 0x04) cp_off |= (*data++ << 16); | |
47 | if (cmd & 0x08) cp_off |= (*data++ << 24); | |
48 | if (cmd & 0x10) cp_size = *data++; | |
49 | if (cmd & 0x20) cp_size |= (*data++ << 8); | |
d60fc1c8 | 50 | if (cmd & 0x40) cp_size |= (*data++ << 16); |
a310d434 | 51 | if (cp_size == 0) cp_size = 0x10000; |
8960844a NP |
52 | if (cp_off + cp_size < cp_size || |
53 | cp_off + cp_size > src_size || | |
54 | cp_size > size) | |
57b73150 | 55 | break; |
1d7f171c | 56 | memcpy(out, (char *) src_buf + cp_off, cp_size); |
a310d434 | 57 | out += cp_size; |
8960844a NP |
58 | size -= cp_size; |
59 | } else if (cmd) { | |
60 | if (cmd > size) | |
57b73150 | 61 | break; |
a310d434 NP |
62 | memcpy(out, data, cmd); |
63 | out += cmd; | |
64 | data += cmd; | |
8960844a NP |
65 | size -= cmd; |
66 | } else { | |
67 | /* | |
68 | * cmd == 0 is reserved for future encoding | |
69 | * extensions. In the mean time we must fail when | |
70 | * encountering them (might be data corruption). | |
71 | */ | |
57b73150 | 72 | error("unexpected delta opcode 0"); |
8960844a | 73 | goto bad; |
a310d434 NP |
74 | } |
75 | } | |
76 | ||
77 | /* sanity check */ | |
8960844a | 78 | if (data != top || size != 0) { |
57b73150 | 79 | error("delta replay has gone wild"); |
8960844a | 80 | bad: |
a310d434 NP |
81 | free(dst_buf); |
82 | return NULL; | |
83 | } | |
84 | ||
8960844a | 85 | *dst_size = out - dst_buf; |
a310d434 NP |
86 | return dst_buf; |
87 | } |