From c9d5f25901f035c9b72b0e425008a439f032c491 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 4 Jun 2025 09:42:13 +0800 Subject: [PATCH 1/5] digest.c: fix endianness handling: swap bytes conditionally --- src/digest.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/digest.c b/src/digest.c index 0e75499..5ea29c9 100644 --- a/src/digest.c +++ b/src/digest.c @@ -145,13 +145,21 @@ void rev_memcpy(char *dst, const void *src, int len) { // n.b. ripe templating to e.g. _store_from_integral<> if switching to c++ void _store_from_int32(const uint32_t hash, char *output, const int leaveRaw) { if (leaveRaw) { +#if BYTE_ORDER == LITTLE_ENDIAN rev_memcpy(output, &hash, sizeof(uint32_t)); +#else + memcpy(output, &hash, sizeof(uint32_t)); +#endif } else snprintf(output, sizeof(uint32_t)*2 + 1, "%08x", hash); } void _store_from_int64(const uint64_t hash, char *output, const int leaveRaw) { if (leaveRaw) { +#if BYTE_ORDER == LITTLE_ENDIAN rev_memcpy(output, &hash, sizeof(uint64_t)); +#else + memcpy(output, &hash, sizeof(uint64_t)); +#endif } else snprintf(output, sizeof(uint64_t)*2 + 1, "%016" PRIx64, hash); } From 13e6086eaf997a1942e10e90177414db55c1f9e3 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 4 Jun 2025 10:07:12 +0800 Subject: [PATCH 2/5] digest.c: fix endianness for xxh3_128bits --- src/digest.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/digest.c b/src/digest.c index 5ea29c9..c0a7307 100644 --- a/src/digest.c +++ b/src/digest.c @@ -324,12 +324,12 @@ SEXP digest(SEXP Txt, SEXP Algo, SEXP Length, SEXP Skip, SEXP Leave_raw, SEXP Se XXH128_hash_t val = XXH3_128bits_withSeed(txt, nChar, seed); - // need something a bit fancier here if (leaveRaw) { - rev_memcpy(output, &val.high64, 8); - rev_memcpy(output + 8, &val.low64, 8); + XXH128_canonical_t canon; + XXH128_canonicalFromHash(&canon, val); + memcpy(output, &canon, 16); } else { - snprintf(output, 128, "%016" PRIx64 "%016" PRIx64, val.high64, val.low64); + snprintf(output, 128, "%016" PRIx64 "%016" PRIx64, val.high64, val.low64); } break; } @@ -647,10 +647,10 @@ SEXP digest(SEXP Txt, SEXP Algo, SEXP Length, SEXP Skip, SEXP Leave_raw, SEXP Se XXH128_hash_t val = XXH3_128bits_digest(state); XXH3_freeState(state); - // need something a bit fancier here if (leaveRaw) { - rev_memcpy(output, &val.high64, 8); - rev_memcpy(output + 8, &val.low64, 8); + XXH128_canonical_t canon; + XXH128_canonicalFromHash(&canon, val); + memcpy(output, &canon, 16); } else { snprintf(output, 128, "%016" PRIx64 "%016" PRIx64, val.high64, val.low64); } From 4836132c4b090b32806aecf9c3e723838d2a2ae5 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 4 Jun 2025 10:08:42 +0800 Subject: [PATCH 3/5] digest.c: non-functional: tabs to spaces --- src/digest.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/digest.c b/src/digest.c index c0a7307..4006261 100644 --- a/src/digest.c +++ b/src/digest.c @@ -442,9 +442,9 @@ SEXP digest(SEXP Txt, SEXP Algo, SEXP Length, SEXP Skip, SEXP Leave_raw, SEXP Se while ( ( nChar = fread( buf, 1, sizeof( buf ), fp ) ) > 0) SHA512_Update( &ctx, buf, nChar ); } - /* Calling SHA512_Final, because SHA512_End will already - convert the hash to a string, and we also want RAW */ - SHA512_Final(sha512sum, &ctx); + /* Calling SHA512_Final, because SHA512_End will already + convert the hash to a string, and we also want RAW */ + SHA512_Final(sha512sum, &ctx); _store_from_char_ptr(sha512sum, output, output_length, leaveRaw); break; From 1fc2c19a3f21e0dd9a7a97613c25757bd7c29fa7 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 4 Jun 2025 12:38:58 +0800 Subject: [PATCH 4/5] ChangeLog: add an entry for endianness fix --- ChangeLog | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ChangeLog b/ChangeLog index 780f799..a0f7b39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2025-06-04 Sergey Fedorov + + * src/digest.c: Fix endianness handling + 2025-06-03 Dirk Eddelbuettel * DESCRIPTION (Version, Date): Roll micro version and date From 3e6537bf36535bafad67e4940c8638cf51ed3675 Mon Sep 17 00:00:00 2001 From: Sergey Fedorov Date: Wed, 4 Jun 2025 21:07:59 +0800 Subject: [PATCH 5/5] ChangeLog: adjust spacing Co-authored-by: Dirk Eddelbuettel --- ChangeLog | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a0f7b39..56aa583 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,4 @@ -2025-06-04 Sergey Fedorov +2025-06-04 Sergey Fedorov * src/digest.c: Fix endianness handling