Example #1
0
 /**
  * Compress the given value to the specific compression
  *
  * @param String $sValue
  * @param String $iLevel (Optionnal) : Between 0 and 9
  *
  * @return String
  *
  * @throws Exception
  */
 public function compress($sValue, $iLevel = null)
 {
     if (!is_string($sValue)) {
         throw new Exception('Invalid first argument, must be a string');
     }
     return lzf_compress($sValue);
 }
 public function setUp()
 {
     /** @var SocketInterface $socket */
     $socket = $this->getMock('JLaso\\TradukojConnector\\Socket\\SocketInterface');
     $socket->method('write')->will($this->returnCallback(function ($msg, $len) {
         return $len;
     }));
     $socket->method('read')->will($this->returnCallback(function ($len, $type) {
         switch ($len) {
             case 10:
                 return ClientSocketApi::ACK . PHP_EOL;
             case 2048:
                 return "Welcome!";
             default:
                 $val = '{"result":false,"reason":"test"}' . PHP_EOL;
                 $val = function_exists('lzf_compress') ? lzf_compress($val) : gzcompress($val);
                 return sprintf("%06d:001:001:%s", strlen($val), $val);
         }
     }));
     $loader = new ArrayLoader();
     $config = $loader->load($this->getConfigArray());
     $postClient = $this->getMock('JLaso\\TradukojConnector\\PostClient\\PostClientInterface');
     $postClient->method('call')->will($this->returnCallback(function ($url, $data) {
         return ClientSocketApiTest::postClientCallMock($url, $data);
     }));
     $nullOutput = new NullOutput();
     $this->clientSocketApi = new ClientSocketApi($config, $socket, $postClient, $nullOutput);
 }
Example #3
0
 public function compress($data = '', $blockSize = NULL, $workFactor = NULL)
 {
     if (!is_scalar($data)) {
         return Error::set('Error', 'valueParameter', '1.(data)');
     }
     return lzf_compress($data);
 }
 public function compress($data = '')
 {
     if (!isValue($data)) {
         return Error::set(lang('Error', 'valueParameter', '1.(data)'));
     }
     return lzf_compress($data);
 }
 protected function sendMessage($msg, $compress = true)
 {
     if ($compress) {
         if (function_exists(self::LZF_FUNCTION)) {
             $msg = lzf_compress($msg);
         } else {
             $msg = gzcompress($msg, 9);
         }
     } else {
         $msg .= PHP_EOL;
     }
     $len = strlen($msg);
     if (self::DEBUG) {
         print "sending {$len} chars" . PHP_EOL;
     }
     $blocks = ceil($len / self::BLOCK_SIZE);
     for ($i = 0; $i < $blocks; $i++) {
         $block = substr($msg, $i * self::BLOCK_SIZE, $i == $blocks - 1 ? $len - ($i - 1) * self::BLOCK_SIZE : self::BLOCK_SIZE);
         $prefix = sprintf("%06d:%03d:%03d:", strlen($block), $i + 1, $blocks);
         $aux = $prefix . $block;
         if (self::DEBUG) {
             print sprintf("sending block %d from %d, prefix = %s\n", $i + 1, $blocks, $prefix);
         }
         if (false === socket_write($this->socket, $aux, strlen($aux))) {
             die('error');
         }
         do {
             $read = socket_read($this->socket, 10, PHP_NORMAL_READ);
             //print $read;
         } while (strpos($read, self::ACK) !== 0);
     }
     return true;
 }
Example #6
0
 /**
  * Compresses the given content
  *
  * @param  string $content
  * @return string
  */
 public function compress($content)
 {
     $compressed = lzf_compress($content);
     if (!$compressed) {
         throw new Zend_Filter_Exception('Error during compression');
     }
     return $compressed;
 }
Example #7
0
 /**
  * Compresses the given content
  *
  * @param  string $content
  * @return string
  * @throws Exception\RuntimeException if error occurs during compression
  */
 public function compress($content)
 {
     $compressed = lzf_compress($content);
     if (!$compressed) {
         throw new Exception\RuntimeException('Error during compression');
     }
     return $compressed;
 }
Example #8
0
 /**
  * Compresses the given content
  *
  * @param  string $content
  * @return string
  */
 public function compress($content)
 {
     $compressed = lzf_compress($content);
     if (!$compressed) {
         require_once 'Zend/Filter/Exception.php';
         throw new Zend_Filter_Exception('Error during compression');
     }
     return $compressed;
 }
Example #9
0
 /**
  * Compresses the given content
  *
  * @param  string $content
  * @return string
  */
 public function compress($content)
 {
     $compressed = lzf_compress($content);
     if (!$compressed) {
         require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Filter/Exception.php';
         throw new IfwPsn_Vendor_Zend_Filter_Exception('Error during compression');
     }
     return $compressed;
 }
Example #10
0
 public function testLzfDriverDecompress()
 {
     try {
         $ob = new Horde_Compress_Fast(array('drivers' => array('Horde_Compress_Fast_Lzf')));
     } catch (Horde_Compress_Fast_Exception $e) {
         $this->markTestSkipped('LZF extension not available.');
     }
     $this->assertEquals($this->compress_text, $ob->decompress(lzf_compress($this->compress_text)));
     try {
         $ob->decompress(new stdClass());
         $this->fail('Expected exception.');
     } catch (Horde_Compress_Fast_Exception $e) {
     }
 }
Example #11
0
 /**
  * Set cached data.
  *
  * @param string $key  Cache key.
  * @param mixed $data  Data to store.
  *
  * @return boolean  True on success, false on failure.
  */
 public function set($key, $data)
 {
     $data = $this->_mask & self::MSGPACK ? msgpack_pack($data) : json_encode($data);
     if ($this->_mask & self::LZ4) {
         $data = @horde_lz4_compress($data);
     } elseif ($this->_mask & self::LZF) {
         $data = lzf_compress($data);
     }
     if ($this->_mask & self::APC) {
         return apc_store($key, $data);
     } elseif ($this->_mask & self::XCACHE) {
         return xcache_set($key, $data);
     } elseif ($this->_mask & self::EACCELERATOR) {
         eaccelerator_put($key, $data);
         return true;
     } elseif ($this->_mask & self::TEMPFILE) {
         return file_put_contents($this->_tempdir . '/' . $key, $data);
     }
     return false;
 }
 /**
  * @param string $data
  * @param int $level
  * @throws Exception
  * @return string
  */
 protected function _encodeData($data, $level)
 {
     if ($level && strlen($data) >= $this->_compressThreshold) {
         switch ($this->_compressionLib) {
             case 'snappy':
                 $data = snappy_compress($data);
                 break;
             case 'lzf':
                 $data = lzf_compress($data);
                 break;
             case 'gzip':
                 $data = gzcompress($data, $level);
                 break;
         }
         if (!$data) {
             throw new Exception("Could not compress cache data.");
         }
         return $this->_compressPrefix . $data;
     }
     return $data;
 }
Example #13
0
 /**
  * Serialize data.
  *
  * @param mixed $data    The data to be serialized.
  * @param mixed $mode    The mode of serialization. Can be
  *                       either a single mode or array of modes.
  *                       If array, will be serialized in the
  *                       order provided.
  * @param mixed $params  Any additional parameters the serialization method
  *                       requires.
  *
  * @return string  A serialized string.
  * @throws Horde_Serialize_Exception
  */
 protected static function _serialize($data, $mode, $params = null)
 {
     switch ($mode) {
         case self::NONE:
             break;
             // $params['level'] = Level of compression (default: 3)
             // $params['workfactor'] = How does compression phase behave when given
             //                         worst case, highly repetitive, input data
             //                         (default: 30)
         // $params['level'] = Level of compression (default: 3)
         // $params['workfactor'] = How does compression phase behave when given
         //                         worst case, highly repetitive, input data
         //                         (default: 30)
         case self::BZIP:
             $data = bzcompress($data, isset($params['level']) ? $params['level'] : 3, isset($params['workfactor']) ? $params['workfactor'] : 30);
             if (is_integer($data)) {
                 $data = false;
             }
             break;
         case self::WDDX:
             $data = wddx_serialize_value($data);
             break;
         case self::IMAP8:
             $data = Horde_Mime::quotedPrintableEncode($data);
             break;
         case self::IMAPUTF7:
             $data = Horde_Imap_Client_Utf7imap::Utf8ToUtf7Imap(Horde_String::convertCharset($data, 'ISO-8859-1', 'UTF-8'));
             break;
         case self::IMAPUTF8:
             $data = Horde_Mime::decode($data);
             break;
             // $params['level'] = Level of compression (default: 3)
         // $params['level'] = Level of compression (default: 3)
         case self::GZ_DEFLATE:
             $data = gzdeflate($data, isset($params['level']) ? $params['level'] : 3);
             break;
         case self::BASIC:
             $data = serialize($data);
             break;
             // $params['level'] = Level of compression (default: 3)
         // $params['level'] = Level of compression (default: 3)
         case self::GZ_COMPRESS:
             $data = gzcompress($data, isset($params['level']) ? $params['level'] : 3);
             break;
         case self::BASE64:
             $data = base64_encode($data);
             break;
             // $params['level'] = Level of compression (default: 3)
         // $params['level'] = Level of compression (default: 3)
         case self::GZ_ENCODE:
             $data = gzencode($data, isset($params['level']) ? $params['level'] : 3);
             break;
         case self::RAW:
             $data = rawurlencode($data);
             break;
         case self::URL:
             $data = urlencode($data);
             break;
             // $params = Source character set
         // $params = Source character set
         case self::UTF7:
             $data = Horde_String::convertCharset($data, $params, 'UTF-7');
             break;
             // $params = Source character set
         // $params = Source character set
         case self::UTF7_BASIC:
             $data = self::serialize($data, array(self::UTF7, self::BASIC), $params);
             break;
         case self::JSON:
             $tmp = json_encode($data);
             /* Basic error handling attempts.
              * TODO: JSON_ERROR_UTF8 = 5; available as of PHP 5.3.3 */
             if (json_last_error() === 5) {
                 $data = json_encode(Horde_String::convertCharset($data, $params, 'UTF-8', true));
             } else {
                 $data = $tmp;
             }
             break;
         case self::LZF:
             $data = lzf_compress($data);
             break;
     }
     if ($data === false) {
         throw new Horde_Serialize_Exception('Serialization failed.');
     }
     return $data;
 }
 /**
  * @param string $message
  *
  * @return string
  */
 protected function compress($message)
 {
     return function_exists('lzf_compress') ? lzf_compress($message) : gzcompress($message, 9);
 }
