22 # define _RABINKARP_H_
31 # define RABINKARP_SEED 1
38 # define RABINKARP_MULT 0x08104225
44 # define RABINKARP_INVM 0x98f009ad
50 # define RABINKARP_ADJ 0x08104224
53 typedef struct _rabinkarp {
59 static inline uint32_t uint32_pow(uint32_t m,
size_t p)
72 static inline void rabinkarp_init(rabinkarp_t *sum)
75 sum->hash = RABINKARP_SEED;
79 static inline void rabinkarp_update(rabinkarp_t *sum,
const unsigned char *buf,
82 for (
size_t i = len; i; i--) {
83 sum->hash = sum->hash * RABINKARP_MULT + *buf++;
86 sum->mult *= uint32_pow(RABINKARP_MULT, len);
89 static inline void rabinkarp_rotate(rabinkarp_t *sum,
unsigned char out,
93 sum->hash * RABINKARP_MULT + in - sum->mult * (out + RABINKARP_ADJ);
96 static inline void rabinkarp_rollin(rabinkarp_t *sum,
unsigned char in)
98 sum->hash = sum->hash * RABINKARP_MULT + in;
100 sum->mult *= RABINKARP_MULT;
103 static inline void rabinkarp_rollout(rabinkarp_t *sum,
unsigned char out)
106 sum->mult *= RABINKARP_INVM;
107 sum->hash -= sum->mult * (out + RABINKARP_ADJ);
110 static inline uint32_t rabinkarp_digest(rabinkarp_t *sum)