Removed PHP and JS versions of LZ Compressor

Becoming too problematic to use and for such little benefit so dropping
This commit is contained in:
Matt Pass
2016-04-02 11:42:36 +01:00
parent a66661b194
commit de5460b43f
7 changed files with 0 additions and 652 deletions

View File

@@ -1,91 +0,0 @@
<?php
namespace LZCompressor;
class LZContext
{
/**
* @var array
*/
public $dictionary = array();
/**
* @var array
*/
public $dictionaryToCreate = array();
/**
* @var string
*/
public $c = '';
/**
* @var string
*/
public $wc = '';
/**
* @var string
*/
public $w = '';
/**
* @var int
*/
public $enlargeIn = 2;
/**
* @var int
*/
public $dictSize = 3;
/**
* @var int
*/
public $numBits = 2;
/**
* @var LZData
*/
public $data;
function __construct()
{
$this->data = new LZData;
}
// Helper
/**
* @param string $val
* @return bool
*/
public function dictionaryContains($val) {
return array_key_exists($val, $this->dictionary);
}
/**
* @param $val
*/
public function addToDictionary($val) {
$this->dictionary[$val] = $this->dictSize++;
}
/**
* @param string $val
* @return bool
*/
public function dictionaryToCreateContains($val) {
return array_key_exists($val, $this->dictionaryToCreate);
}
/**
* decrements enlargeIn and extends numbits in case enlargeIn drops to 0
*/
public function enlargeIn() {
$this->enlargeIn--;
if($this->enlargeIn==0) {
$this->enlargeIn = pow(2, $this->numBits);
$this->numBits++;
}
}
}

View File

@@ -1,29 +0,0 @@
<?php
namespace LZCompressor;
class LZData
{
/**
* @var
*/
public $str = '';
/**
* @var
*/
public $val;
/**
* @var int
*/
public $position = 0;
/**
* @var int
*/
public $index = 1;
public function append($str) {
$this->str .= $str;
}
}

View File

@@ -1,33 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: sics
* Date: 28.02.2016
* Time: 12:53
*/
namespace LZCompressor;
class LZReverseDictionary
{
public $entries = array(0, 1 ,2);
public function size() {
return count($this->entries);
}
public function hasEntry($index) {
return array_key_exists($index, $this->entries);
}
public function getEntry($index) {
return $this->entries[$index];
}
public function addEntry($char) {
$this->entries[] = $char;
}
}

View File

