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
33 /** Number of aligned offset codes */
34 #define LZX_ALIGNOFFSET_CODES 8
36 /** Aligned offset code length (in bits) */
37 #define LZX_ALIGNOFFSET_BITS 3
39 /** Number of pretree codes */
40 #define LZX_PRETREE_CODES 20
42 /** Pretree code length (in bits) */
43 #define LZX_PRETREE_BITS 4
45 /** Number of literal main codes */
46 #define LZX_MAIN_LIT_CODES 256
48 /** Number of position slots */
49 #define LZX_POSITION_SLOTS 30
51 /** Number of main codes */
52 #define LZX_MAIN_CODES ( LZX_MAIN_LIT_CODES + ( 8 * LZX_POSITION_SLOTS ) )
54 /** Number of length codes */
55 #define LZX_LENGTH_CODES 249
57 /** Block type length (in bits) */
58 #define LZX_BLOCK_TYPE_BITS 3
60 /** Default block length */
61 #define LZX_DEFAULT_BLOCK_LEN 32768
63 /** Number of repeated offsets */
64 #define LZX_REPEATED_OFFSETS 3
67 #define LZX_WIM_MAGIC_FILESIZE 12000000
72 LZX_BLOCK_VERBATIM
= 1,
73 /** Aligned offset block */
74 LZX_BLOCK_ALIGNOFFSET
= 2,
75 /** Uncompressed block */
76 LZX_BLOCK_UNCOMPRESSED
= 3,
79 /** An LZX input stream */
80 struct lzx_input_stream
{
85 /** Offset within stream */
89 /** An LZX output stream */
90 struct lzx_output_stream
{
93 /** Offset within stream */
95 /** End of current block within stream */
99 /** LZX decompressor */
102 struct lzx_input_stream input
;
104 struct lzx_output_stream output
;
106 uint32_t accumulator
;
107 /** Number of bits in accumulator */
110 enum lzx_block_type block_type
;
111 /** Repeated offsets */
112 unsigned int repeated_offset
[LZX_REPEATED_OFFSETS
];
114 /** Aligned offset Huffman alphabet */
115 struct huffman_alphabet alignoffset
;
116 /** Aligned offset raw symbols
118 * Must immediately follow the aligned offset Huffman
121 huffman_raw_symbol_t alignoffset_raw
[LZX_ALIGNOFFSET_CODES
];
122 /** Aligned offset code lengths */
123 uint8_t alignoffset_lengths
[LZX_ALIGNOFFSET_CODES
];
125 /** Pretree Huffman alphabet */
126 struct huffman_alphabet pretree
;
127 /** Pretree raw symbols
129 * Must immediately follow the pretree Huffman alphabet.
131 huffman_raw_symbol_t pretree_raw
[LZX_PRETREE_CODES
];
132 /** Preetree code lengths */
133 uint8_t pretree_lengths
[LZX_PRETREE_CODES
];
135 /** Main Huffman alphabet */
136 struct huffman_alphabet main
;
139 * Must immediately follow the main Huffman alphabet.
141 huffman_raw_symbol_t main_raw
[LZX_MAIN_CODES
];
142 /** Main code lengths */
145 uint8_t literals
[LZX_MAIN_LIT_CODES
];
146 /** Remaining symbols */
147 uint8_t remainder
[ LZX_MAIN_CODES
- LZX_MAIN_LIT_CODES
];
148 } __attribute__ (( packed
)) main_lengths
;
150 /** Length Huffman alphabet */
151 struct huffman_alphabet length
;
152 /** Length raw symbols
154 * Must immediately follow the length Huffman alphabet.
156 huffman_raw_symbol_t length_raw
[LZX_LENGTH_CODES
];
157 /** Length code lengths */
158 uint8_t length_lengths
[LZX_LENGTH_CODES
];
162 * Calculate number of footer bits for a given position slot
164 * @v position_slot Position slot
165 * @ret footer_bits Number of footer bits
167 static inline unsigned int lzx_footer_bits ( unsigned int position_slot
) {
169 if ( position_slot
< 2 ) {
171 } else if ( position_slot
< 38 ) {
172 return ( ( position_slot
/ 2 ) - 1 );
178 extern ssize_t
lzx_decompress ( const void *data
, size_t len
, void *buf
);