author | Yuya Nishihara <yuya@tcha.org> |
Sat, 21 May 2016 17:06:39 +0900 | |
changeset 29531 | b439a2a268eb |
parent 29444 | 284d742e5611 |
child 29691 | e9a0bcc9314d |
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 |
||
16758
9a8ab5c47f84
mpatch: use Py_ssize_t for string length
Adrian Buehlmann <adrian@cadifra.com>
parents:
16757
diff
changeset
|
23 |
#define PY_SSIZE_T_CLEAN |
72 | 24 |
#include <Python.h> |
25 |
#include <stdlib.h> |
|
26 |
#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
|
27 |
|
11360
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
28 |
#include "util.h" |
29444
284d742e5611
internals: move the bitmanipulation routines into its own file
Maciej Fijalkowski <fijall@gmail.com>
parents:
28782
diff
changeset
|
29 |
#include "bitmanipulation.h" |
11360
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
30 |
|
72 | 31 |
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
|
32 |
static PyObject *mpatch_Error; |
72 | 33 |
|
34 |
struct frag { |
|
35 |
int start, end, len; |
|
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
36 |
const char *data; |
72 | 37 |
}; |
38 |
||
39 |
struct flist { |
|
40 |
struct frag *base, *head, *tail; |
|
41 |
}; |
|
42 |
||
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
43 |
static struct flist *lalloc(Py_ssize_t size) |
72 | 44 |
{ |
128 | 45 |
struct flist *a = NULL; |
72 | 46 |
|
3138
cc856c4d91ca
mpatch: Fix for malloc corner case on AIX
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
47 |
if (size < 1) |
cc856c4d91ca
mpatch: Fix for malloc corner case on AIX
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
48 |
size = 1; |
cc856c4d91ca
mpatch: Fix for malloc corner case on AIX
Matt Mackall <mpm@selenic.com>
parents:
2859
diff
changeset
|
49 |
|
1978
10606ee61107
do proper typecasting on malloc() and calloc() calls
TK Soh <teekaysoh@yahoo.com>
parents:
1746
diff
changeset
|
50 |
a = (struct flist *)malloc(sizeof(struct flist)); |
128 | 51 |
if (a) { |
1978
10606ee61107
do proper typecasting on malloc() and calloc() calls
TK Soh <teekaysoh@yahoo.com>
parents:
1746
diff
changeset
|
52 |
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
|
53 |
if (a->base) { |
128 | 54 |
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
|
55 |
return a; |
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
56 |
} |
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
57 |
free(a); |
8f9660c568b8
Set correct exception for another possible malloc error in mpatch.c
Thomas Arendsen Hein <thomas@intevation.de>
parents:
1978
diff
changeset
|
58 |
a = NULL; |
128 | 59 |
} |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
60 |
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
|
61 |
PyErr_NoMemory(); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
62 |
return NULL; |
72 | 63 |
} |
64 |
||
65 |
static void lfree(struct flist *a) |
|
66 |
{ |
|
128 | 67 |
if (a) { |
68 |
free(a->base); |
|
69 |
free(a); |
|
70 |
} |
|
72 | 71 |
} |
72 |
||
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
73 |
static Py_ssize_t lsize(struct flist *a) |
72 | 74 |
{ |
75 |
return a->tail - a->head; |
|
76 |
} |
|
77 |
||
78 |
/* move hunks in source that are less cut to dest, compensating |
|
79 |
for changes in offset. the last hunk may be split if necessary. |
|
80 |
*/ |
|
81 |
static int gather(struct flist *dest, struct flist *src, int cut, int offset) |
|
82 |
{ |
|
83 |
struct frag *d = dest->tail, *s = src->head; |
|
84 |
int postend, c, l; |
|
85 |
||
86 |
while (s != src->tail) { |
|
87 |
if (s->start + offset >= cut) |
|
82 | 88 |
break; /* we've gone far enough */ |
72 | 89 |
|
90 |
postend = offset + s->start + s->len; |
|
91 |
if (postend <= cut) { |
|
92 |
/* save this hunk */ |
|
93 |
offset += s->start + s->len - s->end; |
|
94 |
*d++ = *s++; |
|
95 |
} |
|
96 |
else { |
|
97 |
/* break up this hunk */ |
|
98 |
c = cut - offset; |
|
99 |
if (s->end < c) |
|
100 |
c = s->end; |
|
101 |
l = cut - offset - s->start; |
|
102 |
if (s->len < l) |
|
103 |
l = s->len; |
|
104 |
||
105 |
offset += s->start + l - c; |
|
106 |
||
107 |
d->start = s->start; |
|
108 |
d->end = c; |
|
109 |
d->len = l; |
|
110 |
d->data = s->data; |
|
111 |
d++; |
|
112 |
s->start = c; |
|
113 |
s->len = s->len - l; |
|
114 |
s->data = s->data + l; |
|
115 |
||
82 | 116 |
break; |
72 | 117 |
} |
118 |
} |
|
119 |
||
120 |
dest->tail = d; |
|
121 |
src->head = s; |
|
122 |
return offset; |
|
123 |
} |
|
124 |
||
125 |
/* like gather, but with no output list */ |
|
126 |
static int discard(struct flist *src, int cut, int offset) |
|
127 |
{ |
|
128 |
struct frag *s = src->head; |
|
129 |
int postend, c, l; |
|
130 |
||
131 |
while (s != src->tail) { |
|
132 |
if (s->start + offset >= cut) |
|
82 | 133 |
break; |
72 | 134 |
|
135 |
postend = offset + s->start + s->len; |
|
136 |
if (postend <= cut) { |
|
137 |
offset += s->start + s->len - s->end; |
|
138 |
s++; |
|
139 |
} |
|
140 |
else { |
|
141 |
c = cut - offset; |
|
142 |
if (s->end < c) |
|
143 |
c = s->end; |
|
144 |
l = cut - offset - s->start; |
|
145 |
if (s->len < l) |
|
146 |
l = s->len; |
|
147 |
||
148 |
offset += s->start + l - c; |
|
149 |
s->start = c; |
|
150 |
s->len = s->len - l; |
|
151 |
s->data = s->data + l; |
|
152 |
||
82 | 153 |
break; |
72 | 154 |
} |
155 |
} |
|
156 |
||
157 |
src->head = s; |
|
158 |
return offset; |
|
159 |
} |
|
160 |
||
161 |
/* combine hunk lists a and b, while adjusting b for offset changes in a/ |
|
162 |
this deletes a and b and returns the resultant list. */ |
|
163 |
static struct flist *combine(struct flist *a, struct flist *b) |
|
164 |
{ |
|
128 | 165 |
struct flist *c = NULL; |
166 |
struct frag *bh, *ct; |
|
72 | 167 |
int offset = 0, post; |
168 |
||
128 | 169 |
if (a && b) |
170 |
c = lalloc((lsize(a) + lsize(b)) * 2); |
|
171 |
||
172 |
if (c) { |
|
72 | 173 |
|
128 | 174 |
for (bh = b->head; bh != b->tail; bh++) { |
175 |
/* save old hunks */ |
|
176 |
offset = gather(c, a, bh->start, offset); |
|
72 | 177 |
|
128 | 178 |
/* discard replaced hunks */ |
179 |
post = discard(a, bh->end, offset); |
|
72 | 180 |
|
128 | 181 |
/* insert new hunk */ |
182 |
ct = c->tail; |
|
183 |
ct->start = bh->start - offset; |
|
184 |
ct->end = bh->end - post; |
|
185 |
ct->len = bh->len; |
|
186 |
ct->data = bh->data; |
|
187 |
c->tail++; |
|
188 |
offset = post; |
|
189 |
} |
|
190 |
||
191 |
/* hold on to tail from a */ |
|
192 |
memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a)); |
|
193 |
c->tail += lsize(a); |
|
72 | 194 |
} |
195 |
||
196 |
lfree(a); |
|
197 |
lfree(b); |
|
198 |
return c; |
|
199 |
} |
|
200 |
||
201 |
/* decode a binary patch into a hunk list */ |
|
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
202 |
static struct flist *decode(const char *bin, Py_ssize_t len) |
72 | 203 |
{ |
204 |
struct flist *l; |
|
205 |
struct frag *lt; |
|
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
206 |
int pos = 0; |
72 | 207 |
|
208 |
/* assume worst case size, we won't have many of these lists */ |
|
28656
b6ed2505d6cf
parsers: fix list sizing rounding error (SEC)
Matt Mackall <mpm@selenic.com>
parents:
20167
diff
changeset
|
209 |
l = lalloc(len / 12 + 1); |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
210 |
if (!l) |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
211 |
return NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
212 |
|
72 | 213 |
lt = l->tail; |
214 |
||
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
215 |
while (pos >= 0 && pos < len) { |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
216 |
lt->start = getbe32(bin + pos); |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
217 |
lt->end = getbe32(bin + pos + 4); |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
218 |
lt->len = getbe32(bin + pos + 8); |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
219 |
lt->data = bin + pos + 12; |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
220 |
pos += 12 + lt->len; |
28657
b9714d958e89
parsers: detect short records (SEC)
Matt Mackall <mpm@selenic.com>
parents:
28656
diff
changeset
|
221 |
if (lt->start > lt->end || lt->len < 0) |
b9714d958e89
parsers: detect short records (SEC)
Matt Mackall <mpm@selenic.com>
parents:
28656
diff
changeset
|
222 |
break; /* sanity check */ |
72 | 223 |
lt++; |
224 |
} |
|
225 |
||
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
226 |
if (pos != len) { |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
227 |
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
|
228 |
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
|
229 |
lfree(l); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
230 |
return NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
231 |
} |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
232 |
|
72 | 233 |
l->tail = lt; |
234 |
return l; |
|
235 |
} |
|
236 |
||
237 |
/* calculate the size of resultant text */ |
|
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
238 |
static Py_ssize_t calcsize(Py_ssize_t len, struct flist *l) |
72 | 239 |
{ |
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
240 |
Py_ssize_t outlen = 0, last = 0; |
72 | 241 |
struct frag *f = l->head; |
242 |
||
243 |
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
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
"invalid patch"); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
248 |
return -1; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
249 |
} |
72 | 250 |
outlen += f->start - last; |
251 |
last = f->end; |
|
252 |
outlen += f->len; |
|
253 |
f++; |
|
254 |
} |
|
255 |
||
256 |
outlen += len - last; |
|
257 |
return outlen; |
|
258 |
} |
|
259 |
||
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
260 |
static int apply(char *buf, const char *orig, Py_ssize_t len, struct flist *l) |
72 | 261 |
{ |
262 |
struct frag *f = l->head; |
|
263 |
int last = 0; |
|
264 |
char *p = buf; |
|
265 |
||
266 |
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
|
267 |
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
|
268 |
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
|
269 |
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
|
270 |
"invalid patch"); |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
271 |
return 0; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
272 |
} |
72 | 273 |
memcpy(p, orig + last, f->start - last); |
274 |
p += f->start - last; |
|
275 |
memcpy(p, f->data, f->len); |
|
276 |
last = f->end; |
|
277 |
p += f->len; |
|
278 |
f++; |
|
279 |
} |
|
280 |
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
|
281 |
return 1; |
72 | 282 |
} |
283 |
||
284 |
/* recursively generate a patch of all bins between start and end */ |
|
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
285 |
static struct flist *fold(PyObject *bins, Py_ssize_t start, Py_ssize_t end) |
72 | 286 |
{ |
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
287 |
Py_ssize_t len, blen; |
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
288 |
const char *buffer; |
72 | 289 |
|
290 |
if (start + 1 == end) { |
|
291 |
/* trivial case, output a decoded list */ |
|
292 |
PyObject *tmp = PyList_GetItem(bins, start); |
|
128 | 293 |
if (!tmp) |
294 |
return NULL; |
|
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
295 |
if (PyObject_AsCharBuffer(tmp, &buffer, &blen)) |
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
296 |
return NULL; |
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
297 |
return decode(buffer, blen); |
72 | 298 |
} |
299 |
||
300 |
/* divide and conquer, memory management is elsewhere */ |
|
301 |
len = (end - start) / 2; |
|
302 |
return combine(fold(bins, start, start + len), |
|
303 |
fold(bins, start + len, end)); |
|
304 |
} |
|
305 |
||
306 |
static PyObject * |
|
307 |
patches(PyObject *self, PyObject *args) |
|
308 |
{ |
|
309 |
PyObject *text, *bins, *result; |
|
310 |
struct flist *patch; |
|
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
311 |
const char *in; |
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
312 |
char *out; |
16757
923bd97b86a0
mpatch: use Py_ssize_t
Adrian Buehlmann <adrian@cadifra.com>
parents:
16437
diff
changeset
|
313 |
Py_ssize_t len, outlen, inlen; |
72 | 314 |
|
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
315 |
if (!PyArg_ParseTuple(args, "OO:mpatch", &text, &bins)) |
72 | 316 |
return NULL; |
317 |
||
318 |
len = PyList_Size(bins); |
|
319 |
if (!len) { |
|
320 |
/* nothing to do */ |
|
321 |
Py_INCREF(text); |
|
322 |
return text; |
|
323 |
} |
|
324 |
||
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
325 |
if (PyObject_AsCharBuffer(text, &in, &inlen)) |
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
326 |
return NULL; |
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
327 |
|
72 | 328 |
patch = fold(bins, 0, len); |
128 | 329 |
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
|
330 |
return NULL; |
128 | 331 |
|
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
332 |
outlen = calcsize(inlen, patch); |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
333 |
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
|
334 |
result = NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
335 |
goto cleanup; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
336 |
} |
11360
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
337 |
result = PyBytes_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
|
338 |
if (!result) { |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
339 |
result = NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
340 |
goto cleanup; |
128 | 341 |
} |
11360
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
342 |
out = PyBytes_AsString(result); |
5444
a0952e4e52eb
mpatch: allow buffer objects for input
Matt Mackall <mpm@selenic.com>
parents:
4377
diff
changeset
|
343 |
if (!apply(out, in, inlen, patch)) { |
1722
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
344 |
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
|
345 |
result = NULL; |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
346 |
} |
681c5c211b92
catch errors and throw exception with invalid binary patch data
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
597
diff
changeset
|
347 |
cleanup: |
72 | 348 |
lfree(patch); |
349 |
return result; |
|
350 |
} |
|
351 |
||
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
352 |
/* calculate size of a patched file directly */ |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
353 |
static PyObject * |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
354 |
patchedsize(PyObject *self, PyObject *args) |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
355 |
{ |
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
356 |
long orig, start, end, len, outlen = 0, last = 0, pos = 0; |
16758
9a8ab5c47f84
mpatch: use Py_ssize_t for string length
Adrian Buehlmann <adrian@cadifra.com>
parents:
16757
diff
changeset
|
357 |
Py_ssize_t patchlen; |
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
358 |
char *bin; |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
359 |
|
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
360 |
if (!PyArg_ParseTuple(args, "ls#", &orig, &bin, &patchlen)) |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
361 |
return NULL; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
362 |
|
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
363 |
while (pos >= 0 && pos < patchlen) { |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
364 |
start = getbe32(bin + pos); |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
365 |
end = getbe32(bin + pos + 4); |
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
366 |
len = getbe32(bin + pos + 8); |
4358
11dc22eb8e8d
Fix segfaults when parsing bdiff hunks in mpatch.decode() and .patchedsize()
Thomas Arendsen Hein <thomas@intevation.de>
parents:
3138
diff
changeset
|
367 |
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
|
368 |
break; /* sanity check */ |
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
369 |
pos += 12 + len; |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
370 |
outlen += start - last; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
371 |
last = end; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
372 |
outlen += len; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
373 |
} |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
374 |
|
20167
09e41ac6289d
mpatch: rewrite pointer overflow checks
Matt Mackall <mpm@selenic.com>
parents:
16758
diff
changeset
|
375 |
if (pos != patchlen) { |
2078
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
376 |
if (!PyErr_Occurred()) |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
377 |
PyErr_SetString(mpatch_Error, "patch cannot be decoded"); |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
378 |
return NULL; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
379 |
} |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
380 |
|
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
381 |
outlen += orig - last; |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
382 |
return Py_BuildValue("l", outlen); |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
383 |
} |
441ea218414e
Fill in the uncompressed size during revlog.addgroup
mason@suse.com
parents:
1978
diff
changeset
|
384 |
|
72 | 385 |
static PyMethodDef methods[] = { |
386 |
{"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
|
387 |
{"patchedsize", patchedsize, METH_VARARGS, "calculed patched size\n"}, |
72 | 388 |
{NULL, NULL} |
389 |
}; |
|
390 |
||
11360
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
391 |
#ifdef IS_PY3K |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
392 |
static struct PyModuleDef mpatch_module = { |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
393 |
PyModuleDef_HEAD_INIT, |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
394 |
"mpatch", |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
395 |
mpatch_doc, |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
396 |
-1, |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
397 |
methods |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
398 |
}; |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
399 |
|
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
400 |
PyMODINIT_FUNC PyInit_mpatch(void) |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
401 |
{ |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
402 |
PyObject *m; |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
403 |
|
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
404 |
m = PyModule_Create(&mpatch_module); |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
405 |
if (m == NULL) |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
406 |
return NULL; |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
407 |
|
28782
f736f98e16ca
mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents:
28657
diff
changeset
|
408 |
mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError", |
f736f98e16ca
mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents:
28657
diff
changeset
|
409 |
NULL, NULL); |
11360
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
410 |
Py_INCREF(mpatch_Error); |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
411 |
PyModule_AddObject(m, "mpatchError", mpatch_Error); |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
412 |
|
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
413 |
return m; |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
414 |
} |
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
415 |
#else |
72 | 416 |
PyMODINIT_FUNC |
417 |
initmpatch(void) |
|
418 |
{ |
|
419 |
Py_InitModule3("mpatch", methods, mpatch_doc); |
|
28782
f736f98e16ca
mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents:
28657
diff
changeset
|
420 |
mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError", |
f736f98e16ca
mpatch: unify mpatchError (issue5182)
timeless <timeless@mozdev.org>
parents:
28657
diff
changeset
|
421 |
NULL, NULL); |
72 | 422 |
} |
11360
2ac98313b26c
mpatch.c: Added preliminary support for py3k.
Renato Cunha <renatoc@gmail.com>
parents:
10282
diff
changeset
|
423 |
#endif |