Exemple #1
0
    def test_array_infinite_nested_block():
        random.seed(0)

        class leaf(pint.uint32_t): pass
        class rootcontainer(parray.block):
            _object_ = leaf

        class acontainer(rootcontainer):
            blocksize = lambda x: 8

        class bcontainer(rootcontainer):
            _object_ = pint.uint16_t
            blocksize = lambda x: 8

        class ccontainer(rootcontainer):
            _object_ = pint.uint8_t
            blocksize = lambda x: 8

        class arr(parray.infinite):
            def randomcontainer(self):
                l = [ acontainer, bcontainer, ccontainer ]
                return random.sample(l, 1)[0]

            _object_ = randomcontainer

        string = str().join([ six.int2byte(random.randint(six.byte2int('A'),six.byte2int('Z'))) for x in six.moves.range(0x100) ])
        a = arr(source=provider.string(string))
        a=a.l
        if a.blocksize() == 0x108:
            raise Success
Exemple #2
0
 def decode(self, encoded_packet):
     """Decode a transmitted package."""
     b64 = False
     self.packet_type = six.byte2int(encoded_packet[0:1])
     if self.packet_type == 98:  # 'b' --> binary base64 encoded packet
         self.binary = True
         encoded_packet = encoded_packet[1:]
         self.packet_type = six.byte2int(encoded_packet[0:1])
         self.packet_type -= 48
         b64 = True
     elif self.packet_type >= 48:
         self.packet_type -= 48
         self.binary = False
     else:
         self.binary = True
     self.data = None
     if len(encoded_packet) > 1:
         if self.binary:
             if b64:
                 self.data = base64.b64decode(encoded_packet[1:])
             else:
                 self.data = encoded_packet[1:]
         else:
             try:
                 self.data = self.json.loads(
                     encoded_packet[1:].decode('utf-8'))
             except ValueError:
                 self.data = encoded_packet[1:].decode('utf-8')
    def decode(self, encoded_payload):
        """Decode a transmitted payload."""
        self.packets = []
        while encoded_payload:
            if six.byte2int(encoded_payload[0:1]) <= 1:
                packet_len = 0
                i = 1
                while six.byte2int(encoded_payload[i:i + 1]) != 255:
                    packet_len = packet_len * 10 + six.byte2int(
                        encoded_payload[i:i + 1])
                    i += 1
                self.packets.append(packet.Packet(
                    encoded_packet=encoded_payload[i + 1:i + 1 + packet_len]))
            else:
                i = encoded_payload.find(b':')
                if i == -1:
                    raise ValueError('invalid payload')

                # extracting the packet out of the payload is extremely
                # inefficient, because the payload needs to be treated as
                # binary, but the non-binary packets have to be parsed as
                # unicode. Luckily this complication only applies to long
                # polling, as the websocket transport sends packets
                # individually wrapped.
                packet_len = int(encoded_payload[0:i])
                pkt = encoded_payload.decode('utf-8', errors='ignore')[
                    i + 1: i + 1 + packet_len].encode('utf-8')
                self.packets.append(packet.Packet(encoded_packet=pkt))

                # the engine.io protocol sends the packet length in
                # utf-8 characters, but we need it in bytes to be able to
                # jump to the next packet in the payload
                packet_len = len(pkt)
            encoded_payload = encoded_payload[i + 1 + packet_len:]
Exemple #4
0
    def validate(self, skip_utf8_validation=False):
        """
        validate the ABNF frame.
        skip_utf8_validation: skip utf8 validation.
        """
        if self.rsv1 or self.rsv2 or self.rsv3:
            raise WebSocketProtocolException("rsv is not implemented, yet")

        if self.opcode not in ABNF.OPCODES:
            raise WebSocketProtocolException("Invalid opcode %r", self.opcode)

        if self.opcode == ABNF.OPCODE_PING and not self.fin:
            raise WebSocketProtocolException("Invalid ping frame.")

        if self.opcode == ABNF.OPCODE_CLOSE:
            l = len(self.data)
            if not l:
                return
            if l == 1 or l >= 126:
                raise WebSocketProtocolException("Invalid close frame.")
            if l > 2 and not skip_utf8_validation and not validate_utf8(self.data[2:]):
                raise WebSocketProtocolException("Invalid close frame.")

            code = 256 * six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2])
            if not self._is_valid_close_status(code):
                raise WebSocketProtocolException("Invalid close opcode.")
Exemple #5
0
 def _wait_close(self):
     logger.debug('hxsocks _wait_close')
     self.settimeout(8)
     while 1:
         try:
             ctlen = self._rfile_read(2)
             if not ctlen:
                 raise IOError(0, '')
             ctlen = struct.unpack('>H', self.pskcipher.decrypt(ctlen))[0]
             ct = self._rfile_read(ctlen)
             mac = self._rfile_read(MAC_LEN)
             data = self.cipher.decrypt(ct, mac)
             pad_len = byte2int(data)
             if 0 < pad_len < 8:
                 # fake chunk, drop
                 if pad_len == 1:
                     self.send_fake_chunk(2)
                 # server should be sending another chunk right away
                 continue
             data = data[1:0-pad_len] if byte2int(data) else data[1:]
             if not data:
                 logger.debug('hxsocks add to pool')
                 self.pooled = 1
                 POOL.put(self.hxsServer.parse.hostname, self, self.hxsServer.name)
                 self.readable = 0
                 break
         except Exception:
             self._sock.close()
             return
Exemple #6
0
def decode_version(version):
    """ Takes a byte version and decodes it into human-readable <major>.<minor> format. """
    if len(version) != 1:
        raise ValueError("Can only decode a single byte!")
    major = six.byte2int(version) >> 4
    minor = six.byte2int(version) & 15
    return ('%d.%d' % (major, minor)).encode('ascii')
Exemple #7
0
 def __getvalue__(self):
     if not self.initializedQ():
         raise error.InitializationError(self, 'int')
     if self.byteorder is config.byteorder.bigendian:
         return six.moves.reduce(lambda x,y: x << 8 | six.byte2int(y), self.serialize(), 0)
     elif self.byteorder is config.byteorder.littleendian:
         return six.moves.reduce(lambda x,y: x << 8 | six.byte2int(y), reversed(self.serialize()), 0)
     raise error.SyntaxError(self, 'integer_t.int', message='Unknown integer endianness {!r}'.format(self.byteorder))
