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