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