Exemple #8
0
    def _decode_carddata(data, card_type, reftime = None):
        """Decodes a data record read from an SI Card."""

        ret = {}
        card = SIReader.CARD[card_type]
        
        # the slicing of data is necessary for Python 3 to get a bytes object instead
        # of an int
        ret['card_number'] = SIReader._decode_cardnr(b'\x00'
                                                     + data[card['CN2']:card['CN2']+1]
                                                     + data[card['CN1']:card['CN1']+1]
                                                     + data[card['CN0']:card['CN0']+1])

        ret['start'] = SIReader._decode_time(data[card['ST']:card['ST']+2],
                                             data[card['STD']] if card['STD'] else None,
                                             reftime)
        ret['finish'] = SIReader._decode_time(data[card['FT']:card['FT']+2],
                                              data[card['FTD']] if card['FTD'] is not None else None,
                                             reftime)
        ret['check'] = SIReader._decode_time(data[card['CT']:card['CT']+2],
                                             data[card['CTD']] if card['CTD'] is not None else None,
                                             reftime)
        if card['LT'] is not None:
            ret['clear'] = SIReader._decode_time(data[card['LT']:card['LT']+2],
                                                 data[card['LTD']] if card['LTD'] is not None else None,
                                                 reftime)
        else:
            ret['clear'] = None # SI 5 and 9 cards don't store the clear time

        punch_count = byte2int(data[card['RC']:card['RC']+1])
        if card_type == 'SI5':
            # RC is the index of the next punch on SI5
            punch_count -= 1
            
        if punch_count > card['PM']:
            punch_count = card['PM']
            
        ret['punches'] = []
        p = 0
        i = card['P1']
        while p < punch_count:
            if card_type == 'SI5' and i % 16 == 0:
                # first byte of each block is reserved for punches 31-36
                i += 1

            ptd = data[i + card['PTD']] if card['PTD'] is not None else None
            cn  = byte2int(data[i + card['CN']])
            pt  = data[i + card['PTH']:i + card['PTL']+1]

            SIReader._append_punch(ret['punches'], cn, pt, ptd, reftime)

            i += card['PL']
            p += 1
            
        return ret
Exemple #9
0
 def _get_close_args(self,data):
     """ this functions extracts the code, reason from the close body
     if they exists, and if the self.on_close except three arguments """
     import inspect
     # if the on_close callback is "old", just return empty list
     if not self.on_close or len(inspect.getargspec(self.on_close).args) != 3:
         return []
     if data and len(data) >=2:
         code = 256*six.byte2int(data[0]) + six.byte2int(data[1])
         reason = data[2:].decode('utf-8')
         return [code,reason]
     return [None,None]
Exemple #10
0
    def _read_command(self, timeout = None):

        try:
            if timeout != None:
                old_timeout = self._serial.timeout
                self._serial.timeout = timeout
            char = self._serial.read()
            if timeout != None:
                self._serial.timeout = old_timeout

            if char == b'':
                raise SIReaderTimeout('No data available')
            elif char == SIReader.NAK:
                raise SIReaderException('Invalid command or parameter.')
            elif char != SIReader.STX:
                self._serial.flushInput()
                raise SIReaderException('Invalid start byte %s' % hex(byte2int(char)))

            # Read command, length, data, crc, ETX
            cmd = self._serial.read()
            length = self._serial.read()
            station = self._serial.read(2)
            self.station_code = SIReader._to_int(station)
            data = self._serial.read(byte2int(length)-2)
            crc = self._serial.read(2)
            etx = self._serial.read()

            if self._debug:
                print("<<== command '%s', len %i, station %s, data %s, crc %s, etx %s" % (hexlify(cmd).decode('ascii'),
                                                                                          byte2int(length),
                                                                                          hexlify(station).decode('ascii'),
                                                                                          ' '.join([hexlify(int2byte(c)).decode('ascii') for c in data]),
                                                                                          hexlify(crc).decode('ascii'),
                                                                                          hexlify(etx).decode('ascii'),
                                                                                          ))

            if etx != SIReader.ETX:
                raise SIReaderException('No ETX byte received.')
            if not SIReader._crc_check(cmd + length + station + data, crc):
                raise SIReaderException('CRC check failed')

            if self._logfile:
                self._logfile.write('r %s %s\n' % (datetime.now(), char + cmd + length + station + data + crc + etx))
                self._logfile.flush()
                os.fsync(self._logfile)
                
        except (SerialException, OSError) as msg:
            raise SIReaderException('Error reading command: %s' % msg)

        return (cmd, data)
    def derive(self, type, site, counter=1):
        value = ""
        seed = self.seed(site, counter)
        try:
            templates = Templates[type].value
        except KeyError as e:
            log.error("Unknown key type '{}'".format(type))
            raise e
        template = templates[six.byte2int(seed[0]) % len(templates)]
        for i in range(0, len(template)):
            passChars = CHARACTER_CLASSES[template[i]]
            passChar = passChars[six.byte2int(seed[i + 1]) % len(passChars)]
            value += passChar

        return value
Exemple #12
0
def read_loose(stream):
    """Turn a HL7-like blob of text into a real HL7 messages"""
    # look for the START_BLOCK to delineate messages
    START_BLOCK = b'MSH|^~\&|'

    # load all the data
    data = stream.read()

    # take out all the typical MLLP separators. In Python 3, iterating
    # through a bytestring returns ints, so we need to filter out the int
    # versions of the separators, then convert back from a list of ints to
    # a bytestring (In Py3, we could just call bytes([ints]))
    separators = [six.byte2int(bs) for bs in [EB, FF, SB]]
    data = b''.join([six.int2byte(c) for c in six.iterbytes(data) if c not in separators])

    # Windows & Unix new lines to segment separators
    data = data.replace(b'\r\n', b'\r').replace(b'\n', b'\r')

    for m in data.split(START_BLOCK):
        if not m:
            # the first element will not have any data from the split
            continue

        # strip any trailing whitespace
        m = m.strip(CR + b'\n ')

        # re-insert the START_BLOCK, which was removed via the split
        yield START_BLOCK + m
def decode_caveat(key, caveat):
    '''Decode caveat by decrypting the encrypted part using key.

    @param key the nacl private key to decode.
    @param caveat bytes.
    @return ThirdPartyCaveatInfo
    '''
    if len(caveat) == 0:
        raise VerificationError('empty third party caveat')

    first = caveat[:1]
    if first == b'e':
        # 'e' will be the first byte if the caveatid is a base64
        # encoded JSON object.
        return _decode_caveat_v1(key, caveat)
    first_as_int = six.byte2int(first)
    if (first_as_int == VERSION_2 or
            first_as_int == VERSION_3):
        if (len(caveat) < _VERSION3_CAVEAT_MIN_LEN
                and first_as_int == VERSION_3):
            # If it has the version 3 caveat tag and it's too short, it's
            # almost certainly an id, not an encrypted payload.
            raise VerificationError(
                'caveat id payload not provided for caveat id {}'.format(
                    caveat))
        return _decode_caveat_v2_v3(first_as_int, key, caveat)
    raise VerificationError('unknown version for caveat')
Exemple #14
0
    def decode(self, input, errors='strict'):
        """
        Decode byte array to string
        :param input: byte array to convert to unicode string
        :param errors: defines the error handling to apply
        :return: returns a tuple (output object, length consumed)
        """
        decode_buffer = u""
        consumed = 0

        num = 0
        for value in input:
            consumed += 1
            num |= byte2int([value])
            if num == self._ESCAPE:
                num <<= 8
                continue
            try:
                decode_buffer += unichr(self._decode_map[num])
            except KeyError as ex:
                if errors == 'replace':
                    decode_buffer += u'\ufffd'
                elif errors == 'ignore':
                    pass
                else:
                    if num & (self._ESCAPE << 8):
                        raise ValueError("'%s' codec can't decode byte 0x%x in position %d" %
                                         (self.NAME, ex.args[0] & 0xff, consumed - 1))
                    else:
                        raise ValueError("'%s' codec can't decode byte 0x%x in position %d" %
                                         (self.NAME, ex.args[0], consumed - 1))
            num = 0
        return decode_buffer, consumed
