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 | { |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 11 | assert(arglen >= 0); |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 12 | |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 13 | Py_UCS1 sep_char = 0; |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 14 | if (sep) { |
Pablo Galindo | 938d9a0 | 2019-06-01 21:02:08 +0100 | [diff] [blame] | 15 | Py_ssize_t seplen = PyObject_Length((PyObject*)sep); |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 16 | if (seplen < 0) { |
| 17 | return NULL; |
| 18 | } |
| 19 | if (seplen != 1) { |
| 20 | PyErr_SetString(PyExc_ValueError, "sep must be length 1."); |
| 21 | return NULL; |
| 22 | } |
| 23 | if (PyUnicode_Check(sep)) { |
| 24 | if (PyUnicode_READY(sep)) |
| 25 | return NULL; |
| 26 | if (PyUnicode_KIND(sep) != PyUnicode_1BYTE_KIND) { |
| 27 | PyErr_SetString(PyExc_ValueError, "sep must be ASCII."); |
| 28 | return NULL; |
| 29 | } |
| 30 | sep_char = PyUnicode_READ_CHAR(sep, 0); |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 31 | } |
| 32 | else if (PyBytes_Check(sep)) { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 33 | sep_char = PyBytes_AS_STRING(sep)[0]; |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 34 | } |
| 35 | else { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 36 | PyErr_SetString(PyExc_TypeError, "sep must be str or bytes."); |
| 37 | return NULL; |
| 38 | } |
| 39 | if (sep_char > 127 && !return_bytes) { |
| 40 | PyErr_SetString(PyExc_ValueError, "sep must be ASCII."); |
| 41 | return NULL; |
| 42 | } |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 43 | } |
| 44 | else { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 45 | bytes_per_sep_group = 0; |
| 46 | } |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 47 | |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 48 | unsigned int abs_bytes_per_sep = abs(bytes_per_sep_group); |
| 49 | Py_ssize_t resultlen = 0; |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 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 | |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 65 | PyObject *retval; |
| 66 | Py_UCS1 *retbuf; |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 67 | if (return_bytes) { |
| 68 | /* If _PyBytes_FromSize() were public we could avoid malloc+copy. */ |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 69 | retval = PyBytes_FromStringAndSize(NULL, resultlen); |
| 70 | if (!retval) { |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 71 | return NULL; |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 72 | } |
| 73 | retbuf = (Py_UCS1 *)PyBytes_AS_STRING(retval); |
| 74 | } |
| 75 | else { |
| 76 | retval = PyUnicode_New(resultlen, 127); |
| 77 | if (!retval) { |
| 78 | return NULL; |
| 79 | } |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 80 | retbuf = PyUnicode_1BYTE_DATA(retval); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 83 | /* Hexlify */ |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 84 | Py_ssize_t i, j; |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 85 | for (i=j=0; i < arglen; ++i) { |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 86 | assert((j + 1) < resultlen); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 87 | unsigned char c; |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 88 | c = (argbuf[i] >> 4) & 0x0f; |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 89 | retbuf[j++] = Py_hexdigits[c]; |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 90 | c = argbuf[i] & 0x0f; |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 91 | retbuf[j++] = Py_hexdigits[c]; |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 92 | if (bytes_per_sep_group && i < arglen - 1) { |
| 93 | Py_ssize_t anchor; |
| 94 | anchor = (bytes_per_sep_group > 0) ? (arglen - 1 - i) : (i + 1); |
| 95 | if (anchor % abs_bytes_per_sep == 0) { |
| 96 | retbuf[j++] = sep_char; |
| 97 | } |
| 98 | } |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 99 | } |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 100 | assert(j == resultlen); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 101 | |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 102 | #ifdef Py_DEBUG |
Victor Stinner | 455df97 | 2020-04-15 14:05:24 +0200 | [diff] [blame] | 103 | if (!return_bytes) { |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 104 | assert(_PyUnicode_CheckConsistency(retval, 1)); |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | return retval; |
| 109 | } |
| 110 | |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 111 | PyObject * _Py_strhex(const char* argbuf, const Py_ssize_t arglen) |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 112 | { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 113 | return _Py_strhex_impl(argbuf, arglen, NULL, 0, 0); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* Same as above but returns a bytes() instead of str() to avoid the |
| 117 | * need to decode the str() when bytes are needed. */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 118 | 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] | 119 | { |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 120 | return _Py_strhex_impl(argbuf, arglen, NULL, 0, 1); |
| 121 | } |
| 122 | |
| 123 | /* These variants include support for a separator between every N bytes: */ |
| 124 | |
| 125 | PyObject * _Py_strhex_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group) |
| 126 | { |
| 127 | return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 0); |
| 128 | } |
| 129 | |
| 130 | /* Same as above but returns a bytes() instead of str() to avoid the |
| 131 | * need to decode the str() when bytes are needed. */ |
| 132 | PyObject * _Py_strhex_bytes_with_sep(const char* argbuf, const Py_ssize_t arglen, const PyObject* sep, const int bytes_per_group) |
| 133 | { |
| 134 | return _Py_strhex_impl(argbuf, arglen, sep, bytes_per_group, 1); |
Gregory P. Smith | e3f6393 | 2015-04-26 00:41:00 +0000 | [diff] [blame] | 135 | } |