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