Exemple #15
0
 def extract_message(cls, raw_bytes):
     if len(raw_bytes) < 2:
         return None, raw_bytes
     if six.byte2int(raw_bytes) != 123:
         raise FramingError(
             'Broken state. Expected JSON Object, got: %s' % raw_bytes)
     stack = [123]
     uniesc = 0
     poppers = {91: [93], 123: [125], 34: [34]}
     adders = {91: [34, 91, 123], 123: [34, 91, 123], 34: [92], 92: [117]}
     for idx in range(1, len(raw_bytes)):
         cbyte = six.indexbytes(raw_bytes, idx)
         if cbyte in poppers.get(stack[-1], []):
             stack.pop()
         elif cbyte in adders.get(stack[-1], []):
             stack.append(cbyte)
         elif stack[-1] == 92:
             stack.pop()
         elif stack[-1] == 117:
             uniesc += 1
             if uniesc >= 4:
                 stack = stack[:-2]
                 uniesc = 0
         if not stack:
             return raw_bytes[:idx + 1], raw_bytes[idx + 1:]
     return None, raw_bytes
Exemple #16
0
    def poll_punch(self, timeout=0):
        """Polls for new punches.
        @return: list of (cardnr, punchtime) tuples, empty list if no new punches are available
        """

        if not self.proto_config['ext_proto']:
            raise SIReaderException('This command only supports stations in "Extended Protocol" '
                                    'mode. Switch mode first')

        if not self.proto_config['auto_send']:
            raise SIReaderException('This command only supports stations in "Autosend" '
                                    'mode. Switch mode first')

        punches = []
        while True:
            try:
                c = self._read_command(timeout = timeout)
            except SIReaderTimeout:
                break 
        
            if c[0] == SIReader.C_TRANS_REC:
                cur_offset = SIReader._to_int(c[1][SIReader.T_OFFSET:SIReader.T_OFFSET+3])
                if self._next_offset is not None:
                    while self._next_offset < cur_offset:
                        # recover lost punches
                        punches.append(self._read_punch(self._next_offset))
                        self._next_offset += SIReader.REC_LEN

                self._next_offset = cur_offset + SIReader.REC_LEN
            punches.append( (self._decode_cardnr(c[1][SIReader.T_CN:SIReader.T_CN+4]), 
                             self._decode_time(c[1][SIReader.T_TIME:SIReader.T_TIME+2])) )
        else:
            raise SIReaderException('Unexpected command %s received' % hex(byte2int(c[0])))
        
        return punches
Exemple #17
0
    def _update_proto_config(self):
        # Read protocol configuration
        ret = self._send_command(SIReader.C_GET_SYS_VAL, SIReader.O_PROTO+b'\x01')
        config_byte = byte2int(ret[1][1])
        self.proto_config = {}
        self.proto_config['ext_proto']  = config_byte & (1 << 0) != 0
        self.proto_config['auto_send']  = config_byte & (1 << 1) != 0
        self.proto_config['handshake']  = config_byte & (1 << 2) != 0
        self.proto_config['pw_access']  = config_byte & (1 << 4) != 0
        self.proto_config['punch_read'] = config_byte & (1 << 7) != 0

        # Read operating mode
        ret = self._send_command(SIReader.C_GET_SYS_VAL, SIReader.O_MODE+b'\x01')
        self.proto_config['mode'] = byte2int(ret[1][1])

        return self.proto_config
Exemple #18
0
def decode_name(name):
    # ToDo: Rewrite this simpler, we're using less than written
    """
    Perform first and second level decoding of name as specified in RFC 1001 (Section 4)

    :param string name: the name to dencode

    :return string: the decoded name.
    """

    name_length = ord(name[0])
    assert name_length == 32

    decoded_name = re.sub('..', _do_first_level_decoding, name[1:33])
    if name[33] == '\0':
        return 34, decoded_name, ''
    else:
        decoded_domain = ''
        offset = 34
        while 1:
            domain_length = byte2int(name[offset:offset+1])
            if domain_length == 0:
                break
            decoded_domain = '.' + name[offset:offset + domain_length]
            offset += domain_length
        return offset + 1, decoded_name, decoded_domain
Exemple #19
0
def _bytes_text(code_point_iter, quote, prefix=b'', suffix=b''):
    quote_code_point = six.byte2int(quote)
    buf = BytesIO()
    buf.write(prefix)
    buf.write(quote)
    for code_point in code_point_iter:
        if code_point == quote_code_point:
            buf.write(b'\\' + quote)
        elif code_point == six.byte2int(b'\\'):
            buf.write(b'\\\\')
        elif _is_printable_ascii(code_point):
            buf.write(six.int2byte(code_point))
        else:
            buf.write(_escape(code_point))
    buf.write(quote)
    buf.write(suffix)
    return buf.getvalue()
Exemple #20
0
def escape(data):
    result = b''
    for c in iter_single_bytes(data):
        if c in b'#$}*':
            result += b'}' + six.int2byte(six.byte2int(c) ^ 0x20)
        else:
            result += c
    return result
Exemple #21
0
def unpackbyte(b):
   """
   Given a one-byte long byte string, returns an integer. Equivalent
   to struct.unpack("B", b)
   """
   if isinstance(b, bytes):
       return six.byte2int(b)
   return b
Exemple #22
0
    def read_frame(self):
        """Read frame from connection

        Note that the frame may be destined for any channel. It is permitted to interleave frames
        from different channels.

        :return: frame
        :rtype: amqpy.proto.Frame
        """
        frame = Frame()
        try:
            # read frame header: 7 bytes
            frame_header = self.read(7, True)
            frame.data.extend(frame_header)

            # read frame payload
            payload = self.read(frame.payload_size)
            frame.data.extend(payload)

            # read frame terminator byte
            frame_terminator = self.read(1)
            frame.data.extend(frame_terminator)

            if six.PY2:
                #: :type: int
                i_last_byte = six.byte2int(frame_terminator)
            else:
                # this fixes the change in memoryview in Python 3.3 (accessing an element returns the
                #  correct type)
                #: :type: int
                i_last_byte = six.byte2int(bytes(frame_terminator))
        except (OSError, IOError, socket.error) as exc:
            # don't disconnect for ssl read time outs (Python 3.2):
            # http://bugs.python.org/issue10272
            if isinstance(exc, SSLError) and 'timed out' in str(exc):
                raise socket.timeout()
            if get_errno(exc) not in _UNAVAIL and not isinstance(exc, socket.timeout):
                self.connected = False
            raise

        if i_last_byte == FrameType.END:
            if frame.frame_type == FrameType.HEARTBEAT:
                self.last_heartbeat_received = datetime.datetime.now()
            return frame
        else:
            raise UnexpectedFrame('Received {} while expecting 0xce (FrameType.END)'.format(hex(i_last_byte)))
Exemple #23
0
 def decode(self, text):
     '''
     Remove the PKCS#7 padding from a text string
     '''
     val = six.byte2int([text[-1]])
     if val > self.k:
         raise ValueError('Input is not padded or padding is corrupt')
     return text[:-val]
