OSDN Git Service

three.jsをThirdPartyに追加
[webglgame/webgl_framework.git] / webglFramework / Thirdparty / three.js-master / examples / js / loaders / sea3d / SEA3DLZMA.js
1 /*
2 Copyright (c) 2011 Juan Mellado
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 /*
24 References:
25 - "LZMA SDK" by Igor Pavlov
26   http://www.7-zip.org/sdk.html
27 */
28
29 'use strict';
30
31 SEA3D.LZMA = function () {
32
33         var LZMA = LZMA || {};
34
35         LZMA.OutWindow = function () {
36
37                 this._windowSize = 0;
38
39         };
40
41         LZMA.OutWindow.prototype.create = function ( windowSize ) {
42
43                 if ( ( ! this._buffer ) || ( this._windowSize !== windowSize ) ) {
44
45                         this._buffer = [];
46
47                 }
48                 this._windowSize = windowSize;
49                 this._pos = 0;
50                 this._streamPos = 0;
51
52         };
53
54         LZMA.OutWindow.prototype.flush = function () {
55
56                 var size = this._pos - this._streamPos;
57                 if ( size !== 0 ) {
58
59                         while ( size -- ) {
60
61                                 this._stream.writeByte( this._buffer[ this._streamPos ++ ] );
62
63                         }
64                         if ( this._pos >= this._windowSize ) {
65
66                                 this._pos = 0;
67
68                         }
69                         this._streamPos = this._pos;
70
71                 }
72
73         };
74
75         LZMA.OutWindow.prototype.releaseStream = function () {
76
77                 this.flush();
78                 this._stream = null;
79
80         };
81
82         LZMA.OutWindow.prototype.setStream = function ( stream ) {
83
84                 this.releaseStream();
85                 this._stream = stream;
86
87         };
88
89         LZMA.OutWindow.prototype.init = function ( solid ) {
90
91                 if ( ! solid ) {
92
93                         this._streamPos = 0;
94                         this._pos = 0;
95
96                 }
97
98         };
99
100         LZMA.OutWindow.prototype.copyBlock = function ( distance, len ) {
101
102                 var pos = this._pos - distance - 1;
103                 if ( pos < 0 ) {
104
105                         pos += this._windowSize;
106
107                 }
108                 while ( len -- ) {
109
110                         if ( pos >= this._windowSize ) {
111
112                                 pos = 0;
113
114                         }
115                         this._buffer[ this._pos ++ ] = this._buffer[ pos ++ ];
116                         if ( this._pos >= this._windowSize ) {
117
118                                 this.flush();
119
120                         }
121
122                 }
123
124         };
125
126         LZMA.OutWindow.prototype.putByte = function ( b ) {
127
128                 this._buffer[ this._pos ++ ] = b;
129                 if ( this._pos >= this._windowSize ) {
130
131                         this.flush();
132
133                 }
134
135         };
136
137         LZMA.OutWindow.prototype.getByte = function ( distance ) {
138
139                 var pos = this._pos - distance - 1;
140                 if ( pos < 0 ) {
141
142                         pos += this._windowSize;
143
144                 }
145                 return this._buffer[ pos ];
146
147         };
148
149         LZMA.RangeDecoder = function () {
150         };
151
152         LZMA.RangeDecoder.prototype.setStream = function ( stream ) {
153
154                 this._stream = stream;
155
156         };
157
158         LZMA.RangeDecoder.prototype.releaseStream = function () {
159
160                 this._stream = null;
161
162         };
163
164         LZMA.RangeDecoder.prototype.init = function () {
165
166                 var i = 5;
167
168                 this._code = 0;
169                 this._range = - 1;
170
171                 while ( i -- ) {
172
173                         this._code = ( this._code << 8 ) | this._stream.readByte();
174
175                 }
176
177         };
178
179         LZMA.RangeDecoder.prototype.decodeDirectBits = function ( numTotalBits ) {
180
181                 var result = 0, i = numTotalBits, t;
182
183                 while ( i -- ) {
184
185                         this._range >>>= 1;
186                         t = ( this._code - this._range ) >>> 31;
187                         this._code -= this._range & ( t - 1 );
188                         result = ( result << 1 ) | ( 1 - t );
189
190                         if ( ( this._range & 0xff000000 ) === 0 ) {
191
192                                 this._code = ( this._code << 8 ) | this._stream.readByte();
193                                 this._range <<= 8;
194
195                         }
196
197                 }
198
199                 return result;
200
201         };
202
203         LZMA.RangeDecoder.prototype.decodeBit = function ( probs, index ) {
204
205                 var prob = probs[ index ],
206                         newBound = ( this._range >>> 11 ) * prob;
207
208                 if ( ( this._code ^ 0x80000000 ) < ( newBound ^ 0x80000000 ) ) {
209
210                         this._range = newBound;
211                         probs[ index ] += ( 2048 - prob ) >>> 5;
212                         if ( ( this._range & 0xff000000 ) === 0 ) {
213
214                                 this._code = ( this._code << 8 ) | this._stream.readByte();
215                                 this._range <<= 8;
216
217                         }
218                         return 0;
219
220                 }
221
222                 this._range -= newBound;
223                 this._code -= newBound;
224                 probs[ index ] -= prob >>> 5;
225                 if ( ( this._range & 0xff000000 ) === 0 ) {
226
227                         this._code = ( this._code << 8 ) | this._stream.readByte();
228                         this._range <<= 8;
229
230                 }
231                 return 1;
232
233         };
234
235         LZMA.initBitModels = function ( probs, len ) {
236
237                 while ( len -- ) {
238
239                         probs[ len ] = 1024;
240
241                 }
242
243         };
244
245         LZMA.BitTreeDecoder = function ( numBitLevels ) {
246
247                 this._models = [];
248                 this._numBitLevels = numBitLevels;
249
250         };
251
252         LZMA.BitTreeDecoder.prototype.init = function () {
253
254                 LZMA.initBitModels( this._models, 1 << this._numBitLevels );
255
256         };
257
258         LZMA.BitTreeDecoder.prototype.decode = function ( rangeDecoder ) {
259
260                 var m = 1, i = this._numBitLevels;
261
262                 while ( i -- ) {
263
264                         m = ( m << 1 ) | rangeDecoder.decodeBit( this._models, m );
265
266                 }
267                 return m - ( 1 << this._numBitLevels );
268
269         };
270
271         LZMA.BitTreeDecoder.prototype.reverseDecode = function ( rangeDecoder ) {
272
273                 var m = 1, symbol = 0, i = 0, bit;
274
275                 for ( ; i < this._numBitLevels; ++ i ) {
276
277                         bit = rangeDecoder.decodeBit( this._models, m );
278                         m = ( m << 1 ) | bit;
279                         symbol |= bit << i;
280
281                 }
282                 return symbol;
283
284         };
285
286         LZMA.reverseDecode2 = function ( models, startIndex, rangeDecoder, numBitLevels ) {
287
288                 var m = 1, symbol = 0, i = 0, bit;
289
290                 for ( ; i < numBitLevels; ++ i ) {
291
292                         bit = rangeDecoder.decodeBit( models, startIndex + m );
293                         m = ( m << 1 ) | bit;
294                         symbol |= bit << i;
295
296                 }
297                 return symbol;
298
299         };
300
301         LZMA.LenDecoder = function () {
302
303                 this._choice = [];
304                 this._lowCoder = [];
305                 this._midCoder = [];
306                 this._highCoder = new LZMA.BitTreeDecoder( 8 );
307                 this._numPosStates = 0;
308
309         };
310
311         LZMA.LenDecoder.prototype.create = function ( numPosStates ) {
312
313                 for ( ; this._numPosStates < numPosStates; ++ this._numPosStates ) {
314
315                         this._lowCoder[ this._numPosStates ] = new LZMA.BitTreeDecoder( 3 );
316                         this._midCoder[ this._numPosStates ] = new LZMA.BitTreeDecoder( 3 );
317
318                 }
319
320         };
321
322         LZMA.LenDecoder.prototype.init = function () {
323
324                 var i = this._numPosStates;
325                 LZMA.initBitModels( this._choice, 2 );
326                 while ( i -- ) {
327
328                         this._lowCoder[ i ].init();
329                         this._midCoder[ i ].init();
330
331                 }
332                 this._highCoder.init();
333
334         };
335
336         LZMA.LenDecoder.prototype.decode = function ( rangeDecoder, posState ) {
337
338                 if ( rangeDecoder.decodeBit( this._choice, 0 ) === 0 ) {
339
340                         return this._lowCoder[ posState ].decode( rangeDecoder );
341
342                 }
343                 if ( rangeDecoder.decodeBit( this._choice, 1 ) === 0 ) {
344
345                         return 8 + this._midCoder[ posState ].decode( rangeDecoder );
346
347                 }
348                 return 16 + this._highCoder.decode( rangeDecoder );
349
350         };
351
352         LZMA.Decoder2 = function () {
353
354                 this._decoders = [];
355
356         };
357
358         LZMA.Decoder2.prototype.init = function () {
359
360                 LZMA.initBitModels( this._decoders, 0x300 );
361
362         };
363
364         LZMA.Decoder2.prototype.decodeNormal = function ( rangeDecoder ) {
365
366                 var symbol = 1;
367
368                 do {
369
370                         symbol = ( symbol << 1 ) | rangeDecoder.decodeBit( this._decoders, symbol );
371
372                 }while ( symbol < 0x100 );
373
374                 return symbol & 0xff;
375
376         };
377
378         LZMA.Decoder2.prototype.decodeWithMatchByte = function ( rangeDecoder, matchByte ) {
379
380                 var symbol = 1, matchBit, bit;
381
382                 do {
383
384                         matchBit = ( matchByte >> 7 ) & 1;
385                         matchByte <<= 1;
386                         bit = rangeDecoder.decodeBit( this._decoders, ( ( 1 + matchBit ) << 8 ) + symbol );
387                         symbol = ( symbol << 1 ) | bit;
388                         if ( matchBit !== bit ) {
389
390                                 while ( symbol < 0x100 ) {
391
392                                         symbol = ( symbol << 1 ) | rangeDecoder.decodeBit( this._decoders, symbol );
393
394                                 }
395                                 break;
396
397                         }
398
399                 }while ( symbol < 0x100 );
400
401                 return symbol & 0xff;
402
403         };
404
405         LZMA.LiteralDecoder = function () {
406         };
407
408         LZMA.LiteralDecoder.prototype.create = function ( numPosBits, numPrevBits ) {
409
410                 var i;
411
412                 if ( this._coders
413                         && ( this._numPrevBits === numPrevBits )
414                         && ( this._numPosBits === numPosBits ) ) {
415
416                         return;
417
418                 }
419                 this._numPosBits = numPosBits;
420                 this._posMask = ( 1 << numPosBits ) - 1;
421                 this._numPrevBits = numPrevBits;
422
423                 this._coders = [];
424
425                 i = 1 << ( this._numPrevBits + this._numPosBits );
426                 while ( i -- ) {
427
428                         this._coders[ i ] = new LZMA.Decoder2();
429
430                 }
431
432         };
433
434         LZMA.LiteralDecoder.prototype.init = function () {
435
436                 var i = 1 << ( this._numPrevBits + this._numPosBits );
437                 while ( i -- ) {
438
439                         this._coders[ i ].init();
440
441                 }
442
443         };
444
445         LZMA.LiteralDecoder.prototype.getDecoder = function ( pos, prevByte ) {
446
447                 return this._coders[ ( ( pos & this._posMask ) << this._numPrevBits )
448                         + ( ( prevByte & 0xff ) >>> ( 8 - this._numPrevBits ) ) ];
449
450         };
451
452         LZMA.Decoder = function () {
453
454                 this._outWindow = new LZMA.OutWindow();
455                 this._rangeDecoder = new LZMA.RangeDecoder();
456                 this._isMatchDecoders = [];
457                 this._isRepDecoders = [];
458                 this._isRepG0Decoders = [];
459                 this._isRepG1Decoders = [];
460                 this._isRepG2Decoders = [];
461                 this._isRep0LongDecoders = [];
462                 this._posSlotDecoder = [];
463                 this._posDecoders = [];
464                 this._posAlignDecoder = new LZMA.BitTreeDecoder( 4 );
465                 this._lenDecoder = new LZMA.LenDecoder();
466                 this._repLenDecoder = new LZMA.LenDecoder();
467                 this._literalDecoder = new LZMA.LiteralDecoder();
468                 this._dictionarySize = - 1;
469                 this._dictionarySizeCheck = - 1;
470
471                 this._posSlotDecoder[ 0 ] = new LZMA.BitTreeDecoder( 6 );
472                 this._posSlotDecoder[ 1 ] = new LZMA.BitTreeDecoder( 6 );
473                 this._posSlotDecoder[ 2 ] = new LZMA.BitTreeDecoder( 6 );
474                 this._posSlotDecoder[ 3 ] = new LZMA.BitTreeDecoder( 6 );
475
476         };
477
478         LZMA.Decoder.prototype.setDictionarySize = function ( dictionarySize ) {
479
480                 if ( dictionarySize < 0 ) {
481
482                         return false;
483
484                 }
485                 if ( this._dictionarySize !== dictionarySize ) {
486
487                         this._dictionarySize = dictionarySize;
488                         this._dictionarySizeCheck = Math.max( this._dictionarySize, 1 );
489                         this._outWindow.create( Math.max( this._dictionarySizeCheck, 4096 ) );
490
491                 }
492                 return true;
493
494         };
495
496         LZMA.Decoder.prototype.setLcLpPb = function ( lc, lp, pb ) {
497
498                 var numPosStates = 1 << pb;
499
500                 if ( lc > 8 || lp > 4 || pb > 4 ) {
501
502                         return false;
503
504                 }
505
506                 this._literalDecoder.create( lp, lc );
507
508                 this._lenDecoder.create( numPosStates );
509                 this._repLenDecoder.create( numPosStates );
510                 this._posStateMask = numPosStates - 1;
511
512                 return true;
513
514         };
515
516         LZMA.Decoder.prototype.init = function () {
517
518                 var i = 4;
519
520                 this._outWindow.init( false );
521
522                 LZMA.initBitModels( this._isMatchDecoders, 192 );
523                 LZMA.initBitModels( this._isRep0LongDecoders, 192 );
524                 LZMA.initBitModels( this._isRepDecoders, 12 );
525                 LZMA.initBitModels( this._isRepG0Decoders, 12 );
526                 LZMA.initBitModels( this._isRepG1Decoders, 12 );
527                 LZMA.initBitModels( this._isRepG2Decoders, 12 );
528                 LZMA.initBitModels( this._posDecoders, 114 );
529
530                 this._literalDecoder.init();
531
532                 while ( i -- ) {
533
534                         this._posSlotDecoder[ i ].init();
535
536                 }
537
538                 this._lenDecoder.init();
539                 this._repLenDecoder.init();
540                 this._posAlignDecoder.init();
541                 this._rangeDecoder.init();
542
543         };
544
545         LZMA.Decoder.prototype.decode = function ( inStream, outStream, outSize ) {
546
547                 var state = 0, rep0 = 0, rep1 = 0, rep2 = 0, rep3 = 0, nowPos64 = 0, prevByte = 0,
548                         posState, decoder2, len, distance, posSlot, numDirectBits;
549
550                 this._rangeDecoder.setStream( inStream );
551                 this._outWindow.setStream( outStream );
552
553                 this.init();
554
555                 while ( outSize < 0 || nowPos64 < outSize ) {
556
557                         posState = nowPos64 & this._posStateMask;
558
559                         if ( this._rangeDecoder.decodeBit( this._isMatchDecoders, ( state << 4 ) + posState ) === 0 ) {
560
561                                 decoder2 = this._literalDecoder.getDecoder( nowPos64 ++, prevByte );
562
563                                 if ( state >= 7 ) {
564
565                                         prevByte = decoder2.decodeWithMatchByte( this._rangeDecoder, this._outWindow.getByte( rep0 ) );
566
567                                 } else {
568
569                                         prevByte = decoder2.decodeNormal( this._rangeDecoder );
570
571                                 }
572                                 this._outWindow.putByte( prevByte );
573
574                                 state = state < 4 ? 0 : state - ( state < 10 ? 3 : 6 );
575
576                         } else {
577
578                                 if ( this._rangeDecoder.decodeBit( this._isRepDecoders, state ) === 1 ) {
579
580                                         len = 0;
581                                         if ( this._rangeDecoder.decodeBit( this._isRepG0Decoders, state ) === 0 ) {
582
583                                                 if ( this._rangeDecoder.decodeBit( this._isRep0LongDecoders, ( state << 4 ) + posState ) === 0 ) {
584
585                                                         state = state < 7 ? 9 : 11;
586                                                         len = 1;
587
588                                                 }
589
590                                         } else {
591
592                                                 if ( this._rangeDecoder.decodeBit( this._isRepG1Decoders, state ) === 0 ) {
593
594                                                         distance = rep1;
595
596                                                 } else {
597
598                                                         if ( this._rangeDecoder.decodeBit( this._isRepG2Decoders, state ) === 0 ) {
599
600                                                                 distance = rep2;
601
602                                                         } else {
603
604                                                                 distance = rep3;
605                                                                 rep3 = rep2;
606
607                                                         }
608                                                         rep2 = rep1;
609
610                                                 }
611                                                 rep1 = rep0;
612                                                 rep0 = distance;
613
614                                         }
615                                         if ( len === 0 ) {
616
617                                                 len = 2 + this._repLenDecoder.decode( this._rangeDecoder, posState );
618                                                 state = state < 7 ? 8 : 11;
619
620                                         }
621
622                                 } else {
623
624                                         rep3 = rep2;
625                                         rep2 = rep1;
626                                         rep1 = rep0;
627
628                                         len = 2 + this._lenDecoder.decode( this._rangeDecoder, posState );
629                                         state = state < 7 ? 7 : 10;
630
631                                         posSlot = this._posSlotDecoder[ len <= 5 ? len - 2 : 3 ].decode( this._rangeDecoder );
632                                         if ( posSlot >= 4 ) {
633
634                                                 numDirectBits = ( posSlot >> 1 ) - 1;
635                                                 rep0 = ( 2 | ( posSlot & 1 ) ) << numDirectBits;
636
637                                                 if ( posSlot < 14 ) {
638
639                                                         rep0 += LZMA.reverseDecode2( this._posDecoders,
640                                                                 rep0 - posSlot - 1, this._rangeDecoder, numDirectBits );
641
642                                                 } else {
643
644                                                         rep0 += this._rangeDecoder.decodeDirectBits( numDirectBits - 4 ) << 4;
645                                                         rep0 += this._posAlignDecoder.reverseDecode( this._rangeDecoder );
646                                                         if ( rep0 < 0 ) {
647
648                                                                 if ( rep0 === - 1 ) {
649
650                                                                         break;
651
652                                                                 }
653                                                                 return false;
654
655                                                         }
656
657                                                 }
658
659                                         } else {
660
661                                                 rep0 = posSlot;
662
663                                         }
664
665                                 }
666
667                                 if ( rep0 >= nowPos64 || rep0 >= this._dictionarySizeCheck ) {
668
669                                         return false;
670
671                                 }
672
673                                 this._outWindow.copyBlock( rep0, len );
674                                 nowPos64 += len;
675                                 prevByte = this._outWindow.getByte( 0 );
676
677                         }
678
679                 }
680
681                 this._outWindow.flush();
682                 this._outWindow.releaseStream();
683                 this._rangeDecoder.releaseStream();
684
685                 return true;
686
687         };
688
689         LZMA.Decoder.prototype.setDecoderProperties = function ( properties ) {
690
691                 var value, lc, lp, pb, dictionarySize;
692
693                 if ( properties.size < 5 ) {
694
695                         return false;
696
697                 }
698
699                 value = properties.readByte();
700                 lc = value % 9;
701                 value = ~~ ( value / 9 );
702                 lp = value % 5;
703                 pb = ~~ ( value / 5 );
704
705                 if ( ! this.setLcLpPb( lc, lp, pb ) ) {
706
707                         return false;
708
709                 }
710
711                 dictionarySize = properties.readByte();
712                 dictionarySize |= properties.readByte() << 8;
713                 dictionarySize |= properties.readByte() << 16;
714                 dictionarySize += properties.readByte() * 16777216;
715
716                 return this.setDictionarySize( dictionarySize );
717
718         };
719
720         LZMA.decompress = function ( properties, inStream, outStream, outSize ) {
721
722                 var decoder = new LZMA.Decoder();
723
724                 if ( ! decoder.setDecoderProperties( properties ) ) {
725
726                         throw "Incorrect stream properties";
727
728                 }
729
730                 if ( ! decoder.decode( inStream, outStream, outSize ) ) {
731
732                         throw "Error in data stream";
733
734                 }
735
736                 return true;
737
738         };
739
740         LZMA.decompressFile = function ( inStream, outStream ) {
741
742                 var decoder = new LZMA.Decoder(), outSize;
743
744                 if ( ! decoder.setDecoderProperties( inStream ) ) {
745
746                         throw "Incorrect stream properties";
747
748                 }
749
750                 outSize = inStream.readByte();
751                 outSize |= inStream.readByte() << 8;
752                 outSize |= inStream.readByte() << 16;
753                 outSize += inStream.readByte() * 16777216;
754
755                 inStream.readByte();
756                 inStream.readByte();
757                 inStream.readByte();
758                 inStream.readByte();
759
760                 if ( ! decoder.decode( inStream, outStream, outSize ) ) {
761
762                         throw "Error in data stream";
763
764                 }
765
766                 return true;
767
768         };
769
770         return LZMA;
771
772 }();
773
774
775 /**
776  *      SEA3D LZMA
777  *      @author Sunag / http://www.sunag.com.br/
778  */
779
780 SEA3D.File.LZMAUncompress = function ( data ) {
781
782         data = new Uint8Array( data );
783
784         var inStream = {
785                 data: data,
786                 position: 0,
787                 readByte: function () {
788
789                         return this.data[ this.position ++ ];
790
791                 }
792         };
793
794         var outStream = {
795                 data: [],
796                 position: 0,
797                 writeByte: function ( value ) {
798
799                         this.data[ this.position ++ ] = value;
800
801                 }
802         };
803
804         SEA3D.LZMA.decompressFile( inStream, outStream );
805
806         return new Uint8Array( outStream.data ).buffer;
807
808 };
809
810 SEA3D.File.setDecompressionEngine( 2, "lzma", SEA3D.File.LZMAUncompress );