5 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 /** Number of aligned offset codes */
33 #define LZX_ALIGNOFFSET_CODES 8
35 /** Aligned offset code length (in bits) */
36 #define LZX_ALIGNOFFSET_BITS 3
38 /** Number of pretree codes */
39 #define LZX_PRETREE_CODES 20
41 /** Pretree code length (in bits) */
42 #define LZX_PRETREE_BITS 4
44 /** Number of literal main codes */
45 #define LZX_MAIN_LIT_CODES 256
47 /** Number of position slots */
48 #define LZX_POSITION_SLOTS 30
50 /** Number of main codes */
51 #define LZX_MAIN_CODES ( LZX_MAIN_LIT_CODES + ( 8 * LZX_POSITION_SLOTS ) )
53 /** Number of length codes */
54 #define LZX_LENGTH_CODES 249
56 /** Block type length (in bits) */
57 #define LZX_BLOCK_TYPE_BITS 3
59 /** Default block length */
60 #define LZX_DEFAULT_BLOCK_LEN 32768
62 /** Number of repeated offsets */
63 #define LZX_REPEATED_OFFSETS 3
66 #define LZX_WIM_MAGIC_FILESIZE 12000000
71 LZX_BLOCK_VERBATIM
= 1,
72 /** Aligned offset block */
73 LZX_BLOCK_ALIGNOFFSET
= 2,
74 /** Uncompressed block */
75 LZX_BLOCK_UNCOMPRESSED
= 3,
78 /** An LZX input stream */
79 struct lzx_input_stream
{
84 /** Offset within stream */
88 /** An LZX output stream */
89 struct lzx_output_stream
{
92 /** Offset within stream */
94 /** End of current block within stream */
98 /** LZX decompressor */
101 struct lzx_input_stream input
;
103 struct lzx_output_stream output
;
105 uint32_t accumulator
;
106 /** Number of bits in accumulator */
109 enum lzx_block_type block_type
;
110 /** Repeated offsets */
111 unsigned int repeated_offset
[LZX_REPEATED_OFFSETS
];
113 /** Aligned offset Huffman alphabet */
114 struct huffman_alphabet alignoffset
;
115 /** Aligned offset raw symbols
117 * Must immediately follow the aligned offset Huffman
120 huffman_raw_symbol_t alignoffset_raw
[LZX_ALIGNOFFSET_CODES
];
121 /** Aligned offset code lengths */
122 uint8_t alignoffset_lengths
[LZX_ALIGNOFFSET_CODES
];
124 /** Pretree Huffman alphabet */
125 struct huffman_alphabet pretree
;
126 /** Pretree raw symbols
128 * Must immediately follow the pretree Huffman alphabet.
130 huffman_raw_symbol_t pretree_raw
[LZX_PRETREE_CODES
];
131 /** Preetree code lengths */
132 uint8_t pretree_lengths
[LZX_PRETREE_CODES
];
134 /** Main Huffman alphabet */
135 struct huffman_alphabet main
;
138 * Must immediately follow the main Huffman alphabet.
140 huffman_raw_symbol_t main_raw
[LZX_MAIN_CODES
];
141 /** Main code lengths */
144 uint8_t literals
[LZX_MAIN_LIT_CODES
];
145 /** Remaining symbols */
146 uint8_t remainder
[ LZX_MAIN_CODES
- LZX_MAIN_LIT_CODES
];
147 } __attribute__ (( packed
)) main_lengths
;
149 /** Length Huffman alphabet */
150 struct huffman_alphabet length
;
151 /** Length raw symbols
153 * Must immediately follow the length Huffman alphabet.
155 huffman_raw_symbol_t length_raw
[LZX_LENGTH_CODES
];
156 /** Length code lengths */
157 uint8_t length_lengths
[LZX_LENGTH_CODES
];
161 * Calculate number of footer bits for a given position slot
163 * @v position_slot Position slot
164 * @ret footer_bits Number of footer bits
166 static inline unsigned int lzx_footer_bits ( unsigned int position_slot
) {
168 if ( position_slot
< 2 ) {
170 } else if ( position_slot
< 38 ) {
171 return ( ( position_slot
/ 2 ) - 1 );
177 extern ssize_t
lzx_decompress ( const void *data
, size_t len
, void *buf
);