1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 00:57:15 +03:00
fq/format/format.go

402 lines
13 KiB
Go
Raw Normal View History

package format
import (
"path/filepath"
"strings"
"github.com/wader/fq/pkg/decode"
)
// TODO: do before-format somehow and topology sort?
const (
ProbeOrderBinUnique = 0 // binary with unlikely overlap
ProbeOrderBinFuzzy = 100 // binary with possible overlap
ProbeOrderTextJSON = 200 // text json has prio as yaml overlap
ProbeOrderTextFuzzy = 300 // text with possible overlap
)
// TODO: move to group package somehow?
type Probe_In struct {
IsProbe bool
Filename string
}
// Use HasExt("cer", "CeR", ...)
func (pi Probe_In) HasExt(ss ...string) bool {
ext := filepath.Ext(pi.Filename)
if ext == "" {
return false
}
ext = ext[1:]
for _, s := range ss {
if strings.EqualFold(s, ext) {
return true
}
}
return false
}
type Probe_Args_In struct {
IsProbeArgs bool
Filename string
DecodeGroup string
}
var (
2023-05-01 14:19:04 +03:00
Image = &decode.Group{Name: "image"}
Probe = &decode.Group{Name: "probe", DefaultInArg: Probe_In{}}
Probe_Args = &decode.Group{Name: "probe_args", DefaultInArg: Probe_Args_In{}}
2023-05-01 14:19:04 +03:00
Link_Frame = &decode.Group{Name: "link_frame", DefaultInArg: Link_Frame_In{}} // ex: ethernet
INET_Packet = &decode.Group{Name: "inet_packet", DefaultInArg: INET_Packet_In{}} // ex: ipv4
IP_Packet = &decode.Group{Name: "ip_packet", DefaultInArg: INET_Packet_In{}} // ex: tcp
TCP_Stream = &decode.Group{Name: "tcp_stream", DefaultInArg: TCP_Stream_In{}} // ex: http
UDP_Payload = &decode.Group{Name: "udp_payload", DefaultInArg: UDP_Payload_In{}} // ex: dns
MP3_Frame_Tags = &decode.Group{Name: "mp3_frame_tags"}
Bytes = &decode.Group{Name: "bytes"}
Bits = &decode.Group{Name: "bits"}
2023-05-01 14:19:04 +03:00
AAC_Frame = &decode.Group{Name: "aac_frame"}
ADTS = &decode.Group{Name: "adts"}
ADTS_Frame = &decode.Group{Name: "adts_frame"}
AIFF = &decode.Group{Name: "aiff"}
AMF0 = &decode.Group{Name: "amf0"}
Apev2 = &decode.Group{Name: "apev2"}
Apple_Bookmark = &decode.Group{Name: "apple_bookmark"}
AR = &decode.Group{Name: "ar"}
ASN1_BER = &decode.Group{Name: "asn1_ber"}
AV1_CCR = &decode.Group{Name: "av1_ccr"}
AV1_Frame = &decode.Group{Name: "av1_frame"}
AV1_OBU = &decode.Group{Name: "av1_obu"}
AVC_Annexb = &decode.Group{Name: "avc_annexb"}
AVC_AU = &decode.Group{Name: "avc_au"}
AVC_DCR = &decode.Group{Name: "avc_dcr"}
AVC_NALU = &decode.Group{Name: "avc_nalu"}
AVC_PPS = &decode.Group{Name: "avc_pps"}
AVC_SEI = &decode.Group{Name: "avc_sei"}
AVC_SPS = &decode.Group{Name: "avc_sps"}
AVI = &decode.Group{Name: "avi"}
Avro_Ocf = &decode.Group{Name: "avro_ocf"}
Bencode = &decode.Group{Name: "bencode"}
Bitcoin_Blkdat = &decode.Group{Name: "bitcoin_blkdat"}
Bitcoin_Block = &decode.Group{Name: "bitcoin_block"}
Bitcoin_Script = &decode.Group{Name: "bitcoin_script"}
Bitcoin_Transaction = &decode.Group{Name: "bitcoin_transaction"}
Opentimestamps = &decode.Group{Name: "opentimestamps"}
2023-05-01 14:19:04 +03:00
Bplist = &decode.Group{Name: "bplist"}
BSD_Loopback_Frame = &decode.Group{Name: "bsd_loopback_frame"}
BSON = &decode.Group{Name: "bson"}
Bzip2 = &decode.Group{Name: "bzip2"}
2023-08-17 23:17:01 +03:00
CAFF = &decode.Group{Name: "caff"}
2023-05-01 14:19:04 +03:00
CBOR = &decode.Group{Name: "cbor"}
CSV = &decode.Group{Name: "csv"}
DNS = &decode.Group{Name: "dns"}
DNS_TCP = &decode.Group{Name: "dns_tcp"}
ELF = &decode.Group{Name: "elf"}
Ether_8023_Frame = &decode.Group{Name: "ether8023_frame"}
Exif = &decode.Group{Name: "exif"}
Fairplay_SPC = &decode.Group{Name: "fairplay_spc"}
FLAC = &decode.Group{Name: "flac"}
FLAC_Frame = &decode.Group{Name: "flac_frame"}
FLAC_Metadatablock = &decode.Group{Name: "flac_metadatablock"}
FLAC_Metadatablocks = &decode.Group{Name: "flac_metadatablocks"}
FLAC_Picture = &decode.Group{Name: "flac_picture"}
FLAC_Streaminfo = &decode.Group{Name: "flac_streaminfo"}
FLV = &decode.Group{Name: "flv"}
GIF = &decode.Group{Name: "gif"}
Gzip = &decode.Group{Name: "gzip"}
HEVC_Annexb = &decode.Group{Name: "hevc_annexb"}
HEVC_AU = &decode.Group{Name: "hevc_au"}
HEVC_DCR = &decode.Group{Name: "hevc_dcr"}
HEVC_NALU = &decode.Group{Name: "hevc_nalu"}
HEVC_PPS = &decode.Group{Name: "hevc_pps"}
HEVC_SPS = &decode.Group{Name: "hevc_sps"}
HEVC_VPS = &decode.Group{Name: "hevc_vps"}
HTML = &decode.Group{Name: "html"}
ICC_Profile = &decode.Group{Name: "icc_profile"}
ICMP = &decode.Group{Name: "icmp"}
ICMPv6 = &decode.Group{Name: "icmpv6"}
ID3v1 = &decode.Group{Name: "id3v1"}
ID3v11 = &decode.Group{Name: "id3v11"}
ID3v2 = &decode.Group{Name: "id3v2"}
IPv4Packet = &decode.Group{Name: "ipv4_packet"}
IPv6Packet = &decode.Group{Name: "ipv6_packet"}
JPEG = &decode.Group{Name: "jpeg"}
JSON = &decode.Group{Name: "json"}
JSONL = &decode.Group{Name: "jsonl"}
2023-12-05 13:31:30 +03:00
LDB = &decode.Group{Name: "leveldb_ldb"}
2023-06-20 19:24:49 +03:00
LuaJIT = &decode.Group{Name: "luajit"}
2023-05-01 14:19:04 +03:00
MachO = &decode.Group{Name: "macho"}
MachO_Fat = &decode.Group{Name: "macho_fat"}
Markdown = &decode.Group{Name: "markdown"}
Matroska = &decode.Group{Name: "matroska"}
2023-08-18 06:07:55 +03:00
MOC3 = &decode.Group{Name: "moc3"}
2023-05-01 14:19:04 +03:00
MP3 = &decode.Group{Name: "mp3"}
MP3_Frame = &decode.Group{Name: "mp3_frame"}
MP3_Frame_VBRI = &decode.Group{Name: "mp3_frame_vbri"}
MP3_Frame_XING = &decode.Group{Name: "mp3_frame_xing"}
MP4 = &decode.Group{Name: "mp4"}
MPEG_ASC = &decode.Group{Name: "mpeg_asc"}
MPEG_ES = &decode.Group{Name: "mpeg_es"}
MPES_PES = &decode.Group{Name: "mpeg_pes"}
MPEG_PES_Packet = &decode.Group{Name: "mpeg_pes_packet"}
MPEG_SPU = &decode.Group{Name: "mpeg_spu"}
MPEG_TS = &decode.Group{Name: "mpeg_ts"}
MsgPack = &decode.Group{Name: "msgpack"}
Ogg = &decode.Group{Name: "ogg"}
Ogg_Page = &decode.Group{Name: "ogg_page"}
Opus_Packet = &decode.Group{Name: "opus_packet"}
PCAP = &decode.Group{Name: "pcap"}
PCAPNG = &decode.Group{Name: "pcapng"}
2023-05-04 08:45:42 +03:00
Pg_BTree = &decode.Group{Name: "pg_btree"}
Pg_Control = &decode.Group{Name: "pg_control"}
Pg_Heap = &decode.Group{Name: "pg_heap"}
2023-05-01 14:19:04 +03:00
PNG = &decode.Group{Name: "png"}
Prores_Frame = &decode.Group{Name: "prores_frame"}
Protobuf = &decode.Group{Name: "protobuf"}
ProtobufWidevine = &decode.Group{Name: "protobuf_widevine"}
PSSH_Playready = &decode.Group{Name: "pssh_playready"}
RTMP = &decode.Group{Name: "rtmp"}
SLL_Packet = &decode.Group{Name: "sll_packet"}
SLL2_Packet = &decode.Group{Name: "sll2_packet"}
TAR = &decode.Group{Name: "tar"}
TCP_Segment = &decode.Group{Name: "tcp_segment"}
TIFF = &decode.Group{Name: "tiff"}
TLS = &decode.Group{Name: "tls"}
TOML = &decode.Group{Name: "toml"}
Tzif = &decode.Group{Name: "tzif"}
UDP_Datagram = &decode.Group{Name: "udp_datagram"}
Vorbis_Comment = &decode.Group{Name: "vorbis_comment"}
Vorbis_Packet = &decode.Group{Name: "vorbis_packet"}
VP8_Frame = &decode.Group{Name: "vp8_frame"}
VP9_CFM = &decode.Group{Name: "vp9_cfm"}
VP9_Frame = &decode.Group{Name: "vp9_frame"}
VPX_CCR = &decode.Group{Name: "vpx_ccr"}
WASM = &decode.Group{Name: "wasm"}
WAV = &decode.Group{Name: "wav"}
WebP = &decode.Group{Name: "webp"}
XML = &decode.Group{Name: "xml"}
YAML = &decode.Group{Name: "yaml"}
Zip = &decode.Group{Name: "zip"}
)
// below are data types used to communicate between formats <FormatName>In/Out
2023-05-01 14:19:04 +03:00
type AAC_Frame_In struct {
ObjectType int `doc:"Audio object type"`
}
2023-05-01 14:19:04 +03:00
type AVC_AU_In struct {
LengthSize uint64 `doc:"Length value size"`
}
2023-05-01 14:19:04 +03:00
type AVC_DCR_Out struct {
LengthSize uint64
}
2023-08-17 23:17:01 +03:00
type CAFF_In struct {
Uncompress bool `doc:"Uncompress and probe files"`
}
2023-05-01 14:19:04 +03:00
type FLAC_Frame_In struct {
SamplesBuf []byte
BitsPerSample int `doc:"Bits per sample"`
}
2023-05-01 14:19:04 +03:00
type FLAC_Frame_Out struct {
SamplesBuf []byte
Samples uint64
Channels int
BitsPerSample int
}
2023-05-01 14:19:04 +03:00
type FLAC_Stream_Info struct {
SampleRate uint64
BitsPerSample uint64
TotalSamplesInStream uint64
MD5 []byte
}
2023-05-01 14:19:04 +03:00
type FLAC_Streaminfo_Out struct {
StreamInfo FLAC_Stream_Info
}
2023-05-01 14:19:04 +03:00
type FLAC_Metadatablock_Out struct {
IsLastBlock bool
HasStreamInfo bool
2023-05-01 14:19:04 +03:00
StreamInfo FLAC_Stream_Info
}
2023-05-01 14:19:04 +03:00
type FLAC_Metadatablocks_Out struct {
HasStreamInfo bool
2023-05-01 14:19:04 +03:00
StreamInfo FLAC_Stream_Info
}
2023-05-01 14:19:04 +03:00
type HEVC_AU_In struct {
LengthSize uint64 `doc:"Length value size"`
}
2023-05-01 14:19:04 +03:00
type HEVC_DCR_Out struct {
LengthSize uint64
}
2023-05-01 14:19:04 +03:00
type Ogg_Page_Out struct {
IsLastPage bool
IsFirstPage bool
IsContinuedPacket bool
StreamSerialNumber uint32
SequenceNo uint32
Segments [][]byte
}
2023-05-01 14:19:04 +03:00
type Protobuf_In struct {
Message ProtoBufMessage
}
2023-05-01 14:19:04 +03:00
type Matroska_In struct {
DecodeSamples bool `doc:"Decode samples"`
}
2023-05-01 14:19:04 +03:00
type MP3_In struct {
MaxUniqueHeaderConfigs int `doc:"Max number of unique frame header configs allowed"`
MaxUnknown int `doc:"Max percent (0-100) unknown bits"`
MaxSyncSeek int `doc:"Max byte distance to next sync"`
}
2023-05-01 14:19:04 +03:00
type MP3_Frame_Out struct {
MPEGVersion int
ProtectionAbsent bool
BitRate int
SampleRate int
ChannelsIndex int
ChannelModeIndex int
}
2023-05-01 14:19:04 +03:00
type MPEG_Decoder_Config struct {
ObjectType int
ASCObjectType int
}
2023-05-01 14:19:04 +03:00
type MPEG_ES_Out struct {
DecoderConfigs []MPEG_Decoder_Config
}
2023-05-01 14:19:04 +03:00
type MPEG_ASC_Out struct {
ObjectType int
}
2023-05-01 14:19:04 +03:00
type Link_Frame_In struct {
Type int
IsLittleEndian bool // pcap endian etc
}
2023-05-01 14:19:04 +03:00
type INET_Packet_In struct {
EtherType int
}
2023-05-01 14:19:04 +03:00
type IP_Packet_In struct {
Protocol int
}
2023-05-01 14:19:04 +03:00
type UDP_Payload_In struct {
SourcePort int
DestinationPort int
}
2023-05-01 14:19:04 +03:00
func (u UDP_Payload_In) IsPort(ports ...int) bool {
for _, p := range ports {
if u.DestinationPort == p || u.SourcePort == p {
return true
}
}
return false
}
2023-05-01 14:19:04 +03:00
func (u UDP_Payload_In) MustIsPort(fn func(format string, a ...any), ports ...int) {
if !u.IsPort(ports...) {
fn("incorrect udp port %t src:%d dst:%d", u.DestinationPort, u.SourcePort)
}
}
2023-05-01 14:19:04 +03:00
type TCP_Stream_In struct {
IsClient bool
HasStart bool
HasEnd bool
SkippedBytes uint64
SourcePort int
DestinationPort int
}
2023-05-01 14:19:04 +03:00
type TCP_Stream_Out struct {
PostFn func(peerIn any)
InArg any
}
2023-05-01 14:19:04 +03:00
func (t TCP_Stream_In) IsPort(ports ...int) bool {
for _, p := range ports {
if (t.IsClient && t.DestinationPort == p) ||
(!t.IsClient && t.SourcePort == p) {
return true
}
}
return false
}
2023-05-01 14:19:04 +03:00
func (t TCP_Stream_In) MustIsPort(fn func(format string, a ...any), ports ...int) {
if !t.IsPort(ports...) {
fn("incorrect tcp port client %t src:%d dst:%d", t.IsClient, t.DestinationPort, t.SourcePort)
}
}
2023-05-01 14:19:04 +03:00
type MP4_In struct {
DecodeSamples bool `doc:"Decode samples"`
AllowTruncated bool `doc:"Allow box to be truncated"`
}
2023-05-01 14:19:04 +03:00
type AVI_In struct {
DecodeSamples bool `doc:"Decode samples"`
DecodeExtendedChunks bool `doc:"Decode extended chunks"`
}
2023-05-01 14:19:04 +03:00
type Zip_In struct {
2022-05-26 20:27:17 +03:00
Uncompress bool `doc:"Uncompress and probe files"`
}
2023-05-01 14:19:04 +03:00
type XML_In struct {
Seq bool `doc:"Use seq attribute to preserve element order"`
Array bool `doc:"Decode as nested arrays"`
AttributePrefix string `doc:"Prefix for attribute keys"`
}
2023-05-01 14:19:04 +03:00
type HTML_In struct {
Seq bool `doc:"Use seq attribute to preserve element order"`
Array bool `doc:"Decode as nested arrays"`
AttributePrefix string `doc:"Prefix for attribute keys"`
}
2023-05-01 14:19:04 +03:00
type CSV_In struct {
Comma string `doc:"Separator character"`
Comment string `doc:"Comment line character"`
}
2023-05-01 14:19:04 +03:00
type Bitcoin_Block_In struct {
HasHeader bool `doc:"Has blkdat header"`
}
2023-05-01 14:19:04 +03:00
type TLS_In struct {
Keylog string `doc:"NSS Key Log content"`
}
2023-05-04 08:45:42 +03:00
type Pg_Control_In struct {
2022-09-23 13:50:54 +03:00
Flavour string `doc:"PostgreSQL flavour: postgres14, pgproee14.., postgres10"`
}
2023-05-04 08:45:42 +03:00
type Pg_Heap_In struct {
Flavour string `doc:"PostgreSQL flavour: postgres14, pgproee14.., postgres10"`
Page int `doc:"First page number in file, default is 0"`
Segment int `doc:"Segment file number (16790.1 is 1), default is 0"`
}
2023-05-04 08:45:42 +03:00
type Pg_BTree_In struct {
Page int `doc:"First page number in file, default is 0"`
}