1 /*
2  * This file is part of John the Ripper password cracker.
3  *
4  * Some Jumbo-specific tweaks go here. This file must not introduce further
5  * dependenices within the Jumbo tree (except jumbo.o). Use misc.[ho] for
6  * such things (the latter depends on memory.o, logger.o and so on).
7  *
8  * This file is Copyright (c) 2013-2014 magnum, Lukasz and JimF,
9  * and is hereby released to the general public under the following terms:
10  * Redistribution and use in source and binary forms, with or without
11  * modifications, are permitted.
12  *
13  * For Linux' lseek64, ensure you're defining  _LARGEFILE64_SOURCE before
14  * including sys/types.h and unistd.h. Preferably globally (we do it in
15  * ./configure)
16  */
17 #ifndef _JTR_JUMBO_H
18 #define _JTR_JUMBO_H
19 
20 #include "arch.h"
21 #include <stdio.h>
22 #include <errno.h>
23 #if !AC_BUILT || HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #if (!AC_BUILT || HAVE_UNISTD_H) && !_MSC_VER
27 #include <unistd.h>
28 #endif
29 #if !AC_BUILT || HAVE_STRING_H
30 #include <string.h>
31 #endif
32 
33 #if !AC_BUILT && (_MSC_VER || __MINGW32__ || __MINGW64__)
34 #define HAVE__ATOI64 1
35 #endif
36 
37 #include <stdint.h>
38 #define __STDC_FORMAT_MACROS
39 #if (!AC_BUILT || HAVE_INTTYPES_H) && ! defined(_MSC_VER)
40 #include <inttypes.h>
41 #else
42 #ifndef PRIx64
43 #define PRIx64    "llx"
44 #endif
45 #ifndef PRIu64
46 #define PRIu64    "llu"
47 #endif
48 #ifndef PRId64
49 #define PRId64    "lld"
50 #endif
51 #endif
52 
53 /******************************************/
54 /* here we try to 'find' a usable fseek64 */
55 /******************************************/
56 #if SIZEOF_LONG == 8
57 #define jtr_fseek64 fseek
58 
59 #elif HAVE_FSEEK64 /* Various */
60 // int fseek64 (FILE *stream, long long offset, int whence);
61 #define jtr_fseek64 fseek64
62 
63 #elif HAVE_FSEEKO64 /* Various */
64 // int fseeko64 (FILE *stream, long long offset, int whence);
65 #define jtr_fseek64 fseeko64
66 
67 #elif defined (HAVE__FSEEKI64) || defined (_MSC_VER) /* Windows */
68 // int _fseeki64(FILE *stream, __int64 offset, int origin);
69 #define jtr_fseek64 _fseeki64
70 
71 #elif SIZEOF_OFF_T == 8 && HAVE_FSEEKO /* Other LLP64 */
72 // int _fseeko(FILE *stream, __int64 offset, int origin);
73 #define jtr_fseek64 fseeko
74 
75 #elif HAVE_LSEEK64 /* Linux 32-bit or X32 */
76 // off64_t lseek64(int fd, off64_t offset, int whence);
77 //   !!!WARNING, we may need to flush, if file open for reading
78 //      for this to work right, especially in SEEK_END mode
79 #define jtr_fseek64(s,o,w) lseek64(fileno(s),o,w);
80 
81 #elif SIZEOF_OFF_T == 8 && HAVE_LSEEK /* POSIX.1 */
82 // off_t lseek(int fildes, off_t offset, int whence);
83 //   !!!WARNING, we may need to flush, if file open for reading
84 //      for this to work right, especially in SEEK_END mode
85 #define jtr_fseek64(s,o,w) lseek(fileno(s),o,w)
86 
87 #else
88 // at this point, we have NO easy workaround for a seek64 function.
89 // we can code things for specific environments, OR simply fall
90 // back to using fseek (and warn the user)
91 #if defined (__CYGWIN32__) && !defined (__CYGWIN64__)
92    extern  int fseeko64 (FILE* stream, int64_t offset, int whence);
93   #define jtr_fseek64 fseeko64
94 #elif defined (__CYGWIN64__)
95    extern  int fseeko (FILE* stream, int64_t offset, int whence);
96   #define jtr_fseek64 fseeko
97 #else
98   #if defined(__GNUC__) && defined (AC_BUILT)
99     #warning Using 32-bit fseek(). Files larger than 2GB will be handled unreliably
100   #endif
101   #define jtr_fseek64 fseek
102 #endif
103 
104 #endif /* fseek */
105 
106 
107 /******************************************/
108 /* here we try to 'find' a usable ftell64 */
109 /******************************************/
110 #if SIZEOF_LONG == 8
111 #define jtr_ftell64 ftell
112 
113 #elif HAVE_FTELL64 /* Linux and others */
114 // long long ftell64(FILE *stream)
115 #define jtr_ftell64 ftell64
116 
117 #elif HAVE_FTELLO64 /* Solaris */
118 // integer*8 function ftello64 (lunit) (Solaris)
119 #define jtr_ftell64 ftello64
120 
121 #elif defined (HAVE__FTELLI64) || defined (_MSC_VER) /* Windows */
122 // __int64 _ftelli64(FILE *stream);
123 #define jtr_ftell64 _ftelli64
124 
125 #elif SIZEOF_OFF_T == 8 && HAVE_FTELLO /* Other LLP64 */
126 // off_t ftello(FILE *stream);
127 #define jtr_ftell64 ftello
128 
129 #else
130 // at this point, we have NO easy workaround for a tell64 function.
131 // we can code things for specific environments, OR simply fall
132 // back to using ftell (and warn the user)
133 #if defined (__CYGWIN32__) && !defined (__CYGWIN64__)
134    extern  int64_t ftello64 (FILE* stream);
135   #define jtr_ftell64 ftello64
136 #elif defined (__CYGWIN64__)
137    extern  int64_t ftello (FILE* stream);
138   #define jtr_ftell64 ftello
139 #else
140   #if defined(__GNUC__) && defined (AC_BUILT)
141     #warning Using 32-bit ftell(). Files larger than 2GB will be handled unreliably
142   #endif
143   #define jtr_ftell64 ftell
144 #endif
145 
146 #endif /* ftell */
147 
148 /*************************************************/
149 /* here we figure out if we use fopen or fopen64 */
150 /*************************************************/
151 #if SIZEOF_LONG == 8
152   #define jtr_fopen fopen
153 #elif HAVE_FOPEN64
154   #define jtr_fopen fopen64
155 #elif HAVE__FOPEN64
156   #define jtr_fopen _fopen64
157 #else
158   #define jtr_fopen fopen
159 #endif
160 #if __CYGWIN32__ || _MSC_VER
161    extern  FILE *_fopen64 (const char *Fname, const char *type);
162 #endif
163 
164 #ifndef O_LARGEFILE
165 #define O_LARGEFILE 0
166 #endif
167 
168 /*
169  * Portable basename() function.  DO NOT USE basename().  Use this
170  * proper working equivalent.  The _r version is thread safe. In the
171  * _r version, pass in a buffer that is at least strlen(name)+1 bytes
172  * long, however, PATH_BUFFER_SIZE+1 can also be used.
173  *
174  *  here is what defined:
175  *    if name is null, or points to a null string (0 byte), then a '.' is returned.
176  *    if name is all / chars (or \ chars), then a single / (or \) is returned.
177  *    DOS drive letters are ignored.
178  *    / or \ chars are properly handled.
179  *    Trailing / (or \) are removed, IF there was real path data in there.
180  *
181  *  here are some examples:
182  *    jtr_basename("/user/lib")      == lib
183  *    jtr_basename("/user/")         == user
184  *    jtr_basename("/")              == /
185  *    jtr_basename("//")             == /
186  *    jtr_basename("///")            == /
187  *    jtr_basename("//user//lib//")  == lib
188  *    jtr_basename("c:\\txt.doc")    == txt.doc
189  *    jtr_basename("c:txt.doc")      == txt.doc
190  *    jtr_basename("c:b/c\\txt.doc/")== txt.doc
191  *    jtr_basename("c:\\txt.doc\\")  == txt.doc
192  *    jtr_basename("c:")             == .
193  *    jtr_basename("")               == .
194  *    jtr_basename(NULL)             == .
195  *    jtr_basename("\\user\\lib")    == lib
196  *    jtr_basename("\\user\\")       == user
197  *    jtr_basename("\\")             == \
198  *    jtr_basename("\\\\")           == \
199  *    jtr_basename("one")            == one
200  */
201 extern char *jtr_basename(const char *name);
202 extern char *jtr_basename_r(const char *name, char *buf);
203 #undef basename
204 #define basename(a) jtr_basename(a)
205 
206 /*
207  * Removes suffixes from src.
208  */
209 extern char *strip_suffixes(const char *src, const char *suffixes[], int count);
210 
211 #if !HAVE_MEMMEM
212 
213 #undef memmem
214 #define memmem	jtr_memmem
215 
216 /* Return the first occurrence of NEEDLE in HAYSTACK. */
217 extern void *memmem(const void *haystack, size_t haystack_len,
218                         const void *needle, size_t needle_len);
219 
220 #endif /* HAVE_!MEMMEM */
221 
222 // We configure search for unix sleep(seconds) function, MSVC and MinGW do not have this,
223 // so we replicate it with Win32 Sleep(ms) function.
224 #if (AC_BUILT && !HAVE_SLEEP) || (!AC_BUILT && (_MSC_VER || __MINGW32__ || __MINGW64__))
225 extern unsigned int sleep(unsigned int i);
226 #endif
227 
228 #if !AC_BUILT
229 #if _MSC_VER
230 #define strcasecmp _stricmp
231 #endif
232 #else
233 #if !HAVE_STRCASECMP
234 #if HAVE__STRICMP
235 #define strcasecmp _stricmp
236 #elif HAVE__STRCMPI
237 #define strcasecmp _strcmpi
238 #elif HAVE_STRICMP
239 #define strcasecmp stricmp
240 #elif HAVE_STRCMPI
241 #define strcasecmp strcmpi
242 #else
243 #define NEED_STRCASECMP_NATIVE 1
244 extern int strcasecmp(const char *dst, const char *src);
245 #endif
246 #endif
247 #endif
248 
249 #if !AC_BUILT
250 #if _MSC_VER
251 #define strncasecmp _strnicmp
252 #endif
253 #else
254 #if !HAVE_STRNCASECMP
255 #if HAVE__STRNICMP
256 #define strncasecmp _strnicmp
257 #elif HAVE__STRNCMPI
258 #define strncasecmp _strncmpi
259 #elif HAVE_STRNICMP
260 #define strncasecmp strnicmp
261 #elif HAVE_STRNCMPI
262 #define strncasecmp strncmpi
263 #else
264 #define NEED_STRNCASECMP_NATIVE 1
265 extern int strncasecmp(const char *dst, const char *src, size_t count);
266 #endif
267 #endif
268 #endif
269 
270 #if (AC_BUILT && HAVE__STRUPR && HAVE_STRUPR) || (!AC_BUILT && _MSC_VER)
271 #define strupr _strupr
272 #endif
273 
274 #if (AC_BUILT && HAVE__STRLWR && HAVE_STRLWR) || (!AC_BUILT && _MSC_VER)
275 #define strlwr _strlwr
276 #endif
277 
278 #if (AC_BUILT && !HAVE_STRLWR) || (!AC_BUILT && !_MSC_VER)
279 extern char *strlwr(char *s);
280 #endif
281 #if (AC_BUILT && !HAVE_STRUPR) || (!AC_BUILT && !_MSC_VER)
282 extern char *strupr(char *s);
283 #endif
284 
285 #if !HAVE_ATOLL
286 #if HAVE__ATOI64
287 #define atoll _atoi64
288 #else
289 #define NEED_ATOLL_NATIVE 1
290 #undef atoll
291 #define atoll jtr_atoll
292 extern long long jtr_atoll(const char *);
293 #endif
294 #endif
295 
296 void memcpylwr(char *, const char *, size_t);
297 
298 #if (__MINGW32__ || __MINGW64__) && __STRICT_ANSI__
299 // since we added -std=c99 for Mingw builds (to handle printf/scanf %xxx specifiers better),
300 // we had to make some 'changes'. Mostly, some of the string types are undeclared (but will
301 // link properly). Also, sys/file, sys/stat, fcntl.h will not include properly, due to
302 // off64_t missing.
303 extern char *strdup(const char *);
304 extern char *strlwr(char *);
305 extern char *strupr(char *);
306 //extern int _strnicmp(const char*, const char *, int);
307 extern int _strncmp(const char*, const char *);
308 //extern int _stricmp(const char*, const char *);
309 extern FILE *fopen64(const char *, const char *);
310 extern FILE *fdopen(int, const char *);
311 //extern int ftruncate(int, int);
312 extern long long ftello64(FILE *);
313 extern int fseeko64(FILE *, long long, int);
314 extern int fileno(FILE *);
315 //extern int _exit(int);
316 #define off64_t long long
317 #undef __STRICT_ANSI__
318 #include <sys/file.h>
319 #include <sys/stat.h>
320 #include <fcntl.h>
321 #define __STRICT_ANSI__ 1
322 #endif
323 
324 #ifdef _MSC_VER
325 #undef inline
326 #define inline _inline
327 #define strupr _strupr
328 #define strlwr _strlwr
329 #define open _open
330 #define fdopen _fdopen
331 #pragma warning(disable: 4244) // possible loss of data
332 #pragma warning(disable: 4334) // 32 bit shift implictly converted to 64 bits
333 #pragma warning(disable: 4133) // function imcompatible types
334 #pragma warning(disable: 4146) // unary minus applied to unsigned
335 #pragma warning(disable: 4715) // not all control paths return a value
336 #endif
337 
338 #if (AC_BUILT && !HAVE_SNPRINTF && HAVE_SPRINTF_S) || (!AC_BUILT && _MSC_VER)
339 #undef  snprintf
340 #define snprintf sprintf_s
341 #endif
342 
343 #if _MSC_VER
344 // I tried adding these to autoconf, BUT I ended up with systems where
345 // the function can be linked to, but it was not in headers. SO, I simply
346 // blindly use these for VC.  For VC, they are used to work around many
347 // red-herring compiler warnings
348 #undef snprintf
349 #if _MSC_VER < 1900
350 // note, in VC 2015, snprintf was fixed to be POSIX compliant. The legacy _snprintf function
351 // starting at at VC 2015 is no longer the same as snprintf (as it was prior).  The _snprintf
352 // was kept at the legacy problematic manner, while snprintf now 'works' properly.
353 // _MSC_VER == 1900 is the key for VC 2015
354 #define snprintf(str, size, ...) vc_fixed_snprintf((str), (size), __VA_ARGS__)
355 extern int vc_fixed_snprintf(char *Dest, size_t max_cnt, const char *Fmt, ...);
356 #endif
357 #undef alloca
358 #define alloca _alloca
359 #undef unlink
360 #define unlink _unlink
361 #undef fileno
362 #define fileno _fileno
363 #pragma warning (disable : 4018 297 )
364 #endif
365 
366 // NOTE, this still will fail on REALLY old systems, where you can only
367 // do a setenv by getting the pointer in getenv and mofifying the results.
368 // old Borland C (DOS days), was this way, as was a few others. This should
369 // hopefully NOT be an issue any more, and systems will either have setenv
370 // putenv or both. This code handles builds of ONLY putenv (VC, Mingw)
371 #if (AC_BUILT && !HAVE_SETENV && HAVE_PUTENV) || \
372     (!AC_BUILT && (_MSC_VER || __MINGW32__ || __MINGW64__))
373 extern int setenv(const char *name, const char *val, int overwrite);
374 #endif
375 
376 #if (__MINGW32__ && !__MINGW64__) || _MSC_VER
377 // Later versions of MSVC can handle %lld but some older
378 // ones can only handle %I64d.  Easiest to simply use
379 // %I64d then all versions of MSVC will handle it just fine
380 #define LLu "%I64u"
381 #define LLd "%I64d"
382 #define LLx "%I64x"
383 #define Zu  "%u"
384 #define Zd  "%d"
385 #else
386 #define LLu "%llu"
387 #define LLd "%lld"
388 #define LLx "%llx"
389 #define Zu  "%zu"
390 #define Zd  "%zd"
391 #endif
392 
393 #if (AC_BUILT && !HAVE_STRREV) ||(!AC_BUILT && !_MSC_VER)
394 char *strrev(char *str);
395 #endif
396 
397 // Start handing these (some we may not be able to, or are too hard to 'care', and we should
398 // simply #ifdef around the logic where the functions are used, or find some other way.
399 //HAVE_ATEXIT
400 //HAVE_ENDPWENT  (no mingw)
401 //HAVE_FLOOR
402 //HAVE_FTRUNCATE
403 //HAVE_GETHOSTBYNAME  (no mingw)
404 //HAVE_GETTIMEOFDAY
405 //HAVE_INET_NTOA   (no mingw)
406 //HAVE_ISASCII     (no mingw)
407 //HAVE_MKDIR
408 //HAVE_RMDIR
409 //HAVE_STRRCHR
410 //HAVE_STRCSPN
411 //HAVE_STRSPN
412 //HAVE_STRTOL
413 //HAVE_STRTOUL
414 
415 /*
416  * Like strlen but will not scan past max, so will return at most max.
417  */
418 #if AC_BUILT && !HAVE_STRNLEN
419 #undef strnlen
420 #define strnlen jtr_strnlen
421 extern size_t strnlen(const char *s, size_t max);
422 #endif
423 
424 #if AC_BUILT && !HAVE_STRCASESTR || !AC_BUILT && defined(__MINGW__)
425 char *strcasestr(const char *haystack, const char *needle);
426 #endif
427 
428 /*
429  * Standard PKCS padding check. On success, returns net length.
430  * On failure, returns -1.
431  */
432 extern int check_pkcs_pad(const unsigned char* data, size_t len, int blocksize);
433 
434 /*
435  * Parse string for boolean. Case insensitive:
436  * y/yes/true/1: return 1
437  * n/no/false/0: return 0
438  * None of the above: return -1
439  */
440 extern int parse_bool(char *string);
441 
442 #endif /* _JTR_JUMBO_H */
443