def is_of_types(obj, the_types):
def is_int(obj):
def is_in(obj, it):
def not_none(obj): ENC_STR_DEFAULT = StrEncoder() class StrEncoder(object):
    '''
    Base encoder class for str values
    The String encoders *encode* function receives a *str* object as an argument and returns an encoded *Bits* object.

    +----------------------+------------------------------------+---------------------------+
    | Singleton Name       | Encoding                           | Class                     |
    +======================+====================================+===========================+
    | ENC_STR_UTF8         | Encode the str in UTF-8            | StrEncodeEncoder          |
    +----------------------+------------------------------------+                           |
    | ENC_STR_HEX          | Encode the str in hex              |                           |
    +----------------------+------------------------------------+                           |
    | ENC_STR_BASE64       | Encode the str in base64           |                           |
    +----------------------+------------------------------------+---------------------------+
    | ENC_STR_DEFAULT      | Do nothing, just convert the str   | StrEncoder                |
    |                      | to Bits object                     |                           |
    +----------------------+------------------------------------+---------------------------+
    '''

    def encode(self, value):
        '''
        :type value: ``str``
        :param value: value to encode
        '''
        return Bits(bytes=strToBytes(value)) ENC_INT_DEFAULT = ENC_INT_BIN class BitFieldEncoder(object):
    '''
    Base encoder class for BitField values

    +-------------------+---------------------------------------+-----------------------+
    | Singleton Name    | Encoding                              | Class                 |
    +===================+=======================================+=======================+
    | ENC_INT_BIN       | Encode as binary bits                 | BitFieldBinEncoder    |
    +-------------------+---------------------------------------+                       +
    | ENC_INT_LE        | Encode as a little endian binary bits |                       |
    +-------------------+---------------------------------------+                       |
    | ENC_INT_BE        | Encode as a big endian binary bits    |                       |
    +-------------------+---------------------------------------+-----------------------+
    | ENC_INT_DEC       | Encode as a decimal value             | BitFieldAsciiEncoder  |
    +-------------------+---------------------------------------+                       |
    | ENC_INT_HEX       | Encode as a hex value                 |                       |
    +-------------------+---------------------------------------+                       |
    | ENC_INT_HEX_UPPER | Encode as an upper case hex value     |                       |
    +-------------------+---------------------------------------+-----------------------+
    | ENC_INT_DEFAULT   | Same as ENC_INT_BIN                   | BitFieldBinEncoder    |
    +-------------------+---------------------------------------+-----------------------+
    '''

    def encode(self, value, length, signed):
        '''
        :type value: ``int``
        :param value: value to encode
        :type length: ``int``
        :param length: length of value in bits
        :type signed: ``boolean``
        :param signed: is value signed
        '''
        raise NotImplementedError('should be implemented in sub classes') ENC_BITS_DEFAULT = ENC_BITS_NONE class BitsEncoder(object):
    '''
    Base encoder class for Bits values

    The Bits encoders *encode* function receives a *Bits* object as an argument and returns an encoded *Bits* object.

    +-----------------------+----------------------------------------+------------------------+
    | Singleton Name        | Encoding                               | Class                  |
    +=======================+========================================+========================+
    | ENC_BITS_NONE         | None, returns the same value received  | BitsEncoder            |
    +-----------------------+----------------------------------------+------------------------+
    | ENC_BITS_BYTE_ALIGNED | Appends bits to the received object to | ByteAlignedBitsEncoder |
    |                       | make it byte aligned                   |                        |
    +-----------------------+----------------------------------------+------------------------+
    | ENC_BITS_REVERSE      | Reverse the order of bits              | ReverseBitsEncoder     |
    +-----------------------+----------------------------------------+------------------------+
    | ENC_BITS_BASE64       | Encode a Byte aligned bits in base64   | StrEncoderWrapper      |
    +-----------------------+----------------------------------------+                        |
    | ENC_BITS_UTF8         | Encode a Byte aligned bits in UTF-8    |                        |
    +-----------------------+----------------------------------------+                        |
    | ENC_BITS_HEX          | Encode a Byte aligned bits in hex      |                        |
    +-----------------------+----------------------------------------+------------------------+
    | ENC_BITS_DEFAULT      | Same as ENC_BITS_NONE                  |                        |
    +-----------------------+----------------------------------------+------------------------+
    '''

    def encode(self, value):
        '''
        :type value: Bits
        :param value: value to encode
        '''
        kassert.is_of_types(value, Bits)
        return value ENC_FLT_DEFAULT = ENC_FLT_BE class FloatEncoder(object):
    '''
    Base encoder class for FloatingPoint values

    +-------------------+---------------------------------------+-----------------------------------------------------------+
    | Singleton Name    | Encoding                              | Class                                                     |
    +===================+=======================================+===========================================================+
    | ENC_FLT_LE        | Encode as a little endian 32 bit      | :class:`~kitty.model.low_level.encoder.FloatBinEncoder`   |
    +-------------------+---------------------------------------+                                                           |
    | ENC_FLT_BE        | Encode as a big endian 32 bit         |                                                           |
    +-------------------+---------------------------------------+                                                           |
    | ENC_DBL_LE        | Encode as a little endian 64 bit      |                                                           |
    +-------------------+---------------------------------------+                                                           |
    | ENC_DBL_BE        | Encode as a big endian 64 bit         |                                                           |
    +-------------------+---------------------------------------+-----------------------------------------------------------+
    | ENC_FLT_FP        | Fixed point                           | :class:`~kitty.model.low_level.encoder.FloatAsciiEncoder` |
    +-------------------+---------------------------------------+                                                           |
    | ENC_FLT_EXP       | Exponent notation                     |                                                           |
    +-------------------+---------------------------------------+                                                           |
    | ENC_FLT_EXP_UPPER | Exponent notation, with upper case E  |                                                           |
    +-------------------+---------------------------------------+                                                           |
    | ENC_FLT_GEN       | General format                        |                                                           |
    +-------------------+---------------------------------------+                                                           |
    | ENC_FLT_GEN_UPPER | General format, with upper case       |                                                           |
    +-------------------+---------------------------------------+-----------------------------------------------------------+
    | ENC_FLT_DEFAULT   | Same as ENC_FLT_BE                    | :class:`~kitty.model.low_level.encoder.FloatBinEncoder`   |
    +-------------------+---------------------------------------+-----------------------------------------------------------+
    '''

    def encode(self, value):
        '''
        :type value: ``float``
        :param value: value to encode
        :rtype: ``Bits``
        :return: encoded value in bits
        '''
        raise NotImplementedError('should be implemented in sub classes') def strToBytes(value):
    '''
    :type value: ``str``
    :param value: value to encode
    '''
    kassert.is_of_types(value, (bytes, bytearray, six.string_types))
    if isinstance(value, six.string_types):
        return bytes(bytearray([ord(x) for x in value]))
    elif isinstance(value, bytearray):
        return bytes(value)
    return value
