|
- <?php
- function bin_carry_convert($data,$old,$new){
- $old &= 255;
- $new &= 255;
- $k = 0;
- $return = array();
- $len = strlen($data);
- $data_array = array();
- if($old <=0 || $new <= 0 || $len <= 0){
- return;
- }elseif($old == $new) {
- return $data;
- }
- $old2 = $old+1;$new2 = $new+1;
-
- for($i=0;$i<$len;$i++){
- $data_array[$i] = ord($data[$i]);
- if($data_array[$i] > $old){
- return;
- }
- }
- while($len >= 1){
- $j = 0;
- for($i=0;$i<$len;$i++){
- $j = $j * $old2 + $data_array[$i];
- $data_array[$i] = floor($j / $new2);
- $j %= $new2;
- }
- $return[$k++] = chr($j);
- $j = 0;
- while($j < $len && $data_array[$j] == 0){
- $j++;
- }
- array_splice($data_array,0,$j);
- $len=sizeof($data_array);
- }
- return implode(array_reverse($return));
- }
- function logic2bin($logic){
- $len = sizeof($logic);
- if($len %8) return;
- $i=0;
- $char =0;
- $return = null;
- foreach($logic as $bit){
- $bit &=1;
- $char|= $bit ? 1<< $i : 0;
- $i++;
- $i&=7;
- if(!$i) {
- $return.=chr($char);
- $char = 0;
- }
- }
- return $return;
- }
- function bin2logic($bin){
- $len = strlen($bin);
- $return = array();
- $k=0;
- for($i=0;$i<$len;$i++){
- $char=ord($bin[$i]);
- for($j=0;$j<8;$j++){
- $return[$k++]=$char&(1<<$j) ? 1 :0;
- }
- }
- return $return;
- }
- function appear_char($data){
- $return = array();
- $len = strlen($data);
- $acn = 0;
- for($i=0;$i<256;$i++){
- $return[$i] = 0;
- }
- for($i=0;$i<$len;$i++){
- $char = ord($data[$i]);
- if(!$return[$char]){
- $return[$char] = 1;
- $acn++;
- if($acn >= 256) break;
- }
- }
- return logic2bin($return);
- }
- function bin_encode($data,&$key){
- $len = strlen($data);
- $key = appear_char($data);
- $key_array = bin2logic($key);
- $key_table = array();
- $j = 0;
- $return = null;
- for($i=0;$i<256;$i++){
- if($key_array[$i]){
- $key_table[$i] = $j++;
- }
- }
- for($i=0;$i<$len;$i++){
- $return .= chr($key_table[ord($data[$i])]);
- }
- return $return;
- }
- function bin_decode($data,$key){
- $len = strlen($data);
- $key_array = bin2logic($key);
- $key_table = array();
- $return = null;
- $j = 0;
- for($i=0;$i<256;$i++){
- if($key_array[$i]){
- $key_table[$j++] = $i;
- }
- }
- for($i=0;$i<$len;$i++){
- $return .= chr($key_table[ord($data[$i])]);
- }
-
- return $return;
- }
- function max_bin($bin){
- $len = strlen($bin);
- $return = 0;
- for($i=0;$i<$len;$i++){
- $return = max($return,ord($bin[$i]));
- if($return == 255){
- break;
- }
- }
- return $return;
- }
- function within_encode($data){
- $carry = max_bin($data);
- $carry = $carry ? $carry : 1;
- return chr($carry).bin_carry_convert(chr(1).$data,$carry,255);
- }
- function within_decode($data){
- return substr(bin_carry_convert(substr($data,1),255,ord($data[0])),1);
- }
- //=====================公开函数===================
- function compression_encode($data){ //数据压缩
- $data = bin_encode($data,$key);
- return $key . within_encode($data);
- }
- function compression_decode($data){ //数据解压
- $key = substr($data,0,32);
- $data = substr($data,32);
- return bin_decode(within_decode($data),$key);
- }
- //===================测试函数======================
- /*
- 该源码与以下使用同一种算法,且互相兼容。
- https://www.fedcba9876543210.com/thread-1464-1-1.html
- */
- function test(){
- $data = file_get_contents("Test.txt");
- $data2 = compression_encode($data);
- $len = strlen($data);
- $len2 = strlen($data2);
- echo "压缩前:{$len} 压缩后:{$len2}\r\n";
- echo compression_decode($data2) == $data ? "OK!!" : "Err!";
- }
- test();
- ?>
复制代码 |
|