Example #1
1
    def __init__(self, file):
        # Parse the header
        if isinstance(file, str):
            file = open(file, "rb")
        assert file.read(4) == "PTCH"  # Magic

        ##
        # Sizes
        # - patchSize: Size of the entire patch data, not including the 'PTCH' signature itself
        # - sizeBefore: Size of the file before patching
        # - sizeAfter: Size of the file after patching
        self.patchSize, self.sizeBefore, self.sizeAfter = unpack("iii", file.read(12))

        ##
        # MD5 block
        # - md5BlockSize: Size of the MD5 block, including the signature and size
        # - md5Before: MD5 digest of the original (unpatched) file
        # - md5After: MD5 digest of the patched file
        assert file.read(4) == "MD5_"
        self.md5BlockSize, = unpack("i", file.read(4))
        self.md5Before, self.md5After = unpack("16s16s", file.read(32))
        self.md5Before, self.md5After = hexlify(self.md5Before), hexlify(self.md5After)

        ##
        # XFRM block
        # - xfrmBlockSize: Size of the XFRM block + the packed data
        # - unpackedSize: Unpacked size of the patch data
        assert file.read(4) == "XFRM"
        self.xfrmBlockSize, = unpack("i", file.read(4))
        assert file.read(4) in ("BSD0", "COPY")  # patch type?
        self.unpackedSize, = unpack("i", file.read(4))

        self.compressedDiff = file.read()

        file.close()
 def deserialize(self, str):
     """
 unpack serialized message in str into this message instance
 :param str: byte array of serialized message, ``str``
 """
     try:
         end = 0
         _x = self
         start = end
         end += 9
         (_x.bool_resp, _x.rtn_code, _x.plan_id) = _struct_B2i.unpack(str[start:end])
         self.bool_resp = bool(self.bool_resp)
         start = end
         end += 4
         (length,) = _struct_I.unpack(str[start:end])
         pattern = "<%sf" % length
         start = end
         end += struct.calcsize(pattern)
         self.q_vec_start = struct.unpack(pattern, str[start:end])
         start = end
         end += 4
         (length,) = _struct_I.unpack(str[start:end])
         pattern = "<%sf" % length
         start = end
         end += struct.calcsize(pattern)
         self.q_vec_end = struct.unpack(pattern, str[start:end])
         return self
     except struct.error as e:
         raise genpy.DeserializationError(e)  # most likely buffer underfill
Example #3
1
    def deserialize_tx_payload(self, data):
        msg = {}
        if type(data) is str:
            data = StringIO(data)

        msg["version"] = struct.unpack("<I", data.read(4))[0]

        msg["tx_in_count"] = self.deserialize_int(data)
        msg["tx_in"] = []
        for _ in xrange(msg["tx_in_count"]):
            tx_in = self.deserialize_tx_in(data)
            msg["tx_in"].append(tx_in)

        msg["tx_out_count"] = self.deserialize_int(data)
        msg["tx_out"] = []
        for _ in xrange(msg["tx_out_count"]):
            tx_out = self.deserialize_tx_out(data)
            msg["tx_out"].append(tx_out)

        msg["lock_time"] = struct.unpack("<I", data.read(4))[0]

        # Calculate hash from the entire payload
        payload = self.serialize_tx_payload(msg)
        msg["tx_hash"] = hexlify(sha256(sha256(payload))[::-1])

        return msg
