1
1
mirror of https://github.com/wader/fq.git synced 2024-09-19 15:57:29 +03:00

decode,format: Add d.FieldFormatOrRaw(Len)

This commit is contained in:
Mattias Wadman 2022-04-11 22:45:46 +02:00
parent 9a053f0a3d
commit b35b1804be
18 changed files with 70 additions and 78 deletions

View File

@ -59,11 +59,7 @@ func apev2Decode(d *decode.D, in interface{}) interface{} {
d.FramedFn(int64(itemSize)*8, func(d *decode.D) {
d.FieldUTF8Null("filename")
// assume image if binary
dv, _, _ := d.TryFieldFormat("value", imageFormat, nil)
if dv == nil {
// TODO: framed and unknown instead?
d.FieldRawLen("value", d.BitsLeft())
}
d.FieldFormatOrRaw("value", imageFormat, nil)
})
} else {
d.FieldUTF8("value", int(itemSize))

View File

@ -37,10 +37,7 @@ func decodeAr(d *decode.D, in interface{}) interface{} {
}
size := int64(sizeS.SymU()) * 8
d.FieldUTF8("ending_characters", 2)
dv, _, _ := d.TryFieldFormatLen("data", size, probeFormat, nil)
if dv == nil {
d.FieldRawLen("data", size)
}
d.FieldFormatOrRawLen("data", size, probeFormat, nil)
padding := d.AlignBits(16)
if padding > 0 {
d.FieldRawLen("padding", int64(padding))

View File

@ -57,9 +57,7 @@ func pictureDecode(d *decode.D, in interface{}) interface{} {
d.FieldU32("color_depth")
d.FieldU32("number_of_index_colors")
pictureLen := d.FieldU32("picture_length")
if dv, _, _ := d.TryFieldFormatLen("picture_data", int64(pictureLen)*8, images, nil); dv == nil {
d.FieldRawLen("picture_data", int64(pictureLen)*8)
}
d.FieldFormatOrRawLen("picture_data", int64(pictureLen)*8, images, nil)
return nil
}

View File

@ -430,9 +430,7 @@ func decodeFrame(d *decode.D, version int) uint64 {
d.FieldUTF8("image_format", 3)
d.FieldU8("picture_type") // TODO: table
d.FieldStrFn("description", textNullFn(int(encoding)))
if dv, _, _ := d.TryFieldFormatLen("picture", d.BitsLeft(), imageFormat, nil); dv == nil {
d.FieldRawLen("picture", d.BitsLeft())
}
d.FieldFormatOrRawLen("picture", d.BitsLeft(), imageFormat, nil)
},
// <Header for 'Attached picture', ID: "APIC">
@ -446,9 +444,7 @@ func decodeFrame(d *decode.D, version int) uint64 {
d.FieldStrFn("mime_type", textNullFn(encodingUTF8))
d.FieldU8("picture_type") // TODO: table
d.FieldStrFn("description", textNullFn(int(encoding)))
if dv, _, _ := d.TryFieldFormatLen("picture", d.BitsLeft(), imageFormat, nil); dv == nil {
d.FieldRawLen("picture", d.BitsLeft())
}
d.FieldFormatOrRawLen("picture", d.BitsLeft(), imageFormat, nil)
},
// <Header for 'General encapsulated object', ID: "GEOB">
@ -462,9 +458,7 @@ func decodeFrame(d *decode.D, version int) uint64 {
d.FieldStrFn("mime_type", textNullFn(encodingUTF8))
d.FieldStrFn("filename", textNullFn(int(encoding)))
d.FieldStrFn("description", textNullFn(int(encoding)))
if dv, _, _ := d.TryFieldFormatLen("data", d.BitsLeft(), imageFormat, nil); dv == nil {
d.FieldRawLen("data", d.BitsLeft())
}
d.FieldFormatOrRawLen("data", d.BitsLeft(), imageFormat, nil)
},
// Unsynced lyrics/text "ULT"

View File

@ -52,14 +52,13 @@ func decodeLoopbackFrame(d *decode.D, in interface{}) interface{} {
networkLayer := d.FieldU32("network_layer", bsdLookbackNetworkLayerMap, scalar.Hex)
if dv, _, _ := d.TryFieldFormatLen(
d.FieldFormatOrRawLen(
"payload",
d.BitsLeft(),
bsdLoopbackFrameInetPacketGroup,
// TODO: unknown mapped to ether type 0 is ok?
format.InetPacketIn{EtherType: bsdLoopbackFrameNetworkLayerEtherType[networkLayer]}); dv == nil {
d.FieldRawLen("payload", d.BitsLeft())
}
format.InetPacketIn{EtherType: bsdLoopbackFrameNetworkLayerEtherType[networkLayer]},
)
return nil
}

View File

@ -45,13 +45,12 @@ func decodeEthernetFrame(d *decode.D, in interface{}) interface{} {
d.FieldU("source", 48, mapUToEtherSym, scalar.Hex)
etherType := d.FieldU16("ether_type", format.EtherTypeMap, scalar.Hex)
if dv, _, _ := d.TryFieldFormatLen(
d.FieldFormatOrRawLen(
"payload",
d.BitsLeft(),
ether8023FrameInetPacketGroup,
format.InetPacketIn{EtherType: int(etherType)}); dv == nil {
d.FieldRawLen("payload", d.BitsLeft())
}
format.InetPacketIn{EtherType: int(etherType)},
)
return nil
}

View File

@ -102,12 +102,13 @@ func decodeIPv4(d *decode.D, in interface{}) interface{} {
if moreFragments || fragmentOffset > 0 {
d.FieldRawLen("payload", dataLen)
} else if dv, _, _ := d.TryFieldFormatLen(
} else {
d.FieldFormatOrRawLen(
"payload",
dataLen,
ipv4IpPacketGroup,
format.IPPacketIn{Protocol: int(protocol)}); dv == nil {
d.FieldRawLen("payload", dataLen)
format.IPPacketIn{Protocol: int(protocol)},
)
}
return nil

View File

@ -162,14 +162,12 @@ func decodeIPv6(d *decode.D, in interface{}) interface{} {
// TODO: nextHeader 59 skip
payloadLen := int64(dataLength)*8 - extLen
if dv, _, _ := d.TryFieldFormatLen(
d.FieldFormatOrRawLen(
"payload",
payloadLen,
ipv4IpPacketGroup,
format.IPPacketIn{Protocol: int(nextHeader)}); dv == nil {
d.FieldRawLen("payload", payloadLen)
}
format.IPPacketIn{Protocol: int(nextHeader)},
)
return nil
}

View File

@ -52,13 +52,12 @@ func decodeSLL2(d *decode.D, in interface{}) interface{} {
switch arpHdrType {
case arpHdrTypeLoopback, arpHdrTypeEther:
_ = d.FieldMustGet("link_address").TryScalarFn(mapUToEtherSym, scalar.Hex)
if dv, _, _ := d.TryFieldFormatLen(
d.FieldFormatOrRawLen(
"payload",
d.BitsLeft(),
sllPacket2InetPacketGroup,
format.LinkFrameIn{Type: int(protcolType)}); dv == nil {
d.FieldRawLen("payload", d.BitsLeft())
}
format.LinkFrameIn{Type: int(protcolType)},
)
default:
d.FieldRawLen("payload", d.BitsLeft())
}

View File

@ -129,13 +129,12 @@ func decodeSLL(d *decode.D, in interface{}) interface{} {
case arpHdrTypeLoopback, arpHdrTypeEther:
_ = d.FieldMustGet("link_address").TryScalarFn(mapUToEtherSym, scalar.Hex)
protcolType := d.FieldU16("protocol_type", format.EtherTypeMap, scalar.Hex)
if dv, _, _ := d.TryFieldFormatLen(
d.FieldFormatOrRawLen(
"payload",
d.BitsLeft(),
sllPacketInetPacketGroup,
format.LinkFrameIn{Type: int(protcolType)}); dv == nil {
d.FieldRawLen("payload", d.BitsLeft())
}
format.LinkFrameIn{Type: int(protcolType)},
)
default:
d.FieldU16LE("protocol_type")
d.FieldRawLen("payload", d.BitsLeft())

View File

@ -32,16 +32,15 @@ func decodeUDP(d *decode.D, in interface{}) interface{} {
d.FieldU16("checksum", scalar.Hex)
payloadLen := int64(length-8) * 8
if dv, _, _ := d.TryFieldFormatLen(
d.FieldFormatOrRawLen(
"payload",
payloadLen,
udpPayloadGroup,
format.UDPPayloadIn{
SourcePort: int(sourcePort),
DestinationPort: int(destPort),
}); dv == nil {
d.FieldRawLen("payload", payloadLen)
}
},
)
// TODO: for checksum need to pass ipv4 pseudo header somehow

View File

@ -727,10 +727,7 @@ func init() {
d.FieldU24("flags")
d.FieldU32("reserved")
if ctx.isParent("covr") {
dv, _, _ := d.TryFieldFormatLen("data", d.BitsLeft(), imageFormat, nil)
if dv == nil {
d.FieldRawLen("data", d.BitsLeft())
}
d.FieldFormatOrRawLen("data", d.BitsLeft(), imageFormat, nil)
} else {
d.FieldUTF8("data", int(d.BitsLeft()/8))
}

View File

@ -85,12 +85,14 @@ func decodePcap(d *decode.D, in interface{}) interface{} {
_ = fn(fd, bs)
}
if dv, _, _ := d.TryFieldFormatLen("packet", int64(inclLen)*8, pcapLinkFrameFormat, format.LinkFrameIn{
d.FieldFormatOrRawLen(
"packet",
int64(inclLen)*8,
pcapLinkFrameFormat, format.LinkFrameIn{
Type: linkType,
IsLittleEndian: d.Endian == decode.LittleEndian,
}); dv == nil {
d.FieldRawLen("packet", int64(inclLen)*8)
}
},
)
})
}
})

View File

@ -239,12 +239,15 @@ var blockFns = map[uint64]func(d *decode.D, dc *decodeContext){
_ = fn(dc.flowDecoder, bs)
}
if dv, _, _ := d.TryFieldFormatLen("packet", int64(capturedLength)*8, pcapngLinkFrameFormat, format.LinkFrameIn{
d.FieldFormatOrRawLen(
"packet",
int64(capturedLength)*8,
pcapngLinkFrameFormat,
format.LinkFrameIn{
Type: linkType,
IsLittleEndian: d.Endian == decode.LittleEndian,
}); dv == nil {
d.FieldRawLen("packet", int64(capturedLength)*8)
}
},
)
d.FieldRawLen("padding", int64(d.AlignBits(32)))
d.FieldArray("options", func(d *decode.D) { decoodeOptions(d, enhancedPacketOptionsMap) })

View File

@ -66,10 +66,7 @@ func tarDecode(d *decode.D, in interface{}) interface{} {
d.FieldUTF8("prefix", 155, mapTrimSpaceNull)
d.FieldRawLen("header_block_padding", blockPadding(d), d.BitBufIsZero())
dv, _, _ := d.TryFieldFormatLen("data", size, probeFormat, nil)
if dv == nil {
d.FieldRawLen("data", size)
}
d.FieldFormatOrRawLen("data", size, probeFormat, nil)
d.FieldRawLen("data_block_padding", blockPadding(d), d.BitBufIsZero())
})