@@ -1,298 +0,0 @@
<?php
namespace LZCompressor;
class LZString
{
public static function compressToBase64($input)
{
$res = self::_compress($input, 6, function($a) {
return LZUtil::$keyStrBase64{$a};
});
switch (strlen($res) % 4) { // To produce valid Base64
default: // When could this happen ?
case 0 : return $res;
case 1 : return $res ."===";
case 2 : return $res ."==";
case 3 : return $res ."=";
}
}
public static function decompressFromBase64($input)
{
return self::_decompress($input, 32, function($feed, $index) {
return LZUtil::getBaseValue(LZUtil::$keyStrBase64, LZUtil::utf8_charAt($feed, $index));
});
}
public static function compressToUTF16($input) {
return self::_compress($input, 15, function($a) {
return LZUtil16::fromCharCode($a+32);
}) . LZUtil16::utf16_chr(32);
}
public static function decompressFromUTF16($input) {
return self::_decompress($input, 16384, function($feed, $index) {
return LZUtil16::charCodeAt($feed, $index)-32;
});
}
/**
* @param string $uncompressed
* @return string
*/
public static function compress($uncompressed)
{
return self::_compress($uncompressed, 16, function($a) {
return LZUtil::fromCharCode($a);
});
}
/**
* @param string $compressed
* @return string
*/
public static function decompress($compressed)
{
return self::_decompress($compressed, 32768, function($feed, $index) {
return LZUtil::charCodeAt($feed, $index);
});
}
/**
* @param string $uncompressed
* @param integer $bitsPerChar
* @param callable $getCharFromInt
* @return string
*/
private static function _compress($uncompressed, $bitsPerChar, $getCharFromInt) {
if(!is_string($uncompressed) || strlen($uncompressed) === 0) {
return '';
}
$context = new LZContext();
$length = LZUtil::utf8_strlen($uncompressed);
for($ii=0; $ii<$length; $ii++) {
$context->c = LZUtil::utf8_charAt($uncompressed, $ii);
if(!$context->dictionaryContains($context->c)) {
$context->addToDictionary($context->c);
$context->dictionaryToCreate[$context->c] = true;
}
$context->wc = $context->w . $context->c;
if($context->dictionaryContains($context->wc)) {
$context->w = $context->wc;
} else {
self::produceW($context, $bitsPerChar, $getCharFromInt);
}
}
if($context->w !== '') {
self::produceW($context, $bitsPerChar, $getCharFromInt);
}
$value = 2;
for($i=0; $i<$context->numBits; $i++) {
self::writeBit($value&1, $context->data, $bitsPerChar, $getCharFromInt);
$value = $value >> 1;
}
while (true) {
$context->data->val = $context->data->val << 1;
if ($context->data->position == ($bitsPerChar-1)) {
$context->data->append($getCharFromInt($context->data->val));
break;
}
$context->data->position++;
}
return $context->data->str;
}
/**
* @param LZContext $context
* @param integer $bitsPerChar
* @param callable $getCharFromInt
*
* @return LZContext
*/
private static function produceW(LZContext $context, $bitsPerChar, $getCharFromInt)
{
if($context->dictionaryToCreateContains($context->w)) {
if(LZUtil::charCodeAt($context->w)<256) {
for ($i=0; $i<$context->numBits; $i++) {
self::writeBit(null, $context->data, $bitsPerChar, $getCharFromInt);
}
$value = LZUtil::charCodeAt($context->w);
for ($i=0; $i<8; $i++) {
self::writeBit($value&1, $context->data, $bitsPerChar, $getCharFromInt);
$value = $value >> 1;
}
} else {
$value = 1;
for ($i=0; $i<$context->numBits; $i++) {
self::writeBit($value, $context->data, $bitsPerChar, $getCharFromInt);
$value = 0;
}
$value = LZUtil::charCodeAt($context->w);
for ($i=0; $i<16; $i++) {
self::writeBit($value&1, $context->data, $bitsPerChar, $getCharFromInt);
$value = $value >> 1;
}
}
$context->enlargeIn();
unset($context->dictionaryToCreate[$context->w]);
} else {
$value = $context->dictionary[$context->w];
for ($i=0; $i<$context->numBits; $i++) {
self::writeBit($value&1, $context->data, $bitsPerChar, $getCharFromInt);
$value = $value >> 1;
}
}
$context->enlargeIn();
$context->addToDictionary($context->wc);
$context->w = $context->c.'';
}
/**
* @param string $value
* @param LZData $data
* @param integer $bitsPerChar
* @param callable $getCharFromInt
*/
private static function writeBit($value, LZData $data, $bitsPerChar, $getCharFromInt)
{
if(null !== $value) {
$data->val = ($data->val << 1) | $value;
} else {
$data->val = ($data->val << 1);
}
if ($data->position == ($bitsPerChar-1)) {
$data->position = 0;
$data->append($getCharFromInt($data->val));
$data->val = 0;
} else {
$data->position++;
}
}
/**
* @param LZData $data
* @param integer $resetValue
* @param callable $getNextValue
* @param integer $exponent
* @param string $feed
* @return integer
*/
private static function readBits(LZData $data, $resetValue, $getNextValue, $feed, $exponent)
{
$bits = 0;
$maxPower = pow(2, $exponent);
$power=1;
while($power != $maxPower) {
$resb = $data->val & $data->position;
$data->position >>= 1;
if ($data->position == 0) {
$data->position = $resetValue;
$data->val = $getNextValue($feed, $data->index++);
}
$bits |= (($resb>0 ? 1 : 0) * $power);
$power <<= 1;
}
return $bits;
}
/**
* @param string $compressed
* @param integer $resetValue
* @param callable $getNextValue
* @return string
*/
private static function _decompress($compressed, $resetValue, $getNextValue)
{
if(!is_string($compressed) || strlen($compressed) === 0) {
return '';
}
$length = LZUtil::utf8_strlen($compressed);
$entry = null;
$enlargeIn = 4;
$numBits = 3;
$result = '';
$dictionary = new LZReverseDictionary();
$data = new LZData();
$data->str = $compressed;
$data->val = $getNextValue($compressed, 0);
$data->position = $resetValue;
$data->index = 1;
$next = self::readBits($data, $resetValue, $getNextValue, $compressed, 2);
if($next < 0 || $next > 1) {
return '';
}
$exponent = ($next == 0) ? 8 : 16;
$bits = self::readBits($data, $resetValue, $getNextValue, $compressed, $exponent);
$c = LZUtil::fromCharCode($bits);
$dictionary->addEntry($c);
$w = $c;
$result .= $c;
while(true) {
if($data->index > $length) {
return '';
}
$bits = self::readBits($data, $resetValue, $getNextValue, $compressed, $numBits);
$c = $bits;
switch($c) {
case 0:
$bits = self::readBits($data, $resetValue, $getNextValue, $compressed, 8);
$c = $dictionary->size();
$dictionary->addEntry(LZUtil::fromCharCode($bits));
$enlargeIn--;
break;
case 1:
$bits = self::readBits($data, $resetValue, $getNextValue, $compressed, 16);
$c = $dictionary->size();
$dictionary->addEntry(LZUtil::fromCharCode($bits));
$enlargeIn--;
break;
case 2:
return $result;
break;
}
if($enlargeIn == 0) {
$enlargeIn = pow(2, $numBits);
$numBits++;
}
if($dictionary->hasEntry($c)) {
$entry = $dictionary->getEntry($c);
}
else {
if ($c == $dictionary->size()) {
$entry = $w . $w{0};
} else {
return null;
}
}
$result .= $entry;
$dictionary->addEntry($w . LZUtil::utf8_charAt($entry, 0));
$w = $entry;
$enlargeIn--;
if($enlargeIn == 0) {
$enlargeIn = pow(2, $numBits);
$numBits++;
}
}
}
}

View File

@@ -1,110 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: sics
* Date: 27.02.2016
* Time: 15:54
*/
namespace LZCompressor;
class LZUtil
{
/**
* @var string
*/
public static $keyStrBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
public static $keyStrUriSafe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$";
private static $baseReverseDic = array();
/**
* @param string $alphabet
* @param integer $character
* @return string
*/
public static function getBaseValue($alphabet, $character)
{
if(!array_key_exists($alphabet, self::$baseReverseDic)) {
self::$baseReverseDic[$alphabet] = array();
for($i=0; $i<strlen($alphabet); $i++) {
self::$baseReverseDic[$alphabet][$alphabet{$i}] = $i;
}
}
return self::$baseReverseDic[$alphabet][$character];
}
/**
* @return string
*/
public static function fromCharCode()
{
return array_reduce(func_get_args(), function ($a, $b) {
$a .= self::utf8_chr($b);
return $a;
});
}
/**
* Phps chr() equivalent for UTF-8 encoding
*
* @param int|string $u
* @return string
*/
public static function utf8_chr($u)
{
return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-8', 'HTML-ENTITIES');
}
/**
* @param string $str
* @param int $num
*
* @return bool|integer
*/
public static function charCodeAt($str, $num=0)
{
return self::utf8_ord(self::utf8_charAt($str, $num));
}
/**
* @param string $ch
*
* @return bool|integer
*/
public static function utf8_ord($ch)
{
// must remain php's strlen
$len = strlen($ch);
if ($len <= 0) {
return -1;
}
$h = ord($ch{0});
if ($h <= 0x7F) return $h;
if ($h < 0xC2) return -3;
if ($h <= 0xDF && $len > 1) return ($h & 0x1F) << 6 | (ord($ch{1}) & 0x3F);
if ($h <= 0xEF && $len > 2) return ($h & 0x0F) << 12 | (ord($ch{1}) & 0x3F) << 6 | (ord($ch{2}) & 0x3F);
if ($h <= 0xF4 && $len > 3)
return ($h & 0x0F) << 18 | (ord($ch{1}) & 0x3F) << 12 | (ord($ch{2}) & 0x3F) << 6 | (ord($ch{3}) & 0x3F);
return -2;
}
/**
* @param string $str
* @param integer $num
*
* @return string
*/
public static function utf8_charAt($str, $num)
{
return mb_substr($str, $num, 1, 'UTF-8');
}
/**
* @param string $str
* @return integer
*/
public static function utf8_strlen($str) {
return mb_strlen($str, 'UTF-8');
}
}