from random import Random
from base64 import b64encode
from bitstring import Bits
from kitty.core import KittyObject, KittyException, kassert, khash
from kitty.model.low_level.encoder import ENC_STR_DEFAULT, StrEncoder
from kitty.model.low_level.encoder import ENC_INT_DEFAULT, BitFieldEncoder
from kitty.model.low_level.encoder import ENC_BITS_DEFAULT, BitsEncoder
from kitty.model.low_level.encoder import ENC_FLT_DEFAULT, FloatEncoder
from kitty.model.low_level.encoder import strToBytes
import copy
import os
import logging
    def __init__(self, value, encoder=ENC_FLT_DEFAULT, fuzzable=True, name=None):
        '''
        :type value: float
        :param value: default value
        :type encoder: :class:`~kitty.model.low_level.encoder.FloatEncoder`
        :param encoder: encoder for the field (default: ENC_FLT_DEFAULT)
        :param fuzzable: is field fuzzable (default: True)
        :param name: name of the object (default: None)

        :example:

            ::

                Float(0.3)
        '''
        super(Float, self).__init__(value=value, encoder=encoder, fuzzable=fuzzable, name=name)

    def _get_local_lib(self):
        return []

    def _get_class_lib(self):
        lib = []
        lib.append((float('NaN'), 'positive NaN'))
        lib.append((float('-NaN'), 'negative NaN'))
        lib.append((float('inf'), 'positive infinity'))
        lib.append((float('-inf'), 'negative infinity'))
        lib.append((float(0.), 'positive zero'))
        lib.append((float(-0.), 'negative zero'))
        #
        # what else?
        #
        return lib