Exemple #24
0
    def parse_binary_value(self, data, display, length, format):
        from . import event

        estruct = display.event_classes.get(byte2int(data) & 0x7f, event.AnyEvent)
        if type(estruct) == dict:
            # this etype refers to a set of sub-events with individual subcodes
            estruct = estruct[indexbytes(data, 1)]

        return estruct(display = display, binarydata = data[:32]), data[32:]
 def test_sends_valid_rekey_message(self):
     self.session.send_rekey()
     rekey_msg = self.channel.sent_messages.pop(0).msg
     self.assert_message_type(rekey_msg, 0x03)
     self.assertEqual(len(rekey_msg), 42)
     # Sequence number should be 0
     self.assertEqual(six.byte2int(rekey_msg[1:]), 0x00)
     expected_mac = self.get_mac(rekey_msg[:-8])
     self.assertEqual(rekey_msg[-8:], expected_mac)
Exemple #26
0
 def getMouseEvent():
   global oldbutt
   global pin
   buf = file.read(3)
   pin = pin & 0x07
   button = six.byte2int(buf) & pin # mask out just the required button(s)
   if button != oldbutt:  # only send if changed
       oldbutt = button
       six.print_(button)
Exemple #27
0
    def from_file(cls, fp):
        """
          read a websockets frame header
        """
        first_byte = six.byte2int(fp.safe_read(1))
        second_byte = six.byte2int(fp.safe_read(1))

        fin = utils.getbit(first_byte, 7)
        rsv1 = utils.getbit(first_byte, 6)
        rsv2 = utils.getbit(first_byte, 5)
        rsv3 = utils.getbit(first_byte, 4)
        # grab right-most 4 bits
        opcode = first_byte & 15
        mask_bit = utils.getbit(second_byte, 7)
        # grab the next 7 bits
        length_code = second_byte & 127

        # payload_lengthy > 125 indicates you need to read more bytes
        # to get the actual payload length
        if length_code <= 125:
            payload_length = length_code
        elif length_code == 126:
            payload_length, = struct.unpack("!H", fp.safe_read(2))
        elif length_code == 127:
            payload_length, = struct.unpack("!Q", fp.safe_read(8))

        # masking key only present if mask bit set
        if mask_bit == 1:
            masking_key = fp.safe_read(4)
        else:
            masking_key = None

        return cls(
            fin=fin,
            rsv1=rsv1,
            rsv2=rsv2,
            rsv3=rsv3,
            opcode=opcode,
            mask=mask_bit,
            length_code=length_code,
            payload_length=payload_length,
            masking_key=masking_key,
        )
Exemple #28
0
def main():
    """ Test application for the CGA card. """
    import sys
    
    print("CGA test application.")
    char_generator = CharacterGeneratorMDA_CGA_ROM(sys.argv[1], CharacterGeneratorMDA_CGA_ROM.CGA_WIDE_FONT)
    
    cga = ColorGraphicsAdapter(char_generator, double = False)
    cga.reset()
    
    pygame.key.set_repeat(250, 25)
    overscan_color = 0x00
    cursor = CGA_OFFSET
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_KP_PLUS:
                    overscan_color = (overscan_color + 1) & 0x0F
                    print("overscan_color = 0x%x" % overscan_color)
                    cga.io_write_byte(PALETTE_REG_PORT, overscan_color)
                    cga.draw()
                    
                elif event.key == pygame.K_KP_MINUS:
                    old_value = cga.io_read_byte(CONTROL_REG_PORT)
                    if old_value & CONTROL_REG_HIGH_RES:
                        cga.io_write_byte(0x3D4, 1)
                        cga.io_write_byte(0x3D5, 40)
                        cga.io_write_byte(CONTROL_REG_PORT, old_value & ~CONTROL_REG_HIGH_RES)
                    else:
                        cga.io_write_byte(0x3D4, 1)
                        cga.io_write_byte(0x3D5, 80)
                        cga.io_write_byte(CONTROL_REG_PORT, old_value | CONTROL_REG_HIGH_RES)
                        
                    cga.draw()
                    
                elif event.key == pygame.K_KP_MULTIPLY:
                    old_value = cga.io_read_byte(CONTROL_REG_PORT)
                    if old_value & CONTROL_REG_GRAPHICS_MODE:
                        cga.io_write_byte(CONTROL_REG_PORT, old_value & ~CONTROL_REG_GRAPHICS_MODE)
                    else:
                        cga.io_write_byte(CONTROL_REG_PORT, old_value | CONTROL_REG_GRAPHICS_MODE)
                        
                    cga.mem_write_byte(CGA_OFFSET + 0, 0xA5)
                    cga.mem_write_byte(CGA_OFFSET + 8192, 0x5A)
                    cga.draw()
                    
                elif len(event.unicode) > 0:
                    byte = six.byte2int(event.unicode.encode("utf-8"))
                    cga.mem_write_byte(cursor, byte)
                    cga.mem_write_byte(cursor + 1, random.randint(1, 0x0F))
                    cursor += 2
                    cga.draw()
                    
            elif event.type == pygame.QUIT:
                sys.exit()
Exemple #29
0
 def recv(self, size):
     logger.debug('hxsocks recv')
     # if not self.readable:
     #     return b''
     buf = self._rbuffer
     buf.seek(0, 2)  # seek end
     buf_len = buf.tell()
     self._rbuffer = io.BytesIO()  # reset _rbuf.  we consume it via buf.
     if buf_len == 0:
         logger.debug('Nothing in buffer. Try to read.')
         while 1:
             ctlen = self._rfile_read(2)
             if not ctlen:
                 return b''
             ctlen = struct.unpack('>H', self.pskcipher.decrypt(ctlen))[0]
             ct = self._rfile_read(ctlen)
             mac = self._rfile_read(MAC_LEN)
             data = self.cipher.decrypt(ct, mac)
             pad_len = byte2int(data)
             if 0 < pad_len < 8:
                 logger.debug('Fake chunk, drop')
                 if pad_len == 1:
                     logger.debug('sending fake chunk')
                     self.send_fake_chunk(2)
                 # server should be sending another chunk right away
                 continue
             data = data[1:0-pad_len] if byte2int(data) else data[1:]
             if not data:
                 logger.debug('hxsocks recv closed gracefully')
                 self.readable = 0
                 return b''
             if len(data) <= size:
                 return data
             buf_len = len(data)
             buf.write(data)
             del data  # explicit free
             break
     buf.seek(0)
     rv = buf.read(size)
     if buf_len > size:
         self._rbuffer.write(buf.read())
     return rv
Exemple #30
0
    def parse_response(self, request):
        """Internal method.

        Parse data recieved from server.  If REQUEST is not None
        true is returned if the request with that serial number
        was recieved, otherwise false is returned.

        If REQUEST is -1, we're parsing the server connection setup
        response.
        """

        if request == -1:
            return self.parse_connection_setup()

        # Parse ordinary server response
        gotreq = 0
        while 1:
            if self.data_recv:
                # Check the first byte to find out what kind of response it is
                rtype = byte2int(self.data_recv)

            # Are we're waiting for additional data for the current packet?
            if self.recv_packet_len:
                if len(self.data_recv) < self.recv_packet_len:
                    return gotreq

                if rtype == 1:
                    gotreq = self.parse_request_response(request) or gotreq
                    continue
                elif rtype & 0x7f == ge.GenericEventCode:
                    self.parse_event_response(rtype)
                    continue
                else:
                    raise AssertionError(rtype)

            # Every response is at least 32 bytes long, so don't bother
            # until we have recieved that much
            if len(self.data_recv) < 32:
                return gotreq

            # Error resposne
            if rtype == 0:
                gotreq = self.parse_error_response(request) or gotreq

            # Request response or generic event.
            elif rtype == 1 or rtype & 0x7f == ge.GenericEventCode:
                # Set reply length, and loop around to see if
                # we have got the full response
                rlen = int(struct.unpack('=L', self.data_recv[4:8])[0])
                self.recv_packet_len = 32 + rlen * 4

            # Else non-generic event
            else:
                self.parse_event_response(rtype)