View File

@ -34,13 +34,13 @@ func commentDecode(d *decode.D, in interface{}) interface{} {
userCommentLength := d.FieldU32("length")
userCommentStart := d.Pos()
userComment := d.FieldUTF8("comment", int(userCommentLength))
var metadataBlockPicturePreix = "METADATA_BLOCK_PICTURE="
var metadataBlockPicturePrefix = "METADATA_BLOCK_PICTURE="
var metadataBlockPicturePrefixLower = "metadata_block_picture="
if strings.HasPrefix(userComment, metadataBlockPicturePreix) ||
if strings.HasPrefix(userComment, metadataBlockPicturePrefix) ||
strings.HasPrefix(userComment, metadataBlockPicturePrefixLower) {
base64Offset := int64(len(metadataBlockPicturePreix)) * 8
base64Offset := int64(len(metadataBlockPicturePrefix)) * 8
base64Len := int64(len(userComment))*8 - base64Offset
_, base64Br, dv, _, _ := d.TryFieldReaderRangeFormat(
"picture",

View File

@ -278,9 +278,7 @@ func zipDecode(d *decode.D, in interface{}) interface{} {
}
if compressionMethod == compressionMethodNone {
if dv, _, _ := d.TryFieldFormatLen("uncompressed", compressedSize, probeFormat, nil); dv == nil {
d.FieldRawLen("uncompressed", compressedSize)
}
d.FieldFormatOrRawLen("uncompressed", compressedSize, probeFormat, nil)
} else {
var rFn func(r io.Reader) io.Reader
switch compressionMethod {

View File

@ -969,6 +969,14 @@ func (d *D) FieldFormat(name string, group Group, inArg interface{}) (*Value, in
return dv, v
}
func (d *D) FieldFormatOrRaw(name string, group Group, inArg interface{}) (*Value, interface{}) {
dv, v, _ := d.TryFieldFormat(name, group, inArg)
if dv == nil {
d.FieldRawLen(name, d.BitsLeft())
}
return dv, v
}
func (d *D) TryFieldFormatLen(name string, nBits int64, group Group, inArg interface{}) (*Value, interface{}, error) {
dv, v, err := decode(d.Ctx, d.bitBuf, group, Options{
Name: name,
@ -999,6 +1007,14 @@ func (d *D) FieldFormatLen(name string, nBits int64, group Group, inArg interfac
return dv, v
}
func (d *D) FieldFormatOrRawLen(name string, nBits int64, group Group, inArg interface{}) (*Value, interface{}) {
dv, v, _ := d.TryFieldFormatLen(name, nBits, group, inArg)
if dv == nil {
d.FieldRawLen(name, nBits)
}
return dv, v
}
// TODO: return decooder?
func (d *D) TryFieldFormatRange(name string, firstBit int64, nBits int64, group Group, inArg interface{}) (*Value, interface{}, error) {
dv, v, err := decode(d.Ctx, d.bitBuf, group, Options{