2021-12-06 21:33:17 +03:00
|
|
|
package ar
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-06 21:33:17 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
|
|
|
)
|
|
|
|
|
2023-03-29 01:36:55 +03:00
|
|
|
var probeGroup decode.Group
|
2021-12-06 21:33:17 +03:00
|
|
|
|
|
|
|
func init() {
|
2023-03-29 01:36:55 +03:00
|
|
|
interp.RegisterFormat(
|
2023-05-01 14:19:04 +03:00
|
|
|
format.AR,
|
2023-03-29 01:36:55 +03:00
|
|
|
&decode.Format{
|
|
|
|
Description: "Unix archive",
|
|
|
|
Groups: []*decode.Group{format.Probe},
|
|
|
|
DecodeFn: decodeAr,
|
|
|
|
Dependencies: []decode.Dependency{
|
|
|
|
{Groups: []*decode.Group{format.Probe}, Out: &probeGroup},
|
|
|
|
},
|
|
|
|
})
|
2021-12-06 21:33:17 +03:00
|
|
|
}
|
|
|
|
|
2023-02-18 23:10:48 +03:00
|
|
|
func decodeAr(d *decode.D) any {
|
2022-09-30 14:58:23 +03:00
|
|
|
d.FieldUTF8("signature", 8, d.StrAssert("!<arch>\n"))
|
2021-12-06 21:33:17 +03:00
|
|
|
d.FieldArray("files", func(d *decode.D) {
|
|
|
|
for !d.End() {
|
|
|
|
d.FieldStruct("file", func(d *decode.D) {
|
2022-05-07 13:46:34 +03:00
|
|
|
d.FieldUTF8("identifier", 16, scalar.ActualTrimSpace)
|
2022-09-30 14:58:23 +03:00
|
|
|
d.FieldUTF8("modification_timestamp", 12, scalar.ActualTrimSpace, scalar.TryStrSymParseUint(10))
|
|
|
|
d.FieldUTF8("owner_id", 6, scalar.ActualTrimSpace, scalar.TryStrSymParseUint(10))
|
|
|
|
d.FieldUTF8("group_id", 6, scalar.ActualTrimSpace, scalar.TryStrSymParseUint(10))
|
|
|
|
d.FieldUTF8("file_mode", 8, scalar.ActualTrimSpace, scalar.TryStrSymParseUint(8)) // Octal
|
|
|
|
sizeStr := d.FieldScalarUTF8("file_size", 10, scalar.ActualTrimSpace, scalar.TryStrSymParseUint(10))
|
|
|
|
if sizeStr.Sym == nil {
|
2021-12-06 21:33:17 +03:00
|
|
|
d.Fatalf("could not decode file_size")
|
|
|
|
}
|
2022-09-30 14:58:23 +03:00
|
|
|
size := int64(sizeStr.SymUint()) * 8
|
2021-12-06 21:33:17 +03:00
|
|
|
d.FieldUTF8("ending_characters", 2)
|
2023-05-08 23:37:50 +03:00
|
|
|
d.FieldFormatOrRawLen("data", size, &probeGroup, format.Probe_In{})
|
2021-12-06 21:33:17 +03:00
|
|
|
padding := d.AlignBits(16)
|
|
|
|
if padding > 0 {
|
|
|
|
d.FieldRawLen("padding", int64(padding))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|