Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-fatal compiler error when using custom type after a type that ends on a non-byte boundary #485

Closed
Tracked by #1070
elliottecton opened this issue Oct 31, 2018 · 5 comments

Comments

@elliottecton
Copy link

Kaitai Document

meta:
  id: tcp_segment
  title: TCP segment
  license: CC0-1.0
seq:
  - id: src_port
    type: u2be
  - id: dst_port
    type: u2be
  - id: seq_num
    type: u4be
  - id: ack_num
    type: u4be
  - id: hdr_length
    type: b4             ### Four bits followed by
  - id: flags             ###
    type: tcp_flags  ### Custom type
  - id: window_size
    type: u2be
  - id: checksum
    type: u2be
  - id: urgent_pointer
    type: u2be
  - id: options
    size: (hdr_length * 4) - 20
    if: (hdr_length * 4) - 20 != 0
types:
  tcp_flags:
    seq:
      - id: reserved
        type: b3
      - id: nonce
        type: b1
      - id: congestion_win_reduced
        type: b1
      - id: ecn_echo
        type: b1
      - id: urgent 
        type: b1
      - id: ack 
        type: b1
      - id: push
        type: b1
      - id: reset
        type: b1
      - id: syn
        type: b1
      - id: fin
        type: b1

Compiled Python

# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

from pkg_resources import parse_version
from kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

class TcpSegment(KaitaiStruct):
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._read()

    def _read(self):
        self.src_port = self._io.read_u2be()
        self.dst_port = self._io.read_u2be()
        self.seq_num = self._io.read_u4be()
        self.ack_num = self._io.read_u4be()
        self.hdr_length = self._io.read_bits_int(4)
        self._io.align_to_byte()                      ### This misaligns the structure
        self.flags = self._root.TcpFlags(self._io, self, self._root)
        self.window_size = self._io.read_u2be()
        self.checksum = self._io.read_u2be()
        self.urgent_pointer = self._io.read_u2be()
        if (self.hdr_length - 20) != 0:
            self.options = self._io.read_bytes((self.hdr_length - 20))


    class TcpFlags(KaitaiStruct):
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._read()

        def _read(self):
            self.reserved = self._io.read_bits_int(3)
            self.nonce = self._io.read_bits_int(1) != 0
            self.congestion_win_reduced = self._io.read_bits_int(1) != 0
            self.ecn_echo = self._io.read_bits_int(1) != 0
            self.urgent = self._io.read_bits_int(1) != 0
            self.ack = self._io.read_bits_int(1) != 0
            self.push = self._io.read_bits_int(1) != 0
            self.reset = self._io.read_bits_int(1) != 0
            self.syn = self._io.read_bits_int(1) != 0
            self.fin = self._io.read_bits_int(1) != 0
@KOLANICH
Copy link

KOLANICH commented Nov 1, 2018

Thank you for bringing it up.

kaitai-io/kaitai_struct_formats#68 and kaitai-io/kaitai_struct_formats#70 are related, I just forgot to add an issue about the problems encountered there.

@GreyCat
Copy link
Member

GreyCat commented Nov 1, 2018

I believe the problem you're getting is that user types are by default always treated as byte-aligned.

This is by design, and there is relatively broad #12 issue about that. If there's no objections, I would mark this as duplicate and redirect you to that discussion?

@elliottecton
Copy link
Author

I am fine with dup'ing to 12. I do think there should be some sort of switch that overrides the automatic byte-alignment.

@GreyCat
Copy link
Member

GreyCat commented Nov 1, 2018

Thanks! Unfortunately, there's no good solution now, I'll try to prioritize #12 and come with something decent at least to enable bit/byte-alignment switching soon.

@generalmimon
Copy link
Member

Duplicate of #1070

@generalmimon generalmimon marked this as a duplicate of #1070 Oct 4, 2023
@generalmimon generalmimon closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants