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