]> icculus.org git repositories - icculus/xz.git/blob - src/common/sysdefs.h
5b8b9ceca9dab815c2a7ff3cf364ebb1b3bb4751
[icculus/xz.git] / src / common / sysdefs.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       sysdefs.h
4 /// \brief      Common includes, definitions, system-specific things etc.
5 ///
6 /// This file is used also by the lzma command line tool, that's why this
7 /// file is separate from common.h.
8 //
9 //  Copyright (C) 2007 Lasse Collin
10 //
11 //  This library is free software; you can redistribute it and/or
12 //  modify it under the terms of the GNU Lesser General Public
13 //  License as published by the Free Software Foundation; either
14 //  version 2.1 of the License, or (at your option) any later version.
15 //
16 //  This library is distributed in the hope that it will be useful,
17 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 //  Lesser General Public License for more details.
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #ifndef LZMA_SYSDEFS_H
24 #define LZMA_SYSDEFS_H
25
26 //////////////
27 // Includes //
28 //////////////
29
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 // size_t and NULL
35 #include <stddef.h>
36
37 #ifdef HAVE_INTTYPES_H
38 #       include <inttypes.h>
39 #endif
40
41 // C99 says that inttypes.h always includes stdint.h, but some systems
42 // don't do that, and require including stdint.h separately.
43 #ifdef HAVE_STDINT_H
44 #       include <stdint.h>
45 #endif
46
47 // Some pre-C99 systems have SIZE_MAX in limits.h instead of stdint.h. The
48 // limits are also used to figure out some macros missing from pre-C99 systems.
49 #ifdef HAVE_LIMITS_H
50 #       include <limits.h>
51 #endif
52
53 // Be more compatible with systems that have non-conforming inttypes.h.
54 // We assume that int is 32-bit and that long is either 32-bit or 64-bit.
55 // Full Autoconf test could be more correct, but this should work well enough.
56 // Note that this duplicates some code from lzma.h, but this is better since
57 // we can work without inttypes.h thanks to Autoconf tests.
58 #ifndef UINT32_C
59 #       if UINT_MAX != 4294967295U
60 #               error UINT32_C is not defined and unsiged int is not 32-bit.
61 #       endif
62 #       define UINT32_C(n) n ## U
63 #endif
64 #ifndef UINT32_MAX
65 #       define UINT32_MAX UINT32_C(4294967295)
66 #endif
67 #ifndef PRIu32
68 #       define PRIu32 "u"
69 #endif
70 #ifndef PRIX32
71 #       define PRIX32 "X"
72 #endif
73
74 #if ULONG_MAX == 4294967295UL
75 #       ifndef UINT64_C
76 #               define UINT64_C(n) n ## ULL
77 #       endif
78 #       ifndef PRIu64
79 #               define PRIu64 "llu"
80 #       endif
81 #       ifndef PRIX64
82 #               define PRIX64 "llX"
83 #       endif
84 #else
85 #       ifndef UINT64_C
86 #               define UINT64_C(n) n ## UL
87 #       endif
88 #       ifndef PRIu64
89 #               define PRIu64 "lu"
90 #       endif
91 #       ifndef PRIX64
92 #               define PRIX64 "lX"
93 #       endif
94 #endif
95 #ifndef UINT64_MAX
96 #       define UINT64_MAX UINT64_C(18446744073709551615)
97 #endif
98
99 // The code currently assumes that size_t is either 32-bit or 64-bit.
100 #ifndef SIZE_MAX
101 #       if SIZEOF_SIZE_T == 4
102 #               define SIZE_MAX UINT32_MAX
103 #       elif SIZEOF_SIZE_T == 8
104 #               define SIZE_MAX UINT64_MAX
105 #       else
106 #               error sizeof(size_t) is not 32-bit or 64-bit
107 #       endif
108 #endif
109 #if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
110 #       error sizeof(size_t) is not 32-bit or 64-bit
111 #endif
112
113 #include <stdlib.h>
114 #include <assert.h>
115
116 // Pre-C99 systems lack stdbool.h. All the code in LZMA Utils must be written
117 // so that it works with fake bool type, for example:
118 //
119 //    bool foo = (flags & 0x100) != 0;
120 //    bool bar = !!(flags & 0x100);
121 //
122 // This works with the real C99 bool but breaks with fake bool:
123 //
124 //    bool baz = (flags & 0x100);
125 //
126 #ifdef HAVE_STDBOOL_H
127 #       include <stdbool.h>
128 #else
129 #       if ! HAVE__BOOL
130 typedef unsigned char _Bool;
131 #       endif
132 #       define bool _Bool
133 #       define false 0
134 #       define true 1
135 #       define __bool_true_false_are_defined 1
136 #endif
137
138 // string.h should be enough but let's include strings.h and memory.h too if
139 // they exists, since that shouldn't do any harm, but may improve portability.
140 #ifdef HAVE_STRING_H
141 #       include <string.h>
142 #endif
143
144 #ifdef HAVE_STRINGS_H
145 #       include <strings.h>
146 #endif
147
148 #ifdef HAVE_MEMORY_H
149 #       include <memory.h>
150 #endif
151
152
153 ////////////
154 // Macros //
155 ////////////
156
157 #if defined(_WIN32) || defined(__MSDOS__) || defined(__OS2__)
158 #       define DOSLIKE 1
159 #endif
160
161 #undef memzero
162 #define memzero(s, n) memset(s, 0, n)
163
164 #ifndef MIN
165 #       define MIN(x, y) ((x) < (y) ? (x) : (y))
166 #endif
167
168 #ifndef MAX
169 #       define MAX(x, y) ((x) > (y) ? (x) : (y))
170 #endif
171
172 #ifndef ARRAY_SIZE
173 #       define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
174 #endif
175
176 #endif