1
1
mirror of https://github.com/wader/fq.git synced 2024-12-28 08:02:28 +03:00

tar: Some number fields can be empty

This commit is contained in:
Mattias Wadman 2022-08-15 13:05:09 +02:00
parent 29c188b459
commit 0d44b9376c
2 changed files with 28 additions and 13 deletions

View File

@ -45,24 +45,24 @@ func tarDecode(d *decode.D, _ any) any {
for !d.End() {
d.FieldStruct("file", func(d *decode.D) {
d.FieldUTF8("name", 100, mapTrimSpaceNull)
d.FieldUTF8NullFixedLen("mode", 8, scalar.SymUParseUint(8))
d.FieldUTF8NullFixedLen("uid", 8, scalar.SymUParseUint(8))
d.FieldUTF8NullFixedLen("gid", 8, scalar.SymUParseUint(8))
sizeS := d.FieldScalarUTF8NullFixedLen("size", 12, scalar.SymUParseUint(8))
d.FieldUTF8NullFixedLen("mode", 8, scalar.TrySymUParseUint(8))
d.FieldUTF8NullFixedLen("uid", 8, scalar.TrySymUParseUint(8))
d.FieldUTF8NullFixedLen("gid", 8, scalar.TrySymUParseUint(8))
sizeS := d.FieldScalarUTF8NullFixedLen("size", 12, scalar.TrySymUParseUint(8))
if sizeS.Sym == nil {
d.Fatalf("could not decode size")
}
size := int64(sizeS.SymU()) * 8
d.FieldUTF8NullFixedLen("mtime", 12, scalar.SymUParseUint(8), scalar.DescriptionSymUUnixTime)
d.FieldUTF8NullFixedLen("chksum", 8, scalar.SymUParseUint(8))
d.FieldUTF8NullFixedLen("mtime", 12, scalar.TrySymUParseUint(8), scalar.DescriptionSymUUnixTime)
d.FieldUTF8NullFixedLen("chksum", 8, scalar.TrySymUParseUint(8))
d.FieldUTF8("typeflag", 1, mapTrimSpaceNull)
d.FieldUTF8("linkname", 100, mapTrimSpaceNull)
d.FieldUTF8("magic", 6, mapTrimSpaceNull, d.AssertStr("ustar"))
d.FieldUTF8NullFixedLen("version", 2, scalar.SymUParseUint(8))
d.FieldUTF8NullFixedLen("version", 2, scalar.TrySymUParseUint(8))
d.FieldUTF8("uname", 32, mapTrimSpaceNull)
d.FieldUTF8("gname", 32, mapTrimSpaceNull)
d.FieldUTF8NullFixedLen("devmajor", 8, scalar.SymUParseUint(8))
d.FieldUTF8NullFixedLen("devminor", 8, scalar.SymUParseUint(8))
d.FieldUTF8NullFixedLen("devmajor", 8, scalar.TrySymUParseUint(8))
d.FieldUTF8NullFixedLen("devminor", 8, scalar.TrySymUParseUint(8))
d.FieldUTF8("prefix", 155, mapTrimSpaceNull)
d.FieldRawLen("header_block_padding", blockPadding(d), d.BitBufIsZero())

View File

@ -103,11 +103,14 @@ func ActualTrim(cutset string) ActualStrFn {
var ActualTrimSpace = ActualStrFn(strings.TrimSpace)
func strMapToSym(fn func(s string) (any, error)) Mapper {
func strMapToSym(fn func(s string) (any, error), try bool) Mapper {
return Fn(func(s S) (S, error) {
ts := strings.TrimSpace(s.ActualStr())
n, err := fn(ts)
if err != nil {
if try {
return s, nil
}
return s, err
}
s.Sym = n
@ -115,16 +118,28 @@ func strMapToSym(fn func(s string) (any, error)) Mapper {
})
}
func TrySymUParseUint(base int) Mapper {
return strMapToSym(func(s string) (any, error) { return strconv.ParseUint(s, base, 64) }, true)
}
func TrySymSParseInt(base int) Mapper {
return strMapToSym(func(s string) (any, error) { return strconv.ParseInt(s, base, 64) }, true)
}
func TrySymFParseFloat(base int) Mapper {
return strMapToSym(func(s string) (any, error) { return strconv.ParseFloat(s, base) }, true)
}
func SymUParseUint(base int) Mapper {
return strMapToSym(func(s string) (any, error) { return strconv.ParseUint(s, base, 64) })
return strMapToSym(func(s string) (any, error) { return strconv.ParseUint(s, base, 64) }, false)
}
func SymSParseInt(base int) Mapper {
return strMapToSym(func(s string) (any, error) { return strconv.ParseInt(s, base, 64) })
return strMapToSym(func(s string) (any, error) { return strconv.ParseInt(s, base, 64) }, false)
}
func SymFParseFloat(base int) Mapper {
return strMapToSym(func(s string) (any, error) { return strconv.ParseFloat(s, base) })
return strMapToSym(func(s string) (any, error) { return strconv.ParseFloat(s, base) }, false)
}
type URangeEntry struct {