From ef1988c903958e74f33e2684d6321dfeb5a062b5 Mon Sep 17 00:00:00 2001 From: MouriNaruto Date: Sat, 13 Apr 2024 15:33:55 +0800 Subject: [PATCH] Update RHash to latest master (https://github.com/rhash/RHash/commit/d916787590b9dc57eb9d420fd13e469160e0b04c) which is after v1.4.4. --- NanaZip.Codecs/RHash/aich.c | 2 +- NanaZip.Codecs/RHash/byte_order.h | 4 ++-- NanaZip.Codecs/RHash/hex.c | 3 +++ NanaZip.Codecs/RHash/torrent.c | 26 +++++++++++++------------- NanaZip.Codecs/RHash/util.h | 7 ++++--- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/NanaZip.Codecs/RHash/aich.c b/NanaZip.Codecs/RHash/aich.c index 99e1758d4..aa76a197d 100644 --- a/NanaZip.Codecs/RHash/aich.c +++ b/NanaZip.Codecs/RHash/aich.c @@ -451,7 +451,7 @@ size_t rhash_aich_export(const aich_ctx* ctx, void* out, size_t size) if (out_ptr && ARE_OPENSSL_METHODS(ctx->sha1_methods)) { int* error_ptr = (int*)((char*)out + head_size + offsetof(aich_ctx, error)); *error_ptr |= AICH_CTX_OSSL_FLAG; - RHASH_ASSERT(sizeof(*error_ptr) == sizeof(ctx->error)); + RHASH_ASSERT(sizeof(*error_ptr) == sizeof(ctx->error)); } #endif return exported_size; diff --git a/NanaZip.Codecs/RHash/byte_order.h b/NanaZip.Codecs/RHash/byte_order.h index 1a4f34fd0..068d90602 100644 --- a/NanaZip.Codecs/RHash/byte_order.h +++ b/NanaZip.Codecs/RHash/byte_order.h @@ -61,8 +61,8 @@ extern "C" { #ifdef RHASH_BYTE_ORDER #elif defined(CPU_IA32) || defined(CPU_X64) || defined(__ia64) || defined(__ia64__) || \ defined(__alpha__) || defined(_M_ALPHA) || defined(vax) || defined(MIPSEL) || \ - defined(_ARM_) || defined(__arm__) || defined(_M_ARM) || defined(_M_ARM64) || \ - defined(_M_ARM64EC) || defined(__loongarch64) + defined(_ARM_) || defined(__arm__) || defined(_M_ARM64) || defined(_M_ARM64EC) || \ + defined(__loongarch64) # define RHASH_BYTE_ORDER RHASH_BYTE_ORDER_LE #elif defined(__sparc) || defined(__sparc__) || defined(sparc) || \ defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_POWER) || \ diff --git a/NanaZip.Codecs/RHash/hex.c b/NanaZip.Codecs/RHash/hex.c index 36af0f99a..1b0133f71 100644 --- a/NanaZip.Codecs/RHash/hex.c +++ b/NanaZip.Codecs/RHash/hex.c @@ -112,6 +112,9 @@ size_t rhash_base64_url_encoded_helper(char* dst, const unsigned char* src, size char buffer[164]; RHASH_ASSERT((BASE64_LENGTH(B64_CHUNK_SIZE) + 4) <= sizeof(buffer)); RHASH_ASSERT((B64_CHUNK_SIZE % 6) == 0); +#ifdef __clang_analyzer__ + memset(buffer, 0, sizeof(buffer)); +#endif if (url_encode) { size_t result_length = 0; for (; length > 0; src += B64_CHUNK_SIZE) { diff --git a/NanaZip.Codecs/RHash/torrent.c b/NanaZip.Codecs/RHash/torrent.c index 69885ca77..73d64abe4 100644 --- a/NanaZip.Codecs/RHash/torrent.c +++ b/NanaZip.Codecs/RHash/torrent.c @@ -254,9 +254,9 @@ void bt_final(torrent_ctx* ctx, unsigned char result[20]) static int bt_str_ensure_length(torrent_ctx* ctx, size_t length) { char* new_str; - if (ctx->error) - return 0; - if (length >= ctx->content.allocated) { + if (ctx->error) + return 0; + if (length >= ctx->content.allocated) { length++; /* allocate one character more */ if (length < 64) length = 64; else length = (length + 255) & ~255; @@ -529,10 +529,10 @@ static void bt_generate_torrent(torrent_ctx* ctx) /* calculate BTIH */ SHA1_INIT(ctx); - if (ctx->content.str) { - SHA1_UPDATE(ctx, (unsigned char*)ctx->content.str + info_start_pos, - ctx->content.length - info_start_pos - 1); - } + if (ctx->content.str) { + SHA1_UPDATE(ctx, (unsigned char*)ctx->content.str + info_start_pos, + ctx->content.length - info_start_pos - 1); + } SHA1_FINAL(ctx, ctx->btih); } @@ -764,7 +764,7 @@ size_t bt_export(const torrent_ctx* ctx, void* out, size_t size) if (out_ptr) { if (size < exported_size) return 0; - assert(ctx->content.str != NULL); + assert(ctx->content.str != NULL); memcpy(out_ptr, ctx->content.str, ctx->content.length + 1); out_ptr += aligned_length; } @@ -773,11 +773,11 @@ size_t bt_export(const torrent_ctx* ctx, void* out, size_t size) assert(!out || (size_t)(out_ptr - (char*)out) == exported_size); #if defined(USE_OPENSSL) - if (out_ptr && ARE_OPENSSL_METHODS(ctx->sha1_methods)) { - size_t* error_ptr = (size_t*)((char*)out + head_size + offsetof(torrent_ctx, error)); - *error_ptr |= BT_CTX_OSSL_FLAG; - RHASH_ASSERT(sizeof(*error_ptr) == sizeof(ctx->error)); - } + if (out_ptr && ARE_OPENSSL_METHODS(ctx->sha1_methods)) { + size_t* error_ptr = (size_t*)((char*)out + head_size + offsetof(torrent_ctx, error)); + *error_ptr |= BT_CTX_OSSL_FLAG; + RHASH_ASSERT(sizeof(*error_ptr) == sizeof(ctx->error)); + } #endif return exported_size; } diff --git a/NanaZip.Codecs/RHash/util.h b/NanaZip.Codecs/RHash/util.h index 1b76243a0..213da8644 100644 --- a/NanaZip.Codecs/RHash/util.h +++ b/NanaZip.Codecs/RHash/util.h @@ -2,6 +2,8 @@ #ifndef UTIL_H #define UTIL_H +#include /* for aligned_alloc and __GLIBC__ version macros */ + #ifdef __cplusplus extern "C" { #endif @@ -42,11 +44,11 @@ extern "C" { # define rhash_aligned_free(ptr) _aligned_free(ptr) #elif !defined(NO_STDC_ALIGNED_ALLOC) && (__STDC_VERSION__ >= 201112L || defined(_ISOC11_SOURCE)) \ - && !defined(__APPLE__) && !defined(__HAIKU__) \ + && !defined(__APPLE__) && !defined(__HAIKU__) && !defined(__sun) \ + && (!defined(__GLIBC__) || __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 15)) \ && (!defined(__ANDROID_API__) || __ANDROID_API__ >= 28) # define HAS_STDC_ALIGNED_ALLOC -# include # define rhash_aligned_alloc(alignment, size) aligned_alloc((alignment), ALIGN_SIZE_BY(size, alignment)) # define rhash_aligned_free(ptr) free(ptr) @@ -57,7 +59,6 @@ extern "C" { # if !defined(NO_POSIX_ALIGNED_ALLOC) && (_POSIX_VERSION >= 200112L || _XOPEN_SOURCE >= 600) # define HAS_POSIX_ALIGNED_ALLOC -# include # define rhash_aligned_alloc(alignment, size) rhash_px_aalloc((alignment), ALIGN_SIZE_BY(size, sizeof(void*))) # define rhash_aligned_free(ptr) free(ptr) void* rhash_px_aalloc(size_t size, size_t alignment);