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