mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Let flatecut accept a degenerate Huffman tree
This commit is contained in:
1 parent
52261e7528
commit
0cc17985db
7 files changed
+132
-3
No files matched your search
@@ -253,7 +253,7 @@ func (h *huffman) construct(lengths []uint32) (endCodeBits uint32, endCodeNBits
|
||||
for _, x := range lengths {
|
||||
h.counts[x]++
|
||||
}
|
||||
if h.counts[0] == uint32(len(lengths)) {
|
||||
if h.counts[0] >= uint32(len(lengths)) {
|
||||
return 0, 0, errInvalidBadHuffmanTree
|
||||
}
|
||||
|
||||
@@ -284,7 +284,12 @@ func (h *huffman) construct(lengths []uint32) (endCodeBits uint32, endCodeNBits
|
||||
}
|
||||
}
|
||||
if remaining != 0 {
|
||||
return 0, 0, errInvalidBadHuffmanTree
|
||||
if ((h.counts[0] + 1) == uint32(len(lengths))) && (h.counts[1] == 1) {
|
||||
// No-op. We allow a degenerate Huffman tree with only one code (of
|
||||
// length 1 bit).
|
||||
} else {
|
||||
return 0, 0, errInvalidBadHuffmanTree
|
||||
}
|
||||
}
|
||||
|
||||
offsets := [maxCodeBits + 1]uint32{}
|
||||
|
||||
@@ -15,8 +15,10 @@
|
||||
package flatecut
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/google/wuffs/internal/testcut"
|
||||
@@ -109,3 +111,39 @@ func TestHuffmanDecode(t *testing.T) {
|
||||
t.Fatalf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDegenerateHuffmanUnused(t *testing.T) {
|
||||
decode := func(src []byte) string {
|
||||
dst, err := ioutil.ReadAll(
|
||||
flate.NewReader(bytes.NewReader(src)))
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return string(dst)
|
||||
}
|
||||
|
||||
full, err := ioutil.ReadFile("../../test/data/artificial/deflate-degenerate-huffman-unused.deflate")
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile: %v", err)
|
||||
}
|
||||
if len(full) != 15 {
|
||||
t.Fatalf("len(full): got %d, want %d", len(full), 15)
|
||||
}
|
||||
|
||||
if got, want := decode(full), "foo"; got != want {
|
||||
t.Fatalf("before Cut: got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
if encLen, decLen, err := Cut(nil, full, 14); err != nil {
|
||||
t.Fatalf("Cut: %v", err)
|
||||
} else if encLen != 14 {
|
||||
t.Fatalf("encLen: got %d, want %d", encLen, 14)
|
||||
} else if decLen != 2 {
|
||||
t.Fatalf("decLen: got %d, want %d", decLen, 2)
|
||||
}
|
||||
|
||||
if got, want := decode(full[:14]), "fo"; got != want {
|
||||
t.Fatalf("after Cut: got %q, want %q", got, want)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user