OSDN Git Service

Added XOR table generator utility.
[mhash384/mhash384.git] / etc / gentable_XOR / src / common.h
1 /* ---------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Simple fast portable secure hashing library                                        */
3 /* Copyright(c) 2016-2020 LoRd_MuldeR <mulder2@gmx.de>                                            */
4 /*                                                                                                */
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:                           */
10 /*                                                                                                */
11 /* The above copyright notice and this permission notice shall be included in all copies or       */
12 /* substantial portions of the Software.                                                          */
13 /*                                                                                                */
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 /* ---------------------------------------------------------------------------------------------- */
20
21 #ifndef INC_COMMON_H
22 #define INC_COMMON_H
23
24 #define _CRT_RAND_S
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <time.h>
28 #include <stdio.h>
29
30 #include "hd_table.h"
31
32 #ifndef max
33 #define max(a,b) (((a) > (b)) ? (a) : (b))
34 #endif
35 #ifndef min
36 #define min(a,b) (((a) < (b)) ? (a) : (b))
37 #endif
38
39 static inline void crit_exit(const char *const msg)
40 {
41         printf("\n\n%s\n\n", msg);
42         flushall();
43 #ifndef NDEBUG
44         abort();
45 #endif
46         for(;;) _exit(666);
47 }
48
49 static inline uint32_t make_seed(void)
50 {
51         uint32_t seed;
52         if (rand_s(&seed) != 0)
53         {
54                 crit_exit("FATAL: System PRNG initialization has failed!");
55         }
56         return seed;
57 }
58
59 static inline void invert_byte_buffer(uint8_t *const buffer, const size_t size)
60 {
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)
65         {
66                 *((uint32_t*)pos) = (~(*((uint32_t*)pos)));
67                 pos += sizeof(uint32_t);
68         }
69         for (size_t i = 0; i < bytes; ++i)
70         {
71                 *pos = (~(*pos));
72                 pos += sizeof(uint8_t);
73         }
74 }
75
76 static inline uint_fast32_t hamming_distance(const uint8_t *const a, const uint8_t *const b, const size_t len)
77 {
78         uint_fast32_t distance = 0U;
79         for (size_t i = 0; i < len; ++i)
80         {
81                 distance += HAMMING_DISTANCE_LUT[a[i]][b[i]];
82         }
83         return distance;
84 }
85
86 static inline void get_time_str(char *const time_string, const size_t buff_size)
87 {
88         time_t current_time;
89         struct tm time_info;
90         time(&current_time);
91         if (localtime_s(&time_info, &current_time))
92         {
93                 crit_exit("FATAL: localtime_s() has failed!");
94         }
95         const size_t len = strftime(time_string, buff_size, "%H:%M:%S", &time_info);
96         if ((len < 1) || (len >= buff_size))
97         {
98                 crit_exit("FATAL: strftime() has failed!");
99         }
100 }
101
102 static inline uint32_t max_ui32(const uint32_t a, const uint32_t b)
103 {
104         return (a > b) ? a : b;
105 }
106
107 static inline uint_fast16_t max_ui16(const uint_fast16_t a, const uint_fast16_t b)
108 {
109         return (a > b) ? a : b;
110 }
111
112 static inline uint_fast8_t max_ui8(const uint_fast8_t a, const uint_fast8_t b)
113 {
114         return (a > b) ? a : b;
115 }
116
117 static inline double clip_dbl(const double min, const double val, const double max)
118 {
119         return (val > max) ? max : ((val < min) ? min : val);
120 }
121
122 static inline uint32_t adler32(const uint8_t *const buffer, size_t buflength)
123 {
124         uint32_t s1 = 1, s2 = 0;
125         for (size_t n = 0; n < buflength; n++)
126         {
127                 s1 = (s1 + buffer[n]) % 65521;
128                 s2 = (s2 + s1) % 65521;
129         }
130         return (s2 << 16) | s1;
131 }
132
133 #endif //INC_COMMON_H