1 ////////////////////////////////////////////////////////////////////////////////
2 // compression code license (ONLY compression code)
4 // Copyright (c) 1997-2001 John Sadler (john_sadler@alum.mit.edu)
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // * Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
18 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 static inline uint16_t nwu16 (uint16_t number
) {
31 const uint8_t *pointer
= (const uint8_t *)&number
;
33 return (uint16_t)(((uint16_t)(pointer
[0]<<8))|(pointer
[1]));
37 static inline uint32_t nwu32 (uint32_t number
) {
38 const uint16_t *pointer
= (const uint16_t *)&number
;
40 return ((uint32_t)(nwu16(pointer
[0])<<16))|nwu16(pointer
[1]);
44 #define BIT_NUMBER(_x) (1<<(_x))
45 #define LZ_TYPE_BITS (1)
46 #define LZ_OFFSET_BITS (12)
47 #define LZ_LENGTH_BITS (5)
48 #define LZ_NEXT_BITS (8)
49 #define LZ_PHRASE_BITS (LZ_TYPE_BITS+LZ_OFFSET_BITS+LZ_LENGTH_BITS+LZ_NEXT_BITS)
50 #define LZ_SYMBOL_BITS (LZ_TYPE_BITS+LZ_NEXT_BITS)
53 ** if you match fewer characters than this, don't bother,
54 ** it's smaller to encode it as a sequence of symbol tokens.
56 #define LZ_MINIMUM_USEFUL_MATCH ((int)(LZ_PHRASE_BITS/LZ_SYMBOL_BITS))
58 #define LZ_WINDOW_SIZE (BIT_NUMBER(LZ_OFFSET_BITS))
59 #define LZ_WINDOW_SIZE (BIT_NUMBER(LZ_OFFSET_BITS))
60 #define LZ_BUFFER_SIZE (BIT_NUMBER(LZ_LENGTH_BITS)+LZ_MINIMUM_USEFUL_MATCH)
63 ({ typeof(a) _a = (a); \
68 static inline void bitSet (unsigned char *bits
, size_t index
, int value
) {
69 int byteIndex
= (index
>>3), bitIndex
= (index
&0x07);
70 unsigned char mask
= (unsigned char)(128>>bitIndex
);
72 if (value
) bits
[byteIndex
] |= mask
; else bits
[byteIndex
] &= ~mask
;
76 static inline int bitGet (const unsigned char *bits
, size_t index
) {
77 int byteIndex
= (index
>>3), bitIndex
= (index
&0x07);
79 return ((bits
[byteIndex
]>>(7-bitIndex
))&0x01);