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