##// END OF EJS Templates
base85: fix comparison of an int and a long...
Augie Fackler -
r26074:c1aefe57 default
parent child Browse files
Show More
@@ -1,185 +1,185 b''
1 1 /*
2 2 base85 codec
3 3
4 4 Copyright 2006 Brendan Cully <brendan@kublai.com>
5 5
6 6 This software may be used and distributed according to the terms of
7 7 the GNU General Public License, incorporated herein by reference.
8 8
9 9 Largely based on git's implementation
10 10 */
11 11
12 12 #define PY_SSIZE_T_CLEAN
13 13 #include <Python.h>
14 14
15 15 #include "util.h"
16 16
17 17 static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
18 18 "abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
19 19 static char b85dec[256];
20 20
21 21 static void
22 22 b85prep(void)
23 23 {
24 int i;
24 unsigned i;
25 25
26 26 memset(b85dec, 0, sizeof(b85dec));
27 27 for (i = 0; i < sizeof(b85chars); i++)
28 28 b85dec[(int)(b85chars[i])] = i + 1;
29 29 }
30 30
31 31 static PyObject *
32 32 b85encode(PyObject *self, PyObject *args)
33 33 {
34 34 const unsigned char *text;
35 35 PyObject *out;
36 36 char *dst;
37 37 Py_ssize_t len, olen, i;
38 38 unsigned int acc, val, ch;
39 39 int pad = 0;
40 40
41 41 if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
42 42 return NULL;
43 43
44 44 if (pad)
45 45 olen = ((len + 3) / 4 * 5) - 3;
46 46 else {
47 47 olen = len % 4;
48 48 if (olen)
49 49 olen++;
50 50 olen += len / 4 * 5;
51 51 }
52 52 if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
53 53 return NULL;
54 54
55 55 dst = PyBytes_AsString(out);
56 56
57 57 while (len) {
58 58 acc = 0;
59 59 for (i = 24; i >= 0; i -= 8) {
60 60 ch = *text++;
61 61 acc |= ch << i;
62 62 if (--len == 0)
63 63 break;
64 64 }
65 65 for (i = 4; i >= 0; i--) {
66 66 val = acc % 85;
67 67 acc /= 85;
68 68 dst[i] = b85chars[val];
69 69 }
70 70 dst += 5;
71 71 }
72 72
73 73 if (!pad)
74 74 _PyBytes_Resize(&out, olen);
75 75
76 76 return out;
77 77 }
78 78
79 79 static PyObject *
80 80 b85decode(PyObject *self, PyObject *args)
81 81 {
82 82 PyObject *out;
83 83 const char *text;
84 84 char *dst;
85 85 Py_ssize_t len, i, j, olen, cap;
86 86 int c;
87 87 unsigned int acc;
88 88
89 89 if (!PyArg_ParseTuple(args, "s#", &text, &len))
90 90 return NULL;
91 91
92 92 olen = len / 5 * 4;
93 93 i = len % 5;
94 94 if (i)
95 95 olen += i - 1;
96 96 if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
97 97 return NULL;
98 98
99 99 dst = PyBytes_AsString(out);
100 100
101 101 i = 0;
102 102 while (i < len)
103 103 {
104 104 acc = 0;
105 105 cap = len - i - 1;
106 106 if (cap > 4)
107 107 cap = 4;
108 108 for (j = 0; j < cap; i++, j++)
109 109 {
110 110 c = b85dec[(int)*text++] - 1;
111 111 if (c < 0)
112 112 return PyErr_Format(
113 113 PyExc_ValueError,
114 114 "bad base85 character at position %d",
115 115 (int)i);
116 116 acc = acc * 85 + c;
117 117 }
118 118 if (i++ < len)
119 119 {
120 120 c = b85dec[(int)*text++] - 1;
121 121 if (c < 0)
122 122 return PyErr_Format(
123 123 PyExc_ValueError,
124 124 "bad base85 character at position %d",
125 125 (int)i);
126 126 /* overflow detection: 0xffffffff == "|NsC0",
127 127 * "|NsC" == 0x03030303 */
128 128 if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
129 129 return PyErr_Format(
130 130 PyExc_ValueError,
131 131 "bad base85 sequence at position %d",
132 132 (int)i);
133 133 acc += c;
134 134 }
135 135
136 136 cap = olen < 4 ? olen : 4;
137 137 olen -= cap;
138 138 for (j = 0; j < 4 - cap; j++)
139 139 acc *= 85;
140 140 if (cap && cap < 4)
141 141 acc += 0xffffff >> (cap - 1) * 8;
142 142 for (j = 0; j < cap; j++)
143 143 {
144 144 acc = (acc << 8) | (acc >> 24);
145 145 *dst++ = acc;
146 146 }
147 147 }
148 148
149 149 return out;
150 150 }
151 151
152 152 static char base85_doc[] = "Base85 Data Encoding";
153 153
154 154 static PyMethodDef methods[] = {
155 155 {"b85encode", b85encode, METH_VARARGS,
156 156 "Encode text in base85.\n\n"
157 157 "If the second parameter is true, pad the result to a multiple of "
158 158 "five characters.\n"},
159 159 {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
160 160 {NULL, NULL}
161 161 };
162 162
163 163 #ifdef IS_PY3K
164 164 static struct PyModuleDef base85_module = {
165 165 PyModuleDef_HEAD_INIT,
166 166 "base85",
167 167 base85_doc,
168 168 -1,
169 169 methods
170 170 };
171 171
172 172 PyMODINIT_FUNC PyInit_base85(void)
173 173 {
174 174 b85prep();
175 175
176 176 return PyModule_Create(&base85_module);
177 177 }
178 178 #else
179 179 PyMODINIT_FUNC initbase85(void)
180 180 {
181 181 Py_InitModule3("base85", methods, base85_doc);
182 182
183 183 b85prep();
184 184 }
185 185 #endif
General Comments 0
You need to be logged in to leave comments. Login now