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