Example #4
1
def _unpack_response(response, cursor_id=None, as_class=dict, tz_aware=False, uuid_subtype=OLD_UUID_SUBTYPE):
    """Unpack a response from the database.

    Check the response for errors and unpack, returning a dictionary
    containing the response data.

    :Parameters:
      - `response`: byte string as returned from the database
      - `cursor_id` (optional): cursor_id we sent to get this response -
        used for raising an informative exception when we get cursor id not
        valid at server response
      - `as_class` (optional): class to use for resulting documents
    """
    response_flag = struct.unpack("<i", response[:4])[0]
    if response_flag & 1:
        # Shouldn't get this response if we aren't doing a getMore
        assert cursor_id is not None

        raise OperationFailure("cursor id '%s' not valid at server" % cursor_id)
    elif response_flag & 2:
        error_object = bson.BSON(response[20:]).decode()
        if error_object["$err"].startswith("not master"):
            raise AutoReconnect(error_object["$err"])
        raise OperationFailure("database error: %s" % error_object["$err"])

    result = {}
    result["cursor_id"] = struct.unpack("<q", response[4:12])[0]
    result["starting_from"] = struct.unpack("<i", response[12:16])[0]
    result["number_returned"] = struct.unpack("<i", response[16:20])[0]
    result["data"] = bson.decode_all(response[20:], as_class, tz_aware, uuid_subtype)
    assert len(result["data"]) == result["number_returned"]
    return result
Example #5
1
File: mzdb.py Project: manor/mz
    def xic(self, start_time, stop_time, start_mz, stop_mz, filter=None):
        """Generates eXtracted Ion Chromatogram (XIC) for given time and mz range

        The function integrates the precursor intensities for given time and mz range.
        The xic is a list of (mz,intensity) pairs.

        Example:
        >>> xic = myPeakFile.xic(31.4, 32.4, 435.82, 436.00)

        """
        conn = sqlite3.connect(self.data_file)
        conn.row_factory = sqlite3.Row
        c = conn.cursor()
        # s_start =self.scanForTime(start_time)
        # s_stop =self.scanForTime(stop_time)
        c.execute(
            """select time,peak_num,mzs,ys from scans where time>=? and time<=? and precursor = 0.0 ORDER BY time""",
            (start_time, stop_time),
        )
        xic = []
        for r in c:
            peak_num = r["peak_num"]
            s_time = r["time"]
            mzs = struct.unpack("!%df" % peak_num, r["mzs"])
            ys = struct.unpack("!%df" % peak_num, r["ys"])
            sdata = zip(mzs, ys)
            xic.append((s_time, sum([y for (m, y) in sdata if m >= start_mz and m <= stop_mz])))
        return xic
    def _update_info(self):
        """ Call the command getInfo and fill up the information received in
        the fields of the object
        """

        # Call getInfo ...
        pk = CRTPPacket()
        pk.set_header(0xFF, 0xFF)
        pk.data = (0xFF, 0x10)
        self.link.send_packet(pk)

        # Wait for the answer
        pk = self.link.receive_packet(2)

        if pk and pk.header == 0xFF and struct.unpack("<BB", pk.data[0:2]) == (0xFF, 0x10):
            tab = struct.unpack("BBHHHH", pk.data[0:10])
            cpuid = struct.unpack("B" * 12, pk.data[10:22])
            if len(pk.data) > 22:
                self.protocol_version = pk.data[22]
            self.page_size = tab[2]
            self.buffer_pages = tab[3]
            self.flash_pages = tab[4]
            self.start_page = tab[5]
            self.cpuid = "%02X" % cpuid[0]
            for i in cpuid[1:]:
                self.cpuid += ":%02X" % i

            return True

        return False
Example #7
1
def jpgDims(jpegFile):
    jpeg = open(jpegFile, "rb")
    jpeg.read(2)
    b = jpeg.read(1)
    try:
        while b and ord(b) != 0xDA:
            while ord(b) != 0xFF:
                b = jpeg.read
            while ord(b) == 0xFF:
                b = jpeg.read(1)
            if ord(b) >= 0xC0 and ord(b) <= 0xC3:
                jpeg.read(3)
                h, w = struct.unpack(">HH", jpeg.read(4))
                break
            else:
                jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0]) - 2)
            b = jpeg.read(1)
        width = int(w)
        height = int(h)
    except struct.error:
        pass
    except ValueError:
        pass

    return width, height