Exemple #31
0
def unpack_timestamp_resolution(data):
    """
    Unpack a timestamp resolution.

    Returns a floating point number representing the timestamp
    resolution (multiplier).
    """
    if len(data) != 1:
        raise ValueError('Data must be exactly one byte')
    num = byte2int(data)
    base = 2 if (num >> 7 & 1) else 10
    exponent = num & 0b01111111
    return base**(-exponent)
Exemple #32
0
    def decode(self, encoded_payload):
        """Decode a transmitted payload."""
        self.packets = []
        while encoded_payload:
            # JSONP POST payload starts with 'd='
            if encoded_payload.startswith(b'd='):
                encoded_payload = urllib.parse.parse_qs(
                    encoded_payload)[b'd'][0]

            if six.byte2int(encoded_payload[0:1]) <= 1:
                packet_len = 0
                i = 1
                while six.byte2int(encoded_payload[i:i + 1]) != 255:
                    packet_len = packet_len * 10 + six.byte2int(
                        encoded_payload[i:i + 1])
                    i += 1
                self.packets.append(packet.Packet(
                    encoded_packet=encoded_payload[i + 1:i + 1 + packet_len]))
            else:
                i = encoded_payload.find(b':')
                if i == -1:
                    raise ValueError('invalid payload')

                # extracting the packet out of the payload is extremely
                # inefficient, because the payload needs to be treated as
                # binary, but the non-binary packets have to be parsed as
                # unicode. Luckily this complication only applies to long
                # polling, as the websocket transport sends packets
                # individually wrapped.
                packet_len = int(encoded_payload[0:i])
                pkt = encoded_payload.decode('utf-8', errors='ignore')[
                    i + 1: i + 1 + packet_len].encode('utf-8')
                self.packets.append(packet.Packet(encoded_packet=pkt))

                # the engine.io protocol sends the packet length in
                # utf-8 characters, but we need it in bytes to be able to
                # jump to the next packet in the payload
                packet_len = len(pkt)
            encoded_payload = encoded_payload[i + 1 + packet_len:]
Exemple #33
0
 def decode(self, encoded_payload):
     """Decode a transmitted payload."""
     self.packets = []
     while encoded_payload:
         if six.byte2int(encoded_payload[0:1]) <= 1:
             packet_len = 0
             i = 1
             while six.byte2int(encoded_payload[i:i + 1]) != 255:
                 packet_len = packet_len * 10 + six.byte2int(
                     encoded_payload[i:i + 1])
                 i += 1
             self.packets.append(
                 packet.Packet(encoded_packet=encoded_payload[i + 1:i + 1 +
                                                              packet_len]))
         else:
             i = encoded_payload.find(b':')
             if i == -1:
                 raise ValueError('invalid payload')
             packet_len = int(encoded_payload[0:i])
             pkt = encoded_payload[i + 1:i + 1 + packet_len]
             self.packets.append(packet.Packet(encoded_packet=pkt))
         encoded_payload = encoded_payload[i + 1 + packet_len:]
Exemple #34
0
def escape(data):
    """! @brief Escape binary data to be sent to Gdb.
    
    @param data Bytes-like object containing raw binary.
    @return Bytes object with the characters in '#$}*' escaped as required by Gdb.
    """
    result = b''
    for c in iter_single_bytes(data):
        if c in b'#$}*':
            result += b'}' + six.int2byte(six.byte2int(c) ^ 0x20)
        else:
            result += c
    return result
Exemple #35
0
    def pack_data(cmd, cmd_to_struct,
                  *args):  # type: (int, Dict, Union[*float, *int]) -> bytes
        if not cmd_to_struct or len(args) == 0:
            # If no arguments are given, then the message does not contain a data package
            return b''

        for key in cmd_to_struct.keys():
            if cmd == key if isinstance(key, int) else cmd in key:
                value = cmd_to_struct[key]
                if isinstance(value, tuple):
                    # The struct is given as the fist argument
                    struct_t = value[0]  # type: struct.Struct

                    # The conversion from SI-units to raw values are given in the rest of the tuple
                    fmt = struct_t.format
                    if isinstance(fmt, six.string_types):  # pragma: no cover
                        # Needed for Python 3.7
                        fmt = six.b(fmt)

                    # Make sure the endian is given as the first argument
                    assert six.byte2int(fmt) == ord('<') or six.byte2int(
                        fmt) == ord('>')

                    # Disable rounding if the format is a float
                    data = []
                    for c, arg, val in zip(six.iterbytes(fmt[1:]), args,
                                           value[1:]):
                        if c == ord('f'):
                            data.append(arg * val)
                        else:
                            data.append(round(arg * val))
                else:
                    # No conversion from SI-units is needed
                    struct_t = value  # type: struct.Struct
                    data = args

                return struct_t.pack(*data)
        else:
            raise ValueError('Unknown command: 0x{:02X}'.format(cmd))
Exemple #36
0
    def decode(self, encoded_payload):
        """Decode a transmitted payload."""
        self.packets = []

        if len(encoded_payload) == 0:
            return

        # JSONP POST payload starts with 'd='
        if encoded_payload.startswith(b'd='):
            encoded_payload = urllib.parse.parse_qs(
                encoded_payload)[b'd'][0]

        i = 0
        if six.byte2int(encoded_payload[0:1]) <= 1:
            # binary encoding
            while i < len(encoded_payload):
                if len(self.packets) >= self.max_decode_packets:
                    raise ValueError('Too many packets in payload')
                packet_len = 0
                i += 1
                while six.byte2int(encoded_payload[i:i + 1]) != 255:
                    packet_len = packet_len * 10 + six.byte2int(
                        encoded_payload[i:i + 1])
                    i += 1
                self.packets.append(packet.Packet(
                    encoded_packet=encoded_payload[i + 1:i + 1 + packet_len]))
                i += packet_len + 1
        else:
            # assume text encoding
            encoded_payload = encoded_payload.decode('utf-8')
            while i < len(encoded_payload):
                if len(self.packets) >= self.max_decode_packets:
                    raise ValueError('Too many packets in payload')
                j = encoded_payload.find(':', i)
                packet_len = int(encoded_payload[i:j])
                pkt = encoded_payload[j + 1:j + 1 + packet_len]
                self.packets.append(packet.Packet(encoded_packet=pkt))
                i = j + 1 + packet_len
