1
1
mirror of https://github.com/wader/fq.git synced 2024-11-27 06:04:47 +03:00

decode,png: Add FieldFormatReaderLen, refactor out zlib to format

This commit is contained in:
Mattias Wadman 2021-09-19 20:49:15 +02:00
parent d03a1c910b
commit d4142b875c
2 changed files with 6 additions and 5 deletions

View File

@ -6,6 +6,7 @@ package png
// TODO: color types
import (
"compress/zlib"
"hash/crc32"
"github.com/wader/fq/format"
@ -116,7 +117,7 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
switch compressionMethod {
case compressionDeflate:
dd := d.FieldStructFn("data", func(d *decode.D) {
d.FieldFormatZlibLen("uncompressed", int64(dataLen), decode.FormatFn(func(d *decode.D, in interface{}) interface{} {
d.FieldFormatReaderLen("uncompressed", int64(dataLen), zlib.NewReader, decode.FormatFn(func(d *decode.D, in interface{}) interface{} {
d.FieldUTF8("text", int(d.BitsLeft()/8))
return nil
}))
@ -137,7 +138,7 @@ func pngDecode(d *decode.D, in interface{}) interface{} {
switch compressionMethod {
case compressionDeflate:
dd := d.FieldStructFn("data", func(d *decode.D) {
d.FieldFormatZlibLen("uncompressed", int64(dataLen), iccProfileFormat)
d.FieldFormatReaderLen("uncompressed", int64(dataLen), zlib.NewReader, iccProfileFormat)
})
dd.Value.Range = ranges.Range{Start: d.Pos() - int64(dataLen), Len: int64(dataLen)}
default:

View File

@ -4,10 +4,10 @@ package decode
import (
"bytes"
"compress/zlib"
"context"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"strings"
@ -1034,12 +1034,12 @@ func (d *D) FieldBitBufLen(name string, nBits int64) *bitio.Buffer {
}
// TODO: range?
func (d *D) FieldFormatZlibLen(name string, nBits int64, formats []*Format) (*Value, interface{}) {
func (d *D) FieldFormatReaderLen(name string, nBits int64, fn func(r io.Reader) (io.ReadCloser, error), formats []*Format) (*Value, interface{}) {
bb, err := d.bitBuf.BitBufLen(nBits)
if err != nil {
panic(err)
}
zr, err := zlib.NewReader(bb)
zr, err := fn(bb)
if err != nil {
panic(err)
}