Example #8
0
    def CheckIncomingPDU(self):
        rawpdu = ""
        # There is something to read
        # read type
        try:
            rawpdu = self.RemoteClientSocket.recv(1)
        except socket.error:
            self.event.put("Evt17")
            self.RemoteClientSocket.close()
            self.RemoteClientSocket = None
            return

        if rawpdu == "":
            # Remote port has been closed
            self.event.put("Evt17")
            self.RemoteClientSocket.close()
            self.RemoteClientSocket = None
            return
        else:
            type = unpack("B", rawpdu)
            res = recvn(self.RemoteClientSocket, 1)
            rawpdu += res
            res = unpack("B", res)
            length = recvn(self.RemoteClientSocket, 4)
            rawpdu += length
            length = unpack(">L", length)
            tmp = recvn(self.RemoteClientSocket, length[0])
            rawpdu += tmp
            # Determine the type of PDU coming on remote port
            # and set the event accordingly
            self.pdu = Socket2PDU(rawpdu)
            self.event.put(PDU2Event(self.pdu))

            self.primitive = self.pdu.ToParams()
Example #9
0
def init_cam():
    global PASSWORD
    try:
        bcv = httpopen('http://%s/bacpac/cv' % CAM_IP).read()
        print('bacpac CV:', repr(bcv))
        bacpac_version = struct.unpack('BBB', bcv[9:12])
        bacpac_version = '.'.join([str(x) for x in bacpac_version])
        print('   bacpac version:', bacpac_version)
        bacpac_mac = bcv[12:18]
        bacpac_mac = ':'.join(['%02x' % x for x in bacpac_mac])
        print('bacpac mac:', bacpac_mac)
        bacpac_name = bcv[19:].decode('utf-8')
        print('bacpac name:', bacpac_name)
        
        bsd = httpopen('http://%s/bacpac/sd' % CAM_IP).read()
        print('bacpac SD:', repr(bsd))
        PASSWORD = bsd[2:].decode('utf-8')
        print('bacpac password', PASSWORD)
     
        ccv = httpopen('http://%s/camera/cv' % CAM_IP).read()
        print('camera CV:', repr(ccv))
        # b'\x00\x00\x01\x13HD2.08.12.198.47.00\x05HERO2'
        dlen = struct.unpack('B', ccv[3:4])[0]
        camera_version = ccv[4:4+dlen].decode('UTF-8')
        print('camera version', camera_version)
        ipos = 4+dlen
        dlen = struct.unpack('B', ccv[ipos:ipos+1])[0]
        ipos += 1
        camera_model = ccv[ipos:ipos+dlen].decode('UTF-8')
        print('camera_model', camera_model) #FIXME this is the CN parameter
        return True
    except (urllib.error.HTTPError, urllib.error.URLError, socket.error):
        print('Error communicating with bacpac/camera')
        return False
Example #10
0
def tcdbiter(fp, eor):
    locs = {}
    fp.seek(eor)
    while 1:
        x = fp.read(8)
        if not x:
            break
        (h, pos) = unpack("<II", x)
        if pos:
            locs[pos] = h
    pos = 2048
    fp.seek(pos)
    key = ()
    parents = [0]
    while pos < eor:
        (klen, vlen) = unpack("<II", fp.read(8))
        k = fp.read(klen)
        v = fp.read(vlen)
        h = locs[pos]
        for (i, p) in enumerate(parents):
            if cdbhash(k, p + 5381L) == h:
                parents = parents[: i + 1]
                key = key[:i]
                break
        key += (k,)
        yield (key, v)
        parents.append(pos)
        pos += 8 + klen + vlen
    fp.close()
    return
