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