-
Notifications
You must be signed in to change notification settings - Fork 4
/
easydeflate.js
209 lines (206 loc) · 4.79 KB
/
easydeflate.js
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
/**
Copyright (c) 2013, Specialisterne.
http://specialisterne.com/dk/
All rights reserved.
Authors:
Jacob Christian Munch-Andersen
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
// For information and latest version see: https://github.com/Jacob-Christian-Munch-Andersen/Easy-Deflate
zip={}
function UTF8encode(str){
var out=[]
var a
var c,c2
for(a=0;a<str.length;a++){
c=str.charCodeAt(a)
if(c<128){
out.push(c)
}
else if(c<2048){
out.push((c >> 6)+192)
out.push((c & 63)+128)
}
else if(c<65536){
if(c>=0xD800 && c<0xDC00){
a++
if(a>=str.length){
return null
}
c2=str.charCodeAt(a)
if(c2>=0xDC00 && c2<0xE000){
c=65536+(c-0xD800)*1024+c2-0xDC00
out.push((c >> 18)+240)
out.push(((c >> 12) & 63)+128)
out.push(((c >> 6) & 63)+128)
out.push((c & 63)+128)
}
else{
return null
}
}
else if(c>=0xDC00 && c<0xE000){
return null
}
else{
out.push((c >> 12)+224)
out.push(((c >> 6) & 63)+128)
out.push((c & 63)+128)
}
}
else{
return null
}
}
return new Uint8Array(out)
}
function UTF8decodeA(arrarr){
var result=""
var intermediate
var minvalue
var missing=0
var a,b
var arr
var c
var lower,upper
for(a=0;a<arrarr.length;a++){
arr=arrarr[a]
for(b=0;b<arr.length;b++){
c=arr[b]
if(missing){
if(c>127 && c<192){
intermediate=intermediate*64+c-128
missing--
if(!missing){
if(intermediate>=minvalue){
if(intermediate>=65536){
if(intermediate>0x10FFFF){
return null
}
upper=(intermediate-65536)>>10
lower=intermediate%1024
result+=String.fromCharCode(upper+0xD800,lower+0xDC00)
}
else{
result+=String.fromCharCode(intermediate)
}
}
else{
return null
}
}
}
else{
return null
}
}
else if(c<128){
result+=String.fromCharCode(c)
}
else if(c>191 && c<248){
if(c<224){
intermediate=c-192
minvalue=128
missing=1
}
else if(c<240){
intermediate=c-224
minvalue=2048
missing=2
}
else{
intermediate=c-240
minvalue=65536
missing=3
}
}
else{
return null
}
}
}
if(missing){
return null
}
return result
}
function deflate(str){
var a,c
var readlen=50000
var resulta=[]
var results=""
var b,d
var zipper=new zip.Deflater(9)
for(a=0;a<str.length;a+=readlen){
d=UTF8encode(str.substr(a,readlen))
if(d==null){ //This error may be due to a 4 byte charachter being split, retry with a string that is 1 longer to fix it.
d=UTF8encode(str.substr(a,readlen+1))
a+=1
if(d==null){
return null
}
}
b=zipper.append(d)
if(b.length!=0){
resulta.push(b)
}
}
b=zipper.flush()
if(b.length!=0){
resulta.push(b)
}
for(a=0;a<resulta.length;a++){
for(c=0;c<resulta[a].length;c++){
results+=String.fromCharCode(resulta[a][c])
}
}
return "rawdeflate,"+btoa(results)
}
function inflate(dfl){
var unzipper=new zip.Inflater()
var resulta=[]
var dfls
var a,c
var b,d
if(dfl.slice(0,11)!="rawdeflate,"){
return null
}
try{
dfls=atob(dfl.slice(11))
}
catch(e){
return null
}
try{
for(a=0;a<dfls.length;a+=50000){
b=new Uint8Array(Math.min(50000,dfls.length-a))
for(c=0;c<b.length;c++){
b[c]=dfls.charCodeAt(c+a)
}
d=unzipper.append(b)
if(d.length){
resulta.push(d)
}
}
return UTF8decodeA(resulta)
}
catch(e){
return null
}
}