📄 binascii.c
字号:
return PyInt_FromLong(result);
}
static PyObject *
binascii_hexlify(PyObject *self, PyObject *args)
{
char* argbuf;
int arglen;
PyObject *retval;
char* retbuf;
int i, j;
if (!PyArg_ParseTuple(args, "t#:b2a_hex", &argbuf, &arglen))
return NULL;
retval = PyString_FromStringAndSize(NULL, arglen*2);
if (!retval)
return NULL;
retbuf = PyString_AsString(retval);
if (!retbuf)
goto finally;
/* make hex version of string, taken from shamodule.c */
for (i=j=0; i < arglen; i++) {
char c;
c = (argbuf[i] >> 4) & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
retbuf[j++] = c;
c = argbuf[i] & 0xf;
c = (c>9) ? c+'a'-10 : c + '0';
retbuf[j++] = c;
}
return retval;
finally:
Py_DECREF(retval);
return NULL;
}
static const char doc_hexlify[] =
"b2a_hex(data) -> s; Hexadecimal representation of binary data.\n\
\n\
This function is also available as \"hexlify()\".";
static int
to_int(int c)
{
if (isdigit(c))
return c - '0';
else {
if (isupper(c))
c = tolower(c);
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
}
return -1;
}
static PyObject *
binascii_unhexlify(PyObject *self, PyObject *args)
{
char* argbuf;
int arglen;
PyObject *retval;
char* retbuf;
int i, j;
if (!PyArg_ParseTuple(args, "s#:a2b_hex", &argbuf, &arglen))
return NULL;
/* XXX What should we do about strings with an odd length? Should
* we add an implicit leading zero, or a trailing zero? For now,
* raise an exception.
*/
if (arglen % 2) {
PyErr_SetString(PyExc_TypeError, "Odd-length string");
return NULL;
}
retval = PyString_FromStringAndSize(NULL, (arglen/2));
if (!retval)
return NULL;
retbuf = PyString_AsString(retval);
if (!retbuf)
goto finally;
for (i=j=0; i < arglen; i += 2) {
int top = to_int(Py_CHARMASK(argbuf[i]));
int bot = to_int(Py_CHARMASK(argbuf[i+1]));
if (top == -1 || bot == -1) {
PyErr_SetString(PyExc_TypeError,
"Non-hexadecimal digit found");
goto finally;
}
retbuf[j++] = (top << 4) + bot;
}
return retval;
finally:
Py_DECREF(retval);
return NULL;
}
static const char doc_unhexlify[] =
"a2b_hex(hexstr) -> s; Binary data of hexadecimal representation.\n\
\n\
hexstr must contain an even number of hex digits (upper or lower case).\n\
This function is also available as \"unhexlify()\"";
static const int table_hex[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
};
#define hexval(c) table_hex[(unsigned int)(c)]
#define MAXLINESIZE 76
static const char doc_a2b_qp[] = "Decode a string of qp-encoded data";
static PyObject*
binascii_a2b_qp(PyObject *self, PyObject *args, PyObject *kwargs)
{
unsigned int in, out;
char ch;
unsigned char *data, *odata;
unsigned int datalen = 0;
PyObject *rv;
static const char * const kwlist[] = {"data", "header", NULL};
int header = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|i", kwlist, &data,
&datalen, &header))
return NULL;
/* We allocate the output same size as input, this is overkill */
odata = (unsigned char *) calloc(1, datalen);
if (odata == NULL) {
PyErr_NoMemory();
return NULL;
}
in = out = 0;
while (in < datalen) {
if (data[in] == '=') {
in++;
if (in >= datalen) break;
/* Soft line breaks */
if ((data[in] == '\n') || (data[in] == '\r') ||
(data[in] == ' ') || (data[in] == '\t')) {
if (data[in] != '\n') {
while (in < datalen && data[in] != '\n') in++;
}
if (in < datalen) in++;
}
else if (data[in] == '=') {
/* broken case from broken python qp */
odata[out++] = '=';
in++;
}
else if (((data[in] >= 'A' && data[in] <= 'F') ||
(data[in] >= 'a' && data[in] <= 'f') ||
(data[in] >= '0' && data[in] <= '9')) &&
((data[in+1] >= 'A' && data[in+1] <= 'F') ||
(data[in+1] >= 'a' && data[in+1] <= 'f') ||
(data[in+1] >= '0' && data[in+1] <= '9'))) {
/* hexval */
ch = hexval(data[in]) << 4;
in++;
ch |= hexval(data[in]);
in++;
odata[out++] = ch;
}
else {
odata[out++] = '=';
}
}
else if (header && data[in] == '_') {
odata[out++] = ' ';
in++;
}
else {
odata[out] = data[in];
in++;
out++;
}
}
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
free (odata);
return NULL;
}
free (odata);
return rv;
}
static int
to_hex (unsigned char ch, unsigned char *s)
{
unsigned int uvalue = ch;
s[1] = "0123456789ABCDEF"[uvalue % 16];
uvalue = (uvalue / 16);
s[0] = "0123456789ABCDEF"[uvalue % 16];
return 0;
}
static const char doc_b2a_qp[] =
"b2a_qp(data, quotetabs=0, istext=1, header=0) -> s; \n\
Encode a string using quoted-printable encoding. \n\
\n\
On encoding, when istext is set, newlines are not encoded, and white \n\
space at end of lines is. When istext is not set, \\r and \\n (CR/LF) are \n\
both encoded. When quotetabs is set, space and tabs are encoded.";
/* XXX: This is ridiculously complicated to be backward compatible
* (mostly) with the quopri module. It doesn't re-create the quopri
* module bug where text ending in CRLF has the CR encoded */
static PyObject*
binascii_b2a_qp (PyObject *self, PyObject *args, PyObject *kwargs)
{
unsigned int in, out;
unsigned char *data, *odata;
unsigned int datalen = 0, odatalen = 0;
PyObject *rv;
unsigned int linelen = 0;
static const char * const kwlist[] = {"data", "quotetabs", "istext", "header", NULL};
int istext = 1;
int quotetabs = 0;
int header = 0;
unsigned char ch;
int crlf = 0;
unsigned char *p;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s#|iii", kwlist, &data,
&datalen, "etabs, &istext, &header))
return NULL;
/* See if this string is using CRLF line ends */
/* XXX: this function has the side effect of converting all of
* the end of lines to be the same depending on this detection
* here */
p = (unsigned char *) strchr((char *)data, '\n');
if ((p != NULL) && (p > data) && (*(p-1) == '\r'))
crlf = 1;
/* First, scan to see how many characters need to be encoded */
in = 0;
while (in < datalen) {
if ((data[in] > 126) ||
(data[in] == '=') ||
(header && data[in] == '_') ||
((data[in] == '.') && (linelen == 1)) ||
(!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
((data[in] < 33) &&
(data[in] != '\r') && (data[in] != '\n') &&
(quotetabs && ((data[in] != '\t') || (data[in] != ' ')))))
{
if ((linelen + 3) >= MAXLINESIZE) {
linelen = 0;
if (crlf)
odatalen += 3;
else
odatalen += 2;
}
linelen += 3;
odatalen += 3;
in++;
}
else {
if (istext &&
((data[in] == '\n') ||
((in+1 < datalen) && (data[in] == '\r') &&
(data[in+1] == '\n'))))
{
linelen = 0;
/* Protect against whitespace on end of line */
if (in && ((data[in-1] == ' ') || (data[in-1] == '\t')))
odatalen += 2;
if (crlf)
odatalen += 2;
else
odatalen += 1;
if (data[in] == '\r')
in += 2;
else
in++;
}
else {
if ((in + 1 != datalen) &&
(data[in+1] != '\n') &&
(linelen + 1) >= MAXLINESIZE) {
linelen = 0;
if (crlf)
odatalen += 3;
else
odatalen += 2;
}
linelen++;
odatalen++;
in++;
}
}
}
odata = (unsigned char *) calloc(1, odatalen);
if (odata == NULL) {
PyErr_NoMemory();
return NULL;
}
in = out = linelen = 0;
while (in < datalen) {
if ((data[in] > 126) ||
(data[in] == '=') ||
(header && data[in] == '_') ||
((data[in] == '.') && (linelen == 1)) ||
(!istext && ((data[in] == '\r') || (data[in] == '\n'))) ||
((data[in] == '\t' || data[in] == ' ') && (in + 1 == datalen)) ||
((data[in] < 33) &&
(data[in] != '\r') && (data[in] != '\n') &&
(quotetabs && ((data[in] != '\t') || (data[in] != ' ')))))
{
if ((linelen + 3 )>= MAXLINESIZE) {
odata[out++] = '=';
if (crlf) odata[out++] = '\r';
odata[out++] = '\n';
linelen = 0;
}
odata[out++] = '=';
to_hex(data[in], &odata[out]);
out += 2;
in++;
linelen += 3;
}
else {
if (istext &&
((data[in] == '\n') ||
((in+1 < datalen) && (data[in] == '\r') &&
(data[in+1] == '\n'))))
{
linelen = 0;
/* Protect against whitespace on end of line */
if (out && ((odata[out-1] == ' ') || (odata[out-1] == '\t'))) {
ch = odata[out-1];
odata[out-1] = '=';
to_hex(ch, &odata[out]);
out += 2;
}
if (crlf) odata[out++] = '\r';
odata[out++] = '\n';
if (data[in] == '\r')
in += 2;
else
in++;
}
else {
if ((in + 1 != datalen) &&
(data[in+1] != '\n') &&
(linelen + 1) >= MAXLINESIZE) {
odata[out++] = '=';
if (crlf) odata[out++] = '\r';
odata[out++] = '\n';
linelen = 0;
}
linelen++;
if (header && data[in] == ' ') {
odata[out++] = '_';
in++;
}
else {
odata[out++] = data[in++];
}
}
}
}
if ((rv = PyString_FromStringAndSize((char *)odata, out)) == NULL) {
free (odata);
return NULL;
}
free (odata);
return rv;
}
/* List of functions defined in the module */
static const struct PyMethodDef binascii_module_methods[] = {
{"a2b_uu", binascii_a2b_uu, METH_VARARGS, doc_a2b_uu},
{"b2a_uu", binascii_b2a_uu, METH_VARARGS, doc_b2a_uu},
{"a2b_base64", binascii_a2b_base64, METH_VARARGS, doc_a2b_base64},
{"b2a_base64", binascii_b2a_base64, METH_VARARGS, doc_b2a_base64},
{"a2b_hqx", binascii_a2b_hqx, METH_VARARGS, doc_a2b_hqx},
{"b2a_hqx", binascii_b2a_hqx, METH_VARARGS, doc_b2a_hqx},
{"b2a_hex", binascii_hexlify, METH_VARARGS, doc_hexlify},
{"a2b_hex", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
{"hexlify", binascii_hexlify, METH_VARARGS, doc_hexlify},
{"unhexlify", binascii_unhexlify, METH_VARARGS, doc_unhexlify},
{"rlecode_hqx", binascii_rlecode_hqx, METH_VARARGS, doc_rlecode_hqx},
{"rledecode_hqx", binascii_rledecode_hqx, METH_VARARGS,
doc_rledecode_hqx},
{"crc_hqx", binascii_crc_hqx, METH_VARARGS, doc_crc_hqx},
{"crc32", binascii_crc32, METH_VARARGS, doc_crc32},
{"a2b_qp", (PyCFunction)binascii_a2b_qp, METH_VARARGS | METH_KEYWORDS,
doc_a2b_qp},
{"b2a_qp", (PyCFunction)binascii_b2a_qp, METH_VARARGS | METH_KEYWORDS,
doc_b2a_qp},
{NULL, NULL} /* sentinel */
};
/* Initialization function for the module (*must* be called initbinascii) */
static const char doc_binascii[] = "Conversion between binary data and ASCII";
DL_EXPORT(void)
initbinascii(void)
{
PyObject *m, *d, *x;
/* Create the module and add the functions */
m = Py_InitModule("binascii", binascii_module_methods);
d = PyModule_GetDict(m);
x = PyString_FromString(doc_binascii);
PyDict_SetItemString(d, "__doc__", x);
Py_XDECREF(x);
(PYTHON_GLOBALS->binascii_Error) = PyErr_NewException("binascii.Error", NULL, NULL);
PyDict_SetItemString(d, "Error", (PYTHON_GLOBALS->binascii_Error));
(PYTHON_GLOBALS->binascii_Incomplete) = PyErr_NewException("binascii.Incomplete", NULL, NULL);
PyDict_SetItemString(d, "Incomplete", (PYTHON_GLOBALS->binascii_Incomplete));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -