1,158
edits
imported>Leoetlino |
(→Hash algorithm: replace the code snippet with the sead implementation and add more information about the s8 cast) |
||
Line 10: | Line 10: | ||
<source lang="c++"> | <source lang="c++"> | ||
const | // key is equal to 0x65 | ||
const char* | u32 calcHash32(const sead::SafeString& str, u32 key) | ||
{ | |||
const char* str_ = str.cstr(); | |||
u32 result = 0; | |||
// Each character must be treated as a signed value. | |||
// The cast to s8 (not s32) is necessary to avoid unsigned conversions. | |||
for (s32 i = 0; str_[i] != '\0'; i++) | |||
result = result * key + s8(str_[i]); | |||
return result; | |||
} | } | ||
</source> | </source> | ||
Note that in older versions of sead, characters were not explicitly casted to <code>s8</code> so the end result would depend on the signedness of <code>char</code> (which is implementation-defined). For GHS compiled code on the Wii U, <code>char</code> is unsigned whereas it's signed with Clang (AArch64 target). PC tools most likely use a signed <code>char</code> as that is the default signedness for x86 targets. | |||
In newer versions of sead, an explicit cast to <code>signed char</code>, aka <code>std::int8_t</code>, aka <code>s8</code>, to ensure hashes are the same regardless of the target platform. Without the cast, non-ASCII characters (e.g. Japanese UTF-8 bytes) would result in hashes being different across platforms. | |||
== Usage in ''Breath of the Wild'' == | == Usage in ''Breath of the Wild'' == |