1
1
mirror of https://github.com/wader/fq.git synced 2024-12-24 13:52:02 +03:00
fq/format/ape/apev2.go

76 lines
1.8 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
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"
2020-06-08 03:29:51 +03:00
)
var imageFormat decode.Group
2020-06-08 03:29:51 +03:00
func init() {
interp.RegisterFormat(decode.Format{
2020-06-08 03:29:51 +03:00
Name: format.APEV2,
Description: "APEv2 metadata tag",
DecodeFn: apev2Decode,
Dependencies: []decode.Dependency{
{Names: []string{format.IMAGE}, Group: &imageFormat},
2020-06-08 03:29:51 +03:00
},
})
}
func apev2Decode(d *decode.D, _ any) any {
2020-06-08 03:29:51 +03:00
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.AssertStr("APETAGEX"))
2020-06-08 03:29:51 +03:00
d.FieldU32("version")
d.FieldU32("tag_size")
tagCount = d.FieldU32("item_count")
d.FieldU32("flags")
d.FieldRawLen("reserved", 64, d.BitBufIsZero())
2020-06-08 03:29:51 +03:00
})
return tagCount
}
tagCount := headerFooterFn(d, "header")
2021-11-22 03:45:30 +03:00
if tagCount > 1000 {
d.Fatalf("too many tags %d", tagCount)
}
d.FieldArray("tags", func(d *decode.D) {
2020-06-08 03:29:51 +03:00
for i := uint64(0); i < tagCount; i++ {
d.FieldStruct("tag", func(d *decode.D) {
2020-06-08 03:29:51 +03:00
itemSize := d.FieldU32("item_size")
var binaryItem bool
d.FieldStruct("item_flags", func(d *decode.D) {
2020-06-08 03:29:51 +03:00
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")
2020-06-08 03:29:51 +03:00
// assume image if binary
d.FieldFormatOrRaw("value", imageFormat, nil)
2020-06-08 03:29:51 +03:00
})
} else {
d.FieldUTF8("value", int(itemSize))
}
})
}
})
// TODO: check footer flag
headerFooterFn(d, "footer")
return nil
}