diff --git a/gziputil/util.go b/gziputil/util.go new file mode 100644 index 0000000..7446fa9 --- /dev/null +++ b/gziputil/util.go @@ -0,0 +1,59 @@ +package gziputil + +import ( + "encoding/binary" + "fmt" +) + +type Gzip struct { + magic []byte + cm byte + flg byte + mtime uint32 + xfl byte + os byte + xlen uint16 + xflds []byte + crc32 uint32 + isize uint32 + + rest []byte + trailer []byte +} + +func explainGzip(bs []byte) Gzip { + g := Gzip{ + magic: bs[0:2], + cm: bs[2], + flg: bs[3], + mtime: binary.BigEndian.Uint32(bs[4:8]), + xfl: bs[8], + os: bs[9], + } + + offset := 10 + if g.xfl&2 == 1 { + g.xlen = binary.BigEndian.Uint16(bs[offset : offset+2]) + offset += 2 + + g.xflds = bs[offset : offset+int(g.xlen)] + offset += int(g.xlen) + } + + g.crc32 = binary.BigEndian.Uint32(bs[offset : offset+4]) + offset += 4 + + g.isize = binary.BigEndian.Uint32(bs[offset : offset+4]) + offset += 4 + + g.rest = bs[offset : len(bs)-8] + offset = len(bs) - 8 + + g.trailer = bs[offset:] + + return g +} + +func (g Gzip) String() string { + return fmt.Sprintf("magic=%04x cm=%02x flg=%02x mtime=%d xfl=%02x os=%02x xlen=%d crc32=%d isize=%d rest=%x trailer=%08x", g.magic, g.cm, g.flg, g.mtime, g.xfl, g.os, g.xlen, g.crc32, g.isize, g.rest, g.trailer) +}