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