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