From b0f5c61cb04693652ed5ff9326aa5b09dc530f55 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 9 Jan 2021 18:51:31 +0100 Subject: [PATCH] Use upper-case for hashing to be case insensitive --- source/romfs_dev.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/source/romfs_dev.c b/source/romfs_dev.c index 35e8242..7164867 100644 --- a/source/romfs_dev.c +++ b/source/romfs_dev.c @@ -377,16 +377,35 @@ int32_t romfsUnmount(const char *name) { //----------------------------------------------------------------------------- +static inline uint8_t normalizePathChar(uint8_t c) { + if (c >= 'a' && c <= 'z') { + return c + 'A' - 'a'; + } else { + return c; + } +} + static uint32_t calcHash(uint32_t parent, const uint8_t *name, uint32_t namelen, uint32_t total) { uint32_t hash = parent ^123456789; uint32_t i; for (i = 0; i < namelen; i++) { hash = (hash >> 5) | (hash << 27); - hash ^= name[i]; + hash ^= normalizePathChar(name[i]); } return hash % total; } +static bool comparePaths(const uint8_t *name1, const uint8_t *name2, uint32_t namelen) { + for (uint32_t i = 0; i < namelen; i++) { + uint8_t c1 = normalizePathChar(name1[i]); + uint8_t c2 = normalizePathChar(name2[i]); + if (c1 != c2) { + return false; + } + } + return true; +} + static int searchForDir(romfs_mount *mount, romfs_dir *parent, const uint8_t *name, uint32_t namelen, romfs_dir **out) { uint64_t parentOff = (uintptr_t) parent - (uintptr_t) mount->dirTable; uint32_t hash = calcHash(parentOff, name, namelen, mount->header.dirHashTableSize / 4); @@ -402,7 +421,7 @@ static int searchForDir(romfs_mount *mount, romfs_dir *parent, const uint8_t *na if (curDir->nameLen != namelen) { continue; } - if (memcmp(curDir->name, name, namelen) != 0) { + if (!comparePaths(curDir->name, name, namelen)) { continue; } *out = curDir; @@ -429,7 +448,7 @@ static int searchForFile(romfs_mount *mount, romfs_dir *parent, const uint8_t *n if (curFile->nameLen != namelen) { continue; } - if (memcmp(curFile->name, name, namelen) != 0) { + if (!comparePaths(curFile->name, name, namelen)) { continue; } *out = curFile;