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