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