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-2014 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 | { |
---|
82 | return (lzo_voidp) ((lzo_bytep) ptr + off); |
---|
83 | } |
---|
84 | #endif |
---|
85 | |
---|
86 | |
---|
87 | LZO_PUBLIC(int) |
---|
88 | _lzo_config_check(void) |
---|
89 | { |
---|
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 | union lzo_config_check_union u; |
---|
97 | lzo_voidp p; |
---|
98 | unsigned r = 1; |
---|
99 | |
---|
100 | u.a[0] = u.a[1] = 0; |
---|
101 | p = u2p(&u, 0); |
---|
102 | r &= ((* (lzo_bytep) p) == 0); |
---|
103 | #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 | #if (LZO_ABI_LITTLE_ENDIAN) |
---|
110 | u.a[0] = u.a[1] = 0; u.b[0] = 128; |
---|
111 | p = u2p(&u, 0); |
---|
112 | r &= ((* (lzo_uintp) p) == 128); |
---|
113 | #endif |
---|
114 | u.a[0] = u.a[1] = 0; |
---|
115 | u.b[0] = 1; u.b[3] = 2; |
---|
116 | p = u2p(&u, 1); |
---|
117 | r &= UA_GET_NE16(p) == 0; |
---|
118 | r &= UA_GET_LE16(p) == 0; |
---|
119 | u.b[1] = 128; |
---|
120 | r &= UA_GET_LE16(p) == 128; |
---|
121 | u.a[0] = u.a[1] = 0; |
---|
122 | u.b[0] = 3; u.b[5] = 4; |
---|
123 | p = u2p(&u, 1); |
---|
124 | r &= UA_GET_NE32(p) == 0; |
---|
125 | r &= UA_GET_LE32(p) == 0; |
---|
126 | u.b[1] = 128; |
---|
127 | r &= UA_GET_LE32(p) == 128; |
---|
128 | #if defined(UA_GET_NE64) |
---|
129 | u.c[0] = u.c[1] = 0; |
---|
130 | u.b[0] = 5; u.b[9] = 6; |
---|
131 | p = u2p(&u, 1); |
---|
132 | u.c[0] = u.c[1] = 0; |
---|
133 | r &= UA_GET_NE64(p) == 0; |
---|
134 | #if defined(UA_GET_LE64) |
---|
135 | r &= UA_GET_LE64(p) == 0; |
---|
136 | u.b[1] = 128; |
---|
137 | r &= UA_GET_LE64(p) == 128; |
---|
138 | #endif |
---|
139 | #endif |
---|
140 | #if defined(lzo_bitops_ctlz32) |
---|
141 | { unsigned i = 0; lzo_uint32_t v; |
---|
142 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
---|
143 | r &= lzo_bitops_ctlz32(v) == 31 - i; |
---|
144 | r &= lzo_bitops_ctlz32_func(v) == 31 - i; |
---|
145 | }} |
---|
146 | #endif |
---|
147 | #if defined(lzo_bitops_ctlz64) |
---|
148 | { unsigned i = 0; lzo_uint64_t v; |
---|
149 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
---|
150 | r &= lzo_bitops_ctlz64(v) == 63 - i; |
---|
151 | r &= lzo_bitops_ctlz64_func(v) == 63 - i; |
---|
152 | }} |
---|
153 | #endif |
---|
154 | #if defined(lzo_bitops_cttz32) |
---|
155 | { unsigned i = 0; lzo_uint32_t v; |
---|
156 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
---|
157 | r &= lzo_bitops_cttz32(v) == i; |
---|
158 | r &= lzo_bitops_cttz32_func(v) == i; |
---|
159 | }} |
---|
160 | #endif |
---|
161 | #if defined(lzo_bitops_cttz64) |
---|
162 | { unsigned i = 0; lzo_uint64_t v; |
---|
163 | for (v = 1; v != 0 && r == 1; v <<= 1, i++) { |
---|
164 | r &= lzo_bitops_cttz64(v) == i; |
---|
165 | r &= lzo_bitops_cttz64_func(v) == i; |
---|
166 | }} |
---|
167 | #endif |
---|
168 | #endif |
---|
169 | LZO_UNUSED_FUNC(lzo_bitops_unused_funcs); |
---|
170 | |
---|
171 | return r == 1 ? LZO_E_OK : LZO_E_ERROR; |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | /*********************************************************************** |
---|
176 | // |
---|
177 | ************************************************************************/ |
---|
178 | |
---|
179 | LZO_PUBLIC(int) |
---|
180 | __lzo_init_v2(unsigned v, int s1, int s2, int s3, int s4, int s5, |
---|
181 | int s6, int s7, int s8, int s9) |
---|
182 | { |
---|
183 | int r; |
---|
184 | |
---|
185 | #if defined(__LZO_IN_MINILZO) |
---|
186 | #elif (LZO_CC_MSC && ((_MSC_VER) < 700)) |
---|
187 | #else |
---|
188 | #define LZO_WANT_ACC_CHK_CH 1 |
---|
189 | #undef LZOCHK_ASSERT |
---|
190 | #define LZOCHK_ASSERT(expr) LZO_COMPILE_TIME_ASSERT(expr) |
---|
191 | #include "lzo_supp.h" |
---|
192 | #endif |
---|
193 | #undef LZOCHK_ASSERT |
---|
194 | |
---|
195 | if (v == 0) |
---|
196 | return LZO_E_ERROR; |
---|
197 | |
---|
198 | r = (s1 == -1 || s1 == (int) sizeof(short)) && |
---|
199 | (s2 == -1 || s2 == (int) sizeof(int)) && |
---|
200 | (s3 == -1 || s3 == (int) sizeof(long)) && |
---|
201 | (s4 == -1 || s4 == (int) sizeof(lzo_uint32_t)) && |
---|
202 | (s5 == -1 || s5 == (int) sizeof(lzo_uint)) && |
---|
203 | (s6 == -1 || s6 == (int) lzo_sizeof_dict_t) && |
---|
204 | (s7 == -1 || s7 == (int) sizeof(char *)) && |
---|
205 | (s8 == -1 || s8 == (int) sizeof(lzo_voidp)) && |
---|
206 | (s9 == -1 || s9 == (int) sizeof(lzo_callback_t)); |
---|
207 | if (!r) |
---|
208 | return LZO_E_ERROR; |
---|
209 | |
---|
210 | r = _lzo_config_check(); |
---|
211 | if (r != LZO_E_OK) |
---|
212 | return r; |
---|
213 | |
---|
214 | return r; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | #if !defined(__LZO_IN_MINILZO) |
---|
219 | #include "lzo_dll.ch" |
---|
220 | #endif |
---|
221 | |
---|
222 | |
---|
223 | /* |
---|
224 | vi:ts=4:et |
---|
225 | */ |
---|