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