MPEG2TS的Scapy层,带有用于自适应的条件字段

2024-05-13 23:00:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试为scapy创建MPEG2-TS层。在

我需要根据自适应字段“添加”和解析其他字段。在

这是我的代码:

_adapt_type = {
0x0 : 'Reserved',
0x1 : 'Payload only',
0x2 : 'Adaptation only',
0x3 : 'Adaptation and payload',
}

class MPEG2_TS(Packet):
    name = "MPEG2-TS"
    fields_desc=[

            ByteEnumField('sync_byte', 0x47, _sync_type),
            BitField('trans_err',0x0, 1),
            # Transport Error Indicator (TEI) if set to 1 decoder ignore packet

            BitField('start_ind',0x0, 1),
            BitField('trans_pri',0x0, 1),
            XBitField('pid', 0xA, 13),
            BitEnumField('trans_scramb', 0x0, 2, _scramble_type),
            BitEnumField('adapt_ctrl', 0x0, 2, _adapt_type),
            BitField('cont_count', 0x0, 4),
            ]

如果adapt_ctrl设置为:

^{pr2}$

我怎样才能创建一个条件来根据这个字段的结果来解析它呢? 如果设置了“Payload Only”位,那么头后面没有自适应字段,因此我们不需要解析它。 如果设置了“Adaptation and payload”,我需要使用以下结构解析Adaptation字段:

_adapt_fields = [

            # Number of bytes in the adaptation field immediately following this byte
            ByteField('ada_len', 0x0),

            # Set to 1 if current TS packet is in a discontinuity state with respect to either the continuity counter or the program clock reference
            BitField('dis', 0x0, 1),

            # Set to 1 if the PES packet in this TS packet starts a video/audio sequence
            BitField('ran_acc', 0x0, 1),

            # 1 = higher priority
            BitField('ES_str_pri', 0x0, 1),

            # Set to 1 if adaptation field contains a PCR field
            BitField('PCR_fla', 0x0, 1),                

            # Set to 1 if adaptation field contains an OPCR field
            BitField('OPCR_fla', 0x0, 1),

            # Set to 1 if adaptation field contains a splice countdown field
            BitField('spl_fla', 0x0, 1),

            # Set to 1 if adaptation field contains private data bytes
            BitField('trs_pri_dat_fla', 0x0, 1),

            # Set to 1 if adaptation field contains an extension
            BitField('ada_ext_fla', 0x0, 1),                

            # variable Depends on flags
            # BitField('ada_ext_fla', 0x0, 1),              
            # PCR   33+6+9      Program clock reference, stored in 6 octets in big-endian as 33 bits base, 6 bits padding, 9 bits extension.
            # OPCR  33+6+9      Original Program clock reference. Helps when one TS is copied into another
            # Splice countdown  8       Indicates how many TS packets from this one a splicing point occurs (may be negative)
            # Stuffing bytes    variable        
            ]

我试了一些条件,但到现在还没有运气。 谢谢你的帮助。在


Tags: thetoinfieldifpackettypeset