diff --git a/include/codec/seadHashCRC16.h b/include/codec/seadHashCRC16.h index cc1b006c9..56ae655f9 100644 --- a/include/codec/seadHashCRC16.h +++ b/include/codec/seadHashCRC16.h @@ -14,15 +14,19 @@ class HashCRC16 }; static u32 calcHash(const void* ptr, u32 size); +#if SEAD_HASHCRC_WITHCONTEXT static u32 calcHashWithContext(Context* context, const void* ptr, u32 size); +#endif static u32 calcStringHash(const char* str); static u32 calcStringHash(const SafeString& str) { return calcStringHash(str.cstr()); } +#if SEAD_HASHCRC_WITHCONTEXT static u32 calcStringHashWithContext(Context* context, const char* str); static u32 calcStringHashWithContext(Context* context, const SafeString& str) { return calcStringHashWithContext(context, str.cstr()); } +#endif static void initialize(); diff --git a/include/codec/seadHashCRC32.h b/include/codec/seadHashCRC32.h index 1f2675b78..d331d0dbd 100644 --- a/include/codec/seadHashCRC32.h +++ b/include/codec/seadHashCRC32.h @@ -14,15 +14,19 @@ class HashCRC32 }; static u32 calcHash(const void* ptr, u32 size); +#if SEAD_HASHCRC_WITHCONTEXT static u32 calcHashWithContext(Context* context, const void* ptr, u32 size); +#endif static u32 calcStringHash(const char* str); static u32 calcStringHash(const SafeString& str) { return calcStringHash(str.cstr()); } +#if SEAD_HASHCRC_WITHCONTEXT static u32 calcStringHashWithContext(Context* context, const char* str); static u32 calcStringHashWithContext(Context* context, const SafeString& str) { return calcStringHashWithContext(context, str.cstr()); } +#endif static void initialize(); diff --git a/include/seadVersion.h b/include/seadVersion.h index 2e955911a..4d0cc6074 100644 --- a/include/seadVersion.h +++ b/include/seadVersion.h @@ -37,6 +37,8 @@ /// Add isExistFileImpl_() to sead::ArchiveRes /// SEAD_ARCHIVERES_ISCONST /// Make most functions in sead::ArchiveRes const +/// SEAD_HASHCRC_WITHCONTEXT: +/// Add calcHashWithContext and calcStringHashWithContext to HashCRC16/32 #if SEAD_VERSION == SEAD_VERSION_BOTW #define SEAD_SAFESTRING_NONVIRTUAL 0 @@ -51,6 +53,7 @@ #define SEAD_ARCHIVERES_TRYGETFILEPATH 0 #define SEAD_ARCHIVERES_ISEXISTFILEIMPL 1 #define SEAD_ARCHIVERES_ISCONST 1 +#define SEAD_HASHCRC_WITHCONTEXT 1 #elif SEAD_VERSION == SEAD_VERSION_SMO #define SEAD_SAFESTRING_NONVIRTUAL 0 #define SEAD_RESOURCEMGR_TRYCREATE_NO_FACTORY_NAME 0 @@ -64,6 +67,7 @@ #define SEAD_ARCHIVERES_TRYGETFILEPATH 0 #define SEAD_ARCHIVERES_ISEXISTFILEIMPL 0 #define SEAD_ARCHIVERES_ISCONST 0 +#define SEAD_HASHCRC_WITHCONTEXT 0 #elif SEAD_VERSION == SEAD_VERSION_SPL3 or SEAD_VERSION == SEAD_VERSION_TOTK or \ SEAD_VERSION == SEAD_VERSION_SMBW #define SEAD_SAFESTRING_NONVIRTUAL 1 @@ -78,6 +82,7 @@ #define SEAD_ARCHIVERES_TRYGETFILEPATH 1 #define SEAD_ARCHIVERES_ISEXISTFILEIMPL 1 #define SEAD_ARCHIVERES_ISCONST 1 +#define SEAD_HASHCRC_WITHCONTEXT 1 #endif /// feature-specific macros diff --git a/modules/src/codec/seadHashCRC16.cpp b/modules/src/codec/seadHashCRC16.cpp index 503182fa7..d45ee41a2 100644 --- a/modules/src/codec/seadHashCRC16.cpp +++ b/modules/src/codec/seadHashCRC16.cpp @@ -19,6 +19,7 @@ void HashCRC16::initialize() sInitialized = true; } +#if SEAD_HASHCRC_WITHCONTEXT u32 HashCRC16::calcHash(const void* ptr, u32 size) { Context ctx; @@ -38,6 +39,22 @@ u32 HashCRC16::calcHashWithContext(Context* context, const void* ptr, u32 size) return hash; } +#else + +u32 HashCRC16::calcHash(const void* ptr, u32 size) +{ + if (!sInitialized) + initialize(); + + u32 hash = 0; + const u8* data = static_cast(ptr); + for (u32 i = 0; i < size; i++) + hash = sTable[data[i] ^ (hash & 0xFF)] ^ (hash >> 8); + return hash; +} +#endif + +#if SEAD_HASHCRC_WITHCONTEXT u32 HashCRC16::calcStringHash(const char* str) { Context ctx; @@ -56,4 +73,18 @@ u32 HashCRC16::calcStringHashWithContext(Context* context, const char* str) return hash; } +#else + +u32 HashCRC16::calcStringHash(const char* str) +{ + if (!sInitialized) + initialize(); + + u32 hash = 0; + while (*str) + hash = sTable[*str++ ^ (hash & 0xFF)] ^ (hash >> 8); + return hash; +} +#endif + } // namespace sead diff --git a/modules/src/codec/seadHashCRC32.cpp b/modules/src/codec/seadHashCRC32.cpp index 99fa39afa..f7f2dfb01 100644 --- a/modules/src/codec/seadHashCRC32.cpp +++ b/modules/src/codec/seadHashCRC32.cpp @@ -19,6 +19,7 @@ void HashCRC32::initialize() sInitialized = true; } +#if SEAD_HASHCRC_WITHCONTEXT u32 HashCRC32::calcHash(const void* ptr, u32 size) { Context ctx; @@ -38,6 +39,22 @@ u32 HashCRC32::calcHashWithContext(Context* context, const void* ptr, u32 size) return ~hash; } +#else + +u32 HashCRC32::calcHash(const void* ptr, u32 size) +{ + if (!sInitialized) + initialize(); + + u32 hash = -1; + const u8* data = static_cast(ptr); + for (u32 i = 0; i < size; i++) + hash = sTable[data[i] ^ (hash & 0xFF)] ^ (hash >> 8); + return ~hash; +} +#endif + +#if SEAD_HASHCRC_WITHCONTEXT u32 HashCRC32::calcStringHash(const char* str) { Context ctx; @@ -56,4 +73,18 @@ u32 HashCRC32::calcStringHashWithContext(Context* context, const char* str) return ~hash; } +#else + +u32 HashCRC32::calcStringHash(const char* str) +{ + if (!sInitialized) + initialize(); + + u32 hash = -1; + while (*str) + hash = sTable[*str++ ^ (hash & 0xFF)] ^ (hash >> 8); + return ~hash; +} +#endif + } // namespace sead