author | mpm@selenic.com |
Tue, 21 Jun 2005 18:54:44 -0800 | |
changeset 411 | 9e9f7ab43ce2 |
parent 410 | 7c678976df3e |
child 429 | 688d03d6997a |
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 |
||
17 |
Copyright 2005 Matt Mackall <mpm@selenic.com> |
|
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> |
|
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
26 |
#ifdef _WIN32 |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
27 |
|
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
28 |
typedef unsigned long uint32_t; |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
29 |
|
411
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
30 |
static uint32_t ntohl(uint32_t x) |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
31 |
{ |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
32 |
return ((x & 0x000000ffUL) << 24) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
33 |
((x & 0x0000ff00UL) << 8) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
34 |
((x & 0x00ff0000UL) >> 8) | |
9e9f7ab43ce2
Add 'other OS' bits to bdiff.c / style cleanups
mpm@selenic.com
parents:
410
diff
changeset
|
35 |
((x & 0xff000000UL) >> 24); |
410
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
36 |
} |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
37 |
|
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
38 |
#else |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
39 |
#include <netinet/in.h> |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
40 |
#include <sys/types.h> |
7c678976df3e
Make mpatch.c compilable under the other `OS'
mpm@selenic.com
parents:
384
diff
changeset
|
41 |
#endif |
72 | 42 |
|
43 |
static char mpatch_doc[] = "Efficient binary patching."; |
|
44 |
||
45 |
struct frag { |
|
46 |
int start, end, len; |
|
47 |
char *data; |
|
48 |
}; |
|
49 |
||
50 |
struct flist { |
|
51 |
struct frag *base, *head, *tail; |
|
52 |
}; |
|
53 |
||
54 |
static struct flist *lalloc(int size) |
|
55 |
{ |
|
128 | 56 |
struct flist *a = NULL; |
72 | 57 |
|
58 |
a = malloc(sizeof(struct flist)); |
|
128 | 59 |
if (a) { |
60 |
a->base = malloc(sizeof(struct frag) * size); |
|
282 | 61 |
if (!a->base) { |
128 | 62 |
free(a); |
282 | 63 |
a = NULL; |
64 |
} else |
|
128 | 65 |
a->head = a->tail = a->base; |
66 |
} |
|
72 | 67 |
return a; |
68 |
} |
|
69 |
||
70 |
static void lfree(struct flist *a) |
|
71 |
{ |
|
128 | 72 |
if (a) { |
73 |
free(a->base); |
|
74 |
free(a); |
|
75 |
} |
|
72 | 76 |
} |
77 |
||
78 |
static int lsize(struct flist *a) |
|
79 |
{ |
|
80 |
return a->tail - a->head; |
|
81 |
} |
|
82 |
||
83 |
/* move hunks in source that are less cut to dest, compensating |
|
84 |
for changes in offset. the last hunk may be split if necessary. |
|
85 |
*/ |
|
86 |
static int gather(struct flist *dest, struct flist *src, int cut, int offset) |
|
87 |
{ |
|
88 |
struct frag *d = dest->tail, *s = src->head; |
|
89 |
int postend, c, l; |
|
90 |
||
91 |
while (s != src->tail) { |
|
92 |
if (s->start + offset >= cut) |
|
82 | 93 |
break; /* we've gone far enough */ |
72 | 94 |
|
95 |
postend = offset + s->start + s->len; |
|
96 |
if (postend <= cut) { |
|
97 |
/* save this hunk */ |
|
98 |
offset += s->start + s->len - s->end; |
|
99 |
*d++ = *s++; |
|
100 |
} |
|
101 |
else { |
|
102 |
/* break up this hunk */ |
|
103 |
c = cut - offset; |
|
104 |
if (s->end < c) |
|
105 |
c = s->end; |
|
106 |
l = cut - offset - s->start; |
|
107 |
if (s->len < l) |
|
108 |
l = s->len; |
|
109 |
||
110 |
offset += s->start + l - c; |
|
111 |
||
112 |
d->start = s->start; |
|
113 |
d->end = c; |
|
114 |
d->len = l; |
|
115 |
d->data = s->data; |
|
116 |
d++; |
|
117 |
s->start = c; |
|
118 |
s->len = s->len - l; |
|
119 |
s->data = s->data + l; |
|
120 |
||
82 | 121 |
break; |
72 | 122 |
} |
123 |
} |
|
124 |
||
125 |
dest->tail = d; |
|
126 |
src->head = s; |
|
127 |
return offset; |
|
128 |
} |
|
129 |
||
130 |
/* like gather, but with no output list */ |
|
131 |
static int discard(struct flist *src, int cut, int offset) |
|
132 |
{ |
|
133 |
struct frag *s = src->head; |
|
134 |
int postend, c, l; |
|
135 |
||
136 |
while (s != src->tail) { |
|
137 |
if (s->start + offset >= cut) |
|
82 | 138 |
break; |
72 | 139 |
|
140 |
postend = offset + s->start + s->len; |
|
141 |
if (postend <= cut) { |
|
142 |
offset += s->start + s->len - s->end; |
|
143 |
s++; |
|
144 |
} |
|
145 |
else { |
|
146 |
c = cut - offset; |
|
147 |
if (s->end < c) |
|
148 |
c = s->end; |
|
149 |
l = cut - offset - s->start; |
|
150 |
if (s->len < l) |
|
151 |
l = s->len; |
|
152 |
||
153 |
offset += s->start + l - c; |
|
154 |
s->start = c; |
|
155 |
s->len = s->len - l; |
|
156 |
s->data = s->data + l; |
|
157 |
||
82 | 158 |
break; |
72 | 159 |
} |
160 |
} |
|
161 |
||
162 |
src->head = s; |
|
163 |
return offset; |
|
164 |
} |
|
165 |
||
166 |
/* combine hunk lists a and b, while adjusting b for offset changes in a/ |
|
167 |
this deletes a and b and returns the resultant list. */ |
|
168 |
static struct flist *combine(struct flist *a, struct flist *b) |
|
169 |
{ |
|
128 | 170 |
struct flist *c = NULL; |
171 |
struct frag *bh, *ct; |
|
72 | 172 |
int offset = 0, post; |
173 |
||
128 | 174 |
if (a && b) |
175 |
c = lalloc((lsize(a) + lsize(b)) * 2); |
|
176 |
||
177 |
if (c) { |
|
72 | 178 |
|
128 | 179 |
for (bh = b->head; bh != b->tail; bh++) { |
180 |
/* save old hunks */ |
|
181 |
offset = gather(c, a, bh->start, offset); |
|
72 | 182 |
|
128 | 183 |
/* discard replaced hunks */ |
184 |
post = discard(a, bh->end, offset); |
|
72 | 185 |
|
128 | 186 |
/* insert new hunk */ |
187 |
ct = c->tail; |
|
188 |
ct->start = bh->start - offset; |
|
189 |
ct->end = bh->end - post; |
|
190 |
ct->len = bh->len; |
|
191 |
ct->data = bh->data; |
|
192 |
c->tail++; |
|
193 |
offset = post; |
|
194 |
} |
|
195 |
||
196 |
/* hold on to tail from a */ |
|
197 |
memcpy(c->tail, a->head, sizeof(struct frag) * lsize(a)); |
|
198 |
c->tail += lsize(a); |
|
72 | 199 |
} |
200 |
||
201 |
lfree(a); |
|
202 |
lfree(b); |
|
203 |
return c; |
|
204 |
} |
|
205 |
||
206 |
/* decode a binary patch into a hunk list */ |
|
207 |
static struct flist *decode(char *bin, int len) |
|
208 |
{ |
|
209 |
struct flist *l; |
|
210 |
struct frag *lt; |
|
211 |
char *end = bin + len; |
|
384
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
212 |
char decode[12]; /* for dealing with alignment issues */ |
72 | 213 |
|
214 |
/* assume worst case size, we won't have many of these lists */ |
|
215 |
l = lalloc(len / 12); |
|
216 |
lt = l->tail; |
|
217 |
||
218 |
while (bin < end) { |
|
384
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
219 |
memcpy(decode, bin, 12); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
220 |
lt->start = ntohl(*(uint32_t *)decode); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
221 |
lt->end = ntohl(*(uint32_t *)(decode + 4)); |
a29decbf7475
mpatch: attempt to handle unpack alignment issues on Solaris
mpm@selenic.com
parents:
282
diff
changeset
|
222 |
lt->len = ntohl(*(uint32_t *)(decode + 8)); |
72 | 223 |
lt->data = bin + 12; |
224 |
bin += 12 + lt->len; |
|
225 |
lt++; |
|
226 |
} |
|
227 |
||
228 |
l->tail = lt; |
|
229 |
return l; |
|
230 |
} |
|
231 |
||
232 |
/* calculate the size of resultant text */ |
|
233 |
static int calcsize(int len, struct flist *l) |
|
234 |
{ |
|
235 |
int outlen = 0, last = 0; |
|
236 |
struct frag *f = l->head; |
|
237 |
||
238 |
while (f != l->tail) { |
|
239 |
outlen += f->start - last; |
|
240 |
last = f->end; |
|
241 |
outlen += f->len; |
|
242 |
f++; |
|
243 |
} |
|
244 |
||
245 |
outlen += len - last; |
|
246 |
return outlen; |
|
247 |
} |
|
248 |
||
249 |
static void apply(char *buf, char *orig, int len, struct flist *l) |
|
250 |
{ |
|
251 |
struct frag *f = l->head; |
|
252 |
int last = 0; |
|
253 |
char *p = buf; |
|
254 |
||
255 |
while (f != l->tail) { |
|
256 |
memcpy(p, orig + last, f->start - last); |
|
257 |
p += f->start - last; |
|
258 |
memcpy(p, f->data, f->len); |
|
259 |
last = f->end; |
|
260 |
p += f->len; |
|
261 |
f++; |
|
262 |
} |
|
263 |
memcpy(p, orig + last, len - last); |
|
264 |
} |
|
265 |
||
266 |
/* recursively generate a patch of all bins between start and end */ |
|
267 |
static struct flist *fold(PyObject *bins, int start, int end) |
|
268 |
{ |
|
269 |
int len; |
|
270 |
||
271 |
if (start + 1 == end) { |
|
272 |
/* trivial case, output a decoded list */ |
|
273 |
PyObject *tmp = PyList_GetItem(bins, start); |
|
128 | 274 |
if (!tmp) |
275 |
return NULL; |
|
72 | 276 |
return decode(PyString_AsString(tmp), PyString_Size(tmp)); |
277 |
} |
|
278 |
||
279 |
/* divide and conquer, memory management is elsewhere */ |
|
280 |
len = (end - start) / 2; |
|
281 |
return combine(fold(bins, start, start + len), |
|
282 |
fold(bins, start + len, end)); |
|
283 |
} |
|
284 |
||
285 |
static PyObject * |
|
286 |
patches(PyObject *self, PyObject *args) |
|
287 |
{ |
|
288 |
PyObject *text, *bins, *result; |
|
289 |
struct flist *patch; |
|
290 |
char *in, *out; |
|
291 |
int len, outlen; |
|
292 |
||
128 | 293 |
if (!PyArg_ParseTuple(args, "SO:mpatch", &text, &bins)) |
72 | 294 |
return NULL; |
295 |
||
296 |
len = PyList_Size(bins); |
|
297 |
if (!len) { |
|
298 |
/* nothing to do */ |
|
299 |
Py_INCREF(text); |
|
300 |
return text; |
|
301 |
} |
|
302 |
||
303 |
patch = fold(bins, 0, len); |
|
128 | 304 |
if (!patch) |
305 |
return PyErr_NoMemory(); |
|
306 |
||
72 | 307 |
outlen = calcsize(PyString_Size(text), patch); |
308 |
result = PyString_FromStringAndSize(NULL, outlen); |
|
128 | 309 |
if (result) { |
310 |
in = PyString_AsString(text); |
|
311 |
out = PyString_AsString(result); |
|
312 |
apply(out, in, PyString_Size(text), patch); |
|
313 |
} |
|
314 |
||
72 | 315 |
lfree(patch); |
316 |
return result; |
|
317 |
} |
|
318 |
||
319 |
static PyMethodDef methods[] = { |
|
320 |
{"patches", patches, METH_VARARGS, "apply a series of patches\n"}, |
|
321 |
{NULL, NULL} |
|
322 |
}; |
|
323 |
||
324 |
PyMODINIT_FUNC |
|
325 |
initmpatch(void) |
|
326 |
{ |
|
327 |
Py_InitModule3("mpatch", methods, mpatch_doc); |
|
328 |
} |
|
329 |