Skip to content

Commit

Permalink
decode: Remove D.Scalar* and add d.(Try)FieldScala*Fn instead
Browse files Browse the repository at this point in the history
Idea is scalar fn should not read
  • Loading branch information
wader committed Nov 21, 2021
1 parent 046f2fd commit f40320b
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 3,252 deletions.
26 changes: 22 additions & 4 deletions format/elf/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ var classBits = decode.UToU{
2: 64,
}

//nolint:revive
const (
CLASS_32 = 1
CLASS_64 = 2
)

var osABINames = decode.UToStr{
0: "Sysv",
1: "HPUX",
Expand Down Expand Up @@ -127,26 +133,36 @@ func mapStrTable(table string) func(decode.Scalar) (decode.Scalar, error) {
func elfDecode(d *decode.D, in interface{}) interface{} {
d.AssertAtLeastBitsLeft(128 * 8)

var class uint64
var archBits int
var endian uint64

d.FieldStruct("ident", func(d *decode.D) {
d.FieldRawLen("magic", 4*8, d.AssertBitBuf([]byte("\x7fELF")))
archBits = int(d.FieldU8("class", d.MapUToUSym(classBits)))
class = d.FieldU8("class", d.MapUToUSym(classBits))
endian = d.FieldU8("data", d.MapUToStrSym(endianNames))
d.FieldU8("version")
d.FieldU8("os_abi", d.MapUToStrSym(osABINames))
d.FieldU8("abi_version")
d.FieldRawLen("pad", 7*8, d.BitBufIsZero)
})

switch class {
case CLASS_32:
archBits = 32
case CLASS_64:
archBits = 64
default:
d.Fatalf("unknown class %d", class)
}

switch endian {
case LITTLE_ENDIAN:
d.Endian = decode.LittleEndian
case BIG_ENDIAN:
d.Endian = decode.BigEndian
default:
d.Fatalf("unknown endian")
d.Fatalf("unknown endian %d", endian)
}

// TODO: hex functions?
Expand Down Expand Up @@ -235,6 +251,8 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
if shstrndx != 0 {
var strTableOffset uint64
var strTableSize uint64
// log.Printf("int64((shoff+shstrndx*shentsize)*8): %#+v\n", int64((shoff+shstrndx*shentsize)*8))
// log.Printf("int64(shentsize*8),: %#+v\n", int64(shentsize*8))
d.RangeFn(int64((shoff+shstrndx*shentsize)*8), int64(shentsize*8), func(d *decode.D) {
d.SeekRel(32)
d.SeekRel(32)
Expand Down Expand Up @@ -446,7 +464,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {

switch archBits {
case 32:
shname = d.FieldScalar("sh_name", d.ScalarU32(), mapStrTable(strIndexTable)).SymStr()
shname = d.FieldScalarUFn("sh_name", (*decode.D).U32, mapStrTable(strIndexTable)).SymStr()
typ = d.FieldU32("sh_type", d.MapUToStrSym(shTypeNames), d.Hex)
shFlags(d, archBits)
d.FieldU("sh_addr", archBits)
Expand All @@ -457,7 +475,7 @@ func elfDecode(d *decode.D, in interface{}) interface{} {
d.FieldU32("sh_addralign")
d.FieldU32("sh_entsize")
case 64:
shname = d.FieldScalar("sh_name", d.ScalarU32(), mapStrTable(strIndexTable)).SymStr()
shname = d.FieldScalarUFn("sh_name", (*decode.D).U32, mapStrTable(strIndexTable)).SymStr()
typ = d.FieldU32("sh_type", d.MapUToStrSym(shTypeNames), d.Hex)
shFlags(d, archBits)
d.FieldU("sh_addr", archBits)
Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/mp3_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ func frameDecode(d *decode.D, in interface{}) interface{} {
d.MustCopy(crcHash, d.BitBufRange(6*8, sideInfoBytes*8))

if crcValue != nil {
_ = crcValue.ScalarFn(d.ValidateBitBuf(crcHash.Sum(nil)))
_ = crcValue.TryScalarFn(d.ValidateBitBuf(crcHash.Sum(nil)))
}
d.FieldValueRaw("crc_calculated", crcHash.Sum(nil), d.RawHex)

Expand Down
2 changes: 1 addition & 1 deletion format/mpeg/mpeg_pes.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func pesDecode(d *decode.D, in interface{}) interface{} {

i := 0

spuD := d.FieldArray("spus")
spuD := d.FieldArrayValue("spus")

for d.NotEnd() {
dv, v, err := d.FieldTryFormat("packet", pesPacketFormat, nil)
Expand Down
4 changes: 2 additions & 2 deletions format/ogg/ogg.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type stream struct {
func decodeOgg(d *decode.D, in interface{}) interface{} {
validPages := 0
streams := map[uint32]*stream{}
streamsD := d.FieldArray("streams")
streamsD := d.FieldArrayValue("streams")

d.FieldArray("pages", func(d *decode.D) {
for !d.End() {
Expand All @@ -78,7 +78,7 @@ func decodeOgg(d *decode.D, in interface{}) interface{} {
var packetsD *decode.D
streamsD.FieldStruct("stream", func(d *decode.D) {
d.FieldValueU("serial_number", uint64(oggPageOut.StreamSerialNumber))
packetsD = d.FieldArray("packets")
packetsD = d.FieldArrayValue("packets")
})
s = &stream{
sequenceNo: oggPageOut.SequenceNo,
Expand Down
2 changes: 1 addition & 1 deletion format/ogg/ogg_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func pageDecode(d *decode.D, in interface{}) interface{} {
d.MustCopy(pageCRC, d.BitBufRange(startPos, pageChecksumValue.Range.Start-startPos)) // header before checksum
d.MustCopy(pageCRC, bytes.NewReader([]byte{0, 0, 0, 0})) // zero checksum bits
d.MustCopy(pageCRC, d.BitBufRange(pageChecksumValue.Range.Stop(), endPos-pageChecksumValue.Range.Stop())) // rest of page
_ = pageChecksumValue.ScalarFn(d.ValidateBitBuf(bitio.ReverseBytes(pageCRC.Sum(nil))))
_ = pageChecksumValue.TryScalarFn(d.ValidateBitBuf(bitio.ReverseBytes(pageCRC.Sum(nil))))

return p
}
23 changes: 13 additions & 10 deletions pkg/decode/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,24 +550,28 @@ func (d *D) FieldMustGet(name string) *Value {
panic(fmt.Sprintf("%s not found in struct %s", name, d.Value.Name))
}

func (d *D) FieldArray(name string, fns ...func(d *D)) *D {
func (d *D) FieldArray(name string, fn func(d *D), sfns ...ScalarFn) *D {
cd := d.FieldDecoder(name, d.bitBuf, Compound{IsArray: true, Children: new([]*Value)})
d.AddChild(cd.Value)
for _, fn := range fns {
fn(cd)
}
fn(cd)
return cd
}

func (d *D) FieldStruct(name string, fns ...func(d *D)) *D {
func (d *D) FieldArrayValue(name string) *D {
return d.FieldArray(name, func(d *D) {})
}

func (d *D) FieldStruct(name string, fn func(d *D)) *D {
cd := d.FieldDecoder(name, d.bitBuf, Compound{Children: new([]*Value)})
d.AddChild(cd.Value)
for _, fn := range fns {
fn(cd)
}
fn(cd)
return cd
}

func (d *D) FieldStructValue(name string) *D {
return d.FieldStruct(name, func(d *D) {})
}

func (d *D) FieldStructArrayLoop(name string, structName string, condFn func() bool, fn func(d *D)) *D {
return d.FieldArray(name, func(d *D) {
for condFn() {
Expand Down Expand Up @@ -817,8 +821,6 @@ func (d *D) TryFieldFormatBitBuf(name string, bb *bitio.Buffer, group Group, inA

dv.Range.Start = d.Pos()

// log.Printf("FieldTryFormatBitBuf dv.Range: %#+v\n", dv.Range)

d.AddChild(dv)

return dv, v, err
Expand Down Expand Up @@ -897,6 +899,7 @@ func (d *D) TryFieldReaderRangeFormat(name string, startBit int64, nBits int64,
return 0, nil, nil, nil, err
}
dv, v, err := d.TryFieldFormatBitBuf(name, rbb, group, inArg)

return cz, rbb, dv, v, err
}

Expand Down

0 comments on commit f40320b

Please sign in to comment.