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, " |
|
38 | if (!PyArg_ParseTuple(args, "t#|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, " |
|
85 | if (!PyArg_ParseTuple(args, "t#", &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 | } |
General Comments 0
You need to be logged in to leave comments.
Login now