From d442804300f687c2fa12d7e8a146aa5db45e9caf Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 30 Apr 2017 16:25:07 +1000 Subject: [PATCH] tiff/lzw: sync with the stdlib compress/lzw. As noted at the top of reader.go, this package is a fork of the standard library's LZW package, due to an "off by one" in the TIFF format. Grep for "NOTE" in the Go code for more details. This commit picks up an upstream change: https://go-review.googlesource.com/14410 "tidy up some flush calls" Picking up the more recent change: https://go-review.googlesource.com/42032 "fix hi code overflow" will be a follow-up commit, separate from this straightforward commit, since it has a non-trivial interaction with that off by one. Change-Id: Iaf795d11590b3e3e0891e3ea3f04b696de4243c9 Reviewed-on: https://go-review.googlesource.com/42191 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- tiff/lzw/reader.go | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/tiff/lzw/reader.go b/tiff/lzw/reader.go index ad35819..51ae39f 100644 --- a/tiff/lzw/reader.go +++ b/tiff/lzw/reader.go @@ -147,6 +147,7 @@ func (d *decoder) Read(b []byte) (int, error) { // litWidth is the width in bits of literal codes. func (d *decoder) decode() { // Loop over the code stream, converting codes into decompressed bytes. +loop: for { code, err := d.read(d) if err != nil { @@ -154,8 +155,7 @@ func (d *decoder) decode() { err = io.ErrUnexpectedEOF } d.err = err - d.flush() - return + break } switch { case code < d.clear: @@ -174,9 +174,8 @@ func (d *decoder) decode() { d.last = decoderInvalidCode continue case code == d.eof: - d.flush() d.err = io.EOF - return + break loop case code <= d.hi: c, i := code, len(d.output)-1 if code == d.hi { @@ -206,8 +205,7 @@ func (d *decoder) decode() { } default: d.err = errors.New("lzw: invalid code") - d.flush() - return + break loop } d.last, d.hi = code, d.hi+1 if d.hi+1 >= d.overflow { // NOTE: the "+1" is where TIFF's LZW differs from the standard algorithm. @@ -219,13 +217,10 @@ func (d *decoder) decode() { } } if d.o >= flushBuffer { - d.flush() - return + break } } -} - -func (d *decoder) flush() { + // Flush pending output. d.toRead = d.output[:d.o] d.o = 0 }