1 /* lzo_swd.ch -- sliding window dictionary
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer
8 The LZO library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 The LZO library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the LZO library; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzo/
29 #if (LZO_UINT_MAX < LZO_0xffffffffL)
30 # error "LZO_UINT_MAX"
32 #if defined(LZO_DEBUG)
35 #if defined(__LZO_CHECKER)
40 /***********************************************************************
42 ************************************************************************/
44 /* unsigned type for dictionary access - don't waste memory here */
45 #if (0UL + SWD_N + SWD_F + SWD_F < 65535UL)
46 typedef lzo_uint16_t swd_uint;
47 # define SWD_UINT_MAX 0xffffu
49 typedef lzo_uint32_t swd_uint;
50 # define SWD_UINT_MAX 0xffffffffu
52 #define swd_uintp swd_uint *
53 #define SWD_UINT(x) ((swd_uint)(x))
57 # define SWD_HSIZE 16384
60 # define SWD_MAX_CHAIN 2048
66 ((DMUL(0x9f5f,(((((lzo_xint)b[p]<<5)^b[p+1])<<5)^b[p+2]))>>5) & (SWD_HSIZE-1))
69 ((DMUL(0x9f5f,(((((lzo_xint)b[p+2]<<5)^b[p+1])<<5)^b[p]))>>5) & (SWD_HSIZE-1))
73 #if !(SWD_NO_HEAD2) && (SWD_THRESHOLD == 1) && !defined(HEAD2)
74 # if 1 && (LZO_OPT_UNALIGNED16)
75 # define HEAD2(b,p) UA_GET_NE16((b)+(p))
77 # define HEAD2(b,p) (b[p] ^ ((unsigned)b[(p)+1]<<8))
79 # define NIL2 SWD_UINT_MAX
82 #define IF_HEAD2(s) /*empty*/
88 /* public - "built-in" */
91 lzo_uint swd_threshold;
93 /* public - configuration */
96 lzo_bool use_best_off;
104 #if defined(SWD_BEST_OFF)
105 lzo_uint best_off[ SWD_BEST_OFF ];
111 #if defined(SWD_BEST_OFF)
112 lzo_uint best_pos[ SWD_BEST_OFF ];
116 const lzo_bytep dict;
117 const lzo_bytep dict_end;
121 lzo_uint ip; /* input pointer (lookahead) */
122 lzo_uint bp; /* buffer pointer */
123 lzo_uint rp; /* remove pointer */
131 #if defined(__LZO_CHECKER)
132 /* malloc arrays of the exact size to detect any overrun */
143 unsigned char b [ SWD_N + SWD_F + SWD_F ];
144 swd_uint head3 [ SWD_HSIZE ];
145 swd_uint succ3 [ SWD_N + SWD_F ];
146 swd_uint best3 [ SWD_N + SWD_F ];
147 swd_uint llen3 [ SWD_HSIZE ];
149 swd_uint head2 [ 65536L ];
154 #define lzo_swd_p lzo_swd_t *
158 #define s_head3(s) s->head3
159 #define s_succ3(s) s->succ3
160 #define s_best3(s) s->best3
161 #define s_llen3(s) s->llen3
163 #define s_head2(s) s->head2
165 #define SIZEOF_LZO_SWD_T (sizeof(lzo_swd_t))
168 /* Access macro for head3.
169 * head3[key] may be uninitialized if the list is emtpy,
170 * but then its value will never be used.
172 #if 1 || defined(__LZO_CHECKER)
173 # define s_get_head3(s,key) \
174 ((swd_uint)((s_llen3(s)[key] == 0) ? SWD_UINT_MAX : s_head3(s)[key]))
176 # define s_get_head3(s,key) (s_head3(s)[key])
180 /***********************************************************************
182 ************************************************************************/
185 void swd_initdict(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
187 s->dict = s->dict_end = NULL;
190 if (!dict || dict_len == 0)
192 if (dict_len > s->swd_n)
194 dict += dict_len - s->swd_n;
199 s->dict_len = dict_len;
200 s->dict_end = dict + dict_len;
201 lzo_memcpy(s_b(s),dict,dict_len);
207 void swd_insertdict(lzo_swd_p s, lzo_uint node, lzo_uint len)
211 s->node_count = s->swd_n - len;
216 key = HEAD3(s_b(s),node);
217 s_succ3(s)[node] = s_get_head3(s,key);
218 s_head3(s)[key] = SWD_UINT(node);
219 s_best3(s)[node] = SWD_UINT(s->swd_f + 1);
221 assert(s_llen3(s)[key] <= s->swd_n);
225 key = HEAD2(s_b(s),node);
226 s_head2(s)[key] = SWD_UINT(node);
236 /***********************************************************************
238 ************************************************************************/
241 int swd_init(lzo_swd_p s, const lzo_bytep dict, lzo_uint dict_len)
243 #if defined(__LZO_CHECKER)
244 s->b = (lzo_bytep) malloc(SWD_N + SWD_F + SWD_F);
245 s->head3 = (swd_uintp) malloc(sizeof(swd_uint) * SWD_HSIZE);
246 s->succ3 = (swd_uintp) malloc(sizeof(swd_uint) * (SWD_N + SWD_F));
247 s->best3 = (swd_uintp) malloc(sizeof(swd_uint) * (SWD_N + SWD_F));
248 s->llen3 = (swd_uintp) malloc(sizeof(swd_uint) * SWD_HSIZE);
251 s->head2 = (swd_uintp) malloc(sizeof(swd_uint) * 65536L);
258 #if defined(SWD_BEST_OFF)
261 for (i = 0; i < SWD_BEST_OFF; i++)
262 s->best_off[i] = s->best_pos[i] = 0;
268 s->swd_threshold = SWD_THRESHOLD;
271 s->max_chain = SWD_MAX_CHAIN;
272 s->nice_length = s->swd_f;
276 s->b_size = s->swd_n + s->swd_f;
278 if (2 * s->swd_f >= s->swd_n || s->b_size + s->swd_f >= SWD_UINT_MAX)
281 LZO_COMPILE_TIME_ASSERT(!(0ul + 2 * SWD_F >= SWD_N))
282 LZO_COMPILE_TIME_ASSERT(!(0ul + SWD_N + SWD_F + SWD_F >= SWD_UINT_MAX))
284 s->b_wrap = s_b(s) + s->b_size;
285 s->node_count = s->swd_n;
287 lzo_memset(s_llen3(s), 0, (lzo_uint)sizeof(s_llen3(s)[0]) * (lzo_uint)SWD_HSIZE);
291 lzo_memset(s_head2(s), 0xff, (lzo_uint)sizeof(s_head2(s)[0]) * 65536L);
292 assert(s_head2(s)[0] == NIL2);
295 for (i = 0; i < 65536L; i++)
296 s_head2(s)[i] = NIL2;
302 swd_initdict(s,dict,dict_len);
306 assert(s->ip + s->swd_f <= s->b_size);
308 s->look = (lzo_uint) (s->c->in_end - s->c->ip);
311 if (s->look > s->swd_f)
313 lzo_memcpy(&s_b(s)[s->ip],s->c->ip,s->look);
319 while (s->look < s->swd_f)
322 if ((c = getbyte(*(s->c))) < 0)
324 s_b(s)[s->ip] = LZO_BYTE(c);
329 if (s->ip == s->b_size)
332 if (s->look >= 2 && s->dict_len > 0)
333 swd_insertdict(s,0,s->dict_len);
336 if (s->rp >= s->node_count)
337 s->rp -= s->node_count;
339 s->rp += s->b_size - s->node_count;
341 #if 1 || defined(__LZO_CHECKER)
342 /* initialize memory for the first few HEAD3 (if s->ip is not far
343 * enough ahead to do this job for us). The value doesn't matter. */
345 lzo_bytep p = &s_b(s)[s->bp+s->look];
346 p[0] = p[1] = p[2] = 0;
355 void swd_exit(lzo_swd_p s)
357 #if defined(__LZO_CHECKER)
358 /* free in reverse order of allocations */
360 free(s->head2); s->head2 = NULL;
362 free(s->llen3); s->llen3 = NULL;
363 free(s->best3); s->best3 = NULL;
364 free(s->succ3); s->succ3 = NULL;
365 free(s->head3); s->head3 = NULL;
366 free(s->b); s->b = NULL;
373 #define swd_pos2off(s,pos) \
374 (s->bp > (pos) ? s->bp - (pos) : s->b_size - ((pos) - s->bp))
377 /***********************************************************************
379 ************************************************************************/
382 void swd_getbyte(lzo_swd_p s)
386 if ((c = getbyte(*(s->c))) < 0)
390 #if 1 || defined(__LZO_CHECKER)
391 /* initialize memory - value doesn't matter */
393 if (s->ip < s->swd_f)
394 s->b_wrap[s->ip] = 0;
399 s_b(s)[s->ip] = LZO_BYTE(c);
400 if (s->ip < s->swd_f)
401 s->b_wrap[s->ip] = LZO_BYTE(c);
403 if (++s->ip == s->b_size)
405 if (++s->bp == s->b_size)
407 if (++s->rp == s->b_size)
412 /***********************************************************************
413 // remove node from lists
414 ************************************************************************/
417 void swd_remove_node(lzo_swd_p s, lzo_uint node)
419 if (s->node_count == 0)
424 if (s->first_rp != LZO_UINT_MAX)
426 if (node != s->first_rp)
427 printf("Remove %5ld: %5ld %5ld %5ld %5ld %6ld %6ld\n",
428 (long)node, (long)s->rp, (long)s->ip, (long)s->bp,
429 (long)s->first_rp, (long)(s->ip - node),
430 (long)(s->ip - s->bp));
431 assert(node == s->first_rp);
432 s->first_rp = LZO_UINT_MAX;
436 key = HEAD3(s_b(s),node);
437 assert(s_llen3(s)[key] > 0);
442 key = HEAD2(s_b(s),node);
443 assert(s_head2(s)[key] != NIL2);
444 if ((lzo_uint) s_head2(s)[key] == node)
445 s_head2(s)[key] = NIL2;
454 /***********************************************************************
456 ************************************************************************/
459 void swd_accept(lzo_swd_p s, lzo_uint n)
461 assert(n <= s->look);
467 swd_remove_node(s,s->rp);
469 /* add bp into HEAD3 */
470 key = HEAD3(s_b(s),s->bp);
471 s_succ3(s)[s->bp] = s_get_head3(s,key);
472 s_head3(s)[key] = SWD_UINT(s->bp);
473 s_best3(s)[s->bp] = SWD_UINT(s->swd_f + 1);
475 assert(s_llen3(s)[key] <= s->swd_n);
478 /* add bp into HEAD2 */
480 key = HEAD2(s_b(s),s->bp);
481 s_head2(s)[key] = SWD_UINT(s->bp);
490 /***********************************************************************
492 ************************************************************************/
495 void swd_search(lzo_swd_p s, lzo_uint node, lzo_uint cnt)
500 lzo_uint m_len = s->m_len;
501 const lzo_bytep b = s_b(s);
502 const lzo_bytep bp = s_b(s) + s->bp;
503 const lzo_bytep bx = s_b(s) + s->bp + s->look;
504 unsigned char scan_end1;
506 assert(s->m_len > 0);
508 scan_end1 = bp[m_len - 1];
509 for ( ; cnt-- > 0; node = s_succ3(s)[node])
515 assert(m_len < s->look);
519 p2[m_len - 1] == scan_end1 &&
520 p2[m_len] == p1[m_len] &&
526 assert(lzo_memcmp(bp,&b[node],3) == 0);
528 #if 0 && (LZO_OPT_UNALIGNED32)
530 while (p1 + 4 <= px && UA_GET_NE32(p1) == UA_GET_NE32(p2))
532 while (p1 < px && *p1 == *p2)
536 do {} while (++p1 < px && *p1 == *++p2);
541 if (lzo_memcmp(bp,&b[node],i) != 0)
542 printf("%5ld %5ld %5ld %02x/%02x %02x/%02x\n",
543 (long)s->bp, (long) node, (long) i,
544 bp[0], bp[1], b[node], b[node+1]);
546 assert(lzo_memcmp(bp,&b[node],i) == 0);
548 #if defined(SWD_BEST_OFF)
549 if (i < SWD_BEST_OFF)
551 if (s->best_pos[i] == 0)
552 s->best_pos[i] = node + 1;
557 s->m_len = m_len = i;
559 if (m_len == s->look)
561 if (m_len >= s->nice_length)
563 if (m_len > (lzo_uint) s_best3(s)[node])
565 scan_end1 = bp[m_len - 1];
572 /***********************************************************************
574 ************************************************************************/
579 lzo_bool swd_search2(lzo_swd_p s)
583 assert(s->look >= 2);
584 assert(s->m_len > 0);
586 key = s_head2(s)[ HEAD2(s_b(s),s->bp) ];
590 if (lzo_memcmp(&s_b(s)[s->bp],&s_b(s)[key],2) != 0)
591 printf("%5ld %5ld %02x/%02x %02x/%02x\n", (long)s->bp, (long)key,
592 s_b(s)[s->bp], s_b(s)[s->bp+1], s_b(s)[key], s_b(s)[key+1]);
594 assert(lzo_memcmp(&s_b(s)[s->bp],&s_b(s)[key],2) == 0);
595 #if defined(SWD_BEST_OFF)
596 if (s->best_pos[2] == 0)
597 s->best_pos[2] = key + 1;
611 /***********************************************************************
613 ************************************************************************/
616 void swd_findbest(lzo_swd_p s)
622 assert(s->m_len > 0);
624 /* get current head, add bp into HEAD3 */
625 key = HEAD3(s_b(s),s->bp);
626 node = s_succ3(s)[s->bp] = s_get_head3(s,key);
627 cnt = s_llen3(s)[key]++;
628 assert(s_llen3(s)[key] <= s->swd_n + s->swd_f);
629 if (cnt > s->max_chain && s->max_chain > 0)
631 s_head3(s)[key] = SWD_UINT(s->bp);
633 s->b_char = s_b(s)[s->bp];
635 if (s->m_len >= s->look)
640 s_best3(s)[s->bp] = SWD_UINT(s->swd_f + 1);
645 if (swd_search2(s) && s->look >= 3)
646 swd_search(s,node,cnt);
649 swd_search(s,node,cnt);
652 s->m_off = swd_pos2off(s,s->m_pos);
653 s_best3(s)[s->bp] = SWD_UINT(s->m_len);
655 #if defined(SWD_BEST_OFF)
659 for (i = 2; i < SWD_BEST_OFF; i++)
660 if (s->best_pos[i] > 0)
661 s->best_off[i] = swd_pos2off(s,s->best_pos[i]-1);
668 swd_remove_node(s,s->rp);
671 /* add bp into HEAD2 */
673 key = HEAD2(s_b(s),s->bp);
674 s_head2(s)[key] = SWD_UINT(s->bp);