Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 1 | /* bytes to hex implementation */ |
| 2 | |
| 3 | #include "Python.h" |
| 4 | |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 5 | #include "pystrhex.h" |
| 6 | |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 7 | static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen, |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 8 | const PyObject* sep, int bytes_per_sep_group, |
| 9 | const int return_bytes) |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 10 | { |
| 11 | PyObject *retval; |
| 12 | Py_UCS1* retbuf; |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 13 | Py_ssize_t i, j, resultlen = 0; |
Pablo Galindo | 938d9a0 | 2019-06-01 21:02:08 +0100 | [diff] [blame] | 14 | Py_UCS1 sep_char = 0; |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 15 | unsigned int abs_bytes_per_sep; |
| 16 | |
| 17 | if (sep) { |
Pablo Galindo | 938d9a0 | 2019-06-01 21:02:08 +0100 | [diff] [blame] | 18 | Py_ssize_t seplen = PyObject_Length((PyObject*)sep); |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 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 | } |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 47 | |
| 48 | assert(arglen >= 0); |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 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) { |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 56 | return PyErr_NoMemory(); |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 57 | } |
| 58 | resultlen += arglen * 2; |
| 59 | |
Raymond Hettinger | 0138c4c | 2019-08-27 09:55:13 -0700 | [diff] [blame^] | 60 | if ((size_t)abs_bytes_per_sep >= (size_t)arglen) { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 61 | bytes_per_sep_group = 0; |
| 62 | abs_bytes_per_sep = 0; |
| 63 | } |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 64 | |
| 65 | if (return_bytes) { |
| 66 | /* If _PyBytes_FromSize() were public we could avoid malloc+copy. */ |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 67 | retbuf = (Py_UCS1*) PyMem_Malloc(resultlen); |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 68 | if (!retbuf) |
| 69 | return PyErr_NoMemory(); |
Gregory P. Smith | f789465 | 2015-04-25 23:51:39 -0700 | [diff] [blame] | 70 | retval = NULL; /* silence a compiler warning, assigned later. */ |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 71 | } else { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 72 | retval = PyUnicode_New(resultlen, 127); |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 73 | if (!retval) |
| 74 | return NULL; |
| 75 | retbuf = PyUnicode_1BYTE_DATA(retval); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 78 | /* Hexlify */ |
| 79 | for (i=j=0; i < arglen; ++i) { |
| 80 | assert(j < resultlen); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 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]; |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 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 | } |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 93 | } |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 94 | assert(j == resultlen); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 95 | |
| 96 | if (return_bytes) { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 97 | retval = PyBytes_FromStringAndSize((const char *)retbuf, resultlen); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 109 | PyObject * _Py_strhex(const char* argbuf, const Py_ssize_t arglen) |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 110 | { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 111 | return _Py_strhex_impl(argbuf, arglen, NULL, 0, 0); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 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. */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 116 | PyObject * _Py_strhex_bytes(const char* argbuf, const Py_ssize_t arglen) |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 117 | { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 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 | |
| 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. */ |
| 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); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 133 | } |