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