@@ -5,12 +5,14 @@ package gz
5
5
// TODO: verify isize?
6
6
7
7
import (
8
+ "bytes"
8
9
"compress/flate"
9
10
"hash/crc32"
10
11
"io"
11
12
"time"
12
13
13
14
"github.com/wader/fq/format"
15
+ "github.com/wader/fq/pkg/bitio"
14
16
"github.com/wader/fq/pkg/decode"
15
17
"github.com/wader/fq/pkg/interp"
16
18
"github.com/wader/fq/pkg/scalar"
@@ -29,6 +31,8 @@ func init() {
29
31
{Groups : []* decode.Group {format .Probe }, Out : & probeGroup },
30
32
},
31
33
})
34
+ interp .RegisterFunc0 ("from_deflate" , fromDeflate )
35
+ interp .RegisterFunc0 ("to_deflate" , toDeflate )
32
36
}
33
37
34
38
const deflateMethod = 8
@@ -126,3 +130,47 @@ func gzDecode(d *decode.D) any {
126
130
127
131
return nil
128
132
}
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