更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_hash.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _LOS_HASH_H
33#define _LOS_HASH_H
34
35#include "los_typedef.h"
36
37#ifdef __cplusplus
38#if __cplusplus
39extern "C" {
40#endif /* __cplusplus */
41#endif /* __cplusplus */
42
43/*
44http://www.isthe.com/chongo/tech/comp/fnv/
45FNV算法简介
46FNV算法属于非密码学哈希函数,它最初由Glenn Fowler和Kiem-Phong Vo于1991年在IEEE POSIX P1003.2上首先提出,
47最后由Landon Curt Noll 完善,故该算法以三人姓的首字母命名。
48
49FNV算法目前有三种,分别是FNV-1,FNV-1a和FNV-0,但是FNV-0算法已经被丢弃了。FNV算法的哈希结果有32、64、128、256、512和1024位等长度。
50如果需要哈希结果长度不属于以上任意一种,也可以采用根据Changing the FNV hash size - xor-folding上面的指导进行变换得到。
51FNV-1算法过程如下:
52 hash = offset_basis
53 for each octet_of_data to be hashed
54 hash = hash * FNV_prime
55 hash = hash xor octet_of_data
56 return hash
57
58FNV-1a算法过程如下:
59 hash = offset_basis
60 for each octet_of_data to be hashed
61 hash = hash xor octet_of_data
62 hash = hash * FNV_prime
63 return hash
64FNV-0算法过程如下:
65 hash = 0
66 for each octet_of_data to be hashed
67 hash = hash * FNV_prime
68 hash = hash XOR octet_of_data
69 return hash
70*/
71
72#define FNV1_32A_INIT ((UINT32)0x811c9dc5)
73
74/*
75 * 32 bit magic FNV-1 prime
76 */
77#define FNV_32_PRIME ((UINT32)0x01000193)
78
79LITE_OS_SEC_ALW_INLINE STATIC INLINE UINT32 LOS_HashFNV32aBuf(const VOID *buf, size_t len, UINT32 hval)
80{
81 const UINT8 *hashbuf = (const UINT8 *)buf;
82
83 /*
84 * FNV-1a hash each octet in the buffer
85 */
86 while (len-- != 0) {
87 /* xor the bottom with the current octet */
88 hval ^= (UINT32)*hashbuf++;
89
90 /* multiply by the 32 bit FNV magic prime mod 2^32 */
91 hval *= FNV_32_PRIME;
92 }
93
94 /* return our new hash value */
95 return hval;
96}
97
98LITE_OS_SEC_ALW_INLINE STATIC INLINE UINT32 LOS_HashFNV32aStr(CHAR *str, UINT32 hval)
99{
100 UINT8 *s = (UINT8 *)str;
101
102 /*
103 * FNV-1a hash each octet in the buffer
104 */
105 while (*s) {
106 /* xor the bottom with the current octet */
107 hval ^= (UINT32)*s++;
108
109 /* multiply by the 32 bit FNV magic prime mod 2^32 */
110 hval *= FNV_32_PRIME;
111 }
112
113 /* return our new hash value */
114 return hval;
115}
116
117#ifdef __cplusplus
118#if __cplusplus
119}
120#endif /* __cplusplus */
121#endif /* __cplusplus */
122
123#endif /* _LOS_HASH_H */
LITE_OS_SEC_ALW_INLINE STATIC INLINE UINT32 LOS_HashFNV32aBuf(const VOID *buf, size_t len, UINT32 hval)
Definition: los_hash.h:79
LITE_OS_SEC_ALW_INLINE STATIC INLINE UINT32 LOS_HashFNV32aStr(CHAR *str, UINT32 hval)
Definition: los_hash.h:98
unsigned char UINT8
Definition: los_typedef.h:55
unsigned int UINT32
Definition: los_typedef.h:57
char CHAR
Definition: los_typedef.h:63