def parse_data(msg, ie_key):
    global g_code_length
    g_code_length = ord(msg[1]) & 127
    received_length = 0
    if g_code_length == 126:
        g_code_length = struct.unpack(">H", str(msg[2:4]))[0]
        masks = msg[4:8]
        data = msg[8:]
    elif g_code_length == 127:
        g_code_length = struct.unpack(">Q", str(msg[2:10]))[0]
        masks = msg[10:14]
        data = msg[14:]
    else:
        masks = msg[2:6]
        data = msg[6:]

    i = 0
    raw_str = ""

    for d in data:
        raw_str += chr(ord(d) ^ ord(masks[i % 4]))
        i += 1

    if "\x03\xe9" in raw_str.split():
        sys.exit()
    print "输入[%s]" % raw_str
    try:
        raw_str_source = eval(raw_str)
        raw_str = raw_str_source["cmd"]
        selectserver = raw_str_source["selectserver"]

    except Exception, e:
        print "命令格式错误", e
        sys.exit(1)
Example #12
0
def __parse_tag(fh):
    """
    Reads and parses a single tag.

    returns endian, tag_type, next_tag, next_same
    """
    data = fh.read(16)
    # byte order format for this data. Uses letter “I” for Intel format
    # data (little endian) or letter “M” for Motorola (big endian) format
    format = unpack(b"=c", data[0:1])[0]
    if format == b"I":
        endian = b"<"
    elif format == b"M":
        endian = b">"
    else:
        raise ValueError("Invalid tag: missing byte order information")
    # magic: check for magic number "31"
    magic = unpack(endian + b"B", data[1:2])[0]
    if magic != 31:
        raise ValueError("Invalid tag: missing magic number")
    # tag type: the type of data attached to this tag.
    tag_type = unpack(endian + b"H", data[2:4])[0]
    # NextTag is the offset in bytes from the end of this tag to the start of
    # the next tag. That means, the offset is the size of the data attached
    # to this tag.
    next_tag = unpack(endian + b"i", data[4:8])[0]
    # NextSame is the offset in bytes from the end of this tag to the start
    # of the next tag with the same type. If zero, there is no next tag with
    # the same type.
    next_same = unpack(endian + b"i", data[8:12])[0]
    return endian, tag_type, next_tag, next_same
Example #13
0
    def queryPlayers(self):

        data = self.cmd("qp")
        data = struct.unpack(">2H", data)
        numTeams_, numPlayers = data
        data = self.getResponse("tu")
        numTeams, data = ord(data[0]), data[1:]
        # if numTeams != numTeams_ :
        # 	raise Error( 'Inconsistency in numTeams (got %d and %d)' \
        # 		% ( numTeams_ , numTeams ) )
        teamsInfo = {}
        for i in range(numTeams):
            teamInfo, data = data[:8], data[8:]
            team, size, won, lost = struct.unpack(">4H", teamInfo)
            score = won - lost
            teamsInfo[teamsName[team]] = {"size": size, "score": score, "won": won, "lost": lost}
        playersInfo = []
        for i in range(numPlayers):
            data = self.getResponse("ap")
            pId, type, team, won, lost, tks, sign, motto = struct.unpack(">b5H32s128s", data)
            playerInfo = {
                "pId": pId,
                "type": type,
                "team": teamsName[team],
                "score": won - lost,
                "won": won,
                "lost": lost,
                "tks": tks,
                "sign": sign.rstrip("\x00"),
                "motto": motto.rstrip("\x00"),
            }
            playersInfo.append(playerInfo)
        return teamsInfo, playersInfo
