# Path: librouteros/exceptions.py
# class ProtocolError(LibRouterosError):
#     """Raised when e.g. encoding/decoding fails."""
# 
# class FatalError(ProtocolError):
#     """Exception raised when !fatal is received."""
# 
# Path: librouteros/connections.py
# class SocketTransport:
# 
#     def __init__(self, sock: socket):
#         self.sock = sock
# 
#     def write(self, data: bytes) -> None:
#         """
#         Write given bytes to socket. Loop as long as every byte in
#         string is written unless exception is raised.
#         """
#         self.sock.sendall(data)
# 
#     def read(self, length: int) -> bytes:
#         """
#         Read as many bytes from socket as specified in length.
#         Loop as long as every byte is read unless exception is raised.
#         """
#         data = bytearray()
#         while len(data) != length:
#             data += self.sock.recv((length - len(data)))
#             if not data:
#                 raise ConnectionClosed('Connection unexpectedly closed.')
#         return data
# 
#     def close(self) -> None:
#         self.sock.close()


import typing
from struct import pack, unpack #pylint: disable=no-name-in-module
from logging import getLogger, NullHandler
from librouteros.exceptions import (
    ProtocolError,
    FatalError,
)
from librouteros.connections import SocketTransport
        """
        Decode length based on given bytes.

        :param length: Bytes string to decode.
        :return: Decoded length.
        """
        bytes_length = len(length)

        if bytes_length < 2:
            offset = b'\x00\x00\x00'
            xor = 0
        elif bytes_length < 3:
            offset = b'\x00\x00'
            xor = 0x8000
        elif bytes_length < 4:
            offset = b'\x00'
            xor = 0xC00000
        elif bytes_length < 5:
            offset = b''
            xor = 0xE0000000
        else:
            raise ProtocolError(f'Unable to decode length of {length!r}')

        decoded: int = unpack('!I', (offset + length))[0]
        decoded ^= xor
        return decoded


class ApiProtocol(Encoder, Decoder):


# Path: librouteros/exceptions.py
# class ProtocolError(LibRouterosError):
#     """Raised when e.g. encoding/decoding fails."""
# 
# class FatalError(ProtocolError):
#     """Exception raised when !fatal is received."""
# 
# Path: librouteros/connections.py
# class SocketTransport:
# 
#     def __init__(self, sock: socket):
#         self.sock = sock
# 
#     def write(self, data: bytes) -> None:
#         """
#         Write given bytes to socket. Loop as long as every byte in
#         string is written unless exception is raised.
#         """
#         self.sock.sendall(data)
# 
#     def read(self, length: int) -> bytes:
#         """
#         Read as many bytes from socket as specified in length.
#         Loop as long as every byte is read unless exception is raised.
#         """
#         data = bytearray()
#         while len(data) != length:
#             data += self.sock.recv((length - len(data)))
#             if not data:
#                 raise ConnectionClosed('Connection unexpectedly closed.')
#         return data
# 
#     def close(self) -> None:
#         self.sock.close()


# Path: librouteros/protocol.py
import typing
from struct import pack, unpack #pylint: disable=no-name-in-module
from logging import getLogger, NullHandler
from librouteros.exceptions import (
    ProtocolError,
    FatalError,
)
from librouteros.connections import SocketTransport

        """
        Decode length based on given bytes.

        :param length: Bytes string to decode.
        :return: Decoded length.
        """
        bytes_length = len(length)

        if bytes_length < 2:
            offset = b'\x00\x00\x00'
            xor = 0
        elif bytes_length < 3:
            offset = b'\x00\x00'
            xor = 0x8000
        elif bytes_length < 4:
            offset = b'\x00'
            xor = 0xC00000
        elif bytes_length < 5:
            offset = b''
            xor = 0xE0000000
        else:
            raise ProtocolError(f'Unable to decode length of {length!r}')

        decoded: int = unpack('!I', (offset + length))[0]
        decoded ^= xor
        return decoded


class ApiProtocol(Encoder, Decoder):


    def __init__(self, transport: SocketTransport, encoding: str):