View File

@@ -1,90 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: sics
* Date: 27.02.2016
* Time: 15:54
*/
namespace LZCompressor;
class LZUtil16
{
/**
* @return string
*/
public static function fromCharCode()
{
return array_reduce(func_get_args(), function ($a, $b) {
$a .= self::utf16_chr($b);
return $a;
});
}
/**
* Phps chr() equivalent for UTF-16 encoding
*
* @param int|string $u
* @return string
*/
public static function utf16_chr($u)
{
return mb_convert_encoding('&#' . intval($u) . ';', 'UTF-16', 'HTML-ENTITIES');
}
/**
* @param string $str
* @param int $num
*
* @return bool|integer
*/
public static function charCodeAt($str, $num=0)
{
return self::utf16_ord(self::utf16_charAt($str, $num));
}
/**
* @source http://blog.sarabande.jp/post/35970262740
* @param string $ch
* @return bool|integer
*/
function utf16_ord($ch) {
$length = strlen($ch);
if (2 === $length) {
return hexdec(bin2hex($ch));
} else if (4 === $length) {
$w1 = $ch[0].$ch[1];
$w2 = $ch[2].$ch[3];
if ($w1 < "\xD8\x00" || "\xDF\xFF" < $w1 || $w2 < "\xDC\x00" || "\xDF\xFF" < $w2) {
return false;
}
$w1 = (hexdec(bin2hex($w1)) & 0x3ff) << 10;
$w2 = hexdec(bin2hex($w2)) & 0x3ff;
return $w1 + $w2 + 0x10000;
}
return false;
}
/**
* @param string $str
* @param integer $num
*
* @return string
*/
public static function utf16_charAt($str, $num)
{
return mb_substr($str, $num, 1, 'UTF-16');
}
/**
* @param string $str
* @return integer
*/
public static function utf16_strlen($str)
{
return mb_strlen($str, 'UTF-16');
}
}

View File