Example #14
0
    def _readheader(self, infile):
        """
        Read and decode the header of a Rigaku RAXIS image.
        The Rigaku format uses a block of (at least) 1400 bytes for storing
        information. The information has a fixed structure, but endianness
        can be flipped for non-char values. Header items which are not
        capitalised form part of a non-standardized data block and may not
        be accurate.

        TODO: It would be useful to have an automatic endianness test in here.

        @param infile: Opened python file (can be stringIO or bzipped file)
        """
        endianness = self.endianness
        # list of header key to keep the order (when writing)
        RKey, orderList = self.rigakuKeys()
        self.header = {}
        self.header_keys = orderList

        # swapBool=False
        fs = endianness
        minHeaderLength = 1400  # from rigaku's def
        # if (numpy.little_endian and endianness=='>'):
        #    swapBool=True
        # file should be open already
        # fh=open(filename,'rb')
        infile.seek(0)  # hopefully seeking works.
        rawHead = infile.read(minHeaderLength)
        # fh.close() #don't like open files in case of intermediate crash

        self.header = dict()
        curByte = 0
        for key in orderList:
            if isinstance(RKey[key], int):
                # read a number of bytes, convert to char.
                # if -1, read remainder of header
                if RKey[key] == -1:
                    rByte = len(rawHead) - curByte
                    self.header[key] = struct.unpack(fs + str(rByte) + "s", rawHead[curByte : curByte + rByte])[0]
                    curByte += rByte
                    break

                rByte = RKey[key]
                self.header[key] = struct.unpack(fs + str(rByte) + "s", rawHead[curByte : curByte + rByte])[0]
                curByte += rByte
            elif RKey[key] == "float":
                # read a float, 4 bytes
                rByte = 4
                self.header[key] = struct.unpack(fs + "f", rawHead[curByte : curByte + rByte])[0]
                curByte += rByte
            elif RKey[key] == "long":
                # read a long, 4 bytes
                rByte = 4
                self.header[key] = struct.unpack(fs + "l", rawHead[curByte : curByte + rByte])[0]
                curByte += rByte
            else:
                logging.warn("special header data type {} not understood".format(Rkey[key]))
            if len(rawHead) == curByte:
                # "end reached"
                break
Example #15
0
 def read_lap(self):
     raw = self.read(0x20)
     lap = {}
     (
         hh,
         mm,
         ss,
         sss,
         dist,
         cal,
         speed_max,
         speed,
         lap["autolap"],
         beats_high,
         beats_mid,
         beats_low,
         lap["sec"],
         lap["MaxHeart"],
         lap["MinHeart"],
         z0,
         lap["Lap"],
         track_number,
         sign,
     ) = struct.unpack(">4BI IxBxB 4BH2B 4s HBB", raw)
     if 0xFD != sign:
         raise BadSignature()
     lap["Elevation"] = struct.unpack("<I", z0)[0] / 1e2
     lap["Time"] = hh * 3600 + mm * 60 + ss + sss / 100.0
     lap["Speed"] = speed / 10.0
     lap["Distance"] = dist / 1e5
     lap["kcal"] = cal / 1e4
     lap["MaxSpeed"] = speed_max / 10.0
     lap["Beats"] = beats_high << 16 | beats_mid << 8 | beats_low
     return lap
Example #16
0
 def deserialize(self, f):
     self.nVersion = struct.unpack("<i", f.read(4))[0]
     self.vin = deser_vector(f, CTxIn)
     self.vout = deser_vector(f, CTxOut)
     self.nLockTime = struct.unpack("<I", f.read(4))[0]
     self.sha256 = None
     self.hash = None
Example #17
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 #18
0
 def _parse_hdrs(self, data):
     "Given a control frame data block, return a list of (name, value) tuples."
     # TODO: separate null-delimited into separate instances
     if self._decompress != dummy:
         data = self._decompress(data)  # FIXME: catch errors
     cursor = 2
     (num_hdrs,) = struct.unpack("!h", data[:cursor])  # FIXME: catch errors
     hdrs = []
     while cursor < len(data):
         try:
             (name_len,) = struct.unpack("!h", data[cursor : cursor + 2])  # FIXME: catch errors
             cursor += 2
             name = data[cursor : cursor + name_len]  # FIXME: catch errors
             cursor += name_len
         except IndexError:
             raise
         except struct.error:
             raise
         try:
             (val_len,) = struct.unpack("!h", data[cursor : cursor + 2])  # FIXME: catch errors
             cursor += 2
             value = data[cursor : cursor + val_len]  # FIXME: catch errors
             cursor += val_len
         except IndexError:
             raise
         except struct.error:
             print len(data), cursor, data  # FIXME
             raise
         hdrs.append((name, value))
     return hdrs
