mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 10:18:13 +00:00
LibELF: Move ELF hash functions to their own file (and make constexpr)
This commit is contained in:
parent
145923bdc9
commit
37420f1baf
3 changed files with 67 additions and 35 deletions
64
Userland/Libraries/LibELF/Hashes.h
Normal file
64
Userland/Libraries/LibELF/Hashes.h
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*
|
||||
* Copyright (c) 2019-2020, Andrew Kaster <andrewdkaster@gmail.com>
|
||||
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/StringView.h>
|
||||
|
||||
namespace ELF {
|
||||
|
||||
constexpr u32 compute_sysv_hash(const StringView& name)
|
||||
{
|
||||
// SYSV ELF hash algorithm
|
||||
// Note that the GNU HASH algorithm has less collisions
|
||||
|
||||
u32 hash = 0;
|
||||
|
||||
for (auto ch : name) {
|
||||
hash = hash << 4;
|
||||
hash += ch;
|
||||
|
||||
const u32 top_nibble_of_hash = hash & 0xf0000000u;
|
||||
hash ^= top_nibble_of_hash >> 24;
|
||||
hash &= ~top_nibble_of_hash;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
constexpr u32 compute_gnu_hash(const StringView& name)
|
||||
{
|
||||
// GNU ELF hash algorithm
|
||||
u32 hash = 5381;
|
||||
|
||||
for (auto ch : name)
|
||||
hash = hash * 33 + ch;
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue