1 /* bytes to hex implementation */
2
3 #include "Python.h"
4
5 #include "pystrhex.h"
6
_Py_strhex_impl(const char * argbuf,const Py_ssize_t arglen,const PyObject * sep,int bytes_per_sep_group,const int return_bytes)7 static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
8 const PyObject* sep, int bytes_per_sep_group,
9 const int return_bytes)
10 {
11 PyObject *retval;
12 Py_UCS1* retbuf;
13 Py_ssize_t i, j, resultlen = 0;
14 Py_UCS1 sep_char = 0;
15 unsigned int abs_bytes_per_sep;
16
17 if (sep) {
18 Py_ssize_t seplen = PyObject_Length((PyObject*)sep);
19 if (seplen < 0) {
20 return NULL;
21 }
22 if (seplen != 1) {
23 PyErr_SetString(PyExc_ValueError, "sep must be length 1.");
24 return NULL;
25 }
26 if (PyUnicode_Check(sep)) {
27 if (PyUnicode_READY(sep))
28 return NULL;
29 if (PyUnicode_KIND(sep) != PyUnicode_1BYTE_KIND) {
30 PyErr_SetString(PyExc_ValueError, "sep must be ASCII.");
31 return NULL;
32 }
33 sep_char = PyUnicode_READ_CHAR(sep, 0);
34 } else if (PyBytes_Check(sep)) {
35 sep_char = PyBytes_AS_STRING(sep)[0];
36 } else {
37 PyErr_SetString(PyExc_TypeError, "sep must be str or bytes.");
38 return NULL;
39 }
40 if (sep_char > 127 && !return_bytes) {
41 PyErr_SetString(PyExc_ValueError, "sep must be ASCII.");
42 return NULL;
43 }
44 } else {
45 bytes_per_sep_group = 0;
46 }
47
48 assert(arglen >= 0);
49 abs_bytes_per_sep = abs(bytes_per_sep_group);
50 if (bytes_per_sep_group && arglen > 0) {
51 /* How many sep characters we'll be inserting. */
52 resultlen = (arglen - 1) / abs_bytes_per_sep;
53 }
54 /* Bounds checking for our Py_ssize_t indices. */
55 if (arglen >= PY_SSIZE_T_MAX / 2 - resultlen) {
56 return PyErr_NoMemory();
57 }
58 resultlen += arglen * 2;
59
60 if ((size_t)abs_bytes_per_sep >= (size_t)arglen) {
61 bytes_per_sep_group = 0;
62 abs_bytes_per_sep = 0;
63 }
64
65 if (return_bytes) {
66 /* If _PyBytes_FromSize() were public we could avoid malloc+copy. */
67 retbuf = (Py_UCS1*) PyMem_Malloc(resultlen);
68 if (!retbuf)
69 return PyErr_NoMemory();
70 retval = NULL; /* silence a compiler warning, assigned later. */
71 } else {
72 retval = PyUnicode_New(resultlen, 127);
73 if (!retval)
74 return NULL;
75 retbuf = PyUnicode_1BYTE_DATA(retval);
76 }
77
78 /* Hexlify */
79 for (i=j=0; i < arglen; ++i) {
80 assert(j < resultlen);
81 unsigned char c;
82 c = (argbuf[i] >> 4) & 0xf;
83 retbuf[j++] = Py_hexdigits[c];
84 c = argbuf[i] & 0xf;
85 retbuf[j++] = Py_hexdigits[c];
86 if (bytes_per_sep_group && i < arglen - 1) {
87 Py_ssize_t anchor;
88 anchor = (bytes_per_sep_group > 0) ? (arglen - 1 - i) : (i + 1);
89 if (anchor % abs_bytes_per_sep == 0) {
90 retbuf[j++] = sep_char;
91 }
92 }
93 }
94 assert(j == resultlen);
95
96 if (return_bytes) {
97 retval = PyBytes_FromStringAndSize((const char *)retbuf, resultlen);
98 PyMem_Free(retbuf);
99 }
100 #ifdef Py_DEBUG
101 else {
102 assert(_PyUnicode_CheckConsistency(retval, 1));
103 }
104 #endif
105
106 return retval;
107 }
108
_Py_strhex(const char * argbuf,const Py_ssize_t arglen)109 PyObject * _Py_strhex(const char* argbuf, const Py_ssize_t arglen)
110 {
111 return _Py_strhex_impl(argbuf, arglen, NULL, 0, 0);
112 }
113
114 /* Same as above but returns a bytes() instead of str() to avoid the
115 * need to decode the str() when bytes are needed. */
_Py_strhex_bytes(const char * argbuf,const Py_ssize_t arglen)116 PyObject * _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen)
117 {
118 return _Py_strhex_impl(argbuf, arglen, NULL, 0, 1);
119 }
120
121 /* These variants include support for a separator between every N bytes: */
122
_Py_strhex_with_sep(const char * argbuf,const Py_ssize_t arglen,const PyObject * sep,const int bytes_per_group)123 PyObject * _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group)
124 {
125 return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 0);
126 }
127
128 /* Same as above but returns a bytes() instead of str() to avoid the
129 * need to decode the str() when bytes are needed. */
_Py_strhex_bytes_with_sep(const char * argbuf,const Py_ssize_t arglen,const PyObject * sep,const int bytes_per_group)130 PyObject * _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group)
131 {
132 return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 1);
133 }
134