/src/lzo-2.10/src/lzo_init.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* lzo_init.c -- initialization of the LZO library |
2 | | |
3 | | This file is part of the LZO real-time data compression library. |
4 | | |
5 | | Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer |
6 | | All Rights Reserved. |
7 | | |
8 | | The LZO library is free software; you can redistribute it and/or |
9 | | modify it under the terms of the GNU General Public License as |
10 | | published by the Free Software Foundation; either version 2 of |
11 | | the License, or (at your option) any later version. |
12 | | |
13 | | The LZO library is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with the LZO library; see the file COPYING. |
20 | | If not, write to the Free Software Foundation, Inc., |
21 | | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
22 | | |
23 | | Markus F.X.J. Oberhumer |
24 | | <markus@oberhumer.com> |
25 | | http://www.oberhumer.com/opensource/lzo/ |
26 | | */ |
27 | | |
28 | | |
29 | | #include "lzo_conf.h" |
30 | | |
31 | | |
32 | | /*********************************************************************** |
33 | | // Runtime check of the assumptions about the size of builtin types, |
34 | | // memory model, byte order and other low-level constructs. |
35 | | // |
36 | | // We are really paranoid here - LZO should either fail |
37 | | // at startup or not at all. |
38 | | // |
39 | | // Because of inlining much of these functions evaluates to nothing. |
40 | | // |
41 | | // And while many of the tests seem highly obvious and redundant they are |
42 | | // here to catch compiler/optimizer bugs. Yes, these do exist. |
43 | | ************************************************************************/ |
44 | | |
45 | | #if !defined(__LZO_IN_MINILZO) |
46 | | |
47 | | #define LZO_WANT_ACC_CHK_CH 1 |
48 | | #undef LZOCHK_ASSERT |
49 | | #include "lzo_supp.h" |
50 | | |
51 | | LZOCHK_ASSERT((LZO_UINT32_C(1) << (int)(8*sizeof(LZO_UINT32_C(1))-1)) > 0) |
52 | | LZOCHK_ASSERT_IS_SIGNED_T(lzo_int) |
53 | | LZOCHK_ASSERT_IS_UNSIGNED_T(lzo_uint) |
54 | | #if !(__LZO_UINTPTR_T_IS_POINTER) |
55 | | LZOCHK_ASSERT_IS_UNSIGNED_T(lzo_uintptr_t) |
56 | | #endif |
57 | | LZOCHK_ASSERT(sizeof(lzo_uintptr_t) >= sizeof(lzo_voidp)) |
58 | | LZOCHK_ASSERT_IS_UNSIGNED_T(lzo_xint) |
59 | | |
60 | | #endif |
61 | | #undef LZOCHK_ASSERT |
62 | | |
63 | | |
64 | | /*********************************************************************** |
65 | | // |
66 | | ************************************************************************/ |
67 | | |
68 | | union lzo_config_check_union { |
69 | | lzo_uint a[2]; |
70 | | unsigned char b[2*LZO_MAX(8,sizeof(lzo_uint))]; |
71 | | #if defined(lzo_uint64_t) |
72 | | lzo_uint64_t c[2]; |
73 | | #endif |
74 | | }; |
75 | | |
76 | | |
77 | | #if 0 |
78 | | #define u2p(ptr,off) ((lzo_voidp) (((lzo_bytep)(lzo_voidp)(ptr)) + (off))) |
79 | | #else |
80 | | static __lzo_noinline lzo_voidp u2p(lzo_voidp ptr, lzo_uint off) |
81 | 15 | { |
82 | 15 | return (lzo_voidp) ((lzo_bytep) ptr + off); |
83 | 15 | } |
84 | | #endif |
85 | | |
86 | | |
87 | | LZO_PUBLIC(int) |
88 | | _lzo_config_check(void) |
89 | 3 | { |
90 | | #if (LZO_CC_CLANG && (LZO_CC_CLANG >= 0x030100ul && LZO_CC_CLANG < 0x030300ul)) |
91 | | # if 0 |
92 | | /* work around a clang 3.1 and clang 3.2 compiler bug; clang 3.3 and 3.4 work */ |
93 | | volatile |
94 | | # endif |
95 | | #endif |
96 | 3 | union lzo_config_check_union u; |
97 | 3 | lzo_voidp p; |
98 | 3 | unsigned r = 1; |
99 | | |
100 | 3 | u.a[0] = u.a[1] = 0; |
101 | 3 | p = u2p(&u, 0); |
102 | 3 | r &= ((* (lzo_bytep) p) == 0); |
103 | 3 | #if !(LZO_CFG_NO_CONFIG_CHECK) |
104 | | #if (LZO_ABI_BIG_ENDIAN) |
105 | | u.a[0] = u.a[1] = 0; u.b[sizeof(lzo_uint) - 1] = 128; |
106 | | p = u2p(&u, 0); |
107 | | r &= ((* (lzo_uintp) p) == 128); |
108 | | #endif |
109 | 3 | #if (LZO_ABI_LITTLE_ENDIAN) |
110 | 3 | u.a[0] = u.a[1] = 0; u.b[0] = 128; |
111 | 3 | p = u2p(&u, 0); |
112 | 3 | r &= ((* (lzo_uintp) p) == 128); |
113 | 3 | #endif |
114 | 3 | u.a[0] = u.a[1] = 0; |
115 | 3 | u.b[0] = 1; u.b[3] = 2; |
116 | 3 | p = u2p(&u, 1); |
117 | 3 | r &= UA_GET_NE16(p) == 0; |
118 | 3 | r &= UA_GET_LE16(p) == 0; |
119 | 3 | u.b[1] = 128; |
120 | 3 | r &= UA_GET_LE16(p) == 128; |
121 | 3 | u.b[2] = 129; |
122 | 3 | r &= UA_GET_LE16(p) == LZO_UINT16_C(0x8180); |
123 | | #if (LZO_ABI_BIG_ENDIAN) |
124 | | r &= UA_GET_NE16(p) == LZO_UINT16_C(0x8081); |
125 | | #endif |
126 | 3 | #if (LZO_ABI_LITTLE_ENDIAN) |
127 | 3 | r &= UA_GET_NE16(p) == LZO_UINT16_C(0x8180); |
128 | 3 | #endif |
129 | 3 | u.a[0] = u.a[1] = 0; |
130 | 3 | u.b[0] = 3; u.b[5] = 4; |
131 | 3 | p = u2p(&u, 1); |
132 | 3 | r &= UA_GET_NE32(p) == 0; |
133 | 3 | r &= UA_GET_LE32(p) == 0; |
134 | 3 | u.b[1] = 128; |
135 | 3 | r &= UA_GET_LE32(p) == 128; |
136 | 3 | u.b[2] = 129; u.b[3] = 130; u.b[4] = 131; |
137 | 3 | r &= UA_GET_LE32(p) == LZO_UINT32_C(0x83828180); |
138 | | #if (LZO_ABI_BIG_ENDIAN) |
139 | | r &= UA_GET_NE32(p) == LZO_UINT32_C(0x80818283); |
140 | | #endif |
141 | 3 | #if (LZO_ABI_LITTLE_ENDIAN) |
142 | 3 | r &= UA_GET_NE32(p) == LZO_UINT32_C(0x83828180); |
143 | 3 | #endif |
144 | 3 | #if defined(UA_GET_NE64) |
145 | 3 | u.c[0] = u.c[1] = 0; |
146 | 3 | u.b[0] = 5; u.b[9] = 6; |
147 | 3 | p = u2p(&u, 1); |
148 | 3 | u.c[0] = u.c[1] = 0; |
149 | 3 | r &= UA_GET_NE64(p) == 0; |
150 | 3 | #if defined(UA_GET_LE64) |
151 | 3 | r &= UA_GET_LE64(p) == 0; |
152 | 3 | u.b[1] = 128; |
153 | 3 | r &= UA_GET_LE64(p) == 128; |
154 | 3 | #endif |
155 | 3 | #endif |
156 | 3 | #if defined(lzo_bitops_ctlz32) |
157 | 3 | { unsigned i = 0; lzo_uint32_t v; |
158 | 99 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
159 | 96 | r &= lzo_bitops_ctlz32(v) == 31 - i; |
160 | 96 | r &= lzo_bitops_ctlz32_func(v) == 31 - i; |
161 | 96 | }} |
162 | 3 | #endif |
163 | 3 | #if defined(lzo_bitops_ctlz64) |
164 | 3 | { unsigned i = 0; lzo_uint64_t v; |
165 | 195 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
166 | 192 | r &= lzo_bitops_ctlz64(v) == 63 - i; |
167 | 192 | r &= lzo_bitops_ctlz64_func(v) == 63 - i; |
168 | 192 | }} |
169 | 3 | #endif |
170 | 3 | #if defined(lzo_bitops_cttz32) |
171 | 3 | { unsigned i = 0; lzo_uint32_t v; |
172 | 99 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
173 | 96 | r &= lzo_bitops_cttz32(v) == i; |
174 | 96 | r &= lzo_bitops_cttz32_func(v) == i; |
175 | 96 | }} |
176 | 3 | #endif |
177 | 3 | #if defined(lzo_bitops_cttz64) |
178 | 3 | { unsigned i = 0; lzo_uint64_t v; |
179 | 195 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
180 | 192 | r &= lzo_bitops_cttz64(v) == i; |
181 | 192 | r &= lzo_bitops_cttz64_func(v) == i; |
182 | 192 | }} |
183 | 3 | #endif |
184 | 3 | #endif |
185 | 3 | LZO_UNUSED_FUNC(lzo_bitops_unused_funcs); |
186 | | |
187 | 3 | return r == 1 ? LZO_E_OK : LZO_E_ERROR; |
188 | 3 | } |
189 | | |
190 | | |
191 | | /*********************************************************************** |
192 | | // |
193 | | ************************************************************************/ |
194 | | |
195 | | LZO_PUBLIC(int) |
196 | | __lzo_init_v2(unsigned v, int s1, int s2, int s3, int s4, int s5, |
197 | | int s6, int s7, int s8, int s9) |
198 | 3 | { |
199 | 3 | int r; |
200 | | |
201 | | #if defined(__LZO_IN_MINILZO) |
202 | | #elif (LZO_CC_MSC && ((_MSC_VER) < 700)) |
203 | | #else |
204 | 3 | #define LZO_WANT_ACC_CHK_CH 1 |
205 | 3 | #undef LZOCHK_ASSERT |
206 | 798 | #define LZOCHK_ASSERT(expr) LZO_COMPILE_TIME_ASSERT(expr) |
207 | 3 | #include "lzo_supp.h" |
208 | 3 | #endif |
209 | 3 | #undef LZOCHK_ASSERT |
210 | | |
211 | 3 | if (v == 0) |
212 | 0 | return LZO_E_ERROR; |
213 | | |
214 | 3 | r = (s1 == -1 || s1 == (int) sizeof(short)) && |
215 | 3 | (s2 == -1 || s2 == (int) sizeof(int)) && |
216 | 3 | (s3 == -1 || s3 == (int) sizeof(long)) && |
217 | 3 | (s4 == -1 || s4 == (int) sizeof(lzo_uint32_t)) && |
218 | 3 | (s5 == -1 || s5 == (int) sizeof(lzo_uint)) && |
219 | 3 | (s6 == -1 || s6 == (int) lzo_sizeof_dict_t) && |
220 | 3 | (s7 == -1 || s7 == (int) sizeof(char *)) && |
221 | 3 | (s8 == -1 || s8 == (int) sizeof(lzo_voidp)) && |
222 | 3 | (s9 == -1 || s9 == (int) sizeof(lzo_callback_t)); |
223 | 3 | if (!r) |
224 | 0 | return LZO_E_ERROR; |
225 | | |
226 | 3 | r = _lzo_config_check(); |
227 | 3 | if (r != LZO_E_OK) |
228 | 0 | return r; |
229 | | |
230 | 3 | return r; |
231 | 3 | } |
232 | | |
233 | | |
234 | | #if !defined(__LZO_IN_MINILZO) |
235 | | #include "lzo_dll.ch" |
236 | | #endif |
237 | | |
238 | | |
239 | | /* vim:set ts=4 sw=4 et: */ |