|
44 | 44 | #define XXH128_DIGESTSIZE 16 |
45 | 45 | #define XXH128_BLOCKSIZE 64 |
46 | 46 |
|
47 | | -static const char _hexdigits[] = "0123456789abcdef"; |
48 | 47 |
|
49 | 48 |
|
50 | 49 | /* Get a buffer from an object. Rejects str with hashlib-compatible error. */ |
@@ -219,9 +218,13 @@ static PyObject *xxh32_hexdigest(PyObject *self, PyObject *const *args, |
219 | 218 | char retbuf[XXH32_DIGESTSIZE * 2]; |
220 | 219 | int i, j; |
221 | 220 | for (i = j = 0; i < XXH32_DIGESTSIZE; i++) { |
222 | | - unsigned char byte = digest[i]; |
223 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
224 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 221 | + unsigned char c; |
| 222 | + c = (digest[i] >> 4) & 0xf; |
| 223 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 224 | + retbuf[j++] = c; |
| 225 | + c = (digest[i] & 0xf); |
| 226 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 227 | + retbuf[j++] = c; |
225 | 228 | } |
226 | 229 |
|
227 | 230 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -285,9 +288,13 @@ static PyObject *xxh64_hexdigest(PyObject *self, PyObject *const *args, |
285 | 288 | char retbuf[XXH64_DIGESTSIZE * 2]; |
286 | 289 | int i, j; |
287 | 290 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
288 | | - unsigned char byte = digest[i]; |
289 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
290 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 291 | + unsigned char c; |
| 292 | + c = (digest[i] >> 4) & 0xf; |
| 293 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 294 | + retbuf[j++] = c; |
| 295 | + c = (digest[i] & 0xf); |
| 296 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 297 | + retbuf[j++] = c; |
291 | 298 | } |
292 | 299 |
|
293 | 300 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -351,9 +358,13 @@ static PyObject *xxh3_64_hexdigest(PyObject *self, PyObject *const *args, |
351 | 358 | char retbuf[XXH64_DIGESTSIZE * 2]; |
352 | 359 | int i, j; |
353 | 360 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
354 | | - unsigned char byte = digest[i]; |
355 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
356 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 361 | + unsigned char c; |
| 362 | + c = (digest[i] >> 4) & 0xf; |
| 363 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 364 | + retbuf[j++] = c; |
| 365 | + c = (digest[i] & 0xf); |
| 366 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 367 | + retbuf[j++] = c; |
357 | 368 | } |
358 | 369 |
|
359 | 370 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -432,9 +443,13 @@ static PyObject *xxh3_128_hexdigest(PyObject *self, PyObject *const *args, |
432 | 443 | char retbuf[XXH128_DIGESTSIZE * 2]; |
433 | 444 | int i, j; |
434 | 445 | for (i = j = 0; i < XXH128_DIGESTSIZE; i++) { |
435 | | - unsigned char byte = digest[i]; |
436 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
437 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 446 | + unsigned char c; |
| 447 | + c = (digest[i] >> 4) & 0xf; |
| 448 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 449 | + retbuf[j++] = c; |
| 450 | + c = (digest[i] & 0xf); |
| 451 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 452 | + retbuf[j++] = c; |
438 | 453 | } |
439 | 454 |
|
440 | 455 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -607,9 +622,13 @@ static PyObject *PYXXH32_hexdigest(PYXXH32Object *self) |
607 | 622 | XXH32_canonicalFromHash((XXH32_canonical_t *)digest, intdigest); |
608 | 623 |
|
609 | 624 | for (i = j = 0; i < XXH32_DIGESTSIZE; i++) { |
610 | | - unsigned char byte = digest[i]; |
611 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
612 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 625 | + unsigned char c; |
| 626 | + c = (digest[i] >> 4) & 0xf; |
| 627 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 628 | + retbuf[j++] = c; |
| 629 | + c = (digest[i] & 0xf); |
| 630 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 631 | + retbuf[j++] = c; |
613 | 632 | } |
614 | 633 |
|
615 | 634 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -955,9 +974,13 @@ static PyObject *PYXXH64_hexdigest(PYXXH64Object *self) |
955 | 974 | XXH64_canonicalFromHash((XXH64_canonical_t *)digest, intdigest); |
956 | 975 |
|
957 | 976 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
958 | | - unsigned char byte = digest[i]; |
959 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
960 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 977 | + unsigned char c; |
| 978 | + c = (digest[i] >> 4) & 0xf; |
| 979 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 980 | + retbuf[j++] = c; |
| 981 | + c = (digest[i] & 0xf); |
| 982 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 983 | + retbuf[j++] = c; |
961 | 984 | } |
962 | 985 |
|
963 | 986 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -1304,9 +1327,13 @@ static PyObject *PYXXH3_64_hexdigest(PYXXH3_64Object *self) |
1304 | 1327 | XXH64_canonicalFromHash((XXH64_canonical_t *)digest, intdigest); |
1305 | 1328 |
|
1306 | 1329 | for (i = j = 0; i < XXH64_DIGESTSIZE; i++) { |
1307 | | - unsigned char byte = digest[i]; |
1308 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
1309 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 1330 | + unsigned char c; |
| 1331 | + c = (digest[i] >> 4) & 0xf; |
| 1332 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 1333 | + retbuf[j++] = c; |
| 1334 | + c = (digest[i] & 0xf); |
| 1335 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 1336 | + retbuf[j++] = c; |
1310 | 1337 | } |
1311 | 1338 |
|
1312 | 1339 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
@@ -1660,9 +1687,13 @@ static PyObject *PYXXH3_128_hexdigest(PYXXH3_128Object *self) |
1660 | 1687 | XXH128_canonicalFromHash((XXH128_canonical_t *)digest, intdigest); |
1661 | 1688 |
|
1662 | 1689 | for (i = j = 0; i < XXH128_DIGESTSIZE; i++) { |
1663 | | - unsigned char byte = digest[i]; |
1664 | | - retbuf[j++] = _hexdigits[byte >> 4]; |
1665 | | - retbuf[j++] = _hexdigits[byte & 0xf]; |
| 1690 | + unsigned char c; |
| 1691 | + c = (digest[i] >> 4) & 0xf; |
| 1692 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 1693 | + retbuf[j++] = c; |
| 1694 | + c = (digest[i] & 0xf); |
| 1695 | + c = (c > 9) ? c + 'a' - 10 : c + '0'; |
| 1696 | + retbuf[j++] = c; |
1666 | 1697 | } |
1667 | 1698 |
|
1668 | 1699 | return PyUnicode_FromStringAndSize(retbuf, sizeof(retbuf)); |
|
0 commit comments