1 /********************************************************************** 2 Copyright(c) 2024 Intel Corporation All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions 6 are met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in 11 the documentation and/or other materials provided with the 12 distribution. 13 * Neither the name of Intel Corporation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 **********************************************************************/ 29 30 /** 31 * @file rolling_hashx_internal.h 32 * @brief Internal fingerprint functions based on rolling hash 33 * 34 * rolling_hash2 - checks hash in a sliding window based on random 64-bit hash. 35 */ 36 37 #ifndef _ROLLING_INTERNAL_HASHX_H_ 38 #define _ROLLING_INTERNAL_HASHX_H_ 39 40 #ifdef __cplusplus 41 extern "C" { 42 #endif 43 44 #include <stdint.h> 45 #include "rolling_hashx.h" 46 47 /** 48 * @brief Initialize state object for rolling hash2 49 * 50 * @param state Structure holding state info on current rolling hash 51 * @param w Window width (1 <= w <= 32) 52 * @returns 0 - success, -1 - failure 53 */ 54 int 55 _rolling_hash2_init(struct isal_rh_state2 *state, uint32_t w); 56 57 /** 58 * @brief Reset the hash state history 59 * 60 * @param state Structure holding state info on current rolling hash 61 * @param init_bytes Optional window size buffer to pre-init hash 62 * @returns none 63 */ 64 void 65 _rolling_hash2_reset(struct isal_rh_state2 *state, uint8_t *init_bytes); 66 67 /** 68 * @brief Run rolling hash function until trigger met or max length reached 69 * 70 * Checks for trigger based on a random hash in a sliding window. 71 * @param state Structure holding state info on current rolling hash 72 * @param buffer Pointer to input buffer to run windowed hash on 73 * @param max_len Max length to run over input 74 * @param mask Mask bits ORed with hash before test with trigger 75 * @param trigger Match value to compare with windowed hash at each input byte 76 * @param offset Offset from buffer to match, set if match found 77 * @returns ISAL_FINGERPRINT_RET_HIT - match found, ISAL_FINGERPRINT_RET_MAX - exceeded max length 78 */ 79 int 80 _rolling_hash2_run(struct isal_rh_state2 *state, uint8_t *buffer, uint32_t max_len, uint32_t mask, 81 uint32_t trigger, uint32_t *offset); 82 83 /** 84 * @brief Generate an appropriate mask to target mean hit rate 85 * 86 * @param mean Target chunk size in bytes 87 * @param shift Bits to rotate result to get independent masks 88 * @returns 32-bit mask value 89 */ 90 uint32_t 91 _rolling_hashx_mask_gen(long mean, int shift); 92 93 #ifdef __cplusplus 94 } 95 #endif 96 97 #endif // _ROLLING_INTERNAL_HASHX_H_ 98