# Path: oppy/cell/exceptions.py
# class NotEnoughBytes(Exception):
#     pass
# 
# class UnknownCellCommand(Exception):
#     pass


import abc
import struct
import oppy.cell.definitions as DEF
from oppy.cell.exceptions import NotEnoughBytes, UnknownCellCommand
            from oppy.cell.varlen import VarLenCell
            from oppy.cell.relay import RelayCell
            from oppy.cell.fixedlen import FixedLenCell
        how much data is required. For variable-length cells, additionally
        check the length bytes.

        :param str data: The raw string to check.
        :param int link_version: Link Protocol version in use. In version
            3, 512 bytes are required for a fixed-length cell. In version
            4, 514 bytes are required.
        :returns: **bool** that's **True** iff data is long enough to
            build the type of cell indicated by the command byte.
        '''
        if link_version < 4:
            fmt = "!HB"
            header_len = DEF.PAYLOAD_START_V3
            required_length = DEF.FIXED_LEN_V3_LEN
        else:
            fmt = "!IB"
            header_len = DEF.PAYLOAD_START_V4
            required_length = DEF.FIXED_LEN_V4_LEN

        if len(data) < header_len:
            return False
        _, cmd = struct.unpack(fmt, data[:header_len])
        if cmd in DEF.FIXED_LEN_CMD_IDS:
            return len(data) >= required_length
        elif cmd in DEF.VAR_LEN_CMD_IDS:
            required_len = struct.unpack('!H',
                                         data[header_len:header_len + 2])[0]
            return len(data) >= required_len
        else:
            msg = "Unknown cell cmd: {}.".format(cmd)

# Path: oppy/cell/exceptions.py
# class NotEnoughBytes(Exception):
#     pass
# 
# class UnknownCellCommand(Exception):
#     pass


# Path: oppy/cell/cell.py
import abc
import struct
import oppy.cell.definitions as DEF
from oppy.cell.exceptions import NotEnoughBytes, UnknownCellCommand
            from oppy.cell.varlen import VarLenCell
            from oppy.cell.relay import RelayCell
            from oppy.cell.fixedlen import FixedLenCell

        how much data is required. For variable-length cells, additionally
        check the length bytes.

        :param str data: The raw string to check.
        :param int link_version: Link Protocol version in use. In version
            3, 512 bytes are required for a fixed-length cell. In version
            4, 514 bytes are required.
        :returns: **bool** that's **True** iff data is long enough to
            build the type of cell indicated by the command byte.
        '''
        if link_version < 4:
            fmt = "!HB"
            header_len = DEF.PAYLOAD_START_V3
            required_length = DEF.FIXED_LEN_V3_LEN
        else:
            fmt = "!IB"
            header_len = DEF.PAYLOAD_START_V4
            required_length = DEF.FIXED_LEN_V4_LEN

        if len(data) < header_len:
            return False
        _, cmd = struct.unpack(fmt, data[:header_len])
        if cmd in DEF.FIXED_LEN_CMD_IDS:
            return len(data) >= required_length
        elif cmd in DEF.VAR_LEN_CMD_IDS:
            required_len = struct.unpack('!H',
                                         data[header_len:header_len + 2])[0]
            return len(data) >= required_len
        else:
            msg = "Unknown cell cmd: {}.".format(cmd)

            raise UnknownCellCommand(msg)