Ejemplo n.º 1
0
 def setQuestion(self, qname, qtype=QTYPE_A, qclass=QCLASS_IN):
     for label in qname.split('.'):
         label = str(label)
         l = len(label)
         self.buf += struct_pack('b', l)
         self.buf += struct_pack(str(l)+'s', label)
     self.buf += '\0'
     self.buf += self._pack16bit(qtype) 
     self.buf += self._pack16bit(qclass)
Ejemplo n.º 2
0
	def tick(self,sample,sample2=None):
		if (sample2 == None):
			s1 = int(self.sampleScale*sample)
			s1 = clip_value(s1,-self.sampleScale,self.sampleScale)
			self.wavFile.writeframes(struct_pack(self.packFormat,s1))
			return sample
		else:
			s1 = int(self.sampleScale*sample)
			s1 = clip_value(s1,-self.sampleScale,self.sampleScale)
			s2 = int(self.sampleScale*sample2)
			s2 = clip_value(s2,-self.sampleScale,self.sampleScale)
			self.wavFile.writeframes(struct_pack(self.packFormat,s1))
			self.wavFile.writeframes(struct_pack(self.packFormat,s2))
			return (sample,sample2)
Ejemplo n.º 3
0
def encode_time(value):
    if value.microsecond > 0:
        val = (
            value.microsecond |
            value.second << 20 |
            value.minute << 26 |
            value.hour << 32
        )
        return DYN_COL_TIME, struct_pack('Q', val)[:6]
    else:
        val = (
            value.second |
            value.minute << 6 |
            value.hour << 12
        )
        return DYN_COL_TIME, struct_pack('I', val)[:3]
Ejemplo n.º 4
0
def bool_to_bitarray(value):
    """
    Converts a numpy boolean array to a bit array (a string of bits in
    a bytes object).

    Parameters
    ----------
    value : numpy bool array

    Returns
    -------
    bit_array : bytes
        The first value in the input array will be the most
        significant bit in the result.  The length will be `floor((N +
        7) / 8)` where `N` is the length of `value`.
    """
    value = value.flat
    bit_no = 7
    byte = 0
    bytes = []
    for v in value:
        if v:
            byte |= 1 << bit_no
        if bit_no == 0:
            bytes.append(byte)
            bit_no = 7
            byte = 0
        else:
            bit_no -= 1
    if bit_no != 7:
        bytes.append(byte)

    return struct_pack("%sB" % len(bytes), *bytes)
Ejemplo n.º 5
0
 def pack_test(self, string, buffsize):
     """ packs wireless request data for sending it to the kernel """
     buffsize = buffsize - len(string)
     buff = array('c', string+'\0'*buffsize)
     caddr_t, length = buff.buffer_info()
     s = struct_pack('Pii', caddr_t, length, 1)
     return buff, s
Ejemplo n.º 6
0
def encode_float(value):
    if isnan(value) or isinf(value):
        raise DynColValueError("Float value not encodeable: {}".format(value))
    encvalue = struct_pack('d', value)

    # -0.0 is not supported in SQL, change to 0.0
    if encvalue == b'\x00\x00\x00\x00\x00\x00\x00\x80':
        encvalue = b'\x00\x00\x00\x00\x00\x00\x00\x00'

    return DYN_COL_DOUBLE, encvalue
Ejemplo n.º 7
0
 def pack_wrq(self, buffsize):
     """ packs wireless request data for sending it to the kernel """
     # Prepare a buffer
     # We need the address of our buffer and the size for it. The
     # ioctl itself looks for the pointer to the address in our
     # memory and the size of it.
     # Dont change the order how the structure is packed!!!
     buff = array('c', '\0'*buffsize)
     caddr_t, length = buff.buffer_info()
     s = struct_pack('Pi', caddr_t, length)
     return buff, s
Ejemplo n.º 8
0
def snappy_emit_backref(f, offset, length):
  if 4 <= length <= 11 and offset < 2048:
    f.write(chr(1 | ((length - 4) << 2) | ((offset >> 8) << 5)))
    f.write(chr(offset & 255))
  else: # a back offset with offset > 65536 is not supported by this encoder!
    encoded_offset = struct_pack("<H", offset)
    while length > 0:
      curr_len_chunk = min(length, 64)
      f.write(chr(2 | ((curr_len_chunk - 1) << 2)))
      f.write(encoded_offset)
      length -= curr_len_chunk
