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