1 /**
2  * $Id$
3  *
4  * This file provides common include files but no specifics for the hashdeep/md5deep system.
5  *
6  * The version information, VERSION, is defined in config.h
7  * AUTHOR and COPYRIGHT moved to main.cpp
8  *
9  */
10 
11 #ifndef __COMMON_H
12 #define __COMMON_H
13 
14 #include "config.h"
15 
16 #define TRUE   1
17 #define FALSE  0
18 #define ONE_MEGABYTE  1048576
19 
20 #define MAX_ALGORITHM_RESIDUE_SIZE 256
21 // Raised for SHA-3
22 #define MAX_ALGORITHM_CONTEXT_SIZE 384
23 
24 #ifdef _WIN32
25 /* For some reason this doesn't work properly with mingw */
26 #undef HAVE_EXTERN_PROGNAME
27 #endif
28 
29 #include <assert.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <ctype.h>
33 #include <errno.h>
34 
35 #ifdef HAVE_STDLIB_H
36 # include <stdlib.h>
37 #endif
38 
39 #ifdef HAVE_LIMITS_H
40 # include <limits.h>
41 #endif
42 
43 #ifdef HAVE_DIRENT_H
44 # include <dirent.h>
45 #endif
46 
47 #ifdef HAVE_STRING_H
48 # include <string.h>
49 #endif
50 
51 #ifdef HAVE_UNISTD_H
52 # include <unistd.h>
53 #endif
54 
55 #ifdef HAVE_SYS_TIME_H
56 #include <sys/time.h>
57 #endif
58 
59 #ifdef HAVE_TIME_H
60 #include <time.h>
61 #endif
62 
63 #ifdef HAVE_SYS_TYPES_H
64 # include <sys/types.h>
65 #endif
66 
67 #ifdef HAVE_SYS_PARAM_H
68 # include <sys/param.h>
69 #endif
70 
71 #ifdef HAVE_SYS_STAT_H
72 # include <sys/stat.h>
73 #endif
74 
75 #ifdef HAVE_SYS_IOCTL_H
76 # include <sys/ioctl.h>
77 #endif
78 
79 #ifdef HAVE_SYS_MOUNT_H
80 # include <sys/mount.h>
81 #endif
82 
83 #ifdef HAVE_SYS_DISK_H
84 # include <sys/disk.h>
85 #endif
86 
87 #ifdef HAVE_LIBGEN_H
88 # include <libgen.h>
89 #endif
90 
91 #ifdef HAVE_SYS_CDEFS_H
92 # include <sys/cdefs.h>
93 #endif
94 
95 #ifdef HAVE_PTHREAD_H
96 # include <pthread.h>
97 #endif
98 
99 // This allows us to open standard input in binary mode by default
100 // See http://gnuwin32.sourceforge.net/compile.html for more
101 #ifdef HAVE_FCNTL_H
102 # include <fcntl.h>
103 #endif
104 
105 /* If we are including inttypes.h, mmake sure __STDC_FORMAT_MACROS is defined */
106 #ifdef HAVE_INTTYPES_H
107 #ifndef __STDC_FORMAT_MACROS
108 #define __STDC_FORMAT_MACROS
109 #endif
110 # include <inttypes.h>
111 #else
112 // This should really have been caught during configuration, but just in case...
113 # error Unable to work without inttypes.h!
114 #endif
115 
116 #ifdef HAVE_STDINT_H
117 #include <stdint.h>
118 #endif
119 
120 // A few operating systems (e.g. versions of OpenBSD) don't meet the
121 // C99 standard and don't define the PRI??? macros we use to display
122 // large numbers. We have to do something to help those systems, so
123 // we guess. This snippet was copied from the FreeBSD source tree,
124 // so hopefully it should work on the other BSDs too.
125 #ifndef PRIu64
126 #define PRIu64 "llu"
127 #endif
128 
129 // Strings have to be long enough to handle inputs from matched hashing files.
130 // The NSRL is already larger than 256 bytes. We go longer to be safer.
131 #define MAX_STRING_LENGTH    2048
132 #define MAX_TIME_STRING_LENGTH  31
133 
134 // This denotes when we don't know the file size.
135 #define UNKNOWN_FILE_SIZE  0xfffffffffffffffeLL
136 
137 // LINE_LENGTH is different between UNIX and WIN32 and is defined below
138 #define MAX_FILENAME_LENGTH   LINE_LENGTH - 41
139 
140 #ifdef HAVE_MMAP_H
141 #include <mmap.h>
142 #endif
143 
144 #ifdef HAVE_SYS_MMAN_H
145 #include <sys/mman.h>
146 #endif
147 
148 #if defined(__cplusplus)
149 #include <string>
150 #include <algorithm>
151 #include <iostream>
152 #include <fstream>
153 #include <ctype.h>
154 #include <vector>
155 #include <sstream>
156 
157 
158 /* Some nice C++ manipulation routines */
159 
makelower(const std::string & a)160 inline std::string makelower(const std::string &a)
161 {
162     std::string ret(a);
163     std::transform(ret.begin(), ret.end(), ret.begin(), ::tolower);
164     return ret;
165 }
166 
makeupper(const std::string & a)167 inline std::string makeupper(const std::string &a)
168 {
169     std::string ret(a);
170     std::transform(ret.begin(), ret.end(), ret.begin(), ::toupper);
171     return ret;
172 }
173 
STRINGS_EQUAL(const std::string & a,const std::string & b)174 inline bool STRINGS_EQUAL(const std::string &a,const std::string &b)
175 {
176     return a==b;
177 }
178 #endif
179 
180 
181 #ifdef _WIN32
182 /*****************************************************************
183  *** Windows support.
184  *** Previously in tchar-local.h.
185  *** Moved here for simplicity
186  * TCHAR:
187  *
188  * On POSIX systems, TCHAR is defined to be char.
189  * On WIN32 systems, TCHAR is wchar_t.
190  *
191  * TCHAR is used for filenames of files to hash.
192  *
193  * We can convert this to UTF-8 using the GNU utf8 package from sourceforce.
194  *
195  *
196  */
197 
198 /*
199  * __MSVCRT_VERSION__ specifies which version of Microsoft's DLL we require.
200  * Mingw defines this to be 0x0600 by default.
201  *
202  * We want version 0x0601 by default to get 64-bit stat functions and _gmtime64.
203  * This is defined in configure.ac.
204  *
205  * If we aren't compiling under mingw, define it by hand.
206  * (Perhaps some poor soul was forced to port this project to VC++.)
207  */
208 #ifndef __MSVCRT_VERSION__
209 #define __MSVCRT_VERSION__ 0x0601
210 #endif
211 
212 /* For reasons I don't understand we typedef wchar_t TCHAR doesn't
213  * work for some reasons for mingw, so we use this.
214  */
215 #ifndef _TCHAR_DEFINED
216 //#define TCHAR wchar_t
217 //#define _TCHAR wchar_t
218 //#define _TCHAR_DEFINED
219 #endif
220 
221 #include <windows.h>
222 #include <windowsx.h>
223 #include <tchar.h>
224 #include <wchar.h>
225 
226 #if defined(__cplusplus)
227 #include <string>
228 /*
229  * Internally we use tstring.
230  * ON WIN32: we get a std::wstring.
231  * ON POSIX: we get a std::string.
232  */
233 
234 #define tstring std::wstring
235 #endif
236 
237 // The current cross compiler for OS X->Windows does not support a few
238 // critical error codes normally defined in errno.h. Because we need
239 // these to detect fatal errors while reading files, we have them here.
240 // These will hopefully get wrapped into the Windows API sometime soon.
241 #ifndef ENOTBLK
242 #define ENOTBLK   15   // Not a block device
243 #endif
244 
245 #ifndef ETXTBSY
246 #define ETXTBSY   26   // Text file busy
247 #endif
248 
249 #ifndef EAGAIN
250 #define EAGAIN    35   // Resource temporarily unavailable
251 #endif
252 
253 #ifndef EALREADY
254 #define EALREADY  37   // Operation already in progress
255 #endif
256 
257 #define NEWLINE "\n"
258 #define CMD_PROMPT "C:\\>"
259 #define DIR_SEPARATOR   '\\'
260 
261 #define LINE_LENGTH 72
262 #define ftello   ftello64	/* use the 64-bit version */
263 #define fseeko   fseeko64	/* use the 64-bit version */
264 #define off_t    off64_t
265 #define tlstat64(path,buf)   _wstat64(path,buf) // on windows, use _wstat64
266 #endif
267 
268 // Set up the environment for the *nix operating systems (Mac, Linux,
269 // BSD, Solaris, and really everybody except Microsoft Windows)
270 //
271 // Do this by faking the wide-character functions
272 
273 #ifndef _WIN32
274 /* The next few paragraphs are similar to tchar.h when UNICODE
275  * is not defined---that is, in the absence of a wide-char mode,
276  * all become the standard char * functions.
277  * tstring is then typedef'ed to be a std::string.
278  * This works just fine on Linux and OS X
279  */
280 
281 typedef char TCHAR;			// no TCHAR on POSIX; use CHAR
282 #define  _TDIR      DIR			// no _TDIR, use dir...
283 #define  _TEXT(A)   A
284 #define  _T(A)      A
285 #define  _tfopen    fopen
286 #define  _topen	    open
287 
288 #define  _topendir  opendir
289 #define  _treaddir  readdir
290 #define  _tdirent   dirent
291 #define  _tclosedir closedir
292 
293 #define  _lstat     lstat
294 #define  _wstat64(path,buf)   stat(path,buf)
295 #define  __stat64   stat
296 
297 
298 #if defined(__cplusplus)
299 typedef std::string tstring;
300 #endif
301 
302 #  define CMD_PROMPT	"$"
303 #  define DIR_SEPARATOR   '/'
304 #  define NEWLINE		"\n"
305 #  define LINE_LENGTH	74
306 
307 
308 #  ifndef HAVE_FSEEKO
309 #    define fseeko fseek
310 #    define ftello ftell
311 #  endif
312 #endif
313 
314 #ifndef __BEGIN_DECLS
315 #if defined(__cplusplus)
316 #define __BEGIN_DECLS   extern "C" {
317 #define __END_DECLS     }
318 #else
319 #define __BEGIN_DECLS
320 #define __END_DECLS
321 #endif
322 #endif
323 /* End Win32 */
324 #endif /* ifndef __COMMON_H */
325