1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package lzw implements the Lempel-Ziv-Welch compressed data format,
6 // described in T. A. Welch, ``A Technique for High-Performance Data
7 // Compression'', Computer, 17(6) (June 1984), pp 8-19.
9 // In particular, it implements LZW as used by the GIF, TIFF and PDF file
10 // formats, which means variable-width codes up to 12 bits and the first
11 // two non-literal codes are a clear code and an EOF code.
14 // TODO(nigeltao): check that TIFF and PDF use LZW in the same way as GIF,
15 // modulo LSB/MSB packing order.
24 // Order specifies the bit ordering in an LZW data stream.
28 // LSB means Least Significant Bits first, as used in the GIF file format.
30 // MSB means Most Significant Bits first, as used in the TIFF and PDF
37 decoderInvalidCode
= 0xffff
38 flushBuffer
= 1 << maxWidth
41 // decoder is the state from which the readXxx method converts a byte
42 // stream into a code stream.
48 read
func(*decoder
) (uint16, error
) // readLSB or readMSB
49 litWidth
int // width in bits of literal codes
52 // The first 1<<litWidth codes are literal codes.
53 // The next two codes mean clear and EOF.
54 // Other valid codes are in the range [lo, hi] where lo := clear + 2,
55 // with the upper bound incrementing on each code seen.
56 // overflow is the code at which hi overflows the code width.
57 // last is the most recently seen code, or decoderInvalidCode.
58 clear
, eof
, hi
, overflow
, last
uint16
60 // Each code c in [lo, hi] expands to two or more bytes. For c != hi:
61 // suffix[c] is the last of these bytes.
62 // prefix[c] is the code for all but the last byte.
63 // This code can either be a literal code or another code in [lo, c).
64 // The c == hi case is a special case.
65 suffix
[1 << maxWidth
]uint8
66 prefix
[1 << maxWidth
]uint16
68 // output is the temporary output buffer.
69 // Literal codes are accumulated from the start of the buffer.
70 // Non-literal codes decode to a sequence of suffixes that are first
71 // written right-to-left from the end of the buffer before being copied
72 // to the start of the buffer.
73 // It is flushed when it contains >= 1<<maxWidth bytes,
74 // so that there is always room to decode an entire code.
75 output
[2 * 1 << maxWidth
]byte
76 o
int // write index into output
77 toRead
[]byte // bytes to return from Read
80 // readLSB returns the next code for "Least Significant Bits first" data.
81 func (d
*decoder
) readLSB() (uint16, error
) {
82 for d
.nBits
< d
.width
{
83 x
, err
:= d
.r
.ReadByte()
87 d
.bits |
= uint32(x
) << d
.nBits
90 code
:= uint16(d
.bits
& (1<<d
.width
- 1))
96 // readMSB returns the next code for "Most Significant Bits first" data.
97 func (d
*decoder
) readMSB() (uint16, error
) {
98 for d
.nBits
< d
.width
{
99 x
, err
:= d
.r
.ReadByte()
103 d
.bits |
= uint32(x
) << (24 - d
.nBits
)
106 code
:= uint16(d
.bits
>> (32 - d
.width
))
112 func (d
*decoder
) Read(b
[]byte) (int, error
) {
114 if len(d
.toRead
) > 0 {
115 n
:= copy(b
, d
.toRead
)
116 d
.toRead
= d
.toRead
[n
:]
126 // decode decompresses bytes from r and leaves them in d.toRead.
127 // read specifies how to decode bytes into codes.
128 // litWidth is the width in bits of literal codes.
129 func (d
*decoder
) decode() {
130 // Loop over the code stream, converting codes into decompressed bytes.
132 code
, err
:= d
.read(d
)
135 err
= io
.ErrUnexpectedEOF
142 // We have a literal code.
143 d
.output
[d
.o
] = uint8(code
)
145 if d
.last
!= decoderInvalidCode
{
146 // Save what the hi code expands to.
147 d
.suffix
[d
.hi
] = uint8(code
)
148 d
.prefix
[d
.hi
] = d
.last
150 case code
== d
.clear
:
151 d
.width
= 1 + uint(d
.litWidth
)
153 d
.overflow
= 1 << d
.width
154 d
.last
= decoderInvalidCode
161 c
, i
:= code
, len(d
.output
)-1
163 // code == hi is a special case which expands to the last expansion
164 // followed by the head of the last expansion. To find the head, we walk
165 // the prefix chain until we find a literal code.
170 d
.output
[i
] = uint8(c
)
174 // Copy the suffix chain into output and then write that to w.
176 d
.output
[i
] = d
.suffix
[c
]
180 d
.output
[i
] = uint8(c
)
181 d
.o
+= copy(d
.output
[d
.o
:], d
.output
[i
:])
182 if d
.last
!= decoderInvalidCode
{
183 // Save what the hi code expands to.
184 d
.suffix
[d
.hi
] = uint8(c
)
185 d
.prefix
[d
.hi
] = d
.last
188 d
.err
= errors
.New("lzw: invalid code")
191 d
.last
, d
.hi
= code
, d
.hi
+1
192 if d
.hi
>= d
.overflow
{
193 if d
.width
== maxWidth
{
194 d
.last
= decoderInvalidCode
200 if d
.o
>= flushBuffer
{
207 func (d
*decoder
) flush() {
208 d
.toRead
= d
.output
[:d
.o
]
212 var errClosed
= errors
.New("compress/lzw: reader/writer is closed")
214 func (d
*decoder
) Close() error
{
215 d
.err
= errClosed
// in case any Reads come along
219 // NewReader creates a new io.ReadCloser that satisfies reads by decompressing
220 // the data read from r.
221 // It is the caller's responsibility to call Close on the ReadCloser when
223 // The number of bits to use for literal codes, litWidth, must be in the
224 // range [2,8] and is typically 8.
225 func NewReader(r io
.Reader
, order Order
, litWidth
int) io
.ReadCloser
{
229 d
.read
= (*decoder
).readLSB
231 d
.read
= (*decoder
).readMSB
233 d
.err
= errors
.New("lzw: unknown order")
236 if litWidth
< 2 ||
8 < litWidth
{
237 d
.err
= fmt
.Errorf("lzw: litWidth %d out of range", litWidth
)
240 if br
, ok
:= r
.(io
.ByteReader
); ok
{
243 d
.r
= bufio
.NewReader(r
)
245 d
.litWidth
= litWidth
246 d
.width
= 1 + uint(litWidth
)
247 d
.clear
= uint16(1) << uint(litWidth
)
248 d
.eof
, d
.hi
= d
.clear
+1, d
.clear
+1
249 d
.overflow
= uint16(1) << d
.width
250 d
.last
= decoderInvalidCode