simplehash.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2023, smartmx <smartmx@qq.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-03-03 smartmx the first version
  9. *
  10. */
  11. /*
  12. * @Note:
  13. * This simple hash algorithm comes from: https://github.com/jiejieTop/cmd-parser.
  14. */
  15. #include "simplehash.h"
  16. /**
  17. * calculate hash_code.
  18. *
  19. * @param hash_key the hash_key start address.
  20. * @param len the length of the hash_key.
  21. *
  22. * @return type uint32_t, the result of calculated value.
  23. */
  24. uint32_t simplehash_caculate32(const void *hash_key, uint32_t len)
  25. {
  26. const uint8_t *hash_key_ptr = (const uint8_t *)hash_key;
  27. uint32_t clen, data;
  28. uint32_t hash = 0;
  29. uint32_t seed = SIMPLEHASH_SEED_VALUE;
  30. /* body */
  31. for (clen = 0; clen < len; clen++)
  32. {
  33. data = *hash_key_ptr;
  34. hash = (hash ^ seed) + data;
  35. hash_key_ptr++;
  36. }
  37. return hash;
  38. }
  39. /**
  40. * upper all lower letters in hash key and calculate the hash_code.
  41. *
  42. * @param c the char value.
  43. *
  44. * @return type uint8_t, the upper char value.
  45. */
  46. static uint8_t simplehash_lower_char_upper(uint8_t c)
  47. {
  48. if ((c >= 'a') && (c <= 'z'))
  49. return c + ('A' - 'a');
  50. return c;
  51. }
  52. /**
  53. * upper all lower letters in hash key and calculate the hash_code.
  54. *
  55. * @param c the char value.
  56. *
  57. * @return type uint8_t, the upper char value.
  58. */
  59. uint8_t simplehash_lower_char_upper_memcmp(const void *src1, const void *src2, uint32_t len)
  60. {
  61. uint8_t *src1_char = (uint8_t *)src1;
  62. uint8_t *src2_char = (uint8_t *)src2;
  63. uint32_t all_len = len;
  64. while (all_len)
  65. {
  66. if (simplehash_lower_char_upper(*src1_char) == simplehash_lower_char_upper(*src2_char))
  67. {
  68. src1_char++;
  69. src2_char++;
  70. all_len--;
  71. }
  72. else
  73. {
  74. return (len - all_len);
  75. }
  76. }
  77. return 0;
  78. }
  79. /**
  80. * upper all lower letters in hash key and calculate the hash_code.
  81. *
  82. * @param hash_key the hash_key start address.
  83. * @param len the length of the hash_key.
  84. *
  85. * @return type uint32_t, the result of calculated value.
  86. */
  87. uint32_t simplehash_upper_caculate32(const void *hash_key, uint32_t len)
  88. {
  89. const uint8_t *hash_key_ptr = (const uint8_t *)hash_key;
  90. uint32_t clen, data;
  91. uint32_t hash = 0;
  92. uint32_t seed = SIMPLEHASH_SEED_VALUE;
  93. /* body */
  94. for (clen = 0; clen < len; clen++)
  95. {
  96. data = simplehash_lower_char_upper(*hash_key_ptr);
  97. hash = (hash ^ seed) + data;
  98. hash_key_ptr++;
  99. }
  100. return hash;
  101. }