2020-06-08 03:29:51 +03:00
|
|
|
package gz
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc1952
|
|
|
|
// TODO: test name, comment etc
|
|
|
|
// TODO: verify isize?
|
|
|
|
|
|
|
|
import (
|
|
|
|
"compress/flate"
|
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
2021-08-17 13:06:32 +03:00
|
|
|
|
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2022-07-16 19:39:57 +03:00
|
|
|
"github.com/wader/fq/pkg/interp"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-17 18:46:10 +03:00
|
|
|
var probeFormat decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2022-07-16 19:39:57 +03:00
|
|
|
interp.RegisterFormat(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.GZIP,
|
|
|
|
Description: "gzip compression",
|
|
|
|
Groups: []string{format.PROBE},
|
|
|
|
DecodeFn: gzDecode,
|
|
|
|
Dependencies: []decode.Dependency{
|
2021-11-17 18:46:10 +03:00
|
|
|
{Names: []string{format.PROBE}, Group: &probeFormat},
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const delfateMethod = 8
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var compressionMethodNames = scalar.UToSymStr{
|
2021-11-05 17:04:26 +03:00
|
|
|
delfateMethod: "deflate",
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var osNames = scalar.UToSymStr{
|
2022-04-05 14:57:55 +03:00
|
|
|
0: "fat",
|
|
|
|
1: "amiga",
|
|
|
|
2: "vms",
|
|
|
|
3: "unix",
|
|
|
|
4: "vm_cms",
|
|
|
|
5: "atari_tOS",
|
|
|
|
6: "hpfs",
|
|
|
|
7: "Mmcintosh",
|
|
|
|
8: "z_system",
|
|
|
|
9: "cpm",
|
|
|
|
10: "tops_20",
|
|
|
|
11: "ntfs",
|
|
|
|
12: "qdos",
|
|
|
|
13: "acorn_riscos",
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var deflateExtraFlagsNames = scalar.UToSymStr{
|
2021-11-05 17:04:26 +03:00
|
|
|
2: "slow",
|
|
|
|
4: "fast",
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:33:50 +03:00
|
|
|
func gzDecode(d *decode.D, _ any) any {
|
2021-11-24 13:15:42 +03:00
|
|
|
d.Endian = decode.LittleEndian
|
|
|
|
|
2021-11-16 15:03:45 +03:00
|
|
|
d.FieldRawLen("identification", 2*8, d.AssertBitBuf([]byte("\x1f\x8b")))
|
2021-12-02 00:48:25 +03:00
|
|
|
compressionMethod := d.FieldU8("compression_method", compressionMethodNames)
|
2020-06-08 03:29:51 +03:00
|
|
|
hasHeaderCRC := false
|
|
|
|
hasExtra := false
|
|
|
|
hasName := false
|
|
|
|
hasComment := false
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("flags", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
d.FieldBool("text")
|
|
|
|
hasHeaderCRC = d.FieldBool("header_crc")
|
|
|
|
hasExtra = d.FieldBool("extra")
|
|
|
|
hasName = d.FieldBool("name")
|
|
|
|
hasComment = d.FieldBool("comment")
|
|
|
|
d.FieldU3("reserved")
|
|
|
|
})
|
2022-05-07 14:13:37 +03:00
|
|
|
d.FieldU32("mtime", scalar.DescriptionActualUUnixTime)
|
2020-06-08 03:29:51 +03:00
|
|
|
switch compressionMethod {
|
|
|
|
case delfateMethod:
|
2021-12-02 00:48:25 +03:00
|
|
|
d.FieldU8("extra_flags", deflateExtraFlagsNames)
|
2020-06-08 03:29:51 +03:00
|
|
|
default:
|
|
|
|
d.FieldU8("extra_flags")
|
|
|
|
}
|
2021-12-02 00:48:25 +03:00
|
|
|
d.FieldU8("os", osNames)
|
2020-06-08 03:29:51 +03:00
|
|
|
if hasExtra {
|
|
|
|
// TODO:
|
|
|
|
xLen := d.FieldU16("xlen")
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("extra_fields", int64(xLen*8))
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
if hasName {
|
2021-11-17 18:13:10 +03:00
|
|
|
d.FieldUTF8Null("name")
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
if hasComment {
|
2021-11-17 18:13:10 +03:00
|
|
|
d.FieldUTF8Null("comment")
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
if hasHeaderCRC {
|
2021-11-05 17:04:26 +03:00
|
|
|
// TODO: validate
|
2021-12-02 00:48:25 +03:00
|
|
|
d.FieldRawLen("header_crc", 16, scalar.RawHex)
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-11-20 18:22:00 +03:00
|
|
|
var rFn func(r io.Reader) io.Reader
|
2020-06-08 03:29:51 +03:00
|
|
|
switch compressionMethod {
|
|
|
|
case delfateMethod:
|
2022-01-24 23:21:48 +03:00
|
|
|
// bitio.NewIOReadSeeker implements io.ByteReader so that deflate don't do own
|
2021-11-20 18:22:00 +03:00
|
|
|
// buffering and might read more than needed messing up knowing compressed size
|
|
|
|
rFn = func(r io.Reader) io.Reader { return flate.NewReader(r) }
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
|
2021-12-06 17:41:39 +03:00
|
|
|
if rFn != nil {
|
2022-01-24 23:21:48 +03:00
|
|
|
readCompressedSize, uncompressedBR, dv, _, _ := d.TryFieldReaderRangeFormat("uncompressed", d.Pos(), d.BitsLeft(), rFn, probeFormat, nil)
|
|
|
|
if uncompressedBR != nil {
|
2021-12-06 17:41:39 +03:00
|
|
|
if dv == nil {
|
2022-01-24 23:21:48 +03:00
|
|
|
d.FieldRootBitBuf("uncompressed", uncompressedBR)
|
2021-12-06 17:41:39 +03:00
|
|
|
}
|
|
|
|
d.FieldRawLen("compressed", readCompressedSize)
|
|
|
|
crc32W := crc32.NewIEEE()
|
2022-01-24 23:21:48 +03:00
|
|
|
// TODO: cleanup clone
|
2022-06-30 13:13:36 +03:00
|
|
|
d.CopyBits(crc32W, d.CloneReadSeeker(uncompressedBR))
|
2022-05-07 13:46:34 +03:00
|
|
|
d.FieldU32("crc32", d.ValidateUBytes(crc32W.Sum(nil)), scalar.ActualHex)
|
2021-12-06 17:41:39 +03:00
|
|
|
d.FieldU32("isize")
|
2021-11-24 17:25:27 +03:00
|
|
|
}
|
2021-11-20 18:22:00 +03:00
|
|
|
}
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|