| 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 |
// This file is a copy of golang.org/x/image/tiff/lzw/reader.go, taken at |
| 6 |
// v0.41.0 and used only as an independent reference for the Aldus variant that |
| 7 |
// Reader.SetAldusCompatible selects. It is deliberately left as close to |
| 8 |
// upstream as possible: only the package clause and the doc comment differ, so |
| 9 |
// that a future upstream change can be diffed against it. |
| 10 |
// |
| 11 |
// It lives in an external test package so that its identifiers cannot collide |
| 12 |
// with the ones being tested. |
| 13 |
|
| 14 |
// Package lzw implements the Lempel-Ziv-Welch compressed data format, |
| 15 |
// described in T. A. Welch, “A Technique for High-Performance Data |
| 16 |
// Compression”, Computer, 17(6) (June 1984), pp 8-19. |
| 17 |
// |
| 18 |
// Upstream doc: it implements LZW as used by the TIFF file format, including |
| 19 |
// an "off by one" algorithmic difference when compared to standard LZW. |
| 20 |
package lzw_test |
| 21 |
|
| 22 |
/* |
| 23 |
This file was branched from src/pkg/compress/lzw/reader.go in the |
| 24 |
standard library. Differences from the original are marked with "NOTE". |
| 25 |
|
| 26 |
The tif_lzw.c file in the libtiff C library has this comment: |
| 27 |
|
| 28 |
---- |
| 29 |
The 5.0 spec describes a different algorithm than Aldus |
| 30 |
implements. Specifically, Aldus does code length transitions |
| 31 |
one code earlier than should be done (for real LZW). |
| 32 |
Earlier versions of this library implemented the correct |
| 33 |
LZW algorithm, but emitted codes in a bit order opposite |
| 34 |
to the TIFF spec. Thus, to maintain compatibility w/ Aldus |
| 35 |
we interpret MSB-LSB ordered codes to be images written w/ |
| 36 |
old versions of this library, but otherwise adhere to the |
| 37 |
Aldus "off by one" algorithm. |
| 38 |
---- |
| 39 |
|
| 40 |
The Go code doesn't read (invalid) TIFF files written by old versions of |
| 41 |
libtiff, but the LZW algorithm in this package still differs from the one in |
| 42 |
Go's standard package library to accommodate this "off by one" in valid TIFFs. |
| 43 |
*/ |
| 44 |
|
| 45 |
import ( |
| 46 |
"bufio" |
| 47 |
"errors" |
| 48 |
"fmt" |
| 49 |
"io" |
| 50 |
) |
| 51 |
|
| 52 |
// Order specifies the bit ordering in an LZW data stream. |
| 53 |
type Order int |
| 54 |
|
| 55 |
const ( |
| 56 |
// LSB means Least Significant Bits first, as used in the GIF file format. |
| 57 |
LSB Order = iota |
| 58 |
// MSB means Most Significant Bits first, as used in the TIFF and PDF |
| 59 |
// file formats. |
| 60 |
MSB |
| 61 |
) |
| 62 |
|
| 63 |
const ( |
| 64 |
maxWidth = 12 |
| 65 |
decoderInvalidCode = 0xffff |
| 66 |
flushBuffer = 1 << maxWidth |
| 67 |
) |
| 68 |
|
| 69 |
// decoder is the state from which the readXxx method converts a byte |
| 70 |
// stream into a code stream. |
| 71 |
type decoder struct { |
| 72 |
r io.ByteReader |
| 73 |
bits uint32 |
| 74 |
nBits uint |
| 75 |
width uint |
| 76 |
read func(*decoder) (uint16, error) // readLSB or readMSB |
| 77 |
litWidth int // width in bits of literal codes |
| 78 |
err error |
| 79 |
|
| 80 |
// The first 1<<litWidth codes are literal codes. |
| 81 |
// The next two codes mean clear and EOF. |
| 82 |
// Other valid codes are in the range [lo, hi] where lo := clear + 2, |
| 83 |
// with the upper bound incrementing on each code seen. |
| 84 |
// overflow is the code at which hi overflows the code width. NOTE: TIFF's LZW is "off by one". |
| 85 |
// last is the most recently seen code, or decoderInvalidCode. |
| 86 |
clear, eof, hi, overflow, last uint16 |
| 87 |
|
| 88 |
// Each code c in [lo, hi] expands to two or more bytes. For c != hi: |
| 89 |
// suffix[c] is the last of these bytes. |
| 90 |
// prefix[c] is the code for all but the last byte. |
| 91 |
// This code can either be a literal code or another code in [lo, c). |
| 92 |
// The c == hi case is a special case. |
| 93 |
suffix [1 << maxWidth]uint8 |
| 94 |
prefix [1 << maxWidth]uint16 |
| 95 |
|
| 96 |
// output is the temporary output buffer. |
| 97 |
// Literal codes are accumulated from the start of the buffer. |
| 98 |
// Non-literal codes decode to a sequence of suffixes that are first |
| 99 |
// written right-to-left from the end of the buffer before being copied |
| 100 |
// to the start of the buffer. |
| 101 |
// It is flushed when it contains >= 1<<maxWidth bytes, |
| 102 |
// so that there is always room to decode an entire code. |
| 103 |
output [2 * 1 << maxWidth]byte |
| 104 |
o int // write index into output |
| 105 |
toRead []byte // bytes to return from Read |
| 106 |
} |
| 107 |
|
| 108 |
// readLSB returns the next code for "Least Significant Bits first" data. |
| 109 |
func (d *decoder) readLSB() (uint16, error) { |
| 110 |
for d.nBits < d.width { |
| 111 |
x, err := d.r.ReadByte() |
| 112 |
if err != nil { |
| 113 |
return 0, err |
| 114 |
} |
| 115 |
d.bits |= uint32(x) << d.nBits |
| 116 |
d.nBits += 8 |
| 117 |
} |
| 118 |
code := uint16(d.bits & (1<<d.width - 1)) |
| 119 |
d.bits >>= d.width |
| 120 |
d.nBits -= d.width |
| 121 |
return code, nil |
| 122 |
} |
| 123 |
|
| 124 |
// readMSB returns the next code for "Most Significant Bits first" data. |
| 125 |
func (d *decoder) readMSB() (uint16, error) { |
| 126 |
for d.nBits < d.width { |
| 127 |
x, err := d.r.ReadByte() |
| 128 |
if err != nil { |
| 129 |
return 0, err |
| 130 |
} |
| 131 |
d.bits |= uint32(x) << (24 - d.nBits) |
| 132 |
d.nBits += 8 |
| 133 |
} |
| 134 |
code := uint16(d.bits >> (32 - d.width)) |
| 135 |
d.bits <<= d.width |
| 136 |
d.nBits -= d.width |
| 137 |
return code, nil |
| 138 |
} |
| 139 |
|
| 140 |
func (d *decoder) Read(b []byte) (int, error) { |
| 141 |
for { |
| 142 |
if len(d.toRead) > 0 { |
| 143 |
n := copy(b, d.toRead) |
| 144 |
d.toRead = d.toRead[n:] |
| 145 |
return n, nil |
| 146 |
} |
| 147 |
if d.err != nil { |
| 148 |
return 0, d.err |
| 149 |
} |
| 150 |
d.decode() |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// decode decompresses bytes from r and leaves them in d.toRead. |
| 155 |
// read specifies how to decode bytes into codes. |
| 156 |
// litWidth is the width in bits of literal codes. |
| 157 |
func (d *decoder) decode() { |
| 158 |
// Loop over the code stream, converting codes into decompressed bytes. |
| 159 |
loop: |
| 160 |
for { |
| 161 |
code, err := d.read(d) |
| 162 |
if err != nil { |
| 163 |
if err == io.EOF { |
| 164 |
err = io.ErrUnexpectedEOF |
| 165 |
} |
| 166 |
d.err = err |
| 167 |
break |
| 168 |
} |
| 169 |
switch { |
| 170 |
case code < d.clear: |
| 171 |
// We have a literal code. |
| 172 |
d.output[d.o] = uint8(code) |
| 173 |
d.o++ |
| 174 |
if d.last != decoderInvalidCode { |
| 175 |
// Save what the hi code expands to. |
| 176 |
d.suffix[d.hi] = uint8(code) |
| 177 |
d.prefix[d.hi] = d.last |
| 178 |
} |
| 179 |
case code == d.clear: |
| 180 |
d.width = 1 + uint(d.litWidth) |
| 181 |
d.hi = d.eof |
| 182 |
d.overflow = 1 << d.width |
| 183 |
d.last = decoderInvalidCode |
| 184 |
continue |
| 185 |
case code == d.eof: |
| 186 |
d.err = io.EOF |
| 187 |
break loop |
| 188 |
case code <= d.hi: |
| 189 |
c, i := code, len(d.output)-1 |
| 190 |
if code == d.hi && d.last != decoderInvalidCode { |
| 191 |
// code == hi is a special case which expands to the last expansion |
| 192 |
// followed by the head of the last expansion. To find the head, we walk |
| 193 |
// the prefix chain until we find a literal code. |
| 194 |
c = d.last |
| 195 |
for c >= d.clear { |
| 196 |
c = d.prefix[c] |
| 197 |
} |
| 198 |
d.output[i] = uint8(c) |
| 199 |
i-- |
| 200 |
c = d.last |
| 201 |
} |
| 202 |
// Copy the suffix chain into output and then write that to w. |
| 203 |
for c >= d.clear { |
| 204 |
d.output[i] = d.suffix[c] |
| 205 |
i-- |
| 206 |
c = d.prefix[c] |
| 207 |
} |
| 208 |
d.output[i] = uint8(c) |
| 209 |
d.o += copy(d.output[d.o:], d.output[i:]) |
| 210 |
if d.last != decoderInvalidCode { |
| 211 |
// Save what the hi code expands to. |
| 212 |
d.suffix[d.hi] = uint8(c) |
| 213 |
d.prefix[d.hi] = d.last |
| 214 |
} |
| 215 |
default: |
| 216 |
d.err = errors.New("lzw: invalid code") |
| 217 |
break loop |
| 218 |
} |
| 219 |
d.last, d.hi = code, d.hi+1 |
| 220 |
if d.hi+1 >= d.overflow { // NOTE: the "+1" is where TIFF's LZW differs from the standard algorithm. |
| 221 |
if d.width == maxWidth { |
| 222 |
d.last = decoderInvalidCode |
| 223 |
} else { |
| 224 |
d.width++ |
| 225 |
d.overflow <<= 1 |
| 226 |
} |
| 227 |
} |
| 228 |
if d.o >= flushBuffer { |
| 229 |
break |
| 230 |
} |
| 231 |
} |
| 232 |
// Flush pending output. |
| 233 |
d.toRead = d.output[:d.o] |
| 234 |
d.o = 0 |
| 235 |
} |
| 236 |
|
| 237 |
var errClosed = errors.New("lzw: reader/writer is closed") |
| 238 |
|
| 239 |
func (d *decoder) Close() error { |
| 240 |
d.err = errClosed // in case any Reads come along |
| 241 |
return nil |
| 242 |
} |
| 243 |
|
| 244 |
// NewReader creates a new io.ReadCloser. |
| 245 |
// Reads from the returned io.ReadCloser read and decompress data from r. |
| 246 |
// If r does not also implement io.ByteReader, |
| 247 |
// the decompressor may read more data than necessary from r. |
| 248 |
// It is the caller's responsibility to call Close on the ReadCloser when |
| 249 |
// finished reading. |
| 250 |
// The number of bits to use for literal codes, litWidth, must be in the |
| 251 |
// range [2,8] and is typically 8. It must equal the litWidth |
| 252 |
// used during compression. |
| 253 |
func NewReader(r io.Reader, order Order, litWidth int) io.ReadCloser { |
| 254 |
d := new(decoder) |
| 255 |
switch order { |
| 256 |
case LSB: |
| 257 |
d.read = (*decoder).readLSB |
| 258 |
case MSB: |
| 259 |
d.read = (*decoder).readMSB |
| 260 |
default: |
| 261 |
d.err = errors.New("lzw: unknown order") |
| 262 |
return d |
| 263 |
} |
| 264 |
if litWidth < 2 || 8 < litWidth { |
| 265 |
d.err = fmt.Errorf("lzw: litWidth %d out of range", litWidth) |
| 266 |
return d |
| 267 |
} |
| 268 |
if br, ok := r.(io.ByteReader); ok { |
| 269 |
d.r = br |
| 270 |
} else { |
| 271 |
d.r = bufio.NewReader(r) |
| 272 |
} |
| 273 |
d.litWidth = litWidth |
| 274 |
d.width = 1 + uint(litWidth) |
| 275 |
d.clear = uint16(1) << uint(litWidth) |
| 276 |
d.eof, d.hi = d.clear+1, d.clear+1 |
| 277 |
d.overflow = uint16(1) << d.width |
| 278 |
d.last = decoderInvalidCode |
| 279 |
|
| 280 |
return d |
| 281 |
} |