1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Simple fast portable secure hashing library */
3 /* Copyright(c) 2016-2020 LoRd_MuldeR <mulder2@gmx.de> */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */
6 /* and associated documentation files (the "Software"), to deal in the Software without */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish, */
8 /* distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the */
9 /* Software is furnished to do so, subject to the following conditions: */
11 /* The above copyright notice and this permission notice shall be included in all copies or */
12 /* substantial portions of the Software. */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
16 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
19 /* ---------------------------------------------------------------------------------------------- */
33 #define max(a,b) (((a) > (b)) ? (a) : (b))
36 #define min(a,b) (((a) < (b)) ? (a) : (b))
39 static inline void crit_exit(const char *const msg)
41 printf("\n\n%s\n\n", msg);
49 static inline uint32_t make_seed(void)
52 if (rand_s(&seed) != 0)
54 crit_exit("FATAL: System PRNG initialization has failed!");
59 static inline void invert_byte_buffer(uint8_t *const buffer, const size_t size)
61 const size_t words = size / sizeof(uint32_t);
62 const size_t bytes = size % sizeof(uint32_t);
63 uint8_t *pos = buffer;
64 for (size_t i = 0; i < words; ++i)
66 *((uint32_t*)pos) = (~(*((uint32_t*)pos)));
67 pos += sizeof(uint32_t);
69 for (size_t i = 0; i < bytes; ++i)
72 pos += sizeof(uint8_t);
76 static inline uint_fast32_t hamming_distance(const uint8_t *const a, const uint8_t *const b, const size_t len)
78 uint_fast32_t distance = 0U;
79 for (size_t i = 0; i < len; ++i)
81 distance += HAMMING_DISTANCE_LUT[a[i]][b[i]];
86 static inline void get_time_str(char *const time_string, const size_t buff_size)
91 if (localtime_s(&time_info, ¤t_time))
93 crit_exit("FATAL: localtime_s() has failed!");
95 const size_t len = strftime(time_string, buff_size, "%H:%M:%S", &time_info);
96 if ((len < 1) || (len >= buff_size))
98 crit_exit("FATAL: strftime() has failed!");
102 static inline uint32_t max_ui32(const uint32_t a, const uint32_t b)
104 return (a > b) ? a : b;
107 static inline uint_fast16_t max_ui16(const uint_fast16_t a, const uint_fast16_t b)
109 return (a > b) ? a : b;
112 static inline uint_fast8_t max_ui8(const uint_fast8_t a, const uint_fast8_t b)
114 return (a > b) ? a : b;
117 static inline double clip_dbl(const double min, const double val, const double max)
119 return (val > max) ? max : ((val < min) ? min : val);
122 static inline uint32_t adler32(const uint8_t *const buffer, size_t buflength)
124 uint32_t s1 = 1, s2 = 0;
125 for (size_t n = 0; n < buflength; n++)
127 s1 = (s1 + buffer[n]) % 65521;
128 s2 = (s2 + s1) % 65521;
130 return (s2 << 16) | s1;
133 #endif //INC_COMMON_H