author | Daniel Holth <dholth@fastmail.fm> |
Wed, 16 May 2007 01:10:12 -0400 | |
changeset 4448 | af013ae3ca10 |
parent 4377 | 4759da3e4dc8 |
child 5444 | a0952e4e52eb |
permissions | -rw-r--r-- |
72 | 1 |
/* |
2 |
mpatch.c - efficient binary patching for Mercurial |
|
3 |
||
4 |
This implements a patch algorithm that's O(m + nlog n) where m is the |
|
5 |
size of the output and n is the number of patches. |
|
6 |
||
7 |
Given a list of binary patches, it unpacks each into a hunk list, |
|
8 |
then combines the hunk lists with a treewise recursion to form a |
|
9 |
single hunk list. This hunk list is then applied to the original |
|
10 |
text. |
|
11 |
||
12 |
The text (or binary) fragments are copied directly from their source |
|
13 |
Python objects into a preallocated output string to avoid the |
|
14 |
allocation of intermediate Python objects. Working memory is about 2x |
|
15 |
the total number of hunks. |
|
16 |
||
2859 | 17 |
Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
72 | 18 |
|
19 |
This software may be used and distributed according to the terms |
|
20 |
of the GNU General Public License, incorporated herein by reference. |
|
21 |
*/ |
|
22 |
||
23 |
#include <Python.h> |
|
24 |
#include <stdlib.h> |
|
25 |
#include <string.h> |
|
2468
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
26 |
|
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
27 |
#ifdef _WIN32 |
2468
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
28 |
# ifdef _MSC_VER |
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
29 |
/* msvc 6.0 has problems */ |
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
30 |
# define inline __inline |
551 | 31 |
typedef unsigned long uint32_t; |
2468
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
32 |
# else |
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
33 |
# include <stdint.h> |
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
34 |
# endif |
411
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
35 |
static uint32_t ntohl(uint32_t x) |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
36 |
{ |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
37 |
return ((x & 0x000000ffUL) << 24) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
38 |
((x & 0x0000ff00UL) << 8) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
39 |
((x & 0x00ff0000UL) >> 8) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
40 |
((x & 0xff000000UL) >> 24); |
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
41 |
} |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
42 |
#else |
2468
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
43 |
/* not windows */ |
1ac0574f1768
mac os x: fixes for 10.2 from chris monson <monpublic@gmail.com>
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
2083
diff
changeset
|
44 |
# include <sys/types.h> |
4073
95ffa36d1d2a
BeOS compatibility support
Andrew Bachmann <andrewbachmann@gmail.com>
parents:
3138
diff
changeset
|
45 |
# ifdef __BEOS__ |
95ffa36d1d2a
BeOS compatibility support
Andrew Bachmann <andrewbachmann@gmail.com>
parents:
3138
diff
changeset
|
46 |
# include <ByteOrder.h> |
95ffa36d1d2a
BeOS compatibility support
Andrew Bachmann <andrewbachmann@gmail.com>
parents:
3138
diff
changeset
|
47 |
# else |
95ffa36d1d2a
BeOS compatibility support
Andrew Bachmann <andrewbachmann@gmail.com>
parents:
3138
diff
changeset
|
48 |
# include <arpa/inet.h> |
95ffa36d1d2a
BeOS compatibility support
Andrew Bachmann <andrewbachmann@gmail.com>
parents:
3138
diff
changeset
|
49 |
# endif |
2543
860e9c83fc59
Include inttypes.h instead of stdint.h (fixes issue299)
Thomas Arendsen Hein <thomas@intevation.de>
parents:
2468
diff
changeset
|
50 |
# include <inttypes.h> |
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
51 |
#endif |
72 | 52 |
|
53 |
static char mpatch_doc[] = "Efficient binary patching."; |
|
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
54 |
static PyObject *mpatch_Error; |
72 | 55 |
|
56 |
struct frag { |
|
57 |
int start, end, len; |
|
58 |
char *data; |
|
59 |
}; |
|
60 |
||
61 |
struct flist { |
|
62 |
struct frag *base, *head, *tail; |
|
63 |
}; |
|
64 |
||
65 |
static struct flist *lalloc(int size) |
|
66 |
{ |
|
128 | 67 |
struct flist *a = NULL; |
72 | 68 |
|
3138
cc856c4d91ca
mpatch: Fix for malloc corner case on AIX
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
69 |
if (size < 1) |
cc856c4d91ca
mpatch: Fix for malloc corner case on AIX
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
70 |
size = 1; |
cc856c4d91ca
mpatch: Fix for malloc corner case on AIX
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
71 |
|
1978
10606ee61107
do proper typecasting on malloc() and calloc() calls
TK Soh <teekaysoh@yahoo.com>
parents:
1746
diff
changeset
|
72 |
a = (struct flist *)malloc(sizeof(struct flist)); |
128 | 73 |
if (a) { |
1978
10606ee61107
do proper typecasting on malloc() and calloc() calls
TK Soh <teekaysoh@yahoo.com>
parents:
1746
diff
changeset
|
74 |
a->base = (struct frag *)malloc(sizeof(struct frag) * size); |
2048
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
75 |
if (a->base) { |
128 | 76 |
a->head = a->tail = a->base; |
2048
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
77 |
return a; |
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
78 |
} |
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
79 |
free(a); |
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
80 |
a = NULL; |
128 | 81 |
} |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
82 |
if (!PyErr_Occurred()) |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
83 |
PyErr_NoMemory(); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
84 |
return NULL; |
72 | 85 |
} |
86 |
||
87 |
static void lfree(struct flist *a) |
|
88 |
{ |
|
128 | 89 |
if (a) { |
90 |
free(a->base); |
|
91 |
free(a); |
|
92 |
} |
|
72 | 93 |
} |
94 |
||
95 |
static int lsize(struct flist *a) |
|
96 |
{ |
|
97 |
return a->tail - a->head; |
|
98 |
} |
|
99 |
||
100 |
/* move hunks in source that are less cut to dest, compensating |
|
101 |
for changes in offset. the last hunk may be split if necessary. |
|
102 |
*/ |
|
103 |
static int gather(struct flist *dest, struct flist *src, int cut, int offset) |
|
104 |
{ |
|
105 |
struct frag *d = dest->tail, *s = src->head; |
|
106 |
int postend, c, l; |
|
107 |
||
108 |
while (s != src->tail) { |
|
109 |
if (s->start + offset >= cut) |
|
82 | 110 |
break; /* we've gone far enough */ |
72 | 111 |
|
112 |
postend = offset + s->start + s->len; |
|
113 |
if (postend <= cut) { |
|
114 |
/* save this hunk */ |
|
115 |
offset += s->start + s->len - s->end; |
|
116 |
*d++ = *s++; |
|
117 |
} |
|
118 |
else { |
|
119 |
/* break up this hunk */ |
|
120 |
c = cut - offset; |
|
121 |
if (s->end < c) |
|
122 |
c = s->end; |
|
123 |
l = cut - offset - s->start; |
|
124 |
if (s->len < l) |
|
125 |
l = s->len; |
|
126 |
||
127 |
offset += s->start + l - c; |
|
128 |
||
129 |
d->start = s->start; |
|
130 |
d->end = c; |
|
131 |
d->len = l; |
|
132 |
d->data = s->data; |
|
133 |
d++; |
|
134 |
s->start = c; |
|
135 |
s->len = s->len - l; |
|
136 |
s->data = s->data + l; |
|
137 |
||
82 | 138 |
break; |
72 | 139 |
} |
140 |
} |
|
141 |
||
142 |
dest->tail = d; |
|
143 |
src->head = s; |
|
144 |
return offset; |
|
145 |
} |
|
146 |
||
147 |
/* like gather, but with no output list */ |
|
148 |
static int discard(struct flist *src, int cut, int offset) |
|
149 |
{ |
|
150 |
struct frag *s = src->head; |
|
151 |
int postend, c, l; |
|
152 |
||
153 |
while (s != src->tail) { |
|
154 |
if (s->start + offset >= cut) |
|
82 | 155 |
break; |
72 | 156 |
|
157 |
postend = offset + s->start + s->len; |
|
158 |
if (postend <= cut) { |
|
159 |
offset += s->start + s->len - s->end; |
|
160 |
s++; |
|
161 |
} |
|
162 |
else { |
|
163 |
c = cut - offset; |
|
164 |
if (s->end < c) |
|
165 |
c = s->end; |
|
166 |
l = cut - offset - s->start; |
|
167 |
if (s->len < l) |
|
168 |
l = s->len; |
|
169 |
||
170 |
offset += s->start + l - c; |
|
171 |
s->start = c; |
|
172 |
s->len = s->len - l; |
|
173 |
s->data = s->data + l; |
|
174 |
||
82 | 175 |
break; |
72 | 176 |
} |
177 |
} |
|
178 |
||
179 |
src->head = s; |
|
180 |
return offset; |
|
181 |
} |
|
182 |
||
183 |
/* combine hunk lists a and b, while adjusting b for offset changes in a/ |
|
184 |
this deletes a and b and returns the resultant list. */ |
|
185 |
static struct flist *combine(struct flist *a, struct flist *b) |
|
186 |
{ |
|
128 | 187 |
struct flist *c = NULL; |
188 |
struct frag *bh, *ct; |
|
72 | 189 |
int offset = 0, post; |
190 |
||
128 | 191 |
if (a && b) |
192 |
c = lalloc((lsize(a) + lsize(b)) * 2); |
|
193 |
||
194 |
if (c) { |
|
72 | 195 |
|
128 | 196 |
for (bh = b->head; bh != b->tail; bh++) { |
197 |
/* save old hunks */ |
|
198 |
offset = gather(c, a, bh->start, offset); |
|
72 | 199 |
|
128 | 200 |
/* discard replaced hunks */ |
201 |
post = discard(a, bh->end, offset); |
|
72 | 202 |
|
128 | 203 |
/* insert new hunk */ |
204 |
ct = c->tail; |
|
205 |
ct->start = bh->start - offset; |
|
206 |
ct->end = bh->end - post; |
|
207 |
ct->len = bh->len; |
|
208 |
ct->data = bh->data; |
|
209 |
c->tail++; |
|
210 |
offset = post; |
|
211 |
} |
|
212 |
||
213 |
/* hold on to tail from a */ |
|
214 |
memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a)); |
|
215 |
c->tail += lsize(a); |
|
72 | 216 |
} |
217 |
||
218 |
lfree(a); |
|
219 |
lfree(b); |
|
220 |
return c; |
|
221 |
} |
|
222 |
||
223 |
/* decode a binary patch into a hunk list */ |
|
224 |
static struct flist *decode(char *bin, int len) |
|
225 |
{ |
|
226 |
struct flist *l; |
|
227 |
struct frag *lt; |
|
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
228 |
char *data = bin + 12, *end = bin + len; |
384
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
229 |
char decode[12]; /* for dealing with alignment issues */ |
72 | 230 |
|
231 |
/* assume worst case size, we won't have many of these lists */ |
|
232 |
l = lalloc(len / 12); |
|
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
233 |
if (!l) |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
234 |
return NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
235 |
|
72 | 236 |
lt = l->tail; |
237 |
||
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
238 |
while (data <= end) { |
384
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
239 |
memcpy(decode, bin, 12); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
240 |
lt->start = ntohl(*(uint32_t *)decode); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
241 |
lt->end = ntohl(*(uint32_t *)(decode + 4)); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
242 |
lt->len = ntohl(*(uint32_t *)(decode + 8)); |
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
243 |
if (lt->start > lt->end) |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
244 |
break; /* sanity check */ |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
245 |
bin = data + lt->len; |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
246 |
if (bin < data) |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
247 |
break; /* big data + big (bogus) len can wrap around */ |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
248 |
lt->data = data; |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
249 |
data = bin + 12; |
72 | 250 |
lt++; |
251 |
} |
|
252 |
||
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
253 |
if (bin != end) { |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
254 |
if (!PyErr_Occurred()) |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
255 |
PyErr_SetString(mpatch_Error, "patch cannot be decoded"); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
256 |
lfree(l); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
257 |
return NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
258 |
} |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
259 |
|
72 | 260 |
l->tail = lt; |
261 |
return l; |
|
262 |
} |
|
263 |
||
264 |
/* calculate the size of resultant text */ |
|
265 |
static int calcsize(int len, struct flist *l) |
|
266 |
{ |
|
267 |
int outlen = 0, last = 0; |
|
268 |
struct frag *f = l->head; |
|
269 |
||
270 |
while (f != l->tail) { |
|
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
271 |
if (f->start < last || f->end > len) { |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
272 |
if (!PyErr_Occurred()) |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
273 |
PyErr_SetString(mpatch_Error, |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
274 |
"invalid patch"); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
275 |
return -1; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
276 |
} |
72 | 277 |
outlen += f->start - last; |
278 |
last = f->end; |
|
279 |
outlen += f->len; |
|
280 |
f++; |
|
281 |
} |
|
282 |
||
283 |
outlen += len - last; |
|
284 |
return outlen; |
|
285 |
} |
|
286 |
||
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
287 |
static int apply(char *buf, char *orig, int len, struct flist *l) |
72 | 288 |
{ |
289 |
struct frag *f = l->head; |
|
290 |
int last = 0; |
|
291 |
char *p = buf; |
|
292 |
||
293 |
while (f != l->tail) { |
|
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
294 |
if (f->start < last || f->end > len) { |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
295 |
if (!PyErr_Occurred()) |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
296 |
PyErr_SetString(mpatch_Error, |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
297 |
"invalid patch"); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
298 |
return 0; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
299 |
} |
72 | 300 |
memcpy(p, orig + last, f->start - last); |
301 |
p += f->start - last; |
|
302 |
memcpy(p, f->data, f->len); |
|
303 |
last = f->end; |
|
304 |
p += f->len; |
|
305 |
f++; |
|
306 |
} |
|
307 |
memcpy(p, orig + last, len - last); |
|
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
308 |
return 1; |
72 | 309 |
} |
310 |
||
311 |
/* recursively generate a patch of all bins between start and end */ |
|
312 |
static struct flist *fold(PyObject *bins, int start, int end) |
|
313 |
{ |
|
314 |
int len; |
|
315 |
||
316 |
if (start + 1 == end) { |
|
317 |
/* trivial case, output a decoded list */ |
|
318 |
PyObject *tmp = PyList_GetItem(bins, start); |
|
128 | 319 |
if (!tmp) |
320 |
return NULL; |
|
72 | 321 |
return decode(PyString_AsString(tmp), PyString_Size(tmp)); |
322 |
} |
|
323 |
||
324 |
/* divide and conquer, memory management is elsewhere */ |
|
325 |
len = (end - start) / 2; |
|
326 |
return combine(fold(bins, start, start + len), |
|
327 |
fold(bins, start + len, end)); |
|
328 |
} |
|
329 |
||
330 |
static PyObject * |
|
331 |
patches(PyObject *self, PyObject *args) |
|
332 |
{ |
|
333 |
PyObject *text, *bins, *result; |
|
334 |
struct flist *patch; |
|
335 |
char *in, *out; |
|
336 |
int len, outlen; |
|
337 |
||
128 | 338 |
if (!PyArg_ParseTuple(args, "SO:mpatch", &text, &bins)) |
72 | 339 |
return NULL; |
340 |
||
341 |
len = PyList_Size(bins); |
|
342 |
if (!len) { |
|
343 |
/* nothing to do */ |
|
344 |
Py_INCREF(text); |
|
345 |
return text; |
|
346 |
} |
|
347 |
||
348 |
patch = fold(bins, 0, len); |
|
128 | 349 |
if (!patch) |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
350 |
return NULL; |
128 | 351 |
|
72 | 352 |
outlen = calcsize(PyString_Size(text), patch); |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
353 |
if (outlen < 0) { |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
354 |
result = NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
355 |
goto cleanup; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
356 |
} |
72 | 357 |
result = PyString_FromStringAndSize(NULL, outlen); |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
358 |
if (!result) { |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
359 |
result = NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
360 |
goto cleanup; |
128 | 361 |
} |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
362 |
in = PyString_AsString(text); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
363 |
out = PyString_AsString(result); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
364 |
if (!apply(out, in, PyString_Size(text), patch)) { |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
365 |
Py_DECREF(result); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
366 |
result = NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
367 |
} |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
368 |
cleanup: |
72 | 369 |
lfree(patch); |
370 |
return result; |
|
371 |
} |
|
372 |
||
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
373 |
/* calculate size of a patched file directly */ |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
374 |
static PyObject * |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
375 |
patchedsize(PyObject *self, PyObject *args) |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
376 |
{ |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
377 |
long orig, start, end, len, outlen = 0, last = 0; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
378 |
int patchlen; |
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
379 |
char *bin, *binend, *data; |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
380 |
char decode[12]; /* for dealing with alignment issues */ |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
381 |
|
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
382 |
if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen)) |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
383 |
return NULL; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
384 |
|
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
385 |
binend = bin + patchlen; |
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
386 |
data = bin + 12; |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
387 |
|
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
388 |
while (data <= binend) { |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
389 |
memcpy(decode, bin, 12); |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
390 |
start = ntohl(*(uint32_t *)decode); |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
391 |
end = ntohl(*(uint32_t *)(decode + 4)); |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
392 |
len = ntohl(*(uint32_t *)(decode + 8)); |
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
393 |
if (start > end) |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
394 |
break; /* sanity check */ |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
395 |
bin = data + len; |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
396 |
if (bin < data) |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
397 |
break; /* big data + big (bogus) len can wrap around */ |
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
398 |
data = bin + 12; |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
399 |
outlen += start - last; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
400 |
last = end; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
401 |
outlen += len; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
402 |
} |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
403 |
|
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
404 |
if (bin != binend) { |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
405 |
if (!PyErr_Occurred()) |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
406 |
PyErr_SetString(mpatch_Error, "patch cannot be decoded"); |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
407 |
return NULL; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
408 |
} |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
409 |
|
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
410 |
outlen += orig - last; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
411 |
return Py_BuildValue("l", outlen); |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
412 |
} |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
413 |
|
72 | 414 |
static PyMethodDef methods[] = { |
415 |
{"patches", patches, METH_VARARGS, "apply a series of patches\n"}, |
|
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
416 |
{"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"}, |
72 | 417 |
{NULL, NULL} |
418 |
}; |
|
419 |
||
420 |
PyMODINIT_FUNC |
|
421 |
initmpatch(void) |
|
422 |
{ |
|
423 |
Py_InitModule3("mpatch", methods, mpatch_doc); |
|
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
424 |
mpatch_Error = PyErr_NewException("mpatch.mpatchError", NULL, NULL); |
72 | 425 |
} |
426 |