SARC: Difference between revisions

1,396 bytes added ,  3 years ago
m
imported>Leoetlino
 
(9 intermediate revisions by 2 users not shown)
Line 3: Line 3:
== Structure ==
== Structure ==
The basic structure of SARC files is documented on the [[mk8:SARC (File Format)|MK8 wiki]].
The basic structure of SARC files is documented on the [[mk8:SARC (File Format)|MK8 wiki]].
=== Hash algorithm ===
Nintendo's algorithm uses an unsigned 32-bit integer for the hash variable. Also note that Nintendo iterates over each byte, not each string character.
Here is an accurate implementation of the algorithm, written in C++:
<source lang="c++">
// key is equal to 0x65
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>
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>, was added 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'' ==