-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhash-match.c
More file actions
160 lines (154 loc) · 4.6 KB
/
hash-match.c
File metadata and controls
160 lines (154 loc) · 4.6 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) 2022-2023, smartmx <smartmx@qq.com>
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-04-03 smartmx the first version
* 2023-03-02 smartmx hash_match_group function will return the pointer to index
*
*/
#include "hash-match.h"
/**
* check all calculated hash code in the group, compare with hash_code.
*
* @param start the hash match group start address.
* @param end the hash match group end address.
* @param hash_code the hash code which is need be checked.
*
* @return None.
*/
void hash_match_check(const hash_match_t *start, const hash_match_t *end, uint32_t hash_code, uint32_t hash_key_len)
{
const hash_match_t *check_start = (const hash_match_t *)start;
HASH_MATCH_PRINTF("check hash_code:'%08x'\n", hash_code);
while (1)
{
if (end <= check_start)
{
break;
}
if (*(check_start->hash_code) == hash_code)
{
HASH_MATCH_PRINTF("hash_code is duplicated with hash_key:\n");
HASH_MATCH_PRINTF("1st:");
for (uint32_t i = 0; i < check_start->hash_key_len; i++)
{
HASH_MATCH_PRINTF(" %02x", check_start->hash_key_src[i]);
}
HASH_MATCH_PRINTF("\n");
HASH_MATCH_PRINTF("2nd:");
for (uint32_t i = 0; i < end->hash_key_len; i++)
{
HASH_MATCH_PRINTF(" %02x", end->hash_key_src[i]);
}
HASH_MATCH_PRINTF("\n");
if (check_start->hash_key_len == hash_key_len)
{
HASH_MATCH_PRINTF("hash_code and hash_key_len %d are all duplicated, you must fix it!\n", hash_key_len);
break;
}
}
check_start++;
}
}
/**
* init a hash code group.
*
* @param start the hash match group start address.
* @param end the hash match group end address.
*
* @return None.
*/
void hash_match_group_init(const hash_match_t *start, const hash_match_t *end)
{
const hash_match_t *init_start = (const hash_match_t *)start;
while (1)
{
if (end <= init_start)
{
break;
}
*(init_start->hash_code) = hash_match_caculate(init_start->hash_key_src, init_start->hash_key_len);
#if HASH_MATCH_INIT_CHECK
hash_match_check(start, init_start, *(init_start->hash_code), init_start->hash_key_len);
#endif
init_start++;
}
}
/**
* check all calculated hash code in the group, compare with hash_code.
*
* @param start the hash match group start address.
* @param end the hash match group end address.
* @param src the hash key source pointer.
* @param len the length of hash key source.
* @param (void*) parameter when calling handler.
*
* @return None.
*/
void *hash_match_group(const hash_match_t *start, const hash_match_t *end, const void *src, uint32_t len, void *param)
{
const hash_match_t *find_start = (const hash_match_t *)start;
uint32_t hash_code;
hash_code = hash_match_caculate(src, len);
while (1)
{
if (end <= find_start)
{
break;
}
if ((*(find_start->hash_code) == hash_code) && (len == find_start->hash_key_len))
{
#if HASH_MATCH_COMPARE_KEY
if (hash_match_memcmp(src, find_start->hash_key_src, len) == HASH_MATCH_MEMCMP_SAME)
{
#endif
if (find_start->handler != NULL)
{
find_start->handler(param);
}
return (void *)find_start;
#if HASH_MATCH_COMPARE_KEY
}
#endif
}
find_start++;
}
return NULL;
}
/**
* list all hash key info in a hash code group.
*
* @param start the hash match group start address.
* @param end the hash match group end address.
*
* @return None.
*/
void hash_match_group_list(const hash_match_t *start, const hash_match_t *end)
{
const hash_match_t *list_start = (const hash_match_t *)start;
uint32_t len;
while (1)
{
if (end <= list_start)
{
break;
}
len = 0;
HASH_MATCH_PRINTF("hash match key:%02x", list_start->hash_key_src[len]);
len++;
while (len < list_start->hash_key_len)
{
HASH_MATCH_PRINTF(" %02x", list_start->hash_key_src[len]);
len++;
}
HASH_MATCH_PRINTF("\nhash match key len:%d\n", list_start->hash_key_len);
HASH_MATCH_PRINTF("hash match hash code:%08x\n", *(list_start->hash_code));
#if HASH_MATCH_SAVE_DESC
HASH_MATCH_PRINTF("hash match description: %s\n", list_start->hash_desc);
#endif
list_start++;
}
}