Merge "Correct recent schema changes for MSSQL, Oracle"
[lhc/web/wiklou.git] / resources / lib / easy-deflate / easydeflate.js
1 /**
2 Copyright (c) 2013, Specialisterne.
3 http://specialisterne.com/dk/
4 All rights reserved.
5 Authors:
6 Jacob Christian Munch-Andersen
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 1. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 **/
28 // For information and latest version see: https://github.com/Jacob-Christian-Munch-Andersen/Easy-Deflate
29 (function(){
30
31 var zip={};
32 function UTF8encode(str){
33 var out=[];
34 var a;
35 var c,c2;
36 for(a=0;a<str.length;a++){
37 c=str.charCodeAt(a);
38 if(c<128){
39 out.push(c);
40 }
41 else if(c<2048){
42 out.push((c >> 6)+192);
43 out.push((c & 63)+128);
44 }
45 else if(c<65536){
46 if(c>=0xD800 && c<0xDC00){
47 a++;
48 if(a>=str.length){
49 return null;
50 }
51 c2=str.charCodeAt(a);
52 if(c2>=0xDC00 && c2<0xE000){
53 c=65536+(c-0xD800)*1024+c2-0xDC00;
54 out.push((c >> 18)+240);
55 out.push(((c >> 12) & 63)+128);
56 out.push(((c >> 6) & 63)+128);
57 out.push((c & 63)+128);
58 }
59 else{
60 return null;
61 }
62 }
63 else if(c>=0xDC00 && c<0xE000){
64 return null;
65 }
66 else{
67 out.push((c >> 12)+224);
68 out.push(((c >> 6) & 63)+128);
69 out.push((c & 63)+128);
70 }
71 }
72 else{
73 return null;
74 }
75 }
76 return new Uint8Array(out);
77 }
78 function UTF8decodeA(arrarr){
79 var result="";
80 var intermediate;
81 var minvalue;
82 var missing=0;
83 var a,b;
84 var arr;
85 var c;
86 var lower,upper;
87 for(a=0;a<arrarr.length;a++){
88 arr=arrarr[a];
89 for(b=0;b<arr.length;b++){
90 c=arr[b];
91 if(missing){
92 if(c>127 && c<192){
93 intermediate=intermediate*64+c-128;
94 missing--;
95 if(!missing){
96 if(intermediate>=minvalue){
97 if(intermediate>=65536){
98 if(intermediate>0x10FFFF){
99 return null;
100 }
101 upper=(intermediate-65536)>>10;
102 lower=intermediate%1024;
103 result+=String.fromCharCode(upper+0xD800,lower+0xDC00);
104 }
105 else{
106 result+=String.fromCharCode(intermediate);
107 }
108 }
109 else{
110 return null;
111 }
112 }
113 }
114 else{
115 return null;
116 }
117 }
118 else if(c<128){
119 result+=String.fromCharCode(c);
120 }
121 else if(c>191 && c<248){
122 if(c<224){
123 intermediate=c-192;
124 minvalue=128;
125 missing=1;
126 }
127 else if(c<240){
128 intermediate=c-224;
129 minvalue=2048;
130 missing=2;
131 }
132 else{
133 intermediate=c-240;
134 minvalue=65536;
135 missing=3;
136 }
137 }
138 else{
139 return null;
140 }
141 }
142 }
143 if(missing){
144 return null;
145 }
146 return result;
147 }
148 function deflate(str){
149 var a,c;
150 var readlen=50000;
151 var resulta=[];
152 var results="";
153 var b,d;
154 var zipper=new zip.Deflater(9);
155 for(a=0;a<str.length;a+=readlen){
156 d=UTF8encode(str.substr(a,readlen));
157 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.
158 d=UTF8encode(str.substr(a,readlen+1));
159 a+=1;
160 if(d===null){
161 return null;
162 }
163 }
164 b=zipper.append(d);
165 if(b.length!==0){
166 resulta.push(b);
167 }
168 }
169 b=zipper.flush();
170 if(b.length!==0){
171 resulta.push(b);
172 }
173 for(a=0;a<resulta.length;a++){
174 for(c=0;c<resulta[a].length;c++){
175 results+=String.fromCharCode(resulta[a][c]);
176 }
177 }
178 return "rawdeflate,"+btoa(results);
179 }
180 function inflate(dfl){
181 var unzipper=new zip.Inflater();
182 var resulta=[];
183 var dfls;
184 var a,c;
185 var b,d;
186 if(dfl.slice(0,11)!="rawdeflate,"){
187 return null;
188 }
189 try{
190 dfls=atob(dfl.slice(11));
191 }
192 catch(e){
193 return null;
194 }
195 try{
196 for(a=0;a<dfls.length;a+=50000){
197 b=new Uint8Array(Math.min(50000,dfls.length-a));
198 for(c=0;c<b.length;c++){
199 b[c]=dfls.charCodeAt(c+a);
200 }
201 d=unzipper.append(b);
202 if(d.length){
203 resulta.push(d);
204 }
205 }
206 return UTF8decodeA(resulta);
207 }
208 catch(e){
209 return null;
210 }
211 }
212
213 window.EasyDeflate = {
214 'zip': zip,
215 'inflate': inflate,
216 'deflate': deflate
217 };
218
219 })();