1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 21:31:33 +03:00

tar: Cleanup api usage

This commit is contained in:
Mattias Wadman 2021-12-04 17:54:50 +01:00
parent 75aa47556a
commit eb4718fbdd

View File

@ -29,69 +29,59 @@ func init() {
} }
func tarDecode(d *decode.D, in interface{}) interface{} { func tarDecode(d *decode.D, in interface{}) interface{} {
fieldStr := func(d *decode.D, name string, nBytes int) string { const blockBytes = 512
return d.FieldUTF8(name, nBytes, scalar.Trim(" \x00")) const blockBits = blockBytes * 8
}
fieldNumStr := func(d *decode.D, name string, nBytes int) uint64 { mapTrimSpaceNull := scalar.Trim(" \x00")
// TODO: some kind of FieldUScalarFn func that returns sym value? mapOctStrToSymU := scalar.Fn(func(s scalar.S) (scalar.S, error) {
var n uint64 ts := strings.Trim(s.ActualStr(), " ")
d.FieldScalar(name, func(_ scalar.S) (scalar.S, error) { if ts != "" {
a := d.UTF8NullFixedLen(nBytes) n, err := strconv.ParseUint(ts, 8, 64)
ts := strings.Trim(a, " ") if err != nil {
n = uint64(0) return s, err
if ts != "" {
var err error
n, err = strconv.ParseUint(ts, 8, 64)
if err != nil {
d.Errorf("failed to parse %s number %s: %s", name, ts, err)
}
} }
return scalar.S{Actual: a, Sym: n}, nil s.Sym = n
})
return n
}
fieldBlockPadding := func(d *decode.D, name string) {
const blockBits = 512 * 8
blockPadding := (blockBits - (d.Pos() % blockBits)) % blockBits
if blockPadding > 0 {
d.FieldRawLen(name, blockPadding, d.BitBufIsZero())
} }
return s, nil
})
blockPadding := func(d *decode.D) int64 {
return (blockBits - (d.Pos() % blockBits)) % blockBits
} }
// 512*2 zero bytes // 512*2 zero bytes
endMarker := [512 * 2]byte{} endMarker := [blockBytes * 2]byte{}
foundEndMarker := false foundEndMarker := false
d.FieldArray("files", func(d *decode.D) { d.FieldArray("files", func(d *decode.D) {
for !d.End() { for !d.End() {
d.FieldStruct("file", func(d *decode.D) { d.FieldStruct("file", func(d *decode.D) {
fieldStr(d, "name", 100) d.FieldUTF8("name", 100, mapTrimSpaceNull)
fieldNumStr(d, "mode", 8) d.FieldUTF8NullFixedLen("mode", 8, mapOctStrToSymU)
fieldNumStr(d, "uid", 8) d.FieldUTF8NullFixedLen("uid", 8, mapOctStrToSymU)
fieldNumStr(d, "gid", 8) d.FieldUTF8NullFixedLen("gid", 8, mapOctStrToSymU)
size := fieldNumStr(d, "size", 12) size := d.FieldScalarUTF8NullFixedLen("size", 12, mapOctStrToSymU).SymU()
fieldNumStr(d, "mtime", 12) d.FieldUTF8NullFixedLen("mtime", 12, mapOctStrToSymU)
fieldNumStr(d, "chksum", 8) d.FieldUTF8NullFixedLen("chksum", 8, mapOctStrToSymU)
fieldStr(d, "typeflag", 1) d.FieldUTF8("typeflag", 1, mapTrimSpaceNull)
fieldStr(d, "linkname", 100) d.FieldUTF8("linkname", 100, mapTrimSpaceNull)
magic := fieldStr(d, "magic", 6) magic := d.FieldUTF8("magic", 6, mapTrimSpaceNull)
if magic != "ustar" { if magic != "ustar" {
d.Errorf("invalid magic %s", magic) d.Errorf("invalid magic %s", magic)
} }
fieldNumStr(d, "version", 2) d.FieldUTF8NullFixedLen("version", 2, mapOctStrToSymU)
fieldStr(d, "uname", 32) d.FieldUTF8("uname", 32, mapTrimSpaceNull)
fieldStr(d, "gname", 32) d.FieldUTF8("gname", 32, mapTrimSpaceNull)
fieldNumStr(d, "devmajor", 8) d.FieldUTF8NullFixedLen("devmajor", 8, mapOctStrToSymU)
fieldNumStr(d, "devminor", 8) d.FieldUTF8NullFixedLen("devminor", 8, mapOctStrToSymU)
fieldStr(d, "prefix", 155) d.FieldUTF8("prefix", 155, mapTrimSpaceNull)
fieldBlockPadding(d, "header_block_padding") d.FieldRawLen("header_block_padding", blockPadding(d), d.BitBufIsZero())
if size > 0 {
dv, _, _ := d.TryFieldFormatLen("data", int64(size)*8, probeFormat, nil) dv, _, _ := d.TryFieldFormatLen("data", int64(size)*8, probeFormat, nil)
if dv == nil { if dv == nil {
d.FieldRawLen("data", int64(size)*8) d.FieldRawLen("data", int64(size)*8)
}
} }
fieldBlockPadding(d, "data_block_padding")
d.FieldRawLen("data_block_padding", blockPadding(d), d.BitBufIsZero())
}) })
bs := d.PeekBytes(512 * 2) bs := d.PeekBytes(512 * 2)