Exemple #37
0
    def read_info_by_serial(cls, port, password):
        params = b''
        params += int2byte(password[0])
        params += int2byte(password[1])
        params += int2byte(password[2])

        resp_code, data = cls._send_command(port,
                                            cls.SERIAL_FUNC_READ_INFO,
                                            params,
                                            timeout=8)
        if resp_code == cls.SERIAL_RESP_INFO:
            state = cls.State()
            state.version = Sportiduino.Version(*data[0:3])
            state.config = cls.Config.unpack(data[3:9])

            state.battery = cls.Battery(byte2int(data[9]))
            state.mode = byte2int(data[10])

            state.timestamp = datetime.fromtimestamp(
                Sportiduino._to_int(data[11:15]))
            state.wakeuptime = datetime.fromtimestamp(
                Sportiduino._to_int(data[15:19]))
            return state
Exemple #38
0
    def connect(self, address):
        self._address = address
        self.getKey()
        if self._sock is None:
            from connection import create_connection
            host, port = self.hxsServer.hostname, self.hxsServer.port
            self._sock = create_connection((host, port),
                                           self.timeout,
                                           parentproxy=self.parentproxy,
                                           tunnel=True)
            self.pskcipher = encrypt.Encryptor(self.PSK, self.method)
            self._rfile = self._sock.makefile('rb')
            self._header_sent = False
            self._header_received = False
        logger.debug('hxsocks send connect request')
        padding_len = random.randint(64, 255)
        pt = b''.join([
            struct.pack('>I', int(time.time())),
            chr(len(self._address[0])).encode('latin1'),
            self._address[0].encode(),
            struct.pack('>H', self._address[1]), b'\x00' * padding_len
        ])
        ct, mac = self.cipher.encrypt(pt)
        self._sock_sendall(
            self.pskcipher.encrypt(b''.join([
                chr(11).encode(), keys[self.serverid][0],
                struct.pack('>H', len(ct))
            ])) + ct + mac)

        resp_len = 2 if self.pskcipher.decipher else self.pskcipher.iv_len + 2
        data = self._rfile_read(resp_len)
        if not data:
            raise IOError(0, 'hxsocks Error: connection closed.')
        resp_len = self.pskcipher.decrypt(data)
        resp_len = struct.unpack('>H', resp_len)[0]

        resp = self.pskcipher.decrypt(self._rfile_read(resp_len))

        d = byte2int(resp) if resp else None
        if d == 0:
            logger.debug('hxsocks connected')
            self.readable = 1
            self.writeable = 1
            return
        elif d == 2:
            raise IOError(0, 'hxsocks Error: remote connect timed out. code 2')
        else:
            if self.serverid in keys:
                del keys[self.serverid]
            raise IOError(0, 'hxsocks Error: invalid shared key. code %d' % d)
Exemple #39
0
def send_strategy_parameter_change_request_msg(socket, strategy_name, control_flag):
    msg = AllProtoMsg_pb2.StrategyParameterChangeRequestMsg()
    msg.Name = strategy_name
    msg.IsEnable = control_flag
    task_logger.info("Send StrategyParameterChangeRequestMsg")
    msg_str = msg.SerializeToString()
    msg_type = 16
    msg_list = [six.int2byte(msg_type), msg_str]
    socket.send_multipart(msg_list)
    recv_message = socket.recv_multipart()
    task_logger.info("Recv Msg.")
    recv_result = six.byte2int(recv_message[0])
    print recv_result
    return recv_result
Exemple #40
0
def get_market_info():
    context = zmq.Context().instance()
    # Socket to talk to server
    print "Connecting to hello world server"
    socket = context.socket(zmq.DEALER)

    # identity_str = addresess.First(p=>p.Contains(".")) + "-" + DateTime.Now.ToString() + "."
    identity_str = b'172.16.11.68'
    socket.setsockopt(zmq.IDENTITY, identity_str)
    # socket.bind_to_random_port('tcp://172.16.11.68')
    # socket.bind('tcp://1172.16.11.68-2017/4/10 16:05:46.')

    # socket.connect("tcp://172.16.12.118:17000")
    # socket.connect("tcp://172.16.10.126:10000")
    socket.connect("tcp://172.16.11.106:10000")

    msg_list = [six.int2byte(100), bytearray('login')]
    socket.send_multipart(msg_list, copy=False, track=True)

    message = socket.recv_multipart()
    print 'login recv:', message

    # SendZMQMsg(100, System.Text.Encoding.ASCII.GetBytes("login"))

    # Do 10 requests, waiting each time for a response
    msg = AllProtoMsg_pb2.InstrumentInfoRequestMsg()
    msg.IsAll = True
    msg.IncludeStaticInfo = True
    # msg.LastUpdate = time.time()

    msg_str = msg.SerializeToString()
    msg_list = [six.int2byte(2), msg_str]
    socket.send_multipart(msg_list)
    # Get the reply.
    message = socket.recv_multipart()
    print 'market recv:', message
    print 'type:', six.byte2int(message[0])

    recv_message = AllProtoMsg_pb2.InstrumentInfoResponseMsg()
    recv_message.ParseFromString(zlib.decompress(message[1]))

    instrument_dict = dict()
    for instrument_msg in recv_message.Targets:
        instrument_dict[instrument_msg.id] = instrument_msg

    for market_msg in recv_message.Infos:
        instrument_info = instrument_dict[market_msg.ID]
        market_args = market_msg.Args
        print instrument_info.ticker, market_args.LastPrice, __GetDateTime(
            market_args.UpdateTime)
Exemple #41
0
    def decode_payload(self, bytes):
        """Decode a received payload."""
        packets = []
        while bytes:
            if six.byte2int(bytes[0:1]) <= 1:
                packet_len = 0
                i = 1
                while six.byte2int(bytes[i:i + 1]) != 255:
                    packet_len = packet_len * 10 + six.byte2int(bytes[i:i + 1])
                    i += 1
                packet_start = i+1
            else:
                i = bytes.find(b':')
                if i == -1:
                    raise ValueError('Invalid payload')
                packet_len = int(bytes[0:i])
                packet_start = i+1

            packet = self.decode_packet(bytes[packet_start:packet_start+packet_len])
            packets.append(packet)
            bytes = bytes[packet_start+packet_len:]

        return packets
Exemple #42
0
 def validate(cls, data):
     """
     Given a bytes object, checks if the given class *cls* supports decoding
     this object. If not, raises a ValueError.
     """
     # TODO: Making this function return a boolean instead of raising an exception would make the code potentially more readable.
     tinfo = TypeInfo.from_bytes(data[0])
     if tinfo.cls != cls.TYPECLASS or tinfo.tag != cls.TAG:
         raise ValueError(
             'Invalid type header! '
             'Expected a %s class with tag '
             'ID 0x%02x, but got a %s class with '
             'tag ID 0x%02x' %
             (cls.TYPECLASS, cls.TAG, tinfo.cls, six.byte2int(data)))
Exemple #43
0
 def _get_response_generator(self):
     """A generator of reponses from the server. Yields the data decoded.
     """
     while True:
         with self.client.readlock:
             opcode, data = self.client.recv_data()
         if opcode == websocket.ABNF.OPCODE_TEXT:
             if six.PY3:
                 data = data.decode('utf-8')
             data_dict = json.loads(data)
             if data_dict['type'] == 'connected':
                 self.on_connected(data_dict['id'])
             else:
                 yield data
         elif opcode == websocket.ABNF.OPCODE_CLOSE:
             if data and len(data) >= 2:
                 code = 256 * six.byte2int(data[0:1]) + \
                     six.byte2int(data[1:2])
                 reason = data[2:].decode('utf-8')
                 self.on_close(code, reason)
             return
         else:
             yield ''
