1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License as
4  * published by the Free Software Foundation; either version 2 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
9  * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
10  * NON-INFRINGEMENT.  See the GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
15  * MA 02110-1301, USA.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU General Public License in all respects
23  * for all of the code used other than OpenSSL.  If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so.  If you
26  * do not wish to do so, delete this exception statement from your
27  * version.  If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29  */
30 #ifndef _COMMON_H_
31 #define _COMMON_H_
32 
33 #if defined(__CYGWIN32__) && !defined(__CYGWIN64__)
34 int fseeko64(FILE * fp, int64_t offset, int whence);
35 int64_t ftello64(FILE * fp);
36 #undef fseek
37 #define fseek fseeko64
38 #undef ftello
39 #define ftello ftello64
40 #endif
41 
42 #if defined(__FreeBSD__) || defined(__OpenBSD__)
43 #undef rand
44 #define rand lrand48
45 #undef srand
46 #define srand srand48
47 #endif
48 
49 #include <time.h>
50 
51 #define SWAP(x, y)                                                             \
52 	{                                                                          \
53 		unsigned char tmp = x;                                                 \
54 		x = y;                                                                 \
55 		y = tmp;                                                               \
56 	}
57 
58 #define SWAP32(x)                                                              \
59 	x = (((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00)                    \
60 		 | ((x << 8) & 0x00FF0000)                                             \
61 		 | ((x << 24) & 0xFF000000));
62 
63 #define PCT                                                                    \
64 	{                                                                          \
65 		struct tm * lt;                                                        \
66 		time_t tc = time(NULL);                                                \
67 		lt = localtime(&tc);                                                   \
68 		printf("%02d:%02d:%02d  ", lt->tm_hour, lt->tm_min, lt->tm_sec);       \
69 	}
70 
71 #ifndef MAX
72 #define MAX(x, y) ((x) > (y) ? (x) : (y))
73 #endif
74 
75 #ifndef MIN
76 #define MIN(x, y) ((x) > (y) ? (y) : (x))
77 #endif
78 
79 #ifndef ABS
80 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
81 #endif
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 extern const unsigned char ZERO[33];
88 
89 extern void calctime(time_t t, float calc);
90 
91 /// Retrieves the working directory.
92 extern char * get_current_working_directory(void);
93 
94 /// Trim excess whitespace from the right-most of \a line.
95 extern void rtrim(char * line);
96 
97 extern int is_string_number(const char * str);
98 
99 extern int get_ram_size(void);
100 
101 extern char * getVersion(const char * progname,
102 						 const unsigned int maj,
103 						 const unsigned int min,
104 						 const unsigned int submin,
105 						 const char * rev,
106 						 const unsigned int beta,
107 						 const unsigned int rc);
108 
109 /// Returns the number of CPU/cores available and online.
110 extern int get_nb_cpus(void);
111 
112 extern int maccmp(unsigned char * mac1, unsigned char * mac2);
113 
114 extern char * mac2string(unsigned char * mac_address);
115 
116 extern int hexCharToInt(unsigned char c);
117 
118 extern int
119 hexStringToArray(char * in, int in_length, unsigned char * out, int out_length);
120 
121 /// Return the mac address bytes (or null if it's not a mac address)
122 extern int
123 getmac(const char * macAddress, const int strict, unsigned char * mac);
124 
125 /// Read a line of characters inputted by the user
126 extern int readLine(char line[], int maxlength);
127 
128 extern int hexToInt(char s[], int len);
129 
130 extern void rtrim(char * line);
131 
132 extern int string_has_suffix(const char * str, const char * suf);
133 
134 // Returns 1 if the current process is running in the background, 0 otherwise
135 extern int is_background(void);
136 
137 #ifdef __cplusplus
138 };
139 #endif
140 
141 #endif
142