Python zlib.decompress() Examples

The following are 30 code examples of zlib.decompress(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module zlib , or try the search function .
Example #1
Source File: vachat.py    From The-chat-room with MIT License 10 votes vote down vote up
def run(self):
        print("VEDIO server starts...")
        self.sock.bind(self.ADDR)
        self.sock.listen(1)
        conn, addr = self.sock.accept()
        print("remote VEDIO client success connected...")
        data = "".encode("utf-8")
        payload_size = struct.calcsize("L")
        cv2.namedWindow('Remote', cv2.WINDOW_AUTOSIZE)
        while True:
            while len(data) < payload_size:
                data += conn.recv(81920)
            packed_size = data[:payload_size]
            data = data[payload_size:]
            msg_size = struct.unpack("L", packed_size)[0]
            while len(data) < msg_size:
                data += conn.recv(81920)
            zframe_data = data[:msg_size]
            data = data[msg_size:]
            frame_data = zlib.decompress(zframe_data)
            frame = pickle.loads(frame_data)
            cv2.imshow('Remote', frame)
            if cv2.waitKey(1) & 0xFF == 27:
                break 
Example #2
Source File: protocol.py    From Pyro5 with MIT License 6 votes vote down vote up
def add_payload(self, payload):
        """Parses (annotations processing) and adds payload data to a received message."""
        assert not self.data
        if len(payload) != self.data_size + self.annotations_size:
            raise errors.ProtocolError("payload length doesn't match message header")
        if self.annotations_size:
            payload = memoryview(payload)  # avoid copying
            self.annotations = {}
            i = 0
            while i < self.annotations_size:
                annotation_id = bytes(payload[i:i+4]).decode("ascii")
                length = int.from_bytes(payload[i+4:i+8], "big")
                self.annotations[annotation_id] = payload[i+8:i+8+length]     # note: it stores a memoryview!
                i += 8 + length
            assert i == self.annotations_size
            self.data = payload[self.annotations_size:]
        else:
            self.data = payload
        if self.flags & FLAGS_COMPRESSED:
            self.data = zlib.decompress(self.data)
            self.flags &= ~FLAGS_COMPRESSED
            self.data_size = len(self.data) 
Example #3
Source File: signing.py    From django-cryptography with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def loads(s,
          key=None,
          salt='django.core.signing',
          serializer=JSONSerializer,
          max_age=None):
    """
    Reverse of dumps(), raises BadSignature if signature fails.

    The serializer is expected to accept a bytestring.
    """
    # TimestampSigner.unsign always returns unicode but base64 and zlib
    # compression operate on bytes.
    base64d = force_bytes(
        TimestampSigner(key, salt=salt).unsign(s, max_age=max_age))
    decompress = False
    if base64d[:1] == b'.':
        # It's compressed; uncompress it first
        base64d = base64d[1:]
        decompress = True
    data = b64_decode(base64d)
    if decompress:
        data = zlib.decompress(data)
    return serializer().loads(data) 
Example #4
Source File: response.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def decode_content(self):
        content_encoding = self.content_encoding or 'identity'
        if content_encoding == 'identity':
            return
        if content_encoding not in ('gzip', 'deflate'):
            raise ValueError(
                "I don't know how to decode the content %s" % content_encoding)
        if content_encoding == 'gzip':
            from gzip import GzipFile
            from io import BytesIO
            gzip_f = GzipFile(filename='', mode='r', fileobj=BytesIO(self.body))
            self.body = gzip_f.read()
            self.content_encoding = None
            gzip_f.close()
        else:
            # Weird feature: http://bugs.python.org/issue5784
            self.body = zlib.decompress(self.body, -15)
            self.content_encoding = None 
Example #5
Source File: soapdenovo.py    From CAMISIM with Apache License 2.0 6 votes vote down vote up
def sortReads(inReadsFile, outReadsFile, headerToNum=lambda x: int(x.split('_', 2)[1].strip('nr'))):
    i = 0
    seqName = None
    tupleList = []
    for line in csv.getColumnAsList(inReadsFile, sep='\n'):
        if i % 2 == 0:
            seqName = line
        else:
            seq = line
            assert seqName is not None
            tupleList.append((seqName, zlib.compress(seq), headerToNum(seqName)))
            seqName = None
        i += 1
    tupleList.sort(key=lambda x: x[2])

    out = csv.OutFileBuffer(outReadsFile)
    for t in tupleList:
        out.writeText(str(t[0]) + '\n' + str(zlib.decompress(t[1])) + '\n')
    out.close() 
Example #6
Source File: signing.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def loads(s, key=None, salt='django.core.signing', serializer=JSONSerializer, max_age=None):
    """
    Reverse of dumps(), raises BadSignature if signature fails.

    The serializer is expected to accept a bytestring.
    """
    # TimestampSigner.unsign always returns unicode but base64 and zlib
    # compression operate on bytes.
    base64d = force_bytes(TimestampSigner(key, salt=salt).unsign(s, max_age=max_age))
    decompress = False
    if base64d[:1] == b'.':
        # It's compressed; uncompress it first
        base64d = base64d[1:]
        decompress = True
    data = b64_decode(base64d)
    if decompress:
        data = zlib.decompress(data)
    return serializer().loads(data) 
Example #7
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _decompressContent(response, new_content):
    content = new_content
    try:
        encoding = response.get("content-encoding", None)
        if encoding in ["gzip", "deflate"]:
            if encoding == "gzip":
                content = gzip.GzipFile(fileobj=io.BytesIO(new_content)).read()
            if encoding == "deflate":
                content = zlib.decompress(content, -zlib.MAX_WBITS)
            response["content-length"] = str(len(content))
            # Record the historical presence of the encoding in a way the won't interfere.
            response["-content-encoding"] = response["content-encoding"]
            del response["content-encoding"]
    except (IOError, zlib.error):
        content = ""
        raise FailedToDecompressContent(
            _("Content purported to be compressed with %s but failed to decompress.")
            % response.get("content-encoding"),
            response,
            content,
        )
    return content 
Example #8
Source File: __init__.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def _decompressContent(response, new_content):
    content = new_content
    try:
        encoding = response.get("content-encoding", None)
        if encoding in ["gzip", "deflate"]:
            if encoding == "gzip":
                content = gzip.GzipFile(fileobj=StringIO.StringIO(new_content)).read()
            if encoding == "deflate":
                content = zlib.decompress(content, -zlib.MAX_WBITS)
            response["content-length"] = str(len(content))
            # Record the historical presence of the encoding in a way the won't interfere.
            response["-content-encoding"] = response["content-encoding"]
            del response["content-encoding"]
    except (IOError, zlib.error):
        content = ""
        raise FailedToDecompressContent(
            _("Content purported to be compressed with %s but failed to decompress.")
            % response.get("content-encoding"),
            response,
            content,
        )
    return content 
Example #9
Source File: conftest.py    From genomeview with MIT License 6 votes vote down vote up
def reference_path():
    # download chr4 from ucsc if needed
    reference_path = "data/chr4.fa"

    if not os.path.exists(reference_path):
        import urllib.request
        import zlib
        url = "http://hgdownload.cse.ucsc.edu/goldenPath/hg19/chromosomes/chr4.fa.gz"

        resource = urllib.request.urlopen(url)

        with open(reference_path, "w") as outf:
            data = zlib.decompress(resource.read(), 16+zlib.MAX_WBITS).decode("utf-8")
            outf.write(data)

    return reference_path 
Example #10
Source File: __init__.py    From earthengine with MIT License 6 votes vote down vote up
def _decompressContent(response, new_content):
    content = new_content
    try:
        encoding = response.get('content-encoding', None)
        if encoding in ['gzip', 'deflate']:
            if encoding == 'gzip':
                content = gzip.GzipFile(fileobj=StringIO.StringIO(new_content)).read()
            if encoding == 'deflate':
                content = zlib.decompress(content)
            response['content-length'] = str(len(content))
            # Record the historical presence of the encoding in a way the won't interfere.
            response['-content-encoding'] = response['content-encoding']
            del response['content-encoding']
    except IOError:
        content = ""
        raise FailedToDecompressContent(_("Content purported to be compressed with %s but failed to decompress.") % response.get('content-encoding'), response, content)
    return content 
Example #11
Source File: zipimport.py    From maubot with GNU Affero General Public License v3.0 6 votes vote down vote up
def _get_decompress_func():
    global _importing_zlib
    if _importing_zlib:
        # Someone has a zlib.py[co] in their Zip file
        # let's avoid a stack overflow.
        _bootstrap._verbose_message('zipimport: zlib UNAVAILABLE')
        raise ZipImportError("can't decompress data; zlib not available")

    _importing_zlib = True
    try:
        from zlib import decompress
    except Exception:
        _bootstrap._verbose_message('zipimport: zlib UNAVAILABLE')
        raise ZipImportError("can't decompress data; zlib not available")
    finally:
        _importing_zlib = False

    _bootstrap._verbose_message('zipimport: zlib available')
    return decompress

# Given a path to a Zip file and a toc_entry, return the (uncompressed) data. 
Example #12
Source File: url_safe.py    From recruit with Apache License 2.0 6 votes vote down vote up
def load_payload(self, payload, *args, **kwargs):
        decompress = False
        if payload.startswith(b"."):
            payload = payload[1:]
            decompress = True
        try:
            json = base64_decode(payload)
        except Exception as e:
            raise BadPayload(
                "Could not base64 decode the payload because of an exception",
                original_error=e,
            )
        if decompress:
            try:
                json = zlib.decompress(json)
            except Exception as e:
                raise BadPayload(
                    "Could not zlib decompress the payload before decoding the payload",
                    original_error=e,
                )
        return super(URLSafeSerializerMixin, self).load_payload(json, *args, **kwargs) 
Example #13
Source File: serialize.py    From recruit with Apache License 2.0 6 votes vote down vote up
def _loads_v2(self, request, data):
        try:
            cached = json.loads(zlib.decompress(data).decode("utf8"))
        except ValueError:
            return

        # We need to decode the items that we've base64 encoded
        cached["response"]["body"] = _b64_decode_bytes(
            cached["response"]["body"]
        )
        cached["response"]["headers"] = dict(
            (_b64_decode_str(k), _b64_decode_str(v))
            for k, v in cached["response"]["headers"].items()
        )
        cached["response"]["reason"] = _b64_decode_str(
            cached["response"]["reason"],
        )
        cached["vary"] = dict(
            (_b64_decode_str(k), _b64_decode_str(v) if v is not None else v)
            for k, v in cached["vary"].items()
        )

        return self.prepare_response(request, cached) 
Example #14
Source File: ewf.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def handle_table(self, section):
        """Parse the table and store it in our lookup table."""
        table_header = self.profile.ewf_table_header_v1(
            vm=self.address_space, offset=section.obj_end)

        number_of_entries = table_header.number_of_entries

        # This is an optimization which allows us to avoid small reads for each
        # chunk. We just load the entire table into memory and read it on demand
        # from there.
        table = array.array("I")
        table.fromstring(self.address_space.read(
            table_header.entries.obj_offset,
            4 * table_header.number_of_entries))

        # We assume the last chunk is a full chunk. Feeding zlib.decompress()
        # extra data does not matter so we just read the most we can.
        table.append(table[-1] + self.chunk_size)

        self.tables.insert(
            # First chunk for this table, table header, table entry cache.
            (self._chunk_offset, table_header, table))

        # The next table starts at this chunk.
        self._chunk_offset += number_of_entries 
Example #15
Source File: itsdangerous.py    From jbox with MIT License 6 votes vote down vote up
def load_payload(self, payload):
        decompress = False
        if payload.startswith(b'.'):
            payload = payload[1:]
            decompress = True
        try:
            json = base64_decode(payload)
        except Exception as e:
            raise BadPayload('Could not base64 decode the payload because of '
                'an exception', original_error=e)
        if decompress:
            try:
                json = zlib.decompress(json)
            except Exception as e:
                raise BadPayload('Could not zlib decompress the payload before '
                    'decoding the payload', original_error=e)
        return super(URLSafeSerializerMixin, self).load_payload(json) 
Example #16
Source File: serialize.py    From jbox with MIT License 6 votes vote down vote up
def _loads_v2(self, request, data):
        try:
            cached = json.loads(zlib.decompress(data).decode("utf8"))
        except ValueError:
            return

        # We need to decode the items that we've base64 encoded
        cached["response"]["body"] = _b64_decode_bytes(
            cached["response"]["body"]
        )
        cached["response"]["headers"] = dict(
            (_b64_decode_str(k), _b64_decode_str(v))
            for k, v in cached["response"]["headers"].items()
        )
        cached["response"]["reason"] = _b64_decode_str(
            cached["response"]["reason"],
        )
        cached["vary"] = dict(
            (_b64_decode_str(k), _b64_decode_str(v) if v is not None else v)
            for k, v in cached["vary"].items()
        )

        return self.prepare_response(request, cached) 
Example #17
Source File: http.py    From ftw with Apache License 2.0 6 votes vote down vote up
def parse_content_encoding(self, response_headers, response_data):
        """
        Parses a response that contains Content-Encoding to retrieve
        response_data
        """
        if response_headers['content-encoding'] == 'gzip':
            buf = StringIO.StringIO(response_data)
            zipbuf = gzip.GzipFile(fileobj=buf)
            response_data = zipbuf.read()
        elif response_headers['content-encoding'] == 'deflate':
            data = StringIO.StringIO(zlib.decompress(response_data))
            response_data = data.read()
        else:
            raise errors.TestError(
                'Received unknown Content-Encoding',
                {
                    'content-encoding':
                        str(response_headers['content-encoding']),
                    'function': 'http.HttpResponse.parse_content_encoding'
                })
        return response_data 
Example #18
Source File: utils.py    From mars with Apache License 2.0 6 votes vote down vote up
def deserialize_graph(ser_graph, graph_cls=None):
    from google.protobuf.message import DecodeError
    from .serialize.protos.graph_pb2 import GraphDef
    from .graph import DirectedGraph
    graph_cls = graph_cls or DirectedGraph
    ser_graph_bin = to_binary(ser_graph)
    g = GraphDef()
    try:
        ser_graph = ser_graph
        g.ParseFromString(ser_graph_bin)
        return graph_cls.from_pb(g)
    except DecodeError:
        pass

    try:
        ser_graph_bin = zlib.decompress(ser_graph_bin)
        g.ParseFromString(ser_graph_bin)
        return graph_cls.from_pb(g)
    except (zlib.error, DecodeError):
        pass

    json_obj = json.loads(to_str(ser_graph))
    return graph_cls.from_json(json_obj) 
Example #19
Source File: types.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def convert(s):
        return zlib.decompress(s) 
Example #20
Source File: download.py    From InSilicoSeq with MIT License 5 votes vote down vote up
def assembly_to_fasta(url, output, chunk_size=1024):
    """download an assembly from the ncbi ftp and append
    the chromosome sequence to a fasta file.

    This function discards the plsamid sequences!

    Args:
        url (string): an url to a fasta file
        output (string): the output file name

    Returns:
        str: the file name
    """
    logger = logging.getLogger(__name__)
    if url.startswith("ftp://"):  # requests doesnt support ftp
        url = url.replace("ftp://", "https://")
    if url:
        request = requests.get(url)
        if request.status_code == 200:
            request = zlib.decompress(
                request.content, zlib.MAX_WBITS | 32).decode()
        else:
            raise BadRequestError(url, request.status_code)

    with io.StringIO(request) as fasta_io:
        seq_handle = SeqIO.parse(fasta_io, 'fasta')
        chromosome = filter_plasmids(seq_handle)

        try:
            f = open(output, 'a')
        except (IOError, OSError) as e:
            logger.error('Failed to open output file: %s' % e)
            sys.exit(1)
        else:
            logger.debug('Writing genome to %s' % output)
            with f:
                SeqIO.write(chromosome, f, 'fasta')
    return output 
Example #21
Source File: base.py    From pytfa with Apache License 2.0 5 votes vote down vote up
def load_thermoDB(path):
    """ Load a thermodynamic database

    :param string path: The path of the file to load
    :returns: The thermodynamic database
    :rtype: dict

    """
    with open(path, 'rb') as file:
        ReactionDB = pickle.loads(zlib.decompress(file.read()))

    return ReactionDB 
Example #22
Source File: test_zlib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_gzip(self):
        """decompression with gzip header"""
        do = zlib.decompressobj(zlib.MAX_WBITS | 16)
        self.assertEqual(do.decompress(self.gzip_data), self.text)
        self.assertEqual(zlib.decompress(self.gzip_data, zlib.MAX_WBITS | 16), self.text) 
Example #23
Source File: mtunnel.py    From MarkovObfuscate with MIT License 5 votes vote down vote up
def handle_read(self):
            data = self.recv(BUFFER_SIZE)
            logging.info("Recv'd {0} bytes from the other side of the tunnel".format(len(data)))
            self.read_buffer += data
            while "\n" in self.read_buffer:
                data, self.read_buffer = self.read_buffer.split("\n", 1)
                logging.info("Recv'd obfuscated {0} bytes from the other side of the tunnel".format(len(data)))
                if len(data) > 0:
                    data = zlib.decompress(self.markov.deobfuscate_string(data))
                    logging.info("Deobfuscated {0} bytes from the other side of the tunnel".format(len(data)))
                    self.client.send(data) 
Example #24
Source File: test_zlib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_header_auto_detect(self):
        """autodetect zlib and gzip header"""
        do = zlib.decompressobj(zlib.MAX_WBITS | 32)
        self.assertEqual(do.decompress(self.gzip_data), self.text)
        do = zlib.decompressobj(zlib.MAX_WBITS | 32)
        self.assertEqual(do.decompress(self.zlib_data), self.text)
        self.assertEqual(zlib.decompress(self.gzip_data, zlib.MAX_WBITS | 32), self.text)
        self.assertEqual(zlib.decompress(self.zlib_data, zlib.MAX_WBITS | 32), self.text) 
Example #25
Source File: test_zipimport.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def testAFakeZlib(self):
        #
        # This could cause a stack overflow before: importing zlib.py
        # from a compressed archive would cause zlib to be imported
        # which would find zlib.py in the archive, which would... etc.
        #
        # This test *must* be executed first: it must be the first one
        # to trigger zipimport to import zlib (zipimport caches the
        # zlib.decompress function object, after which the problem being
        # tested here wouldn't be a problem anymore...
        # (Hence the 'A' in the test method name: to make it the first
        # item in a list sorted by name, like unittest.makeSuite() does.)
        #
        # This test fails on platforms on which the zlib module is
        # statically linked, but the problem it tests for can't
        # occur in that case (builtin modules are always found first),
        # so we'll simply skip it then. Bug #765456.
        #
        if "zlib" in sys.builtin_module_names:
            return
        if "zlib" in sys.modules:
            del sys.modules["zlib"]
        files = {"zlib.py": (NOW, test_src)}
        try:
            self.doTest(".py", files, "zlib")
        except ImportError:
            if self.compression != ZIP_DEFLATED:
                self.fail("expected test to not raise ImportError")
        else:
            if self.compression != ZIP_STORED:
                self.fail("expected test to raise ImportError") 
Example #26
Source File: jwe.py    From jwcrypto with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _decrypt(self, key, ppe):

        jh = self._get_jose_header(ppe.get('header', None))

        # TODO: allow caller to specify list of headers it understands
        self._check_crit(jh.get('crit', dict()))

        for hdr in jh:
            if hdr in self.header_registry:
                if not self.header_registry.check_header(hdr, self):
                    raise InvalidJWEData('Failed header check')

        alg = self._jwa_keymgmt(jh.get('alg', None))
        enc = self._jwa_enc(jh.get('enc', None))

        aad = base64url_encode(self.objects.get('protected', ''))
        if 'aad' in self.objects:
            aad += '.' + base64url_encode(self.objects['aad'])

        cek = alg.unwrap(key, enc.wrap_key_size,
                         ppe.get('encrypted_key', b''), jh)
        data = enc.decrypt(cek, aad.encode('utf-8'),
                           self.objects['iv'],
                           self.objects['ciphertext'],
                           self.objects['tag'])

        self.decryptlog.append('Success')
        self.cek = cek

        compress = jh.get('zip', None)
        if compress == 'DEF':
            self.plaintext = zlib.decompress(data, -zlib.MAX_WBITS)
        elif compress is None:
            self.plaintext = data
        else:
            raise ValueError('Unknown compression') 
Example #27
Source File: ewf.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def handle_header(self, section):
        """Handle the header section.

        We do not currently do anything with it.
        """
        # The old header contains an ascii encoded description, compressed with
        # zlib.
        data = zlib.decompress(
            section.obj_vm.read(section.obj_end, section.size))

        # We dont do anything with this data right now. 
Example #28
Source File: ewf.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def handle_header2(self, section):
        """Handle the header2 section.

        We do not currently do anything with it.
        """
        # The header contains a utf16 encoded description, compressed with zlib.
        data = zlib.decompress(
            section.obj_vm.read(section.obj_end, section.size)).decode("utf16")

        # We dont do anything with this data right now. 
Example #29
Source File: database.py    From plasma with GNU General Public License v3.0 5 votes vote down vote up
def load(self, filename):
        gc.disable()

        self.path = filename

        if os.path.exists(self.path):
            info("open database %s" % self.path)

            fd = open(self.path, "rb")

            data = fd.read()
            if data.startswith(b"ZLIB"):
                data = zlib.decompress(data[4:])
            data = msgpack.unpackb(data, encoding="utf-8")
            fd.close()

            self.__load_meta(data)

            if self.version == LAST_COMPATIBLE:
                warning("the database version is old, some information may be missing")
            elif self.version < LAST_COMPATIBLE:
                die("the database is too old")

            self.__load_memory(data)
            self.__load_symbols(data)
            self.__load_jmptables(data)
            self.__load_comments(data)
            self.__load_functions(data)
            self.__load_history(data)
            self.__load_xrefs(data)
            self.__load_imports(data)
            self.__load_immediates(data)
            self.__load_inverted_cond(data)

            self.loaded = True

        gc.enable() 
Example #30
Source File: server.py    From pyvpn with The Unlicense 5 votes vote down vote up
def trans_data(self, pack):
        assert self._is_authed
        raw_data = zlib.decompress(pack[3:])
        self.tundev.writeSomeData(raw_data)