##// END OF EJS Templates
Allow Mercurial to build on HP-UX 11...
tksoh@users.sourceforge.net -
r867:0cd2ee61 default
parent child Browse files
Show More
@@ -1,350 +1,355
1 /*
1 /*
2 bdiff.c - efficient binary diff extension for Mercurial
2 bdiff.c - efficient binary diff extension for Mercurial
3
3
4 Copyright 2005 Matt Mackall <mpm@selenic.com>
4 Copyright 2005 Matt Mackall <mpm@selenic.com>
5
5
6 This software may be used and distributed according to the terms of
6 This software may be used and distributed according to the terms of
7 the GNU General Public License, incorporated herein by reference.
7 the GNU General Public License, incorporated herein by reference.
8
8
9 Based roughly on Python difflib
9 Based roughly on Python difflib
10 */
10 */
11
11
12 #include <Python.h>
12 #include <Python.h>
13 #include <stdlib.h>
13 #include <stdlib.h>
14 #include <string.h>
14 #include <string.h>
15
16 #ifdef __hpux
17 #define inline
18 #endif
19
15 #ifdef _WIN32
20 #ifdef _WIN32
16 #ifdef _MSC_VER
21 #ifdef _MSC_VER
17 #define inline __inline
22 #define inline __inline
18 typedef unsigned long uint32_t;
23 typedef unsigned long uint32_t;
19 #else
24 #else
20 #include <stdint.h>
25 #include <stdint.h>
21 #endif
26 #endif
22 static uint32_t htonl(uint32_t x)
27 static uint32_t htonl(uint32_t x)
23 {
28 {
24 return ((x & 0x000000ffUL) << 24) |
29 return ((x & 0x000000ffUL) << 24) |
25 ((x & 0x0000ff00UL) << 8) |
30 ((x & 0x0000ff00UL) << 8) |
26 ((x & 0x00ff0000UL) >> 8) |
31 ((x & 0x00ff0000UL) >> 8) |
27 ((x & 0xff000000UL) >> 24);
32 ((x & 0xff000000UL) >> 24);
28 }
33 }
29 #else
34 #else
30 #include <sys/types.h>
35 #include <sys/types.h>
31 #include <arpa/inet.h>
36 #include <arpa/inet.h>
32 #endif
37 #endif
33
38
34 struct line {
39 struct line {
35 int h, len, n, e;
40 int h, len, n, e;
36 const char *l;
41 const char *l;
37 };
42 };
38
43
39 struct pos {
44 struct pos {
40 int pos, len;
45 int pos, len;
41 };
46 };
42
47
43 struct hunk {
48 struct hunk {
44 int a1, a2, b1, b2;
49 int a1, a2, b1, b2;
45 };
50 };
46
51
47 struct hunklist {
52 struct hunklist {
48 struct hunk *base, *head;
53 struct hunk *base, *head;
49 };
54 };
50
55
51 static __inline uint32_t rol32(uint32_t word, unsigned int shift)
56 static __inline uint32_t rol32(uint32_t word, unsigned int shift)
52 {
57 {
53 return (word << shift) | (word >> (32 - shift));
58 return (word << shift) | (word >> (32 - shift));
54 }
59 }
55
60
56 int splitlines(const char *a, int len, struct line **lr)
61 int splitlines(const char *a, int len, struct line **lr)
57 {
62 {
58 int h, i;
63 int h, i;
59 const char *p, *b = a;
64 const char *p, *b = a;
60 struct line *l;
65 struct line *l;
61
66
62 /* count the lines */
67 /* count the lines */
63 i = 1; /* extra line for sentinel */
68 i = 1; /* extra line for sentinel */
64 for (p = a; p < a + len; p++)
69 for (p = a; p < a + len; p++)
65 if (*p == '\n' || p == a + len - 1)
70 if (*p == '\n' || p == a + len - 1)
66 i++;
71 i++;
67
72
68 *lr = l = malloc(sizeof(struct line) * i);
73 *lr = l = malloc(sizeof(struct line) * i);
69 if (!l)
74 if (!l)
70 return -1;
75 return -1;
71
76
72 /* build the line array and calculate hashes */
77 /* build the line array and calculate hashes */
73 h = 0;
78 h = 0;
74 for (p = a; p < a + len; p++) {
79 for (p = a; p < a + len; p++) {
75 h = *p + rol32(h, 7); /* a simple hash from GNU diff */
80 h = *p + rol32(h, 7); /* a simple hash from GNU diff */
76 if (*p == '\n' || p == a + len - 1) {
81 if (*p == '\n' || p == a + len - 1) {
77 l->len = p - b + 1;
82 l->len = p - b + 1;
78 l->h = h * l->len;
83 l->h = h * l->len;
79 l->l = b;
84 l->l = b;
80 l->n = -1;
85 l->n = -1;
81 l++;
86 l++;
82 b = p + 1;
87 b = p + 1;
83 h = 0;
88 h = 0;
84 }
89 }
85 }
90 }
86
91
87 /* set up a sentinel */
92 /* set up a sentinel */
88 l->h = l->len = 0;
93 l->h = l->len = 0;
89 l->l = a + len;
94 l->l = a + len;
90 return i - 1;
95 return i - 1;
91 }
96 }
92
97
93 int inline cmp(struct line *a, struct line *b)
98 int inline cmp(struct line *a, struct line *b)
94 {
99 {
95 return a->h != b->h || a->len != b->len || memcmp(a->l, b->l, a->len);
100 return a->h != b->h || a->len != b->len || memcmp(a->l, b->l, a->len);
96 }
101 }
97
102
98 static int equatelines(struct line *a, int an, struct line *b, int bn)
103 static int equatelines(struct line *a, int an, struct line *b, int bn)
99 {
104 {
100 int i, j, buckets = 1, t;
105 int i, j, buckets = 1, t;
101 struct pos *h;
106 struct pos *h;
102
107
103 /* build a hash table of the next highest power of 2 */
108 /* build a hash table of the next highest power of 2 */
104 while (buckets < bn + 1)
109 while (buckets < bn + 1)
105 buckets *= 2;
110 buckets *= 2;
106
111
107 h = malloc(buckets * sizeof(struct pos));
112 h = malloc(buckets * sizeof(struct pos));
108 buckets = buckets - 1;
113 buckets = buckets - 1;
109 if (!h)
114 if (!h)
110 return 0;
115 return 0;
111
116
112 /* clear the hash table */
117 /* clear the hash table */
113 for (i = 0; i <= buckets; i++) {
118 for (i = 0; i <= buckets; i++) {
114 h[i].pos = -1;
119 h[i].pos = -1;
115 h[i].len = 0;
120 h[i].len = 0;
116 }
121 }
117
122
118 /* add lines to the hash table chains */
123 /* add lines to the hash table chains */
119 for (i = bn - 1; i >= 0; i--) {
124 for (i = bn - 1; i >= 0; i--) {
120 /* find the equivalence class */
125 /* find the equivalence class */
121 for (j = b[i].h & buckets; h[j].pos != -1;
126 for (j = b[i].h & buckets; h[j].pos != -1;
122 j = (j + 1) & buckets)
127 j = (j + 1) & buckets)
123 if (!cmp(b + i, b + h[j].pos))
128 if (!cmp(b + i, b + h[j].pos))
124 break;
129 break;
125
130
126 /* add to the head of the equivalence class */
131 /* add to the head of the equivalence class */
127 b[i].n = h[j].pos;
132 b[i].n = h[j].pos;
128 b[i].e = j;
133 b[i].e = j;
129 h[j].pos = i;
134 h[j].pos = i;
130 h[j].len++; /* keep track of popularity */
135 h[j].len++; /* keep track of popularity */
131 }
136 }
132
137
133 /* compute popularity threshold */
138 /* compute popularity threshold */
134 t = (bn >= 200) ? bn / 100 : bn + 1;
139 t = (bn >= 200) ? bn / 100 : bn + 1;
135
140
136 /* match items in a to their equivalence class in b */
141 /* match items in a to their equivalence class in b */
137 for (i = 0; i < an; i++) {
142 for (i = 0; i < an; i++) {
138 /* find the equivalence class */
143 /* find the equivalence class */
139 for (j = a[i].h & buckets; h[j].pos != -1;
144 for (j = a[i].h & buckets; h[j].pos != -1;
140 j = (j + 1) & buckets)
145 j = (j + 1) & buckets)
141 if (!cmp(a + i, b + h[j].pos))
146 if (!cmp(a + i, b + h[j].pos))
142 break;
147 break;
143
148
144 a[i].e = j; /* use equivalence class for quick compare */
149 a[i].e = j; /* use equivalence class for quick compare */
145 if(h[j].len <= t)
150 if(h[j].len <= t)
146 a[i].n = h[j].pos; /* point to head of match list */
151 a[i].n = h[j].pos; /* point to head of match list */
147 else
152 else
148 a[i].n = -1; /* too popular */
153 a[i].n = -1; /* too popular */
149 }
154 }
150
155
151 /* discard hash tables */
156 /* discard hash tables */
152 free(h);
157 free(h);
153 return 1;
158 return 1;
154 }
159 }
155
160
156 static int longest_match(struct line *a, struct line *b, struct pos *pos,
161 static int longest_match(struct line *a, struct line *b, struct pos *pos,
157 int a1, int a2, int b1, int b2, int *omi, int *omj)
162 int a1, int a2, int b1, int b2, int *omi, int *omj)
158 {
163 {
159 int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k;
164 int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k;
160
165
161 for (i = a1; i < a2; i++) {
166 for (i = a1; i < a2; i++) {
162 /* skip things before the current block */
167 /* skip things before the current block */
163 for (j = a[i].n; j != -1 && j < b1; j = b[j].n)
168 for (j = a[i].n; j != -1 && j < b1; j = b[j].n)
164 ;
169 ;
165
170
166 /* loop through all lines match a[i] in b */
171 /* loop through all lines match a[i] in b */
167 for (; j != -1 && j < b2; j = b[j].n) {
172 for (; j != -1 && j < b2; j = b[j].n) {
168 /* does this extend an earlier match? */
173 /* does this extend an earlier match? */
169 if (i > a1 && j > b1 && pos[j - 1].pos == i - 1)
174 if (i > a1 && j > b1 && pos[j - 1].pos == i - 1)
170 k = pos[j - 1].len + 1;
175 k = pos[j - 1].len + 1;
171 else
176 else
172 k = 1;
177 k = 1;
173 pos[j].pos = i;
178 pos[j].pos = i;
174 pos[j].len = k;
179 pos[j].len = k;
175
180
176 /* best match so far? */
181 /* best match so far? */
177 if (k > mk) {
182 if (k > mk) {
178 mi = i;
183 mi = i;
179 mj = j;
184 mj = j;
180 mk = k;
185 mk = k;
181 }
186 }
182 }
187 }
183 }
188 }
184
189
185 if (mk) {
190 if (mk) {
186 mi = mi - mk + 1;
191 mi = mi - mk + 1;
187 mj = mj - mk + 1;
192 mj = mj - mk + 1;
188 }
193 }
189
194
190 /* expand match to include neighboring popular lines */
195 /* expand match to include neighboring popular lines */
191 while (mi - mb > a1 && mj - mb > b1 &&
196 while (mi - mb > a1 && mj - mb > b1 &&
192 a[mi - mb - 1].e == b[mj - mb - 1].e)
197 a[mi - mb - 1].e == b[mj - mb - 1].e)
193 mb++;
198 mb++;
194 while (mi + mk < a2 && mj + mk < b2 &&
199 while (mi + mk < a2 && mj + mk < b2 &&
195 a[mi + mk].e == b[mj + mk].e)
200 a[mi + mk].e == b[mj + mk].e)
196 mk++;
201 mk++;
197
202
198 *omi = mi - mb;
203 *omi = mi - mb;
199 *omj = mj - mb;
204 *omj = mj - mb;
200 return mk + mb;
205 return mk + mb;
201 }
206 }
202
207
203 static void recurse(struct line *a, struct line *b, struct pos *pos,
208 static void recurse(struct line *a, struct line *b, struct pos *pos,
204 int a1, int a2, int b1, int b2, struct hunklist *l)
209 int a1, int a2, int b1, int b2, struct hunklist *l)
205 {
210 {
206 int i, j, k;
211 int i, j, k;
207
212
208 /* find the longest match in this chunk */
213 /* find the longest match in this chunk */
209 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
214 k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j);
210 if (!k)
215 if (!k)
211 return;
216 return;
212
217
213 /* and recurse on the remaining chunks on either side */
218 /* and recurse on the remaining chunks on either side */
214 recurse(a, b, pos, a1, i, b1, j, l);
219 recurse(a, b, pos, a1, i, b1, j, l);
215 l->head->a1 = i;
220 l->head->a1 = i;
216 l->head->a2 = i + k;
221 l->head->a2 = i + k;
217 l->head->b1 = j;
222 l->head->b1 = j;
218 l->head->b2 = j + k;
223 l->head->b2 = j + k;
219 l->head++;
224 l->head++;
220 recurse(a, b, pos, i + k, a2, j + k, b2, l);
225 recurse(a, b, pos, i + k, a2, j + k, b2, l);
221 }
226 }
222
227
223 static struct hunklist diff(struct line *a, int an, struct line *b, int bn)
228 static struct hunklist diff(struct line *a, int an, struct line *b, int bn)
224 {
229 {
225 struct hunklist l;
230 struct hunklist l;
226 struct pos *pos;
231 struct pos *pos;
227 int t;
232 int t;
228
233
229 /* allocate and fill arrays */
234 /* allocate and fill arrays */
230 t = equatelines(a, an, b, bn);
235 t = equatelines(a, an, b, bn);
231 pos = calloc(bn, sizeof(struct pos));
236 pos = calloc(bn, sizeof(struct pos));
232 /* we can't have more matches than lines in the shorter file */
237 /* we can't have more matches than lines in the shorter file */
233 l.head = l.base = malloc(sizeof(struct hunk) * ((an<bn ? an:bn) + 1));
238 l.head = l.base = malloc(sizeof(struct hunk) * ((an<bn ? an:bn) + 1));
234
239
235 if (pos && l.base && t) {
240 if (pos && l.base && t) {
236 /* generate the matching block list */
241 /* generate the matching block list */
237 recurse(a, b, pos, 0, an, 0, bn, &l);
242 recurse(a, b, pos, 0, an, 0, bn, &l);
238 l.head->a1 = an;
243 l.head->a1 = an;
239 l.head->b1 = bn;
244 l.head->b1 = bn;
240 l.head++;
245 l.head++;
241 }
246 }
242
247
243 free(pos);
248 free(pos);
244 return l;
249 return l;
245 }
250 }
246
251
247 static PyObject *blocks(PyObject *self, PyObject *args)
252 static PyObject *blocks(PyObject *self, PyObject *args)
248 {
253 {
249 PyObject *sa, *sb, *rl = NULL, *m;
254 PyObject *sa, *sb, *rl = NULL, *m;
250 struct line *a, *b;
255 struct line *a, *b;
251 struct hunklist l;
256 struct hunklist l;
252 struct hunk *h;
257 struct hunk *h;
253 int an, bn, pos = 0;
258 int an, bn, pos = 0;
254
259
255 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
260 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
256 return NULL;
261 return NULL;
257
262
258 an = splitlines(PyString_AsString(sa), PyString_Size(sa), &a);
263 an = splitlines(PyString_AsString(sa), PyString_Size(sa), &a);
259 bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &b);
264 bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &b);
260 if (!a || !b)
265 if (!a || !b)
261 goto nomem;
266 goto nomem;
262
267
263 l = diff(a, an, b, bn);
268 l = diff(a, an, b, bn);
264 rl = PyList_New(l.head - l.base);
269 rl = PyList_New(l.head - l.base);
265 if (!l.head || !rl)
270 if (!l.head || !rl)
266 goto nomem;
271 goto nomem;
267
272
268 for(h = l.base; h != l.head; h++) {
273 for(h = l.base; h != l.head; h++) {
269 m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
274 m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
270 PyList_SetItem(rl, pos, m);
275 PyList_SetItem(rl, pos, m);
271 pos++;
276 pos++;
272 }
277 }
273
278
274 nomem:
279 nomem:
275 free(a);
280 free(a);
276 free(b);
281 free(b);
277 free(l.base);
282 free(l.base);
278 return rl ? rl : PyErr_NoMemory();
283 return rl ? rl : PyErr_NoMemory();
279 }
284 }
280
285
281 static PyObject *bdiff(PyObject *self, PyObject *args)
286 static PyObject *bdiff(PyObject *self, PyObject *args)
282 {
287 {
283 PyObject *sa, *sb, *result = NULL;
288 PyObject *sa, *sb, *result = NULL;
284 struct line *al, *bl;
289 struct line *al, *bl;
285 struct hunklist l;
290 struct hunklist l;
286 struct hunk *h;
291 struct hunk *h;
287 char encode[12], *rb;
292 char encode[12], *rb;
288 int an, bn, len = 0, la = 0, lb = 0;
293 int an, bn, len = 0, la = 0, lb = 0;
289
294
290 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
295 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
291 return NULL;
296 return NULL;
292
297
293 an = splitlines(PyString_AsString(sa), PyString_Size(sa), &al);
298 an = splitlines(PyString_AsString(sa), PyString_Size(sa), &al);
294 bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &bl);
299 bn = splitlines(PyString_AsString(sb), PyString_Size(sb), &bl);
295 if (!al || !bl)
300 if (!al || !bl)
296 goto nomem;
301 goto nomem;
297
302
298 l = diff(al, an, bl, bn);
303 l = diff(al, an, bl, bn);
299 if (!l.head)
304 if (!l.head)
300 goto nomem;
305 goto nomem;
301
306
302 /* calculate length of output */
307 /* calculate length of output */
303 for(h = l.base; h != l.head; h++) {
308 for(h = l.base; h != l.head; h++) {
304 if (h->a1 != la || h->b1 != lb)
309 if (h->a1 != la || h->b1 != lb)
305 len += 12 + bl[h->b1].l - bl[lb].l;
310 len += 12 + bl[h->b1].l - bl[lb].l;
306 la = h->a2;
311 la = h->a2;
307 lb = h->b2;
312 lb = h->b2;
308 }
313 }
309
314
310 result = PyString_FromStringAndSize(NULL, len);
315 result = PyString_FromStringAndSize(NULL, len);
311 if (!result)
316 if (!result)
312 goto nomem;
317 goto nomem;
313
318
314 /* build binary patch */
319 /* build binary patch */
315 rb = PyString_AsString(result);
320 rb = PyString_AsString(result);
316 la = lb = 0;
321 la = lb = 0;
317
322
318 for(h = l.base; h != l.head; h++) {
323 for(h = l.base; h != l.head; h++) {
319 if (h->a1 != la || h->b1 != lb) {
324 if (h->a1 != la || h->b1 != lb) {
320 len = bl[h->b1].l - bl[lb].l;
325 len = bl[h->b1].l - bl[lb].l;
321 *(uint32_t *)(encode) = htonl(al[la].l - al->l);
326 *(uint32_t *)(encode) = htonl(al[la].l - al->l);
322 *(uint32_t *)(encode + 4) = htonl(al[h->a1].l - al->l);
327 *(uint32_t *)(encode + 4) = htonl(al[h->a1].l - al->l);
323 *(uint32_t *)(encode + 8) = htonl(len);
328 *(uint32_t *)(encode + 8) = htonl(len);
324 memcpy(rb, encode, 12);
329 memcpy(rb, encode, 12);
325 memcpy(rb + 12, bl[lb].l, len);
330 memcpy(rb + 12, bl[lb].l, len);
326 rb += 12 + len;
331 rb += 12 + len;
327 }
332 }
328 la = h->a2;
333 la = h->a2;
329 lb = h->b2;
334 lb = h->b2;
330 }
335 }
331
336
332 nomem:
337 nomem:
333 free(al);
338 free(al);
334 free(bl);
339 free(bl);
335 free(l.base);
340 free(l.base);
336 return result ? result : PyErr_NoMemory();
341 return result ? result : PyErr_NoMemory();
337 }
342 }
338
343
339 static char mdiff_doc[] = "Efficient binary diff.";
344 static char mdiff_doc[] = "Efficient binary diff.";
340
345
341 static PyMethodDef methods[] = {
346 static PyMethodDef methods[] = {
342 {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
347 {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
343 {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
348 {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
344 {NULL, NULL}
349 {NULL, NULL}
345 };
350 };
346
351
347 PyMODINIT_FUNC initbdiff(void)
352 PyMODINIT_FUNC initbdiff(void)
348 {
353 {
349 Py_InitModule3("bdiff", methods, mdiff_doc);
354 Py_InitModule3("bdiff", methods, mdiff_doc);
350 }
355 }
General Comments 0
You need to be logged in to leave comments. Login now