Example #1
0
    def parse(self, raw_bytes: bytes):
        """Parse synchrphasor message stream


        """
        stream = []
        self._raw_data = raw_bytes
        _io = KaitaiStream(BytesIO(self._raw_data))
        while not _io.is_eof():
            message = PhasorMessage(_io, _mini_cfgs=self._mini_cfgs)
            stream.append(message)
        return stream
Example #2
0
def get_gse_from_bbdata(bbdata):
    # Loop through the Bytes a bbframe payload and attempt to parse them as GSEpackets
    io = KaitaiStream(BytesIO(bbdata))
    gse_packets = []
    while not io.is_eof():
        try:
            current_gse = PureGse(io).gse_packet
            gse_packets.append(current_gse)
        except EOFError:
            counters['truncated_gse_packets'] += 1
        except ValueError:
            counters['truncated_gse_packets'] += 1
    return gse_packets
Example #3
0
def main_streaming():
    with open(DEMO_PATH, 'rb') as f:
        stream = KaitaiStream(f)
        header = Dem.Header(stream)
        print_demo_header(header)
        i = 0
        while not stream.is_eof():
            if i == 2:
                break
            frame = Frame(stream)

            i += 1

            print_frame(frame)
Example #4
0
 def parse_object(self, header_row):
     object_raw = self.read_object(header_row)
     stream = KaitaiStream(BytesIO(object_raw))
     num_check = 0
     while not stream.is_eof():
         obj = Model3Clb.LogObject(stream)
         if not obj.number == num_check:
             print(
                 f'Encountered blocknumber: {obj.number}, expected: {num_check}, stopping'
             )
             break
         num_check += 1
         if not self.crc_check(obj.number, obj.crc, obj.raw_body):
             raise ValueError('Invalid CRC')
         for varint in obj.body.varints:
             yield varint.value
Example #5
0
    def parse(self, raw_bytes):
        """Parse synchrphasor message stream


        """
        stream = []
        _io = KaitaiStream(BytesIO(raw_bytes))
        while not _io.is_eof():
            try:
                message = PhasorMessage(_io, _mini_cfgs=self._mini_cfgs)
                if type(message.data) != type(b''):
                    stream.append(message)
            except Exception as e:
                LOG.debug("Parsing error.")
                print(len(stream))
                raise
        return stream
Example #6
0
 def parse(self, stream: KaitaiStream, context: AST = None) -> AST:
     if self.if_expr is not None:
         if not self.if_expr.interpret(context):
             return None
     ast = AST(self, parent=context)
     if self.repeat == Repeat.EOS:
         while not stream.is_eof():
             ast.add_child(self.type.parse(stream, ast))
     elif self.repeat == Repeat.EXPR:
         iterations = int.from_bytes(self.repeat_expr.interpret(context),
                                     byteorder='big')
         for i in range(iterations):
             ast.add_child(self.type.parse(stream, ast))
     elif self.repeat == Repeat.UNTIL:
         while not self.repeat_until.interpret(context):
             ast.add_child(self.type.parse(stream, ast))
     else:
         ast.add_child(self.type.parse(stream, ast))
     return ast
def read_entries(data, start_offset=0):
    # Inspired by: https://stackoverflow.com/questions/49699820/parsing-binary-messages-with-kaitai-struct-python
    stream = KaitaiStream(BytesIO(data))
    stream.seek(start_offset)
    last = stream.pos()
    start = ModelSxLog(stream)
    log_entry = start.log_entry
    yield log_entry
    n_entries = 1
    with tqdm(total=stream.size() - start_offset,
              unit='B',
              unit_scale=True,
              desc='Processing log') as pbar:
        while not stream.is_eof():
            if n_entries % 1000 == 0:
                consumed = stream.pos() - last
                pbar.update(consumed)
                last = stream.pos()
            try:
                log_entry = ModelSxLog.Entry(stream, _root=start._root)
                if sum(log_entry.raw_bytes) % 256 != 0:
                    print(
                        f'Checksum error at {stream.pos()}, seeking to the next entry...'
                    )
                    stream.read_bytes_term(0xaa,
                                           include_term=False,
                                           consume_term=False,
                                           eos_error=True)
                else:
                    yield log_entry
            except ValidationNotEqualError:
                print(
                    f'Encountered an error at {stream.pos()}, probably a corrupt entry, seeking to next one...'
                )
                stream.read_bytes_term(0xaa,
                                       include_term=False,
                                       consume_term=False,
                                       eos_error=True)
                pass
            n_entries += 1
        pbar.update(stream.pos() - last)
        stream.close()
Example #8
0
def read_entries(data, max_num=1000):
    # Inspired by: https://stackoverflow.com/questions/49699820/parsing-binary-messages-with-kaitai-struct-python
    stream = KaitaiStream(BytesIO(data))
    # print(f'{stream.pos():08x} ', end='')
    start = ModelSxLog(stream)  # Initialize the parser on the root stream
    log_entry = start.log_entry
    yield log_entry
    num_entries = 1
    while not stream.is_eof():
        try:
            # print(f'{stream.pos():08x} ', end='')
            log_entry = ModelSxLog.Record(stream, _root=start._root)
            if sum(log_entry.raw_bytes) % 256 != 0:
                print(
                    f'Checksum error at {stream.pos()}, seeking to the next entry...'
                )
                stream.read_bytes_term(0xaa,
                                       include_term=False,
                                       consume_term=False,
                                       eos_error=True)
            else:
                yield log_entry
        except:
            # Unfortunately kaitaistruct does not specify the exception, assuming it's a wrong delimiter
            print(
                f'Encountered an error at {stream.pos()}, probably a corrupt entry, seeking to next one...'
            )
            stream.read_bytes_term(0xaa,
                                   include_term=False,
                                   consume_term=False,
                                   eos_error=True)
            pass
        num_entries += 1
        if num_entries > max_num:
            break
    stream.close()