Skip to content

Commit 1abb878

Browse files
codec: Fix HashCRC16/32 for SMO (#244)
1 parent 9913a12 commit 1abb878

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

include/codec/seadHashCRC16.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ class HashCRC16
1414
};
1515

1616
static u32 calcHash(const void* ptr, u32 size);
17+
#if SEAD_HASHCRC_WITHCONTEXT
1718
static u32 calcHashWithContext(Context* context, const void* ptr, u32 size);
19+
#endif
1820

1921
static u32 calcStringHash(const char* str);
2022
static u32 calcStringHash(const SafeString& str) { return calcStringHash(str.cstr()); }
23+
#if SEAD_HASHCRC_WITHCONTEXT
2124
static u32 calcStringHashWithContext(Context* context, const char* str);
2225
static u32 calcStringHashWithContext(Context* context, const SafeString& str)
2326
{
2427
return calcStringHashWithContext(context, str.cstr());
2528
}
29+
#endif
2630

2731
static void initialize();
2832

include/codec/seadHashCRC32.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ class HashCRC32
1414
};
1515

1616
static u32 calcHash(const void* ptr, u32 size);
17+
#if SEAD_HASHCRC_WITHCONTEXT
1718
static u32 calcHashWithContext(Context* context, const void* ptr, u32 size);
19+
#endif
1820

1921
static u32 calcStringHash(const char* str);
2022
static u32 calcStringHash(const SafeString& str) { return calcStringHash(str.cstr()); }
23+
#if SEAD_HASHCRC_WITHCONTEXT
2124
static u32 calcStringHashWithContext(Context* context, const char* str);
2225
static u32 calcStringHashWithContext(Context* context, const SafeString& str)
2326
{
2427
return calcStringHashWithContext(context, str.cstr());
2528
}
29+
#endif
2630

2731
static void initialize();
2832

include/seadVersion.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
/// Add isExistFileImpl_() to sead::ArchiveRes
3838
/// SEAD_ARCHIVERES_ISCONST
3939
/// Make most functions in sead::ArchiveRes const
40+
/// SEAD_HASHCRC_WITHCONTEXT:
41+
/// Add calcHashWithContext and calcStringHashWithContext to HashCRC16/32
4042

4143
#if SEAD_VERSION == SEAD_VERSION_BOTW
4244
#define SEAD_SAFESTRING_NONVIRTUAL 0
@@ -51,6 +53,7 @@
5153
#define SEAD_ARCHIVERES_TRYGETFILEPATH 0
5254
#define SEAD_ARCHIVERES_ISEXISTFILEIMPL 1
5355
#define SEAD_ARCHIVERES_ISCONST 1
56+
#define SEAD_HASHCRC_WITHCONTEXT 1
5457
#elif SEAD_VERSION == SEAD_VERSION_SMO
5558
#define SEAD_SAFESTRING_NONVIRTUAL 0
5659
#define SEAD_RESOURCEMGR_TRYCREATE_NO_FACTORY_NAME 0
@@ -64,6 +67,7 @@
6467
#define SEAD_ARCHIVERES_TRYGETFILEPATH 0
6568
#define SEAD_ARCHIVERES_ISEXISTFILEIMPL 0
6669
#define SEAD_ARCHIVERES_ISCONST 0
70+
#define SEAD_HASHCRC_WITHCONTEXT 0
6771
#elif SEAD_VERSION == SEAD_VERSION_SPL3 or SEAD_VERSION == SEAD_VERSION_TOTK or \
6872
SEAD_VERSION == SEAD_VERSION_SMBW
6973
#define SEAD_SAFESTRING_NONVIRTUAL 1
@@ -78,6 +82,7 @@
7882
#define SEAD_ARCHIVERES_TRYGETFILEPATH 1
7983
#define SEAD_ARCHIVERES_ISEXISTFILEIMPL 1
8084
#define SEAD_ARCHIVERES_ISCONST 1
85+
#define SEAD_HASHCRC_WITHCONTEXT 1
8186
#endif
8287

8388
/// feature-specific macros

modules/src/codec/seadHashCRC16.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void HashCRC16::initialize()
1919
sInitialized = true;
2020
}
2121

22+
#if SEAD_HASHCRC_WITHCONTEXT
2223
u32 HashCRC16::calcHash(const void* ptr, u32 size)
2324
{
2425
Context ctx;
@@ -38,6 +39,22 @@ u32 HashCRC16::calcHashWithContext(Context* context, const void* ptr, u32 size)
3839
return hash;
3940
}
4041

42+
#else
43+
44+
u32 HashCRC16::calcHash(const void* ptr, u32 size)
45+
{
46+
if (!sInitialized)
47+
initialize();
48+
49+
u32 hash = 0;
50+
const u8* data = static_cast<const u8*>(ptr);
51+
for (u32 i = 0; i < size; i++)
52+
hash = sTable[data[i] ^ (hash & 0xFF)] ^ (hash >> 8);
53+
return hash;
54+
}
55+
#endif
56+
57+
#if SEAD_HASHCRC_WITHCONTEXT
4158
u32 HashCRC16::calcStringHash(const char* str)
4259
{
4360
Context ctx;
@@ -56,4 +73,18 @@ u32 HashCRC16::calcStringHashWithContext(Context* context, const char* str)
5673
return hash;
5774
}
5875

76+
#else
77+
78+
u32 HashCRC16::calcStringHash(const char* str)
79+
{
80+
if (!sInitialized)
81+
initialize();
82+
83+
u32 hash = 0;
84+
while (*str)
85+
hash = sTable[*str++ ^ (hash & 0xFF)] ^ (hash >> 8);
86+
return hash;
87+
}
88+
#endif
89+
5990
} // namespace sead

modules/src/codec/seadHashCRC32.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ void HashCRC32::initialize()
1919
sInitialized = true;
2020
}
2121

22+
#if SEAD_HASHCRC_WITHCONTEXT
2223
u32 HashCRC32::calcHash(const void* ptr, u32 size)
2324
{
2425
Context ctx;
@@ -38,6 +39,22 @@ u32 HashCRC32::calcHashWithContext(Context* context, const void* ptr, u32 size)
3839
return ~hash;
3940
}
4041

42+
#else
43+
44+
u32 HashCRC32::calcHash(const void* ptr, u32 size)
45+
{
46+
if (!sInitialized)
47+
initialize();
48+
49+
u32 hash = -1;
50+
const u8* data = static_cast<const u8*>(ptr);
51+
for (u32 i = 0; i < size; i++)
52+
hash = sTable[data[i] ^ (hash & 0xFF)] ^ (hash >> 8);
53+
return ~hash;
54+
}
55+
#endif
56+
57+
#if SEAD_HASHCRC_WITHCONTEXT
4158
u32 HashCRC32::calcStringHash(const char* str)
4259
{
4360
Context ctx;
@@ -56,4 +73,18 @@ u32 HashCRC32::calcStringHashWithContext(Context* context, const char* str)
5673
return ~hash;
5774
}
5875

76+
#else
77+
78+
u32 HashCRC32::calcStringHash(const char* str)
79+
{
80+
if (!sInitialized)
81+
initialize();
82+
83+
u32 hash = -1;
84+
while (*str)
85+
hash = sTable[*str++ ^ (hash & 0xFF)] ^ (hash >> 8);
86+
return ~hash;
87+
}
88+
#endif
89+
5990
} // namespace sead

0 commit comments

Comments
 (0)