core.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. int hydro_init(void)
  2. {
  3. if (hydro_random_init() != 0) {
  4. abort();
  5. }
  6. return 0;
  7. }
  8. void hydro_memzero(void *pnt, size_t len)
  9. {
  10. volatile unsigned char *volatile pnt_ =
  11. (volatile unsigned char *volatile)pnt;
  12. size_t i = (size_t)0U;
  13. while (i < len) {
  14. pnt_[i++] = 0U;
  15. }
  16. }
  17. void hydro_increment(uint8_t *n, size_t len)
  18. {
  19. size_t i;
  20. uint_fast16_t c = 1U;
  21. for (i = 0; i < len; i++) {
  22. c += (uint_fast16_t)n[i];
  23. n[i] = (uint8_t)c;
  24. c >>= 8;
  25. }
  26. }
  27. char *hydro_bin2hex(
  28. char *hex, size_t hex_maxlen, const uint8_t *bin, size_t bin_len)
  29. {
  30. size_t i = (size_t)0U;
  31. unsigned int x;
  32. int b;
  33. int c;
  34. if (bin_len >= SIZE_MAX / 2 || hex_maxlen <= bin_len * 2U) {
  35. abort();
  36. }
  37. while (i < bin_len) {
  38. c = bin[i] & 0xf;
  39. b = bin[i] >> 4;
  40. x = (unsigned char)(87U + c + (((c - 10U) >> 8) & ~38U)) << 8 |
  41. (unsigned char)(87U + b + (((b - 10U) >> 8) & ~38U));
  42. hex[i * 2U] = (char)x;
  43. x >>= 8;
  44. hex[i * 2U + 1U] = (char)x;
  45. i++;
  46. }
  47. hex[i * 2U] = 0U;
  48. return hex;
  49. }
  50. int hydro_hex2bin(uint8_t *bin, size_t bin_maxlen, const char *hex,
  51. size_t hex_len, const char *ignore, size_t *bin_len, const char **hex_end)
  52. {
  53. size_t bin_pos = (size_t)0U;
  54. size_t hex_pos = (size_t)0U;
  55. int ret = 0;
  56. unsigned char c;
  57. unsigned char c_alpha0, c_alpha;
  58. unsigned char c_num0, c_num;
  59. uint8_t c_acc = 0U;
  60. uint8_t c_val;
  61. unsigned char state = 0U;
  62. while (hex_pos < hex_len) {
  63. c = (unsigned char)hex[hex_pos];
  64. c_num = c ^ 48U;
  65. c_num0 = (c_num - 10U) >> 8;
  66. c_alpha = (c & ~32U) - 55U;
  67. c_alpha0 = ((c_alpha - 10U) ^ (c_alpha - 16U)) >> 8;
  68. if ((c_num0 | c_alpha0) == 0U) {
  69. if (ignore != NULL && state == 0U && strchr(ignore, c) != NULL) {
  70. hex_pos++;
  71. continue;
  72. }
  73. break;
  74. }
  75. c_val = (uint8_t)((c_num0 & c_num) | (c_alpha0 & c_alpha));
  76. if (bin_pos >= bin_maxlen) {
  77. ret = -1;
  78. errno = ERANGE;
  79. break;
  80. }
  81. if (state == 0U) {
  82. c_acc = c_val * 16U;
  83. } else {
  84. bin[bin_pos++] = c_acc | c_val;
  85. }
  86. state = ~state;
  87. hex_pos++;
  88. }
  89. if (state != 0U) {
  90. hex_pos--;
  91. }
  92. if (hex_end != NULL) {
  93. *hex_end = &hex[hex_pos];
  94. }
  95. if (bin_len != NULL) {
  96. *bin_len = bin_pos;
  97. }
  98. return ret;
  99. }
  100. bool hydro_equal(const void *b1_, const void *b2_, size_t len)
  101. {
  102. const volatile uint8_t *volatile b1 = (const volatile uint8_t *volatile)b1_;
  103. const volatile uint8_t *volatile b2 = (const volatile uint8_t *volatile)b2_;
  104. size_t i;
  105. uint8_t d = (uint8_t)0U;
  106. if (b1 == b2) {
  107. d = ~d;
  108. }
  109. for (i = 0U; i < len; i++) {
  110. d |= b1[i] ^ b2[i];
  111. }
  112. return (bool)(1 & ((d - 1) >> 8));
  113. }
  114. int hydro_compare(const uint8_t *b1_, const uint8_t *b2_, size_t len)
  115. {
  116. const volatile uint8_t *volatile b1 = (const volatile uint8_t *volatile)b1_;
  117. const volatile uint8_t *volatile b2 = (const volatile uint8_t *volatile)b2_;
  118. uint8_t gt = 0U;
  119. uint8_t eq = 1U;
  120. size_t i;
  121. i = len;
  122. while (i != 0U) {
  123. i--;
  124. gt |= ((b2[i] - b1[i]) >> 8) & eq;
  125. eq &= ((b2[i] ^ b1[i]) - 1) >> 8;
  126. }
  127. return (int)(gt + gt + eq) - 1;
  128. }