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