Exemple #44
0
def _decode_macaroon_id(id):
    storage_id = b''
    base64_decoded = False
    first = id[:1]
    if first == b'A':
        # The first byte is not a version number and it's 'A', which is the
        # base64 encoding of the top 6 bits (all zero) of the version number 2
        # or 3, so we assume that it's the base64 encoding of a new-style
        # macaroon id, so we base64 decode it.
        #
        # Note that old-style ids always start with an ASCII character >= 4
        # (> 32 in fact) so this logic won't be triggered for those.
        try:
            dec = b64decode(id.decode('utf-8'))
            # Set the id only on success.
            id = dec
            base64_decoded = True
        except:
            # if it's a bad encoding, we'll get an error which is fine
            pass

    # Trim any extraneous information from the id before retrieving
    # it from storage, including the UUID that's added when
    # creating macaroons to make all macaroons unique even if
    # they're using the same root key.
    first = six.byte2int(id[:1])
    if first == VERSION_2:
        # Skip the UUID at the start of the id.
        storage_id = id[1 + 16:]
    if first == VERSION_3:
        try:
            id1 = id_pb2.MacaroonId.FromString(id[1:])
        except google.protobuf.message.DecodeError:
            raise VerificationError('no operations found in macaroon')
        if len(id1.ops) == 0 or len(id1.ops[0].actions) == 0:
            raise VerificationError('no operations found in macaroon')

        ops = []
        for op in id1.ops:
            for action in op.actions:
                ops.append(Op(op.entity, action))
        return id1.storageId, ops

    if not base64_decoded and _is_lower_case_hex_char(first):
        # It's an old-style id, probably with a hyphenated UUID.
        # so trim that off.
        last = id.rfind(b'-')
        if last >= 0:
            storage_id = id[0:last]
    return storage_id, [LOGIN_OP]
Exemple #45
0
def arcfour(key, plaintext):
    """RC4 algorithm, based on wikipedia pseudo-code"""
    S = list(range(256))  # pylint: disable=invalid-name
    keylength = len(key)

    j = 0  # pylint: disable=invalid-name
    for i in range(256):  # pylint: disable=invalid-name
        j = (j + S[i] + six.byte2int([key[i % keylength]])) % 256
        S[i], S[j] = S[j], S[i]

    output = []
    i = 0  # pylint: disable=invalid-name
    j = 0  # pylint: disable=invalid-name
    for char1 in plaintext:
        i = (i + 1) % 256
        j = (j + S[i]) % 256
        S[i], S[j] = S[j], S[i]
        K = (S[i] + S[j]) % 256  # pylint: disable=invalid-name

        char1 = six.byte2int([char1])
        output.append(six.int2byte(char1 ^ S[K]))

    return b"".join(output)
Exemple #46
0
def _crc16(data):
    reg = 0x0000
    data += '\x00\x00'
    for byte in data:
        mask = 0x80
        while mask > 0:
            reg <<= 1
            if byte2int(byte) & mask:
                reg += 1
            mask >>= 1
            if reg > 0xffff:
                reg &= 0xffff
                reg ^= 0x1021
    return reg
Exemple #47
0
    def _verifying_key_from_pubkey(cls, pubkey):
        """ Converts a 33-byte compressed pubkey into an ecdsa.VerifyingKey object. """
        if not isinstance(pubkey, (bytes, bytearray)):
            raise TypeError('pubkey must be raw bytes')
        if len(pubkey) != 33:
            raise ValueError('pubkey must be 33 bytes')
        if byte2int(pubkey[0]) not in (2, 3):
            raise ValueError('invalid pubkey prefix byte')
        curve = cls.CURVE.curve

        is_odd = byte2int(pubkey[0]) == 3
        x = bytes_to_int(pubkey[1:])

        # p is the finite field order
        a, b, p = curve.a(), curve.b(), curve.p()
        y2 = pow(x, 3, p) + b
        assert a == 0  # Otherwise y2 += a * pow(x, 2, p)
        y = NT.square_root_mod_prime(y2 % p, p)
        if bool(y & 1) != is_odd:
            y = p - y
        point = EC.Point(curve, x, y)

        return ecdsa.VerifyingKey.from_public_point(point, curve=cls.CURVE)
Exemple #48
0
 def decode(self, encoded_packet):
     """Decode a transmitted package."""
     b64 = False
     if not isinstance(encoded_packet, binary_types):
         encoded_packet = encoded_packet.encode('utf-8')
     elif not isinstance(encoded_packet, bytes):
         encoded_packet = bytes(encoded_packet)
     self.packet_type = six.byte2int(encoded_packet[0:1])
     if self.packet_type == 98:  # 'b' --> binary base64 encoded packet
         self.binary = True
         encoded_packet = encoded_packet[1:]
         self.packet_type = six.byte2int(encoded_packet[0:1])
         self.packet_type -= 48
         b64 = True
     elif self.packet_type >= 48:
         self.packet_type -= 48
         self.binary = False
     else:
         self.binary = True
     self.data = None
     if len(encoded_packet) > 1:
         if self.binary:
             if b64:
                 self.data = base64.b64decode(encoded_packet[1:])
             else:
                 self.data = encoded_packet[1:]
         else:
             try:
                 self.data = self.json.loads(
                     encoded_packet[1:].decode('utf-8'))
                 if isinstance(self.data, int):
                     # do not allow integer payloads, see
                     # github.com/miguelgrinberg/python-engineio/issues/75
                     # for background on this decision
                     raise ValueError
             except ValueError:
                 self.data = encoded_packet[1:].decode('utf-8')
Exemple #49
0
    def _pack_int8(self, num):
        '''
        Pack a signed 8 bit int into one 8 bit unsigned byte, little-endian
        Returns:
            bytes

        Struct:
            Fmt    C Type                 Python Type    Standard Size
            b      signed char/int8       integer        1
        '''
        try:
            data = struct.pack('<b', num)
        except struct.error as e:
            raise ArgumentOutOfRangeException(e)
        return chr(six.byte2int(data[:1]))
Exemple #50
0
    def _update_proto_config(self):
        self.proto_config = {}
        if self._noconnect:
            self.proto_config['ext_proto']  = True
            self.proto_config['auto_send']  = True
            self.proto_config['handshake']  = False
            self.proto_config['pw_access']  = False
            self.proto_config['punch_read'] = False
            self.proto_config['mode'] = 2
        else:
            # Read protocol configuration
            ret = self._send_command(SIReader.C_GET_SYS_VAL, SIReader.O_PROTO+b'\x01')
            config_byte = byte2int(ret[1][1])
            self.proto_config['ext_proto']  = config_byte & (1 << 0) != 0
            self.proto_config['auto_send']  = config_byte & (1 << 1) != 0
            self.proto_config['handshake']  = config_byte & (1 << 2) != 0
            self.proto_config['pw_access']  = config_byte & (1 << 4) != 0
            self.proto_config['punch_read'] = config_byte & (1 << 7) != 0
    
            # Read operating mode
            ret = self._send_command(SIReader.C_GET_SYS_VAL, SIReader.O_MODE+b'\x01')
            self.proto_config['mode'] = byte2int(ret[1][1])

        return self.proto_config
