-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsimplehash.c
111 lines (101 loc) · 2.49 KB
/
simplehash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (c) 2023, smartmx <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-03-03 smartmx the first version
*
*/
/*
* @Note:
* This simple hash algorithm comes from: https://github.com/jiejieTop/cmd-parser.
*/
#include "simplehash.h"
/**
* calculate hash_code.
*
* @param hash_key the hash_key start address.
* @param len the length of the hash_key.
*
* @return type uint32_t, the result of calculated value.
*/
uint32_t simplehash_caculate32(const void *hash_key, uint32_t len)
{
const uint8_t *hash_key_ptr = (const uint8_t *)hash_key;
uint32_t clen, data;
uint32_t hash = 0;
uint32_t seed = SIMPLEHASH_SEED_VALUE;
/* body */
for (clen = 0; clen < len; clen++)
{
data = *hash_key_ptr;
hash = (hash ^ seed) + data;
hash_key_ptr++;
}
return hash;
}
/**
* upper all lower letters in hash key and calculate the hash_code.
*
* @param c the char value.
*
* @return type uint8_t, the upper char value.
*/
static uint8_t simplehash_lower_char_upper(uint8_t c)
{
if ((c >= 'a') && (c <= 'z'))
return c + ('A' - 'a');
return c;
}
/**
* upper all lower letters in hash key and calculate the hash_code.
*
* @param c the char value.
*
* @return type uint8_t, the upper char value.
*/
uint8_t simplehash_lower_char_upper_memcmp(const void *src1, const void *src2, uint32_t len)
{
uint8_t *src1_char = (uint8_t *)src1;
uint8_t *src2_char = (uint8_t *)src2;
uint32_t all_len = len;
while (all_len)
{
if (simplehash_lower_char_upper(*src1_char) == simplehash_lower_char_upper(*src2_char))
{
src1_char++;
src2_char++;
all_len--;
}
else
{
return (len - all_len);
}
}
return 0;
}
/**
* upper all lower letters in hash key and calculate the hash_code.
*
* @param hash_key the hash_key start address.
* @param len the length of the hash_key.
*
* @return type uint32_t, the result of calculated value.
*/
uint32_t simplehash_upper_caculate32(const void *hash_key, uint32_t len)
{
const uint8_t *hash_key_ptr = (const uint8_t *)hash_key;
uint32_t clen, data;
uint32_t hash = 0;
uint32_t seed = SIMPLEHASH_SEED_VALUE;
/* body */
for (clen = 0; clen < len; clen++)
{
data = simplehash_lower_char_upper(*hash_key_ptr);
hash = (hash ^ seed) + data;
hash_key_ptr++;
}
return hash;
}