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