def send_server_parameter_change_request_msg(socket, command, location,
                                             service_name):
    msg = AllProtoMsg_pb2.ServerParameterChangeRequestMsg()
    msg.Command = command
    msg.Location = location
    msg.ServiceName = service_name
    custom_log.log_debug_task("Send ServerParameterChangeRequestMsg")
    msg_str = msg.SerializeToString()
    msg_list = [six.int2byte(13), msg_str]

    socket.send_multipart(msg_list)
    recv_message = socket.recv_multipart()
    custom_log.log_debug_task("Recv Msg.")
    recv_result = six.byte2int(recv_message[0])
    return recv_result
Exemple #52
0
def _bytes_text(code_point_iter, quote, prefix=b'', suffix=b''):
    quote_code_point = six.byte2int(quote)
    buf = BytesIO()
    buf.write(prefix)
    buf.write(quote)
    for code_point in code_point_iter:
        if code_point == quote_code_point:
            buf.write(b'\\' + quote)
        if _is_printable_ascii(code_point):
            buf.write(six.int2byte(code_point))
        else:
            buf.write(_escape(code_point))
    buf.write(quote)
    buf.write(suffix)
    return buf.getvalue()
Exemple #53
0
 def _preprocess_response(code, data, log_debug):
     if code == Sportiduino.RESP_ERROR:
         if data == Sportiduino.ERR_COM:
             raise SportiduinoException("COM error")
         elif data == Sportiduino.ERR_WRITE_CARD:
             raise SportiduinoException("Card write error")
         elif data == Sportiduino.ERR_READ_CARD:
             raise SportiduinoException("Card read error")
         elif data == Sportiduino.ERR_READ_EEPROM:
             raise SportiduinoException("EEPROM read error")
         else:
             raise SportiduinoException("Error with code %s" % hex(byte2int(code)))
     elif code == Sportiduino.RESP_OK:
         log_debug("Ok received")
     return code, data
Exemple #54
0
    def decode(cls, data: bytes) -> Tuple[Optional[ClockSAL], bytes]:
        """
        Do not call this method directly -- use ClockSAL.decode
        """

        argument = byte2int(data)
        data = data[1:]

        if argument != 0x03:
            warnings.warn(
                'Request refresh argument != 3 (got %d instead)' % argument,
                UserWarning)
            return None, data

        return cls(), data
Exemple #55
0
    def validate(self):
        """
        validate the ABNF frame.
        """
        if self.rsv1 or self.rsv2 or self.rsv3:
            raise WebSocketProtocolException("rsv is not implemented, yet")

        if self.opcode not in ABNF.OPCODES:
            raise WebSocketProtocolException("Invalid opcode %r", self.opcode)

        if self.opcode == ABNF.OPCODE_PING and not self.fin:
            raise WebSocketProtocolException("Invalid ping frame.")

        if self.opcode == ABNF.OPCODE_CLOSE:
            l = len(self.data)
            if not l:
                return
            if l == 1 or l >= 126:
                raise WebSocketProtocolException("Invalid close frame.")

            code = 256 * \
                six.byte2int(self.data[0:1]) + six.byte2int(self.data[1:2])
            if not self._is_valid_close_status(code):
                raise WebSocketProtocolException("Invalid close opcode.")
Exemple #56
0
    def _version_negotiation(self, client_socket, server_socket):
        """Performs the RFB Version negotiation between client/server.

        :param client_socket:  The client-side socket to receive data from.
        :param server_socket:  The server-side socket to forward data to.
        """
        # Do a pass-thru of the RFB Version negotiation up-front
        # The length of the version is 12, such as 'RFB 003.007\n'
        client_socket.sendall(self._socket_receive(server_socket, 12))
        server_socket.sendall(self._socket_receive(client_socket, 12))
        # Since we are doing our own additional authentication
        # just tell the server we are doing No Authentication (1) to it
        auth_size = self._socket_receive(server_socket, 1)
        self._socket_receive(server_socket, six.byte2int(auth_size))
        server_socket.sendall(six.int2byte(1))
def send_strategy_parameter_change_request_msg(socket, strategy_name, location,
                                               control_flag):
    msg = AllProtoMsg_pb2.StrategyParameterChangeRequestMsg()
    msg.Name = strategy_name
    msg.IsEnable = control_flag
    msg.Location = location
    custom_log.log_debug_task("Send StrategyParameterChangeRequestMsg")
    msg_str = msg.SerializeToString()
    msg_type = const.MSG_TYPEID_ENUMS.StrategyParameterChangeRequest
    msg_list = [six.int2byte(msg_type), msg_str]
    socket.send_multipart(msg_list)
    recv_message = socket.recv_multipart()
    custom_log.log_debug_task("Recv Msg.")
    recv_result = six.byte2int(recv_message[0])
    return recv_result
    def test_rsa_public_exponent_mhsm(self, **kwargs):
        """The public exponent of a Managed HSM RSA key can be specified during creation"""
        self._skip_if_not_configured(True)
        endpoint_url = self.managed_hsm_url

        client = self.create_key_client(endpoint_url)
        self.assertIsNotNone(client)

        key_name = self.get_resource_name("rsa-key")
        key = self._create_rsa_key(client,
                                   key_name,
                                   hardware_protected=True,
                                   public_exponent=17)
        public_exponent = byte2int(key.key.e)
        assert public_exponent == 17
Exemple #59
0
def decrypt(key, ciphertext):
    """Given the first 16 bytes of splunk.secret, decrypt a Splunk password"""
    if ciphertext.startswith("$1$"):
        ciphertext = base64.b64decode(ciphertext[3:])

    algorithm = algorithms.ARC4(key)
    cipher = Cipher(algorithm, mode=None, backend=default_backend())
    decryptor = cipher.decryptor()
    plaintext = decryptor.update(ciphertext)

    chars = []
    for char1, char2 in zip(plaintext[:-1], itertools.cycle("DEFAULTSA")):
        chars.append(six.byte2int([char1]) ^ ord(char2))

    return "".join([six.unichr(c) for c in chars])
Exemple #60
0
    def decode_packet(cls, data: bytes, checksum: bool,
                      priority_class: PriorityClass) -> PointToPointPacket:

        # now decode the unit address or bridge address
        params = {}
        if indexbytes(data, 1) == 0x00:
            # this is a unit address
            unit_address = byte2int(data)
            data = data[2:]
        else:
            params['bridge_address'] = byte2int(data)

            bridge_length = BRIDGE_LENGTHS[indexbytes(data, 1)]

            data = data[2:]
            params['hops'] = hops = []

            for x in range(bridge_length):
                # get all the hops
                hops.append(byte2int(data))
                data = data[1:]

            unit_address = byte2int(data)

            data = data[1:]

        # now decode messages
        cals = []
        while data:
            cal, cal_len = cls.decode_cal(data)
            data = data[cal_len:]
            cals.append(cal)

        return PointToPointPacket(
            checksum=checksum, priority_class=priority_class,
            unit_address=unit_address, cals=cals, **params)