easydeflate.js 4.8 KB

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