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