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