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