1 /* lzo_init.c -- initialization of the LZO library
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer
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.
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.
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.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzo/
32 /***********************************************************************
33 // Runtime check of the assumptions about the size of builtin types,
34 // memory model, byte order and other low-level constructs.
36 // We are really paranoid here - LZO should either fail
37 // at startup or not at all.
39 // Because of inlining much of these functions evaluates to nothing.
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 ************************************************************************/
45 #if !defined(__LZO_IN_MINILZO)
47 #define LZO_WANT_ACC_CHK_CH 1
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
)
57 LZOCHK_ASSERT(sizeof(lzo_uintptr_t
) >= sizeof(lzo_voidp
))
58 LZOCHK_ASSERT_IS_UNSIGNED_T(lzo_xint
)
64 /***********************************************************************
66 ************************************************************************/
68 union lzo_config_check_union
{
70 unsigned char b
[2*LZO_MAX(8,sizeof(lzo_uint
))];
71 #if defined(lzo_uint64_t)
78 #define u2p(ptr,off) ((lzo_voidp) (((lzo_bytep)(lzo_voidp)(ptr)) + (off)))
80 static __lzo_noinline lzo_voidp
u2p(lzo_voidp ptr
, lzo_uint off
)
82 return (lzo_voidp
) ((lzo_bytep
) ptr
+ off
);
88 _lzo_config_check(void)
90 #if (LZO_CC_CLANG && (LZO_CC_CLANG >= 0x030100ul && LZO_CC_CLANG < 0x030300ul))
92 /* work around a clang 3.1 and clang 3.2 compiler bug; clang 3.3 and 3.4 work */
96 union lzo_config_check_union u
;
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;
107 r
&= ((* (lzo_uintp
) p
) == 128);
109 #if (LZO_ABI_LITTLE_ENDIAN)
110 u
.a
[0] = u
.a
[1] = 0; u
.b
[0] = 128;
112 r
&= ((* (lzo_uintp
) p
) == 128);
115 u
.b
[0] = 1; u
.b
[3] = 2;
117 r
&= UA_GET_NE16(p
) == 0;
118 r
&= UA_GET_LE16(p
) == 0;
120 r
&= UA_GET_LE16(p
) == 128;
122 u
.b
[0] = 3; u
.b
[5] = 4;
124 r
&= UA_GET_NE32(p
) == 0;
125 r
&= UA_GET_LE32(p
) == 0;
127 r
&= UA_GET_LE32(p
) == 128;
128 #if defined(UA_GET_NE64)
130 u
.b
[0] = 5; u
.b
[9] = 6;
133 r
&= UA_GET_NE64(p
) == 0;
134 #if defined(UA_GET_LE64)
135 r
&= UA_GET_LE64(p
) == 0;
137 r
&= UA_GET_LE64(p
) == 128;
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
;
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
;
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
;
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
;
169 LZO_UNUSED_FUNC(lzo_bitops_unused_funcs
);
171 return r
== 1 ? LZO_E_OK
: LZO_E_ERROR
;
175 /***********************************************************************
177 ************************************************************************/
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
)
185 #if defined(__LZO_IN_MINILZO)
186 #elif (LZO_CC_MSC && ((_MSC_VER) < 700))
188 #define LZO_WANT_ACC_CHK_CH 1
190 #define LZOCHK_ASSERT(expr) LZO_COMPILE_TIME_ASSERT(expr)
191 #include "lzo_supp.h"
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
));
210 r
= _lzo_config_check();
218 #if !defined(__LZO_IN_MINILZO)
219 #include "lzo_dll.ch"