| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /*
- * 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++;
- }
- }
|