Example #19
0
    def receive(sock):
        """Receives a block sent over the network."""

        header = sock.recv(2)
        block_type, = unpack("!H", header)
        block = USPBlock(block_type)

        # Note that here I deliberately increase the size compared to send()
        # because some of the code suggests that blocks larger than 512 bytes
        # may actually exist
        magic_number = 4096

        last = False
        while not last:
            subheader, = unpack("!H", sock.recv(2))
            last = (subheader & 0x8000) != 0
            size = (subheader & 0x0FFF) - 2
            if size > magic_number:
                raise ProtocolError("Subblock size is too large")

            buffer = b""
            while len(buffer) < size:
                old_len = len(buffer)
                buffer += sock.recv(size - len(buffer))
                if len(buffer) == old_len:
                    raise ProtocolError("Connection broken while transmitting a block")

            block.buffer += buffer

        return block
Example #20
0
    def write_register(self, args):
        register, value = args.split(b"=", 2)
        index = int(register, 16)

        if index is None:
            self.rsp.send_packet(b"E00")
            return

        archdef = architectures["i386:x86-64"]

        register = archdef["registers"][index]
        register_name = register["name"]
        register_size = register["size"]

        s = 0
        if register_size == 4:
            s, = struct.unpack("<I", binascii.unhexlify(value))
        elif register_size == 8:
            s, = struct.unpack("<Q", binascii.unhexlify(value))
        elif register_size == 16:
            s = struct.unpack("<QQ", binascii.unhexlify(value))
        elif register_size == 10:
            s = binascii.unhexlify(value)

        value = self.write_register_impl(register_name, s)
        self.rsp.send_packet(b"OK")
Example #21
0
   def read_command(self):
       """Read a command from the client
	First byte read is the length of the command.
	Then 2 bytes for ephemeral port.
	Then the command, which is saved to self.command
	"""
       size_bytes = self._recv(1)
       if not size_bytes:
           self.kill()
           return
       size = struct.unpack("!B", size_bytes)[0]
       port_bytes = self._recv(2)
       self.ephemeral = struct.unpack("!H", port_bytes)[0]
       command = self._recv(size).decode("utf-8")
       if command in ["ls", "quit"]:
           self.command = command
       elif command[:3] in ["get", "put"]:
           try:
               command, fn = command.split()
           except ValueError as e:
               # Too many arguments given
               self.log("error parsing: " + command)
               self.kill()
               self.command = ""
               return
           self.command = command
           self.filename = fn
       else:
           _err_log("Received invalid command: " + command)
           self.command = ""
           self.kill()
Example #22
0
def terminal_size():
    """
  Determines the size of the terminal.
  """

    if os.name == "nt":
        # http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/
        import ctypes, struct

        h = ctypes.windll.kernel32.GetStdHandle(-12)
        csbi = ctypes.create_string_buffer(22)
        res = ctypes.windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
        if res:
            (bufx, bufy, curx, cury, wattr, left, top, right, bottom, maxx, maxy) = struct.unpack(
                "hhhhHhhhhhh", csbi.raw
            )
            sizex = right - left + 1
            sizey = bottom - top + 1
        else:
            sizex, sizey = 80, 25
        return (sizex, sizey)
    else:
        # http://stackoverflow.com/a/3010495/791713
        import fcntl, termios, struct

        h, w, hp, wp = struct.unpack("HHHH", fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack("HHHH", 0, 0, 0, 0)))
        return w, h
