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