Show More
@@ -1,453 +1,455 | |||||
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 __linux |
|
21 | #ifdef __linux | |
22 | #define inline __inline |
|
22 | #define inline __inline | |
23 | #endif |
|
23 | #endif | |
24 |
|
24 | |||
25 | #ifdef _WIN32 |
|
25 | #ifdef _WIN32 | |
26 | #ifdef _MSC_VER |
|
26 | #ifdef _MSC_VER | |
27 | #define inline __inline |
|
27 | #define inline __inline | |
28 | typedef unsigned long uint32_t; |
|
28 | typedef unsigned long uint32_t; | |
29 | #else |
|
29 | #else | |
30 | #include <stdint.h> |
|
30 | #include <stdint.h> | |
31 | #endif |
|
31 | #endif | |
32 | static uint32_t htonl(uint32_t x) |
|
32 | static uint32_t htonl(uint32_t x) | |
33 | { |
|
33 | { | |
34 | return ((x & 0x000000ffUL) << 24) | |
|
34 | return ((x & 0x000000ffUL) << 24) | | |
35 | ((x & 0x0000ff00UL) << 8) | |
|
35 | ((x & 0x0000ff00UL) << 8) | | |
36 | ((x & 0x00ff0000UL) >> 8) | |
|
36 | ((x & 0x00ff0000UL) >> 8) | | |
37 | ((x & 0xff000000UL) >> 24); |
|
37 | ((x & 0xff000000UL) >> 24); | |
38 | } |
|
38 | } | |
39 | #else |
|
39 | #else | |
40 | #include <sys/types.h> |
|
40 | #include <sys/types.h> | |
41 | #if defined __BEOS__ && !defined __HAIKU__ |
|
41 | #if defined __BEOS__ && !defined __HAIKU__ | |
42 | #include <ByteOrder.h> |
|
42 | #include <ByteOrder.h> | |
43 | #else |
|
43 | #else | |
44 | #include <arpa/inet.h> |
|
44 | #include <arpa/inet.h> | |
45 | #endif |
|
45 | #endif | |
46 | #include <inttypes.h> |
|
46 | #include <inttypes.h> | |
47 | #endif |
|
47 | #endif | |
48 |
|
48 | |||
49 | #include "util.h" |
|
49 | #include "util.h" | |
50 |
|
50 | |||
51 | struct line { |
|
51 | struct line { | |
52 | int h, len, n, e; |
|
52 | int h, len, n, e; | |
53 | const char *l; |
|
53 | const char *l; | |
54 | }; |
|
54 | }; | |
55 |
|
55 | |||
56 | struct pos { |
|
56 | struct pos { | |
57 | int pos, len; |
|
57 | int pos, len; | |
58 | }; |
|
58 | }; | |
59 |
|
59 | |||
60 | struct hunk; |
|
60 | struct hunk; | |
61 | struct hunk { |
|
61 | struct hunk { | |
62 | int a1, a2, b1, b2; |
|
62 | int a1, a2, b1, b2; | |
63 | struct hunk *next; |
|
63 | struct hunk *next; | |
64 | }; |
|
64 | }; | |
65 |
|
65 | |||
66 | static int splitlines(const char *a, int len, struct line **lr) |
|
66 | static int splitlines(const char *a, int len, struct line **lr) | |
67 | { |
|
67 | { | |
68 | int h, i; |
|
68 | unsigned h; | |
|
69 | int i; | |||
69 | const char *p, *b = a; |
|
70 | const char *p, *b = a; | |
70 | const char * const plast = a + len - 1; |
|
71 | const char * const plast = a + len - 1; | |
71 | struct line *l; |
|
72 | struct line *l; | |
72 |
|
73 | |||
73 | /* count the lines */ |
|
74 | /* count the lines */ | |
74 | i = 1; /* extra line for sentinel */ |
|
75 | i = 1; /* extra line for sentinel */ | |
75 | for (p = a; p < a + len; p++) |
|
76 | for (p = a; p < a + len; p++) | |
76 | if (*p == '\n' || p == plast) |
|
77 | if (*p == '\n' || p == plast) | |
77 | i++; |
|
78 | i++; | |
78 |
|
79 | |||
79 | *lr = l = (struct line *)malloc(sizeof(struct line) * i); |
|
80 | *lr = l = (struct line *)malloc(sizeof(struct line) * i); | |
80 | if (!l) |
|
81 | if (!l) | |
81 | return -1; |
|
82 | return -1; | |
82 |
|
83 | |||
83 | /* build the line array and calculate hashes */ |
|
84 | /* build the line array and calculate hashes */ | |
84 | h = 0; |
|
85 | h = 0; | |
85 | for (p = a; p < a + len; p++) { |
|
86 | for (p = a; p < a + len; p++) { | |
86 | /* Leonid Yuriev's hash */ |
|
87 | /* Leonid Yuriev's hash */ | |
87 | h = (h * 1664525) + (unsigned char)*p + 1013904223; |
|
88 | h = (h * 1664525) + (unsigned char)*p + 1013904223; | |
88 |
|
89 | |||
89 | if (*p == '\n' || p == plast) { |
|
90 | if (*p == '\n' || p == plast) { | |
90 | l->h = h; |
|
91 | l->h = h; | |
91 | h = 0; |
|
92 | h = 0; | |
92 | l->len = p - b + 1; |
|
93 | l->len = p - b + 1; | |
93 | l->l = b; |
|
94 | l->l = b; | |
94 | l->n = INT_MAX; |
|
95 | l->n = INT_MAX; | |
95 | l++; |
|
96 | l++; | |
96 | b = p + 1; |
|
97 | b = p + 1; | |
97 | } |
|
98 | } | |
98 | } |
|
99 | } | |
99 |
|
100 | |||
100 | /* set up a sentinel */ |
|
101 | /* set up a sentinel */ | |
101 |
l->h = |
|
102 | l->h = 0; | |
|
103 | l->len = 0; | |||
102 | l->l = a + len; |
|
104 | l->l = a + len; | |
103 | return i - 1; |
|
105 | return i - 1; | |
104 | } |
|
106 | } | |
105 |
|
107 | |||
106 | static inline int cmp(struct line *a, struct line *b) |
|
108 | static inline int cmp(struct line *a, struct line *b) | |
107 | { |
|
109 | { | |
108 | 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); | |
109 | } |
|
111 | } | |
110 |
|
112 | |||
111 | 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) | |
112 | { |
|
114 | { | |
113 | int i, j, buckets = 1, t, scale; |
|
115 | int i, j, buckets = 1, t, scale; | |
114 | struct pos *h = NULL; |
|
116 | struct pos *h = NULL; | |
115 |
|
117 | |||
116 | /* build a hash table of the next highest power of 2 */ |
|
118 | /* build a hash table of the next highest power of 2 */ | |
117 | while (buckets < bn + 1) |
|
119 | while (buckets < bn + 1) | |
118 | buckets *= 2; |
|
120 | buckets *= 2; | |
119 |
|
121 | |||
120 | /* try to allocate a large hash table to avoid collisions */ |
|
122 | /* try to allocate a large hash table to avoid collisions */ | |
121 | for (scale = 4; scale; scale /= 2) { |
|
123 | for (scale = 4; scale; scale /= 2) { | |
122 | h = (struct pos *)malloc(scale * buckets * sizeof(struct pos)); |
|
124 | h = (struct pos *)malloc(scale * buckets * sizeof(struct pos)); | |
123 | if (h) |
|
125 | if (h) | |
124 | break; |
|
126 | break; | |
125 | } |
|
127 | } | |
126 |
|
128 | |||
127 | if (!h) |
|
129 | if (!h) | |
128 | return 0; |
|
130 | return 0; | |
129 |
|
131 | |||
130 | buckets = buckets * scale - 1; |
|
132 | buckets = buckets * scale - 1; | |
131 |
|
133 | |||
132 | /* clear the hash table */ |
|
134 | /* clear the hash table */ | |
133 | for (i = 0; i <= buckets; i++) { |
|
135 | for (i = 0; i <= buckets; i++) { | |
134 | h[i].pos = INT_MAX; |
|
136 | h[i].pos = INT_MAX; | |
135 | h[i].len = 0; |
|
137 | h[i].len = 0; | |
136 | } |
|
138 | } | |
137 |
|
139 | |||
138 | /* add lines to the hash table chains */ |
|
140 | /* add lines to the hash table chains */ | |
139 | for (i = bn - 1; i >= 0; i--) { |
|
141 | for (i = bn - 1; i >= 0; i--) { | |
140 | /* find the equivalence class */ |
|
142 | /* find the equivalence class */ | |
141 | for (j = b[i].h & buckets; h[j].pos != INT_MAX; |
|
143 | for (j = b[i].h & buckets; h[j].pos != INT_MAX; | |
142 | j = (j + 1) & buckets) |
|
144 | j = (j + 1) & buckets) | |
143 | if (!cmp(b + i, b + h[j].pos)) |
|
145 | if (!cmp(b + i, b + h[j].pos)) | |
144 | break; |
|
146 | break; | |
145 |
|
147 | |||
146 | /* add to the head of the equivalence class */ |
|
148 | /* add to the head of the equivalence class */ | |
147 | b[i].n = h[j].pos; |
|
149 | b[i].n = h[j].pos; | |
148 | b[i].e = j; |
|
150 | b[i].e = j; | |
149 | h[j].pos = i; |
|
151 | h[j].pos = i; | |
150 | h[j].len++; /* keep track of popularity */ |
|
152 | h[j].len++; /* keep track of popularity */ | |
151 | } |
|
153 | } | |
152 |
|
154 | |||
153 | /* compute popularity threshold */ |
|
155 | /* compute popularity threshold */ | |
154 | t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1); |
|
156 | t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1); | |
155 |
|
157 | |||
156 | /* match items in a to their equivalence class in b */ |
|
158 | /* match items in a to their equivalence class in b */ | |
157 | for (i = 0; i < an; i++) { |
|
159 | for (i = 0; i < an; i++) { | |
158 | /* find the equivalence class */ |
|
160 | /* find the equivalence class */ | |
159 | for (j = a[i].h & buckets; h[j].pos != INT_MAX; |
|
161 | for (j = a[i].h & buckets; h[j].pos != INT_MAX; | |
160 | j = (j + 1) & buckets) |
|
162 | j = (j + 1) & buckets) | |
161 | if (!cmp(a + i, b + h[j].pos)) |
|
163 | if (!cmp(a + i, b + h[j].pos)) | |
162 | break; |
|
164 | break; | |
163 |
|
165 | |||
164 | a[i].e = j; /* use equivalence class for quick compare */ |
|
166 | a[i].e = j; /* use equivalence class for quick compare */ | |
165 | if (h[j].len <= t) |
|
167 | if (h[j].len <= t) | |
166 | a[i].n = h[j].pos; /* point to head of match list */ |
|
168 | a[i].n = h[j].pos; /* point to head of match list */ | |
167 | else |
|
169 | else | |
168 | a[i].n = INT_MAX; /* too popular */ |
|
170 | a[i].n = INT_MAX; /* too popular */ | |
169 | } |
|
171 | } | |
170 |
|
172 | |||
171 | /* discard hash tables */ |
|
173 | /* discard hash tables */ | |
172 | free(h); |
|
174 | free(h); | |
173 | return 1; |
|
175 | return 1; | |
174 | } |
|
176 | } | |
175 |
|
177 | |||
176 | static int longest_match(struct line *a, struct line *b, struct pos *pos, |
|
178 | static int longest_match(struct line *a, struct line *b, struct pos *pos, | |
177 | int a1, int a2, int b1, int b2, int *omi, int *omj) |
|
179 | int a1, int a2, int b1, int b2, int *omi, int *omj) | |
178 | { |
|
180 | { | |
179 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; |
|
181 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; | |
180 |
|
182 | |||
181 | for (i = a1; i < a2; i++) { |
|
183 | for (i = a1; i < a2; i++) { | |
182 | /* skip things before the current block */ |
|
184 | /* skip things before the current block */ | |
183 | for (j = a[i].n; j < b1; j = b[j].n) |
|
185 | for (j = a[i].n; j < b1; j = b[j].n) | |
184 | ; |
|
186 | ; | |
185 |
|
187 | |||
186 | /* loop through all lines match a[i] in b */ |
|
188 | /* loop through all lines match a[i] in b */ | |
187 | for (; j < b2; j = b[j].n) { |
|
189 | for (; j < b2; j = b[j].n) { | |
188 | /* does this extend an earlier match? */ |
|
190 | /* does this extend an earlier match? */ | |
189 | if (i > a1 && j > b1 && pos[j - 1].pos == i - 1) |
|
191 | if (i > a1 && j > b1 && pos[j - 1].pos == i - 1) | |
190 | k = pos[j - 1].len + 1; |
|
192 | k = pos[j - 1].len + 1; | |
191 | else |
|
193 | else | |
192 | k = 1; |
|
194 | k = 1; | |
193 | pos[j].pos = i; |
|
195 | pos[j].pos = i; | |
194 | pos[j].len = k; |
|
196 | pos[j].len = k; | |
195 |
|
197 | |||
196 | /* best match so far? */ |
|
198 | /* best match so far? */ | |
197 | if (k > mk) { |
|
199 | if (k > mk) { | |
198 | mi = i; |
|
200 | mi = i; | |
199 | mj = j; |
|
201 | mj = j; | |
200 | mk = k; |
|
202 | mk = k; | |
201 | } |
|
203 | } | |
202 | } |
|
204 | } | |
203 | } |
|
205 | } | |
204 |
|
206 | |||
205 | if (mk) { |
|
207 | if (mk) { | |
206 | mi = mi - mk + 1; |
|
208 | mi = mi - mk + 1; | |
207 | mj = mj - mk + 1; |
|
209 | mj = mj - mk + 1; | |
208 | } |
|
210 | } | |
209 |
|
211 | |||
210 | /* expand match to include neighboring popular lines */ |
|
212 | /* expand match to include neighboring popular lines */ | |
211 | while (mi - mb > a1 && mj - mb > b1 && |
|
213 | while (mi - mb > a1 && mj - mb > b1 && | |
212 | a[mi - mb - 1].e == b[mj - mb - 1].e) |
|
214 | a[mi - mb - 1].e == b[mj - mb - 1].e) | |
213 | mb++; |
|
215 | mb++; | |
214 | while (mi + mk < a2 && mj + mk < b2 && |
|
216 | while (mi + mk < a2 && mj + mk < b2 && | |
215 | a[mi + mk].e == b[mj + mk].e) |
|
217 | a[mi + mk].e == b[mj + mk].e) | |
216 | mk++; |
|
218 | mk++; | |
217 |
|
219 | |||
218 | *omi = mi - mb; |
|
220 | *omi = mi - mb; | |
219 | *omj = mj - mb; |
|
221 | *omj = mj - mb; | |
220 |
|
222 | |||
221 | return mk + mb; |
|
223 | return mk + mb; | |
222 | } |
|
224 | } | |
223 |
|
225 | |||
224 | static struct hunk *recurse(struct line *a, struct line *b, struct pos *pos, |
|
226 | static struct hunk *recurse(struct line *a, struct line *b, struct pos *pos, | |
225 | int a1, int a2, int b1, int b2, struct hunk *l) |
|
227 | int a1, int a2, int b1, int b2, struct hunk *l) | |
226 | { |
|
228 | { | |
227 | int i, j, k; |
|
229 | int i, j, k; | |
228 |
|
230 | |||
229 | while (1) { |
|
231 | while (1) { | |
230 | /* find the longest match in this chunk */ |
|
232 | /* find the longest match in this chunk */ | |
231 | k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); |
|
233 | k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); | |
232 | if (!k) |
|
234 | if (!k) | |
233 | return l; |
|
235 | return l; | |
234 |
|
236 | |||
235 | /* and recurse on the remaining chunks on either side */ |
|
237 | /* and recurse on the remaining chunks on either side */ | |
236 | l = recurse(a, b, pos, a1, i, b1, j, l); |
|
238 | l = recurse(a, b, pos, a1, i, b1, j, l); | |
237 | if (!l) |
|
239 | if (!l) | |
238 | return NULL; |
|
240 | return NULL; | |
239 |
|
241 | |||
240 | l->next = (struct hunk *)malloc(sizeof(struct hunk)); |
|
242 | l->next = (struct hunk *)malloc(sizeof(struct hunk)); | |
241 | if (!l->next) |
|
243 | if (!l->next) | |
242 | return NULL; |
|
244 | return NULL; | |
243 |
|
245 | |||
244 | l = l->next; |
|
246 | l = l->next; | |
245 | l->a1 = i; |
|
247 | l->a1 = i; | |
246 | l->a2 = i + k; |
|
248 | l->a2 = i + k; | |
247 | l->b1 = j; |
|
249 | l->b1 = j; | |
248 | l->b2 = j + k; |
|
250 | l->b2 = j + k; | |
249 | l->next = NULL; |
|
251 | l->next = NULL; | |
250 |
|
252 | |||
251 | /* tail-recursion didn't happen, so do equivalent iteration */ |
|
253 | /* tail-recursion didn't happen, so do equivalent iteration */ | |
252 | a1 = i + k; |
|
254 | a1 = i + k; | |
253 | b1 = j + k; |
|
255 | b1 = j + k; | |
254 | } |
|
256 | } | |
255 | } |
|
257 | } | |
256 |
|
258 | |||
257 | static int diff(struct line *a, int an, struct line *b, int bn, |
|
259 | static int diff(struct line *a, int an, struct line *b, int bn, | |
258 | struct hunk *base) |
|
260 | struct hunk *base) | |
259 | { |
|
261 | { | |
260 | struct hunk *curr; |
|
262 | struct hunk *curr; | |
261 | struct pos *pos; |
|
263 | struct pos *pos; | |
262 | int t, count = 0; |
|
264 | int t, count = 0; | |
263 |
|
265 | |||
264 | /* allocate and fill arrays */ |
|
266 | /* allocate and fill arrays */ | |
265 | t = equatelines(a, an, b, bn); |
|
267 | t = equatelines(a, an, b, bn); | |
266 | pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos)); |
|
268 | pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos)); | |
267 |
|
269 | |||
268 | if (pos && t) { |
|
270 | if (pos && t) { | |
269 | /* generate the matching block list */ |
|
271 | /* generate the matching block list */ | |
270 |
|
272 | |||
271 | curr = recurse(a, b, pos, 0, an, 0, bn, base); |
|
273 | curr = recurse(a, b, pos, 0, an, 0, bn, base); | |
272 | if (!curr) |
|
274 | if (!curr) | |
273 | return -1; |
|
275 | return -1; | |
274 |
|
276 | |||
275 | /* sentinel end hunk */ |
|
277 | /* sentinel end hunk */ | |
276 | curr->next = (struct hunk *)malloc(sizeof(struct hunk)); |
|
278 | curr->next = (struct hunk *)malloc(sizeof(struct hunk)); | |
277 | if (!curr->next) |
|
279 | if (!curr->next) | |
278 | return -1; |
|
280 | return -1; | |
279 | curr = curr->next; |
|
281 | curr = curr->next; | |
280 | curr->a1 = curr->a2 = an; |
|
282 | curr->a1 = curr->a2 = an; | |
281 | curr->b1 = curr->b2 = bn; |
|
283 | curr->b1 = curr->b2 = bn; | |
282 | curr->next = NULL; |
|
284 | curr->next = NULL; | |
283 | } |
|
285 | } | |
284 |
|
286 | |||
285 | free(pos); |
|
287 | free(pos); | |
286 |
|
288 | |||
287 | /* normalize the hunk list, try to push each hunk towards the end */ |
|
289 | /* normalize the hunk list, try to push each hunk towards the end */ | |
288 | for (curr = base->next; curr; curr = curr->next) { |
|
290 | for (curr = base->next; curr; curr = curr->next) { | |
289 | struct hunk *next = curr->next; |
|
291 | struct hunk *next = curr->next; | |
290 | int shift = 0; |
|
292 | int shift = 0; | |
291 |
|
293 | |||
292 | if (!next) |
|
294 | if (!next) | |
293 | break; |
|
295 | break; | |
294 |
|
296 | |||
295 | if (curr->a2 == next->a1) |
|
297 | if (curr->a2 == next->a1) | |
296 | while (curr->a2 + shift < an && curr->b2 + shift < bn |
|
298 | while (curr->a2 + shift < an && curr->b2 + shift < bn | |
297 | && !cmp(a + curr->a2 + shift, |
|
299 | && !cmp(a + curr->a2 + shift, | |
298 | b + curr->b2 + shift)) |
|
300 | b + curr->b2 + shift)) | |
299 | shift++; |
|
301 | shift++; | |
300 | else if (curr->b2 == next->b1) |
|
302 | else if (curr->b2 == next->b1) | |
301 | while (curr->b2 + shift < bn && curr->a2 + shift < an |
|
303 | while (curr->b2 + shift < bn && curr->a2 + shift < an | |
302 | && !cmp(b + curr->b2 + shift, |
|
304 | && !cmp(b + curr->b2 + shift, | |
303 | a + curr->a2 + shift)) |
|
305 | a + curr->a2 + shift)) | |
304 | shift++; |
|
306 | shift++; | |
305 | if (!shift) |
|
307 | if (!shift) | |
306 | continue; |
|
308 | continue; | |
307 | curr->b2 += shift; |
|
309 | curr->b2 += shift; | |
308 | next->b1 += shift; |
|
310 | next->b1 += shift; | |
309 | curr->a2 += shift; |
|
311 | curr->a2 += shift; | |
310 | next->a1 += shift; |
|
312 | next->a1 += shift; | |
311 | } |
|
313 | } | |
312 |
|
314 | |||
313 | for (curr = base->next; curr; curr = curr->next) |
|
315 | for (curr = base->next; curr; curr = curr->next) | |
314 | count++; |
|
316 | count++; | |
315 | return count; |
|
317 | return count; | |
316 | } |
|
318 | } | |
317 |
|
319 | |||
318 | static void freehunks(struct hunk *l) |
|
320 | static void freehunks(struct hunk *l) | |
319 | { |
|
321 | { | |
320 | struct hunk *n; |
|
322 | struct hunk *n; | |
321 | for (; l; l = n) { |
|
323 | for (; l; l = n) { | |
322 | n = l->next; |
|
324 | n = l->next; | |
323 | free(l); |
|
325 | free(l); | |
324 | } |
|
326 | } | |
325 | } |
|
327 | } | |
326 |
|
328 | |||
327 | static PyObject *blocks(PyObject *self, PyObject *args) |
|
329 | static PyObject *blocks(PyObject *self, PyObject *args) | |
328 | { |
|
330 | { | |
329 | PyObject *sa, *sb, *rl = NULL, *m; |
|
331 | PyObject *sa, *sb, *rl = NULL, *m; | |
330 | struct line *a, *b; |
|
332 | struct line *a, *b; | |
331 | struct hunk l, *h; |
|
333 | struct hunk l, *h; | |
332 | int an, bn, count, pos = 0; |
|
334 | int an, bn, count, pos = 0; | |
333 |
|
335 | |||
334 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) |
|
336 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) | |
335 | return NULL; |
|
337 | return NULL; | |
336 |
|
338 | |||
337 | an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); |
|
339 | an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); | |
338 | bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); |
|
340 | bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); | |
339 |
|
341 | |||
340 | if (!a || !b) |
|
342 | if (!a || !b) | |
341 | goto nomem; |
|
343 | goto nomem; | |
342 |
|
344 | |||
343 | l.next = NULL; |
|
345 | l.next = NULL; | |
344 | count = diff(a, an, b, bn, &l); |
|
346 | count = diff(a, an, b, bn, &l); | |
345 | if (count < 0) |
|
347 | if (count < 0) | |
346 | goto nomem; |
|
348 | goto nomem; | |
347 |
|
349 | |||
348 | rl = PyList_New(count); |
|
350 | rl = PyList_New(count); | |
349 | if (!rl) |
|
351 | if (!rl) | |
350 | goto nomem; |
|
352 | goto nomem; | |
351 |
|
353 | |||
352 | for (h = l.next; h; h = h->next) { |
|
354 | for (h = l.next; h; h = h->next) { | |
353 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); |
|
355 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); | |
354 | PyList_SetItem(rl, pos, m); |
|
356 | PyList_SetItem(rl, pos, m); | |
355 | pos++; |
|
357 | pos++; | |
356 | } |
|
358 | } | |
357 |
|
359 | |||
358 | nomem: |
|
360 | nomem: | |
359 | free(a); |
|
361 | free(a); | |
360 | free(b); |
|
362 | free(b); | |
361 | freehunks(l.next); |
|
363 | freehunks(l.next); | |
362 | return rl ? rl : PyErr_NoMemory(); |
|
364 | return rl ? rl : PyErr_NoMemory(); | |
363 | } |
|
365 | } | |
364 |
|
366 | |||
365 | static PyObject *bdiff(PyObject *self, PyObject *args) |
|
367 | static PyObject *bdiff(PyObject *self, PyObject *args) | |
366 | { |
|
368 | { | |
367 | char *sa, *sb; |
|
369 | char *sa, *sb; | |
368 | PyObject *result = NULL; |
|
370 | PyObject *result = NULL; | |
369 | struct line *al, *bl; |
|
371 | struct line *al, *bl; | |
370 | struct hunk l, *h; |
|
372 | struct hunk l, *h; | |
371 | char encode[12], *rb; |
|
373 | char encode[12], *rb; | |
372 | int an, bn, len = 0, la, lb, count; |
|
374 | int an, bn, len = 0, la, lb, count; | |
373 |
|
375 | |||
374 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) |
|
376 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) | |
375 | return NULL; |
|
377 | return NULL; | |
376 |
|
378 | |||
377 | an = splitlines(sa, la, &al); |
|
379 | an = splitlines(sa, la, &al); | |
378 | bn = splitlines(sb, lb, &bl); |
|
380 | bn = splitlines(sb, lb, &bl); | |
379 | if (!al || !bl) |
|
381 | if (!al || !bl) | |
380 | goto nomem; |
|
382 | goto nomem; | |
381 |
|
383 | |||
382 | l.next = NULL; |
|
384 | l.next = NULL; | |
383 | count = diff(al, an, bl, bn, &l); |
|
385 | count = diff(al, an, bl, bn, &l); | |
384 | if (count < 0) |
|
386 | if (count < 0) | |
385 | goto nomem; |
|
387 | goto nomem; | |
386 |
|
388 | |||
387 | /* calculate length of output */ |
|
389 | /* calculate length of output */ | |
388 | la = lb = 0; |
|
390 | la = lb = 0; | |
389 | for (h = l.next; h; h = h->next) { |
|
391 | for (h = l.next; h; h = h->next) { | |
390 | if (h->a1 != la || h->b1 != lb) |
|
392 | if (h->a1 != la || h->b1 != lb) | |
391 | len += 12 + bl[h->b1].l - bl[lb].l; |
|
393 | len += 12 + bl[h->b1].l - bl[lb].l; | |
392 | la = h->a2; |
|
394 | la = h->a2; | |
393 | lb = h->b2; |
|
395 | lb = h->b2; | |
394 | } |
|
396 | } | |
395 |
|
397 | |||
396 | result = PyBytes_FromStringAndSize(NULL, len); |
|
398 | result = PyBytes_FromStringAndSize(NULL, len); | |
397 |
|
399 | |||
398 | if (!result) |
|
400 | if (!result) | |
399 | goto nomem; |
|
401 | goto nomem; | |
400 |
|
402 | |||
401 | /* build binary patch */ |
|
403 | /* build binary patch */ | |
402 | rb = PyBytes_AsString(result); |
|
404 | rb = PyBytes_AsString(result); | |
403 | la = lb = 0; |
|
405 | la = lb = 0; | |
404 |
|
406 | |||
405 | for (h = l.next; h; h = h->next) { |
|
407 | for (h = l.next; h; h = h->next) { | |
406 | if (h->a1 != la || h->b1 != lb) { |
|
408 | if (h->a1 != la || h->b1 != lb) { | |
407 | len = bl[h->b1].l - bl[lb].l; |
|
409 | len = bl[h->b1].l - bl[lb].l; | |
408 | *(uint32_t *)(encode) = htonl(al[la].l - al->l); |
|
410 | *(uint32_t *)(encode) = htonl(al[la].l - al->l); | |
409 | *(uint32_t *)(encode + 4) = htonl(al[h->a1].l - al->l); |
|
411 | *(uint32_t *)(encode + 4) = htonl(al[h->a1].l - al->l); | |
410 | *(uint32_t *)(encode + 8) = htonl(len); |
|
412 | *(uint32_t *)(encode + 8) = htonl(len); | |
411 | memcpy(rb, encode, 12); |
|
413 | memcpy(rb, encode, 12); | |
412 | memcpy(rb + 12, bl[lb].l, len); |
|
414 | memcpy(rb + 12, bl[lb].l, len); | |
413 | rb += 12 + len; |
|
415 | rb += 12 + len; | |
414 | } |
|
416 | } | |
415 | la = h->a2; |
|
417 | la = h->a2; | |
416 | lb = h->b2; |
|
418 | lb = h->b2; | |
417 | } |
|
419 | } | |
418 |
|
420 | |||
419 | nomem: |
|
421 | nomem: | |
420 | free(al); |
|
422 | free(al); | |
421 | free(bl); |
|
423 | free(bl); | |
422 | freehunks(l.next); |
|
424 | freehunks(l.next); | |
423 | return result ? result : PyErr_NoMemory(); |
|
425 | return result ? result : PyErr_NoMemory(); | |
424 | } |
|
426 | } | |
425 |
|
427 | |||
426 | static char mdiff_doc[] = "Efficient binary diff."; |
|
428 | static char mdiff_doc[] = "Efficient binary diff."; | |
427 |
|
429 | |||
428 | static PyMethodDef methods[] = { |
|
430 | static PyMethodDef methods[] = { | |
429 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, |
|
431 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, | |
430 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, |
|
432 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, | |
431 | {NULL, NULL} |
|
433 | {NULL, NULL} | |
432 | }; |
|
434 | }; | |
433 |
|
435 | |||
434 | #ifdef IS_PY3K |
|
436 | #ifdef IS_PY3K | |
435 | static struct PyModuleDef bdiff_module = { |
|
437 | static struct PyModuleDef bdiff_module = { | |
436 | PyModuleDef_HEAD_INIT, |
|
438 | PyModuleDef_HEAD_INIT, | |
437 | "bdiff", |
|
439 | "bdiff", | |
438 | mdiff_doc, |
|
440 | mdiff_doc, | |
439 | -1, |
|
441 | -1, | |
440 | methods |
|
442 | methods | |
441 | }; |
|
443 | }; | |
442 |
|
444 | |||
443 | PyMODINIT_FUNC PyInit_bdiff(void) |
|
445 | PyMODINIT_FUNC PyInit_bdiff(void) | |
444 | { |
|
446 | { | |
445 | return PyModule_Create(&bdiff_module); |
|
447 | return PyModule_Create(&bdiff_module); | |
446 | } |
|
448 | } | |
447 | #else |
|
449 | #else | |
448 | PyMODINIT_FUNC initbdiff(void) |
|
450 | PyMODINIT_FUNC initbdiff(void) | |
449 | { |
|
451 | { | |
450 | Py_InitModule3("bdiff", methods, mdiff_doc); |
|
452 | Py_InitModule3("bdiff", methods, mdiff_doc); | |
451 | } |
|
453 | } | |
452 | #endif |
|
454 | #endif | |
453 |
|
455 |
General Comments 0
You need to be logged in to leave comments.
Login now