Kaitai Struct: transfer some field to achieve fault tolerance

is there any way to pass any field when analyzing a truncated log in Kaitai Struct? Because if it reads a field (type indicate an enumeration), but the value is not there, it throws a NullPointer exception .
So I want to ask if there is a way to achieve this, like the attribute default: passin the python Construct library

Here is my file ksy:

meta:
  id: btsnoop
  endian: be
seq:
  - id: header
    type: header
  - id: packets
    type: packet
    repeat: eos
types:
  header:
    seq:
      - id: iden
        size: 8
      - id: version
        type: u4
      - id: datalink_type
        type: u4
        enum: linktype
  packet:
    seq:
      - id: ori_len
        type: u4
      - id: include_len
        type: u4
      - id: pkt_flags
        type: u4
      - id: cumu_drop
        type: u4
      - id: timestamp
        type: s8
      - id: data
        size: include_len
        type: frame
  frame:
    seq:
      - id: pkt_type
        type: u1
        enum: pkttype
      - id: cmd
        type: cmd
        if: pkt_type == pkttype::cmd_pkt
      - id: acl
        type: acl
        if: pkt_type == pkttype::acl_pkt
      - id: evt
        type: evt
        if: pkt_type == pkttype::evt_pkt  
  cmd:
    seq:
      - id: opcode
        type: u2le
      - id: params_len
        type: u1
      - id: params
        size: params_len
  acl:
    seq:
      - id: handle
        type: u2le
  evt:
    seq:
      - id: status
        type: u1
        enum: status
      - id: total_length
        type: u1
      - id: params
        size-eos: true
enums:  <-- I need to list all possible option in every enum?
  linktype:
    0x03E9: unencapsulated_hci
    0x03EA: hci_uart
    0x03EB: hci_bscp
    0x03EC: hci_serial
  pkttype:
    1: cmd_pkt
    2: acl_pkt
    4: evt_pkt
  status:
    0x0D: complete_D
    0x0E: complete_E
    0xFF: vendor_specific

Thanks for the answer:)

+4
source share
1 answer

There are two more questions you face :)

Partial / Truncated / Corrupted Data Analysis

, Kaitai Struct .ksy , . , , , . , , . EOFException, , - .

, , , " " - .. . : " " , ( ), ( ).

Kaitai Struct - --debug. , , (void _read()). , . , :

Btssnoop b = Btssnoop.fromFile("/path/to/file.bin");
System.out.println(b.packets.size());

, --debug, :

Btssnoop b = Btssnoop.fromFile("/path/to/file.bin");
b._read();
System.out.println(b.packets.size());

try/catch IOException:

Btssnoop b = Btssnoop.fromFile("/path/to/file.bin");
try {
    b._read();
} catch (IOException e) {
    System.out.println("warning: truncated packets");
}
System.out.println(b.packets.size());

:

  • --debug Java, v0.3; , git , , .
  • --debug , , , /. , " /" --debug - , .
  • , , . , , , .

NPE

Java enums, -

this.pet1 = Animal.byId(_io.readU4le());

Animal.byId :

private static final Map<Long, Animal> byId = new HashMap<Long, Animal>(3);
static {
    for (Animal e : Animal.values())
        byId.put(e.id(), e);
}
public static Animal byId(long id) { return byId.get(id); }

Java null , . null - ( ) true false. , NPE, .. , ?

+3
source

Source: https://habr.com/ru/post/1648720/


All Articles