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