1 /* ----------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Generate tables utility program */
3 /* Copyright(c) 2016-2017 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 /* ----------------------------------------------------------------------------------------------- */
32 static inline void crit_exit(const char *const msg)
34 printf("\n\n%s\n\n", msg);
42 static inline uint32_t make_seed(void)
45 if (rand_s(&seed) != 0)
47 crit_exit("FATAL: System PRNG initialization has failed!");
52 static inline void invert_byte_buffer(uint8_t *const buffer, const size_t size)
54 const size_t words = size / sizeof(uint32_t);
55 const size_t bytes = size % sizeof(uint32_t);
56 uint8_t *pos = buffer;
57 for (size_t i = 0; i < words; ++i)
59 *((uint32_t*)pos) = (~(*((uint32_t*)pos)));
60 pos += sizeof(uint32_t);
62 for (size_t i = 0; i < bytes; ++i)
65 pos += sizeof(uint8_t);
69 static inline uint32_t hamming_distance(const uint8_t *const a, const uint8_t *const b, const size_t len)
71 uint32_t distance = 0U;
72 for (size_t i = 0; i < len; ++i)
74 distance += HAMMING_DISTANCE_LUT[a[i]][b[i]];
79 static inline void get_time_str(char *const time_string, const size_t buff_size)
84 if (localtime_s(&time_info, ¤t_time))
86 crit_exit("FATAL: localtime_s() has failed!");
88 const size_t len = strftime(time_string, buff_size, "%H:%M:%S", &time_info);
89 if ((len < 1) || (len >= buff_size))
91 crit_exit("FATAL: strftime() has failed!");
95 static inline uint32_t max_ui32(const uint32_t a, const uint32_t b)
97 return (a > b) ? a : b;
100 static inline uint_fast16_t max_ui16(const uint_fast16_t a, const uint_fast16_t b)
102 return (a > b) ? a : b;
105 static inline uint_fast8_t max_ui8(const uint_fast8_t a, const uint_fast8_t b)
107 return (a > b) ? a : b;
110 static inline double clip_dbl(const double min, const double val, const double max)
112 return (val > max) ? max : ((val < min) ? min : val);
115 static inline uint32_t adler32(const uint8_t *const buffer, size_t buflength)
117 uint32_t s1 = 1, s2 = 0;
118 for (size_t n = 0; n < buflength; n++)
120 s1 = (s1 + buffer[n]) % 65521;
121 s2 = (s2 + s1) % 65521;
123 return (s2 << 16) | s1;
126 #endif //INC_COMMON_H