1
1
mirror of https://github.com/wader/fq.git synced 2024-11-26 21:55:57 +03:00
fq/format/ape/apev2.go
Mattias Wadman b08ef00dd1 decode,interp: Refactor format groups into a proper struct
Replaces []Format with a Group type.
A bit more type safe.
Breaking change for RegisterFormat, now takes a first argument that is a "single" format group.
Lots of naming cleanup.

This is also preparation for decode group argument which will enable doing intresting
probing, ex a format decoder could know it's decode as part of probe group  (html could
be probed possibly), or have "arg probe" group for decoder who inspect args to know
if they should probe (-d /path/to/schema etc) to enable nice CLI-ergonomics.
2023-04-29 20:02:34 +02:00

77 lines
1.8 KiB
Go

package ape
// http://wiki.hydrogenaud.io/index.php?title=APE_Tags_Header
import (
"github.com/wader/fq/format"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
)
var imageGroup decode.Group
func init() {
interp.RegisterFormat(
format.Apev2,
&decode.Format{
Description: "APEv2 metadata tag",
DecodeFn: apev2Decode,
Dependencies: []decode.Dependency{
{Groups: []*decode.Group{format.Image}, Out: &imageGroup},
},
})
}
func apev2Decode(d *decode.D) any {
d.Endian = decode.LittleEndian
headerFooterFn := func(d *decode.D, name string) uint64 {
var tagCount uint64
d.FieldStruct(name, func(d *decode.D) {
d.FieldUTF8("preamble", 8, d.StrAssert("APETAGEX"))
d.FieldU32("version")
d.FieldU32("tag_size")
tagCount = d.FieldU32("item_count")
d.FieldU32("flags")
d.FieldRawLen("reserved", 64, d.BitBufIsZero())
})
return tagCount
}
tagCount := headerFooterFn(d, "header")
if tagCount > 1000 {
d.Fatalf("too many tags %d", tagCount)
}
d.FieldArray("tags", func(d *decode.D) {
for i := uint64(0); i < tagCount; i++ {
d.FieldStruct("tag", func(d *decode.D) {
itemSize := d.FieldU32("item_size")
var binaryItem bool
d.FieldStruct("item_flags", func(d *decode.D) {
d.FieldU6("unused0")
binaryItem = d.FieldBool("binary")
d.FieldU25("unused1")
})
keyLen := d.PeekFindByte(0, -1)
d.FieldUTF8("key", int(keyLen))
d.FieldU8("key_terminator")
if binaryItem {
d.FramedFn(int64(itemSize)*8, func(d *decode.D) {
d.FieldUTF8Null("filename")
// assume image if binary
d.FieldFormatOrRaw("value", &imageGroup, nil)
})
} else {
d.FieldUTF8("value", int(itemSize))
}
})
}
})
// TODO: check footer flag
headerFooterFn(d, "footer")
return nil
}