2020-06-08 03:29:51 +03:00
|
|
|
package ape
|
|
|
|
|
|
|
|
// http://wiki.hydrogenaud.io/index.php?title=APE_Tags_Header
|
|
|
|
|
|
|
|
import (
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/registry"
|
|
|
|
"github.com/wader/fq/pkg/decode"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
2021-11-17 18:46:10 +03:00
|
|
|
var imageFormat decode.Group
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
func init() {
|
2021-11-17 18:46:10 +03:00
|
|
|
registry.MustRegister(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.APEV2,
|
|
|
|
Description: "APEv2 metadata tag",
|
|
|
|
DecodeFn: apev2Decode,
|
|
|
|
Dependencies: []decode.Dependency{
|
2021-11-17 18:46:10 +03:00
|
|
|
{Names: []string{format.IMAGE}, Group: &imageFormat},
|
2020-06-08 03:29:51 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-20 16:10:41 +03:00
|
|
|
func apev2Decode(d *decode.D, in 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
|
2021-11-05 17:04:26 +03:00
|
|
|
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")
|
2021-12-02 00:48:25 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("tags", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for i := uint64(0); i < tagCount; i++ {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("tag", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
itemSize := d.FieldU32("item_size")
|
|
|
|
var binaryItem bool
|
2021-11-05 17:04:26 +03:00
|
|
|
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 {
|
2022-01-13 20:34:59 +03:00
|
|
|
d.FramedFn(int64(itemSize)*8, func(d *decode.D) {
|
2021-11-17 18:13:10 +03:00
|
|
|
d.FieldUTF8Null("filename")
|
2020-06-08 03:29:51 +03:00
|
|
|
// assume image if binary
|
2022-04-11 23:45:46 +03:00
|
|
|
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
|
|
|
|
}
|