-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompress.php
286 lines (246 loc) · 6.58 KB
/
Compress.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
namespace ArabyPHP\Compress;
class Compress
{
/**
* @var
*/
private $_encode;
/**
* @var string
*/
private $_binary;
/**
* @var string
*/
private $_hex;
/**
* @var string
*/
private $_bin;
/**
* Loads initialize values.
*
* @ignore
*/
public function __construct()
{
$this->_encode = iconv('utf-8', 'cp1256', ' الميوتة');
$this->_binary = '0000|0001|0010|0011|0100|0101|0110|0111|';
$this->_hex = '0123456789abcdef';
$this->_bin = '0000|0001|0010|0011|0100|0101|0110|0111|1000|';
$this->_bin = $this->_bin.'1001|1010|1011|1100|1101|1110|1111|';
}
/**
* Set required encode and binary hash of most probably character in
* selected language.
*
* @param string $lang [en, fr, gr, it, sp, ar] Language profile selected
*
* @return object $this to build a fluent interface
*
*/
public function setCompressLang($lang)
{
switch ($lang) {
case 'en':
$this->_encode = ' etaoins';
break;
case 'fr':
$this->_encode = ' enasriu';
break;
case 'gr':
$this->_encode = ' enristu';
break;
case 'it':
$this->_encode = ' eiaorln';
break;
case 'sp':
$this->_encode = ' eaosrin';
break;
default:
$this->_encode = iconv('utf-8', 'cp1256', ' الميوتة');
}
$this->_binary = '0000|0001|0010|0011|0100|0101|0110|0111|';
return $this;
}
/**
* Compress the given string using the Huffman-like coding.
*
* @param string $str The text to compress
*
* @return mixed The compressed string in binary format
*
*/
public function compress($str)
{
$str = iconv('utf-8', 'cp1256', $str);
$bits = $this->str2bits($str);
$hex = $this->bits2hex($bits);
$bin = pack('h*', $hex);
return $bin;
}
/**
* Uncompress a compressed string.
*
* @param binary $bin The text compressed by compress().
*
* @return string The original uncompressed string
*
*/
public function decompress($bin)
{
$temp = unpack('h*', $bin);
$bytes = $temp[1];
$bits = $this->hex2bits($bytes);
$str = $this->bits2str($bits);
$str = iconv('cp1256', 'utf-8', $str);
return $str;
}
/**
* Search a compressed string for a given word.
*
* @param binary $bin Compressed binary string
* @param string $word The string you looking for
*
* @return bool True if found and False if not found
*
*/
public function search($bin, $word)
{
$word = iconv('utf-8', 'cp1256', $word);
$wBits = $this->str2bits($word);
$temp = unpack('h*', $bin);
$bytes = $temp[1];
$bits = $this->hex2bits($bytes);
if (strpos($bits, $wBits)) {
return true;
} else {
return false;
}
}
/**
* Retrieve the original string length.
*
* @param binary $bin Compressed binary string
*
* @return int Original string length
*
*/
public function length($bin)
{
$temp = unpack('h*', $bin);
$bytes = $temp[1];
$bits = $this->hex2bits($bytes);
$count = 0;
$i = 0;
while (isset($bits[$i])) {
$count++;
if ($bits[$i] == 1) {
$i += 9;
} else {
$i += 4;
}
}
return $count;
}
/**
* Convert textual string into binary string.
*
* @param string $str The textual string to convert
*
* @return binary The binary representation of textual string
*
*/
protected function str2bits($str)
{
$bits = '';
$total = strlen($str);
$i = -1;
while (++$i < $total) {
$char = $str[$i];
$pos = strpos($this->_encode, $char);
if ($pos !== false) {
$bits .= substr($this->_binary, $pos * 5, 4);
} else {
$int = ord($char);
$bits .= '1'.substr($this->_bin, (int) ($int / 16) * 5, 4);
$bits .= substr($this->_bin, ($int % 16) * 5, 4);
}
}
// Complete nibbel
$add = strlen($bits) % 4;
$bits .= str_repeat('0', $add);
return $bits;
}
/**
* Convert binary string into textual string.
*
* @param binary $bits The binary string to convert
*
* @return string The textual representation of binary string
*
*/
protected function bits2str($bits)
{
$str = '';
while ($bits) {
$flag = substr($bits, 0, 1);
$bits = substr($bits, 1);
if ($flag == 1) {
$byte = substr($bits, 0, 8);
$bits = substr($bits, 8);
if ($bits || strlen($code) == 8) {
$int = base_convert($byte, 2, 10);
$char = chr($int);
$str .= $char;
}
} else {
$code = substr($bits, 0, 3);
$bits = substr($bits, 3);
if ($bits || strlen($code) == 3) {
$pos = strpos($this->_binary, "0$code|");
$str .= substr($this->_encode, $pos / 5, 1);
}
}
}
return $str;
}
/**
* Convert binary string into hexadecimal string.
*
* @param binary $bits The binary string to convert
*
* @return hexadecimal The hexadecimal representation of binary string
*
*/
protected function bits2hex($bits)
{
$hex = '';
$total = strlen($bits) / 4;
for ($i = 0; $i < $total; $i++) {
$nibbel = substr($bits, $i * 4, 4);
$pos = strpos($this->_bin, $nibbel);
$hex .= substr($this->_hex, $pos / 5, 1);
}
return $hex;
}
/**
* Convert hexadecimal string into binary string.
*
* @param hexadecimal $hex The hexadezimal string to convert
*
* @return binary The binary representation of hexadecimal string
*
*/
protected function hex2bits($hex)
{
$bits = '';
$total = strlen($hex);
for ($i = 0; $i < $total; $i++) {
$pos = strpos($this->_hex, $hex[$i]);
$bits .= substr($this->_bin, $pos * 5, 4);
}
return $bits;
}
}