Skip to content

Commit

Permalink
decode: Nicer scalar template and add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Nov 17, 2021
1 parent 15b6d64 commit ede2e77
Show file tree
Hide file tree
Showing 44 changed files with 2,224 additions and 877 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,5 @@
"[plaintext]": {
"files.trimTrailingWhitespace": false,
},
"fracturedjsonvsc.MaxInlineLength": 160,
}
24 changes: 24 additions & 0 deletions dev/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"text/template"
)

Expand All @@ -20,6 +22,17 @@ func toInt(v interface{}) int {
}
}

func toString(v interface{}) string {
switch v := v.(type) {
case string:
return v
case int:
return strconv.Itoa(v)
default:
return ""
}
}

func main() {
funcMap := template.FuncMap{
"xrange": func(args ...interface{}) (interface{}, error) {
Expand All @@ -36,6 +49,17 @@ func main() {

return v, nil
},
"replace": func(args ...interface{}) (interface{}, error) {
if len(args) < 3 {
return nil, errors.New("need tmpl, old and new argument")
}

s := toString(args[0])
o := toString(args[1])
n := toString(args[2])

return strings.Replace(s, o, n, -1), nil
},
}

data := map[string]interface{}{}
Expand Down
2 changes: 1 addition & 1 deletion format/ape/apev2.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func apev2Decode(d *decode.D, in interface{}) interface{} {
d.FieldU8("key_terminator")
if binaryItem {
d.LenFn(int64(itemSize)*8, func(d *decode.D) {
d.FieldUTF8NullTerminated("filename")
d.FieldUTF8Null("filename")
// assume image if binary
dv, _, _ := d.FieldTryFormat("value", imageFormat, nil)
if dv == nil {
Expand Down
2 changes: 1 addition & 1 deletion format/av1/av1_obu.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func obuDecode(d *decode.D, in interface{}) interface{} {

d.FieldStruct("header", func(d *decode.D) {
d.FieldU1("forbidden_bit")
obuType = d.FieldU4("type", d.MapUToStr(obuTypeNames))
obuType = d.FieldU4("type", d.MapUToStrSym(obuTypeNames))
hasExtension = d.FieldBool("extension_flag")
hasSizeField = d.FieldBool("has_size_field")
d.FieldU1("reserved_1bit")
Expand Down
8 changes: 4 additions & 4 deletions format/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func fieldFormatRR(d *decode.D, count uint64, name string, structName string) {
for i := uint64(0); i < count; i++ {
d.FieldStruct(structName, func(d *decode.D) {
fieldFormatLabel(d, "name")
typ := d.FieldU16("type", d.MapUToStr(typeNames))
typ := d.FieldU16("type", d.MapUToStrSym(typeNames))
class := d.FieldU16("class", d.MapURangeToScalar(classNames))
d.FieldU32("ttl")
// TODO: pointer?
Expand All @@ -194,11 +194,11 @@ func fieldFormatRR(d *decode.D, count uint64, name string, structName string) {
func dnsDecode(d *decode.D, in interface{}) interface{} {
d.FieldStruct("header", func(d *decode.D) {
d.FieldU16("id")
d.FieldBool("query", d.MapBoolToStr(decode.BoolToStr{
d.FieldBool("query", d.MapBoolToStrSym(decode.BoolToStr{
true: "Query",
false: "Response",
}))
d.FieldU4("opcode", d.MapUToStr(decode.UToStr{
d.FieldU4("opcode", d.MapUToStrSym(decode.UToStr{
0: "Query",
1: "IQuery",
2: "Status",
Expand All @@ -222,7 +222,7 @@ func dnsDecode(d *decode.D, in interface{}) interface{} {
for i := uint64(0); i < qdCount; i++ {
d.FieldStruct("question", func(d *decode.D) {
fieldFormatLabel(d, "name")
d.FieldU16("type", d.MapUToStr(typeNames))
d.FieldU16("type", d.MapUToStrSym(typeNames))
d.FieldU16("class", d.MapURangeToScalar(classNames))
})
}
Expand Down
20 changes: 10 additions & 10 deletions format/elf/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ func elfDecode(d *decode.D, in interface{}) interface{} {

d.FieldStruct("ident", func(d *decode.D) {
d.FieldRawLen("magic", 4*8, d.AssertBitBuf([]byte("\x7fELF")))
archBits = int(d.FieldU8("class", d.MapUToU(classBits)))
endian = d.FieldU8("data", d.MapUToStr(endianNames))
archBits = int(d.FieldU8("class", d.MapUToUSym(classBits)))
endian = d.FieldU8("data", d.MapUToStrSym(endianNames))
d.FieldU8("version")
d.FieldU8("os_abi", d.MapUToStr(osABINames))
d.FieldU8("os_abi", d.MapUToStrSym(osABINames))
d.FieldU8("abi_version")
d.FieldRawLen("pad", 7*8, d.BitBufIsZero)
})
Expand All @@ -151,7 +151,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {

// TODO: hex functions?

d.FieldU16("type", d.MapUToStr(decode.UToStr{
d.FieldU16("type", d.MapUToStrSym(decode.UToStr{
0x00: "None",
0x01: "Rel",
0x02: "Exec",
Expand All @@ -163,7 +163,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
0xffff: "Hiproc",
}), d.Hex)

d.FieldU16("machine", d.MapUToStr(decode.UToStr{
d.FieldU16("machine", d.MapUToStrSym(decode.UToStr{
0x00: "No specific instruction set",
0x01: "AT&T WE 32100",
0x02: "SPARC",
Expand Down Expand Up @@ -291,7 +291,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {

switch archBits {
case 32:
d.FieldUFn("p_type", func(d *decode.D) uint64 { return d.U32() & 0xf }, d.MapUToStr(pTypeNames))
d.FieldUFn("p_type", func(d *decode.D) uint64 { return d.U32() & 0xf }, d.MapUToStrSym(pTypeNames))
offset = d.FieldU("p_offset", archBits)
d.FieldU("p_vaddr", archBits)
d.FieldU("p_paddr", archBits)
Expand All @@ -300,7 +300,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
pFlags(d)
d.FieldU32("p_align")
case 64:
d.FieldUFn("p_type", func(d *decode.D) uint64 { return d.U32() & 0xf }, d.MapUToStr(pTypeNames))
d.FieldUFn("p_type", func(d *decode.D) uint64 { return d.U32() & 0xf }, d.MapUToStrSym(pTypeNames))
pFlags(d)
offset = d.FieldU("p_offset", archBits)
d.FieldU("p_vaddr", archBits)
Expand Down Expand Up @@ -447,7 +447,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
switch archBits {
case 32:
shname = d.FieldScalar("sh_name", d.ScalarU32(), mapStrTable(strIndexTable)).SymStr()
typ = d.FieldU32("sh_type", d.MapUToStr(shTypeNames), d.Hex)
typ = d.FieldU32("sh_type", d.MapUToStrSym(shTypeNames), d.Hex)
shFlags(d, archBits)
d.FieldU("sh_addr", archBits)
offset = d.FieldU("sh_offset", archBits)
Expand All @@ -458,7 +458,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
d.FieldU32("sh_entsize")
case 64:
shname = d.FieldScalar("sh_name", d.ScalarU32(), mapStrTable(strIndexTable)).SymStr()
typ = d.FieldU32("sh_type", d.MapUToStr(shTypeNames), d.Hex)
typ = d.FieldU32("sh_type", d.MapUToStrSym(shTypeNames), d.Hex)
shFlags(d, archBits)
d.FieldU("sh_addr", archBits)
offset = d.FieldU("sh_offset", archBits)
Expand All @@ -483,7 +483,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
d.FieldArray("dynamic_tags", func(d *decode.D) {
for d.NotEnd() {
d.FieldStruct("tag", func(d *decode.D) {
tag := d.FieldUFn("tag", func(d *decode.D) uint64 { return d.U(archBits) }, d.MapUToStr(dtNames), d.Hex)
tag := d.FieldUFn("tag", func(d *decode.D) uint64 { return d.U(archBits) }, d.MapUToStrSym(dtNames), d.Hex)
switch tag {
case DT_NEEDED:
// TODO: DT_STRTAB
Expand Down
2 changes: 1 addition & 1 deletion format/flac/flac_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
// <1> Blocking strategy:
// 0 : fixed-blocksize stream; frame header encodes the frame number
// 1 : variable-blocksize stream; frame header encodes the sample number
blockingStrategy := d.FieldU1("blocking_strategy", d.MapUToStr(BlockingStrategyNames))
blockingStrategy := d.FieldU1("blocking_strategy", d.MapUToStrSym(BlockingStrategyNames))

// <4> Block size in inter-channel samples:
// 0000 : reserved
Expand Down
2 changes: 1 addition & 1 deletion format/flac/flac_metadatablock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func metadatablockDecode(d *decode.D, in interface{}) interface{} {
var streamInfo format.FlacStreamInfo

isLastBlock := d.FieldBool("last_block")
typ := d.FieldU7("type", d.MapUToStr(metadataBlockNames))
typ := d.FieldU7("type", d.MapUToStrSym(metadataBlockNames))
length := d.FieldU24("length")

switch typ {
Expand Down
2 changes: 1 addition & 1 deletion format/flac/flac_picture.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func pictureDecode(d *decode.D, in interface{}) interface{} {
l := d.FieldU32(name + "_length")
return d.FieldUTF8(name, int(l))
}
d.FieldU32("picture_type", d.MapUToStr(pictureTypeNames))
d.FieldU32("picture_type", d.MapUToStrSym(pictureTypeNames))
lenStr("mime")
lenStr("description")
d.FieldU32("width")
Expand Down
4 changes: 2 additions & 2 deletions format/flv/flv.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func flvDecode(d *decode.D, in interface{}) interface{} {
}

fieldScriptDataValue := func(d *decode.D, _ string) uint64 {
typ := d.FieldU8("type", d.MapUToStr(typeNames))
typ := d.FieldU8("type", d.MapUToStrSym(typeNames))
if typ == typeECMAArray {
d.FieldU32("ecma_array_length")
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func flvDecode(d *decode.D, in interface{}) interface{} {
for !d.End() {
d.FieldStruct("tag", func(d *decode.D) {
d.FieldU32("previous_tag_size")
tagType := d.FieldU8("tag_type", d.MapUToStr(tagTypeNames))
tagType := d.FieldU8("tag_type", d.MapUToStrSym(tagTypeNames))
dataSize := d.FieldU24("data_size")
d.FieldU24("timestamp")
d.FieldU8("timestamp_extended")
Expand Down
2 changes: 1 addition & 1 deletion format/gif/gif.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func gifDecode(d *decode.D, in interface{}) interface{} {
case 0x21: /* "!" */
d.FieldStruct("extension_block", func(d *decode.D) {
d.FieldU8("introducer")
functionCode := d.FieldU8("function_code", d.MapUToStr(extensionNames), d.Hex)
functionCode := d.FieldU8("function_code", d.MapUToStrSym(extensionNames), d.Hex)

dataBytes := &bytes.Buffer{}

Expand Down
10 changes: 5 additions & 5 deletions format/gzip/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var deflateExtraFlagsNames = decode.UToStr{

func gzDecode(d *decode.D, in interface{}) interface{} {
d.FieldRawLen("identification", 2*8, d.AssertBitBuf([]byte("\x1f\x8b")))
compressionMethod := d.FieldU8("compression_method", d.MapUToStr(compressionMethodNames))
compressionMethod := d.FieldU8("compression_method", d.MapUToStrSym(compressionMethodNames))
hasHeaderCRC := false
hasExtra := false
hasName := false
Expand All @@ -76,21 +76,21 @@ func gzDecode(d *decode.D, in interface{}) interface{} {
d.FieldU32LE("mtime") // TODO: unix time
switch compressionMethod {
case delfateMethod:
d.FieldU8("extra_flags", d.MapUToStr(deflateExtraFlagsNames))
d.FieldU8("extra_flags", d.MapUToStrSym(deflateExtraFlagsNames))
default:
d.FieldU8("extra_flags")
}
d.FieldU8("os", d.MapUToStr(osNames))
d.FieldU8("os", d.MapUToStrSym(osNames))
if hasExtra {
// TODO:
xLen := d.FieldU16("xlen")
d.FieldRawLen("extra_fields", int64(xLen*8))
}
if hasName {
d.FieldUTF8NullTerminated("name")
d.FieldUTF8Null("name")
}
if hasComment {
d.FieldUTF8NullTerminated("comment")
d.FieldUTF8Null("comment")
}
if hasHeaderCRC {
// TODO: validate
Expand Down
38 changes: 19 additions & 19 deletions format/icc/icc_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func xyzType(d *decode.D) {
}

func textType(d *decode.D) {
d.FieldUTF8NullTerminatedLen("text", int(d.BitsLeft()/8))
d.FieldUTF8NullFixedLen("text", int(d.BitsLeft()/8))
}

func paraType(d *decode.D) {
Expand All @@ -37,13 +37,13 @@ func paraType(d *decode.D) {

func descType(d *decode.D) {
descLen := d.FieldU32("description_length")
d.FieldUTF8NullTerminatedLen("description", int(descLen))
d.FieldUTF8NullFixedLen("description", int(descLen))
d.FieldU32("language_code")
localDescLen := d.FieldU32("localizable_description_length")
d.FieldUTF8NullTerminatedLen("localizable_description", int(localDescLen))
d.FieldUTF8NullFixedLen("localizable_description", int(localDescLen))
d.FieldU16("script_code")
d.FieldU8("macintosh_description_length")
d.FieldUTF8NullTerminatedLen("macintosh_description", 67)
d.FieldUTF8NullFixedLen("macintosh_description", 67)
}

var typToDecode = map[string]func(d *decode.D){
Expand Down Expand Up @@ -87,13 +87,13 @@ func iccProfileDecode(d *decode.D, in interface{}) interface{} {
d.LenFn(int64(size)*8, func(d *decode.D) {
d.FieldStruct("header", func(d *decode.D) {
d.FieldU32("size")
d.FieldUTF8NullTerminatedLen("cmm_type_signature", 4)
d.FieldUTF8NullFixedLen("cmm_type_signature", 4)
d.FieldUFn("version_major", decodeBCDU8)
d.FieldUFn("version_minor", decodeBCDU8)
d.FieldU16("version_reserved")
d.FieldUTF8NullTerminatedLen("device_class_signature", 4)
d.FieldUTF8NullTerminatedLen("color_space", 4)
d.FieldUTF8NullTerminatedLen("connection_space", 4)
d.FieldUTF8NullFixedLen("device_class_signature", 4)
d.FieldUTF8NullFixedLen("color_space", 4)
d.FieldUTF8NullFixedLen("connection_space", 4)
d.FieldStruct("timestamp", func(d *decode.D) {
d.FieldU16("year")
d.FieldU16("month")
Expand All @@ -103,16 +103,16 @@ func iccProfileDecode(d *decode.D, in interface{}) interface{} {
d.FieldU16("seconds")

})
d.FieldUTF8NullTerminatedLen("file_signature", 4)
d.FieldUTF8NullTerminatedLen("primary_platform", 4)
d.FieldUTF8NullFixedLen("file_signature", 4)
d.FieldUTF8NullFixedLen("primary_platform", 4)
d.FieldU32("flags")
d.FieldUTF8NullTerminatedLen("device_manufacturer", 4)
d.FieldUTF8NullTerminatedLen("device_model", 4)
d.FieldUTF8NullTerminatedLen("device_attribute", 8)
d.FieldUTF8NullTerminatedLen("render_intent", 4)
d.FieldUTF8NullTerminatedLen("xyz_illuminant", 12)
d.FieldUTF8NullTerminatedLen("profile_creator_signature", 4)
d.FieldUTF8NullTerminatedLen("profile_id", 16)
d.FieldUTF8NullFixedLen("device_manufacturer", 4)
d.FieldUTF8NullFixedLen("device_model", 4)
d.FieldUTF8NullFixedLen("device_attribute", 8)
d.FieldUTF8NullFixedLen("render_intent", 4)
d.FieldUTF8NullFixedLen("xyz_illuminant", 12)
d.FieldUTF8NullFixedLen("profile_creator_signature", 4)
d.FieldUTF8NullFixedLen("profile_id", 16)
d.FieldRawLen("reserved", 28*8, d.BitBufIsZero)
})

Expand All @@ -121,12 +121,12 @@ func iccProfileDecode(d *decode.D, in interface{}) interface{} {
d.FieldArray("table", func(d *decode.D) {
for i := uint64(0); i < tagCount; i++ {
d.FieldStruct("element", func(d *decode.D) {
d.FieldUTF8NullTerminatedLen("signature", 4)
d.FieldUTF8NullFixedLen("signature", 4)
offset := d.FieldU32("offset")
size := d.FieldU32("size")

d.RangeFn(int64(offset)*8, int64(size)*8, func(d *decode.D) {
typ := d.FieldUTF8NullTerminatedLen("type", 4)
typ := d.FieldUTF8NullFixedLen("type", 4)
d.FieldU32("reserved")

if fn, ok := typToDecode[typ]; ok {
Expand Down
12 changes: 6 additions & 6 deletions format/id3/id3v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func id3v1Decode(d *decode.D, in interface{}) interface{} {
if d.PeekBits(8) == uint64('+') {
d.Error("looks like id3v11")
}
d.FieldUTF8NullTerminatedLen("song_name", 30)
d.FieldUTF8NullTerminatedLen("artist", 30)
d.FieldUTF8NullTerminatedLen("album_name", 30)
d.FieldUTF8NullTerminatedLen("year", 4)
d.FieldUTF8NullTerminatedLen("comment", 30)
d.FieldUTF8NullFixedLen("song_name", 30)
d.FieldUTF8NullFixedLen("artist", 30)
d.FieldUTF8NullFixedLen("album_name", 30)
d.FieldUTF8NullFixedLen("year", 4)
d.FieldUTF8NullFixedLen("comment", 30)
// from https://en.wikipedia.org/wiki/List_of_ID3v1_Genres
d.FieldU8("genre", d.MapUToStr(decode.UToStr{
d.FieldU8("genre", d.MapUToStrSym(decode.UToStr{
0: "Blues",
1: "Classic Rock",
2: "Country",
Expand Down
2 changes: 1 addition & 1 deletion format/id3/id3v11.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func id3v11Decode(d *decode.D, in interface{}) interface{} {
d.FieldUTF8("title", 60)
d.FieldUTF8("artist", 60)
d.FieldUTF8("album", 60)
d.FieldU8("speed", d.MapUToStr(decode.UToStr{
d.FieldU8("speed", d.MapUToStrSym(decode.UToStr{
0: "unset",
1: "slow",
2: "medium",
Expand Down

0 comments on commit ede2e77

Please sign in to comment.