1 /* ucl_mchw.ch -- matching functions using a window
3 This file is part of the UCL data compression library.
5 Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer
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.
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.
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.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
30 /***********************************************************************
32 ************************************************************************/
38 ucl_uint look; /* bytes in lookahead buffer */
49 const ucl_byte *in_end;
60 struct ucl_compress_config_t conf;
63 ucl_progress_callback_p cb;
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 */
70 unsigned long lit_bytes;
71 unsigned long match_bytes;
72 unsigned long rep_bytes;
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))
83 #define getbyte(c) ((c).ip < (c).in_end ? *((c).ip)++ : (-1))
90 /***********************************************************************
92 ************************************************************************/
95 init_match ( UCL_COMPRESS_T *c, ucl_swd_t *s,
96 const ucl_byte *dict, ucl_uint dict_len,
106 c->last_m_len = c->last_m_off = 0;
108 c->textsize = c->codesize = c->printcount = 0;
109 c->lit_bytes = c->match_bytes = c->rep_bytes = 0;
112 r = swd_init(s,dict,dict_len);
119 s->use_best_off = (flags & 1) ? 1 : 0;
124 /***********************************************************************
126 ************************************************************************/
129 find_match ( UCL_COMPRESS_T *c, ucl_swd_t *s,
130 ucl_uint this_len, ucl_uint skip )
136 assert(this_len >= skip);
137 swd_accept(s, this_len - skip);
138 c->textsize += this_len - skip + 1;
142 assert(this_len <= 1);
143 c->textsize += this_len - skip;
146 s->m_len = THRESHOLD;
149 memset(s->best_pos,0,sizeof(s->best_pos));
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.
157 c->m_off = (s->m_len == THRESHOLD) ? 0 : s->m_off;
172 c->look = s->look + 1;
174 c->bp = c->ip - c->look;
177 /* brute force match search */
178 if (c->m_len > THRESHOLD && c->m_len + 1 <= c->look)
180 const ucl_byte *ip = c->bp;
181 const ucl_byte *m = c->bp - c->m_off;
182 const ucl_byte *in = c->in;
193 if (memcmp(in,ip,c->m_len+1) == 0)
194 printf("%p %p %p %5d\n",in,ip,m,c->m_len);
200 if (c->cb && c->textsize > c->printcount)
202 (*c->cb->callback)(c->textsize,c->codesize,3,c->cb->user);
203 c->printcount += 1024;
210 /***********************************************************************
212 ************************************************************************/
214 static int bbConfig(UCL_COMPRESS_T *c, int endian, int bitsize)
220 c->bb_c_endian = endian;
224 if (bitsize != 8 && bitsize != 16 && bitsize != 32)
227 c->bb_c_s8 = bitsize / 8;
229 c->bb_b = 0; c->bb_k = 0;
236 static void bbWriteBits(UCL_COMPRESS_T *c)
238 ucl_byte *p = c->bb_p;
239 ucl_uint32 b = c->bb_b;
241 p[0] = UCL_BYTE(b >> 0);
244 p[1] = UCL_BYTE(b >> 8);
247 p[2] = UCL_BYTE(b >> 16);
248 p[3] = UCL_BYTE(b >> 24);
254 static void bbPutBit(UCL_COMPRESS_T *c, unsigned bit)
256 assert(bit == 0 || bit == 1);
257 assert(c->bb_k <= c->bb_c_s);
259 if (c->bb_k < c->bb_c_s)
263 assert(c->bb_p == NULL);
265 c->bb_op += c->bb_c_s8;
267 assert(c->bb_p != NULL);
268 assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
270 c->bb_b = (c->bb_b << 1) + bit;
275 assert(c->bb_p != NULL);
276 assert(c->bb_p + c->bb_c_s8 <= c->bb_op);
280 c->bb_op += c->bb_c_s8;
287 static void bbPutByte(UCL_COMPRESS_T *c, unsigned b)
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);
295 static void bbFlushBits(UCL_COMPRESS_T *c, unsigned filler_bit)
299 assert(c->bb_k <= c->bb_c_s);
300 while (c->bb_k != c->bb_c_s)
301 bbPutBit(c, filler_bit);