Example #15
0
 /**
  * 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;
 }
 /**
  * Public for testing purposes only.
  *
  * @param string $data
  * @return string
  */
 public function _encodeData($data)
 {
     $originalDataSize = strlen($data);
     if ($this->_compressionThreshold > 0 && $this->_compressionLib != 'none' && $originalDataSize >= $this->_compressionThreshold) {
         if ($this->_logLevel >= 7) {
             Mage::log(sprintf("%s: Compressing %s bytes with %s", $this->_getPid(), $originalDataSize, $this->_compressionLib), Zend_Log::DEBUG, self::LOG_FILE);
             // reset timer
             $this->_timeStart = microtime(true);
         }
         switch ($this->_compressionLib) {
             case 'snappy':
                 $data = snappy_compress($data);
                 break;
             case 'lzf':
                 $data = lzf_compress($data);
                 break;
             case 'gzip':
                 $data = gzcompress($data, 1);
                 break;
         }
         if ($data) {
             $data = ':' . substr($this->_compressionLib, 0, 2) . ':' . $data;
             if ($this->_logLevel >= 7) {
                 Mage::log(sprintf("%s: Data compressed by %.1f percent in %.5f seconds", $this->_getPid(), $originalDataSize == 0 ? 0 : 100 - strlen($data) / $originalDataSize * 100, microtime(true) - $this->_timeStart), Zend_Log::DEBUG, self::LOG_FILE);
             }
         } else {
             if ($this->_logLevel >= 4) {
                 Mage::log(sprintf("%s: Could not compress session data using %s", $this->_getPid(), $this->_compressionLib), Zend_Log::WARN, self::LOG_FILE);
             }
         }
     }
     return $data;
 }
 /**
  * LZF compression.
  *
  * @param string $data
  * @return string
  */
 protected function lzf($data)
 {
     return lzf_compress($data);
 }
Example #18
0
 public function encode($data)
 {
     return lzf_compress($data);
 }
Example #19
0
 /**
  * Returns decoded image data
  *
  * @param string $data         The image data
  * @param string $encoding     The encoding type for the image data.
  *                             (none, base64, or binhex)
  * @param string $compression  The compression type for the image data.
  *                             (none, gzip, or lzf)
  * @param boolean $upload      Process direction (true of encode/compress or false if decode/decompress)
  *
  * @return string  The decoded/encoded image data
  */
 protected function _getImageData($data, $encoding = 'none', $compression = 'none', $upload = true)
 {
     switch ($encoding) {
         case 'base64':
             $data = $upload ? base64_decode($data) : base64_encode($data);
             break;
         case 'binhex':
             $data = $upload ? pack('H*', $data) : unpack('H*', $data);
     }
     switch ($compression) {
         case 'gzip':
             if (Horde_Util::loadExtension('zlib')) {
                 return $upload ? gzuncompress($data) : gzcompress($data);
             }
             break;
         case 'lzf':
             if (Horde_Util::loadExtension('lzf')) {
                 return $upload ? lzf_decompress($data) : lzf_compress($data);
             }
             break;
         default:
             return $data;
     }
 }
