base85.c
189 lines
| 3.5 KiB
| text/x-c
|
CLexer
/ mercurial / base85.c
|
r3283 | /* | ||
base85 codec | ||||
Copyright 2006 Brendan Cully <brendan@kublai.com> | ||||
This software may be used and distributed according to the terms of | ||||
the GNU General Public License, incorporated herein by reference. | ||||
Largely based on git's implementation | ||||
*/ | ||||
|
r16837 | #define PY_SSIZE_T_CLEAN | ||
|
r3283 | #include <Python.h> | ||
|
r11362 | #include "util.h" | ||
|
r3283 | static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; | ||||
static char b85dec[256]; | ||||
|
r27060 | static void b85prep(void) | ||
|
r3283 | { | ||
|
r26074 | unsigned i; | ||
|
r3283 | |||
memset(b85dec, 0, sizeof(b85dec)); | ||||
for (i = 0; i < sizeof(b85chars); i++) | ||||
b85dec[(int)(b85chars[i])] = i + 1; | ||||
} | ||||
|
r27060 | static PyObject *b85encode(PyObject *self, PyObject *args) | ||
|
r3283 | { | ||
const unsigned char *text; | ||||
PyObject *out; | ||||
char *dst; | ||||
|
r16837 | Py_ssize_t len, olen, i; | ||
|
r3283 | unsigned int acc, val, ch; | ||
|
r7190 | int pad = 0; | ||
|
r3283 | |||
|
r3369 | if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad)) | ||
|
r3283 | return NULL; | ||
|
r7190 | if (pad) | ||
olen = ((len + 3) / 4 * 5) - 3; | ||||
else { | ||||
olen = len % 4; | ||||
if (olen) | ||||
olen++; | ||||
olen += len / 4 * 5; | ||||
} | ||||
|
r11362 | if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3))) | ||
|
r3283 | return NULL; | ||
|
r11362 | dst = PyBytes_AsString(out); | ||
|
r3283 | |||
|
r3288 | while (len) { | ||
|
r3283 | acc = 0; | ||
for (i = 24; i >= 0; i -= 8) { | ||||
ch = *text++; | ||||
acc |= ch << i; | ||||
if (--len == 0) | ||||
break; | ||||
} | ||||
for (i = 4; i >= 0; i--) { | ||||
val = acc % 85; | ||||
acc /= 85; | ||||
dst[i] = b85chars[val]; | ||||
} | ||||
dst += 5; | ||||
} | ||||
|
r7190 | if (!pad) | ||
|
r11362 | _PyBytes_Resize(&out, olen); | ||
|
r3288 | |||
|
r3283 | return out; | ||
} | ||||
|
r27060 | static PyObject *b85decode(PyObject *self, PyObject *args) | ||
|
r3283 | { | ||
PyObject *out; | ||||
const char *text; | ||||
char *dst; | ||||
|
r16837 | Py_ssize_t len, i, j, olen, cap; | ||
int c; | ||||
|
r3283 | unsigned int acc; | ||
|
r3369 | if (!PyArg_ParseTuple(args, "s#", &text, &len)) | ||
|
r3283 | return NULL; | ||
|
r3288 | olen = len / 5 * 4; | ||
i = len % 5; | ||||
if (i) | ||||
olen += i - 1; | ||||
|
r11362 | if (!(out = PyBytes_FromStringAndSize(NULL, olen))) | ||
|
r3283 | return NULL; | ||
|
r11362 | dst = PyBytes_AsString(out); | ||
|
r3283 | |||
|
r3288 | i = 0; | ||
while (i < len) | ||||
|
r3283 | { | ||
acc = 0; | ||||
|
r3288 | cap = len - i - 1; | ||
if (cap > 4) | ||||
cap = 4; | ||||
for (j = 0; j < cap; i++, j++) | ||||
|
r3283 | { | ||
c = b85dec[(int)*text++] - 1; | ||||
if (c < 0) | ||||
|
r10282 | return PyErr_Format( | ||
PyExc_ValueError, | ||||
|
r16848 | "bad base85 character at position %d", | ||
(int)i); | ||||
|
r3283 | acc = acc * 85 + c; | ||
} | ||||
|
r3288 | if (i++ < len) | ||
|
r3283 | { | ||
c = b85dec[(int)*text++] - 1; | ||||
if (c < 0) | ||||
|
r10282 | return PyErr_Format( | ||
PyExc_ValueError, | ||||
|
r16848 | "bad base85 character at position %d", | ||
(int)i); | ||||
|
r3288 | /* overflow detection: 0xffffffff == "|NsC0", | ||
* "|NsC" == 0x03030303 */ | ||||
if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c) | ||||
|
r10282 | return PyErr_Format( | ||
PyExc_ValueError, | ||||
|
r16848 | "bad base85 sequence at position %d", | ||
(int)i); | ||||
|
r3288 | acc += c; | ||
|
r3283 | } | ||
|
r3288 | cap = olen < 4 ? olen : 4; | ||
olen -= cap; | ||||
for (j = 0; j < 4 - cap; j++) | ||||
acc *= 85; | ||||
if (cap && cap < 4) | ||||
acc += 0xffffff >> (cap - 1) * 8; | ||||
for (j = 0; j < cap; j++) | ||||
|
r3283 | { | ||
acc = (acc << 8) | (acc >> 24); | ||||
|
r3288 | *dst++ = acc; | ||
|
r3283 | } | ||
} | ||||
return out; | ||||
} | ||||
static char base85_doc[] = "Base85 Data Encoding"; | ||||
static PyMethodDef methods[] = { | ||||
|
r3288 | {"b85encode", b85encode, METH_VARARGS, | ||
|
r7190 | "Encode text in base85.\n\n" | ||
"If the second parameter is true, pad the result to a multiple of " | ||||
"five characters.\n"}, | ||||
|
r3288 | {"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"}, | ||
|
r3283 | {NULL, NULL} | ||
}; | ||||
|
r32356 | static const int version = 1; | ||
|
r11362 | #ifdef IS_PY3K | ||
static struct PyModuleDef base85_module = { | ||||
PyModuleDef_HEAD_INIT, | ||||
"base85", | ||||
base85_doc, | ||||
-1, | ||||
methods | ||||
}; | ||||
PyMODINIT_FUNC PyInit_base85(void) | ||||
{ | ||||
|
r32356 | PyObject *m; | ||
|
r11362 | b85prep(); | ||
|
r32356 | m = PyModule_Create(&base85_module); | ||
PyModule_AddIntConstant(m, "version", version); | ||||
return m; | ||||
|
r11362 | } | ||
#else | ||||
|
r3283 | PyMODINIT_FUNC initbase85(void) | ||
{ | ||||
|
r32356 | PyObject *m; | ||
m = Py_InitModule3("base85", methods, base85_doc); | ||||
|
r3283 | |||
b85prep(); | ||||
|
r32356 | PyModule_AddIntConstant(m, "version", version); | ||
|
r3283 | } | ||
|
r11362 | #endif | ||