Skip to content

Commit dcfabdf

Browse files
committed
gzip: Add to_deflate/from_deflate functions
1 parent 7e2a10c commit dcfabdf

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

format/gzip/gzip.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package gz
55
// TODO: verify isize?
66

77
import (
8+
"bytes"
89
"compress/flate"
910
"hash/crc32"
1011
"io"
1112
"time"
1213

1314
"github.com/wader/fq/format"
15+
"github.com/wader/fq/pkg/bitio"
1416
"github.com/wader/fq/pkg/decode"
1517
"github.com/wader/fq/pkg/interp"
1618
"github.com/wader/fq/pkg/scalar"
@@ -29,6 +31,8 @@ func init() {
2931
{Groups: []*decode.Group{format.Probe}, Out: &probeGroup},
3032
},
3133
})
34+
interp.RegisterFunc0("from_deflate", fromDeflate)
35+
interp.RegisterFunc0("to_deflate", toDeflate)
3236
}
3337

3438
const deflateMethod = 8
@@ -126,3 +130,47 @@ func gzDecode(d *decode.D) any {
126130

127131
return nil
128132
}
133+
134+
func fromDeflate(_ *interp.Interp, c any) any {
135+
inBR, err := interp.ToBitReader(c)
136+
if err != nil {
137+
return err
138+
}
139+
140+
fr := flate.NewReader(bitio.NewIOReader(inBR))
141+
b := &bytes.Buffer{}
142+
//nolint:gosec
143+
if _, err := io.Copy(b, fr); err != nil {
144+
return err
145+
}
146+
fr.Close()
147+
148+
bb, err := interp.NewBinaryFromBitReader(bitio.NewBitReader(b.Bytes(), -1), 8, 0)
149+
if err != nil {
150+
return err
151+
}
152+
return bb
153+
}
154+
155+
func toDeflate(_ *interp.Interp, c any) any {
156+
inBR, err := interp.ToBitReader(c)
157+
if err != nil {
158+
return err
159+
}
160+
161+
bb := &bytes.Buffer{}
162+
fw, err := flate.NewWriter(bb, 9) // TODO: level option
163+
if err != nil {
164+
return err
165+
}
166+
if _, err := io.Copy(fw, bitio.NewIOReader(inBR)); err != nil {
167+
return err
168+
}
169+
fw.Close()
170+
171+
bib, err := interp.NewBinaryFromBitReader(bitio.NewBitReader(bb.Bytes(), -1), 8, 0)
172+
if err != nil {
173+
return err
174+
}
175+
return bib
176+
}

0 commit comments

Comments
 (0)