Repository navigation
Expand file tree
/
Copy pathZDA.inc.php
More file actions
185 lines (170 loc) · 4.24 KB
/
Copy pathZDA.inc.php
File metadata and controls
185 lines (170 loc) · 4.24 KB
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
<?php
class ZDArchive {
/*
*
* 创建字典
*
*/
private function createDict($sc, $data = '', &$dict)
{
if(isset($sc['k'])) {
$dict[$sc['k']] = $data;
} else {
$this->createDict($sc['l'], "{$data}0", $dict);
$this->createDict($sc['r'], "{$data}1", $dict);
}
}
/*
*
* 初始化哈夫曼树
*
*/
private function createHuffmanTree($data)
{
$hc = count_chars($data, 1);
$ht = [];
foreach ($hc as $k => $v) {
$ht[] = [
'k' => chr($k),
'v' => $v,
'l' => NULL,
'r' => NULL,
];
}
return $ht;
}
/*
*
* 建立哈夫曼树
*
*/
private function buildTree($tree)
{
$count = count($tree);
for($i = 0;$i !== $count - 1;$i++) {
uasort($tree, function($a, $b) {
if($a['v'] === $b['v']) {
return 0;
}
return $a['v'] < $b['v'] ? -1 : 1;
});
$a = array_shift($tree);
$b = array_shift($tree);
$tree[] = [
'v' => $a['v'] + $b['v'],
'l' => $b,
'r' => $a,
];
}
return $tree;
}
/*
*
* 压缩文件
*
*/
public function compress($fileData, $outputFile = '', $multiple = false, $encryptKey = '', $encryptMode = 'AES-256-CBC')
{
$dict = [];
$ht = $this->createHuffmanTree($fileData);
$ht = $this->buildTree($ht);
$prefix = empty($encryptKey) ? "ZDA:NOR|" : "ZDA:AES,{$encryptMode}|";
$root = current($ht);
$this->createDict($root, '', $dict);
$dtxt = gzcompress(serialize($dict), 9);
$header = pack('VV', strlen($dtxt), strlen($fileData));
$buff = '';
$data = '';
$i = 0;
if(!empty($encryptKey)) {
$multiple = false;
}
if($multiple) {
if(empty($outputFile)) {
throw new Exception("Output file cannot be empty when multiple mode On");
}
$fh = fopen($outputFile, "w+");
fwrite($fh, $prefix . $header . $dtxt);
} else {
$data .= $header . $dtxt;
}
while(isset($fileData[$i])) {
$buf .= $dict[$fileData[$i]];
while(isset($buf[7])) {
$chr = chr(bindec(substr($buf, 0, 8)));
$buf = substr($buf, 8);
$multiple ? fwrite($fh, $chr) : $data .= $chr;
}
$i++;
}
if(!empty($buf)) {
$chr = chr(bindec(str_pad($buf, 8, '0')));
$multiple ? fwrite($fh, $chr) : $data .= $chr;
}
if(!empty($encryptKey)) {
$data = openssl_encrypt($data, $encryptMode, hash('sha256', $encryptKey, true), OPENSSL_RAW_DATA, substr(md5($encryptKey), 0, 16));
}
if(!$multiple && !empty($outputFile)) {
file_put_contents($outputFile, $prefix . "gz:" . gzcompress($data));
}
return $multiple ? fclose($fh) : $prefix . "gz:" . gzcompress($data, 9);
}
/*
*
* 解压文件
*
*/
public function decompress($data, $outputFile = '', $encryptKey = '')
{
if(substr($data, 0, 4) == "ZDA:") {
$type = substr($data, 4, 3);
switch($type) {
case "NOR":
$data = substr($data, 8, strlen($data) - 8);
if(substr($data, 0, 3) == "gz:") {
$data = gzuncompress(substr($data, 3));
}
break;
case "AES":
if(empty($encryptKey)) {
echo "Enter the password: ";
$fp = fopen("php://stdin", "r");
$encryptKey = rtrim(fgets($fp, 1024));
}
$encMode = substr($data, 8);
$encMode = substr($encMode, 0, strpos($encMode, "|"));
$data = substr($data, strpos($data, "|") + 1);
$data = openssl_decrypt($data, $encMode, hash('sha256', $encryptKey, true), OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, substr(md5($encryptKey), 0, 16));
if(substr($data, 0, 3) == "gz:") {
$data = gzuncompress(substr($data, 3));
}
break;
default:
throw new Exception("Not a ZDArchive file!");
}
$header = unpack('VdictLen/VdataLen', $data);
$dict = array_flip(unserialize(gzuncompress(substr($data, 8, $header['dictLen']))));
$bin = substr($data, 8 + $header['dictLen']);
$dl = 0;
$i = 0;
while($bin[$i] !== null && $dl !== $header['dataLen']) {
$bins = str_pad(decbin(ord($bin[$i])), 8, '0', STR_PAD_LEFT);
for($s = 0; $s !== 8; $s++) {
$k .= $bins[$s];
if($dict[$k] !== null) {
$out .= $dict[$k];
$k = '';
$dl++;
if($dl === $header['dataLen']) {
break;
}
}
}
$i++;
}
return !empty($outputFile) ? file_put_contents($outputFile, $out) : $out;
} else {
throw new Exception("Not a ZDArchive file");
}
}
}