Ejemplo n.º 9
0
 def pack_bytes_header(self, size):
     stream = self.stream
     if size < PLUS_2_TO_THE_8:
         stream.write(BYTES_8)
         stream.write(PACKED_UINT_8[size])
     elif size < PLUS_2_TO_THE_16:
         stream.write(BYTES_16)
         stream.write(PACKED_UINT_16[size])
     elif size < PLUS_2_TO_THE_32:
         stream.write(BYTES_32)
         stream.write(struct_pack(UINT_32_STRUCT, size))
     else:
         raise OverflowError("Bytes header size out of range")
Ejemplo n.º 10
0
 def a_qr_str(self, grupo_cat=None, con_dnis=True):
     """Devuelve la informacion del recuento para almacenar en qr."""
     encoded_data = self.a_tag(grupo_cat, con_dnis)
     datos = bytearray(b"")
     # Agregamos el Token
     datos.append(int(TOKEN, 16))
     # agregamos el largo de los datos en 2 bytes
     len_data = len(encoded_data) * 2
     datos.extend(struct_pack(">H", len_data))
     # metemos el resto de los datos
     datos.extend(encoded_data)
     # lo encodeamos en hexa y lo pasamos a mayúsculas
     todo = encode(datos, "hex_codec").decode().upper()
     return todo
Ejemplo n.º 11
0
 def pack_string_header(self, size):
     stream = self.stream
     if size < PLUS_2_TO_THE_4:
         stream.write(TINY_STRING[size])
     elif size < PLUS_2_TO_THE_8:
         stream.write(STRING_8)
         stream.write(PACKED_UINT_8[size])
     elif size < PLUS_2_TO_THE_16:
         stream.write(STRING_16)
         stream.write(PACKED_UINT_16[size])
     elif size < PLUS_2_TO_THE_32:
         stream.write(STRING_32)
         stream.write(struct_pack(UINT_32_STRUCT, size))
     else:
         raise OverflowError("String header size out of range")
Ejemplo n.º 12
0
 def pack_map_header(self, size):
     stream = self.stream
     if size < PLUS_2_TO_THE_4:
         stream.write(TINY_MAP[size])
     elif size < PLUS_2_TO_THE_8:
         stream.write(MAP_8)
         stream.write(PACKED_UINT_8[size])
     elif size < PLUS_2_TO_THE_16:
         stream.write(MAP_16)
         stream.write(PACKED_UINT_16[size])
     elif size < PLUS_2_TO_THE_32:
         stream.write(MAP_32)
         stream.write(struct_pack(UINT_32_STRUCT, size))
     else:
         raise OverflowError("Map header size out of range")
Ejemplo n.º 13
0
 def flush(self, end_of_message=False):
     """ Flush everything written since the last chunk to the
     stream, followed by a zero-chunk if required.
     """
     output_buffer = self.output_buffer
     if output_buffer:
         lines = [struct_pack(">H", self.output_size)] + output_buffer
     else:
         lines = []
     if end_of_message:
         lines.append(b"\x00\x00")
     if lines:
         self.raw.writelines(lines)
         self.raw.flush()
         del output_buffer[:]
         self.output_size = 0
Ejemplo n.º 14
0
def encode_int(value):
    if value < 0:
        dtype = DYN_COL_INT
        encvalue = -(value << 1) - 1
        if value < -(2 ** 32 - 1):
            raise DynColValueError("int {} out of range".format(value))
    else:
        if value <= (2 ** 63 - 1):
            dtype = DYN_COL_INT
            encvalue = value << 1
        elif value <= (2 ** 64 - 1):
            dtype = DYN_COL_UINT
            encvalue = value
        else:
            raise DynColValueError("int {} out of range".format(value))

    to_enc = []
    while encvalue:
        to_enc.append(encvalue & 0xff)
        encvalue = encvalue >> 8
    return dtype, struct_pack('B' * len(to_enc), *to_enc)
Ejemplo n.º 15
0
    def binoutput(self, value, mask):
        if np.any(mask):
            vo_warn(W39)

        value = value.flat
        bit_no = 7
        byte = 0
        bytes = []
        for v in value:
            if v:
                byte |= 1 << bit_no
            if bit_no == 0:
                bytes.append(byte)
                bit_no = 7
                byte = 0
            else:
                bit_no -= 1
        if bit_no != 7:
            bytes.append(byte)

        assert len(bytes) == self._bytes

        return struct_pack("%sB" % len(bytes), *bytes)
Ejemplo n.º 16
0
def rgb2hex(rgb):
    return '#' + binascii.hexlify(struct_pack('BBB', *rgb)).decode('ascii')
Ejemplo n.º 17
0
def int_to_ip_address(address):
    return inet_ntoa(struct_pack("!I", address))
