| 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. |
| 4 |
|
| 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. |
| 8 |
// |
| 9 |
// In particular, it implements LZW as used by the GIF file format, which means |
| 10 |
// variable-width codes up to 12 bits and the first two non-literal codes are a |
| 11 |
// clear code and an EOF code. |
| 12 |
// |
| 13 |
// The TIFF file format uses a variant that increases the code width one code |
| 14 |
// early, as does PDF's LZWDecode filter by default. Decoding those requires |
| 15 |
// [Reader.SetAldusCompatible]. |
| 16 |
package lzw |
| 17 |
|
| 18 |
import ( |
| 19 |
"bufio" |
| 20 |
"encoding/binary" |
| 21 |
"errors" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
|
| 25 |
"github.com/klauspost/compress/internal/regmask" |
| 26 |
) |
| 27 |
|
| 28 |
// Order specifies the bit ordering in an LZW data stream. |
| 29 |
type Order int |
| 30 |
|
| 31 |
const ( |
| 32 |
// LSB means Least Significant Bits first, as used in the GIF file format. |
| 33 |
LSB Order = iota |
| 34 |
// MSB means Most Significant Bits first, as used in the TIFF and PDF |
| 35 |
// file formats. |
| 36 |
MSB |
| 37 |
) |
| 38 |
|
| 39 |
const ( |
| 40 |
maxWidth = 12 |
| 41 |
maxCodes = 1 << maxWidth |
| 42 |
codeMask = maxCodes - 1 |
| 43 |
decoderInvalidCode = 0xffff |
| 44 |
|
| 45 |
// reserve is the output space a single code may need: a maximal expansion |
| 46 |
// plus the overshoot of the eight-bytes-at-a-time stores. |
| 47 |
reserve = maxCodes + 8 |
| 48 |
// flushBuffer is the number of bytes decoded into output before it is |
| 49 |
// handed to the caller. |
| 50 |
flushBuffer = maxCodes |
| 51 |
// inputBuffer is the size of the window read from a readerAt source. |
| 52 |
inputBuffer = 4096 |
| 53 |
) |
| 54 |
|
| 55 |
var errInvalidCode = errors.New("lzw: invalid code") |
| 56 |
|
| 57 |
// A peeker is an input source that can be read without consuming, such as a |
| 58 |
// [bufio.Reader]. It lets the decoder read whole words at a time and consume |
| 59 |
// only the bytes the code stream needed. |
| 60 |
type peeker interface { |
| 61 |
Peek(int) ([]byte, error) |
| 62 |
Discard(int) (int, error) |
| 63 |
Buffered() int |
| 64 |
} |
| 65 |
|
| 66 |
// A readerAt is an input source that can be read ahead of without moving its |
| 67 |
// read position, such as a [bytes.Reader]. Its position is set to just after |
| 68 |
// the last consumed byte whenever decoding pauses. |
| 69 |
type readerAt interface { |
| 70 |
io.ReaderAt |
| 71 |
io.Seeker |
| 72 |
} |
| 73 |
|
| 74 |
// Reader is an [io.Reader] which can be used to read compressed data in the |
| 75 |
// LZW format. |
| 76 |
type Reader struct { |
| 77 |
bits uint64 |
| 78 |
nBits uint |
| 79 |
width uint |
| 80 |
litWidth uint // width in bits of literal codes |
| 81 |
order Order |
| 82 |
aldus bool // decode the Aldus variant, see SetAldusCompatible |
| 83 |
err error |
| 84 |
|
| 85 |
// Exactly one of pk, at and br reads the compressed stream, see init. |
| 86 |
// buf is the window of input read but not yet consumed, pos the read |
| 87 |
// offset into it and off the position of buf[0] within at. |
| 88 |
pk peeker |
| 89 |
at readerAt |
| 90 |
br io.ByteReader |
| 91 |
buf []byte |
| 92 |
pos int |
| 93 |
off int64 |
| 94 |
own []byte // window read from at |
| 95 |
b1 [1]byte |
| 96 |
srcErr error |
| 97 |
|
| 98 |
// The first 1<<litWidth codes are literal codes. |
| 99 |
// The next two codes mean clear and EOF. |
| 100 |
// Other valid codes are in the range [lo, hi] where lo := clear + 2, |
| 101 |
// with the upper bound incrementing on each code seen. |
| 102 |
// |
| 103 |
// last is the most recently seen code, or decoderInvalidCode, lastLen is |
| 104 |
// the length of its expansion and lastF8 its first8 entry. |
| 105 |
// |
| 106 |
// An invariant is that hi < 1<<width. |
| 107 |
clear, eof, hi, last uint16 |
| 108 |
lastLen int |
| 109 |
lastF8 uint64 |
| 110 |
|
| 111 |
// link describes each literal code and each code c in [lo, hi]: bits 0-7 |
| 112 |
// hold the final byte of its expansion, bits 8-19 the length of its |
| 113 |
// expansion minus one and bits 20-31 the code for all but the final byte. |
| 114 |
// That code is either a literal code or another code in [lo, c). |
| 115 |
link [maxCodes]uint32 |
| 116 |
// first8 holds the first eight bytes of the expansion of each of those |
| 117 |
// codes, in output order, zero padded. Codes expanding to eight bytes or |
| 118 |
// fewer are written to the output with a single store from here, so the |
| 119 |
// link chain only has to be walked for the bytes past the first eight. |
| 120 |
first8 [maxCodes]uint64 |
| 121 |
|
| 122 |
// output is the temporary output buffer, used when Read is called with a |
| 123 |
// buffer too small to decode into directly. It is flushed once it holds |
| 124 |
// flushBuffer bytes, so that there is always room to decode an entire code. |
| 125 |
output [flushBuffer + reserve]byte |
| 126 |
toRead []byte // bytes to return from Read |
| 127 |
} |
| 128 |
|
| 129 |
// Read implements io.Reader, reading uncompressed bytes from its underlying reader. |
| 130 |
func (r *Reader) Read(b []byte) (int, error) { |
| 131 |
// Decoding stops a whole maximal expansion short of the end of the buffer, |
| 132 |
// so returning after one pass would hand back all but the last few KB of a |
| 133 |
// large read. Keep going instead, and go short only once the stream itself |
| 134 |
// has ended: a caller that does not loop then fails loudly rather than |
| 135 |
// quietly losing the tail of every read. |
| 136 |
n := 0 |
| 137 |
for n < len(b) { |
| 138 |
if len(r.toRead) > 0 { |
| 139 |
m := copy(b[n:], r.toRead) |
| 140 |
r.toRead, n = r.toRead[m:], n+m |
| 141 |
continue |
| 142 |
} |
| 143 |
if r.err != nil { |
| 144 |
break |
| 145 |
} |
| 146 |
if len(b)-n >= 2*maxCodes { |
| 147 |
// There is room to expand any code, so skip the output buffer. |
| 148 |
n += r.decodeAny(b[n:]) |
| 149 |
continue |
| 150 |
} |
| 151 |
r.toRead = r.output[:r.decodeAny(r.output[:])] |
| 152 |
} |
| 153 |
if n > 0 || len(r.toRead) > 0 { |
| 154 |
return n, nil |
| 155 |
} |
| 156 |
return 0, r.err |
| 157 |
} |
| 158 |
|
| 159 |
func (r *Reader) decodeAny(dst []byte) int { |
| 160 |
if r.aldus { |
| 161 |
return r.decodeAldus(dst) |
| 162 |
} |
| 163 |
return r.decode(dst) |
| 164 |
} |
| 165 |
|
| 166 |
// decodeAldus decompresses the Aldus variant of the format, in which the code |
| 167 |
// width grows one code early, and is otherwise like decode. |
| 168 |
// |
| 169 |
// It is a separate function, and a plain walk of the link chain rather than a |
| 170 |
// copy of decode's machinery, for one reason: adding the variant to decode's |
| 171 |
// loop cost the common path around 7% on MSB streams. The flag becomes another |
| 172 |
// value the loop has to keep live, and it has no registers to spare. Keeping the |
| 173 |
// two apart leaves decode compiling to exactly what it did before, and TIFF and |
| 174 |
// PDF streams have no need of its speed. |
| 175 |
func (r *Reader) decodeAldus(dst []byte) int { |
| 176 |
limit := len(dst) - reserve |
| 177 |
bits := r.bits |
| 178 |
msb := r.order == MSB |
| 179 |
mask := uint16(1)<<r.width - 1 |
| 180 |
o := 0 |
| 181 |
|
| 182 |
loop: |
| 183 |
for { |
| 184 |
if r.nBits < r.width { |
| 185 |
if r.pos+8 <= len(r.buf) { |
| 186 |
if msb { |
| 187 |
bits |= binary.BigEndian.Uint64(r.buf[r.pos:]) >> (r.nBits & regmask.Shift64ByUint) |
| 188 |
} else { |
| 189 |
bits |= binary.LittleEndian.Uint64(r.buf[r.pos:]) << (r.nBits & regmask.Shift64ByUint) |
| 190 |
} |
| 191 |
r.pos += int(63-r.nBits) >> 3 |
| 192 |
r.nBits |= 56 |
| 193 |
} else { |
| 194 |
var ok bool |
| 195 |
bits, r.nBits, ok = r.fillSlow(bits, r.nBits, r.width) |
| 196 |
if !ok { |
| 197 |
break loop |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
var code uint16 |
| 202 |
if msb { |
| 203 |
code = uint16(bits >> ((64 - r.width) & regmask.Shift64ByUint)) |
| 204 |
bits <<= r.width & regmask.Shift64ByUint |
| 205 |
} else { |
| 206 |
code = uint16(bits) & mask |
| 207 |
bits >>= r.width & regmask.Shift64ByUint |
| 208 |
} |
| 209 |
r.nBits -= r.width |
| 210 |
|
| 211 |
if code-r.clear <= 1 { |
| 212 |
if code != r.clear { |
| 213 |
r.err = io.EOF |
| 214 |
break loop |
| 215 |
} |
| 216 |
r.width = 1 + r.litWidth |
| 217 |
mask = uint16(1)<<r.width - 1 |
| 218 |
r.hi, r.last, r.lastLen = r.clear+1, decoderInvalidCode, 0 |
| 219 |
continue |
| 220 |
} else if code > r.hi { |
| 221 |
r.err = errInvalidCode |
| 222 |
break loop |
| 223 |
} |
| 224 |
|
| 225 |
// Write the expansion right to left, ending at its first byte. |
| 226 |
c, n := code, 0 |
| 227 |
kwkwk := code == r.hi && r.last != decoderInvalidCode |
| 228 |
if kwkwk { |
| 229 |
// code == hi expands to the last expansion followed by its head. |
| 230 |
c, n = r.last, r.lastLen+1 |
| 231 |
} else { |
| 232 |
n = int(r.link[code&codeMask]>>8&codeMask) + 1 |
| 233 |
} |
| 234 |
out := dst[o : o+n] |
| 235 |
i := n |
| 236 |
if kwkwk { |
| 237 |
h := r.last |
| 238 |
for h >= r.clear { |
| 239 |
h = uint16(r.link[h&codeMask] >> 20) |
| 240 |
} |
| 241 |
i-- |
| 242 |
out[i] = uint8(h) |
| 243 |
} |
| 244 |
for c >= r.clear { |
| 245 |
e := r.link[c&codeMask] |
| 246 |
i-- |
| 247 |
out[i] = uint8(e) |
| 248 |
c = uint16(e >> 20) |
| 249 |
} |
| 250 |
out[0] = uint8(c) |
| 251 |
o += n |
| 252 |
|
| 253 |
if r.last != decoderInvalidCode { |
| 254 |
// Save what the hi code expands to. first8 is not maintained: this |
| 255 |
// decoder never reads it, and a Reset redefines every entry before |
| 256 |
// any of them can be read again. |
| 257 |
r.link[r.hi&codeMask] = uint32(c) | uint32(r.lastLen)<<8 | uint32(r.last)<<20 |
| 258 |
} |
| 259 |
r.last, r.lastLen = code, n |
| 260 |
r.hi++ |
| 261 |
if r.hi >= mask { // one code earlier than decode |
| 262 |
if r.width == maxWidth { |
| 263 |
r.last = decoderInvalidCode |
| 264 |
r.hi-- |
| 265 |
} else { |
| 266 |
r.width++ |
| 267 |
mask = mask<<1 | 1 |
| 268 |
} |
| 269 |
} |
| 270 |
if o >= limit { |
| 271 |
break |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
r.bits = bits |
| 276 |
r.release() |
| 277 |
return o |
| 278 |
} |
| 279 |
|
| 280 |
// decode decompresses bytes from r into dst and returns the number of bytes |
| 281 |
// written. len(dst) must be greater than reserve. |
| 282 |
func (r *Reader) decode(dst []byte) int { |
| 283 |
limit := len(dst) - reserve |
| 284 |
// Only the bit accumulator and the byte order are worth a local. The loop |
| 285 |
// needs too many values live at once for the rest to stay in registers too, |
| 286 |
// and the spill code the compiler then emits costs more than reading them |
| 287 |
// back out of r: hoisting all of them is around 15% slower on text. |
| 288 |
bits := r.bits |
| 289 |
msb := r.order == MSB |
| 290 |
// mask holds 1<<width - 1. Keeping it rather than deriving it from the width |
| 291 |
// each time round the loop saves both the shift and the guard the compiler |
| 292 |
// would need against it overshifting. Every shift below is masked for the |
| 293 |
// same reason: all of them are provably shorter than a word. It also stands |
| 294 |
// in for the code at which hi overflows the width, which is mask+1. |
| 295 |
mask := uint16(1)<<r.width - 1 |
| 296 |
o := 0 |
| 297 |
|
| 298 |
loop: |
| 299 |
for { |
| 300 |
if r.nBits < r.width { |
| 301 |
if r.pos+8 <= len(r.buf) { |
| 302 |
if msb { |
| 303 |
bits |= binary.BigEndian.Uint64(r.buf[r.pos:]) >> (r.nBits & regmask.Shift64ByUint) |
| 304 |
} else { |
| 305 |
bits |= binary.LittleEndian.Uint64(r.buf[r.pos:]) << (r.nBits & regmask.Shift64ByUint) |
| 306 |
} |
| 307 |
r.pos += int(63-r.nBits) >> 3 |
| 308 |
r.nBits |= 56 |
| 309 |
} else { |
| 310 |
var ok bool |
| 311 |
bits, r.nBits, ok = r.fillSlow(bits, r.nBits, r.width) |
| 312 |
if !ok { |
| 313 |
break loop |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
var code uint16 |
| 318 |
if msb { |
| 319 |
code = uint16(bits >> ((64 - r.width) & regmask.Shift64ByUint)) |
| 320 |
bits <<= r.width & regmask.Shift64ByUint |
| 321 |
} else { |
| 322 |
code = uint16(bits) & mask |
| 323 |
bits >>= r.width & regmask.Shift64ByUint |
| 324 |
} |
| 325 |
r.nBits -= r.width |
| 326 |
|
| 327 |
// f8 is the first8 entry of code, that is the first eight bytes of its |
| 328 |
// expansion, and n is the length of the expansion. Literal codes have |
| 329 |
// entries of their own, so that they need no test of their own here: |
| 330 |
// only the clear and EOF codes are singled out, with a test that is |
| 331 |
// almost always false and so costs next to nothing. |
| 332 |
var f8 uint64 |
| 333 |
var n int |
| 334 |
if code-r.clear <= 1 { |
| 335 |
if code != r.clear { |
| 336 |
r.err = io.EOF |
| 337 |
break loop |
| 338 |
} |
| 339 |
r.width = 1 + r.litWidth |
| 340 |
mask = uint16(1)<<r.width - 1 |
| 341 |
r.hi, r.last, r.lastLen = r.clear+1, decoderInvalidCode, 0 |
| 342 |
continue |
| 343 |
} else if code > r.hi { |
| 344 |
r.err = errInvalidCode |
| 345 |
break loop |
| 346 |
} else if code == r.hi && r.last != decoderInvalidCode { |
| 347 |
// code == hi is a special case which expands to the last expansion |
| 348 |
// followed by the head of the last expansion. |
| 349 |
n = r.lastLen + 1 |
| 350 |
if n <= 8 { |
| 351 |
f8 = r.lastF8 | (r.lastF8&0xff)<<(8*uint(r.lastLen)) |
| 352 |
binary.LittleEndian.PutUint64(dst[o:], f8) |
| 353 |
} else if o >= r.lastLen { |
| 354 |
// The last expansion is still right there in the output. |
| 355 |
copy(dst[o:o+r.lastLen], dst[o-r.lastLen:o]) |
| 356 |
dst[o+r.lastLen] = uint8(r.lastF8) |
| 357 |
f8 = r.lastF8 |
| 358 |
} else { |
| 359 |
out := dst[o : o+n] |
| 360 |
out[n-1] = uint8(r.lastF8) |
| 361 |
e := r.link[r.last&codeMask] |
| 362 |
for i := n - 1; i > 8; i-- { |
| 363 |
out[i-1] = uint8(e) |
| 364 |
e = r.link[e>>20&codeMask] |
| 365 |
} |
| 366 |
f8 = r.lastF8 |
| 367 |
binary.LittleEndian.PutUint64(out, f8) |
| 368 |
} |
| 369 |
} else { |
| 370 |
e := r.link[code&codeMask] |
| 371 |
n = int(e>>8&codeMask) + 1 |
| 372 |
f8 = r.first8[code&codeMask] |
| 373 |
if n <= 8 { |
| 374 |
binary.LittleEndian.PutUint64(dst[o:], f8) |
| 375 |
} else if e>>20 == uint32(r.last) && o >= r.lastLen { |
| 376 |
// This code extends the last one by a byte, and the last |
| 377 |
// expansion is still right there in the output. |
| 378 |
copy(dst[o:o+r.lastLen], dst[o-r.lastLen:o]) |
| 379 |
dst[o+r.lastLen] = uint8(e) |
| 380 |
} else { |
| 381 |
// Walk the link chain writing suffixes right to left, until |
| 382 |
// only the first eight bytes are left to write. |
| 383 |
out := dst[o : o+n] |
| 384 |
for i := n; i > 8; i-- { |
| 385 |
out[i-1] = uint8(e) |
| 386 |
e = r.link[e>>20&codeMask] |
| 387 |
} |
| 388 |
binary.LittleEndian.PutUint64(out, f8) |
| 389 |
} |
| 390 |
} |
| 391 |
o += n |
| 392 |
|
| 393 |
if r.last != decoderInvalidCode { |
| 394 |
// Save what the hi code expands to: the last expansion followed by |
| 395 |
// the first byte of this one. |
| 396 |
r.link[r.hi&codeMask] = uint32(f8&0xff) | uint32(r.lastLen)<<8 | uint32(r.last)<<20 |
| 397 |
r.first8[r.hi&codeMask] = r.lastF8 | (f8&0xff)<<(8*uint(r.lastLen)) |
| 398 |
} |
| 399 |
r.last, r.lastLen, r.lastF8 = code, n, f8 |
| 400 |
r.hi++ |
| 401 |
if r.hi > mask { |
| 402 |
if r.width == maxWidth { |
| 403 |
r.last = decoderInvalidCode |
| 404 |
// Undo the hi++ a few lines above, so that (1) we maintain |
| 405 |
// the invariant that hi < 1<<width, and (2) hi does not |
| 406 |
// eventually overflow a uint16. |
| 407 |
r.hi-- |
| 408 |
} else { |
| 409 |
r.width++ |
| 410 |
mask = mask<<1 | 1 |
| 411 |
} |
| 412 |
} |
| 413 |
if o >= limit { |
| 414 |
break |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
r.bits = bits |
| 419 |
r.release() |
| 420 |
return o |
| 421 |
} |
| 422 |
|
| 423 |
// Reading whole words at a time pulls in bytes whose bits have not been |
| 424 |
// consumed yet. They are still held in the bit accumulator, so nBits/8 of the |
| 425 |
// pos bytes taken from the window count as unread. An invariant of the peeker |
| 426 |
// path, which cannot hand bytes back once they are discarded, is that |
| 427 |
// pos >= nBits/8. |
| 428 |
|
| 429 |
// fillSlow tops up bits from the tail of the input window, refilling it as |
| 430 |
// needed. It reports false and sets r.err if the stream ended. |
| 431 |
func (r *Reader) fillSlow(bits uint64, nBits, width uint) (uint64, uint, bool) { |
| 432 |
for nBits < width { |
| 433 |
if r.pos >= len(r.buf) && !r.refill(nBits) { |
| 434 |
err := r.srcErr |
| 435 |
if err == nil || err == io.EOF { |
| 436 |
err = io.ErrUnexpectedEOF |
| 437 |
} |
| 438 |
r.err = err |
| 439 |
return bits, nBits, false |
| 440 |
} |
| 441 |
x := r.buf[r.pos] |
| 442 |
r.pos++ |
| 443 |
if r.order == MSB { |
| 444 |
bits |= uint64(x) << ((56 - nBits) & regmask.Shift64ByUint) |
| 445 |
} else { |
| 446 |
bits |= uint64(x) << (nBits & regmask.Shift64ByUint) |
| 447 |
} |
| 448 |
nBits += 8 |
| 449 |
} |
| 450 |
return bits, nBits, true |
| 451 |
} |
| 452 |
|
| 453 |
// refill replaces the input window with one starting at the first byte that has |
| 454 |
// not been consumed. It reports whether the new window holds a byte that has |
| 455 |
// not been read yet, recording the reason in r.srcErr if not. |
| 456 |
func (r *Reader) refill(nBits uint) bool { |
| 457 |
switch { |
| 458 |
case r.pk != nil: |
| 459 |
skip := int(nBits) / 8 |
| 460 |
if d := r.pos - skip; d > 0 { |
| 461 |
r.pk.Discard(d) |
| 462 |
} |
| 463 |
r.pos = skip |
| 464 |
// Never ask for more than is already buffered plus the one byte we |
| 465 |
// need, so that we block no longer than a ReadByte would have. |
| 466 |
n := r.pk.Buffered() |
| 467 |
if n <= skip { |
| 468 |
n = skip + 1 |
| 469 |
} |
| 470 |
var err error |
| 471 |
r.buf, err = r.pk.Peek(n) |
| 472 |
if len(r.buf) <= skip { |
| 473 |
r.srcErr = err |
| 474 |
return false |
| 475 |
} |
| 476 |
case r.at != nil: |
| 477 |
if r.own == nil { |
| 478 |
r.own = make([]byte, inputBuffer) |
| 479 |
} |
| 480 |
// Reading at an offset leaves the source's position alone, so the |
| 481 |
// window can start past the bytes still held in the accumulator. |
| 482 |
r.off += int64(r.pos) |
| 483 |
r.pos = 0 |
| 484 |
n, err := r.at.ReadAt(r.own, r.off) |
| 485 |
r.buf = r.own[:n] |
| 486 |
if n == 0 { |
| 487 |
r.srcErr = err |
| 488 |
return false |
| 489 |
} |
| 490 |
default: |
| 491 |
// Nothing may be read ahead, so the accumulator never holds a whole |
| 492 |
// unconsumed byte here. |
| 493 |
x, err := r.br.ReadByte() |
| 494 |
if err != nil { |
| 495 |
r.srcErr = err |
| 496 |
return false |
| 497 |
} |
| 498 |
r.b1[0] = x |
| 499 |
r.buf, r.pos = r.b1[:], 0 |
| 500 |
} |
| 501 |
return true |
| 502 |
} |
| 503 |
|
| 504 |
// release leaves the source positioned just after the last consumed byte, so |
| 505 |
// that a caller reading whatever follows the compressed stream, as image/gif |
| 506 |
// does, sees the same thing it would from a byte at a time decoder. |
| 507 |
func (r *Reader) release() { |
| 508 |
skip := int(r.nBits) / 8 |
| 509 |
if r.srcErr != nil { |
| 510 |
// The source is exhausted; a byte at a time decoder would have |
| 511 |
// consumed the trailing bytes it could not turn into a code too. |
| 512 |
skip = 0 |
| 513 |
} |
| 514 |
switch { |
| 515 |
case r.pk != nil: |
| 516 |
if d := r.pos - skip; d > 0 { |
| 517 |
r.pk.Discard(d) |
| 518 |
} |
| 519 |
r.buf, r.pos = nil, skip |
| 520 |
case r.at != nil: |
| 521 |
r.at.Seek(r.off+int64(r.pos-skip), io.SeekStart) |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
var errClosed = errors.New("lzw: reader/writer is closed") |
| 526 |
|
| 527 |
// Close closes the [Reader] and returns an error for any future read operation. |
| 528 |
// It does not close the underlying [io.Reader]. |
| 529 |
func (r *Reader) Close() error { |
| 530 |
r.err = errClosed // in case any Reads come along |
| 531 |
return nil |
| 532 |
} |
| 533 |
|
| 534 |
// SetAldusCompatible selects the variant of LZW that Aldus implemented for the |
| 535 |
// TIFF file format, which increases the code width one code early: the "off by |
| 536 |
// one" difference from the TIFF 5.0 specification that libtiff documents and |
| 537 |
// keeps for compatibility. The same variant is what PDF's LZWDecode filter |
| 538 |
// specifies by default, through its /EarlyChange value of 1. |
| 539 |
// |
| 540 |
// Set it before the first Read. It is configuration rather than stream state, so |
| 541 |
// it survives [Reader.Reset], including for a pooled [Reader]. |
| 542 |
// |
| 543 |
// Leaving it off costs nothing: the two variants are decoded by separate code. |
| 544 |
func (r *Reader) SetAldusCompatible(b bool) { |
| 545 |
r.aldus = b |
| 546 |
} |
| 547 |
|
| 548 |
// Reset clears the [Reader]'s state and allows it to be reused again |
| 549 |
// as a new [Reader]. It does not change whether the [Reader] is Aldus |
| 550 |
// compatible. |
| 551 |
func (r *Reader) Reset(src io.Reader, order Order, litWidth int) { |
| 552 |
r.bits, r.nBits = 0, 0 |
| 553 |
r.err, r.srcErr = nil, nil |
| 554 |
r.toRead = nil |
| 555 |
r.pk, r.at, r.br = nil, nil, nil |
| 556 |
r.buf, r.pos, r.off = nil, 0, 0 |
| 557 |
r.init(src, order, litWidth) |
| 558 |
} |
| 559 |
|
| 560 |
// NewReader creates a new [io.ReadCloser]. |
| 561 |
// Reads from the returned [io.ReadCloser] read and decompress data from r. |
| 562 |
// If r does not also implement [io.ByteReader], |
| 563 |
// the decompressor may read more data than necessary from r. |
| 564 |
// It is the caller's responsibility to call Close on the ReadCloser when |
| 565 |
// finished reading. |
| 566 |
// The number of bits to use for literal codes, litWidth, must be in the |
| 567 |
// range [2,8] and is typically 8. It must equal the litWidth |
| 568 |
// used during compression. |
| 569 |
// |
| 570 |
// It is guaranteed that the underlying type of the returned [io.ReadCloser] |
| 571 |
// is a *[Reader]. |
| 572 |
func NewReader(r io.Reader, order Order, litWidth int) io.ReadCloser { |
| 573 |
return newReader(r, order, litWidth) |
| 574 |
} |
| 575 |
|
| 576 |
func newReader(src io.Reader, order Order, litWidth int) *Reader { |
| 577 |
r := new(Reader) |
| 578 |
r.init(src, order, litWidth) |
| 579 |
return r |
| 580 |
} |
| 581 |
|
| 582 |
func (r *Reader) init(src io.Reader, order Order, litWidth int) { |
| 583 |
if order != LSB && order != MSB { |
| 584 |
r.err = errors.New("lzw: unknown order") |
| 585 |
return |
| 586 |
} |
| 587 |
if litWidth < 2 || 8 < litWidth { |
| 588 |
r.err = fmt.Errorf("lzw: litWidth %d out of range", litWidth) |
| 589 |
return |
| 590 |
} |
| 591 |
|
| 592 |
// Pick the cheapest way to read the stream that does not consume more of |
| 593 |
// src than the code stream needs. |
| 594 |
switch t := src.(type) { |
| 595 |
case peeker: |
| 596 |
r.pk = t |
| 597 |
case io.ByteReader: |
| 598 |
if at, ok := src.(readerAt); ok { |
| 599 |
if off, err := at.Seek(0, io.SeekCurrent); err == nil { |
| 600 |
r.at, r.off = at, off |
| 601 |
break |
| 602 |
} |
| 603 |
} |
| 604 |
r.br = t |
| 605 |
default: |
| 606 |
if src != nil { |
| 607 |
// Reading ahead of src is allowed, as it is not an io.ByteReader. |
| 608 |
r.pk = bufio.NewReader(src) |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
r.order = order |
| 613 |
lw := uint(litWidth) |
| 614 |
sameLit := r.litWidth == lw |
| 615 |
r.litWidth = lw |
| 616 |
r.width = 1 + lw |
| 617 |
r.clear = uint16(1) << lw |
| 618 |
r.eof, r.hi = r.clear+1, r.clear+1 |
| 619 |
r.last = decoderInvalidCode |
| 620 |
r.lastLen, r.lastF8 = 0, 0 |
| 621 |
// The literal entries only depend on the code, so a stream of the same |
| 622 |
// litWidth as the last one finds them already there: the codes a stream |
| 623 |
// defines start at clear+2, past its own literal range. A narrower stream |
| 624 |
// defines codes inside a wider one's literal range, though, so a change of |
| 625 |
// litWidth means redoing them. |
| 626 |
if !sameLit { |
| 627 |
for c := 0; c < int(r.clear); c++ { |
| 628 |
r.link[c] = uint32(c) |
| 629 |
r.first8[c] = uint64(c) |
| 630 |
} |
| 631 |
} |
| 632 |
} |