/** * Public for testing purposes only. * * @param string $data * @return string */ public function _encodeData($data) { $this->profilerStart(__METHOD__); $originalDataSize = strlen($data); if ($this->_compressionThreshold > 0 && $this->_compressionLib != 'none' && $originalDataSize >= $this->_compressionThreshold) { if ($this->_logLevel >= \Zend_Log::DEBUG) { $this->_log(sprintf("Compressing %s bytes with %s", $originalDataSize, $this->_compressionLib)); $timeStart = microtime(true); } $prefix = ':' . substr($this->_compressionLib, 0, 2) . ':'; switch ($this->_compressionLib) { case 'snappy': $data = snappy_compress($data); break; case 'lzf': $data = lzf_compress($data); break; case 'lz4': $data = lz4_compress($data); $prefix = ':l4:'; break; case 'gzip': $data = gzcompress($data, 1); break; } if ($data) { $data = $prefix . $data; if ($this->_logLevel >= \Zend_Log::DEBUG) { $this->_log(sprintf("Data compressed by %.1f percent in %.5f seconds", $originalDataSize == 0 ? 0 : 100 - strlen($data) / $originalDataSize * 100, microtime(true) - $timeStart)); } } else { if ($this->_logLevel >= \Zend_Log::WARN) { $this->_log(sprintf("Could not compress session data using %s", $this->_compressionLib), \Zend_Log::WARN); } } } $this->profilerStop(__METHOD__); return $data; }
/** * Encode data * * @param string $data * @return string */ protected function _encodeData($data) { $originalDataSize = strlen($data); if ($this->_compressionThreshold > 0 && $this->_compressionLibrary != 'none' && $originalDataSize >= $this->_compressionThreshold) { $this->_log(sprintf("Compressing %s bytes with %s", $originalDataSize, $this->_compressionLibrary)); $timeStart = microtime(true); $prefix = ':' . substr($this->_compressionLibrary, 0, 2) . ':'; switch ($this->_compressionLibrary) { case 'snappy': $data = snappy_compress($data); break; case 'lzf': $data = lzf_compress($data); break; case 'lz4': $data = lz4_compress($data); $prefix = ':l4:'; break; case 'gzip': $data = gzcompress($data, 1); break; } if ($data) { $data = $prefix . $data; $this->_log(sprintf("Data compressed by %.1f percent in %.5f seconds", $originalDataSize == 0 ? 0 : 100 - strlen($data) / $originalDataSize * 100, microtime(true) - $timeStart)); } else { $this->_log(sprintf("Could not compress session data using %s", $this->_compressionLibrary), LoggerInterface::WARNING); } } return $data; }
/** * Compress data * * @param string $data * @return string * @throws Exception */ protected function compress($data) { switch ($this->compression) { case self::COMPRESSION_LZ4: $compressedData = @lz4_compress($data); if (false === $compressedData) { throw new Exception('Error compressing with lz4_compress.'); } break; case self::COMPRESSION_SNAPPY: $compressedData = @snappy_compress($data); if (false === $compressedData) { throw new Exception('Error compressing with snappy_compress.'); } break; case self::COMPRESSION_LZF: $compressedData = @lzf_compress($data); if (false === $compressedData) { throw new Exception('Error compressing with lzf_compress.'); } break; case self::COMPRESSION_GZ: $compressedData = @gzcompress($data); if (false === $compressedData) { throw new Exception('Error compressing with gzcompress.'); } break; case self::COMPRESSION_NONE: default: $compressedData = $data; } return $compressedData; }
VERIFY(strlen($t) < strlen($s)); $u = nzuncompress($t); VS($u, $s); $compressable = str_repeat('\\0', 1024); $bs = $compressable; $bt = nzcompress($bs); VERIFY(strlen($bt) < strlen($bs)); $bu = nzuncompress($bt); VS($bu, $bs); VS(count($bu), count($bs)); ////////////////////////////////////////////////////////////////////// $s = "garbage stuff"; $v = nzuncompress($s); VERIFY($v == false); $empty = ""; $c = nzcompress($empty); $d = nzuncompress($c); VERIFY($d == $empty); VS(lz4_uncompress(lz4_compress("testing lz4_compress")), "testing lz4_compress"); VS(lz4_uncompress(lz4_hccompress("testing lz4_hccompress")), "testing lz4_hccompress"); // first test uncompressing invalid string $s = "invalid compressed string"; $v = lz4_uncompress($s); VERIFY($v == false); // try uncompressing empty string $empty = ""; $v = lz4_uncompress($empty); VERIFY($v == false); $c = lz4_compress($empty); $d = lz4_uncompress($c); VERIFY($d == $empty);
/** * Create memory stream from input file content * @return void */ private function createMemoryStream() { if ($this->inputFileData !== null) { // Create memory stream $this->memoryStream = null; $this->memoryStreamSize = 0; try { $this->memoryStream = fopen("php://memory", 'r+'); $this->memoryStreamSize = fputs($this->memoryStream, $this->method == self::COMPRESS ? lz4_compress($this->inputFileData, $this->compressionLevel) : lz4_uncompress($this->inputFileData)); rewind($this->memoryStream); if ($this->method == self::DECOMPRESS && !$this->AllowFileOutput && ($contentType = $this->getStreamContentType()) !== self::TEXT_TYPE) { fclose($this->memoryStream); $this->memoryStream = null; $this->errors[] = sprintf('Bad decoded content type detected, expected "%s" found "%s"', self::TEXT_TYPE, $contentType); } } catch (Exception $e) { $this->errors[] = $e->getMessage(); } // Free memory unset($this->inputFileData); $this->inputFileData = null; } }