Ejemplo n.º 18
0
Archivo: pcapng.py Proyecto: smutt/dpkt
def _swap32b(i):
    """swap endianness of an uint32"""
    return struct_unpack('<I', struct_pack('>I', i))[0]
Ejemplo n.º 19
0
Archivo: pcapng.py Proyecto: smutt/dpkt
def _padded(s):
    """return str `s` padded with zeroes to align to the 32-bit boundary"""
    return struct_pack('%ss' % _align32b(len(s)), s)
Ejemplo n.º 20
0
def pack32bit(n):
    return struct_pack('!L', n)
Ejemplo n.º 21
0
 def _pack16bit(self,n):
     return struct_pack('!H', n)
Ejemplo n.º 22
0
def int_to_ip_address(address):
    return socket.inet_ntoa(struct_pack("!I", address))
Ejemplo n.º 23
0
 def __bytes__(self) -> bytes:
     return b''.join([
         bytes(self.service_config),
         struct_pack('<I', self.bytes_needed),
         struct_pack('<I', self.return_code.value)
     ])
Ejemplo n.º 24
0
def connect(host_port, ssl_context=None, **config):
    """ Connect and perform a handshake and return a valid Connection object, assuming
    a protocol version can be agreed.
    """

    # Establish a connection to the host and port specified
    # Catches refused connections see:
    # https://docs.python.org/2/library/errno.html
    if __debug__: log_info("~~ [CONNECT] %s", host_port)
    try:
        s = create_connection(host_port)
    except SocketError as error:
        if error.errno == 111 or error.errno == 61:
            raise ProtocolError("Unable to connect to %s on port %d - is the server running?" % host_port)
        else:
            raise

    # Secure the connection if an SSL context has been provided
    if ssl_context and SSL_AVAILABLE:
        host, port = host_port
        if __debug__: log_info("~~ [SECURE] %s", host)
        try:
            s = ssl_context.wrap_socket(s, server_hostname=host if HAS_SNI else None)
        except SSLError as cause:
            error = ProtocolError("Cannot establish secure connection; %s" % cause.args[1])
            error.__cause__ = cause
            raise error
        else:
            # Check that the server provides a certificate
            der_encoded_server_certificate = s.getpeercert(binary_form=True)
            if der_encoded_server_certificate is None:
                raise ProtocolError("When using a secure socket, the server should always provide a certificate")
            trust = config.get("trust", TRUST_DEFAULT)
            if trust == TRUST_ON_FIRST_USE:
                store = PersonalCertificateStore()
                if not store.match_or_trust(host, der_encoded_server_certificate):
                    raise ProtocolError("Server certificate does not match known certificate for %r; check "
                                        "details in file %r" % (host, KNOWN_HOSTS))
    else:
        der_encoded_server_certificate = None

    # Send details of the protocol versions supported
    supported_versions = [1, 0, 0, 0]
    handshake = [MAGIC_PREAMBLE] + supported_versions
    if __debug__: log_info("C: [HANDSHAKE] 0x%X %r", MAGIC_PREAMBLE, supported_versions)
    data = b"".join(struct_pack(">I", num) for num in handshake)
    if __debug__: log_debug("C: %s", ":".join(map(hex2, data)))
    s.sendall(data)

    # Handle the handshake response
    ready_to_read, _, _ = select((s,), (), (), 0)
    while not ready_to_read:
        ready_to_read, _, _ = select((s,), (), (), 0)
    data = s.recv(4)
    data_size = len(data)
    if data_size == 0:
        # If no data is returned after a successful select
        # response, the server has closed the connection
        log_error("S: [CLOSE]")
        raise ProtocolError("Server closed connection without responding to handshake")
    if data_size == 4:
        if __debug__: log_debug("S: %s", ":".join(map(hex2, data)))
    else:
        # Some other garbled data has been received
        log_error("S: @*#!")
        raise ProtocolError("Expected four byte handshake response, received %r instead" % data)
    agreed_version, = struct_unpack(">I", data)
    if __debug__: log_info("S: [HANDSHAKE] %d", agreed_version)
    if agreed_version == 0:
        if __debug__: log_info("~~ [CLOSE]")
        s.shutdown(SHUT_RDWR)
        s.close()
    elif agreed_version == 1:
        return Connection(s, der_encoded_server_certificate=der_encoded_server_certificate, **config)
    elif agreed_version == 1213486160:
        log_error("S: [CLOSE]")
        raise ProtocolError("Server responded HTTP. Make sure you are not trying to connect to the http endpoint " +
                            "(HTTP defaults to port 7474 whereas BOLT defaults to port 7687)")
    else:
        log_error("S: [CLOSE]")
        raise ProtocolError("Unknown Bolt protocol version: %d", agreed_version)