Example #23
0
File: mzdb.py Project: manor/mz
    def scan(self, time):
        """Gets scan based on the specified scan time

        The scan is a list of (mz, intensity) pairs.

        Example:
        >>> scan = myPeakFile.scan(20.035)

        """
        conn = sqlite3.connect(self.data_file)
        conn.row_factory = sqlite3.Row
        c = conn.cursor()

        # Sneaky ability to access scans directly by scan_num... Shhhhh....
        if isinstance(time, float):
            scan_num = self.scanForTime(time)
        else:
            scan_num = time

        (scan_time, mz, sn, st, scan_mode) = self.headers()[scan_num - 1]
        c.execute("""select peak_num,mzs,ys from scans where time==?""", (scan_time,))
        r = c.fetchone()
        peak_num = r["peak_num"]
        mzs = struct.unpack("!%df" % peak_num, r["mzs"])
        ys = struct.unpack("!%df" % peak_num, r["ys"])
        sdata = zip(mzs, ys)
        z = self.extra_info(scan_num)["Charge State"]
        return Scan(sdata, scan_time, scan_mode, mz, z)
Example #24
0
    def decrypt(self, machine_number, eeprom_uid, cartridge_crypted):
        cartridge_packed = cartridge_crypted

        # Validate key fragment checksum
        # TODO

        # Build the key
        key = self.build_key(cartridge_crypted[0x48:0x50], machine_number, eeprom_uid)
        # Validate crypted content checksum
        if (
            self.checksum.checksum(cartridge_crypted[0x00:0x40])
            != struct.unpack("<H", str(cartridge_crypted[0x46:0x48]))[0]
        ):
            raise Exception("invalid crypted content checksum")
        # Decrypt content
        cartridge_packed[0x00:0x40] = self.crypto.decrypt(key, cartridge_crypted[0x00:0x40])
        # Validate crypted current material quantity checksum
        if (
            self.checksum.checksum(cartridge_crypted[0x58:0x60])
            != struct.unpack("<H", str(cartridge_crypted[0x60:0x62]))[0]
        ):
            raise Exception("invalid current material quantity checksum")
        # Decrypt current material quantity
        cartridge_packed[0x58:0x60] = self.crypto.decrypt(key, cartridge_crypted[0x58:0x60])

        return cartridge_packed
Example #25
0
def get_type(path):
    """
    Check the signature of the give file and returns what kind of executable
    matches.
    """
    with open(path, "rb") as f:
        signature = f.read(4)
        if len(signature) < 4:
            return UNKNOWN
        signature = struct.unpack(">L", signature)[0]
        if signature == ELF_SIGNATURE:
            return ELF
        if signature in MACHO_SIGNATURES:
            return MACHO
        if signature != FAT_SIGNATURE:
            return UNKNOWN
        # We have to sanity check the second four bytes, because Java class
        # files use the same magic number as Mach-O fat binaries.
        # This logic is adapted from file(1), which says that Mach-O uses
        # these bytes to count the number of architectures within, while
        # Java uses it for a version number. Conveniently, there are only
        # 18 labelled Mach-O architectures, and Java's first released
        # class format used the version 43.0.
        num = f.read(4)
        if len(num) < 4:
            return UNKNOWN
        num = struct.unpack(">L", num)[0]
        if num < 20:
            return MACHO
        return UNKNOWN
Example #26
0
    def getComponentNames(self, glyfTable):
        if not hasattr(self, "data"):
            if self.isComposite():
                return [c.glyphName for c in self.components]
            else:
                return []

                # Extract components without expanding glyph

        if not self.data or struct.unpack(">h", self.data[:2])[0] >= 0:
            return []  # Not composite

        data = self.data
        i = 10
        components = []
        more = 1
        while more:
            flags, glyphID = struct.unpack(">HH", data[i : i + 4])
            i += 4
            flags = int(flags)
            components.append(glyfTable.getGlyphName(int(glyphID)))

            if flags & ARG_1_AND_2_ARE_WORDS:
                i += 4
            else:
                i += 2
            if flags & WE_HAVE_A_SCALE:
                i += 2
            elif flags & WE_HAVE_AN_X_AND_Y_SCALE:
                i += 4
            elif flags & WE_HAVE_A_TWO_BY_TWO:
                i += 8
            more = flags & MORE_COMPONENTS

        return components