def _calc_bitfield_bounds(self, value, minv, maxv):
    if self._length <= 0:
        raise KittyException('length (%d) <= 0' % (self._length))
    max_possible = 2 ** self._length - 1
    if self._signed:
        self._min_value = ~(max_possible >> 1)
    else:
        self._min_value = 0
    self._max_value = max_possible + self._min_value
    self._max_min_diff = max_possible
    if maxv is not None:
        if maxv > self._max_value:
            raise KittyException('max_value is too big %d > %d' % (maxv, self._max_value))
        self._max_value = maxv
    if minv is not None:
        if minv < self._min_value:
            raise KittyException('min_value is too small %d < %d' % (minv, self._min_value))
        self._min_value = minv
    if self._min_value > self._max_value:
        raise KittyException('min_value (%d) > max_value (%d)' % (self._min_value, self._max_value))
    if (value < self._min_value) or (value > self._max_value):
        raise KittyException('default value (%d) not in range (min=%d, max=%d)' % (value, self._min_value, self._max_value))


class _FullRangeBitField(BaseField):
    '''
    Represents a fixed-length sequence of bits, the mutations are the entire range between min_value and max_value
    '''
    _encoder_type_ = BitFieldEncoder
    lib = None

    def __init__(self, value, length, signed=False, min_value=None, max_value=None, encoder=ENC_INT_DEFAULT, fuzzable=True, name=None):
        '''
        :type value: int
        :param value: default value
        :type length: positive int
        :param length: length of field in bits
        :param signed: are the values signed (default: False)
        :param min_value: minimal allowed value (default: None)
        :param max_value: maximal allowed value (default: None)
        :type encoder: :class:`~kitty.model.low_level.encoder.BitFieldEncoder`
        :param encoder: encoder for the field
        :param fuzzable: is field fuzzable (default: True)
        :param name: name of the object (default: None)
        '''
        self._length = length
        self._signed = signed
        self._min_value = None
        self._max_value = None
        self._max_min_diff = None
        self._calc_bounds(value, min_value, max_value)
        super(_FullRangeBitField, self).__init__(value=value, encoder=encoder, fuzzable=fuzzable, name=name)
        self._initialize()

    _calc_bounds = _calc_bitfield_bounds

    def _init(self):
        self._num_mutations = self._max_value - self._min_value + 1

    def _mutate(self):
        self._current_value = self._min_value + self._current_index

    def _encode_value(self, value):
        return self._encoder.encode(value, self._length, self._signed)

    def skip(self, count):
        self._initialize()
        skipped = 0
        if not self._exhausted():
            skipped = min(count, self.num_mutations() - self._current_index - 1)
            self._current_index += skipped
        return skipped

    def hash(self):
        '''
        :rtype: int
        :return: hash of the field
        '''
        hashed = super(_FullRangeBitField, self).hash()
        return khash(hashed, self._length, self._signed, self._min_value, self._max_value)


class _LibraryBitField(_LibraryField):
    '''
    Represents a fixed-length sequence of bits, the mutations target common integer related vulnerabilities
    '''

    _encoder_type_ = BitFieldEncoder