You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uthash_strcase.h 5.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef UTHASH_STRCASE_H_
  17. #define UTHASH_STRCASE_H_
  18. #ifdef UTHASH_H
  19. #error Invalid include order: uthash is already included
  20. #endif
  21. #include "libcryptobox/cryptobox.h"
  22. #include "libutil/util.h"
  23. /* Utils for uthash tuning */
  24. #ifndef HASH_CASELESS
  25. #define HASH_FUNCTION(key, keylen, num_bkts, hashv, bkt) \
  26. do { \
  27. hashv = (__typeof(hashv)) rspamd_cryptobox_fast_hash(key, keylen, rspamd_hash_seed()); \
  28. bkt = (hashv) & (num_bkts - 1); \
  29. } while (0)
  30. #define HASH_KEYCMP(a, b, len) memcmp(a, b, len)
  31. #else
  32. #define HASH_FUNCTION(key, keylen, num_bkts, hashv, bkt) \
  33. do { \
  34. unsigned _len = keylen; \
  35. rspamd_cryptobox_fast_hash_state_t _hst; \
  36. unsigned _leftover = keylen % 8; \
  37. unsigned _fp, _i; \
  38. const uint8_t *_s = (const uint8_t *) (key); \
  39. union { \
  40. struct { \
  41. unsigned char c1, c2, c3, c4, c5, c6, c7, c8; \
  42. } c; \
  43. uint64_t pp; \
  44. } _u; \
  45. _fp = _len - _leftover; \
  46. rspamd_cryptobox_fast_hash_init(&_hst, rspamd_hash_seed()); \
  47. for (_i = 0; _i != _fp; _i += 8) { \
  48. _u.c.c1 = _s[_i], _u.c.c2 = _s[_i + 1], _u.c.c3 = _s[_i + 2], _u.c.c4 = _s[_i + 3]; \
  49. _u.c.c5 = _s[_i + 4], _u.c.c6 = _s[_i + 5], _u.c.c7 = _s[_i + 6], _u.c.c8 = _s[_i + 7]; \
  50. _u.c.c1 = lc_map[_u.c.c1]; \
  51. _u.c.c2 = lc_map[_u.c.c2]; \
  52. _u.c.c3 = lc_map[_u.c.c3]; \
  53. _u.c.c4 = lc_map[_u.c.c4]; \
  54. _u.c.c1 = lc_map[_u.c.c5]; \
  55. _u.c.c2 = lc_map[_u.c.c6]; \
  56. _u.c.c3 = lc_map[_u.c.c7]; \
  57. _u.c.c4 = lc_map[_u.c.c8]; \
  58. rspamd_cryptobox_fast_hash_update(&_hst, &_u, sizeof(_u)); \
  59. } \
  60. _u.pp = 0; \
  61. switch (_leftover) { \
  62. case 7: \
  63. /* fallthrough */ _u.c.c7 = lc_map[(unsigned char) _s[_i++]]; \
  64. case 6: \
  65. /* fallthrough */ _u.c.c6 = lc_map[(unsigned char) _s[_i++]]; \
  66. case 5: \
  67. /* fallthrough */ _u.c.c5 = lc_map[(unsigned char) _s[_i++]]; \
  68. case 4: \
  69. /* fallthrough */ _u.c.c4 = lc_map[(unsigned char) _s[_i++]]; \
  70. case 3: \
  71. /* fallthrough */ _u.c.c3 = lc_map[(unsigned char) _s[_i++]]; \
  72. case 2: \
  73. /* fallthrough */ _u.c.c2 = lc_map[(unsigned char) _s[_i++]]; \
  74. case 1: \
  75. /* fallthrough */ _u.c.c1 = lc_map[(unsigned char) _s[_i]]; \
  76. rspamd_cryptobox_fast_hash_update(&_hst, &_u, sizeof(_u)); \
  77. break; \
  78. } \
  79. hashv = (__typeof(hashv)) rspamd_cryptobox_fast_hash_final(&_hst); \
  80. bkt = (hashv) & (num_bkts - 1); \
  81. } while (0)
  82. #define HASH_KEYCMP(a, b, len) rspamd_lc_cmp(a, b, len)
  83. #endif
  84. #include "uthash.h"
  85. #endif /* UTHASH_STRCASE_H_ */