| 65 |
cmwslw |
1 |
/* ucl_util.c -- utilities for the UCL library
|
|
|
2 |
|
|
|
3 |
This file is part of the UCL data compression library.
|
|
|
4 |
|
|
|
5 |
Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
|
|
|
6 |
All Rights Reserved.
|
|
|
7 |
|
|
|
8 |
The UCL 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 UCL 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 UCL library; see the file COPYING.
|
|
|
20 |
If not, write to the Free Software Foundation, Inc.,
|
|
|
21 |
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
22 |
|
|
|
23 |
Markus F.X.J. Oberhumer
|
|
|
24 |
<markus@oberhumer.com>
|
|
|
25 |
http://www.oberhumer.com/opensource/ucl/
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
#include "ucl_conf.h"
|
|
|
30 |
#include "ucl_util.h"
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
/***********************************************************************
|
|
|
34 |
//
|
|
|
35 |
************************************************************************/
|
|
|
36 |
|
|
|
37 |
UCL_PUBLIC(ucl_bool)
|
|
|
38 |
ucl_assert(int expr)
|
|
|
39 |
{
|
|
|
40 |
return (expr) ? 1 : 0;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
/***********************************************************************
|
|
|
45 |
//
|
|
|
46 |
************************************************************************/
|
|
|
47 |
|
|
|
48 |
/* If you use the UCL library in a product, you *must* keep this
|
|
|
49 |
* copyright string in the executable of your product.
|
|
|
50 |
.*/
|
|
|
51 |
|
|
|
52 |
const ucl_byte __ucl_copyright[] =
|
|
|
53 |
"\n\n\n"
|
|
|
54 |
"UCL real-time data compression library.\n"
|
|
|
55 |
"$Copyright: UCL (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 Markus Franz Xaver Johannes Oberhumer $\n"
|
|
|
56 |
"<markus@oberhumer.com>\n"
|
|
|
57 |
"http://www.oberhumer.com\n"
|
|
|
58 |
"\n"
|
|
|
59 |
"UCL version: v" UCL_VERSION_STRING ", " UCL_VERSION_DATE "\n"
|
|
|
60 |
"UCL build date: " __DATE__ " " __TIME__ "\n\n"
|
|
|
61 |
"UCL special compilation options:\n"
|
|
|
62 |
#ifdef __cplusplus
|
|
|
63 |
" __cplusplus\n"
|
|
|
64 |
#endif
|
|
|
65 |
#if defined(__PIC__)
|
|
|
66 |
" __PIC__\n"
|
|
|
67 |
#elif defined(__pic__)
|
|
|
68 |
" __pic__\n"
|
|
|
69 |
#endif
|
|
|
70 |
#if (UINT_MAX < UCL_0xffffffffL)
|
|
|
71 |
" 16BIT\n"
|
|
|
72 |
#endif
|
|
|
73 |
#if defined(__UCL_STRICT_16BIT)
|
|
|
74 |
" __UCL_STRICT_16BIT\n"
|
|
|
75 |
#endif
|
|
|
76 |
#if (UINT_MAX > UCL_0xffffffffL)
|
|
|
77 |
" UINT_MAX=" _UCL_MEXPAND(UINT_MAX) "\n"
|
|
|
78 |
#endif
|
|
|
79 |
#if (ULONG_MAX > UCL_0xffffffffL)
|
|
|
80 |
" ULONG_MAX=" _UCL_MEXPAND(ULONG_MAX) "\n"
|
|
|
81 |
#endif
|
|
|
82 |
#if defined(UCL_BYTE_ORDER)
|
|
|
83 |
" UCL_BYTE_ORDER=" _UCL_MEXPAND(UCL_BYTE_ORDER) "\n"
|
|
|
84 |
#endif
|
|
|
85 |
#if defined(UCL_UNALIGNED_OK_2)
|
|
|
86 |
" UCL_UNALIGNED_OK_2\n"
|
|
|
87 |
#endif
|
|
|
88 |
#if defined(UCL_UNALIGNED_OK_4)
|
|
|
89 |
" UCL_UNALIGNED_OK_4\n"
|
|
|
90 |
#endif
|
|
|
91 |
#if defined(UCL_ALIGNED_OK_4)
|
|
|
92 |
" UCL_ALIGNED_OK_4\n"
|
|
|
93 |
#endif
|
|
|
94 |
#if defined(__UCL_IN_MINIUCL)
|
|
|
95 |
" __UCL_IN_MINIUCL\n"
|
|
|
96 |
#endif
|
|
|
97 |
"\n\n"
|
|
|
98 |
/* RCS information */
|
|
|
99 |
"$Id: UCL " UCL_VERSION_STRING " built " __DATE__ " " __TIME__
|
|
|
100 |
#if defined(__GNUC__) && defined(__VERSION__)
|
|
|
101 |
" by gcc " __VERSION__
|
|
|
102 |
#elif defined(__BORLANDC__)
|
|
|
103 |
" by Borland C " _UCL_MEXPAND(__BORLANDC__)
|
|
|
104 |
#elif defined(_MSC_VER)
|
|
|
105 |
" by Microsoft C " _UCL_MEXPAND(_MSC_VER)
|
|
|
106 |
#elif defined(__PUREC__)
|
|
|
107 |
" by Pure C " _UCL_MEXPAND(__PUREC__)
|
|
|
108 |
#elif defined(__SC__)
|
|
|
109 |
" by Symantec C " _UCL_MEXPAND(__SC__)
|
|
|
110 |
#elif defined(__TURBOC__)
|
|
|
111 |
" by Turbo C " _UCL_MEXPAND(__TURBOC__)
|
|
|
112 |
#elif defined(__WATCOMC__)
|
|
|
113 |
" by Watcom C " _UCL_MEXPAND(__WATCOMC__)
|
|
|
114 |
#endif
|
|
|
115 |
" $\n";
|
|
|
116 |
|
|
|
117 |
UCL_PUBLIC(const ucl_byte *)
|
|
|
118 |
ucl_copyright(void)
|
|
|
119 |
{
|
|
|
120 |
return __ucl_copyright;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
UCL_PUBLIC(ucl_uint32)
|
|
|
124 |
ucl_version(void)
|
|
|
125 |
{
|
|
|
126 |
return UCL_VERSION;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
UCL_PUBLIC(const char *)
|
|
|
130 |
ucl_version_string(void)
|
|
|
131 |
{
|
|
|
132 |
return UCL_VERSION_STRING;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
UCL_PUBLIC(const char *)
|
|
|
136 |
ucl_version_date(void)
|
|
|
137 |
{
|
|
|
138 |
return UCL_VERSION_DATE;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
UCL_PUBLIC(const ucl_charp)
|
|
|
142 |
_ucl_version_string(void)
|
|
|
143 |
{
|
|
|
144 |
return UCL_VERSION_STRING;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
UCL_PUBLIC(const ucl_charp)
|
|
|
148 |
_ucl_version_date(void)
|
|
|
149 |
{
|
|
|
150 |
return UCL_VERSION_DATE;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
/***********************************************************************
|
|
|
155 |
// adler32 checksum
|
|
|
156 |
// adapted from free code by Mark Adler <madler@alumni.caltech.edu>
|
|
|
157 |
// see http://www.cdrom.com/pub/infozip/zlib/
|
|
|
158 |
************************************************************************/
|
|
|
159 |
|
|
|
160 |
#define UCL_BASE 65521u /* largest prime smaller than 65536 */
|
|
|
161 |
#define UCL_NMAX 5552
|
|
|
162 |
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
|
|
|
163 |
|
|
|
164 |
#define UCL_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
|
|
|
165 |
#define UCL_DO2(buf,i) UCL_DO1(buf,i); UCL_DO1(buf,i+1);
|
|
|
166 |
#define UCL_DO4(buf,i) UCL_DO2(buf,i); UCL_DO2(buf,i+2);
|
|
|
167 |
#define UCL_DO8(buf,i) UCL_DO4(buf,i); UCL_DO4(buf,i+4);
|
|
|
168 |
#define UCL_DO16(buf,i) UCL_DO8(buf,i); UCL_DO8(buf,i+8);
|
|
|
169 |
|
|
|
170 |
UCL_PUBLIC(ucl_uint32)
|
|
|
171 |
ucl_adler32(ucl_uint32 adler, const ucl_byte *buf, ucl_uint len)
|
|
|
172 |
{
|
|
|
173 |
ucl_uint32 s1 = adler & 0xffff;
|
|
|
174 |
ucl_uint32 s2 = (adler >> 16) & 0xffff;
|
|
|
175 |
int k;
|
|
|
176 |
|
|
|
177 |
if (buf == NULL)
|
|
|
178 |
return 1;
|
|
|
179 |
|
|
|
180 |
while (len > 0)
|
|
|
181 |
{
|
|
|
182 |
k = len < UCL_NMAX ? (int) len : UCL_NMAX;
|
|
|
183 |
len -= k;
|
|
|
184 |
if (k >= 16) do
|
|
|
185 |
{
|
|
|
186 |
UCL_DO16(buf,0);
|
|
|
187 |
buf += 16;
|
|
|
188 |
k -= 16;
|
|
|
189 |
} while (k >= 16);
|
|
|
190 |
if (k != 0) do
|
|
|
191 |
{
|
|
|
192 |
s1 += *buf++;
|
|
|
193 |
s2 += s1;
|
|
|
194 |
} while (--k > 0);
|
|
|
195 |
s1 %= UCL_BASE;
|
|
|
196 |
s2 %= UCL_BASE;
|
|
|
197 |
}
|
|
|
198 |
return (s2 << 16) | s1;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
|
|
|
202 |
/*
|
|
|
203 |
vi:ts=4:et
|
|
|
204 |
*/
|