@@ -1 +0,0 @@
var LZString=function(){function o(o,r){if(!t[o]){t[o]={};for(var n=0;n<o.length;n++)t[o][o.charAt(n)]=n}return t[o][r]}var r=String.fromCharCode,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$",t={},i={compressToBase64:function(o){if(null==o)return"";var r=i._compress(o,6,function(o){return n.charAt(o)});switch(r.length%4){default:case 0:return r;case 1:return r+"===";case 2:return r+"==";case 3:return r+"="}},decompressFromBase64:function(r){return null==r?"":""==r?null:i._decompress(r.length,32,function(e){return o(n,r.charAt(e))})},compressToUTF16:function(o){return null==o?"":i._compress(o,15,function(o){return r(o+32)})+" "},decompressFromUTF16:function(o){return null==o?"":""==o?null:i._decompress(o.length,16384,function(r){return o.charCodeAt(r)-32})},compressToUint8Array:function(o){for(var r=i.compress(o),n=new Uint8Array(2*r.length),e=0,t=r.length;t>e;e++){var s=r.charCodeAt(e);n[2*e]=s>>>8,n[2*e+1]=s%256}return n},decompressFromUint8Array:function(o){if(null===o||void 0===o)return i.decompress(o);for(var n=new Array(o.length/2),e=0,t=n.length;t>e;e++)n[e]=256*o[2*e]+o[2*e+1];var s=[];return n.forEach(function(o){s.push(r(o))}),i.decompress(s.join(""))},compressToEncodedURIComponent:function(o){return null==o?"":i._compress(o,6,function(o){return e.charAt(o)})},decompressFromEncodedURIComponent:function(r){return null==r?"":""==r?null:(r=r.replace(/ /g,"+"),i._decompress(r.length,32,function(n){return o(e,r.charAt(n))}))},compress:function(o){return i._compress(o,16,function(o){return r(o)})},_compress:function(o,r,n){if(null==o)return"";var e,t,i,s={},p={},u="",c="",a="",l=2,f=3,h=2,d=[],m=0,v=0;for(i=0;i<o.length;i+=1)if(u=o.charAt(i),Object.prototype.hasOwnProperty.call(s,u)||(s[u]=f++,p[u]=!0),c=a+u,Object.prototype.hasOwnProperty.call(s,c))a=c;else{if(Object.prototype.hasOwnProperty.call(p,a)){if(a.charCodeAt(0)<256){for(e=0;h>e;e++)m<<=1,v==r-1?(v=0,d.push(n(m)),m=0):v++;for(t=a.charCodeAt(0),e=0;8>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}else{for(t=1,e=0;h>e;e++)m=m<<1|t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t=0;for(t=a.charCodeAt(0),e=0;16>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}l--,0==l&&(l=Math.pow(2,h),h++),delete p[a]}else for(t=s[a],e=0;h>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1;l--,0==l&&(l=Math.pow(2,h),h++),s[c]=f++,a=String(u)}if(""!==a){if(Object.prototype.hasOwnProperty.call(p,a)){if(a.charCodeAt(0)<256){for(e=0;h>e;e++)m<<=1,v==r-1?(v=0,d.push(n(m)),m=0):v++;for(t=a.charCodeAt(0),e=0;8>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}else{for(t=1,e=0;h>e;e++)m=m<<1|t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t=0;for(t=a.charCodeAt(0),e=0;16>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}l--,0==l&&(l=Math.pow(2,h),h++),delete p[a]}else for(t=s[a],e=0;h>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1;l--,0==l&&(l=Math.pow(2,h),h++)}for(t=2,e=0;h>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1;for(;;){if(m<<=1,v==r-1){d.push(n(m));break}v++}return d.join("")},decompress:function(o){return null==o?"":""==o?null:i._decompress(o.length,32768,function(r){return o.charCodeAt(r)})},_decompress:function(o,n,e){var t,i,s,p,u,c,a,l,f=[],h=4,d=4,m=3,v="",w=[],A={val:e(0),position:n,index:1};for(i=0;3>i;i+=1)f[i]=i;for(p=0,c=Math.pow(2,2),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;switch(t=p){case 0:for(p=0,c=Math.pow(2,8),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;l=r(p);break;case 1:for(p=0,c=Math.pow(2,16),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;l=r(p);break;case 2:return""}for(f[3]=l,s=l,w.push(l);;){if(A.index>o)return"";for(p=0,c=Math.pow(2,m),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;switch(l=p){case 0:for(p=0,c=Math.pow(2,8),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;f[d++]=r(p),l=d-1,h--;break;case 1:for(p=0,c=Math.pow(2,16),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;f[d++]=r(p),l=d-1,h--;break;case 2:return w.join("")}if(0==h&&(h=Math.pow(2,m),m++),f[l])v=f[l];else{if(l!==d)return null;v=s+s.charAt(0)}w.push(v),f[d++]=s+v.charAt(0),h--,s=v,0==h&&(h=Math.pow(2,m),m++)}}};return i}();"function"==typeof define&&define.amd?define(function(){return LZString}):"undefined"!=typeof module&&null!=module&&(module.exports=LZString);