コード例 #1
0
ファイル: zlib.py プロジェクト: chenxingyong/eclipse-plugins
class compressobj:
    # all jython uses wbits for is deciding whether to skip the header if it's negative
    def __init__(self, level=6, method=DEFLATED, wbits=MAX_WBITS,
                       memLevel=0, strategy=0):
        if abs(wbits) > MAX_WBITS or abs(wbits) < 8:
            raise ValueError, "Invalid initialization option"
        self.deflater = Deflater(level, wbits < 0)
        self.deflater.setStrategy(strategy)
        if wbits < 0:
            _get_deflate_data(self.deflater)
        self._ended = False

    def compress(self, string):
        if self._ended:
            raise error("compressobj may not be used after flush(Z_FINISH)")
        string = _to_input(string)
        self.deflater.setInput(string, 0, len(string))
        return _get_deflate_data(self.deflater)

    def flush(self, mode=Z_FINISH):
        if self._ended:
            raise error("compressobj may not be used after flush(Z_FINISH)")
        if mode not in _valid_flush_modes:
            raise ValueError, "Invalid flush option"
        self.deflater.finish()
        last = _get_deflate_data(self.deflater)
        if mode == Z_FINISH:
            self.deflater.end()
            self._ended = True
        return last
コード例 #2
0
ファイル: zlib.py プロジェクト: jythontools/jython
class compressobj(object):
    # All jython uses wbits for is in deciding whether to skip the
    # header if it's negative or to set gzip. But we still raise
    # ValueError to get full test compliance.

    GZIP_HEADER = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x04\x03"

    # NB: this format is little-endian, not big-endian as we might
    # expect for network oriented protocols. Both are 4 bytes unsigned
    # modulus 2^32 per RFC-1952. CRC32.getValue() returns an unsigned
    # int as a long, so cope accordingly. 
    GZIP_TRAILER_FORMAT = struct.Struct("<II")  # crc32, size


    def __init__(self, level=6, method=DEFLATED, wbits=MAX_WBITS,
                       memLevel=0, strategy=0):
        if abs(wbits) & 16:
            if wbits > 0:
                wbits -= 16
            else:
                wbits += 16
            self._gzip = True
        else:
            self._gzip = False
        if abs(wbits) > MAX_WBITS or abs(wbits) < 8:
            raise ValueError, "Invalid initialization option: %s" % (wbits,)
        self.deflater = Deflater(level, wbits < 0 or self._gzip)
        self.deflater.setStrategy(strategy)
        self._ended = False
        self._size = 0
        self._crc32 = CRC32()

    def compress(self, string):
        if self._ended:
            raise error("compressobj may not be used after flush(Z_FINISH)")
        string = _to_input(string)
        self.deflater.setInput(string, 0, len(string))
        deflated = _get_deflate_data(self.deflater)
        self._size += len(string)
        self._crc32.update(string)
        if self._gzip:
            return self.GZIP_HEADER + deflated
        else:
            return deflated

    def flush(self, mode=Z_FINISH):
        if self._ended:
            raise error("compressobj may not be used after flush(Z_FINISH)")
        if mode not in _valid_flush_modes:
            raise ValueError, "Invalid flush option"
        if mode == Z_FINISH:
            self.deflater.finish()
        last = _get_deflate_data(self.deflater, mode)
        if mode == Z_FINISH:
            if self._gzip:
                last += self.GZIP_TRAILER_FORMAT.pack(
                    self._crc32.getValue(), self._size & _MASK32)
            self.deflater.end()
            self._ended = True
        return last
コード例 #3
0
ファイル: zlib.py プロジェクト: loy2000/Ghidra
class compressobj(object):
    # All jython uses wbits for is in deciding whether to skip the
    # header if it's negative or to set gzip. But we still raise
    # ValueError to get full test compliance.

    GZIP_HEADER = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x04\x03"

    # NB: this format is little-endian, not big-endian as we might
    # expect for network oriented protocols, as specified by RFCs;
    # CRC32.getValue() returns an unsigned int as a long, so cope
    # accordingly
    GZIP_TRAILER_FORMAT = struct.Struct("<Ii")  # crc32, size

    def __init__(self, level=6, method=DEFLATED, wbits=MAX_WBITS,
                       memLevel=0, strategy=0):
        if abs(wbits) & 16:
            if wbits > 0:
                wbits -= 16
            else:
                wbits += 16
            self._gzip = True
        else:
            self._gzip = False
        if abs(wbits) > MAX_WBITS or abs(wbits) < 8:
            raise ValueError, "Invalid initialization option: %s" % (wbits,)
        self.deflater = Deflater(level, wbits < 0 or self._gzip)
        self.deflater.setStrategy(strategy)
        self._ended = False
        self._size = 0
        self._crc32 = CRC32()

    def compress(self, string):
        if self._ended:
            raise error("compressobj may not be used after flush(Z_FINISH)")
        string = _to_input(string)
        self.deflater.setInput(string, 0, len(string))
        deflated = _get_deflate_data(self.deflater)
        self._size += len(string)
        self._crc32.update(string)
        if self._gzip:
            return self.GZIP_HEADER + deflated
        else:
            return deflated

    def flush(self, mode=Z_FINISH):
        if self._ended:
            raise error("compressobj may not be used after flush(Z_FINISH)")
        if mode not in _valid_flush_modes:
            raise ValueError, "Invalid flush option"
        if mode == Z_FINISH:
            self.deflater.finish()
        last = _get_deflate_data(self.deflater, mode)
        if mode == Z_FINISH:
            if self._gzip:
                last += self.GZIP_TRAILER_FORMAT.pack(
                    self._crc32.getValue(), self._size % sys.maxint)
            self.deflater.end()
            self._ended = True
        return last
コード例 #4
0
ファイル: zlib.py プロジェクト: chenxingyong/eclipse-plugins
def compress(string, level=6):
    if level < Z_BEST_SPEED or level > Z_BEST_COMPRESSION:
        raise error, "Bad compression level"
    deflater = Deflater(level, 0)
    try:
        string = _to_input(string)
        deflater.setInput(string, 0, len(string))
        deflater.finish()
        return _get_deflate_data(deflater)
    finally:
        deflater.end()