summaryrefslogtreecommitdiff
path: root/tools/ucl/src
diff options
context:
space:
mode:
Diffstat (limited to 'tools/ucl/src')
-rw-r--r--tools/ucl/src/alloc.c151
-rw-r--r--tools/ucl/src/fake16.h81
-rw-r--r--tools/ucl/src/getbit.h64
-rw-r--r--tools/ucl/src/internal.h48
-rw-r--r--tools/ucl/src/io.c105
-rwxr-xr-xtools/ucl/src/n2_99.ch643
-rw-r--r--tools/ucl/src/n2b_99.c38
-rw-r--r--tools/ucl/src/n2b_d.c178
-rw-r--r--tools/ucl/src/n2b_ds.c40
-rw-r--r--tools/ucl/src/n2b_to.c79
-rw-r--r--tools/ucl/src/n2d_99.c38
-rw-r--r--tools/ucl/src/n2d_d.c183
-rw-r--r--tools/ucl/src/n2d_ds.c40
-rw-r--r--tools/ucl/src/n2d_to.c79
-rw-r--r--tools/ucl/src/n2e_99.c38
-rw-r--r--tools/ucl/src/n2e_d.c186
-rw-r--r--tools/ucl/src/n2e_ds.c40
-rw-r--r--tools/ucl/src/n2e_to.c79
-rw-r--r--tools/ucl/src/ucl_conf.h359
-rw-r--r--tools/ucl/src/ucl_crc.c135
-rw-r--r--tools/ucl/src/ucl_dll.c61
-rw-r--r--tools/ucl/src/ucl_init.c500
-rwxr-xr-xtools/ucl/src/ucl_mchw.ch313
-rw-r--r--tools/ucl/src/ucl_ptr.c81
-rw-r--r--tools/ucl/src/ucl_ptr.h211
-rw-r--r--tools/ucl/src/ucl_str.c133
-rwxr-xr-xtools/ucl/src/ucl_swd.ch665
-rw-r--r--tools/ucl/src/ucl_util.c204
-rw-r--r--tools/ucl/src/ucl_util.h180
29 files changed, 4952 insertions, 0 deletions
diff --git a/tools/ucl/src/alloc.c b/tools/ucl/src/alloc.c
new file mode 100644
index 0000000000..d32ea5113a
--- /dev/null
+++ b/tools/ucl/src/alloc.c
@@ -0,0 +1,151 @@
1/* alloc.c -- memory allocation
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
31#if defined(HAVE_MALLOC_H)
32# include <malloc.h>
33#endif
34#if defined(__palmos__)
35# include <System/MemoryMgr.h>
36#endif
37
38
39#undef ucl_alloc_hook
40#undef ucl_free_hook
41#undef ucl_alloc
42#undef ucl_malloc
43#undef ucl_free
44
45
46/***********************************************************************
47// implementation
48************************************************************************/
49
50UCL_PRIVATE(ucl_voidp)
51ucl_alloc_internal(ucl_uint nelems, ucl_uint size)
52{
53 ucl_voidp p = NULL;
54 unsigned long s = (unsigned long) nelems * size;
55
56 if (nelems <= 0 || size <= 0 || s < nelems || s < size)
57 return NULL;
58
59#if defined(__palmos__)
60 p = (ucl_voidp) MemPtrNew(s);
61#elif (UCL_UINT_MAX <= SIZE_T_MAX)
62 if (s < SIZE_T_MAX)
63 p = (ucl_voidp) malloc((size_t)s);
64#elif defined(HAVE_HALLOC) && defined(__DMC__)
65 if (size < SIZE_T_MAX)
66 p = (ucl_voidp) _halloc(nelems,(size_t)size);
67#elif defined(HAVE_HALLOC)
68 if (size < SIZE_T_MAX)
69 p = (ucl_voidp) halloc(nelems,(size_t)size);
70#else
71 if (s < SIZE_T_MAX)
72 p = (ucl_voidp) malloc((size_t)s);
73#endif
74
75 return p;
76}
77
78
79UCL_PRIVATE(void)
80ucl_free_internal(ucl_voidp p)
81{
82 if (!p)
83 return;
84
85#if defined(__palmos__)
86 MemPtrFree(p);
87#elif (UCL_UINT_MAX <= SIZE_T_MAX)
88 free(p);
89#elif defined(HAVE_HALLOC) && defined(__DMC__)
90 _hfree(p);
91#elif defined(HAVE_HALLOC)
92 hfree(p);
93#else
94 free(p);
95#endif
96}
97
98
99/***********************************************************************
100// public interface using the global hooks
101************************************************************************/
102
103/* global allocator hooks */
104ucl_alloc_hook_t ucl_alloc_hook = ucl_alloc_internal;
105ucl_free_hook_t ucl_free_hook = ucl_free_internal;
106
107
108UCL_PUBLIC(ucl_voidp)
109ucl_alloc(ucl_uint nelems, ucl_uint size)
110{
111 if (!ucl_alloc_hook)
112 return NULL;
113
114 return ucl_alloc_hook(nelems,size);
115}
116
117
118UCL_PUBLIC(ucl_voidp)
119ucl_malloc(ucl_uint size)
120{
121 if (!ucl_alloc_hook)
122 return NULL;
123
124#if defined(__palmos__)
125 return ucl_alloc_hook(size,1);
126#elif (UCL_UINT_MAX <= SIZE_T_MAX)
127 return ucl_alloc_hook(size,1);
128#elif defined(HAVE_HALLOC)
129 /* use segment granularity by default */
130 if (size + 15 > size) /* avoid overflow */
131 return ucl_alloc_hook((size+15)/16,16);
132 return ucl_alloc_hook(size,1);
133#else
134 return ucl_alloc_hook(size,1);
135#endif
136}
137
138
139UCL_PUBLIC(void)
140ucl_free(ucl_voidp p)
141{
142 if (!ucl_free_hook)
143 return;
144
145 ucl_free_hook(p);
146}
147
148
149/*
150vi:ts=4:et
151*/
diff --git a/tools/ucl/src/fake16.h b/tools/ucl/src/fake16.h
new file mode 100644
index 0000000000..db773d5284
--- /dev/null
+++ b/tools/ucl/src/fake16.h
@@ -0,0 +1,81 @@
1/* fake16.h -- fake the strict 16-bit memory model for test purposes
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 */
26
27
28/*
29 * NOTE:
30 * this file is *only* for testing the strict 16-bit memory model
31 * on a 32-bit machine. Because things like integral promotion,
32 * size_t and ptrdiff_t cannot be faked this is no real substitute
33 * for testing under a real 16-bit system.
34 *
35 * See also <ucl/ucl16bit.h>
36 *
37 * Usage: #include "src/fake16.h" at the top of <ucl/uclconf.h>
38 */
39
40
41#ifndef __UCLFAKE16BIT_H
42#define __UCLFAKE16BIT_H
43
44#ifdef __UCLCONF_H
45# error "include this file before uclconf.h"
46#endif
47
48#include <limits.h>
49
50#if (USHRT_MAX == 0xffff)
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56#define __UCL16BIT_H /* do not use <ucl/ucl16bit.h> */
57
58#define __UCL_STRICT_16BIT
59#define __UCL_FAKE_STRICT_16BIT
60
61#define UCL_99_UNSUPPORTED
62#define UCL_999_UNSUPPORTED
63
64typedef unsigned short ucl_uint;
65typedef short ucl_int;
66#define UCL_UINT_MAX USHRT_MAX
67#define UCL_INT_MAX SHRT_MAX
68
69#if 1
70#define __UCL_NO_UNALIGNED
71#define __UCL_NO_ALIGNED
72#endif
73
74#ifdef __cplusplus
75} /* extern "C" */
76#endif
77
78#endif
79
80#endif /* already included */
81
diff --git a/tools/ucl/src/getbit.h b/tools/ucl/src/getbit.h
new file mode 100644
index 0000000000..be27254cd4
--- /dev/null
+++ b/tools/ucl/src/getbit.h
@@ -0,0 +1,64 @@
1/* getbit.h -- bit-buffer access
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/***********************************************************************
30//
31************************************************************************/
32
33#if 1
34#define getbit_8(bb, src, ilen) \
35 (((bb = bb & 0x7f ? bb*2 : ((unsigned)src[ilen++]*2+1)) >> 8) & 1)
36#elif 1
37#define getbit_8(bb, src, ilen) \
38 (bb*=2,bb&0xff ? (bb>>8)&1 : ((bb=src[ilen++]*2+1)>>8)&1)
39#else
40#define getbit_8(bb, src, ilen) \
41 (((bb*=2, (bb&0xff ? bb : (bb=src[ilen++]*2+1,bb))) >> 8) & 1)
42#endif
43
44
45#define getbit_le16(bb, src, ilen) \
46 (bb*=2,bb&0xffff ? (bb>>16)&1 : (ilen+=2,((bb=(src[ilen-2]+src[ilen-1]*256u)*2+1)>>16)&1))
47
48
49#if 1 && defined(UCL_UNALIGNED_OK_4) && (UCL_BYTE_ORDER == UCL_LITTLE_ENDIAN)
50#define getbit_le32(bb, bc, src, ilen) \
51 (bc > 0 ? ((bb>>--bc)&1) : (bc=31,\
52 bb=*(const ucl_uint32p)((src)+ilen),ilen+=4,(bb>>31)&1))
53#else
54#define getbit_le32(bb, bc, src, ilen) \
55 (bc > 0 ? ((bb>>--bc)&1) : (bc=31,\
56 bb=src[ilen]+src[ilen+1]*0x100+src[ilen+2]*UCL_UINT32_C(0x10000)+src[ilen+3]*UCL_UINT32_C(0x1000000),\
57 ilen+=4,(bb>>31)&1))
58#endif
59
60
61/*
62vi:ts=4:et
63*/
64
diff --git a/tools/ucl/src/internal.h b/tools/ucl/src/internal.h
new file mode 100644
index 0000000000..101ed45cbb
--- /dev/null
+++ b/tools/ucl/src/internal.h
@@ -0,0 +1,48 @@
1/* internal.h --
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 */
26
27
28/* WARNING: this file should *not* be used by applications. It is
29 part of the implementation of the library and is subject
30 to change.
31 */
32
33
34#ifndef __UCL_INTERNAL_H
35#define __UCL_INTERNAL_H
36
37
38/***********************************************************************
39//
40************************************************************************/
41
42
43#endif /* already included */
44
45/*
46vi:ts=4:et
47*/
48
diff --git a/tools/ucl/src/io.c b/tools/ucl/src/io.c
new file mode 100644
index 0000000000..58bd436ac0
--- /dev/null
+++ b/tools/ucl/src/io.c
@@ -0,0 +1,105 @@
1/* io.c -- io functions
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
31#if !defined(NO_STDIO_H)
32
33#include <stdio.h>
34#include <ucl/uclutil.h>
35
36#undef ucl_fread
37#undef ucl_fwrite
38
39
40/***********************************************************************
41//
42************************************************************************/
43
44UCL_PUBLIC(ucl_uint)
45ucl_fread(FILE *f, ucl_voidp s, ucl_uint len)
46{
47#if 1 && (UCL_UINT_MAX <= SIZE_T_MAX)
48 return fread(s,1,len,f);
49#else
50 ucl_byte *p = (ucl_byte *) s;
51 ucl_uint l = 0;
52 size_t k;
53 unsigned char *b;
54 unsigned char buf[512];
55
56 while (l < len)
57 {
58 k = len - l > sizeof(buf) ? sizeof(buf) : (size_t) (len - l);
59 k = fread(buf,1,k,f);
60 if (k <= 0)
61 break;
62 l += k;
63 b = buf; do *p++ = *b++; while (--k > 0);
64 }
65 return l;
66#endif
67}
68
69
70/***********************************************************************
71//
72************************************************************************/
73
74UCL_PUBLIC(ucl_uint)
75ucl_fwrite(FILE *f, const ucl_voidp s, ucl_uint len)
76{
77#if 1 && (UCL_UINT_MAX <= SIZE_T_MAX)
78 return fwrite(s,1,len,f);
79#else
80 const ucl_byte *p = (const ucl_byte *) s;
81 ucl_uint l = 0;
82 size_t k, n;
83 unsigned char *b;
84 unsigned char buf[512];
85
86 while (l < len)
87 {
88 k = len - l > sizeof(buf) ? sizeof(buf) : (size_t) (len - l);
89 b = buf; n = k; do *b++ = *p++; while (--n > 0);
90 k = fwrite(buf,1,k,f);
91 if (k <= 0)
92 break;
93 l += k;
94 }
95 return l;
96#endif
97}
98
99
100#endif /* !defined(NO_STDIO_H) */
101
102
103/*
104vi:ts=4:et
105*/
diff --git a/tools/ucl/src/n2_99.ch b/tools/ucl/src/n2_99.ch
new file mode 100755
index 0000000000..5df69baaf4
--- /dev/null
+++ b/tools/ucl/src/n2_99.ch
@@ -0,0 +1,643 @@
1/* n2_99.ch -- implementation of the NRV2[BDE]-99 compression algorithms
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
30#include <ucl/uclconf.h>
31#include <ucl/ucl.h>
32#include "ucl_conf.h"
33
34#if 0
35#undef UCL_DEBUG
36#define UCL_DEBUG
37#endif
38
39#include <stdio.h>
40
41#if 0 && !defined(UCL_DEBUG)
42#undef NDEBUG
43#include <assert.h>
44#endif
45
46
47/***********************************************************************
48//
49************************************************************************/
50
51#if 0
52#define N (128*1024ul) /* size of ring buffer */
53#else
54#define N (1024*1024ul) /* size of ring buffer */
55#define SWD_USE_MALLOC
56#define SWD_HSIZE 65536ul
57#endif
58#define THRESHOLD 1 /* lower limit for match length */
59#define F 2048 /* upper limit for match length */
60
61#if defined(NRV2B)
62# define UCL_COMPRESS_T ucl_nrv2b_t
63# define ucl_swd_t ucl_nrv2b_swd_t
64# define ucl_nrv_99_compress ucl_nrv2b_99_compress
65# define M2_MAX_OFFSET 0xd00
66#elif defined(NRV2D)
67# define UCL_COMPRESS_T ucl_nrv2d_t
68# define ucl_swd_t ucl_nrv2d_swd_t
69# define ucl_nrv_99_compress ucl_nrv2d_99_compress
70# define M2_MAX_OFFSET 0x500
71#elif defined(NRV2E)
72# define UCL_COMPRESS_T ucl_nrv2e_t
73# define ucl_swd_t ucl_nrv2e_swd_t
74# define ucl_nrv_99_compress ucl_nrv2e_99_compress
75# define M2_MAX_OFFSET 0x500
76#else
77# error
78#endif
79#define ucl_swd_p ucl_swd_t * __UCL_MMODEL
80
81#if 0
82# define HEAD3(b,p) \
83 ((((((ucl_uint32)b[p]<<3)^b[p+1])<<3)^b[p+2]) & (SWD_HSIZE-1))
84#endif
85#if 0 && defined(UCL_UNALIGNED_OK_4) && (UCL_BYTE_ORDER == UCL_LITTLE_ENDIAN)
86# define HEAD3(b,p) \
87 (((* (ucl_uint32p) &b[p]) ^ ((* (ucl_uint32p) &b[p])>>10)) & (SWD_HSIZE-1))
88#endif
89
90#include "ucl_mchw.ch"
91
92
93/***********************************************************************
94//
95************************************************************************/
96
97static void code_prefix_ss11(UCL_COMPRESS_T *c, ucl_uint32 i)
98{
99 if (i >= 2)
100 {
101 ucl_uint32 t = 4;
102 i += 2;
103 do {
104 t <<= 1;
105 } while (i >= t);
106 t >>= 1;
107 do {
108 t >>= 1;
109 bbPutBit(c, (i & t) ? 1 : 0);
110 bbPutBit(c, 0);
111 } while (t > 2);
112 }
113 bbPutBit(c, (unsigned)i & 1);
114 bbPutBit(c, 1);
115}
116
117
118#if defined(NRV2D) || defined(NRV2E)
119static void code_prefix_ss12(UCL_COMPRESS_T *c, ucl_uint32 i)
120{
121 if (i >= 2)
122 {
123 ucl_uint32 t = 2;
124 do {
125 i -= t;
126 t <<= 2;
127 } while (i >= t);
128 do {
129 t >>= 1;
130 bbPutBit(c, (i & t) ? 1 : 0);
131 bbPutBit(c, 0);
132 t >>= 1;
133 bbPutBit(c, (i & t) ? 1 : 0);
134 } while (t > 2);
135 }
136 bbPutBit(c, (unsigned)i & 1);
137 bbPutBit(c, 1);
138}
139#endif
140
141
142static void
143code_match(UCL_COMPRESS_T *c, ucl_uint m_len, const ucl_uint m_off)
144{
145 unsigned m_low = 0;
146
147 while (m_len > c->conf.max_match)
148 {
149 code_match(c, c->conf.max_match - 3, m_off);
150 m_len -= c->conf.max_match - 3;
151 }
152
153 c->match_bytes += m_len;
154 if (m_len > c->result[3])
155 c->result[3] = m_len;
156 if (m_off > c->result[1])
157 c->result[1] = m_off;
158
159 bbPutBit(c, 0);
160
161#if defined(NRV2B)
162 if (m_off == c->last_m_off)
163 {
164 bbPutBit(c, 0);
165 bbPutBit(c, 1);
166 }
167 else
168 {
169 code_prefix_ss11(c, 1 + ((m_off - 1) >> 8));
170 bbPutByte(c, (unsigned)m_off - 1);
171 }
172 m_len = m_len - 1 - (m_off > M2_MAX_OFFSET);
173 if (m_len >= 4)
174 {
175 bbPutBit(c,0);
176 bbPutBit(c,0);
177 code_prefix_ss11(c, m_len - 4);
178 }
179 else
180 {
181 bbPutBit(c, m_len > 1);
182 bbPutBit(c, (unsigned)m_len & 1);
183 }
184#elif defined(NRV2D)
185 m_len = m_len - 1 - (m_off > M2_MAX_OFFSET);
186 assert(m_len > 0);
187 m_low = (m_len >= 4) ? 0u : (unsigned) m_len;
188 if (m_off == c->last_m_off)
189 {
190 bbPutBit(c, 0);
191 bbPutBit(c, 1);
192 bbPutBit(c, m_low > 1);
193 bbPutBit(c, m_low & 1);
194 }
195 else
196 {
197 code_prefix_ss12(c, 1 + ((m_off - 1) >> 7));
198 bbPutByte(c, ((((unsigned)m_off - 1) & 0x7f) << 1) | ((m_low > 1) ? 0 : 1));
199 bbPutBit(c, m_low & 1);
200 }
201 if (m_len >= 4)
202 code_prefix_ss11(c, m_len - 4);
203#elif defined(NRV2E)
204 m_len = m_len - 1 - (m_off > M2_MAX_OFFSET);
205 assert(m_len > 0);
206 m_low = (m_len <= 2);
207 if (m_off == c->last_m_off)
208 {
209 bbPutBit(c, 0);
210 bbPutBit(c, 1);
211 bbPutBit(c, m_low);
212 }
213 else
214 {
215 code_prefix_ss12(c, 1 + ((m_off - 1) >> 7));
216 bbPutByte(c, ((((unsigned)m_off - 1) & 0x7f) << 1) | (m_low ^ 1));
217 }
218 if (m_low)
219 bbPutBit(c, (unsigned)m_len - 1);
220 else if (m_len <= 4)
221 {
222 bbPutBit(c, 1);
223 bbPutBit(c, (unsigned)m_len - 3);
224 }
225 else
226 {
227 bbPutBit(c, 0);
228 code_prefix_ss11(c, m_len - 5);
229 }
230#else
231# error
232#endif
233
234 c->last_m_off = m_off;
235 UCL_UNUSED(m_low);
236}
237
238
239static void
240code_run(UCL_COMPRESS_T *c, const ucl_byte *ii, ucl_uint lit)
241{
242 if (lit == 0)
243 return;
244 c->lit_bytes += lit;
245 if (lit > c->result[5])
246 c->result[5] = lit;
247 do {
248 bbPutBit(c, 1);
249 bbPutByte(c, *ii++);
250 } while (--lit > 0);
251}
252
253
254/***********************************************************************
255//
256************************************************************************/
257
258static int
259len_of_coded_match(UCL_COMPRESS_T *c, ucl_uint m_len, ucl_uint m_off)
260{
261 int b;
262 if (m_len < 2 || (m_len == 2 && (m_off > M2_MAX_OFFSET))
263 || m_off > c->conf.max_offset)
264 return -1;
265 assert(m_off > 0);
266
267 m_len = m_len - 2 - (m_off > M2_MAX_OFFSET);
268
269 if (m_off == c->last_m_off)
270 b = 1 + 2;
271 else
272 {
273#if defined(NRV2B)
274 b = 1 + 10;
275 m_off = (m_off - 1) >> 8;
276 while (m_off > 0)
277 {
278 b += 2;
279 m_off >>= 1;
280 }
281#elif defined(NRV2D) || defined(NRV2E)
282 b = 1 + 9;
283 m_off = (m_off - 1) >> 7;
284 while (m_off > 0)
285 {
286 b += 3;
287 m_off >>= 2;
288 }
289#else
290# error
291#endif
292 }
293
294#if defined(NRV2B) || defined(NRV2D)
295 b += 2;
296 if (m_len < 3)
297 return b;
298 m_len -= 3;
299#elif defined(NRV2E)
300 b += 2;
301 if (m_len < 2)
302 return b;
303 if (m_len < 4)
304 return b + 1;
305 m_len -= 4;
306#else
307# error
308#endif
309 do {
310 b += 2;
311 m_len >>= 1;
312 } while (m_len > 0);
313
314 return b;
315}
316
317
318/***********************************************************************
319//
320************************************************************************/
321
322#if !defined(NDEBUG)
323static
324void assert_match( const ucl_swd_p swd, ucl_uint m_len, ucl_uint m_off )
325{
326 const UCL_COMPRESS_T *c = swd->c;
327 ucl_uint d_off;
328
329 assert(m_len >= 2);
330 if (m_off <= (ucl_uint) (c->bp - c->in))
331 {
332 assert(c->bp - m_off + m_len < c->ip);
333 assert(ucl_memcmp(c->bp, c->bp - m_off, m_len) == 0);
334 }
335 else
336 {
337 assert(swd->dict != NULL);
338 d_off = m_off - (ucl_uint) (c->bp - c->in);
339 assert(d_off <= swd->dict_len);
340 if (m_len > d_off)
341 {
342 assert(ucl_memcmp(c->bp, swd->dict_end - d_off, d_off) == 0);
343 assert(c->in + m_len - d_off < c->ip);
344 assert(ucl_memcmp(c->bp + d_off, c->in, m_len - d_off) == 0);
345 }
346 else
347 {
348 assert(ucl_memcmp(c->bp, swd->dict_end - d_off, m_len) == 0);
349 }
350 }
351}
352#else
353# define assert_match(a,b,c) ((void)0)
354#endif
355
356
357#if defined(SWD_BEST_OFF)
358
359static void
360better_match ( const ucl_swd_p swd, ucl_uint *m_len, ucl_uint *m_off )
361{
362}
363
364#endif
365
366
367/***********************************************************************
368//
369************************************************************************/
370
371UCL_PUBLIC(int)
372ucl_nrv_99_compress ( const ucl_bytep in, ucl_uint in_len,
373 ucl_bytep out, ucl_uintp out_len,
374 ucl_progress_callback_p cb,
375 int level,
376 const struct ucl_compress_config_p conf,
377 ucl_uintp result)
378{
379 const ucl_byte *ii;
380 ucl_uint lit;
381 ucl_uint m_len, m_off;
382 UCL_COMPRESS_T c_buffer;
383 UCL_COMPRESS_T * const c = &c_buffer;
384#undef swd
385#if 1 && defined(SWD_USE_MALLOC)
386 ucl_swd_t the_swd;
387# define swd (&the_swd)
388#else
389 ucl_swd_p swd;
390#endif
391 ucl_uint result_buffer[16];
392 int r;
393
394 struct swd_config_t
395 {
396 unsigned try_lazy;
397 ucl_uint good_length;
398 ucl_uint max_lazy;
399 ucl_uint nice_length;
400 ucl_uint max_chain;
401 ucl_uint32 flags;
402 ucl_uint32 max_offset;
403 };
404 const struct swd_config_t *sc;
405 static const struct swd_config_t swd_config[10] = {
406 /* faster compression */
407 { 0, 0, 0, 8, 4, 0, 48*1024L },
408 { 0, 0, 0, 16, 8, 0, 48*1024L },
409 { 0, 0, 0, 32, 16, 0, 48*1024L },
410 { 1, 4, 4, 16, 16, 0, 48*1024L },
411 { 1, 8, 16, 32, 32, 0, 48*1024L },
412 { 1, 8, 16, 128, 128, 0, 48*1024L },
413 { 2, 8, 32, 128, 256, 0, 128*1024L },
414 { 2, 32, 128, F, 2048, 1, 128*1024L },
415 { 2, 32, 128, F, 2048, 1, 256*1024L },
416 { 2, F, F, F, 4096, 1, N }
417 /* max. compression */
418 };
419
420 if (level < 1 || level > 10)
421 return UCL_E_INVALID_ARGUMENT;
422 sc = &swd_config[level - 1];
423
424 memset(c, 0, sizeof(*c));
425 c->ip = c->in = in;
426 c->in_end = in + in_len;
427 c->out = out;
428 if (cb && cb->callback)
429 c->cb = cb;
430 cb = NULL;
431 c->result = result ? result : (ucl_uintp) result_buffer;
432 memset(c->result, 0, 16*sizeof(*c->result));
433 c->result[0] = c->result[2] = c->result[4] = UCL_UINT_MAX;
434 result = NULL;
435 memset(&c->conf, 0xff, sizeof(c->conf));
436 if (conf)
437 memcpy(&c->conf, conf, sizeof(c->conf));
438 conf = NULL;
439 r = bbConfig(c, 0, 8);
440 if (r == 0)
441 r = bbConfig(c, c->conf.bb_endian, c->conf.bb_size);
442 if (r != 0)
443 return UCL_E_INVALID_ARGUMENT;
444 c->bb_op = out;
445
446 ii = c->ip; /* point to start of literal run */
447 lit = 0;
448
449#if !defined(swd)
450 swd = (ucl_swd_p) ucl_alloc(1, ucl_sizeof(*swd));
451 if (!swd)
452 return UCL_E_OUT_OF_MEMORY;
453#endif
454 swd->f = UCL_MIN(F, c->conf.max_match);
455 swd->n = UCL_MIN(N, sc->max_offset);
456 if (c->conf.max_offset != UCL_UINT_MAX)
457 swd->n = UCL_MIN(N, c->conf.max_offset);
458 if (in_len >= 256 && in_len < swd->n)
459 swd->n = in_len;
460 if (swd->f < 8 || swd->n < 256)
461 return UCL_E_INVALID_ARGUMENT;
462 r = init_match(c,swd,NULL,0,sc->flags);
463 if (r != UCL_E_OK)
464 {
465#if !defined(swd)
466 ucl_free(swd);
467#endif
468 return r;
469 }
470 if (sc->max_chain > 0)
471 swd->max_chain = sc->max_chain;
472 if (sc->nice_length > 0)
473 swd->nice_length = sc->nice_length;
474 if (c->conf.max_match < swd->nice_length)
475 swd->nice_length = c->conf.max_match;
476
477 if (c->cb)
478 (*c->cb->callback)(0,0,-1,c->cb->user);
479
480 c->last_m_off = 1;
481 r = find_match(c,swd,0,0);
482 if (r != UCL_E_OK)
483 return r;
484 while (c->look > 0)
485 {
486 ucl_uint ahead;
487 ucl_uint max_ahead;
488 int l1, l2;
489
490 c->codesize = c->bb_op - out;
491
492 m_len = c->m_len;
493 m_off = c->m_off;
494
495 assert(c->bp == c->ip - c->look);
496 assert(c->bp >= in);
497 if (lit == 0)
498 ii = c->bp;
499 assert(ii + lit == c->bp);
500 assert(swd->b_char == *(c->bp));
501
502 if (m_len < 2 || (m_len == 2 && (m_off > M2_MAX_OFFSET))
503 || m_off > c->conf.max_offset)
504 {
505 /* a literal */
506 lit++;
507 swd->max_chain = sc->max_chain;
508 r = find_match(c,swd,1,0);
509 assert(r == 0);
510 continue;
511 }
512
513 /* a match */
514#if defined(SWD_BEST_OFF)
515 if (swd->use_best_off)
516 better_match(swd,&m_len,&m_off);
517#endif
518 assert_match(swd,m_len,m_off);
519
520 /* shall we try a lazy match ? */
521 ahead = 0;
522 if (sc->try_lazy <= 0 || m_len >= sc->max_lazy || m_off == c->last_m_off)
523 {
524 /* no */
525 l1 = 0;
526 max_ahead = 0;
527 }
528 else
529 {
530 /* yes, try a lazy match */
531 l1 = len_of_coded_match(c,m_len,m_off);
532 assert(l1 > 0);
533 max_ahead = UCL_MIN(sc->try_lazy, m_len - 1);
534 }
535
536 while (ahead < max_ahead && c->look > m_len)
537 {
538 if (m_len >= sc->good_length)
539 swd->max_chain = sc->max_chain >> 2;
540 else
541 swd->max_chain = sc->max_chain;
542 r = find_match(c,swd,1,0);
543 ahead++;
544
545 assert(r == 0);
546 assert(c->look > 0);
547 assert(ii + lit + ahead == c->bp);
548
549 if (c->m_len < 2)
550 continue;
551#if defined(SWD_BEST_OFF)
552 if (swd->use_best_off)
553 better_match(swd,&c->m_len,&c->m_off);
554#endif
555 l2 = len_of_coded_match(c,c->m_len,c->m_off);
556 if (l2 < 0)
557 continue;
558#if 1
559 if (l1 + (int)(ahead + c->m_len - m_len) * 5 > l2 + (int)(ahead) * 9)
560#else
561 if (l1 > l2)
562#endif
563 {
564 c->lazy++;
565 assert_match(swd,c->m_len,c->m_off);
566
567#if 0
568 if (l3 > 0)
569 {
570 /* code previous run */
571 code_run(c,ii,lit);
572 lit = 0;
573 /* code shortened match */
574 code_match(c,ahead,m_off);
575 }
576 else
577#endif
578 {
579 lit += ahead;
580 assert(ii + lit == c->bp);
581 }
582 goto lazy_match_done;
583 }
584 }
585
586 assert(ii + lit + ahead == c->bp);
587
588 /* 1 - code run */
589 code_run(c,ii,lit);
590 lit = 0;
591
592 /* 2 - code match */
593 code_match(c,m_len,m_off);
594 swd->max_chain = sc->max_chain;
595 r = find_match(c,swd,m_len,1+ahead);
596 assert(r == 0);
597
598lazy_match_done: ;
599 }
600
601 /* store final run */
602 code_run(c,ii,lit);
603
604 /* EOF */
605 bbPutBit(c, 0);
606#if defined(NRV2B)
607 code_prefix_ss11(c, UCL_UINT32_C(0x1000000));
608 bbPutByte(c, 0xff);
609#elif defined(NRV2D) || defined(NRV2E)
610 code_prefix_ss12(c, UCL_UINT32_C(0x1000000));
611 bbPutByte(c, 0xff);
612#else
613# error
614#endif
615 bbFlushBits(c, 0);
616
617 assert(c->textsize == in_len);
618 c->codesize = c->bb_op - out;
619 *out_len = c->bb_op - out;
620 if (c->cb)
621 (*c->cb->callback)(c->textsize,c->codesize,4,c->cb->user);
622
623#if 0
624 printf("%7ld %7ld -> %7ld %7ld %7ld %ld (max: %d %d %d)\n",
625 (long) c->textsize, (long) in_len, (long) c->codesize,
626 c->match_bytes, c->lit_bytes, c->lazy,
627 c->result[1], c->result[3], c->result[5]);
628#endif
629 assert(c->lit_bytes + c->match_bytes == in_len);
630
631 swd_exit(swd);
632#if !defined(swd)
633 ucl_free(swd);
634#endif
635 return UCL_E_OK;
636#undef swd
637}
638
639
640/*
641vi:ts=4:et
642*/
643
diff --git a/tools/ucl/src/n2b_99.c b/tools/ucl/src/n2b_99.c
new file mode 100644
index 0000000000..d2e6d6edf5
--- /dev/null
+++ b/tools/ucl/src/n2b_99.c
@@ -0,0 +1,38 @@
1/* n2b_99.c -- implementation of the NRV2B-99 compression algorithm
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
30#define NRV2B
31#include "n2_99.ch"
32#undef NRV2B
33
34
35/*
36vi:ts=4:et
37*/
38
diff --git a/tools/ucl/src/n2b_d.c b/tools/ucl/src/n2b_d.c
new file mode 100644
index 0000000000..e8f96d6d37
--- /dev/null
+++ b/tools/ucl/src/n2b_d.c
@@ -0,0 +1,178 @@
1/* n2b_d.c -- implementation of the NRV2B decompression algorithm
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/***********************************************************************
30// actual implementation used by a recursive #include
31************************************************************************/
32
33#ifdef getbit
34
35#ifdef SAFE
36#define fail(x,r) if (x) { *dst_len = olen; return r; }
37#else
38#define fail(x,r)
39#endif
40
41{
42 ucl_uint32 bb = 0;
43#ifdef TEST_OVERLAP
44 ucl_uint ilen = src_off, olen = 0, last_m_off = 1;
45#else
46 ucl_uint ilen = 0, olen = 0, last_m_off = 1;
47#endif
48#ifdef SAFE
49 const ucl_uint oend = *dst_len;
50#endif
51 UCL_UNUSED(wrkmem);
52
53#ifdef TEST_OVERLAP
54 src_len += src_off;
55 fail(oend >= src_len, UCL_E_OVERLAP_OVERRUN);
56#endif
57
58 for (;;)
59 {
60 ucl_uint m_off, m_len;
61
62 while (getbit(bb))
63 {
64 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
65 fail(olen >= oend, UCL_E_OUTPUT_OVERRUN);
66#ifdef TEST_OVERLAP
67 fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
68 olen++; ilen++;
69#else
70 dst[olen++] = src[ilen++];
71#endif
72 }
73 m_off = 1;
74 do {
75 m_off = m_off*2 + getbit(bb);
76 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
77 fail(m_off > UCL_UINT32_C(0xffffff) + 3, UCL_E_LOOKBEHIND_OVERRUN);
78 } while (!getbit(bb));
79 if (m_off == 2)
80 {
81 m_off = last_m_off;
82 }
83 else
84 {
85 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
86 m_off = (m_off-3)*256 + src[ilen++];
87 if (m_off == UCL_UINT32_C(0xffffffff))
88 break;
89 last_m_off = ++m_off;
90 }
91 m_len = getbit(bb);
92 m_len = m_len*2 + getbit(bb);
93 if (m_len == 0)
94 {
95 m_len++;
96 do {
97 m_len = m_len*2 + getbit(bb);
98 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
99 fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN);
100 } while (!getbit(bb));
101 m_len += 2;
102 }
103 m_len += (m_off > 0xd00);
104 fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN);
105 fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN);
106#ifdef TEST_OVERLAP
107 olen += m_len + 1;
108 fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
109#else
110 {
111 const ucl_byte *m_pos;
112 m_pos = dst + olen - m_off;
113 dst[olen++] = *m_pos++;
114 do dst[olen++] = *m_pos++; while (--m_len > 0);
115 }
116#endif
117 }
118 *dst_len = olen;
119 return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN);
120}
121
122#undef fail
123
124#endif /* getbit */
125
126
127/***********************************************************************
128// decompressor entries for the different bit-buffer sizes
129************************************************************************/
130
131#ifndef getbit
132
133#include <ucl/ucl.h>
134#include "ucl_conf.h"
135#include "getbit.h"
136
137
138UCL_PUBLIC(int)
139ucl_nrv2b_decompress_8 ( const ucl_bytep src, ucl_uint src_len,
140 ucl_bytep dst, ucl_uintp dst_len,
141 ucl_voidp wrkmem )
142{
143#define getbit(bb) getbit_8(bb,src,ilen)
144#include "n2b_d.c"
145#undef getbit
146}
147
148
149UCL_PUBLIC(int)
150ucl_nrv2b_decompress_le16 ( const ucl_bytep src, ucl_uint src_len,
151 ucl_bytep dst, ucl_uintp dst_len,
152 ucl_voidp wrkmem )
153{
154#define getbit(bb) getbit_le16(bb,src,ilen)
155#include "n2b_d.c"
156#undef getbit
157}
158
159
160UCL_PUBLIC(int)
161ucl_nrv2b_decompress_le32 ( const ucl_bytep src, ucl_uint src_len,
162 ucl_bytep dst, ucl_uintp dst_len,
163 ucl_voidp wrkmem )
164{
165 unsigned bc = 0;
166#define getbit(bb) getbit_le32(bb,bc,src,ilen)
167#include "n2b_d.c"
168#undef getbit
169}
170
171
172#endif /* !getbit */
173
174
175/*
176vi:ts=4:et
177*/
178
diff --git a/tools/ucl/src/n2b_ds.c b/tools/ucl/src/n2b_ds.c
new file mode 100644
index 0000000000..1a52b46218
--- /dev/null
+++ b/tools/ucl/src/n2b_ds.c
@@ -0,0 +1,40 @@
1/* n2b_ds.c -- implementation of the NRV2B decompression algorithm
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#define SAFE
30#define ucl_nrv2b_decompress_8 ucl_nrv2b_decompress_safe_8
31#define ucl_nrv2b_decompress_le16 ucl_nrv2b_decompress_safe_le16
32#define ucl_nrv2b_decompress_le32 ucl_nrv2b_decompress_safe_le32
33#include "n2b_d.c"
34#undef SAFE
35
36
37/*
38vi:ts=4:et
39*/
40
diff --git a/tools/ucl/src/n2b_to.c b/tools/ucl/src/n2b_to.c
new file mode 100644
index 0000000000..29a6d65820
--- /dev/null
+++ b/tools/ucl/src/n2b_to.c
@@ -0,0 +1,79 @@
1/* n2b_to.c -- implementation of the NRV2B test overlap algorithm
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
30/***********************************************************************
31// entries for the different bit-buffer sizes
32************************************************************************/
33
34#include <ucl/ucl.h>
35#include "ucl_conf.h"
36#include "getbit.h"
37
38#define SAFE
39#define TEST_OVERLAP
40
41
42UCL_PUBLIC(int)
43ucl_nrv2b_test_overlap_8 ( const ucl_bytep src, ucl_uint src_off,
44 ucl_uint src_len, ucl_uintp dst_len,
45 ucl_voidp wrkmem )
46{
47#define getbit(bb) getbit_8(bb,src,ilen)
48#include "n2b_d.c"
49#undef getbit
50}
51
52
53UCL_PUBLIC(int)
54ucl_nrv2b_test_overlap_le16 ( const ucl_bytep src, ucl_uint src_off,
55 ucl_uint src_len, ucl_uintp dst_len,
56 ucl_voidp wrkmem )
57{
58#define getbit(bb) getbit_le16(bb,src,ilen)
59#include "n2b_d.c"
60#undef getbit
61}
62
63
64UCL_PUBLIC(int)
65ucl_nrv2b_test_overlap_le32 ( const ucl_bytep src, ucl_uint src_off,
66 ucl_uint src_len, ucl_uintp dst_len,
67 ucl_voidp wrkmem )
68{
69 unsigned bc = 0;
70#define getbit(bb) getbit_le32(bb,bc,src,ilen)
71#include "n2b_d.c"
72#undef getbit
73}
74
75
76/*
77vi:ts=4:et
78*/
79
diff --git a/tools/ucl/src/n2d_99.c b/tools/ucl/src/n2d_99.c
new file mode 100644
index 0000000000..c4d501a26b
--- /dev/null
+++ b/tools/ucl/src/n2d_99.c
@@ -0,0 +1,38 @@
1/* n2d_99.c -- implementation of the NRV2D-99 compression algorithm
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
30#define NRV2D
31#include "n2_99.ch"
32#undef NRV2D
33
34
35/*
36vi:ts=4:et
37*/
38
diff --git a/tools/ucl/src/n2d_d.c b/tools/ucl/src/n2d_d.c
new file mode 100644
index 0000000000..42409506c3
--- /dev/null
+++ b/tools/ucl/src/n2d_d.c
@@ -0,0 +1,183 @@
1/* n2d_d.c -- implementation of the NRV2D decompression algorithm
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/***********************************************************************
30// actual implementation used by a recursive #include
31************************************************************************/
32
33#ifdef getbit
34
35#ifdef SAFE
36#define fail(x,r) if (x) { *dst_len = olen; return r; }
37#else
38#define fail(x,r)
39#endif
40
41{
42 ucl_uint32 bb = 0;
43#ifdef TEST_OVERLAP
44 ucl_uint ilen = src_off, olen = 0, last_m_off = 1;
45#else
46 ucl_uint ilen = 0, olen = 0, last_m_off = 1;
47#endif
48#ifdef SAFE
49 const ucl_uint oend = *dst_len;
50#endif
51 UCL_UNUSED(wrkmem);
52
53#ifdef TEST_OVERLAP
54 src_len += src_off;
55 fail(oend >= src_len, UCL_E_OVERLAP_OVERRUN);
56#endif
57
58 for (;;)
59 {
60 ucl_uint m_off, m_len;
61
62 while (getbit(bb))
63 {
64 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
65 fail(olen >= oend, UCL_E_OUTPUT_OVERRUN);
66#ifdef TEST_OVERLAP
67 fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
68 olen++; ilen++;
69#else
70 dst[olen++] = src[ilen++];
71#endif
72 }
73 m_off = 1;
74 for (;;)
75 {
76 m_off = m_off*2 + getbit(bb);
77 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
78 fail(m_off > UCL_UINT32_C(0xffffff) + 3, UCL_E_LOOKBEHIND_OVERRUN);
79 if (getbit(bb)) break;
80 m_off = (m_off-1)*2 + getbit(bb);
81 }
82 if (m_off == 2)
83 {
84 m_off = last_m_off;
85 m_len = getbit(bb);
86 }
87 else
88 {
89 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
90 m_off = (m_off-3)*256 + src[ilen++];
91 if (m_off == UCL_UINT32_C(0xffffffff))
92 break;
93 m_len = (m_off ^ UCL_UINT32_C(0xffffffff)) & 1;
94 m_off >>= 1;
95 last_m_off = ++m_off;
96 }
97 m_len = m_len*2 + getbit(bb);
98 if (m_len == 0)
99 {
100 m_len++;
101 do {
102 m_len = m_len*2 + getbit(bb);
103 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
104 fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN);
105 } while (!getbit(bb));
106 m_len += 2;
107 }
108 m_len += (m_off > 0x500);
109 fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN);
110 fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN);
111#ifdef TEST_OVERLAP
112 olen += m_len + 1;
113 fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
114#else
115 {
116 const ucl_byte *m_pos;
117 m_pos = dst + olen - m_off;
118 dst[olen++] = *m_pos++;
119 do dst[olen++] = *m_pos++; while (--m_len > 0);
120 }
121#endif
122 }
123 *dst_len = olen;
124 return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN);
125}
126
127#undef fail
128
129#endif /* getbit */
130
131
132/***********************************************************************
133// decompressor entries for the different bit-buffer sizes
134************************************************************************/
135
136#ifndef getbit
137
138#include <ucl/ucl.h>
139#include "ucl_conf.h"
140#include "getbit.h"
141
142
143UCL_PUBLIC(int)
144ucl_nrv2d_decompress_8 ( const ucl_bytep src, ucl_uint src_len,
145 ucl_bytep dst, ucl_uintp dst_len,
146 ucl_voidp wrkmem )
147{
148#define getbit(bb) getbit_8(bb,src,ilen)
149#include "n2d_d.c"
150#undef getbit
151}
152
153
154UCL_PUBLIC(int)
155ucl_nrv2d_decompress_le16 ( const ucl_bytep src, ucl_uint src_len,
156 ucl_bytep dst, ucl_uintp dst_len,
157 ucl_voidp wrkmem )
158{
159#define getbit(bb) getbit_le16(bb,src,ilen)
160#include "n2d_d.c"
161#undef getbit
162}
163
164
165UCL_PUBLIC(int)
166ucl_nrv2d_decompress_le32 ( const ucl_bytep src, ucl_uint src_len,
167 ucl_bytep dst, ucl_uintp dst_len,
168 ucl_voidp wrkmem )
169{
170 unsigned bc = 0;
171#define getbit(bb) getbit_le32(bb,bc,src,ilen)
172#include "n2d_d.c"
173#undef getbit
174}
175
176
177#endif /* !getbit */
178
179
180/*
181vi:ts=4:et
182*/
183
diff --git a/tools/ucl/src/n2d_ds.c b/tools/ucl/src/n2d_ds.c
new file mode 100644
index 0000000000..81b15c02a0
--- /dev/null
+++ b/tools/ucl/src/n2d_ds.c
@@ -0,0 +1,40 @@
1/* n2d_ds.c -- implementation of the NRV2D decompression algorithm
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#define SAFE
30#define ucl_nrv2d_decompress_8 ucl_nrv2d_decompress_safe_8
31#define ucl_nrv2d_decompress_le16 ucl_nrv2d_decompress_safe_le16
32#define ucl_nrv2d_decompress_le32 ucl_nrv2d_decompress_safe_le32
33#include "n2d_d.c"
34#undef SAFE
35
36
37/*
38vi:ts=4:et
39*/
40
diff --git a/tools/ucl/src/n2d_to.c b/tools/ucl/src/n2d_to.c
new file mode 100644
index 0000000000..58b43fa57a
--- /dev/null
+++ b/tools/ucl/src/n2d_to.c
@@ -0,0 +1,79 @@
1/* n2d_to.c -- implementation of the NRV2D test overlap algorithm
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
30/***********************************************************************
31// entries for the different bit-buffer sizes
32************************************************************************/
33
34#include <ucl/ucl.h>
35#include "ucl_conf.h"
36#include "getbit.h"
37
38#define SAFE
39#define TEST_OVERLAP
40
41
42UCL_PUBLIC(int)
43ucl_nrv2d_test_overlap_8 ( const ucl_bytep src, ucl_uint src_off,
44 ucl_uint src_len, ucl_uintp dst_len,
45 ucl_voidp wrkmem )
46{
47#define getbit(bb) getbit_8(bb,src,ilen)
48#include "n2d_d.c"
49#undef getbit
50}
51
52
53UCL_PUBLIC(int)
54ucl_nrv2d_test_overlap_le16 ( const ucl_bytep src, ucl_uint src_off,
55 ucl_uint src_len, ucl_uintp dst_len,
56 ucl_voidp wrkmem )
57{
58#define getbit(bb) getbit_le16(bb,src,ilen)
59#include "n2d_d.c"
60#undef getbit
61}
62
63
64UCL_PUBLIC(int)
65ucl_nrv2d_test_overlap_le32 ( const ucl_bytep src, ucl_uint src_off,
66 ucl_uint src_len, ucl_uintp dst_len,
67 ucl_voidp wrkmem )
68{
69 unsigned bc = 0;
70#define getbit(bb) getbit_le32(bb,bc,src,ilen)
71#include "n2d_d.c"
72#undef getbit
73}
74
75
76/*
77vi:ts=4:et
78*/
79
diff --git a/tools/ucl/src/n2e_99.c b/tools/ucl/src/n2e_99.c
new file mode 100644
index 0000000000..122c4dc036
--- /dev/null
+++ b/tools/ucl/src/n2e_99.c
@@ -0,0 +1,38 @@
1/* n2e_99.c -- implementation of the NRV2E-99 compression algorithm
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
30#define NRV2E
31#include "n2_99.ch"
32#undef NRV2E
33
34
35/*
36vi:ts=4:et
37*/
38
diff --git a/tools/ucl/src/n2e_d.c b/tools/ucl/src/n2e_d.c
new file mode 100644
index 0000000000..6c4586d75c
--- /dev/null
+++ b/tools/ucl/src/n2e_d.c
@@ -0,0 +1,186 @@
1/* n2e_d.c -- implementation of the NRV2E decompression algorithm
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/***********************************************************************
30// actual implementation used by a recursive #include
31************************************************************************/
32
33#ifdef getbit
34
35#ifdef SAFE
36#define fail(x,r) if (x) { *dst_len = olen; return r; }
37#else
38#define fail(x,r)
39#endif
40
41{
42 ucl_uint32 bb = 0;
43#ifdef TEST_OVERLAP
44 ucl_uint ilen = src_off, olen = 0, last_m_off = 1;
45#else
46 ucl_uint ilen = 0, olen = 0, last_m_off = 1;
47#endif
48#ifdef SAFE
49 const ucl_uint oend = *dst_len;
50#endif
51 UCL_UNUSED(wrkmem);
52
53#ifdef TEST_OVERLAP
54 src_len += src_off;
55 fail(oend >= src_len, UCL_E_OVERLAP_OVERRUN);
56#endif
57
58 for (;;)
59 {
60 ucl_uint m_off, m_len;
61
62 while (getbit(bb))
63 {
64 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
65 fail(olen >= oend, UCL_E_OUTPUT_OVERRUN);
66#ifdef TEST_OVERLAP
67 fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
68 olen++; ilen++;
69#else
70 dst[olen++] = src[ilen++];
71#endif
72 }
73 m_off = 1;
74 for (;;)
75 {
76 m_off = m_off*2 + getbit(bb);
77 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
78 fail(m_off > UCL_UINT32_C(0xffffff) + 3, UCL_E_LOOKBEHIND_OVERRUN);
79 if (getbit(bb)) break;
80 m_off = (m_off-1)*2 + getbit(bb);
81 }
82 if (m_off == 2)
83 {
84 m_off = last_m_off;
85 m_len = getbit(bb);
86 }
87 else
88 {
89 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
90 m_off = (m_off-3)*256 + src[ilen++];
91 if (m_off == UCL_UINT32_C(0xffffffff))
92 break;
93 m_len = (m_off ^ UCL_UINT32_C(0xffffffff)) & 1;
94 m_off >>= 1;
95 last_m_off = ++m_off;
96 }
97 if (m_len)
98 m_len = 1 + getbit(bb);
99 else if (getbit(bb))
100 m_len = 3 + getbit(bb);
101 else
102 {
103 m_len++;
104 do {
105 m_len = m_len*2 + getbit(bb);
106 fail(ilen >= src_len, UCL_E_INPUT_OVERRUN);
107 fail(m_len >= oend, UCL_E_OUTPUT_OVERRUN);
108 } while (!getbit(bb));
109 m_len += 3;
110 }
111 m_len += (m_off > 0x500);
112 fail(olen + m_len > oend, UCL_E_OUTPUT_OVERRUN);
113 fail(m_off > olen, UCL_E_LOOKBEHIND_OVERRUN);
114#ifdef TEST_OVERLAP
115 olen += m_len + 1;
116 fail(olen > ilen, UCL_E_OVERLAP_OVERRUN);
117#else
118 {
119 const ucl_byte *m_pos;
120 m_pos = dst + olen - m_off;
121 dst[olen++] = *m_pos++;
122 do dst[olen++] = *m_pos++; while (--m_len > 0);
123 }
124#endif
125 }
126 *dst_len = olen;
127 return ilen == src_len ? UCL_E_OK : (ilen < src_len ? UCL_E_INPUT_NOT_CONSUMED : UCL_E_INPUT_OVERRUN);
128}
129
130#undef fail
131
132#endif /* getbit */
133
134
135/***********************************************************************
136// decompressor entries for the different bit-buffer sizes
137************************************************************************/
138
139#ifndef getbit
140
141#include <ucl/ucl.h>
142#include "ucl_conf.h"
143#include "getbit.h"
144
145
146UCL_PUBLIC(int)
147ucl_nrv2e_decompress_8 ( const ucl_byte *src, ucl_uint src_len,
148 ucl_byte *dst, ucl_uintp dst_len,
149 ucl_voidp wrkmem )
150{
151#define getbit(bb) getbit_8(bb,src,ilen)
152#include "n2e_d.c"
153#undef getbit
154}
155
156
157UCL_PUBLIC(int)
158ucl_nrv2e_decompress_le16 ( const ucl_bytep src, ucl_uint src_len,
159 ucl_bytep dst, ucl_uintp dst_len,
160 ucl_voidp wrkmem )
161{
162#define getbit(bb) getbit_le16(bb,src,ilen)
163#include "n2e_d.c"
164#undef getbit
165}
166
167
168UCL_PUBLIC(int)
169ucl_nrv2e_decompress_le32 ( const ucl_bytep src, ucl_uint src_len,
170 ucl_bytep dst, ucl_uintp dst_len,
171 ucl_voidp wrkmem )
172{
173 unsigned bc = 0;
174#define getbit(bb) getbit_le32(bb,bc,src,ilen)
175#include "n2e_d.c"
176#undef getbit
177}
178
179
180#endif /* !getbit */
181
182
183/*
184vi:ts=4:et
185*/
186
diff --git a/tools/ucl/src/n2e_ds.c b/tools/ucl/src/n2e_ds.c
new file mode 100644
index 0000000000..4afe3899f0
--- /dev/null
+++ b/tools/ucl/src/n2e_ds.c
@@ -0,0 +1,40 @@
1/* n2e_ds.c -- implementation of the NRV2E decompression algorithm
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#define SAFE
30#define ucl_nrv2e_decompress_8 ucl_nrv2e_decompress_safe_8
31#define ucl_nrv2e_decompress_le16 ucl_nrv2e_decompress_safe_le16
32#define ucl_nrv2e_decompress_le32 ucl_nrv2e_decompress_safe_le32
33#include "n2e_d.c"
34#undef SAFE
35
36
37/*
38vi:ts=4:et
39*/
40
diff --git a/tools/ucl/src/n2e_to.c b/tools/ucl/src/n2e_to.c
new file mode 100644
index 0000000000..33bcd07b19
--- /dev/null
+++ b/tools/ucl/src/n2e_to.c
@@ -0,0 +1,79 @@
1/* n2e_to.c -- implementation of the NRV2E test overlap algorithm
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
30/***********************************************************************
31// entries for the different bit-buffer sizes
32************************************************************************/
33
34#include <ucl/ucl.h>
35#include "ucl_conf.h"
36#include "getbit.h"
37
38#define SAFE
39#define TEST_OVERLAP
40
41
42UCL_PUBLIC(int)
43ucl_nrv2e_test_overlap_8 ( const ucl_bytep src, ucl_uint src_off,
44 ucl_uint src_len, ucl_uintp dst_len,
45 ucl_voidp wrkmem )
46{
47#define getbit(bb) getbit_8(bb,src,ilen)
48#include "n2e_d.c"
49#undef getbit
50}
51
52
53UCL_PUBLIC(int)
54ucl_nrv2e_test_overlap_le16 ( const ucl_bytep src, ucl_uint src_off,
55 ucl_uint src_len, ucl_uintp dst_len,
56 ucl_voidp wrkmem )
57{
58#define getbit(bb) getbit_le16(bb,src,ilen)
59#include "n2e_d.c"
60#undef getbit
61}
62
63
64UCL_PUBLIC(int)
65ucl_nrv2e_test_overlap_le32 ( const ucl_bytep src, ucl_uint src_off,
66 ucl_uint src_len, ucl_uintp dst_len,
67 ucl_voidp wrkmem )
68{
69 unsigned bc = 0;
70#define getbit(bb) getbit_le32(bb,bc,src,ilen)
71#include "n2e_d.c"
72#undef getbit
73}
74
75
76/*
77vi:ts=4:et
78*/
79
diff --git a/tools/ucl/src/ucl_conf.h b/tools/ucl/src/ucl_conf.h
new file mode 100644
index 0000000000..37e81c6e03
--- /dev/null
+++ b/tools/ucl/src/ucl_conf.h
@@ -0,0 +1,359 @@
1/* ucl_conf.h -- main internal configuration file for the 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 */
26
27
28/* WARNING: this file should *not* be used by applications. It is
29 part of the implementation of the library and is subject
30 to change.
31 */
32
33
34#ifndef __UCL_CONF_H
35#define __UCL_CONF_H
36
37#if !defined(__UCL_IN_MINIUCL)
38# ifndef __UCLCONF_H
39# include <ucl/uclconf.h>
40# endif
41#endif
42
43
44/***********************************************************************
45// memory checkers
46************************************************************************/
47
48#if defined(__BOUNDS_CHECKING_ON)
49# include <unchecked.h>
50#else
51# define BOUNDS_CHECKING_OFF_DURING(stmt) stmt
52# define BOUNDS_CHECKING_OFF_IN_EXPR(expr) (expr)
53#endif
54
55
56/***********************************************************************
57// autoconf section
58************************************************************************/
59
60#if !defined(UCL_HAVE_CONFIG_H)
61# include <stddef.h> /* ptrdiff_t, size_t */
62# include <string.h> /* memcpy, memmove, memcmp, memset */
63# if !defined(NO_STDLIB_H)
64# include <stdlib.h>
65# endif
66# define HAVE_MEMCMP
67# define HAVE_MEMCPY
68# define HAVE_MEMMOVE
69# define HAVE_MEMSET
70#else
71# include <sys/types.h>
72# if defined(STDC_HEADERS)
73# include <string.h>
74# include <stdlib.h>
75# endif
76# if defined(HAVE_STDDEF_H)
77# include <stddef.h>
78# endif
79# if defined(HAVE_MEMORY_H)
80# include <memory.h>
81# endif
82#endif
83
84#if defined(__UCL_DOS16) || defined(__UCL_WIN16)
85# define HAVE_MALLOC_H
86# define HAVE_HALLOC
87#endif
88
89
90#undef NDEBUG
91#if !defined(UCL_DEBUG)
92# define NDEBUG
93#endif
94#if 1 || defined(UCL_DEBUG) || !defined(NDEBUG)
95# if !defined(NO_STDIO_H)
96# include <stdio.h>
97# endif
98#endif
99#include <assert.h>
100
101
102#if !defined(UCL_UNUSED)
103# define UCL_UNUSED(parm) (parm = parm)
104#endif
105
106
107#if !defined(__inline__) && !defined(__GNUC__)
108# if defined(__cplusplus)
109# define __inline__ inline
110# else
111# define __inline__ /* nothing */
112# endif
113#endif
114
115
116/***********************************************************************
117//
118************************************************************************/
119
120#if 1
121# define UCL_BYTE(x) ((unsigned char) (x))
122#else
123# define UCL_BYTE(x) ((unsigned char) ((x) & 0xff))
124#endif
125#if 0
126# define UCL_USHORT(x) ((unsigned short) (x))
127#else
128# define UCL_USHORT(x) ((unsigned short) ((x) & 0xffff))
129#endif
130
131#define UCL_MAX(a,b) ((a) >= (b) ? (a) : (b))
132#define UCL_MIN(a,b) ((a) <= (b) ? (a) : (b))
133#define UCL_MAX3(a,b,c) ((a) >= (b) ? UCL_MAX(a,c) : UCL_MAX(b,c))
134#define UCL_MIN3(a,b,c) ((a) <= (b) ? UCL_MIN(a,c) : UCL_MIN(b,c))
135
136#define ucl_sizeof(type) ((ucl_uint) (sizeof(type)))
137
138#define UCL_HIGH(array) ((ucl_uint) (sizeof(array)/sizeof(*(array))))
139
140/* this always fits into 16 bits */
141#define UCL_SIZE(bits) (1u << (bits))
142#define UCL_MASK(bits) (UCL_SIZE(bits) - 1)
143
144#define UCL_LSIZE(bits) (1ul << (bits))
145#define UCL_LMASK(bits) (UCL_LSIZE(bits) - 1)
146
147#define UCL_USIZE(bits) ((ucl_uint) 1 << (bits))
148#define UCL_UMASK(bits) (UCL_USIZE(bits) - 1)
149
150/* Maximum value of a signed/unsigned type.
151 Do not use casts, avoid overflows ! */
152#define UCL_STYPE_MAX(b) (((1l << (8*(b)-2)) - 1l) + (1l << (8*(b)-2)))
153#define UCL_UTYPE_MAX(b) (((1ul << (8*(b)-1)) - 1ul) + (1ul << (8*(b)-1)))
154
155
156/***********************************************************************
157//
158************************************************************************/
159
160#if !defined(SIZEOF_UNSIGNED)
161# if (UINT_MAX == 0xffff)
162# define SIZEOF_UNSIGNED 2
163# elif (UINT_MAX == UCL_0xffffffffL)
164# define SIZEOF_UNSIGNED 4
165# elif (UINT_MAX >= UCL_0xffffffffL)
166# define SIZEOF_UNSIGNED 8
167# else
168# error "SIZEOF_UNSIGNED"
169# endif
170#endif
171
172#if !defined(SIZEOF_UNSIGNED_LONG)
173# if (ULONG_MAX == UCL_0xffffffffL)
174# define SIZEOF_UNSIGNED_LONG 4
175# elif (ULONG_MAX >= UCL_0xffffffffL)
176# define SIZEOF_UNSIGNED_LONG 8
177# else
178# error "SIZEOF_UNSIGNED_LONG"
179# endif
180#endif
181
182
183#if !defined(SIZEOF_SIZE_T)
184# define SIZEOF_SIZE_T SIZEOF_UNSIGNED
185#endif
186#if !defined(SIZE_T_MAX)
187# define SIZE_T_MAX UCL_UTYPE_MAX(SIZEOF_SIZE_T)
188#endif
189
190
191/***********************************************************************
192// <string.h> section
193************************************************************************/
194
195#if defined(NO_MEMCMP)
196# undef HAVE_MEMCMP
197#endif
198
199#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCMP)
200# define ucl_memcmp memcmp
201#endif
202#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCPY)
203# define ucl_memcpy memcpy
204#endif
205#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMMOVE)
206# define ucl_memmove memmove
207#endif
208#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
209# define ucl_memset memset
210#endif
211
212#if !defined(HAVE_MEMCMP)
213# undef memcmp
214# define memcmp ucl_memcmp
215#endif
216#if !defined(HAVE_MEMCPY)
217# undef memcpy
218# define memcpy ucl_memcpy
219#endif
220#if !defined(HAVE_MEMMOVE)
221# undef memmove
222# define memmove ucl_memmove
223#endif
224#if !defined(HAVE_MEMSET)
225# undef memset
226# define memset ucl_memset
227#endif
228
229
230/***********************************************************************
231// compiler and architecture specific stuff
232************************************************************************/
233
234/* Some defines that indicate if memory can be accessed at unaligned
235 * memory addresses. You should also test that this is actually faster
236 * even if it is allowed by your system.
237 */
238
239#if 1 && defined(__UCL_i386) && (UINT_MAX == UCL_0xffffffffL)
240# if !defined(UCL_UNALIGNED_OK_2) && (USHRT_MAX == 0xffff)
241# define UCL_UNALIGNED_OK_2
242# endif
243# if !defined(UCL_UNALIGNED_OK_4) && (UCL_UINT32_MAX == UCL_0xffffffffL)
244# define UCL_UNALIGNED_OK_4
245# endif
246#endif
247
248#if defined(UCL_UNALIGNED_OK_2) || defined(UCL_UNALIGNED_OK_4)
249# if !defined(UCL_UNALIGNED_OK)
250# define UCL_UNALIGNED_OK
251# endif
252#endif
253
254#if defined(__UCL_NO_UNALIGNED)
255# undef UCL_UNALIGNED_OK
256# undef UCL_UNALIGNED_OK_2
257# undef UCL_UNALIGNED_OK_4
258#endif
259
260#if defined(UCL_UNALIGNED_OK_2) && (USHRT_MAX != 0xffff)
261# error "UCL_UNALIGNED_OK_2 must not be defined on this system"
262#endif
263#if defined(UCL_UNALIGNED_OK_4) && (UCL_UINT32_MAX != UCL_0xffffffffL)
264# error "UCL_UNALIGNED_OK_4 must not be defined on this system"
265#endif
266
267
268/* Many modern processors can transfer 32bit words much faster than
269 * bytes - this can significantly speed decompression.
270 */
271
272#if defined(__UCL_NO_ALIGNED)
273# undef UCL_ALIGNED_OK_4
274#endif
275
276#if defined(UCL_ALIGNED_OK_4) && (UCL_UINT32_MAX != UCL_0xffffffffL)
277# error "UCL_ALIGNED_OK_4 must not be defined on this system"
278#endif
279
280
281/* Definitions for byte order, according to significance of bytes, from low
282 * addresses to high addresses. The value is what you get by putting '4'
283 * in the most significant byte, '3' in the second most significant byte,
284 * '2' in the second least significant byte, and '1' in the least
285 * significant byte.
286 * The byte order is only needed if we use UCL_UNALIGNED_OK.
287 */
288
289#define UCL_LITTLE_ENDIAN 1234
290#define UCL_BIG_ENDIAN 4321
291#define UCL_PDP_ENDIAN 3412
292
293#if !defined(UCL_BYTE_ORDER)
294# if defined(MFX_BYTE_ORDER)
295# define UCL_BYTE_ORDER MFX_BYTE_ORDER
296# elif defined(__UCL_i386)
297# define UCL_BYTE_ORDER UCL_LITTLE_ENDIAN
298# elif defined(BYTE_ORDER)
299# define UCL_BYTE_ORDER BYTE_ORDER
300# elif defined(__BYTE_ORDER)
301# define UCL_BYTE_ORDER __BYTE_ORDER
302# endif
303#endif
304
305#if defined(UCL_BYTE_ORDER)
306# if (UCL_BYTE_ORDER != UCL_LITTLE_ENDIAN) && \
307 (UCL_BYTE_ORDER != UCL_BIG_ENDIAN)
308# error "invalid UCL_BYTE_ORDER"
309# endif
310#endif
311
312#if defined(UCL_UNALIGNED_OK) && !defined(UCL_BYTE_ORDER)
313# error "UCL_BYTE_ORDER is not defined"
314#endif
315
316
317/***********************************************************************
318// some globals
319************************************************************************/
320
321__UCL_EXTERN_C int __ucl_init_done;
322__UCL_EXTERN_C const ucl_byte __ucl_copyright[];
323UCL_EXTERN(const ucl_byte *) ucl_copyright(void);
324__UCL_EXTERN_C const ucl_uint32 _ucl_crc32_table[256];
325
326
327/***********************************************************************
328// ANSI C preprocessor macros
329************************************************************************/
330
331#define _UCL_STRINGIZE(x) #x
332#define _UCL_MEXPAND(x) _UCL_STRINGIZE(x)
333
334/* concatenate */
335#define _UCL_CONCAT2(a,b) a ## b
336#define _UCL_CONCAT3(a,b,c) a ## b ## c
337#define _UCL_CONCAT4(a,b,c,d) a ## b ## c ## d
338#define _UCL_CONCAT5(a,b,c,d,e) a ## b ## c ## d ## e
339
340/* expand and concatenate (by using one level of indirection) */
341#define _UCL_ECONCAT2(a,b) _UCL_CONCAT2(a,b)
342#define _UCL_ECONCAT3(a,b,c) _UCL_CONCAT3(a,b,c)
343#define _UCL_ECONCAT4(a,b,c,d) _UCL_CONCAT4(a,b,c,d)
344#define _UCL_ECONCAT5(a,b,c,d,e) _UCL_CONCAT5(a,b,c,d,e)
345
346
347/***********************************************************************
348//
349************************************************************************/
350
351#include "ucl_ptr.h"
352
353
354#endif /* already included */
355
356/*
357vi:ts=4:et
358*/
359
diff --git a/tools/ucl/src/ucl_crc.c b/tools/ucl/src/ucl_crc.c
new file mode 100644
index 0000000000..7608ecffb6
--- /dev/null
+++ b/tools/ucl/src/ucl_crc.c
@@ -0,0 +1,135 @@
1/* ucl_crc.c -- crc checksum for the 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// crc32 checksum
35// adapted from free code by Mark Adler <madler@alumni.caltech.edu>
36// see http://www.cdrom.com/pub/infozip/zlib/
37************************************************************************/
38
39const ucl_uint32 _ucl_crc32_table[256] = {
40 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
41 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
42 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
43 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
44 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
45 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
46 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
47 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
48 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
49 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
50 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
51 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
52 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
53 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
54 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
55 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
56 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
57 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
58 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
59 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
60 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
61 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
62 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
63 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
64 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
65 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
66 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
67 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
68 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
69 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
70 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
71 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
72 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
73 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
74 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
75 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
76 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
77 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
78 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
79 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
80 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
81 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
82 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
83 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
84 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
85 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
86 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
87 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
88 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
89 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
90 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
91 0x2d02ef8dL
92};
93
94
95#if 1
96#define UCL_DO1(buf,i) \
97 crc = _ucl_crc32_table[((int)crc ^ buf[i]) & 0xff] ^ (crc >> 8)
98#else
99#define UCL_DO1(buf,i) \
100 crc = _ucl_crc32_table[(unsigned char)((unsigned char)crc ^ buf[i])] ^ (crc >> 8)
101#endif
102#define UCL_DO2(buf,i) UCL_DO1(buf,i); UCL_DO1(buf,i+1);
103#define UCL_DO4(buf,i) UCL_DO2(buf,i); UCL_DO2(buf,i+2);
104#define UCL_DO8(buf,i) UCL_DO4(buf,i); UCL_DO4(buf,i+4);
105#define UCL_DO16(buf,i) UCL_DO8(buf,i); UCL_DO8(buf,i+8);
106
107
108UCL_PUBLIC(ucl_uint32)
109ucl_crc32(ucl_uint32 c, const ucl_bytep buf, ucl_uint len)
110{
111 ucl_uint32 crc = (c & UCL_UINT32_C(0xffffffff)) ^ UCL_UINT32_C(0xffffffff);
112
113 if (buf == NULL)
114 return 0;
115
116 if (len >= 16) do
117 {
118 UCL_DO16(buf,0);
119 buf += 16;
120 len -= 16;
121 } while (len >= 16);
122 if (len != 0) do
123 {
124 UCL_DO1(buf,0);
125 buf += 1;
126 len -= 1;
127 } while (len > 0);
128
129 return crc ^ UCL_UINT32_C(0xffffffff);
130}
131
132
133/*
134vi:ts=4:et
135*/
diff --git a/tools/ucl/src/ucl_dll.c b/tools/ucl/src/ucl_dll.c
new file mode 100644
index 0000000000..ec9df43e73
--- /dev/null
+++ b/tools/ucl/src/ucl_dll.c
@@ -0,0 +1,61 @@
1/* ucl_dll.c -- DLL initialization of 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
31
32/***********************************************************************
33// Windows 16 bit + Watcom C + DLL
34************************************************************************/
35
36#if defined(__UCL_WIN16) && defined(__WATCOMC__) && defined(__SW_BD)
37
38/* don't pull in <windows.h> - we don't need it */
39#if 0
40#include <windows.h>
41#endif
42
43#pragma off (unreferenced);
44#if 0 && defined(WINVER)
45BOOL FAR PASCAL LibMain ( HANDLE hInstance, WORD wDataSegment,
46 WORD wHeapSize, LPSTR lpszCmdLine )
47#else
48int __far __pascal LibMain ( int a, short b, short c, long d )
49#endif
50#pragma on (unreferenced);
51{
52 return 1;
53}
54
55#endif
56
57
58
59/*
60vi:ts=4:et
61*/
diff --git a/tools/ucl/src/ucl_init.c b/tools/ucl/src/ucl_init.c
new file mode 100644
index 0000000000..4798acae97
--- /dev/null
+++ b/tools/ucl/src/ucl_init.c
@@ -0,0 +1,500 @@
1/* ucl_init.c -- initialization of 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#include <stdio.h>
32
33
34#if 0
35# define IS_SIGNED(type) (((type) (1ul << (8 * sizeof(type) - 1))) < 0)
36# define IS_UNSIGNED(type) (((type) (1ul << (8 * sizeof(type) - 1))) > 0)
37#else
38# define IS_SIGNED(type) (((type) (-1)) < ((type) 0))
39# define IS_UNSIGNED(type) (((type) (-1)) > ((type) 0))
40#endif
41
42
43/***********************************************************************
44// Runtime check of the assumptions about the size of builtin types,
45// memory model, byte order and other low-level constructs.
46//
47// We are really paranoid here - UCL should either fail (or crash)
48// at startup or not at all.
49//
50// Because of inlining much of these functions evaluates to nothing.
51************************************************************************/
52
53static ucl_bool schedule_insns_bug(void); /* avoid inlining */
54static ucl_bool strength_reduce_bug(int *); /* avoid inlining */
55
56
57#if 0 || defined(UCL_DEBUG)
58static ucl_bool __ucl_assert_fail(const char *s, unsigned line)
59{
60#if defined(__palmos__)
61 printf("UCL assertion failed in line %u: '%s'\n",line,s);
62#else
63 fprintf(stderr,"UCL assertion failed in line %u: '%s'\n",line,s);
64#endif
65 return 0;
66}
67# define __ucl_assert(x) ((x) ? 1 : __ucl_assert_fail(#x,__LINE__))
68#else
69# define __ucl_assert(x) ((x) ? 1 : 0)
70#endif
71
72
73/***********************************************************************
74// The next two functions should get completely optimized out of existance.
75// Some assertions are redundant - but included for clarity.
76************************************************************************/
77
78static ucl_bool basic_integral_check(void)
79{
80 ucl_bool r = 1;
81 ucl_bool sanity;
82
83 /* paranoia */
84 r &= __ucl_assert(CHAR_BIT == 8);
85 r &= __ucl_assert(sizeof(char) == 1);
86 r &= __ucl_assert(sizeof(short) >= 2);
87 r &= __ucl_assert(sizeof(long) >= 4);
88 r &= __ucl_assert(sizeof(int) >= sizeof(short));
89 r &= __ucl_assert(sizeof(long) >= sizeof(int));
90
91 r &= __ucl_assert(sizeof(ucl_uint32) >= 4);
92 r &= __ucl_assert(sizeof(ucl_uint32) >= sizeof(unsigned));
93#if defined(__UCL_STRICT_16BIT)
94 r &= __ucl_assert(sizeof(ucl_uint) == 2);
95#else
96 r &= __ucl_assert(sizeof(ucl_uint) >= 4);
97 r &= __ucl_assert(sizeof(ucl_uint) >= sizeof(unsigned));
98#endif
99
100#if defined(SIZEOF_UNSIGNED)
101 r &= __ucl_assert(SIZEOF_UNSIGNED == sizeof(unsigned));
102#endif
103#if defined(SIZEOF_UNSIGNED_LONG)
104 r &= __ucl_assert(SIZEOF_UNSIGNED_LONG == sizeof(unsigned long));
105#endif
106#if defined(SIZEOF_UNSIGNED_SHORT)
107 r &= __ucl_assert(SIZEOF_UNSIGNED_SHORT == sizeof(unsigned short));
108#endif
109#if !defined(__UCL_IN_MINIUCL)
110#if defined(SIZEOF_SIZE_T)
111 r &= __ucl_assert(SIZEOF_SIZE_T == sizeof(size_t));
112#endif
113#endif
114
115 /* assert the signedness of our integral types */
116 sanity = IS_UNSIGNED(unsigned short) && IS_UNSIGNED(unsigned) &&
117 IS_UNSIGNED(unsigned long) &&
118 IS_SIGNED(short) && IS_SIGNED(int) && IS_SIGNED(long);
119 if (sanity)
120 {
121 r &= __ucl_assert(IS_UNSIGNED(ucl_uint32));
122 r &= __ucl_assert(IS_UNSIGNED(ucl_uint));
123 r &= __ucl_assert(IS_SIGNED(ucl_int32));
124 r &= __ucl_assert(IS_SIGNED(ucl_int));
125
126 r &= __ucl_assert(INT_MAX == UCL_STYPE_MAX(sizeof(int)));
127 r &= __ucl_assert(UINT_MAX == UCL_UTYPE_MAX(sizeof(unsigned)));
128 r &= __ucl_assert(LONG_MAX == UCL_STYPE_MAX(sizeof(long)));
129 r &= __ucl_assert(ULONG_MAX == UCL_UTYPE_MAX(sizeof(unsigned long)));
130 r &= __ucl_assert(SHRT_MAX == UCL_STYPE_MAX(sizeof(short)));
131 r &= __ucl_assert(USHRT_MAX == UCL_UTYPE_MAX(sizeof(unsigned short)));
132 r &= __ucl_assert(UCL_UINT32_MAX == UCL_UTYPE_MAX(sizeof(ucl_uint32)));
133 r &= __ucl_assert(UCL_UINT_MAX == UCL_UTYPE_MAX(sizeof(ucl_uint)));
134#if !defined(__UCL_IN_MINIUCL)
135 r &= __ucl_assert(SIZE_T_MAX == UCL_UTYPE_MAX(sizeof(size_t)));
136#endif
137 }
138
139 return r;
140}
141
142
143static ucl_bool basic_ptr_check(void)
144{
145 ucl_bool r = 1;
146 ucl_bool sanity;
147
148 r &= __ucl_assert(sizeof(char *) >= sizeof(int));
149 r &= __ucl_assert(sizeof(ucl_byte *) >= sizeof(char *));
150
151 r &= __ucl_assert(sizeof(ucl_voidp) == sizeof(ucl_byte *));
152 r &= __ucl_assert(sizeof(ucl_voidp) == sizeof(ucl_voidpp));
153 r &= __ucl_assert(sizeof(ucl_voidp) == sizeof(ucl_bytepp));
154 r &= __ucl_assert(sizeof(ucl_voidp) >= sizeof(ucl_uint));
155
156 r &= __ucl_assert(sizeof(ucl_ptr_t) == sizeof(ucl_voidp));
157 r &= __ucl_assert(sizeof(ucl_ptr_t) >= sizeof(ucl_uint));
158
159 r &= __ucl_assert(sizeof(ucl_ptrdiff_t) >= 4);
160 r &= __ucl_assert(sizeof(ucl_ptrdiff_t) >= sizeof(ptrdiff_t));
161
162#if defined(SIZEOF_CHAR_P)
163 r &= __ucl_assert(SIZEOF_CHAR_P == sizeof(char *));
164#endif
165#if defined(SIZEOF_PTRDIFF_T)
166 r &= __ucl_assert(SIZEOF_PTRDIFF_T == sizeof(ptrdiff_t));
167#endif
168
169 /* assert the signedness of our integral types */
170 sanity = IS_UNSIGNED(unsigned short) && IS_UNSIGNED(unsigned) &&
171 IS_UNSIGNED(unsigned long) &&
172 IS_SIGNED(short) && IS_SIGNED(int) && IS_SIGNED(long);
173 if (sanity)
174 {
175 r &= __ucl_assert(IS_UNSIGNED(ucl_ptr_t));
176 r &= __ucl_assert(IS_SIGNED(ucl_ptrdiff_t));
177 r &= __ucl_assert(IS_SIGNED(ucl_sptr_t));
178 }
179
180 return r;
181}
182
183
184/***********************************************************************
185//
186************************************************************************/
187
188static ucl_bool ptr_check(void)
189{
190 ucl_bool r = 1;
191 int i;
192 char _wrkmem[10 * sizeof(ucl_byte *) + sizeof(ucl_align_t)];
193 ucl_bytep wrkmem;
194 ucl_bytepp dict;
195 unsigned char x[4 * sizeof(ucl_align_t)];
196 long d;
197 ucl_align_t a;
198 ucl_align_t u;
199
200 for (i = 0; i < (int) sizeof(x); i++)
201 x[i] = UCL_BYTE(i);
202
203 wrkmem = UCL_PTR_ALIGN_UP((ucl_byte *)_wrkmem,sizeof(ucl_align_t));
204
205#if 0
206 dict = (ucl_bytepp) wrkmem;
207#else
208 /* Avoid a compiler warning on architectures that
209 * do not allow unaligned access. */
210 u.a_ucl_bytep = wrkmem; dict = u.a_ucl_bytepp;
211#endif
212
213 d = (long) ((const ucl_bytep) dict - (const ucl_bytep) _wrkmem);
214 r &= __ucl_assert(d >= 0);
215 r &= __ucl_assert(d < (long) sizeof(ucl_align_t));
216
217 memset(&a,0xff,sizeof(a));
218 r &= __ucl_assert(a.a_ushort == USHRT_MAX);
219 r &= __ucl_assert(a.a_uint == UINT_MAX);
220 r &= __ucl_assert(a.a_ulong == ULONG_MAX);
221 r &= __ucl_assert(a.a_ucl_uint == UCL_UINT_MAX);
222
223 /* sanity check of the memory model */
224 if (r == 1)
225 {
226 for (i = 0; i < 8; i++)
227 r &= __ucl_assert((const ucl_voidp) (&dict[i]) == (const ucl_voidp) (&wrkmem[i * sizeof(ucl_byte *)]));
228 }
229
230 /* check BZERO8_PTR and that NULL == 0 */
231 memset(&a,0,sizeof(a));
232 r &= __ucl_assert(a.a_char_p == NULL);
233 r &= __ucl_assert(a.a_ucl_bytep == NULL);
234 r &= __ucl_assert(NULL == (void*)0);
235 if (r == 1)
236 {
237 for (i = 0; i < 10; i++)
238 dict[i] = wrkmem;
239 BZERO8_PTR(dict+1,sizeof(dict[0]),8);
240 r &= __ucl_assert(dict[0] == wrkmem);
241 for (i = 1; i < 9; i++)
242 r &= __ucl_assert(dict[i] == NULL);
243 r &= __ucl_assert(dict[9] == wrkmem);
244 }
245
246 /* check that the pointer constructs work as expected */
247 if (r == 1)
248 {
249 unsigned k = 1;
250 const unsigned n = (unsigned) sizeof(ucl_uint32);
251 ucl_byte *p0;
252 ucl_byte *p1;
253
254 k += __ucl_align_gap(&x[k],n);
255 p0 = (ucl_bytep) &x[k];
256#if defined(PTR_LINEAR)
257 r &= __ucl_assert((PTR_LINEAR(p0) & (n-1)) == 0);
258#else
259 r &= __ucl_assert(n == 4);
260 r &= __ucl_assert(PTR_ALIGNED_4(p0));
261#endif
262
263 r &= __ucl_assert(k >= 1);
264 p1 = (ucl_bytep) &x[1];
265 r &= __ucl_assert(PTR_GE(p0,p1));
266
267 r &= __ucl_assert(k < 1+n);
268 p1 = (ucl_bytep) &x[1+n];
269 r &= __ucl_assert(PTR_LT(p0,p1));
270
271 /* now check that aligned memory access doesn't core dump */
272 if (r == 1)
273 {
274 ucl_uint32 v0, v1;
275#if 0
276 v0 = * (ucl_uint32 *) &x[k];
277 v1 = * (ucl_uint32 *) &x[k+n];
278#else
279 /* Avoid compiler warnings on architectures that
280 * do not allow unaligned access. */
281 u.a_uchar_p = &x[k];
282 v0 = *u.a_ucl_uint32_p;
283 u.a_uchar_p = &x[k+n];
284 v1 = *u.a_ucl_uint32_p;
285#endif
286 r &= __ucl_assert(v0 > 0);
287 r &= __ucl_assert(v1 > 0);
288 }
289 }
290
291 return r;
292}
293
294
295/***********************************************************************
296//
297************************************************************************/
298
299UCL_PUBLIC(int)
300_ucl_config_check(void)
301{
302 ucl_bool r = 1;
303 int i;
304 union {
305 ucl_uint32 a;
306 unsigned short b;
307 ucl_uint32 aa[4];
308 unsigned char x[4*sizeof(ucl_align_t)];
309 } u;
310
311#if 0
312 /* paranoia - the following is guaranteed by definition anyway */
313 r &= __ucl_assert((const void *)&u == (const void *)&u.a);
314 r &= __ucl_assert((const void *)&u == (const void *)&u.b);
315 r &= __ucl_assert((const void *)&u == (const void *)&u.x[0]);
316 r &= __ucl_assert((const void *)&u == (const void *)&u.aa[0]);
317#endif
318
319 r &= basic_integral_check();
320 r &= basic_ptr_check();
321 if (r != 1)
322 return UCL_E_ERROR;
323
324 u.a = 0; u.b = 0;
325 for (i = 0; i < (int) sizeof(u.x); i++)
326 u.x[i] = UCL_BYTE(i);
327
328#if 0
329 /* check if the compiler correctly casts signed to unsigned */
330 r &= __ucl_assert( (int) (unsigned char) ((char) -1) == 255);
331#endif
332
333 /* check UCL_BYTE_ORDER */
334#if defined(UCL_BYTE_ORDER)
335 if (r == 1)
336 {
337# if (UCL_BYTE_ORDER == UCL_LITTLE_ENDIAN)
338 ucl_uint32 a = (ucl_uint32) (u.a & UCL_UINT32_C(0xffffffff));
339 unsigned short b = (unsigned short) (u.b & 0xffff);
340 r &= __ucl_assert(a == UCL_UINT32_C(0x03020100));
341 r &= __ucl_assert(b == 0x0100);
342# elif (UCL_BYTE_ORDER == UCL_BIG_ENDIAN)
343 ucl_uint32 a = u.a >> (8 * sizeof(u.a) - 32);
344 unsigned short b = u.b >> (8 * sizeof(u.b) - 16);
345 r &= __ucl_assert(a == UCL_UINT32_C(0x00010203));
346 r &= __ucl_assert(b == 0x0001);
347# else
348# error "invalid UCL_BYTE_ORDER"
349# endif
350 }
351#endif
352
353 /* check that unaligned memory access works as expected */
354#if defined(UCL_UNALIGNED_OK_2)
355 r &= __ucl_assert(sizeof(short) == 2);
356 if (r == 1)
357 {
358 unsigned short b[4];
359
360 for (i = 0; i < 4; i++)
361 b[i] = * (const unsigned short *) &u.x[i];
362
363# if (UCL_BYTE_ORDER == UCL_LITTLE_ENDIAN)
364 r &= __ucl_assert(b[0] == 0x0100);
365 r &= __ucl_assert(b[1] == 0x0201);
366 r &= __ucl_assert(b[2] == 0x0302);
367 r &= __ucl_assert(b[3] == 0x0403);
368# elif (UCL_BYTE_ORDER == UCL_BIG_ENDIAN)
369 r &= __ucl_assert(b[0] == 0x0001);
370 r &= __ucl_assert(b[1] == 0x0102);
371 r &= __ucl_assert(b[2] == 0x0203);
372 r &= __ucl_assert(b[3] == 0x0304);
373# endif
374 }
375#endif
376
377#if defined(UCL_UNALIGNED_OK_4)
378 r &= __ucl_assert(sizeof(ucl_uint32) == 4);
379 if (r == 1)
380 {
381 ucl_uint32 a[4];
382
383 for (i = 0; i < 4; i++)
384 a[i] = * (const ucl_uint32 *) &u.x[i];
385
386# if (UCL_BYTE_ORDER == UCL_LITTLE_ENDIAN)
387 r &= __ucl_assert(a[0] == UCL_UINT32_C(0x03020100));
388 r &= __ucl_assert(a[1] == UCL_UINT32_C(0x04030201));
389 r &= __ucl_assert(a[2] == UCL_UINT32_C(0x05040302));
390 r &= __ucl_assert(a[3] == UCL_UINT32_C(0x06050403));
391# elif (UCL_BYTE_ORDER == UCL_BIG_ENDIAN)
392 r &= __ucl_assert(a[0] == UCL_UINT32_C(0x00010203));
393 r &= __ucl_assert(a[1] == UCL_UINT32_C(0x01020304));
394 r &= __ucl_assert(a[2] == UCL_UINT32_C(0x02030405));
395 r &= __ucl_assert(a[3] == UCL_UINT32_C(0x03040506));
396# endif
397 }
398#endif
399
400#if defined(UCL_ALIGNED_OK_4)
401 r &= __ucl_assert(sizeof(ucl_uint32) == 4);
402#endif
403
404 /* check the ucl_adler32() function */
405 if (r == 1)
406 {
407 ucl_uint32 adler;
408 adler = ucl_adler32(0, NULL, 0);
409 adler = ucl_adler32(adler, ucl_copyright(), 186);
410 r &= __ucl_assert(adler == UCL_UINT32_C(0x47fb39fc));
411 }
412
413 /* check for the gcc schedule-insns optimization bug */
414 if (r == 1)
415 {
416 r &= __ucl_assert(!schedule_insns_bug());
417 }
418
419 /* check for the gcc strength-reduce optimization bug */
420 if (r == 1)
421 {
422 static int x[3];
423 static unsigned xn = 3;
424 register unsigned j;
425
426 for (j = 0; j < xn; j++)
427 x[j] = (int)j - 3;
428 r &= __ucl_assert(!strength_reduce_bug(x));
429 }
430
431 /* now for the low-level pointer checks */
432 if (r == 1)
433 {
434 r &= ptr_check();
435 }
436
437 return r == 1 ? UCL_E_OK : UCL_E_ERROR;
438}
439
440
441static ucl_bool schedule_insns_bug(void)
442{
443#if defined(__UCL_CHECKER)
444 /* for some reason checker complains about uninitialized memory access */
445 return 0;
446#else
447 const int clone[] = {1, 2, 0};
448 const int *q;
449 q = clone;
450 return (*q) ? 0 : 1;
451#endif
452}
453
454
455static ucl_bool strength_reduce_bug(int *x)
456{
457 return x[0] != -3 || x[1] != -2 || x[2] != -1;
458}
459
460
461/***********************************************************************
462//
463************************************************************************/
464
465int __ucl_init_done = 0;
466
467UCL_PUBLIC(int)
468__ucl_init2(ucl_uint32 v, int s1, int s2, int s3, int s4, int s5,
469 int s6, int s7, int s8, int s9)
470{
471 int r;
472
473 __ucl_init_done = 1;
474
475 if (v == 0)
476 return UCL_E_ERROR;
477
478 r = (s1 == -1 || s1 == (int) sizeof(short)) &&
479 (s2 == -1 || s2 == (int) sizeof(int)) &&
480 (s3 == -1 || s3 == (int) sizeof(long)) &&
481 (s4 == -1 || s4 == (int) sizeof(ucl_uint32)) &&
482 (s5 == -1 || s5 == (int) sizeof(ucl_uint)) &&
483 (s6 == -1 || s6 > 0) &&
484 (s7 == -1 || s7 == (int) sizeof(char *)) &&
485 (s8 == -1 || s8 == (int) sizeof(ucl_voidp)) &&
486 (s9 == -1 || s9 == (int) sizeof(ucl_compress_t));
487 if (!r)
488 return UCL_E_ERROR;
489
490 r = _ucl_config_check();
491 if (r != UCL_E_OK)
492 return r;
493
494 return r;
495}
496
497
498/*
499vi:ts=4:et
500*/
diff --git a/tools/ucl/src/ucl_mchw.ch b/tools/ucl/src/ucl_mchw.ch
new file mode 100755
index 0000000000..f2bcc9506f
--- /dev/null
+++ b/tools/ucl/src/ucl_mchw.ch
@@ -0,0 +1,313 @@
1/* ucl_mchw.ch -- matching functions using a window
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 */
26
27
28
29
30/***********************************************************************
31//
32************************************************************************/
33
34typedef struct
35{
36 int init;
37
38 ucl_uint look; /* bytes in lookahead buffer */
39
40 ucl_uint m_len;
41 ucl_uint m_off;
42
43 ucl_uint last_m_len;
44 ucl_uint last_m_off;
45
46 const ucl_byte *bp;
47 const ucl_byte *ip;
48 const ucl_byte *in;
49 const ucl_byte *in_end;
50 ucl_byte *out;
51
52 ucl_uint32 bb_b;
53 unsigned bb_k;
54 unsigned bb_c_endian;
55 unsigned bb_c_s;
56 unsigned bb_c_s8;
57 ucl_byte *bb_p;
58 ucl_byte *bb_op;
59
60 struct ucl_compress_config_t conf;
61 ucl_uintp result;
62
63 ucl_progress_callback_p cb;
64
65 ucl_uint textsize; /* text size counter */
66 ucl_uint codesize; /* code size counter */
67 ucl_uint printcount; /* counter for reporting progress every 1K bytes */
68
69 /* some stats */
70 unsigned long lit_bytes;
71 unsigned long match_bytes;
72 unsigned long rep_bytes;
73 unsigned long lazy;
74}
75UCL_COMPRESS_T;
76
77
78
79#if defined(__PUREC__) && defined(__UCL_TOS16)
80/* the cast is needed to work around a bug in Pure C */
81#define getbyte(c) ((c).ip < (c).in_end ? (unsigned) *((c).ip)++ : (-1))
82#else
83#define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
84#endif
85
86#include "ucl_swd.ch"
87
88
89
90/***********************************************************************
91//
92************************************************************************/
93
94static int
95init_match ( UCL_COMPRESS_T *c, ucl_swd_t *s,
96 const ucl_byte *dict, ucl_uint dict_len,
97 ucl_uint32 flags )
98{
99 int r;
100
101 assert(!c->init);
102 c->init = 1;
103
104 s->c = c;
105
106 c->last_m_len = c->last_m_off = 0;
107
108 c->textsize = c->codesize = c->printcount = 0;
109 c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
110 c->lazy = 0;
111
112 r = swd_init(s,dict,dict_len);
113 if (r != UCL_E_OK)
114 {
115 swd_exit(s);
116 return r;
117 }
118
119 s->use_best_off = (flags & 1) ? 1 : 0;
120 return UCL_E_OK;
121}
122
123
124/***********************************************************************
125//
126************************************************************************/
127
128static int
129find_match ( UCL_COMPRESS_T *c, ucl_swd_t *s,
130 ucl_uint this_len, ucl_uint skip )
131{
132 assert(c->init);
133
134 if (skip > 0)
135 {
136 assert(this_len >= skip);
137 swd_accept(s, this_len - skip);
138 c->textsize += this_len - skip + 1;
139 }
140 else
141 {
142 assert(this_len <= 1);
143 c->textsize += this_len - skip;
144 }
145
146 s->m_len = THRESHOLD;
147#ifdef SWD_BEST_OFF
148 if (s->use_best_off)
149 memset(s->best_pos,0,sizeof(s->best_pos));
150#endif
151 swd_findbest(s);
152 c->m_len = s->m_len;
153#if defined(__UCL_CHECKER)
154 /* s->m_off may be uninitialized if we didn't find a match,
155 * but then its value will never be used.
156 */
157 c->m_off = (s->m_len == THRESHOLD) ? 0 : s->m_off;
158#else
159 c->m_off = s->m_off;
160#endif
161
162 swd_getbyte(s);
163
164 if (s->b_char < 0)
165 {
166 c->look = 0;
167 c->m_len = 0;
168 swd_exit(s);
169 }
170 else
171 {
172 c->look = s->look + 1;
173 }
174 c->bp = c->ip - c->look;
175
176#if 0
177 /* brute force match search */
178 if (c->m_len > THRESHOLD && c->m_len + 1 <= c->look)
179 {
180 const ucl_byte *ip = c->bp;
181 const ucl_byte *m = c->bp - c->m_off;
182 const ucl_byte *in = c->in;
183
184 if (ip - in > N)
185 in = ip - N;
186 for (;;)
187 {
188 while (*in != *ip)
189 in++;
190 if (in == ip)
191 break;
192 if (in != m)
193 if (memcmp(in,ip,c->m_len+1) == 0)
194 printf("%p %p %p %5d\n",in,ip,m,c->m_len);
195 in++;
196 }
197 }
198#endif
199
200 if (c->cb && c->textsize > c->printcount)
201 {
202 (*c->cb->callback)(c->textsize,c->codesize,3,c->cb->user);
203 c->printcount += 1024;
204 }
205
206 return UCL_E_OK;
207}
208
209
210/***********************************************************************
211// bit buffer
212************************************************************************/
213
214static int bbConfig(UCL_COMPRESS_T *c, int endian, int bitsize)
215{
216 if (endian != -1)
217 {
218 if (endian != 0)
219 return UCL_E_ERROR;
220 c->bb_c_endian = endian;
221 }
222 if (bitsize != -1)
223 {
224 if (bitsize != 8 && bitsize != 16 && bitsize != 32)
225 return UCL_E_ERROR;
226 c->bb_c_s = bitsize;
227 c->bb_c_s8 = bitsize / 8;
228 }
229 c->bb_b = 0; c->bb_k = 0;
230 c->bb_p = NULL;
231 c->bb_op = NULL;
232 return UCL_E_OK;
233}
234
235
236static void bbWriteBits(UCL_COMPRESS_T *c)
237{
238 ucl_byte *p = c->bb_p;
239 ucl_uint32 b = c->bb_b;
240
241 p[0] = UCL_BYTE(b >> 0);
242 if (c->bb_c_s >= 16)
243 {
244 p[1] = UCL_BYTE(b >> 8);
245 if (c->bb_c_s == 32)
246 {
247 p[2] = UCL_BYTE(b >> 16);
248 p[3] = UCL_BYTE(b >> 24);
249 }
250 }
251}
252
253
254static void bbPutBit(UCL_COMPRESS_T *c, unsigned bit)
255{
256 assert(bit == 0 || bit == 1);
257 assert(c->bb_k <= c->bb_c_s);
258
259 if (c->bb_k < c->bb_c_s)
260 {
261 if (c->bb_k == 0)
262 {
263 assert(c->bb_p == NULL);
264 c->bb_p = c->bb_op;
265 c->bb_op += c->bb_c_s8;
266 }
267 assert(c->bb_p != NULL);
268 assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
269
270 c->bb_b = (c->bb_b << 1) + bit;
271 c->bb_k++;
272 }
273 else
274 {
275 assert(c->bb_p != NULL);
276 assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
277
278 bbWriteBits(c);
279 c->bb_p = c->bb_op;
280 c->bb_op += c->bb_c_s8;
281 c->bb_b = bit;
282 c->bb_k = 1;
283 }
284}
285
286
287static void bbPutByte(UCL_COMPRESS_T *c, unsigned b)
288{
289 /**printf("putbyte %p %p %x (%d)\n", op, bb_p, x, bb_k);*/
290 assert(c->bb_p == NULL || c->bb_p + c->bb_c_s8 <= c->bb_op);
291 *c->bb_op++ = UCL_BYTE(b);
292}
293
294
295static void bbFlushBits(UCL_COMPRESS_T *c, unsigned filler_bit)
296{
297 if (c->bb_k > 0)
298 {
299 assert(c->bb_k <= c->bb_c_s);
300 while (c->bb_k != c->bb_c_s)
301 bbPutBit(c, filler_bit);
302 bbWriteBits(c);
303 c->bb_k = 0;
304 }
305 c->bb_p = NULL;
306}
307
308
309
310/*
311vi:ts=4:et
312*/
313
diff --git a/tools/ucl/src/ucl_ptr.c b/tools/ucl/src/ucl_ptr.c
new file mode 100644
index 0000000000..b092d2440f
--- /dev/null
+++ b/tools/ucl/src/ucl_ptr.c
@@ -0,0 +1,81 @@
1/* ucl_ptr.c -- low-level pointer constructs
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
31
32/***********************************************************************
33//
34************************************************************************/
35
36UCL_PUBLIC(ucl_ptr_t)
37__ucl_ptr_linear(const ucl_voidp ptr)
38{
39 ucl_ptr_t p;
40
41#if defined(__UCL_DOS16) || defined(__UCL_WIN16)
42 p = (((ucl_ptr_t)(_FP_SEG(ptr))) << (16 - __UCL_HShift)) + (_FP_OFF(ptr));
43#else
44 p = PTR_LINEAR(ptr);
45#endif
46
47 return p;
48}
49
50
51/***********************************************************************
52//
53************************************************************************/
54
55UCL_PUBLIC(unsigned)
56__ucl_align_gap(const ucl_voidp ptr, ucl_uint size)
57{
58 ucl_ptr_t p, s, n;
59
60 assert(size > 0);
61
62 p = __ucl_ptr_linear(ptr);
63 s = (ucl_ptr_t) (size - 1);
64#if 0
65 assert((size & (size - 1)) == 0);
66 n = ((p + s) & ~s) - p;
67#else
68 n = (((p + s) / size) * size) - p;
69#endif
70
71 assert((long)n >= 0);
72 assert(n <= s);
73
74 return (unsigned)n;
75}
76
77
78
79/*
80vi:ts=4:et
81*/
diff --git a/tools/ucl/src/ucl_ptr.h b/tools/ucl/src/ucl_ptr.h
new file mode 100644
index 0000000000..86221b9ac1
--- /dev/null
+++ b/tools/ucl/src/ucl_ptr.h
@@ -0,0 +1,211 @@
1/* ucl_ptr.h -- low-level pointer constructs
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 */
26
27
28/* WARNING: this file should *not* be used by applications. It is
29 part of the implementation of the library and is subject
30 to change.
31 */
32
33
34#ifndef __UCL_PTR_H
35#define __UCL_PTR_H
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41
42/* This is the lowest part of the UCL library.
43 * It deals with pointer representations at bit level.
44 */
45
46
47/***********************************************************************
48// Includes
49************************************************************************/
50
51#if defined(__UCL_DOS16) || defined(__UCL_WIN16)
52# include <dos.h>
53# if 1 && defined(__WATCOMC__)
54# include <i86.h>
55 __UCL_EXTERN_C unsigned char _HShift;
56# define __UCL_HShift _HShift
57# elif 1 && defined(_MSC_VER)
58 __UCL_EXTERN_C unsigned short __near _AHSHIFT;
59# define __UCL_HShift ((unsigned) &_AHSHIFT)
60# elif defined(__UCL_WIN16)
61# define __UCL_HShift 3
62# else
63# define __UCL_HShift 12
64# endif
65# if !defined(_FP_SEG) && defined(FP_SEG)
66# define _FP_SEG FP_SEG
67# endif
68# if !defined(_FP_OFF) && defined(FP_OFF)
69# define _FP_OFF FP_OFF
70# endif
71#endif
72
73
74/***********************************************************************
75// Integral types
76************************************************************************/
77
78/* ptrdiff_t */
79#if !defined(ucl_ptrdiff_t)
80 #if (UINT_MAX >= UCL_0xffffffffL)
81 typedef ptrdiff_t ucl_ptrdiff_t;
82 #else
83 typedef long ucl_ptrdiff_t;
84 #endif
85#endif
86
87
88/* Unsigned type that has *exactly* the same number of bits as a ucl_voidp */
89#if !defined(__UCL_HAVE_PTR_T)
90# if defined(ucl_ptr_t)
91# define __UCL_HAVE_PTR_T
92# endif
93#endif
94#if !defined(__UCL_HAVE_PTR_T)
95# if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_LONG)
96# if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_LONG)
97 typedef unsigned long ucl_ptr_t;
98 typedef long ucl_sptr_t;
99# define __UCL_HAVE_PTR_T
100# endif
101# endif
102#endif
103#if !defined(__UCL_HAVE_PTR_T)
104# if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED)
105# if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED)
106 typedef unsigned int ucl_ptr_t;
107 typedef int ucl_sptr_t;
108# define __UCL_HAVE_PTR_T
109# endif
110# endif
111#endif
112#if !defined(__UCL_HAVE_PTR_T)
113# if defined(SIZEOF_CHAR_P) && defined(SIZEOF_UNSIGNED_SHORT)
114# if (SIZEOF_CHAR_P == SIZEOF_UNSIGNED_SHORT)
115 typedef unsigned short ucl_ptr_t;
116 typedef short ucl_sptr_t;
117# define __UCL_HAVE_PTR_T
118# endif
119# endif
120#endif
121#if !defined(__UCL_HAVE_PTR_T)
122# if defined(UCL_HAVE_CONFIG_H) || defined(SIZEOF_CHAR_P)
123# error "no suitable type for ucl_ptr_t"
124# else
125 typedef unsigned long ucl_ptr_t;
126 typedef long ucl_sptr_t;
127# define __UCL_HAVE_PTR_T
128# endif
129#endif
130
131
132/***********************************************************************
133//
134************************************************************************/
135
136/* Always use the safe (=integral) version for pointer-comparisions.
137 * The compiler should optimize away the additional casts anyway.
138 *
139 * Note that this only works if the representation and ordering
140 * of the pointer and the integral is the same (at bit level).
141 *
142 * Most 16-bit compilers have their own view about pointers -
143 * fortunately they don't care about comparing pointers
144 * that are pointing to Nirvana.
145 */
146
147#if defined(__UCL_DOS16) || defined(__UCL_WIN16)
148#define PTR(a) ((ucl_bytep) (a))
149/* only need the low bits of the pointer -> offset is ok */
150#define PTR_ALIGNED_4(a) ((_FP_OFF(a) & 3) == 0)
151#define PTR_ALIGNED2_4(a,b) (((_FP_OFF(a) | _FP_OFF(b)) & 3) == 0)
152#else
153#define PTR(a) ((ucl_ptr_t) (a))
154#define PTR_LINEAR(a) PTR(a)
155#define PTR_ALIGNED_4(a) ((PTR_LINEAR(a) & 3) == 0)
156#define PTR_ALIGNED_8(a) ((PTR_LINEAR(a) & 7) == 0)
157#define PTR_ALIGNED2_4(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 3) == 0)
158#define PTR_ALIGNED2_8(a,b) (((PTR_LINEAR(a) | PTR_LINEAR(b)) & 7) == 0)
159#endif
160
161#define PTR_LT(a,b) (PTR(a) < PTR(b))
162#define PTR_GE(a,b) (PTR(a) >= PTR(b))
163#define PTR_DIFF(a,b) ((ucl_ptrdiff_t) (PTR(a) - PTR(b)))
164
165
166UCL_EXTERN(ucl_ptr_t)
167__ucl_ptr_linear(const ucl_voidp ptr);
168
169
170typedef union
171{
172 char a_char;
173 unsigned char a_uchar;
174 short a_short;
175 unsigned short a_ushort;
176 int a_int;
177 unsigned int a_uint;
178 long a_long;
179 unsigned long a_ulong;
180 ucl_int a_ucl_int;
181 ucl_uint a_ucl_uint;
182 ucl_int32 a_ucl_int32;
183 ucl_uint32 a_ucl_uint32;
184 ptrdiff_t a_ptrdiff_t;
185 ucl_ptrdiff_t a_ucl_ptrdiff_t;
186 ucl_ptr_t a_ucl_ptr_t;
187 ucl_voidp a_ucl_voidp;
188 void * a_void_p;
189 ucl_bytep a_ucl_bytep;
190 ucl_bytepp a_ucl_bytepp;
191 ucl_uintp a_ucl_uintp;
192 ucl_uint * a_ucl_uint_p;
193 ucl_uint32p a_ucl_uint32p;
194 ucl_uint32 * a_ucl_uint32_p;
195 unsigned char * a_uchar_p;
196 char * a_char_p;
197}
198ucl_align_t;
199
200
201
202#ifdef __cplusplus
203} /* extern "C" */
204#endif
205
206#endif /* already included */
207
208/*
209vi:ts=4:et
210*/
211
diff --git a/tools/ucl/src/ucl_str.c b/tools/ucl/src/ucl_str.c
new file mode 100644
index 0000000000..5866fb6377
--- /dev/null
+++ b/tools/ucl/src/ucl_str.c
@@ -0,0 +1,133 @@
1/* ucl_str.c -- string functions for the 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
31#undef ucl_memcmp
32#undef ucl_memcpy
33#undef ucl_memmove
34#undef ucl_memset
35
36
37/***********************************************************************
38// slow but portable <string.h> stuff, only used in assertions
39************************************************************************/
40
41UCL_PUBLIC(int)
42ucl_memcmp(const ucl_voidp s1, const ucl_voidp s2, ucl_uint len)
43{
44#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCMP)
45 return memcmp(s1,s2,len);
46#else
47 const ucl_byte *p1 = (const ucl_byte *) s1;
48 const ucl_byte *p2 = (const ucl_byte *) s2;
49 int d;
50
51 if (len > 0) do
52 {
53 d = *p1 - *p2;
54 if (d != 0)
55 return d;
56 p1++;
57 p2++;
58 }
59 while (--len > 0);
60 return 0;
61#endif
62}
63
64
65UCL_PUBLIC(ucl_voidp)
66ucl_memcpy(ucl_voidp dest, const ucl_voidp src, ucl_uint len)
67{
68#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMCPY)
69 return memcpy(dest,src,len);
70#else
71 ucl_byte *p1 = (ucl_byte *) dest;
72 const ucl_byte *p2 = (const ucl_byte *) src;
73
74 if (len <= 0 || p1 == p2)
75 return dest;
76 do
77 *p1++ = *p2++;
78 while (--len > 0);
79 return dest;
80#endif
81}
82
83
84UCL_PUBLIC(ucl_voidp)
85ucl_memmove(ucl_voidp dest, const ucl_voidp src, ucl_uint len)
86{
87#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMMOVE)
88 return memmove(dest,src,len);
89#else
90 ucl_byte *p1 = (ucl_byte *) dest;
91 const ucl_byte *p2 = (const ucl_byte *) src;
92
93 if (len <= 0 || p1 == p2)
94 return dest;
95
96 if (p1 < p2)
97 {
98 do
99 *p1++ = *p2++;
100 while (--len > 0);
101 }
102 else
103 {
104 p1 += len;
105 p2 += len;
106 do
107 *--p1 = *--p2;
108 while (--len > 0);
109 }
110 return dest;
111#endif
112}
113
114
115UCL_PUBLIC(ucl_voidp)
116ucl_memset(ucl_voidp s, int c, ucl_uint len)
117{
118#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
119 return memset(s,c,len);
120#else
121 ucl_byte *p = (ucl_byte *) s;
122
123 if (len > 0) do
124 *p++ = UCL_BYTE(c);
125 while (--len > 0);
126 return s;
127#endif
128}
129
130
131/*
132vi:ts=4:et
133*/
diff --git a/tools/ucl/src/ucl_swd.ch b/tools/ucl/src/ucl_swd.ch
new file mode 100755
index 0000000000..e40b4a4662
--- /dev/null
+++ b/tools/ucl/src/ucl_swd.ch
@@ -0,0 +1,665 @@
1/* ucl_swd.c -- sliding window dictionary
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 */
26
27
28#if (UCL_UINT_MAX < UCL_0xffffffffL)
29# error "UCL_UINT_MAX"
30#endif
31
32
33/***********************************************************************
34//
35************************************************************************/
36
37#ifndef SWD_N
38# define SWD_N N
39#endif
40#ifndef SWD_F
41# define SWD_F F
42#endif
43#ifndef SWD_THRESHOLD
44# define SWD_THRESHOLD THRESHOLD
45#endif
46
47/* unsigned type for dictionary access - don't waste memory here */
48#if (SWD_N + SWD_F + SWD_F < USHRT_MAX)
49 typedef unsigned short swd_uint;
50# define SWD_UINT_MAX USHRT_MAX
51#else
52 typedef ucl_uint swd_uint;
53# define SWD_UINT_MAX UCL_UINT_MAX
54#endif
55#define SWD_UINT(x) ((swd_uint)(x))
56
57
58#ifndef SWD_HSIZE
59# define SWD_HSIZE 16384
60#endif
61#ifndef SWD_MAX_CHAIN
62# define SWD_MAX_CHAIN 2048
63#endif
64
65#if !defined(HEAD3)
66#if 1
67# define HEAD3(b,p) \
68 (((0x9f5f*(((((ucl_uint32)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
69#else
70# define HEAD3(b,p) \
71 (((0x9f5f*(((((ucl_uint32)b[p+2]<<5)^b[p+1])<<5)^b[p]))>>5) & (SWD_HSIZE-1))
72#endif
73#endif
74
75#if (SWD_THRESHOLD == 1) && !defined(HEAD2)
76# if 1 && defined(UCL_UNALIGNED_OK_2)
77# define HEAD2(b,p) (* (const ucl_ushortp) &(b[p]))
78# else
79# define HEAD2(b,p) (b[p] ^ ((unsigned)b[p+1]<<8))
80# endif
81# define NIL2 SWD_UINT_MAX
82#endif
83
84
85#if defined(__UCL_CHECKER)
86 /* malloc arrays of the exact size to detect any overrun */
87# ifndef SWD_USE_MALLOC
88# define SWD_USE_MALLOC
89# endif
90#endif
91
92
93typedef struct
94{
95/* public - "built-in" */
96 ucl_uint n;
97 ucl_uint f;
98 ucl_uint threshold;
99
100/* public - configuration */
101 ucl_uint max_chain;
102 ucl_uint nice_length;
103 ucl_bool use_best_off;
104 ucl_uint lazy_insert;
105
106/* public - output */
107 ucl_uint m_len;
108 ucl_uint m_off;
109 ucl_uint look;
110 int b_char;
111#if defined(SWD_BEST_OFF)
112 ucl_uint best_off[ SWD_BEST_OFF ];
113#endif
114
115/* semi public */
116 UCL_COMPRESS_T *c;
117 ucl_uint m_pos;
118#if defined(SWD_BEST_OFF)
119 ucl_uint best_pos[ SWD_BEST_OFF ];
120#endif
121
122/* private */
123 const ucl_byte *dict;
124 const ucl_byte *dict_end;
125 ucl_uint dict_len;
126
127/* private */
128 ucl_uint ip; /* input pointer (lookahead) */
129 ucl_uint bp; /* buffer pointer */
130 ucl_uint rp; /* remove pointer */
131 ucl_uint b_size;
132
133 unsigned char *b_wrap;
134
135 ucl_uint node_count;
136 ucl_uint first_rp;
137
138#if defined(SWD_USE_MALLOC)
139 unsigned char *b;
140 swd_uint *head3;
141 swd_uint *succ3;
142 swd_uint *best3;
143 swd_uint *llen3;
144#ifdef HEAD2
145 swd_uint *head2;
146#endif
147#else
148 unsigned char b [ SWD_N + SWD_F + SWD_F ];
149 swd_uint head3 [ SWD_HSIZE ];
150 swd_uint succ3 [ SWD_N + SWD_F ];
151 swd_uint best3 [ SWD_N + SWD_F ];
152 swd_uint llen3 [ SWD_HSIZE ];
153#ifdef HEAD2
154 swd_uint head2 [ UCL_UINT32_C(65536) ];
155#endif
156#endif
157}
158ucl_swd_t;
159
160
161/* Access macro for head3.
162 * head3[key] may be uninitialized if the list is emtpy,
163 * but then its value will never be used.
164 */
165#if defined(__UCL_CHECKER)
166# define s_head3(s,key) \
167 ((s->llen3[key] == 0) ? SWD_UINT_MAX : s->head3[key])
168#else
169# define s_head3(s,key) s->head3[key]
170#endif
171
172
173/***********************************************************************
174//
175************************************************************************/
176
177static
178void swd_initdict(ucl_swd_t *s, const ucl_byte *dict, ucl_uint dict_len)
179{
180 s->dict = s->dict_end = NULL;
181 s->dict_len = 0;
182
183 if (!dict || dict_len <= 0)
184 return;
185 if (dict_len > s->n)
186 {
187 dict += dict_len - s->n;
188 dict_len = s->n;
189 }
190
191 s->dict = dict;
192 s->dict_len = dict_len;
193 s->dict_end = dict + dict_len;
194 ucl_memcpy(s->b,dict,dict_len);
195 s->ip = dict_len;
196}
197
198
199static
200void swd_insertdict(ucl_swd_t *s, ucl_uint node, ucl_uint len)
201{
202 ucl_uint key;
203
204 s->node_count = s->n - len;
205 s->first_rp = node;
206
207 while (len-- > 0)
208 {
209 key = HEAD3(s->b,node);
210 s->succ3[node] = s_head3(s,key);
211 s->head3[key] = SWD_UINT(node);
212 s->best3[node] = SWD_UINT(s->f + 1);
213 s->llen3[key]++;
214 assert(s->llen3[key] <= s->n);
215
216#ifdef HEAD2
217 key = HEAD2(s->b,node);
218 s->head2[key] = SWD_UINT(node);
219#endif
220
221 node++;
222 }
223}
224
225
226/***********************************************************************
227//
228************************************************************************/
229
230static
231int swd_init(ucl_swd_t *s, const ucl_byte *dict, ucl_uint dict_len)
232{
233 ucl_uint i = 0;
234 int c = 0;
235
236 if (s->n == 0)
237 s->n = SWD_N;
238 if (s->f == 0)
239 s->f = SWD_F;
240 s->threshold = SWD_THRESHOLD;
241 if (s->n > SWD_N || s->f > SWD_F)
242 return UCL_E_INVALID_ARGUMENT;
243
244#if defined(SWD_USE_MALLOC)
245 s->b = (unsigned char *) ucl_alloc(s->n + s->f + s->f, 1);
246 s->head3 = (swd_uint *) ucl_alloc(SWD_HSIZE, sizeof(*s->head3));
247 s->succ3 = (swd_uint *) ucl_alloc(s->n + s->f, sizeof(*s->succ3));
248 s->best3 = (swd_uint *) ucl_alloc(s->n + s->f, sizeof(*s->best3));
249 s->llen3 = (swd_uint *) ucl_alloc(SWD_HSIZE, sizeof(*s->llen3));
250 if (!s->b || !s->head3 || !s->succ3 || !s->best3 || !s->llen3)
251 return UCL_E_OUT_OF_MEMORY;
252#ifdef HEAD2
253 s->head2 = (swd_uint *) ucl_alloc(UCL_UINT32_C(65536), sizeof(*s->head2));
254 if (!s->head2)
255 return UCL_E_OUT_OF_MEMORY;
256#endif
257#endif
258
259 /* defaults */
260 s->max_chain = SWD_MAX_CHAIN;
261 s->nice_length = s->f;
262 s->use_best_off = 0;
263 s->lazy_insert = 0;
264
265 s->b_size = s->n + s->f;
266 if (s->b_size + s->f >= SWD_UINT_MAX)
267 return UCL_E_ERROR;
268 s->b_wrap = s->b + s->b_size;
269 s->node_count = s->n;
270
271 ucl_memset(s->llen3, 0, sizeof(s->llen3[0]) * SWD_HSIZE);
272#ifdef HEAD2
273#if 1
274 ucl_memset(s->head2, 0xff, sizeof(s->head2[0]) * UCL_UINT32_C(65536));
275 assert(s->head2[0] == NIL2);
276#else
277 for (i = 0; i < UCL_UINT32_C(65536); i++)
278 s->head2[i] = NIL2;
279#endif
280#endif
281
282 s->ip = 0;
283 swd_initdict(s,dict,dict_len);
284 s->bp = s->ip;
285 s->first_rp = s->ip;
286
287 assert(s->ip + s->f <= s->b_size);
288#if 1
289 s->look = (ucl_uint) (s->c->in_end - s->c->ip);
290 if (s->look > 0)
291 {
292 if (s->look > s->f)
293 s->look = s->f;
294 ucl_memcpy(&s->b[s->ip],s->c->ip,s->look);
295 s->c->ip += s->look;
296 s->ip += s->look;
297 }
298#else
299 s->look = 0;
300 while (s->look < s->f)
301 {
302 if ((c = getbyte(*(s->c))) < 0)
303 break;
304 s->b[s->ip] = UCL_BYTE(c);
305 s->ip++;
306 s->look++;
307 }
308#endif
309 if (s->ip == s->b_size)
310 s->ip = 0;
311
312 if (s->look >= 2 && s->dict_len > 0)
313 swd_insertdict(s,0,s->dict_len);
314
315 s->rp = s->first_rp;
316 if (s->rp >= s->node_count)
317 s->rp -= s->node_count;
318 else
319 s->rp += s->b_size - s->node_count;
320
321#if defined(__UCL_CHECKER)
322 /* initialize memory for the first few HEAD3 (if s->ip is not far
323 * enough ahead to do this job for us). The value doesn't matter. */
324 if (s->look < 3)
325 ucl_memset(&s->b[s->bp+s->look],0,3);
326#endif
327
328 UCL_UNUSED(i);
329 UCL_UNUSED(c);
330 return UCL_E_OK;
331}
332
333
334static
335void swd_exit(ucl_swd_t *s)
336{
337#if defined(SWD_USE_MALLOC)
338 /* free in reverse order of allocations */
339#ifdef HEAD2
340 ucl_free(s->head2); s->head2 = NULL;
341#endif
342 ucl_free(s->llen3); s->llen3 = NULL;
343 ucl_free(s->best3); s->best3 = NULL;
344 ucl_free(s->succ3); s->succ3 = NULL;
345 ucl_free(s->head3); s->head3 = NULL;
346 ucl_free(s->b); s->b = NULL;
347#else
348 UCL_UNUSED(s);
349#endif
350}
351
352
353#define swd_pos2off(s,pos) \
354 (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
355
356
357/***********************************************************************
358//
359************************************************************************/
360
361static __inline__
362void swd_getbyte(ucl_swd_t *s)
363{
364 int c;
365
366 if ((c = getbyte(*(s->c))) < 0)
367 {
368 if (s->look > 0)
369 --s->look;
370#if defined(__UCL_CHECKER)
371 /* initialize memory - value doesn't matter */
372 s->b[s->ip] = 0;
373 if (s->ip < s->f)
374 s->b_wrap[s->ip] = 0;
375#endif
376 }
377 else
378 {
379 s->b[s->ip] = UCL_BYTE(c);
380 if (s->ip < s->f)
381 s->b_wrap[s->ip] = UCL_BYTE(c);
382 }
383 if (++s->ip == s->b_size)
384 s->ip = 0;
385 if (++s->bp == s->b_size)
386 s->bp = 0;
387 if (++s->rp == s->b_size)
388 s->rp = 0;
389}
390
391
392/***********************************************************************
393// remove node from lists
394************************************************************************/
395
396static __inline__
397void swd_remove_node(ucl_swd_t *s, ucl_uint node)
398{
399 if (s->node_count == 0)
400 {
401 ucl_uint key;
402
403#ifdef UCL_DEBUG
404 if (s->first_rp != UCL_UINT_MAX)
405 {
406 if (node != s->first_rp)
407 printf("Remove %5d: %5d %5d %5d %5d %6d %6d\n",
408 node, s->rp, s->ip, s->bp, s->first_rp,
409 s->ip - node, s->ip - s->bp);
410 assert(node == s->first_rp);
411 s->first_rp = UCL_UINT_MAX;
412 }
413#endif
414
415 key = HEAD3(s->b,node);
416 assert(s->llen3[key] > 0);
417 --s->llen3[key];
418
419#ifdef HEAD2
420 key = HEAD2(s->b,node);
421 assert(s->head2[key] != NIL2);
422 if ((ucl_uint) s->head2[key] == node)
423 s->head2[key] = NIL2;
424#endif
425 }
426 else
427 --s->node_count;
428}
429
430
431/***********************************************************************
432//
433************************************************************************/
434
435static
436void swd_accept(ucl_swd_t *s, ucl_uint n)
437{
438 assert(n <= s->look);
439
440 if (n > 0) do
441 {
442 ucl_uint key;
443
444 swd_remove_node(s,s->rp);
445
446 /* add bp into HEAD3 */
447 key = HEAD3(s->b,s->bp);
448 s->succ3[s->bp] = s_head3(s,key);
449 s->head3[key] = SWD_UINT(s->bp);
450 s->best3[s->bp] = SWD_UINT(s->f + 1);
451 s->llen3[key]++;
452 assert(s->llen3[key] <= s->n);
453
454#ifdef HEAD2
455 /* add bp into HEAD2 */
456 key = HEAD2(s->b,s->bp);
457 s->head2[key] = SWD_UINT(s->bp);
458#endif
459
460 swd_getbyte(s);
461 } while (--n > 0);
462}
463
464
465/***********************************************************************
466//
467************************************************************************/
468
469static
470void swd_search(ucl_swd_t *s, ucl_uint node, ucl_uint cnt)
471{
472#if 0 && defined(__GNUC__) && defined(__i386__)
473 register const unsigned char *p1 __asm__("%edi");
474 register const unsigned char *p2 __asm__("%esi");
475 register const unsigned char *px __asm__("%edx");
476#else
477 const unsigned char *p1;
478 const unsigned char *p2;
479 const unsigned char *px;
480#endif
481 ucl_uint m_len = s->m_len;
482 const unsigned char * b = s->b;
483 const unsigned char * bp = s->b + s->bp;
484 const unsigned char * bx = s->b + s->bp + s->look;
485 unsigned char scan_end1;
486
487 assert(s->m_len > 0);
488
489 scan_end1 = bp[m_len - 1];
490 for ( ; cnt-- > 0; node = s->succ3[node])
491 {
492 p1 = bp;
493 p2 = b + node;
494 px = bx;
495
496 assert(m_len < s->look);
497
498 if (
499#if 1
500 p2[m_len - 1] == scan_end1 &&
501 p2[m_len] == p1[m_len] &&
502#endif
503 p2[0] == p1[0] &&
504 p2[1] == p1[1])
505 {
506 ucl_uint i;
507 assert(ucl_memcmp(bp,&b[node],3) == 0);
508
509#if 0 && defined(UCL_UNALIGNED_OK_4)
510 p1 += 3; p2 += 3;
511 while (p1 < px && * (const ucl_uint32p) p1 == * (const ucl_uint32p) p2)
512 p1 += 4, p2 += 4;
513 while (p1 < px && *p1 == *p2)
514 p1 += 1, p2 += 1;
515#else
516 p1 += 2; p2 += 2;
517 do {} while (++p1 < px && *p1 == *++p2);
518#endif
519 i = p1 - bp;
520
521#ifdef UCL_DEBUG
522 if (ucl_memcmp(bp,&b[node],i) != 0)
523 printf("%5ld %5ld %02x%02x %02x%02x\n",
524 (long)s->bp, (long) node,
525 bp[0], bp[1], b[node], b[node+1]);
526#endif
527 assert(ucl_memcmp(bp,&b[node],i) == 0);
528
529#if defined(SWD_BEST_OFF)
530 if (i < SWD_BEST_OFF)
531 {
532 if (s->best_pos[i] == 0)
533 s->best_pos[i] = node + 1;
534 }
535#endif
536 if (i > m_len)
537 {
538 s->m_len = m_len = i;
539 s->m_pos = node;
540 if (m_len == s->look)
541 return;
542 if (m_len >= s->nice_length)
543 return;
544 if (m_len > (ucl_uint) s->best3[node])
545 return;
546 scan_end1 = bp[m_len - 1];
547 }
548 }
549 }
550}
551
552
553/***********************************************************************
554//
555************************************************************************/
556
557#ifdef HEAD2
558
559static
560ucl_bool swd_search2(ucl_swd_t *s)
561{
562 ucl_uint key;
563
564 assert(s->look >= 2);
565 assert(s->m_len > 0);
566
567 key = s->head2[ HEAD2(s->b,s->bp) ];
568 if (key == NIL2)
569 return 0;
570#ifdef UCL_DEBUG
571 if (ucl_memcmp(&s->b[s->bp],&s->b[key],2) != 0)
572 printf("%5ld %5ld %02x%02x %02x%02x\n", (long)s->bp, (long)key,
573 s->b[s->bp], s->b[s->bp+1], s->b[key], s->b[key+1]);
574#endif
575 assert(ucl_memcmp(&s->b[s->bp],&s->b[key],2) == 0);
576#if defined(SWD_BEST_OFF)
577 if (s->best_pos[2] == 0)
578 s->best_pos[2] = key + 1;
579#endif
580
581 if (s->m_len < 2)
582 {
583 s->m_len = 2;
584 s->m_pos = key;
585 }
586 return 1;
587}
588
589#endif
590
591
592/***********************************************************************
593//
594************************************************************************/
595
596static
597void swd_findbest(ucl_swd_t *s)
598{
599 ucl_uint key;
600 ucl_uint cnt, node;
601 ucl_uint len;
602
603 assert(s->m_len > 0);
604
605 /* get current head, add bp into HEAD3 */
606 key = HEAD3(s->b,s->bp);
607 node = s->succ3[s->bp] = s_head3(s,key);
608 cnt = s->llen3[key]++;
609 assert(s->llen3[key] <= s->n + s->f);
610 if (cnt > s->max_chain && s->max_chain > 0)
611 cnt = s->max_chain;
612 s->head3[key] = SWD_UINT(s->bp);
613
614 s->b_char = s->b[s->bp];
615 len = s->m_len;
616 if (s->m_len >= s->look)
617 {
618 if (s->look == 0)
619 s->b_char = -1;
620 s->m_off = 0;
621 s->best3[s->bp] = SWD_UINT(s->f + 1);
622 }
623 else
624 {
625#ifdef HEAD2
626 if (swd_search2(s))
627#endif
628 if (s->look >= 3)
629 swd_search(s,node,cnt);
630 if (s->m_len > len)
631 s->m_off = swd_pos2off(s,s->m_pos);
632 s->best3[s->bp] = SWD_UINT(s->m_len);
633
634#if defined(SWD_BEST_OFF)
635 if (s->use_best_off)
636 {
637 int i;
638 for (i = 2; i < SWD_BEST_OFF; i++)
639 if (s->best_pos[i] > 0)
640 s->best_off[i] = swd_pos2off(s,s->best_pos[i]-1);
641 else
642 s->best_off[i] = 0;
643 }
644#endif
645 }
646
647 swd_remove_node(s,s->rp);
648
649#ifdef HEAD2
650 /* add bp into HEAD2 */
651 key = HEAD2(s->b,s->bp);
652 s->head2[key] = SWD_UINT(s->bp);
653#endif
654}
655
656
657#undef HEAD3
658#undef HEAD2
659#undef s_head3
660
661
662/*
663vi:ts=4:et
664*/
665
diff --git a/tools/ucl/src/ucl_util.c b/tools/ucl/src/ucl_util.c
new file mode 100644
index 0000000000..93b7298302
--- /dev/null
+++ b/tools/ucl/src/ucl_util.c
@@ -0,0 +1,204 @@
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
37UCL_PUBLIC(ucl_bool)
38ucl_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
52const 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
117UCL_PUBLIC(const ucl_byte *)
118ucl_copyright(void)
119{
120 return __ucl_copyright;
121}
122
123UCL_PUBLIC(ucl_uint32)
124ucl_version(void)
125{
126 return UCL_VERSION;
127}
128
129UCL_PUBLIC(const char *)
130ucl_version_string(void)
131{
132 return UCL_VERSION_STRING;
133}
134
135UCL_PUBLIC(const char *)
136ucl_version_date(void)
137{
138 return UCL_VERSION_DATE;
139}
140
141UCL_PUBLIC(const ucl_charp)
142_ucl_version_string(void)
143{
144 return UCL_VERSION_STRING;
145}
146
147UCL_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
170UCL_PUBLIC(ucl_uint32)
171ucl_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/*
203vi:ts=4:et
204*/
diff --git a/tools/ucl/src/ucl_util.h b/tools/ucl/src/ucl_util.h
new file mode 100644
index 0000000000..2292716e4c
--- /dev/null
+++ b/tools/ucl/src/ucl_util.h
@@ -0,0 +1,180 @@
1/* ucl_util.h -- 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 */
26
27
28/* WARNING: this file should *not* be used by applications. It is
29 part of the implementation of the library and is subject
30 to change.
31 */
32
33
34#ifndef __UCL_UTIL_H
35#define __UCL_UTIL_H
36
37#ifndef __UCL_CONF_H
38# include "ucl_conf.h"
39#endif
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45
46/***********************************************************************
47// fast memcpy that copies multiples of 8 byte chunks.
48// len is the number of bytes.
49// note: all parameters must be lvalues, len >= 8
50// dest and src advance, len is undefined afterwards
51************************************************************************/
52
53#if 1 && defined(HAVE_MEMCPY)
54#if !defined(__UCL_DOS16) && !defined(__UCL_WIN16)
55
56#define MEMCPY8_DS(dest,src,len) \
57 memcpy(dest,src,len); \
58 dest += len; \
59 src += len
60
61#endif
62#endif
63
64
65#if 0 && !defined(MEMCPY8_DS)
66
67#define MEMCPY8_DS(dest,src,len) \
68 { do { \
69 *dest++ = *src++; \
70 *dest++ = *src++; \
71 *dest++ = *src++; \
72 *dest++ = *src++; \
73 *dest++ = *src++; \
74 *dest++ = *src++; \
75 *dest++ = *src++; \
76 *dest++ = *src++; \
77 len -= 8; \
78 } while (len > 0); }
79
80#endif
81
82
83#if !defined(MEMCPY8_DS)
84
85#define MEMCPY8_DS(dest,src,len) \
86 { register ucl_uint __l = (len) / 8; \
87 do { \
88 *dest++ = *src++; \
89 *dest++ = *src++; \
90 *dest++ = *src++; \
91 *dest++ = *src++; \
92 *dest++ = *src++; \
93 *dest++ = *src++; \
94 *dest++ = *src++; \
95 *dest++ = *src++; \
96 } while (--__l > 0); }
97
98#endif
99
100
101/***********************************************************************
102// memcpy and pseudo-memmove
103// len is the number of bytes.
104// note: all parameters must be lvalues, len > 0
105// dest and src advance, len is undefined afterwards
106************************************************************************/
107
108#define MEMCPY_DS(dest,src,len) \
109 do *dest++ = *src++; \
110 while (--len > 0)
111
112#define MEMMOVE_DS(dest,src,len) \
113 do *dest++ = *src++; \
114 while (--len > 0)
115
116
117/***********************************************************************
118// fast bzero that clears multiples of 8 pointers
119// n is the number of pointers.
120// note: n > 0
121// s and n are undefined afterwards
122************************************************************************/
123
124#if (UCL_UINT_MAX <= SIZE_T_MAX) && defined(HAVE_MEMSET)
125
126#if 1
127#define BZERO8_PTR(s,l,n) memset((s),0,(ucl_uint)(l)*(n))
128#else
129#define BZERO8_PTR(s,l,n) memset((ucl_voidp)(s),0,(ucl_uint)(l)*(n))
130#endif
131
132#else
133
134#define BZERO8_PTR(s,l,n) \
135 ucl_memset((ucl_voidp)(s),0,(ucl_uint)(l)*(n))
136
137#endif
138
139
140/***********************************************************************
141// rotate (not used at the moment)
142************************************************************************/
143
144#if 0
145#if defined(__GNUC__) && defined(__i386__)
146
147unsigned char ucl_rotr8(unsigned char value, int shift);
148extern __inline__ unsigned char ucl_rotr8(unsigned char value, int shift)
149{
150 unsigned char result;
151
152 __asm__ __volatile__ ("movb %b1, %b0; rorb %b2, %b0"
153 : "=a"(result) : "g"(value), "c"(shift));
154 return result;
155}
156
157unsigned short ucl_rotr16(unsigned short value, int shift);
158extern __inline__ unsigned short ucl_rotr16(unsigned short value, int shift)
159{
160 unsigned short result;
161
162 __asm__ __volatile__ ("movw %b1, %b0; rorw %b2, %b0"
163 : "=a"(result) : "g"(value), "c"(shift));
164 return result;
165}
166
167#endif
168#endif
169
170
171
172#ifdef __cplusplus
173} /* extern "C" */
174#endif
175
176#endif /* already included */
177
178/*
179vi:ts=4:et
180*/