Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / gzip-js / lib / gzip.js
1 (function () {
2         'use strict';
3
4         var crc32 = require('crc32'),
5                 deflate = require('deflate-js'),
6                 // magic numbers marking this file as GZIP
7                 ID1 = 0x1F,
8                 ID2 = 0x8B,
9                 compressionMethods = {
10                         'deflate': 8
11                 },
12                 possibleFlags = {
13                         'FTEXT': 0x01,
14                         'FHCRC': 0x02,
15                         'FEXTRA': 0x04,
16                         'FNAME': 0x08,
17                         'FCOMMENT': 0x10
18                 },
19                 osMap = {
20                         'fat': 0, // FAT file system (DOS, OS/2, NT) + PKZIPW 2.50 VFAT, NTFS
21                         'amiga': 1, // Amiga
22                         'vmz': 2, // VMS (VAX or Alpha AXP)
23                         'unix': 3, // Unix
24                         'vm/cms': 4, // VM/CMS
25                         'atari': 5, // Atari
26                         'hpfs': 6, // HPFS file system (OS/2, NT 3.x)
27                         'macintosh': 7, // Macintosh
28                         'z-system': 8, // Z-System
29                         'cplm': 9, // CP/M
30                         'tops-20': 10, // TOPS-20
31                         'ntfs': 11, // NTFS file system (NT)
32                         'qdos': 12, // SMS/QDOS
33                         'acorn': 13, // Acorn RISC OS
34                         'vfat': 14, // VFAT file system (Win95, NT)
35                         'vms': 15, // MVS (code also taken for PRIMOS)
36                         'beos': 16, // BeOS (BeBox or PowerMac)
37                         'tandem': 17, // Tandem/NSK
38                         'theos': 18 // THEOS
39                 },
40                 os = 'unix',
41                 DEFAULT_LEVEL = 6;
42
43         function putByte(n, arr) {
44                 arr.push(n & 0xFF);
45         }
46
47         // LSB first
48         function putShort(n, arr) {
49                 arr.push(n & 0xFF);
50                 arr.push(n >>> 8);
51         }
52
53         // LSB first
54         function putLong(n, arr) {
55                 putShort(n & 0xffff, arr);
56                 putShort(n >>> 16, arr);
57         }
58
59         function putString(s, arr) {
60                 var i, len = s.length;
61                 for (i = 0; i < len; i += 1) {
62                         putByte(s.charCodeAt(i), arr);
63                 }
64         }
65
66         function readByte(arr) {
67                 return arr.shift();
68         }
69
70         function readShort(arr) {
71                 return arr.shift() | (arr.shift() << 8);
72         }
73
74         function readLong(arr) {
75                 var n1 = readShort(arr),
76                         n2 = readShort(arr);
77
78                 // JavaScript can't handle bits in the position 32
79                 // we'll emulate this by removing the left-most bit (if it exists)
80                 // and add it back in via multiplication, which does work
81                 if (n2 > 32768) {
82                         n2 -= 32768;
83
84                         return ((n2 << 16) | n1) + 32768 * Math.pow(2, 16);
85                 }
86
87                 return (n2 << 16) | n1;
88         }
89
90         function readString(arr) {
91                 var charArr = [];
92
93                 // turn all bytes into chars until the terminating null
94                 while (arr[0] !== 0) {
95                         charArr.push(String.fromCharCode(arr.shift()));
96                 }
97
98                 // throw away terminating null
99                 arr.shift();
100
101                 // join all characters into a cohesive string
102                 return charArr.join('');
103         }
104
105         /*
106          * Reads n number of bytes and return as an array.
107          *
108          * @param arr- Array of bytes to read from
109          * @param n- Number of bytes to read
110          */
111         function readBytes(arr, n) {
112                 var i, ret = [];
113                 for (i = 0; i < n; i += 1) {
114                         ret.push(arr.shift());
115                 }
116
117                 return ret;
118         }
119
120         /*
121          * ZIPs a file in GZIP format. The format is as given by the spec, found at:
122          * http://www.gzip.org/zlib/rfc-gzip.html
123          *
124          * Omitted parts in this implementation:
125          */
126         function zip(data, options) {
127                 var flags = 0,
128                         level = options.level || DEFAULT_LEVEL,
129                         crc, out = [];
130
131                 if (typeof data === 'string') {
132                         data = Array.prototype.map.call(data, function (char) {
133                                 return char.charCodeAt(0);
134                         });
135                 }
136
137                 // magic number marking this file as GZIP
138                 putByte(ID1, out);
139                 putByte(ID2, out);
140
141                 putByte(compressionMethods['deflate'], out);
142
143                 if (options.name) {
144                         flags |= possibleFlags['FNAME'];
145                 }
146
147                 putByte(flags, out);
148                 putLong(options.timestamp || parseInt(Date.now() / 1000, 10), out);
149
150                 // put deflate args (extra flags)
151                 if (level === 1) {
152                         // fastest algorithm
153                         putByte(4, out);
154                 } else if (level === 9) {
155                         // maximum compression (fastest algorithm)
156                         putByte(2, out);
157                 } else {
158                         putByte(0, out);
159                 }
160
161                 // OS identifier
162                 putByte(osMap[os], out);
163
164                 if (options.name) {
165                         // ignore the directory part
166                         putString(options.name.substring(options.name.lastIndexOf('/') + 1), out);
167
168                         // terminating null
169                         putByte(0, out);
170                 }
171
172                 deflate.deflate(data, level).forEach(function (byte) {
173                         putByte(byte, out);
174                 });
175
176                 putLong(parseInt(crc32(data), 16), out);
177                 putLong(data.length, out);
178
179                 return out;
180         }
181
182         function unzip(data, options) {
183                 // start with a copy of the array
184                 var arr = Array.prototype.slice.call(data, 0),
185                         t,
186                         compressionMethod,
187                         flags,
188                         mtime,
189                         xFlags,
190                         key,
191                         os,
192                         crc,
193                         size,
194                         res;
195
196                 // check the first two bytes for the magic numbers
197                 if (readByte(arr) !== ID1 || readByte(arr) !== ID2) {
198                         throw 'Not a GZIP file';
199                 }
200
201                 t = readByte(arr);
202                 t = Object.keys(compressionMethods).some(function (key) {
203                         compressionMethod = key;
204                         return compressionMethods[key] === t;
205                 });
206
207                 if (!t) {
208                         throw 'Unsupported compression method';
209                 }
210
211                 flags = readByte(arr);
212                 mtime = readLong(arr);
213                 xFlags = readByte(arr);
214                 t = readByte(arr);
215                 Object.keys(osMap).some(function (key) {
216                         if (osMap[key] === t) {
217                                 os = key;
218                                 return true;
219                         }
220                 });
221
222                 // just throw away the bytes for now
223                 if (flags & possibleFlags['FEXTRA']) {
224                         t = readShort(arr);
225                         readBytes(arr, t);
226                 }
227
228                 // just throw away for now
229                 if (flags & possibleFlags['FNAME']) {
230                         readString(arr);
231                 }
232
233                 // just throw away for now
234                 if (flags & possibleFlags['FCOMMENT']) {
235                         readString(arr);
236                 }
237
238                 // just throw away for now
239                 if (flags & possibleFlags['FHCRC']) {
240                         readShort(arr);
241                 }
242
243                 if (compressionMethod === 'deflate') {
244                         // give deflate everything but the last 8 bytes
245                         // the last 8 bytes are for the CRC32 checksum and filesize
246                         res = deflate.inflate(arr.splice(0, arr.length - 8));
247                 }
248
249                 if (flags & possibleFlags['FTEXT']) {
250                         res = Array.prototype.map.call(res, function (byte) {
251                                 return String.fromCharCode(byte);
252                         }).join('');
253                 }
254
255                 crc = readLong(arr);
256                 if (crc !== parseInt(crc32(res), 16)) {
257                         throw 'Checksum does not match';
258                 }
259
260                 size = readLong(arr);
261                 if (size !== res.length) {
262                         throw 'Size of decompressed file not correct';
263                 }
264
265                 return res;
266         }
267
268         module.exports = {
269                 zip: zip,
270                 unzip: unzip,
271                 get DEFAULT_LEVEL() {
272                         return DEFAULT_LEVEL;
273                 }
274         };
275 }());