1 /*
2  *  This file is part of nzbget. See <http://nzbget.net>.
3  *
4  *  Copyright (C) 2007-2019 Andrey Prygunkov <hugbug@users.sourceforge.net>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #ifndef NZBGET_H
22 #define NZBGET_H
23 
24 /***************** DEFINES FOR WINDOWS *****************/
25 #ifdef WIN32
26 
27 /* Define to 1 to not use curses */
28 //#define DISABLE_CURSES
29 
30 /* Define to 1 to disable smart par-verification and restoration */
31 //#define DISABLE_PARCHECK
32 
33 /* Define to 1 to disable TLS/SSL-support. */
34 //#define DISABLE_TLS
35 
36 #ifndef DISABLE_TLS
37 /* Define to 1 to use OpenSSL library for TLS/SSL-support */
38 #define HAVE_OPENSSL
39 /* Define to 1 to use GnuTLS library for TLS/SSL-support */
40 //#define HAVE_LIBGNUTLS
41 #endif
42 
43 /* Define to 1 if OpenSSL supports function "X509_check_host". */
44 #define HAVE_X509_CHECK_HOST 1
45 
46 /* Define to the name of macro which returns the name of function being
47 compiled */
48 #define FUNCTION_MACRO_NAME __FUNCTION__
49 
50 /* Define to 1 if ctime_r takes 2 arguments */
51 #undef HAVE_CTIME_R_2
52 
53 /* Define to 1 if ctime_r takes 3 arguments */
54 #define HAVE_CTIME_R_3
55 
56 /* Define to 1 if getopt_long is supported */
57 #undef HAVE_GETOPT_LONG
58 
59 /* Define to 1 if variadic macros are supported */
60 #define HAVE_VARIADIC_MACROS
61 
62 /* Define to 1 if function GetAddrInfo is supported */
63 #define HAVE_GETADDRINFO
64 
65 /* Determine what socket length (socklen_t) data type is */
66 #define SOCKLEN_T socklen_t
67 
68 /* Define to 1 if you have the <regex.h> header file. */
69 #ifndef DISABLE_REGEX
70 #define HAVE_REGEX_H 1
71 // Static linking to regex library
72 #define REGEX_STATIC
73 #endif
74 
75 #ifndef DISABLE_GZIP
76 // Static linking to zlib library
77 #define ZLIB_WINAPI
78 #endif
79 
80 /* Suppress warnings */
81 #define _CRT_SECURE_NO_DEPRECATE
82 
83 /* Suppress warnings */
84 #define _CRT_NONSTDC_NO_WARNINGS
85 
86 #ifndef _WIN64
87 #define _USE_32BIT_TIME_T
88 #endif
89 
90 #if _WIN32_WINNT < 0x0501
91 #undef _WIN32_WINNT
92 #define _WIN32_WINNT 0x0501
93 #endif
94 
95 #ifdef _WIN64
96 #define __amd64__
97 #else
98 #define __i686__
99 #endif
100 
101 #ifdef _DEBUG
102 // detection of memory leaks
103 #define _CRTDBG_MAP_ALLOC
104 #endif
105 
106 #pragma warning(disable:4800) // 'type' : forcing value to bool 'true' or 'false' (performance warning)
107 #pragma warning(disable:4267) // 'var' : conversion from 'size_t' to 'type', possible loss of data
108 
109 #endif
110 
111 
112 /***************** GLOBAL INCLUDES *****************/
113 
114 #ifdef WIN32
115 
116 // WINDOWS INCLUDES
117 
118 // Using "WIN32_LEAN_AND_MEAN" to disable including of many unneeded headers
119 #define WIN32_LEAN_AND_MEAN
120 #define NOMINMAX
121 
122 #include <windows.h>
123 #include <winsock2.h>
124 #include <ws2tcpip.h>
125 
126 #include <winsvc.h>
127 #include <direct.h>
128 #include <shlobj.h>
129 #include <dbghelp.h>
130 #include <mmsystem.h>
131 #include <io.h>
132 #include <process.h>
133 #include <WinIoCtl.h>
134 #include <wincon.h>
135 #include <shellapi.h>
136 #include <winreg.h>
137 
138 #include <comutil.h>
139 #import <msxml.tlb> named_guids
140 using namespace MSXML;
141 
142 #if _MSC_VER >= 1600
143 #include <stdint.h>
144 #define HAVE_STDINT_H
145 #endif
146 
147 #ifdef _DEBUG
148 #include <crtdbg.h>
149 #endif
150 
151 #else
152 
153 // POSIX INCLUDES
154 
155 #include "config.h"
156 
157 #include <unistd.h>
158 #include <pwd.h>
159 #include <grp.h>
160 #include <signal.h>
161 #include <netdb.h>
162 #include <unistd.h>
163 #include <pthread.h>
164 #include <unistd.h>
165 #include <getopt.h>
166 #include <sys/socket.h>
167 #include <sys/time.h>
168 #include <sys/resource.h>
169 #include <sys/statvfs.h>
170 #include <sys/wait.h>
171 #include <sys/un.h>
172 #include <sys/file.h>
173 #include <arpa/inet.h>
174 #include <netinet/in.h>
175 #include <stdint.h>
176 #include <pwd.h>
177 #include <dirent.h>
178 
179 #ifndef DISABLE_LIBXML2
180 #include <libxml/parser.h>
181 #include <libxml/xmlreader.h>
182 #include <libxml/xmlerror.h>
183 #include <libxml/entities.h>
184 #endif
185 
186 #ifdef HAVE_SYS_PRCTL_H
187 #include <sys/prctl.h>
188 #endif
189 
190 #ifdef HAVE_ENDIAN_H
191 #include <endian.h>
192 #endif
193 
194 #ifdef HAVE_BACKTRACE
195 #include <execinfo.h>
196 #endif
197 
198 #endif /* POSIX INCLUDES */
199 
200 // COMMON INCLUDES
201 
202 #include <sys/types.h>
203 #include <sys/stat.h>
204 #include <errno.h>
205 #include <stdio.h>
206 #include <fcntl.h>
207 #include <stdlib.h>
208 #include <string.h>
209 #include <stdarg.h>
210 #include <time.h>
211 #include <ctype.h>
212 #include <inttypes.h>
213 
214 #include <string>
215 #include <vector>
216 #include <deque>
217 #include <list>
218 #include <set>
219 #include <map>
220 #include <unordered_map>
221 #include <iterator>
222 #include <algorithm>
223 #include <fstream>
224 #include <memory>
225 #include <functional>
226 #include <thread>
227 #include <mutex>
228 #include <condition_variable>
229 #include <chrono>
230 
231 // NOTE: do not include <iostream> in "nzbget.h". <iostream> contains objects requiring
232 // intialization, causing every unit in nzbget to have initialization routine. This in particular
233 // is causing fatal problems in SIMD units which must not have static initialization because
234 // they contain code with runtime CPU dispatching.
235 //#include <iostream>
236 
237 #ifdef HAVE_LIBGNUTLS
238 #ifdef WIN32
239 #include <BaseTsd.h>
240 typedef SSIZE_T ssize_t;
241 typedef int pid_t;
242 #endif
243 #include <gnutls/gnutls.h>
244 #include <gnutls/x509.h>
245 #if GNUTLS_VERSION_NUMBER <= 0x020b00
246 #define NEED_GCRYPT_LOCKING
247 #endif
248 #ifdef NEED_GCRYPT_LOCKING
249 #include <gcrypt.h>
250 #endif /* NEED_GCRYPT_LOCKING */
251 #include <nettle/sha.h>
252 #include <nettle/pbkdf2.h>
253 #include <nettle/aes.h>
254 #endif /* HAVE_LIBGNUTLS */
255 
256 #ifdef HAVE_OPENSSL
257 #include <openssl/ssl.h>
258 #include <openssl/err.h>
259 #include <openssl/rsa.h>
260 #include <openssl/sha.h>
261 #include <openssl/pem.h>
262 #include <openssl/x509v3.h>
263 #include <openssl/comp.h>
264 #endif /* HAVE_OPENSSL */
265 
266 #ifdef HAVE_REGEX_H
267 #include <regex.h>
268 #endif
269 
270 #ifndef DISABLE_GZIP
271 #include <zlib.h>
272 #endif
273 
274 #ifndef DISABLE_PARCHECK
275 #include <assert.h>
276 #include <iomanip>
277 #include <cassert>
278 #ifdef HAVE_MEMORY_H
279 # include <memory.h>
280 #endif
281 #endif /* NOT DISABLE_PARCHECK */
282 
283 
284 /***************** GLOBAL FUNCTION AND CONST OVERRIDES *****************/
285 
286 #ifdef WIN32
287 
288 // WINDOWS
289 
290 #define snprintf _snprintf
291 #ifndef strdup
292 #define strdup _strdup
293 #endif
294 #define fdopen _fdopen
295 #define ctime_r(timep, buf, bufsize) ctime_s(buf, bufsize, timep)
296 #define gmtime_r(time, tm) gmtime_s(tm, time)
297 #define strtok_r(str, delim, saveptr) strtok_s(str, delim, saveptr)
298 #define strerror_r(errnum, buffer, size) strerror_s(buffer, size, errnum)
299 #define mkdir(dir, flags) _mkdir(dir)
300 #define rmdir _rmdir
301 #define strcasecmp(a, b) _stricmp(a, b)
302 #define strncasecmp(a, b, c) _strnicmp(a, b, c)
303 #define __S_ISTYPE(mode, mask) (((mode) & _S_IFMT) == (mask))
304 #define S_ISDIR(mode) __S_ISTYPE((mode), _S_IFDIR)
305 #define S_ISREG(mode) __S_ISTYPE((mode), _S_IFREG)
306 #define S_DIRMODE nullptr
307 #define socklen_t int
308 #define SHUT_WR 0x01
309 #define SHUT_RDWR 0x02
310 #define PATH_SEPARATOR '\\'
311 #define ALT_PATH_SEPARATOR '/'
312 #define LINE_ENDING "\r\n"
313 #define atoll _atoi64
314 #define fseek _fseeki64
315 #define ftell _ftelli64
316 // va_copy is available in vc2013 and onwards
317 #if _MSC_VER < 1800
318 #define va_copy(d,s) ((d) = (s))
319 #endif
320 #ifndef FSCTL_SET_SPARSE
321 #define FSCTL_SET_SPARSE 590020
322 #endif
323 #define FOPEN_RB "rbN"
324 #define FOPEN_RBP "rb+N"
325 #define FOPEN_WB "wbN"
326 #define FOPEN_AB "abN"
327 
328 #define __SSE2__
329 #define __SSSE3__
330 #define __PCLMUL__
331 
332 #ifdef DEBUG
333 // redefine "exit" to avoid printing memory leaks report when terminated because of wrong command line switches
334 #define exit(code) ExitProcess(code)
335 #endif
336 
337 #ifdef HAVE_OPENSSL
338 FILE _iob[] = {*stdin, *stdout, *stderr};
__iob_func(void)339 extern "C" FILE * __cdecl __iob_func(void) { return _iob; }
340 // For static linking of OpenSSL libraries:
341 #pragma comment (lib, "legacy_stdio_definitions.lib")
342 #endif /* HAVE_OPENSSL */
343 
344 #else
345 
346 // POSIX
347 
348 #define closesocket(sock) close(sock)
349 #define SOCKET int
350 #define INVALID_SOCKET (-1)
351 #define PATH_SEPARATOR '/'
352 #define ALT_PATH_SEPARATOR '\\'
353 #define MAX_PATH 1024
354 #define S_DIRMODE (S_IRWXU | S_IRWXG | S_IRWXO)
355 #define LINE_ENDING "\n"
356 #define FOPEN_RB "rb"
357 #define FOPEN_RBP "rb+"
358 #define FOPEN_WB "wb"
359 #define FOPEN_AB "ab"
360 #define CHILD_WATCHDOG 1
361 
362 #endif /* POSIX */
363 
364 // COMMON DEFINES FOR ALL PLATFORMS
365 #ifndef SHUT_RDWR
366 #define SHUT_RDWR 2
367 #endif
368 
369 #ifdef HAVE_STDINT_H
370 typedef uint8_t uint8;
371 typedef int16_t int16;
372 typedef uint16_t uint16;
373 typedef uint32_t int32;
374 typedef uint32_t uint32;
375 typedef int64_t int64;
376 typedef uint64_t uint64;
377 #else
378 typedef unsigned char uint8;
379 typedef signed short int16;
380 typedef unsigned short uint16;
381 typedef signed int int32;
382 typedef unsigned int uint32;
383 typedef signed long long int64;
384 typedef unsigned long long uint64;
385 #endif
386 
387 #ifndef PRId64
388 #define PRId64 "lld"
389 #endif
390 #ifndef PRIi64
391 #define PRIi64 "lli"
392 #endif
393 #ifndef PRIu64
394 #define PRIu64 "llu"
395 #endif
396 
397 typedef unsigned char uchar;
398 
399 // Assume little endian if byte order is not defined
400 #ifndef __BYTE_ORDER
401 #define __LITTLE_ENDIAN 1234
402 #define __BIG_ENDIAN    4321
403 #define __BYTE_ORDER __LITTLE_ENDIAN
404 #endif
405 
406 #ifdef __GNUC__
407 #define PRINTF_SYNTAX(strindex) __attribute__ ((format (printf, strindex, strindex+1)))
408 #define SCANF_SYNTAX(strindex) __attribute__ ((format (scanf, strindex, strindex+1)))
409 #else
410 #define PRINTF_SYNTAX(strindex)
411 #define SCANF_SYNTAX(strindex)
412 #endif
413 
414 // providing "std::make_unique" for GCC 4.8.x (only 4.8.x)
415 #if __GNUC__ && __cplusplus < 201402L && __cpp_generic_lambdas < 201304
416 namespace std {
417 template<class T> struct _Unique_if { typedef unique_ptr<T> _Single_object; };
418 template<class T> struct _Unique_if<T[]> { typedef unique_ptr<T[]> _Unknown_bound; };
419 template<class T, class... Args> typename _Unique_if<T>::_Single_object make_unique(Args&&... args) {
420 	return unique_ptr<T>(new T(std::forward<Args>(args)...));
421 }
422 template<class T> typename _Unique_if<T>::_Unknown_bound make_unique(size_t n) {
423 	typedef typename remove_extent<T>::type U;
424 	return unique_ptr<T>(new U[n]());
425 }
426 }
427 #endif
428 
429 #endif /* NZBGET_H */
430