Ejemplo n.º 25
0
 def _write_length(length):
     return struct_pack(">I", int(length))
Ejemplo n.º 26
0
def bin2addr(n):
    return inet_ntoa(struct_pack('!L', n))
Ejemplo n.º 27
0
 def _binoutput_fixed(self, value, mask):
     if mask:
         value = u''
     return struct_pack(self._struct_format, value.encode('utf_16_be'))
Ejemplo n.º 28
0
 def __bytes__(self) -> bytes:
     return self.service_handle + struct_pack('<I', self.buf_size)
Ejemplo n.º 29
0
 def _binoutput_fixed(self, value, mask):
     if mask:
         value = _empty_bytes
     return struct_pack(self._struct_format, value)
Ejemplo n.º 30
0
 def _binoutput_fixed(self, value, mask):
     if mask:
         value = _empty_bytes
     return struct_pack(self._struct_format, value)
Ejemplo n.º 31
0
 def pack(self, fmt, *args):
     """ calls struct_pack and returns the result """
     return struct_pack(fmt, *args)
Ejemplo n.º 32
0
def encode_date(value):
    # We don't need any validation since datetime.date is more limited than the
    # MySQL format
    val = value.day | value.month << 5 | value.year << 9
    return DYN_COL_DATE, struct_pack('I', val)[:-1]
Ejemplo n.º 33
0
 def _write_length(length):
     return struct_pack(">I", int(length))
Ejemplo n.º 34
0
def pack32bit(n):
    return struct_pack('!L', n)
Ejemplo n.º 35
0
 def _binoutput_fixed(self, value, mask):
     if mask:
         value = u''
     return struct_pack(self._struct_format, value.encode('utf_16_be'))
Ejemplo n.º 36
0
 def make_length_prefix(count: int) -> bytes:
     return struct_pack('<H', count)
Ejemplo n.º 37
0
def pack16bit(n):
    return struct_pack('!H', n)
Ejemplo n.º 38
0
def pack(dicty):
    """
    Convert a mapping into the MariaDB dynamic columns format
    """
    column_count = 0
    column_directory = []
    directory_offset = 0
    name_offset = 0
    names = []
    data_offset = 0
    data = []
    total_encname_length = 0

    dicty_names_encoded = {
        key.encode('utf-8'): value
        for key, value in six_iteritems(dicty)
    }

    for encname in sorted(six_iterkeys(dicty_names_encoded), key=name_order):
        value = dicty_names_encoded[encname]
        if value is None:
            continue

        if len(encname) > MAX_NAME_LENGTH:
            raise DynColLimitError("Key too long: " + encname.decode('utf-8'))
        total_encname_length += len(encname)
        if total_encname_length > MAX_TOTAL_NAME_LENGTH:
            raise DynColLimitError("Total length of keys too long")

        try:
            encode_func = ENCODE_FUNCS[type(value)]
        except KeyError:
            raise DynColTypeError("Unencodable type {}".format(type(value)))
        dtype, encvalue = encode_func(value)

        column_count += 1
        column_directory.append(name_offset)
        column_directory.append((data_offset << 4) + dtype)
        names.append(encname)
        name_offset += len(encname)
        data.append(encvalue)
        data_offset += len(encvalue)

        directory_offset += 2

    data_size_flag, coldir_size_code, odd_sized_datacode = data_size(data)

    flags = (
        4 |  # means this contains named dynamic columns
        data_size_flag
    )
    enc_names = b''.join(names)

    buf = [
        struct_pack(
            '<BHH',
            flags,
            column_count,
            len(enc_names)
        ),
    ]
    if not odd_sized_datacode:
        buf.append(
            struct_pack(
                '<' + ('H' + coldir_size_code) * (len(column_directory) // 2),
                *column_directory
            )
        )
    else:
        for i, val in enumerate(column_directory):
            if i % 2 == 0:
                # name_offset
                buf.append(struct_pack('<H', val))
            else:
                # data_offset + dtype, have to cut last byte
                val = struct_pack('<' + coldir_size_code, val)
                buf.append(val[:-1])
    buf.append(enc_names)
    buf.extend(data)
    return b''.join(buf)
Ejemplo n.º 39
0
def bin2addr(n):
    return inet_ntoa(struct_pack('!L', n))
Ejemplo n.º 40
0
def pack16bit(n):
    return struct_pack('!H', n)