Example #20
0
 protected function sendMessage($msg, $compress = true)
 {
     if ($compress) {
         if ($this->lzfUse) {
             $msg = lzf_compress($msg);
         } else {
             $msg = gzcompress($msg, 9);
         }
     } else {
         $msg .= PHP_EOL;
     }
     $len = strlen($msg);
     if ($this->debug) {
         print "sending {$len} chars" . PHP_EOL;
     }
     $blocks = ceil($len / self::BLOCK_SIZE);
     for ($i = 0; $i < $blocks; $i++) {
         $block = substr($msg, $i * self::BLOCK_SIZE, $i == $blocks - 1 ? $len - ($i - 1) * self::BLOCK_SIZE : self::BLOCK_SIZE);
         $prefix = sprintf("%06d:%03d:%03d:", strlen($block), $i + 1, $blocks);
         $aux = $prefix . $block;
         if ($this->debug) {
             print sprintf("sending block %d from %d, prefix = %s\n", $i + 1, $blocks, $prefix);
         }
         if (false === socket_write($this->msgsock, $aux, strlen($aux))) {
             die('error');
         }
         do {
             $read = socket_read($this->msgsock, 10, PHP_NORMAL_READ);
             //print $read;
         } while (strpos($read, self::ACK) !== 0);
     }
     $this->sended += strlen($msg);
     $size = $this->prettySize($this->sended);
     echo '^ ', $size, "    ";
     $size = $this->prettySize($this->sended + $this->received);
     echo ', Total:', $size;
     echo str_repeat(chr(8), 80);
     return true;
 }
Example #21
0
 public function do($data)
 {
     return lzf_compress($data);
 }
Example #22
0
 public function Compress($data)
 {
     $this->Check();
     return lzf_compress($data);
 }
Example #23
0
File: Lzf.php Project: horde/horde
 /**
  */
 public function compress($text)
 {
     return strlen($text) ? lzf_compress($text) : '';
 }
Example #24
0
 /**
  * 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;
 }
Example #25
0
 protected function compress()
 {
     switch ($this->compress) {
         case self::COMPRESS_GZIP:
             $this->payload = gzdeflate($this->payload);
             break;
         case self::COMPRESS_BZIP:
             $this->payload = bzcompress($this->payload);
             break;
         case self::COMPRESS_LZF:
             $this->payload = lzf_compress($this->payload);
             break;
         case self::COMPRESS_OFF:
         default:
             //anything but known values result in OFF
             break;
     }
     return true;
 }
Example #26
0
 protected function send($buffer)
 {
     $debug = substr($buffer, 0, 60);
     $buffer = lzf_compress($buffer);
     $this->sended += strlen($buffer);
     $size = $this->prettySize($this->sended);
     echo '^ ', $size, "    ";
     $size = $this->prettySize($this->sended + $this->received);
     echo ', Total:', $size, ' ', $debug;
     echo str_repeat(chr(8), 80);
     return socket_write($this->msgsock, $buffer, strlen($buffer));
 }
Example #27
0
 /**
  * Static method to compress data
  *
  * @param  string $data
  * @return mixed
  */
 public static function compress($data)
 {
     return lzf_compress($data);
 }
Example #28
0
 /**
  * write()
  * バックアップファイルに書き込む
  * TODO:gzとbz2圧縮がスレッドセーフでない
  * @access    private
  * @param     String    $content 文字列
  *
  * @return    Boolean   FALSE:失敗 その他:書き込んだバイト数
  */
 public function set($data = '', $keeptimestamp = false)
 {
     // 古いバックアップを削除(追記する実装でないため)
     $this->remove();
     $file = $this->name . $this->ext;
     // 書き込み
     touch($file);
     $bytes = '';
     switch ($this->ext) {
         case '.txt':
             $bytes = parent::set($data);
             break;
         case '.gz':
             $handle = gzopen($file, 'w');
             if ($handle) {
                 $bytes = gzwrite($handle, $data);
                 gzclose($handle);
             } else {
                 return false;
             }
             break;
         case '.bz2':
             $handle = bzopen($file, 'w');
             if ($handle) {
                 $bytes = bzwrite($handle, $data);
                 bzclose($handle);
             } else {
                 return false;
             }
             break;
         case '.lzf':
             $bytes = parent::set(lzf_compress($data), false);
             break;
     }
     return $bytes;
 }
Example #29
0
 /**
  * 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;
 }