Debian Codegraph @

xpress/xpress.go

raw
1 /*
2 Decompression support for the XPRESS (MS-XCA) compression algorithm.
3
4 Reference:
5 https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-xca/
6 (2.1 XPRESS Algorithm Details, 2.2 LZ77+Huffman Algorithm Details)
7
8 The LZ77+Huffman variant is used for WIM images and for NTFS files
9 compressed with the Windows Overlay Filter (Compact OS / system
10 compression) on Windows 8 and later. The plain LZ77 variant is the
11 COMPRESSION_FORMAT_XPRESS format of the Windows compression API
12 (RtlCompressBuffer), used e.g. for hibernation files.
13
14 Plain LZ77:
15 The stream is a sequence of 32-bit flag groups, each flag tested from
16 bit 31 down. A clear bit is a literal byte. A set bit is a match: an
17 LE16 word with (offset-1) in the top 13 bits and (length-3) in the low
18 3 bits. A length of 7 selects the shared-nibble form: the low nibble of
19 the next stream byte supplies the real length for this match, and the
20 high nibble of that byte supplies the length of the next consecutive
21 match that also uses this form. A nibble of 15 selects a raw length: a
22 byte >= 22, or if that byte is 255, an LE16, or if that is 0, an LE32.
23 The final flag group is padded with set bits; a match flag with no
24 input left is the end-of-data marker.
25
26 LZ77+Huffman:
27 The first 256 bytes hold 512 4-bit code lengths (even symbol in the low
28 nibble, odd in the high). Canonical codes are assigned in (length,
29 symbol) order, most-significant bit first. The bit stream follows as
30 LE16 words, MSB first, read through a 32-bit register that is refilled
31 while fewer than 15 bits remain. Symbols 0..255 are literals. Symbol
32 256 is end-of-data; mid-stream it decodes as a match of length 3 at
33 distance 1 (Microsoft behavior). Symbols 257..511 are matches: the low
34 nibble is (length-3), a value of 15 selecting a raw extension byte
35 (length = byte + 18, or if the byte is 255, an LE16/LE32 value plus 3),
36 and the high nibble is the position of the highest set bit of the
37 offset, whose remaining low bits follow in the stream. The uncompressed
38 size must be known in advance because end-of-data is only recognized
39 once the output is complete.
40
41 https://github.com/sleuthkit/sleuthkit (tsk/fs/xpress.c)
42 */
43
44 package xpress
45
46 import (
47 "encoding/binary"
48 "errors"
49 )
50
51 // MaxSize is the maximum size a single stream may decompress to. Both
52 // variants enforce it so a crafted stream cannot request an excessive
53 // allocation or produce unbounded output.
54 const MaxSize = 32 << 20 // 32 MiB
55
56 var (
57 errInvalidCode = errors.New("xpress: invalid Huffman code lengths")
58 errCorrupt = errors.New("xpress: corrupt stream")
59 errTruncated = errors.New("xpress: truncated stream")
60 errTooLarge = errors.New("xpress: decompressed data exceeds MaxSize")
61 )
62
63 const (
64 // Depth of the canonical decode table (maximum code length is 15).
65 xpressTableBits = 15
66
67 // Size of the canonical decode table.
68 xpressTableSize = 1 << xpressTableBits // 32768 entries
69
70 // Number of Huffman symbols: 256 literals + 256 match symbols.
71 xpressNumSymbols = 512
72 )
73
74 // AppendDecompressed appends the decompressed form of an XPRESS plain
75 // LZ77 stream to out and returns the extended slice. The stream is
76 // self-terminating, so no output size is required. On error, out is
77 // returned unmodified.
78 func AppendDecompressed(out, in []byte) ([]byte, error) {
79 base := len(out)
80
81 // Offset of the shared-nibble byte, or -1 when none is pending.
82 // The pending half survives literals: it is consumed only by the
83 // next match that uses the shared-nibble form.
84 pending_len := -1
85
86 flags := uint32(0)
87 flags_left := 0
88 i := 0
89
90 for {
91 if flags_left == 0 {
92 if len(in)-i == 0 {
93 return out, nil
94 }
95 if len(in)-i < 4 {
96 return out[:base], errTruncated
97 }
98 flags = binary.LittleEndian.Uint32(in[i:])
99 i += 4
100 flags_left = 32
101 }
102 flags_left--
103 if flags&(uint32(1)<<flags_left) == 0 {
104 if i >= len(in) {
105 return out[:base], errTruncated
106 }
107 out = append(out, in[i])
108 i++
109 if len(out)-base > MaxSize {
110 return out[:base], errTooLarge
111 }
112 continue
113 }
114 // A set flag with no input left is the end-of-data marker.
115 if i >= len(in) {
116 return out, nil
117 }
118 if len(in)-i < 2 {
119 return out[:base], errTruncated
120 }
121 mb := binary.LittleEndian.Uint16(in[i:])
122 i += 2
123 moff := int(mb>>3) + 1
124 mlen := int(mb&7) + 3
125
126 if mb&7 == 7 {
127 var nib int
128 if pending_len == -1 {
129 if i >= len(in) {
130 return out[:base], errTruncated
131 }
132 nib = int(in[i] & 0x0F)
133 pending_len = i
134 i++
135 } else {
136 nib = int(in[pending_len] >> 4)
137 pending_len = -1
138 }
139 if nib == 15 {
140 v := int64(0)
141 if i >= len(in) {
142 return out[:base], errTruncated
143 }
144 v = int64(in[i])
145 i++
146 if v == 255 {
147 if len(in)-i < 2 {
148 return out[:base], errTruncated
149 }
150 v = int64(binary.LittleEndian.Uint16(in[i:]))
151 i += 2
152 if v == 0 {
153 if len(in)-i < 4 {
154 return out[:base], errTruncated
155 }
156 v = int64(binary.LittleEndian.Uint32(in[i:]))
157 i += 4
158 }
159 if v < 15+7 {
160 return out[:base], errCorrupt
161 }
162 if v > MaxSize {
163 return out[:base], errTooLarge
164 }
165 mlen = int(v) + 3
166 } else {
167 mlen = int(v) + 25
168 }
169 } else {
170 mlen = nib + 10
171 }
172 }
173
174 if mlen > MaxSize-(len(out)-base) {
175 return out[:base], errTooLarge
176 }
177 // Matches may only reference output produced by this call, never
178 // the caller-provided prefix.
179 if moff > len(out)-base {
180 return out[:base], errCorrupt
181 }
182 for j := 0; j < mlen; j++ {
183 out = append(out, out[len(out)-moff])
184 }
185 }
186 }
187
188 // AppendHDecompressed appends decompressed_size bytes of an XPRESS
189 // LZ77+Huffman stream to out and returns the extended slice. The
190 // uncompressed size must be known in advance. On error, out is returned
191 // unmodified.
192 func AppendHDecompressed(out, in []byte, decompressed_size int) ([]byte, error) {
193 base := len(out)
194 if decompressed_size < 0 || decompressed_size > MaxSize {
195 return out, errTooLarge
196 }
197
198 if len(in) < 256 {
199 return out, errTruncated
200 }
201
202 // 512 4-bit code lengths, even symbol in the low nibble.
203 lens := make([]uint16, xpressNumSymbols)
204 for l := range 256 {
205 lens[l*2] = uint16(in[l] & 0x0F)
206 lens[l*2+1] = uint16(in[l] >> 4)
207 }
208 i := 256
209
210 // Decode table filled in canonical (length, symbol) order.
211 table := make([]uint16, xpressTableSize)
212 entry := 0
213 for l := 1; l <= xpressTableBits; l++ {
214 for s := range xpressNumSymbols {
215 if lens[s] == uint16(l) {
216 for k := 1 << (xpressTableBits - l); k > 0; k-- {
217 if entry >= xpressTableSize {
218 // Oversubscribed codes: the table cannot hold
219 // this many entries.
220 return out, errInvalidCode
221 }
222 table[entry] = uint16(s)
223 entry++
224 }
225 }
226 }
227 }
228 if entry != xpressTableSize {
229 // The code lengths must form a complete prefix code.
230 return out, errInvalidCode
231 }
232
233 // Preload two LE16 words, most-significant bit first.
234 bits := uint32(0)
235 nbits := 0
236 for nbits < 32 {
237 if len(in)-i < 2 {
238 return out, errTruncated
239 }
240 bits |= uint32(binary.LittleEndian.Uint16(in[i:])) << (16 - nbits)
241 i += 2
242 nbits += 16
243 }
244
245 for len(out)-base < decompressed_size {
246 for nbits < 15 {
247 if len(in)-i < 2 {
248 return out[:base], errTruncated
249 }
250 bits |= uint32(binary.LittleEndian.Uint16(in[i:])) << (16 - nbits)
251 i += 2
252 nbits += 16
253 }
254 sym := int(table[(bits>>17)&0x7FFF])
255 clen := int(lens[sym])
256 bits <<= clen
257 nbits -= clen
258
259 if sym < 256 {
260 out = append(out, byte(sym))
261 continue
262 }
263
264 if sym == 256 {
265 // End of data; Microsoft decodes it as match(3, 1)
266 // mid-stream.
267 if len(out)-base == decompressed_size {
268 break
269 }
270 if len(out)-base == 0 || decompressed_size-(len(out)-base) < 3 {
271 return out[:base], errCorrupt
272 }
273 start := len(out) - 1
274 for j := range 3 {
275 out = append(out, out[start+j])
276 }
277 continue
278 }
279
280 hb := (sym - 256) / 16
281 mlen := (sym - 256) % 16
282 if mlen == 15 {
283 v := uint32(0)
284 if i >= len(in) {
285 return out[:base], errTruncated
286 }
287 v = uint32(in[i])
288 i++
289 if v == 255 {
290 if len(in)-i < 2 {
291 return out[:base], errTruncated
292 }
293 v = uint32(binary.LittleEndian.Uint16(in[i:]))
294 i += 2
295 if v == 0 {
296 if len(in)-i < 4 {
297 return out[:base], errTruncated
298 }
299 v = binary.LittleEndian.Uint32(in[i:])
300 i += 4
301 }
302 if v > MaxSize {
303 return out[:base], errTooLarge
304 }
305 mlen = int(v) + 3
306 } else {
307 mlen = int(v) + 18
308 }
309 } else {
310 mlen += 3
311 }
312
313 for nbits < hb {
314 if len(in)-i < 2 {
315 return out[:base], errTruncated
316 }
317 bits |= uint32(binary.LittleEndian.Uint16(in[i:])) << (16 - nbits)
318 i += 2
319 nbits += 16
320 }
321 moff := 0
322 if hb > 0 {
323 moff = int((bits >> (32 - hb)) & (uint32(1)<<hb - 1))
324 bits <<= hb
325 nbits -= hb
326 }
327 moff += 1 << hb
328
329 if moff > len(out)-base {
330 return out[:base], errCorrupt
331 }
332 if len(out)-base > MaxSize-mlen {
333 return out[:base], errTooLarge
334 }
335 start := len(out) - moff
336 for j := 0; j < mlen; j++ {
337 out = append(out, out[start+j])
338 }
339 }
340 return out, nil
341 }