Example #27
0
    def deserialize_version_payload(self, data):
        msg = {}
        data = StringIO(data)

        msg["version"] = struct.unpack("<i", data.read(4))[0]
        if msg["version"] < MIN_PROTOCOL_VERSION:
            raise IncompatibleClientError("{} < {}".format(msg["version"], MIN_PROTOCOL_VERSION))

        msg["services"] = unpack("<Q", data.read(8))
        msg["timestamp"] = unpack("<q", data.read(8))

        msg["to_addr"] = self.deserialize_network_address(data)
        msg["from_addr"] = self.deserialize_network_address(data)

        msg["nonce"] = unpack("<Q", data.read(8))

        msg["user_agent"] = self.deserialize_string(data)

        msg["height"] = unpack("<i", data.read(4))

        try:
            msg["relay"] = struct.unpack("<?", data.read(1))[0]
        except struct.error:
            msg["relay"] = False

        return msg
Example #28
0
def loadmtx(filename):
    """
    Loads an mtx file (binary compressed file)
    (first two lines of the MTX contain information of the data shape and
    what units, limits are present)
    i.e.:

    mtx, header = loadmtx('filename.mtx')

    mtx     :   will contain a 3d numpy array of the data
    header  :   will contain information on the labels and limits
    """
    with open(filename, "rb") as f:

        line = f.readline()
        header = line[:-1].split(",")
        # header = line

        line = f.readline()
        a = line[:-1].split(" ")
        s = np.array(map(float, a))

        raw = f.read()  # reads everything else
        f.close()

    if s[3] == 4:
        data = unpack("f" * (s[2] * s[1] * s[0]), raw)  # uses float
        M = np.reshape(data, (s[2], s[1], s[0]), order="F")
    else:
        data = unpack("d" * (s[2] * s[1] * s[0]), raw)  # uses double
        M = np.reshape(data, (s[2], s[1], s[0]), order="F")
    return M, header
Example #29
0
    def deserialize_block_payload(self, data):
        msg = {}

        # Calculate hash from: version (4 bytes) + prev_block_hash (32 bytes) +
        # merkle_root (32 bytes) + timestamp (4 bytes) + bits (4 bytes) +
        # nonce (4 bytes) = 80 bytes
        msg["block_hash"] = hexlify(sha256(sha256(data[:80]))[::-1])

        data = StringIO(data)

        msg["version"] = struct.unpack("<I", data.read(4))[0]

        # BE (big-endian) -> LE (little-endian)
        msg["prev_block_hash"] = hexlify(data.read(32)[::-1])

        # BE -> LE
        msg["merkle_root"] = hexlify(data.read(32)[::-1])

        msg["timestamp"] = struct.unpack("<I", data.read(4))[0]
        msg["bits"] = struct.unpack("<I", data.read(4))[0]
        msg["nonce"] = struct.unpack("<I", data.read(4))[0]

        msg["tx_count"] = self.deserialize_int(data)
        msg["tx"] = []
        for _ in xrange(msg["tx_count"]):
            tx_payload = self.deserialize_tx_payload(data)
            msg["tx"].append(tx_payload)

        return msg
Example #30
0
 def deserialize(self, str):
     """
 unpack serialized message in str into this message instance
 :param str: byte array of serialized message, ``str``
 """
     try:
         end = 0
         _x = self
         start = end
         end += 8
         (_x.rec_rate, _x.sigma_pose) = _struct_2f.unpack(str[start:end])
         start = end
         end += 4
         (length,) = _struct_I.unpack(str[start:end])
         pattern = "<%sf" % length
         start = end
         end += struct.calcsize(pattern)
         self.prob_dist = struct.unpack(pattern, str[start:end])
         start = end
         end += 4
         (self.sigma_filtered,) = _struct_f.unpack(str[start:end])
         start = end
         end += 4
         (length,) = _struct_I.unpack(str[start:end])
         pattern = "<%sf" % length
         start = end
         end += struct.calcsize(pattern)
         self.filt_dist = struct.unpack(pattern, str[start:end])
         return self
     except struct.error as e:
         raise genpy.DeserializationError(e)  # most likely buffer underfill