* debian/missing-sources/zlib.js-0.1.6: add missing source code for gdk/broadway/rawinflate.min.js (Closes: #753968)

- some files in zlib.js upstream had missing source code themselves
    (closure-compiler.jar) or are not needed here, and have been removed
    to save space; see debian/missing-sources/README for details
This commit is contained in:
Simon McVittie
2014-08-11 09:43:29 +00:00
parent 764165e157
commit 7e6de580a7
52 changed files with 9366 additions and 0 deletions

10
debian/changelog vendored
View File

@ -1,3 +1,13 @@
gtk+3.0 (3.12.2-3) UNRELEASED; urgency=medium
* debian/missing-sources/zlib.js-0.1.6: add missing source code for
gdk/broadway/rawinflate.min.js (Closes: #753968)
- some files in zlib.js upstream had missing source code themselves
(closure-compiler.jar) or are not needed here, and have been removed
to save space; see debian/missing-sources/README for details
-- Simon McVittie <smcv@debian.org> Mon, 11 Aug 2014 10:41:21 +0100
gtk+3.0 (3.12.2-2) unstable; urgency=medium
* Team upload

39
debian/missing-sources/README vendored Normal file
View File

@ -0,0 +1,39 @@
Missing sources from gtk+3.0
============================
zlib.js-0.1.6
-------------
Source for: gdk/broadway/rawinflate.min.js
Obtained from: <https://github.com/imaya/zlib.js> tag "0.1.6"
Files removed:
bin/* except for rawinflate.min.js* (precompiled, not needed here)
.travis/, .travis.yml (regression tests, not needed here)
test/ (regression tests, not needed here)
vendor/google-closure-compiler/ (third-party, source not included)
vendor/mt.js/ (third-party, only needed for tests)
gdk/broadway/rawinflate.min.js is identical to bin/rawinflate.min.js.
Debian's closure-compiler_20130227+dfsg1-6 is not identical to the one
originally distributed with zlib.js, but produces output closely resembling
bin/rawinflate.min.js when invoked like this (command line adapted from
build.xml, using bin/rawinflate.min.js.map to determine minimal sources):
cat LICENSE_min
echo -n '(function() {'
closure-compiler --compilation_level=ADVANCED_OPTIMIZATIONS \
--warning_level=VERBOSE \
--define=goog.DEBUG=false \
--summary_detail_level=3 \
--language_in=ECMASCRIPT5_STRICT \
--source_map_format=V3 \
--process_closure_primitives \
--js closure-primitives/base.js \
--js define/typedarray/hybrid.js \
--js=src/export_object.js \
--js=src/huffman.js \
--js=src/rawinflate.js \
--js=src/zlib.js \
--js=export/rawinflate.js
echo -n '}).call(this); //@ sourceMappingURL=rawinflate.min.js.map'

View File

@ -0,0 +1,133 @@
# Change Log
## 0.1.6: 2013/05/10
https://github.com/imaya/zlib.js/compare/0.1.5...0.1.6
### compression
- export raw deflate
### decompression
- export raw inflate
- fix inflate stream exporting
### etc
- export crc-32
- add pretty print build
- optimize compile settings
- add english document
- support source maps
- support Travis CI
- refactor unit test
- update closure compiler (custom version)
-------------------------------------------------------------------------------
## 0.1.5: 2013/02/10
https://github.com/imaya/zlib.js/compare/0.1.4...0.1.5
### compression
- fix PKZIP CRC-32 bug
### etc
- update PKZIP unit test
-------------------------------------------------------------------------------
## 0.1.4: 2013/02/10
https://github.com/imaya/zlib.js/compare/0.1.3...0.1.4
### compression
- add PKZIP compression (basic support)
### decompression
- add PKZIP decompression (basic support)
### etc
- refactor build environment (use export js file)
- remove license comment in source code
-------------------------------------------------------------------------------
## 0.1.3: 2012/12/21
https://github.com/imaya/zlib.js/compare/0.1.2...0.1.3
### compression
- fix rare case bug
-------------------------------------------------------------------------------
## 0.1.2: 2012/12/17
https://github.com/imaya/zlib.js/compare/0.1.1...0.1.2
### compression
- fix adler-32 bug (byte order)
- refactor raw deflate code
### decompression
- refactor inflate stream code
### etc
- update closure compiler (custom version)
- update inflate unit test (enable adler-32 verification)
-------------------------------------------------------------------------------
## 0.1.1: 2012/11/15
https://github.com/imaya/zlib.js/compare/0.1.0...0.1.1
### compression
- fix huffman coding (add reverse package merge algorithm)
### etc
- fix gunzip unit test
-------------------------------------------------------------------------------
## 0.1.0: 2012/09/24
- first release

View File

@ -0,0 +1,28 @@
/**
* @license
* zlib.js
* JavaScript Zlib Library
* https://github.com/imaya/zlib.js
*
* The MIT License
*
* Copyright (c) 2012 imaya
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

View File

@ -0,0 +1 @@
/** @license zlib.js 2012 - imaya [ https://github.com/imaya/zlib.js ] The MIT License */

View File

@ -0,0 +1,285 @@
zlib.js
=======
[![Build Status](https://travis-ci.org/imaya/zlib.js.png?branch=master)](https://travis-ci.org/imaya/zlib.js)
[Japanese version](./README.md)
zlib.js is ZLIB(RFC1950), DEFLATE(RFC1951), GZIP(RFC1952) and PKZIP implementation in JavaScript.
Usage
------
Use one in "bin" directory.
- zlib_and_gzip.min.js: ZLIB + GZIP
+ (Raw)
* rawdeflate.js: Raw Deflate
* rawinflate.js: Raw Inflate
+ zlib.min.js: ZLIB Inflate + Deflate
* inflate.min.js: ZLIB Inflate
* deflate.min.js: ZLIB Deflate
* inflate_stream.min.js: ZLIB Inflate (stream mode)
+ (GZIP)
* gzip.min.js: GZIP
* gunzip.min.js: GUNZIP
+ (PKZIP)
* zip.min.js ZIP
* unzip.min.js UNZIP
- node-zlib.js: (ZLIB + GZIP for node.js)
### Compression
#### Raw Deflate
```js
// plain = Array.<number> or Uint8Array
var defalte = new Zlib.RawDeflate(plain);
var compressed = deflate.compress();
```
#### Raw Deflate Option
See ZLIB Option.
#### ZLIB
```js
// plain = Array.<number> or Uint8Array
var defalte = new Zlib.Deflate(plain);
var compressed = deflate.compress();
```
##### ZLIB Option
Second argument of Zlib.Deflate constructor
```js
{
compressionType: Zlib.Deflate.CompressionType, // compression type
lazy: number // lazy matching parameter
}
```
<code>Zlib.Deflate.CompressionType</code> is enumerable,
Choose one in <code>NONE</code> (Store), <code>FIXED</code> (Fixed Huffman Coding), <code>DYNAMIC</code> (Dynamic Huffman Coding).
Default value is <code>DYNAMIC</code>.
<code>lazy</code> is Lazy Matching length.
This parameter is deprecated.
#### GZIP
GZIP implementation is imcompleted.
However no problem in usual use.
```js
// plain = Array.<number> or Uint8Array
var gzip = new Zlib.Gzip(plain);
var compressed = gzip.compress();
```
##### GZIP Option
```js
{
deflateOptions: Object, // see: deflate option (ZLIB Option)
flags: {
fname: boolean, // use filename?
comment: boolean, // use comment?
fhcrc: boolean // use file checksum?
},
filename: string, // filename
comment: string // comment
}
```
#### PKZIP
```js
var zip = new Zlib.Zip();
// plainData1
zip.addFile(plainData1, {
filename: stringToByteArray('foo.txt')
});
zip.addFile(plainData2, {
filename: stringToByteArray('bar.txt')
});
zip.addFile(plainData3, {
filename: stringToByteArray('baz.txt')
});
var compressed = zip.compress();
function stringToByteArray(str) {
var array = new (window.Uint8Array !== void 0 ? Uint8Array : Array)(str.length);
var i;
var il;
for (i = 0, il = str.length; i < il; ++i) {
array[i] = str.charCodeAt(i) & 0xff;
}
return array;
}
```
##### PKZIP Option
filename, comment, extraField are must use Uint8Array if enabled Typed Array.
```js
{
filename: (Array.<number>|Uint8Array), // filename
comment: (Array.<number>|Uint8Array), //comment
extraField: (Array.<number>|Uint8Array), // extra field
compress: boolean, // compress when called "addFile" method.
compressionMethod: Zlib.Zip.CompressionMethod, // STORE or DEFLATE
os: Zlib.Zip.OperatingSystem, // MSDOS or UNIX or MACINTOSH
deflateOption: Object // see: ZLIB Option
}
```
### Decompression
#### Raw Deflate
```js
// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.RawInflate(compressed);
var plain = inflate.decompress();
```
#### Raw Deflate Option
See ZLIB Option.
#### ZLIB
```js
// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.Inflate(compressed);
var plain = inflate.decompress();
```
##### ZLIB Option
Second argument of Zlib.Inflate constructor
```js
{
'index': number, // start position in input buffer
'bufferSize': number, // initial output buffer size
'bufferType': Zlib.Inflate.BufferType, // buffer expantion type
'resize': boolean, // resize buffer(ArrayBuffer) when end of decompression (default: false)
'verify': boolean // verify decompression result (default: false)
}
```
<code>Zlib.Inflate.BufferType</code> is enumerable.
Choose one <code>ADAPTIVE</code>(default) and <code>BLOCK</code>.
- <code>ADAPTIVE</code>: buffer expansion based on compression ratio in filled buffer.
- <code>BLOCK</code>: buffer expansion based on <code>BufferSize</code>.
#### GZIP
```js
// compressed = Array.<number> or Uint8Array
var gunzip = new Zlib.Gunzip(compressed);
var plain = gunzip.decompress();
```
#### PKZIP
```js
// compressed = Array.<number> or Uint8Array
var unzip = new Zlib.Unzip(compressed);
var filenames = unzip.getFilenames();
var plain = unzip.decompress(filenames[0]);
```
### Node.js
see unit tests.
<https://github.com/imaya/zlib.js/blob/master/test/node-test.js>
## Debug
If you want to know the code before compile, SourceMaps and PrettyPrint can be used.
### SourceMaps
If you want to enable the SourceMaps if, you can use the `src` directory and *.min.js.map.
- inflate.min.js
- inflate.min.js.map
- [src]
- (source files)
`[src]` is zlib.js source code directory.
### Pretty Print
`zlib.pretty.js` is not renamed symbol.
How to build
------------
Build using Ant and Closure Compiler.
### Requirement
- Ant 1.8+
- JRE 1.6+
- Python
### Build
Use "ant" command.
```
$ ant [target]
```
#### Build target
target | generate file | implementation
---------------|----------------------|-------------
deps | deps.js | (dependency: deps.js)
deflate | deflate.min.js | ZLIB Deflate
inflate | inflate.min.js | ZLIB Inflate
inflate_stream | inlate_stream.min.js | ZLIB Inlate (stream)
zlib | zlib.min.js | ZLIB Deflate + Inflate
gzip | gzip.min.js | GZIP Compression
gunzip | gunzip.min.js | GZIP Decompression
zlib_and_gzip | zlib_and_gzip.min.js | ZLIB + GZIP
node | node-zlib.js | ZLIB + GZIP for node.js
zip | zip.min.js | PKZIP Compression
unzip | unzip.min.js | PKZIP Decompression
all | * | default target
Issue
-----
Preset dictionary is not implemented.
License
--------
Copyright &copy; 2012 imaya.
Licensed under the MIT License.

View File

@ -0,0 +1,307 @@
zlib.js
=======
[![Build Status](https://travis-ci.org/imaya/zlib.js.png?branch=master)](https://travis-ci.org/imaya/zlib.js)
[English version](./README.en.md)
zlib.js は ZLIB(RFC1950), DEFLATE(RFC1951), GZIP(RFC1952), PKZIP の JavaScript 実装です。
使い方
------
zlib.js は必要な機能ごとに分割されています。
bin ディレクトリから必要なものを利用してください。
- zlib_and_gzip.min.js: ZLIB + GZIP
+ (Raw)
* rawdeflate.js: Raw Deflate
* rawinflate.js: Raw Inflate
+ zlib.min.js: ZLIB Inflate + Deflate
* inflate.min.js: ZLIB Inflate
* deflate.min.js: ZLIB Deflate
* inflate_stream.min.js: ZLIB Inflate (stream mode)
+ (GZIP)
* gzip.min.js: GZIP
* gunzip.min.js: GUNZIP
+ (PKZIP)
* zip.min.js ZIP
* unzip.min.js UNZIP
- node-zlib.js: (ZLIB + GZIP for node.js)
### 圧縮 (Compress)
#### Raw Deflate
```js
// plain = Array.<number> or Uint8Array
var defalte = new Zlib.RawDeflate(plain);
var compressed = deflate.compress();
```
#### Raw Deflate Option
ZLIB Option を参照してください。
#### ZLIB
```js
// plain = Array.<number> or Uint8Array
var defalte = new Zlib.Deflate(plain);
var compressed = deflate.compress();
```
##### ZLIB Option
<code>Zlib.Deflate</code> の第二引数にオブジェクトを渡す事で圧縮オプションを指定する事が出来ます。
```js
{
compressionType: Zlib.Deflate.CompressionType, // 圧縮タイプ
lazy: number // lazy matching の閾値
}
```
<code>Zlib.Deflate.CompressionType</code>
<code>NONE</code>(無圧縮), <code>FIXED</code>(固定ハフマン符号), <code>DYNAMIC</code>(動的ハフマン符号) から選択する事ができます。
default は <code>DYNAMIC</code> です。
<code>lazy</code> は Lazy Matching の閾値を指定します。
Lazy Matching とは、LZSS のマッチ長が閾値より低かった場合、次の Byte から LZSS の最長一致を試み、マッチ長の長い方を選択する手法です。
#### GZIP
GZIP の実装は現在不完全ですが、ただの圧縮コンテナとして使用する場合には特に問題はありません。
zlib.js を用いて作成された GZIP の OS は、自動的に UNKNOWN に設定されます。
```js
// plain = Array.<number> or Uint8Array
var gzip = new Zlib.Gzip(plain);
var compressed = gzip.compress();
```
##### GZIP Option
```js
{
deflateOptions: Object, // deflate option (ZLIB Option 参照)
flags: {
fname: boolean, // ファイル名を使用するか
comment: boolean, // コメントを使用するか
fhcrc: boolean // FHCRC を使用するか
},
filename: string, // flags.fname が true のときに書き込むファイル名
comment: string // flags.comment が true のときに書き込むコメント
}
```
#### PKZIP
PKZIP では複数のファイルを扱うため、他のものとは少し使い方が異なります。
```js
var zip = new Zlib.Zip();
// plainData1
zip.addFile(plainData1, {
filename: stringToByteArray('foo.txt')
});
zip.addFile(plainData2, {
filename: stringToByteArray('bar.txt')
});
zip.addFile(plainData3, {
filename: stringToByteArray('baz.txt')
});
var compressed = zip.compress();
function stringToByteArray(str) {
var array = new (window.Uint8Array !== void 0 ? Uint8Array : Array)(str.length);
var i;
var il;
for (i = 0, il = str.length; i < il; ++i) {
array[i] = str.charCodeAt(i) & 0xff;
}
return array;
}
```
##### PKZIP Option
filename, comment, extraField は Typed Array が使用可能な場合は必ず Uint8Array を使用してください。
```js
{
filename: (Array.<number>|Uint8Array), // ファイル名
comment: (Array.<number>|Uint8Array), // コメント
extraField: (Array.<number>|Uint8Array), // その他の領域
compress: boolean, // addFile メソッドを呼んだときに圧縮するか (通常は compress メソッドの呼び出し時に圧縮)
compressionMethod: Zlib.Zip.CompressionMethod, // STORE or DEFLATE
os: Zlib.Zip.OperatingSystem, // MSDOS or UNIX or MACINTOSH
deflateOption: Object // see: ZLIB Option
}
```
### 伸張 (Decompress)
圧縮されたデータの伸張は、基本的に各コンストラクタに圧縮されたデータを渡し、
それの <code>decompress</code> メソッドを呼ぶ事で伸張処理を開始する事が出来ます。
#### Raw Deflate
```js
// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.RawInflate(compressed);
var plain = inflate.decompress();
```
#### Raw Deflate Option
ZLIB Option を参照してください。
#### ZLIB
```js
// compressed = Array.<number> or Uint8Array
var inflate = new Zlib.Inflate(compressed);
var plain = inflate.decompress();
```
##### ZLIB Option
<code>Zlib.Inflate</code> の第二引数にオブジェクトを渡す事で伸張オプションを指定する事ができます。
```js
{
'index': number, // 入力バッファの開始位置
'bufferSize': number, // 出力バッファの初期サイズ
'bufferType': Zlib.Inflate.BufferType, // バッファの管理方法
'resize': boolean, // 出力バッファのリサイズ
'verify': boolean // 伸張結果の検証を行うか
}
```
<code>Zlib.Inflate.BufferType</code><code>ADAPTIVE</code>(default) か <code>BLOCK</code> を選択する事ができます。
- <code>ADAPTIVE</code> はバッファを伸張後のサイズを予測して一気に拡張しますが、データによっては余分にメモリを使用しすぎる事があります。
- <code>BLOCK</code> では <code>BufferSize</code> ずつ拡張していきますが、動作はあまり速くありません。
<code>resize</code> オプションは Typed Array 利用可能時
<code>decompress</code> メソッドで返却する値の <code>ArrayBuffer</code><code>Uint8Array</code> の長さまで縮小させます。
default は <code>false</code> です。
<code>verify</code> オプションは Adler-32 Checksum の検証を行うかを指定します。
default は <code>false</code> です。
#### GZIP
```js
// compressed = Array.<number> or Uint8Array
var gunzip = new Zlib.Gunzip(compressed);
var plain = gunzip.decompress();
```
Gunzip のオプションは現在ありません。
#### PKZIP
PKZIP の構築と同様に複数ファイルを扱うため、他のものとは少し使い方が異なります。
```js
// compressed = Array.<number> or Uint8Array
var unzip = new Zlib.Unzip(compressed);
var filenames = unzip.getFilenames();
var plain = unzip.decompress(filenames[0]);
```
Unzip のオプションは現在ありません。
### Node.js
Node.js で使用する場合はユニットテストを参照してください。
<https://github.com/imaya/zlib.js/blob/master/test/node-test.js>
## Debug
zlib.js では JavaScript ファイルを minify された形で提供していますが、開発中やデバッグ時に minify する前の状態が知りたい事があります。
そういった時のために SourceMaps ファイルや Pretty Print されたファイルも提供しています。
### SourceMaps
SourceMaps を有効にするには以下のように対象となるファイルに `.map` を付けたファイルと、変換前のソースコードである `src` ディレクトリを配置します。
- inflate.min.js
- inflate.min.js.map
- [src]
- (source files)
なお、ここに書いてある `[src]` は zlib.js のリポジトリの `src` ディレクトリをコピーしてください。
### Pretty Print
SourceMaps とは異なりますが、minify の変数名の短縮のみ避けられれば良いという場合には、 Closure Compiler で読みやすくしたファイルを利用することも可能です。
`zlib.pretty.js` というファイル名で全ての実装がはいっていますので、minify されたものをこのファイルに置き換えるだけで使用できます。
How to build
------------
ビルドは Ant と Closure Compiler を使用して行います。
### 必要な環境
- Ant 1.8+
- JRE 1.6+
- Python
### ビルド
Ant を使ってビルドを行います。
```
$ ant [target]
```
#### ビルドターゲット
target | ファイル名 | 含まれる実装
---------------|----------------------|-------------
deps | deps.js | 依存関係の解決
deflate | deflate.min.js | ZLIB Deflate
inflate | inflate.min.js | ZLIB Inflate
inflate_stream | inlate_stream.min.js | ZLIB Inlate (stream)
zlib | zlib.min.js | ZLIB Deflate + Inflate
gzip | gzip.min.js | GZIP Compression
gunzip | gunzip.min.js | GZIP Decompression
zlib_and_gzip | zlib_and_gzip.min.js | ZLIB + GZIP
node | node-zlib.js | ZLIB + GZIP for node.js
zip | zip.min.js | PKZIP Compression
unzip | unzip.min.js | PKZIP Decompression
all | * | default target
Issue
-----
現在プリセット辞書を用いた圧縮形式には対応していません。
プリセット辞書は通常の圧縮では利用されないため、影響は少ないと思います。
ライセンス
-----------
Copyright &copy; 2012 imaya.
Licensed under the MIT License.

View File

@ -0,0 +1,15 @@
/** @license zlib.js 2012 - imaya [ https://github.com/imaya/zlib.js ] The MIT License */(function() {'use strict';var l=void 0,p=this;function q(c,d){var a=c.split("."),b=p;!(a[0]in b)&&b.execScript&&b.execScript("var "+a[0]);for(var e;a.length&&(e=a.shift());)!a.length&&d!==l?b[e]=d:b=b[e]?b[e]:b[e]={}};var r="undefined"!==typeof Uint8Array&&"undefined"!==typeof Uint16Array&&"undefined"!==typeof Uint32Array;function u(c){var d=c.length,a=0,b=Number.POSITIVE_INFINITY,e,f,g,h,k,m,s,n,t;for(n=0;n<d;++n)c[n]>a&&(a=c[n]),c[n]<b&&(b=c[n]);e=1<<a;f=new (r?Uint32Array:Array)(e);g=1;h=0;for(k=2;g<=a;){for(n=0;n<d;++n)if(c[n]===g){m=0;s=h;for(t=0;t<g;++t)m=m<<1|s&1,s>>=1;for(t=m;t<e;t+=k)f[t]=g<<16|n;++h}++g;h<<=1;k<<=1}return[f,a,b]};function v(c,d){this.g=[];this.h=32768;this.c=this.f=this.d=this.k=0;this.input=r?new Uint8Array(c):c;this.l=!1;this.i=w;this.p=!1;if(d||!(d={}))d.index&&(this.d=d.index),d.bufferSize&&(this.h=d.bufferSize),d.bufferType&&(this.i=d.bufferType),d.resize&&(this.p=d.resize);switch(this.i){case x:this.a=32768;this.b=new (r?Uint8Array:Array)(32768+this.h+258);break;case w:this.a=0;this.b=new (r?Uint8Array:Array)(this.h);this.e=this.u;this.m=this.r;this.j=this.s;break;default:throw Error("invalid inflate mode");
}}var x=0,w=1;
v.prototype.t=function(){for(;!this.l;){var c=y(this,3);c&1&&(this.l=!0);c>>>=1;switch(c){case 0:var d=this.input,a=this.d,b=this.b,e=this.a,f=l,g=l,h=l,k=b.length,m=l;this.c=this.f=0;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: LEN (first byte)");g=f;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: LEN (second byte)");g|=f<<8;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: NLEN (first byte)");h=f;f=d[a++];if(f===l)throw Error("invalid uncompressed block header: NLEN (second byte)");h|=
f<<8;if(g===~h)throw Error("invalid uncompressed block header: length verify");if(a+g>d.length)throw Error("input buffer is broken");switch(this.i){case x:for(;e+g>b.length;){m=k-e;g-=m;if(r)b.set(d.subarray(a,a+m),e),e+=m,a+=m;else for(;m--;)b[e++]=d[a++];this.a=e;b=this.e();e=this.a}break;case w:for(;e+g>b.length;)b=this.e({o:2});break;default:throw Error("invalid inflate mode");}if(r)b.set(d.subarray(a,a+g),e),e+=g,a+=g;else for(;g--;)b[e++]=d[a++];this.d=a;this.a=e;this.b=b;break;case 1:this.j(z,
A);break;case 2:B(this);break;default:throw Error("unknown BTYPE: "+c);}}return this.m()};
var C=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15],D=r?new Uint16Array(C):C,E=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,258,258],F=r?new Uint16Array(E):E,G=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0],H=r?new Uint8Array(G):G,I=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577],J=r?new Uint16Array(I):I,K=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,
13],L=r?new Uint8Array(K):K,M=new (r?Uint8Array:Array)(288),N,O;N=0;for(O=M.length;N<O;++N)M[N]=143>=N?8:255>=N?9:279>=N?7:8;var z=u(M),P=new (r?Uint8Array:Array)(30),Q,R;Q=0;for(R=P.length;Q<R;++Q)P[Q]=5;var A=u(P);function y(c,d){for(var a=c.f,b=c.c,e=c.input,f=c.d,g;b<d;){g=e[f++];if(g===l)throw Error("input buffer is broken");a|=g<<b;b+=8}g=a&(1<<d)-1;c.f=a>>>d;c.c=b-d;c.d=f;return g}
function S(c,d){for(var a=c.f,b=c.c,e=c.input,f=c.d,g=d[0],h=d[1],k,m,s;b<h;){k=e[f++];if(k===l)break;a|=k<<b;b+=8}m=g[a&(1<<h)-1];s=m>>>16;c.f=a>>s;c.c=b-s;c.d=f;return m&65535}
function B(c){function d(a,c,b){var d,f,e,g;for(g=0;g<a;)switch(d=S(this,c),d){case 16:for(e=3+y(this,2);e--;)b[g++]=f;break;case 17:for(e=3+y(this,3);e--;)b[g++]=0;f=0;break;case 18:for(e=11+y(this,7);e--;)b[g++]=0;f=0;break;default:f=b[g++]=d}return b}var a=y(c,5)+257,b=y(c,5)+1,e=y(c,4)+4,f=new (r?Uint8Array:Array)(D.length),g,h,k,m;for(m=0;m<e;++m)f[D[m]]=y(c,3);g=u(f);h=new (r?Uint8Array:Array)(a);k=new (r?Uint8Array:Array)(b);c.j(u(d.call(c,a,g,h)),u(d.call(c,b,g,k)))}
v.prototype.j=function(c,d){var a=this.b,b=this.a;this.n=c;for(var e=a.length-258,f,g,h,k;256!==(f=S(this,c));)if(256>f)b>=e&&(this.a=b,a=this.e(),b=this.a),a[b++]=f;else{g=f-257;k=F[g];0<H[g]&&(k+=y(this,H[g]));f=S(this,d);h=J[f];0<L[f]&&(h+=y(this,L[f]));b>=e&&(this.a=b,a=this.e(),b=this.a);for(;k--;)a[b]=a[b++-h]}for(;8<=this.c;)this.c-=8,this.d--;this.a=b};
v.prototype.s=function(c,d){var a=this.b,b=this.a;this.n=c;for(var e=a.length,f,g,h,k;256!==(f=S(this,c));)if(256>f)b>=e&&(a=this.e(),e=a.length),a[b++]=f;else{g=f-257;k=F[g];0<H[g]&&(k+=y(this,H[g]));f=S(this,d);h=J[f];0<L[f]&&(h+=y(this,L[f]));b+k>e&&(a=this.e(),e=a.length);for(;k--;)a[b]=a[b++-h]}for(;8<=this.c;)this.c-=8,this.d--;this.a=b};
v.prototype.e=function(){var c=new (r?Uint8Array:Array)(this.a-32768),d=this.a-32768,a,b,e=this.b;if(r)c.set(e.subarray(32768,c.length));else{a=0;for(b=c.length;a<b;++a)c[a]=e[a+32768]}this.g.push(c);this.k+=c.length;if(r)e.set(e.subarray(d,d+32768));else for(a=0;32768>a;++a)e[a]=e[d+a];this.a=32768;return e};
v.prototype.u=function(c){var d,a=this.input.length/this.d+1|0,b,e,f,g=this.input,h=this.b;c&&("number"===typeof c.o&&(a=c.o),"number"===typeof c.q&&(a+=c.q));2>a?(b=(g.length-this.d)/this.n[2],f=258*(b/2)|0,e=f<h.length?h.length+f:h.length<<1):e=h.length*a;r?(d=new Uint8Array(e),d.set(h)):d=h;return this.b=d};
v.prototype.m=function(){var c=0,d=this.b,a=this.g,b,e=new (r?Uint8Array:Array)(this.k+(this.a-32768)),f,g,h,k;if(0===a.length)return r?this.b.subarray(32768,this.a):this.b.slice(32768,this.a);f=0;for(g=a.length;f<g;++f){b=a[f];h=0;for(k=b.length;h<k;++h)e[c++]=b[h]}f=32768;for(g=this.a;f<g;++f)e[c++]=d[f];this.g=[];return this.buffer=e};
v.prototype.r=function(){var c,d=this.a;r?this.p?(c=new Uint8Array(d),c.set(this.b.subarray(0,d))):c=this.b.subarray(0,d):(this.b.length>d&&(this.b.length=d),c=this.b);return this.buffer=c};q("Zlib.RawInflate",v);q("Zlib.RawInflate.prototype.decompress",v.prototype.t);var T={ADAPTIVE:w,BLOCK:x},U,V,W,X;if(Object.keys)U=Object.keys(T);else for(V in U=[],W=0,T)U[W++]=V;W=0;for(X=U.length;W<X;++W)V=U[W],q("Zlib.RawInflate.BufferType."+V,T[V]);}).call(this); //@ sourceMappingURL=rawinflate.min.js.map

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,451 @@
<?xml version="1.0" encoding="utf-8"?>
<project name="project" default="all">
<!--
<property name="level" value="ADVANCED_OPTIMIZATIONS"/>
-->
<property name="level" value="PERFORMANCE_OPTIMIZATIONS"/>
<property name="src" value="./src"/>
<property name="def" value="./define"/>
<property name="vendor" value="./vendor"/>
<property name="bin" value="./bin"/>
<property name="compiler" value="${vendor}/google-closure-compiler/compiler.jar"/>
<property name="closure_primitives" value="closure-primitives/base.js"/>
<property name="depswriter" value="closure-primitives/depswriter.py"/>
<property name="depend" value="deps.js"/>
<property name="export" value="./export"/>
<property name="basedir" location="."/>
<!-- ライセンスとビルドされたファイルをプロパティとして読み込む -->
<loadfile property="license" srcfile="./LICENSE_min"/>
<!-- ビルドの事前準備 -->
<target name="prebuild" description="ビルドの事前準備を行う">
<mkdir dir="${bin}"/>
</target>
<!-- 依存関係を解決する -->
<target name="deps" description="依存関係を解決する">
<exec executable="python" failonerror="true">
<arg line="${depswriter}"/>
<arg line="--root_with_prefix=&quot;. ..&quot;"/>
<arg line="--output_file=${depend}"/>
</exec>
<pathconvert property="srcfiles" pathsep=" ">
<map from="${basedir}" to="--js ."/>
<fileset dir="${src}" includes="*.js"/>
</pathconvert>
<echo message="${srcfiles}"/>
<pathconvert property="exportfiles" pathsep=" ">
<map from="${basedir}" to="--js ."/>
<fileset dir="${export}" includes="*.js"/>
</pathconvert>
<echo message="${exportfiles}"/>
</target>
<!-- RawInflate の単体ビルド -->
<target name="raw_inflate" depends="deps,prebuild" description="Inflate の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="rawinflate.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
<arg line="--js=${export}/rawinflate.js"/>
</java>
</target>
<!-- RawInflate の単体ビルド -->
<target name="raw_deflate" depends="deps,prebuild" description="Deflate の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="rawdeflate.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/rawdeflate.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Inflate の単体ビルド -->
<target name="inflate" depends="deps,prebuild" description="Inflate の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="inflate.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/inflate.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Inflate(Stream版) の単体ビルド -->
<target name="inflate_stream" depends="deps,prebuild" description="Inflate(Stream版) の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="inflate_stream.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/inflate_stream.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Gzip の単体ビルド -->
<target name="gzip" depends="deps,prebuild" description="Gzip の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="gzip.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='(function() {%output%}).call(this);'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/gzip.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Gunzip の単体ビルド -->
<target name="gunzip" depends="deps,prebuild" description="Inflate の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="gunzip.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/gunzip.js"/>
<arg line="--js=${export}/gunzip_member.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Deflate の単体ビルド -->
<target name="deflate" depends="deps,prebuild" description="Deflate の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="deflate.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/deflate.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Zlib のビルド -->
<target name="zlib" depends="deps,prebuild" description="リリース版のファイル zlib.min.js を作成する">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="zlib.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/inflate.js"/>
<arg line="--js=${export}/deflate.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Zlib のビルド -->
<target name="zlib_and_gzip" depends="deps,prebuild" description="リリース版のファイル zlib_and_gzip.min.js を作成する">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="zlib_and_gzip.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${export}/inflate.js"/>
<arg line="--js=${export}/deflate.js"/>
<arg line="--js=${export}/gunzip.js"/>
<arg line="--js=${export}/gunzip_member.js"/>
<arg line="--js=${export}/gzip.js"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Node.js 版 Zlib のビルド -->
<target name="node" depends="deps,prebuild" description="create node.js version">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="node-zlib.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--externs=node/externs.js"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=node/exports.js"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Unzip の単体ビルド -->
<target name="unzip" depends="deps,prebuild" description="Unzip の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="unzip.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${export}/unzip.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- Zip の単体ビルド -->
<target name="zip" depends="deps,prebuild" description="Zip の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="zip.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${export}/zip.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- CRC32 の単体ビルド -->
<target name="crc32" depends="deps,prebuild" description="CRC32 の独立ビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="crc32.min.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=${level}"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${export}/crc32.js"/>
<arg line="${srcfiles}"/>
</java>
</target>
<!-- 全てのビルド(デバッグ) -->
<target name="pretty" depends="deps,prebuild" description="デバッグ用に pretty print したもののビルドを行う">
<!-- 出力ファイル名 -->
<local name="basename"/>
<property name="basename" value="zlib.pretty.js"/>
<local name="outfile"/>
<property name="outfile" value="${bin}/${basename}"/>
<!-- ビルド(出力先は一時ファイル) -->
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--compilation_level=WHITESPACE_ONLY"/>
<arg line="--formatting PRETTY_PRINT"/>
<arg line="--warning_level=VERBOSE"/>
<arg line="--define=goog.DEBUG=false"/>
<arg line="--summary_detail_level=3"/>
<arg line="--language_in=ECMASCRIPT5_STRICT"/>
<arg line="--source_map_format=V3"/>
<arg line="--output_wrapper='${license}(function() {%output%}).call(this); //@ sourceMappingURL=${basename}.map'"/>
<arg line="--js_output_file=${outfile}"/>
<arg line="--create_source_map=${outfile}.map"/>
<arg line="--manage_closure_dependencies"/>
<arg line="--js=${closure_primitives}"/>
<arg line="--js=${depend}"/>
<arg line="--js=${def}/typedarray/hybrid.js"/>
<arg line="${srcfiles}"/>
<arg line="${exportfiles}"/>
</java>
</target>
<!-- compiler help -->
<target name="help">
<java jar="${compiler}" fork="true" failonerror="true">
<arg line="--help"/>
</java>
</target>
<!-- 全て作成 -->
<target name="all" depends="raw_deflate,raw_inflate,zlib,zlib_and_gzip,inflate,inflate_stream,deflate,gzip,gunzip,zip,unzip,node" />
<!-- 削除 -->
<target name="clean">
<delete file="${depend}"/>
<delete dir="${bin}"/>
</target>
<!-- リビルド -->
<target name="rebuild" depends="clean,all"/>
</project>
<!-- vim: set expandtab ts=2 sw=2: -->

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,202 @@
#!/usr/bin/env python
#
# Copyright 2009 The Closure Library Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Generates out a Closure deps.js file given a list of JavaScript sources.
Paths can be specified as arguments or (more commonly) specifying trees
with the flags (call with --help for descriptions).
Usage: depswriter.py [path/to/js1.js [path/to/js2.js] ...]
"""
import logging
import optparse
import os
import posixpath
import shlex
import sys
import source
import treescan
__author__ = 'nnaze@google.com (Nathan Naze)'
def MakeDepsFile(source_map):
"""Make a generated deps file.
Args:
source_map: A dict map of the source path to source.Source object.
Returns:
str, A generated deps file source.
"""
# Write in path alphabetical order
paths = sorted(source_map.keys())
lines = []
for path in paths:
js_source = source_map[path]
# We don't need to add entries that don't provide anything.
if js_source.provides:
lines.append(_GetDepsLine(path, js_source))
return ''.join(lines)
def _GetDepsLine(path, js_source):
"""Get a deps.js file string for a source."""
provides = sorted(js_source.provides)
requires = sorted(js_source.requires)
return 'goog.addDependency(\'%s\', %s, %s);\n' % (path, provides, requires)
def _GetOptionsParser():
"""Get the options parser."""
parser = optparse.OptionParser(__doc__)
parser.add_option('--output_file',
dest='output_file',
action='store',
help=('If specified, write output to this path instead of '
'writing to standard output.'))
parser.add_option('--root',
dest='roots',
default=[],
action='append',
help='A root directory to scan for JS source files. '
'Paths of JS files in generated deps file will be '
'relative to this path. This flag may be specified '
'multiple times.')
parser.add_option('--root_with_prefix',
dest='roots_with_prefix',
default=[],
action='append',
help='A root directory to scan for JS source files, plus '
'a prefix (if either contains a space, surround with '
'quotes). Paths in generated deps file will be relative '
'to the root, but preceded by the prefix. This flag '
'may be specified multiple times.')
parser.add_option('--path_with_depspath',
dest='paths_with_depspath',
default=[],
action='append',
help='A path to a source file and an alternate path to '
'the file in the generated deps file (if either contains '
'a space, surround with whitespace). This flag may be '
'specified multiple times.')
return parser
def _NormalizePathSeparators(path):
"""Replaces OS-specific path separators with POSIX-style slashes.
Args:
path: str, A file path.
Returns:
str, The path with any OS-specific path separators (such as backslash on
Windows) replaced with URL-compatible forward slashes. A no-op on systems
that use POSIX paths.
"""
return path.replace(os.sep, posixpath.sep)
def _GetRelativePathToSourceDict(root, prefix=''):
"""Scans a top root directory for .js sources.
Args:
root: str, Root directory.
prefix: str, Prefix for returned paths.
Returns:
dict, A map of relative paths (with prefix, if given), to source.Source
objects.
"""
# Remember and restore the cwd when we're done. We work from the root so
# that paths are relative from the root.
start_wd = os.getcwd()
os.chdir(root)
path_to_source = {}
for path in treescan.ScanTreeForJsFiles('.'):
prefixed_path = _NormalizePathSeparators(os.path.join(prefix, path))
path_to_source[prefixed_path] = source.Source(source.GetFileContents(path))
os.chdir(start_wd)
return path_to_source
def _GetPair(s):
"""Return a string as a shell-parsed tuple. Two values expected."""
try:
# shlex uses '\' as an escape character, so they must be escaped.
s = s.replace('\\', '\\\\')
first, second = shlex.split(s)
return (first, second)
except:
raise Exception('Unable to parse input line as a pair: %s' % s)
def main():
"""CLI frontend to MakeDepsFile."""
logging.basicConfig(format=(sys.argv[0] + ': %(message)s'),
level=logging.INFO)
options, args = _GetOptionsParser().parse_args()
path_to_source = {}
# Roots without prefixes
for root in options.roots:
path_to_source.update(_GetRelativePathToSourceDict(root))
# Roots with prefixes
for root_and_prefix in options.roots_with_prefix:
root, prefix = _GetPair(root_and_prefix)
path_to_source.update(_GetRelativePathToSourceDict(root, prefix=prefix))
# Source paths
for path in args:
path_to_source[path] = source.Source(source.GetFileContents(path))
# Source paths with alternate deps paths
for path_with_depspath in options.paths_with_depspath:
srcpath, depspath = _GetPair(path_with_depspath)
path_to_source[depspath] = source.Source(source.GetFileContents(srcpath))
# Make our output pipe.
if options.output_file:
out = open(options.output_file, 'w')
else:
out = sys.stdout
out.write('// This file was autogenerated by %s.\n' % sys.argv[0])
out.write('// Please do not edit.\n')
out.write(MakeDepsFile(path_to_source))
if __name__ == '__main__':
main()

View File

@ -0,0 +1,114 @@
# Copyright 2009 The Closure Library Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Scans a source JS file for its provided and required namespaces.
Simple class to scan a JavaScript file and express its dependencies.
"""
__author__ = 'nnaze@google.com'
import re
_BASE_REGEX_STRING = '^\s*goog\.%s\(\s*[\'"](.+)[\'"]\s*\)'
_PROVIDE_REGEX = re.compile(_BASE_REGEX_STRING % 'provide')
_REQUIRES_REGEX = re.compile(_BASE_REGEX_STRING % 'require')
# This line identifies base.js and should match the line in that file.
_GOOG_BASE_LINE = (
'var goog = goog || {}; // Identifies this file as the Closure base.')
class Source(object):
"""Scans a JavaScript source for its provided and required namespaces."""
# Matches a "/* ... */" comment.
# Note: We can't definitively distinguish a "/*" in a string literal without a
# state machine tokenizer. We'll assume that a line starting with whitespace
# and "/*" is a comment.
_COMMENT_REGEX = re.compile(
r"""
^\s* # Start of a new line and whitespace
/\* # Opening "/*"
.*? # Non greedy match of any characters (including newlines)
\*/ # Closing "*/""",
re.MULTILINE | re.DOTALL | re.VERBOSE)
def __init__(self, source):
"""Initialize a source.
Args:
source: str, The JavaScript source.
"""
self.provides = set()
self.requires = set()
self._source = source
self._ScanSource()
def __str__(self):
return 'Source %s' % self._path
def GetSource(self):
"""Get the source as a string."""
return self._source
@classmethod
def _StripComments(cls, source):
return cls._COMMENT_REGEX.sub('', source)
def _ScanSource(self):
"""Fill in provides and requires by scanning the source."""
source = self._StripComments(self.GetSource())
source_lines = source.splitlines()
for line in source_lines:
match = _PROVIDE_REGEX.match(line)
if match:
self.provides.add(match.group(1))
match = _REQUIRES_REGEX.match(line)
if match:
self.requires.add(match.group(1))
# Closure's base file implicitly provides 'goog'.
for line in source_lines:
if line == _GOOG_BASE_LINE:
if len(self.provides) or len(self.requires):
raise Exception(
'Base files should not provide or require namespaces.')
self.provides.add('goog')
def GetFileContents(path):
"""Get a file's contents as a string.
Args:
path: str, Path to file.
Returns:
str, Contents of file.
Raises:
IOError: An error occurred opening or reading the file.
"""
fileobj = open(path)
try:
return fileobj.read()
finally:
fileobj.close()

View File

@ -0,0 +1,78 @@
#!/usr/bin/env python
#
# Copyright 2010 The Closure Library Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Shared utility functions for scanning directory trees."""
import os
import re
__author__ = 'nnaze@google.com (Nathan Naze)'
# Matches a .js file path.
_JS_FILE_REGEX = re.compile(r'^.+\.js$')
def ScanTreeForJsFiles(root):
"""Scans a directory tree for JavaScript files.
Args:
root: str, Path to a root directory.
Returns:
An iterable of paths to JS files, relative to cwd.
"""
return ScanTree(root, path_filter=_JS_FILE_REGEX)
def ScanTree(root, path_filter=None, ignore_hidden=True):
"""Scans a directory tree for files.
Args:
root: str, Path to a root directory.
path_filter: A regular expression filter. If set, only paths matching
the path_filter are returned.
ignore_hidden: If True, do not follow or return hidden directories or files
(those starting with a '.' character).
Yields:
A string path to files, relative to cwd.
"""
def OnError(os_error):
raise os_error
for dirpath, dirnames, filenames in os.walk(root, onerror=OnError):
# os.walk allows us to modify dirnames to prevent decent into particular
# directories. Avoid hidden directories.
for dirname in dirnames:
if ignore_hidden and dirname.startswith('.'):
dirnames.remove(dirname)
for filename in filenames:
# nothing that starts with '.'
if ignore_hidden and filename.startswith('.'):
continue
fullpath = os.path.join(dirpath, filename)
if path_filter and not path_filter.match(fullpath):
continue
yield os.path.normpath(fullpath)

View File

@ -0,0 +1,14 @@
/**
* defines
*/
goog.provide('USE_TYPEDARRAY');
// Safari が typeof Uint8Array === 'object' になるため、
// 未定義か否かで Typed Array の使用を決定する
/** @const {boolean} use typed array flag. */
var USE_TYPEDARRAY =
(typeof Uint8Array !== 'undefined') &&
(typeof Uint16Array !== 'undefined') &&
(typeof Uint32Array !== 'undefined');

View File

@ -0,0 +1,9 @@
/**
* typed array defines
*/
goog.provide('USE_TYPEDARRAY');
/** @define {boolean} use typed array flag. */
var USE_TYPEDARRAY = true;

View File

@ -0,0 +1,25 @@
// This file was autogenerated by closure-primitives/depswriter.py.
// Please do not edit.
goog.addDependency('../bin/zlib.pretty.js', ['USE_TYPEDARRAY', 'Zlib', 'Zlib.Adler32', 'Zlib.BitStream', 'Zlib.CRC32', 'Zlib.Deflate', 'Zlib.Gunzip', 'Zlib.GunzipMember', 'Zlib.Gzip', 'Zlib.Heap', 'Zlib.Huffman', 'Zlib.Inflate', 'Zlib.InflateStream', 'Zlib.RawDeflate', 'Zlib.RawInflate', 'Zlib.RawInflateStream', 'Zlib.Unzip', 'Zlib.Util', 'Zlib.Zip', 'Zlib.exportObject'], ['USE_TYPEDARRAY', 'Zlib', 'Zlib.Adler32', 'Zlib.BitStream', 'Zlib.CRC32', 'Zlib.Deflate', 'Zlib.Gunzip', 'Zlib.GunzipMember', 'Zlib.Gzip', 'Zlib.Heap', 'Zlib.Huffman', 'Zlib.Inflate', 'Zlib.InflateStream', 'Zlib.RawDeflate', 'Zlib.RawInflate', 'Zlib.RawInflateStream', 'Zlib.Unzip', 'Zlib.Util', 'Zlib.Zip', 'Zlib.exportObject']);
goog.addDependency('../closure-primitives/base.js', ['goog'], []);
goog.addDependency('../define/typedarray/hybrid.js', ['USE_TYPEDARRAY'], []);
goog.addDependency('../define/typedarray/use.js', ['USE_TYPEDARRAY'], []);
goog.addDependency('../src/adler32.js', ['Zlib.Adler32'], ['USE_TYPEDARRAY', 'Zlib.Util']);
goog.addDependency('../src/bitstream.js', ['Zlib.BitStream'], ['USE_TYPEDARRAY']);
goog.addDependency('../src/crc32.js', ['Zlib.CRC32'], ['USE_TYPEDARRAY']);
goog.addDependency('../src/deflate.js', ['Zlib.Deflate'], ['USE_TYPEDARRAY', 'Zlib', 'Zlib.Adler32', 'Zlib.RawDeflate']);
goog.addDependency('../src/export_object.js', ['Zlib.exportObject'], ['Zlib']);
goog.addDependency('../src/gunzip.js', ['Zlib.Gunzip'], ['USE_TYPEDARRAY', 'Zlib.CRC32', 'Zlib.GunzipMember', 'Zlib.Gzip', 'Zlib.RawInflate']);
goog.addDependency('../src/gunzip_member.js', ['Zlib.GunzipMember'], []);
goog.addDependency('../src/gzip.js', ['Zlib.Gzip'], ['USE_TYPEDARRAY', 'Zlib.CRC32', 'Zlib.RawDeflate']);
goog.addDependency('../src/heap.js', ['Zlib.Heap'], ['USE_TYPEDARRAY']);
goog.addDependency('../src/huffman.js', ['Zlib.Huffman'], ['USE_TYPEDARRAY']);
goog.addDependency('../src/inflate.js', ['Zlib.Inflate'], ['USE_TYPEDARRAY', 'Zlib.Adler32', 'Zlib.RawInflate']);
goog.addDependency('../src/inflate_stream.js', ['Zlib.InflateStream'], ['USE_TYPEDARRAY', 'Zlib', 'Zlib.RawInflateStream']);
goog.addDependency('../src/rawdeflate.js', ['Zlib.RawDeflate'], ['USE_TYPEDARRAY', 'Zlib.BitStream', 'Zlib.Heap']);
goog.addDependency('../src/rawinflate.js', ['Zlib.RawInflate'], ['USE_TYPEDARRAY', 'Zlib.Huffman']);
goog.addDependency('../src/rawinflate_stream.js', ['Zlib.RawInflateStream'], ['USE_TYPEDARRAY', 'Zlib.Huffman']);
goog.addDependency('../src/unzip.js', ['Zlib.Unzip'], ['USE_TYPEDARRAY', 'Zlib.CRC32', 'Zlib.RawInflate', 'Zlib.Zip']);
goog.addDependency('../src/util.js', ['Zlib.Util'], []);
goog.addDependency('../src/zip.js', ['Zlib.Zip'], ['USE_TYPEDARRAY', 'Zlib.CRC32', 'Zlib.RawDeflate']);
goog.addDependency('../src/zlib.js', ['Zlib'], []);

View File

@ -0,0 +1,4 @@
goog.require('Zlib.Adler32');
goog.exportSymbol('Zlib.Adler32', Zlib.Adler32);
goog.exportSymbol('Zlib.Adler32.update', Zlib.Adler32.update);

View File

@ -0,0 +1,5 @@
goog.require('Zlib.CRC32');
goog.exportSymbol('Zlib.CRC32', Zlib.CRC32);
goog.exportSymbol('Zlib.CRC32.calc', Zlib.CRC32.calc);
goog.exportSymbol('Zlib.CRC32.update', Zlib.CRC32.update);

View File

@ -0,0 +1,17 @@
goog.require('Zlib.Deflate');
goog.require('Zlib.exportObject');
goog.exportSymbol('Zlib.Deflate', Zlib.Deflate);
goog.exportSymbol(
'Zlib.Deflate.compress',
Zlib.Deflate.compress
);
goog.exportSymbol(
'Zlib.Deflate.prototype.compress',
Zlib.Deflate.prototype.compress
);
Zlib.exportObject('Zlib.Deflate.CompressionType', {
'NONE': Zlib.Deflate.CompressionType.NONE,
'FIXED': Zlib.Deflate.CompressionType.FIXED,
'DYNAMIC': Zlib.Deflate.CompressionType.DYNAMIC
});

View File

@ -0,0 +1,11 @@
goog.require('Zlib.Gunzip');
goog.exportSymbol('Zlib.Gunzip', Zlib.Gunzip);
goog.exportSymbol(
'Zlib.Gunzip.prototype.decompress',
Zlib.Gunzip.prototype.decompress
);
goog.exportSymbol(
'Zlib.Gunzip.prototype.getMembers',
Zlib.Gunzip.prototype.getMembers
);

View File

@ -0,0 +1,15 @@
goog.require('Zlib.GunzipMember');
goog.exportSymbol('Zlib.GunzipMember', Zlib.GunzipMember);
goog.exportSymbol(
'Zlib.GunzipMember.prototype.getName',
Zlib.GunzipMember.prototype.getName
);
goog.exportSymbol(
'Zlib.GunzipMember.prototype.getData',
Zlib.GunzipMember.prototype.getData
);
goog.exportSymbol(
'Zlib.GunzipMember.prototype.getMtime',
Zlib.GunzipMember.prototype.getMtime
);

View File

@ -0,0 +1,7 @@
goog.require('Zlib.Gzip');
goog.exportSymbol('Zlib.Gzip', Zlib.Gzip);
goog.exportSymbol(
'Zlib.Gzip.prototype.compress',
Zlib.Gzip.prototype.compress
);

View File

@ -0,0 +1,12 @@
goog.require('Zlib.Inflate');
goog.require('Zlib.exportObject');
goog.exportSymbol('Zlib.Inflate', Zlib.Inflate);
goog.exportSymbol(
'Zlib.Inflate.prototype.decompress',
Zlib.Inflate.prototype.decompress
);
Zlib.exportObject('Zlib.Inflate.BufferType', {
'ADAPTIVE': Zlib.Inflate.BufferType.ADAPTIVE,
'BLOCK': Zlib.Inflate.BufferType.BLOCK
});

View File

@ -0,0 +1,11 @@
goog.require('Zlib.InflateStream');
goog.exportSymbol('Zlib.InflateStream', Zlib.InflateStream);
goog.exportSymbol(
'Zlib.InflateStream.prototype.decompress',
Zlib.InflateStream.prototype.decompress
);
goog.exportSymbol(
'Zlib.InflateStream.prototype.getBytes',
Zlib.InflateStream.prototype.getBytes
);

View File

@ -0,0 +1,21 @@
goog.require('Zlib.RawDeflate');
goog.require('Zlib.exportObject');
goog.exportSymbol(
'Zlib.RawDeflate',
Zlib.RawDeflate
);
goog.exportSymbol(
'Zlib.RawDeflate.prototype.compress',
Zlib.RawDeflate.prototype.compress
);
Zlib.exportObject(
'Zlib.RawDeflate.CompressionType',
{
'NONE': Zlib.RawDeflate.CompressionType.NONE,
'FIXED': Zlib.RawDeflate.CompressionType.FIXED,
'DYNAMIC': Zlib.RawDeflate.CompressionType.DYNAMIC
}
);

View File

@ -0,0 +1,12 @@
goog.require('Zlib.RawInflate');
goog.require('Zlib.exportObject');
goog.exportSymbol('Zlib.RawInflate', Zlib.RawInflate);
goog.exportSymbol(
'Zlib.RawInflate.prototype.decompress',
Zlib.RawInflate.prototype.decompress
);
Zlib.exportObject('Zlib.RawInflate.BufferType', {
'ADAPTIVE': Zlib.RawInflate.BufferType.ADAPTIVE,
'BLOCK': Zlib.RawInflate.BufferType.BLOCK
});

View File

@ -0,0 +1,11 @@
goog.require('Zlib.RawInflateStream');
goog.exportSymbol('Zlib.RawInflateStream', Zlib.RawInflateStream);
goog.exportSymbol(
'Zlib.RawInflateStream.prototype.decompress',
Zlib.RawInflateStream.prototype.decompress
);
goog.exportSymbol(
'Zlib.RawInflateStream.prototype.getBytes',
Zlib.RawInflateStream.prototype.getBytes
);

View File

@ -0,0 +1,15 @@
goog.require('Zlib.Unzip');
goog.exportSymbol('Zlib.Unzip', Zlib.Unzip);
goog.exportSymbol(
'Zlib.Unzip.prototype.decompress',
Zlib.Unzip.prototype.decompress
);
goog.exportSymbol(
'Zlib.Unzip.prototype.getFilenames',
Zlib.Unzip.prototype.getFilenames
);
goog.exportSymbol(
'Zlib.Unzip.prototype.setPassword',
Zlib.Unzip.prototype.setPassword
);

View File

@ -0,0 +1,33 @@
goog.require('Zlib.Zip');
goog.require('Zlib.exportObject');
goog.exportSymbol(
'Zlib.Zip',
Zlib.Zip
);
goog.exportSymbol(
'Zlib.Zip.prototype.addFile',
Zlib.Zip.prototype.addFile
);
goog.exportSymbol(
'Zlib.Zip.prototype.compress',
Zlib.Zip.prototype.compress
);
goog.exportSymbol(
'Zlib.Zip.prototype.setPassword',
Zlib.Zip.prototype.setPassword
);
Zlib.exportObject(
'Zlib.Zip.CompressionMethod', {
'STORE': Zlib.Zip.CompressionMethod.STORE,
'DEFLATE': Zlib.Zip.CompressionMethod.DEFLATE
}
);
Zlib.exportObject(
'Zlib.Zip.OperatingSystem', {
'MSDOS': Zlib.Zip.OperatingSystem.MSDOS,
'UNIX': Zlib.Zip.OperatingSystem.UNIX,
'MACINTOSH': Zlib.Zip.OperatingSystem.MACINTOSH
}
);
// TODO: Deflate Option

View File

@ -0,0 +1,230 @@
/**
* zlib.js wrapper for node.js
*/
goog.require('Zlib.Inflate');
goog.require('Zlib.Deflate');
goog.require('Zlib.Gunzip');
//-----------------------------------------------------------------------------
// exports methods
//-----------------------------------------------------------------------------
exports['deflate'] = deflate;
exports['deflateSync'] = deflateSync;
exports['inflate'] = inflate;
exports['inflateSync'] = inflateSync;
exports['gzip'] = gzip;
exports['gzipSync'] = gzipSync;
exports['gunzip'] = gunzip;
exports['gunzipSync'] = gunzipSync;
/**
* deflate async.
* @param {!(Buffer|Array.<number>|Uint8Array)} buffer plain data buffer.
* @param {function(Error, !(Buffer|Array.<number>|Uint8Array))} callback
* error calllback function.
* @param {Object=} opt_params option parameters.
*/
function deflate(buffer, callback, opt_params) {
process.nextTick(function(){
/** @type {Error} error */
var error;
/** @type {!(Buffer|Array.<number>|Uint8Array)} deflated buffer. */
var deflated;
try {
deflated = deflateSync(buffer, opt_params);
} catch(e){
error = e;
}
callback(error, deflated);
});
}
/**
* deflate sync.
* @param {!(Buffer|Array.<number>|Uint8Array)} buffer plain data buffer.
* @param {Object=} opt_params option parameters.
* @return {!(Buffer|Array.<number>|Uint8Array)} deflated buffer.
*/
function deflateSync(buffer, opt_params) {
/** @type {Zlib.Deflate} deflate encoder. */
var deflate = new Zlib.Deflate(
/** @type {!(Array.<number>|Uint8Array)} */(buffer)
);
/** @type {!(Array.<number>|Uint8Array)} deflated buffer. */
var deflated;
deflated = deflate.compress();
if (!opt_params) {
opt_params = {};
}
return opt_params.noBuffer ? deflated : toBuffer(deflated);
}
/**
* inflate async.
* @param {!(Array.<number>|Uint8Array)} buffer deflated buffer.
* @param {function(Error, !(Buffer|Array.<number>|Uint8Array))} callback
* error calllback function.
* @param {Object=} opt_params option parameters.
*/
function inflate(buffer, callback, opt_params) {
process.nextTick(function(){
/** @type {Error} error */
var error;
/** @type {!(Buffer|Array.<number>|Uint8Array)} inflated plain buffer. */
var inflated;
try {
inflated = inflateSync(buffer, opt_params);
} catch(e){
error = e;
}
callback(error, inflated);
});
};
/**
* inflate sync.
* @param {!(Array.<number>|Uint8Array)} buffer deflated buffer.
* @param {Object=} opt_params option parameters.
* @return {!(Buffer|Array.<number>|Uint8Array)} inflated plain buffer.
*/
function inflateSync(buffer, opt_params) {
/** @type {Zlib.Inflate} deflate decoder. */
var inflate;
/** @type {!(Buffer|Array.<number>|Uint8Array)} inflated plain buffer. */
var inflated;
buffer.subarray = buffer.slice;
inflate = new Zlib.Inflate(buffer);
inflated = inflate.decompress();
if (!opt_params) {
opt_params = {};
}
return opt_params['noBuffer'] ? inflated : toBuffer(inflated);
}
/**
* gunzip async.
* @param {!(Array.<number>|Uint8Array)} buffer inflated buffer.
* @param {function(Error, !(Buffer|Array.<number>|Uint8Array))} callback
* error calllback function.
* @param {Object=} opt_params option parameters.
*/
function gzip(buffer, callback, opt_params) {
process.nextTick(function(){
/** @type {Error} error */
var error;
/** @type {!(Buffer|Array.<number>|Uint8Array)} deflated buffer. */
var deflated;
try {
deflated = gzipSync(buffer, opt_params);
} catch(e){
error = e;
}
callback(error, deflated);
});
}
/**
* deflate sync.
* @param {!(Array.<number>|Uint8Array)} buffer inflated buffer.
* @param {Object=} opt_params option parameters.
* @return {!(Buffer|Array.<number>|Uint8Array)} deflated buffer.
*/
function gzipSync(buffer, opt_params) {
/** @type {Zlib.Gzip} deflate compressor. */
var deflate;
/** @type {!(Buffer|Array.<number>|Uint8Array)} deflated buffer. */
var deflated;
buffer.subarray = buffer.slice;
deflate = new Zlib.Gzip(buffer);
deflated = deflate.compress();
if (!opt_params) {
opt_params = {};
}
return opt_params.noBuffer ? deflated : toBuffer(deflated);
}
/**
* gunzip async.
* @param {!(Array.<number>|Uint8Array)} buffer deflated buffer.
* @param {function(Error, !(Buffer|Array.<number>|Uint8Array))} callback
* error calllback function.
* @param {Object=} opt_params option parameters.
*/
function gunzip(buffer, callback, opt_params) {
process.nextTick(function(){
/** @type {Error} error */
var error;
/** @type {!(Buffer|Array.<number>|Uint8Array)} inflated plain buffer. */
var inflated;
try {
inflated = gunzipSync(buffer, opt_params);
} catch(e){
error = e;
}
callback(error, inflated);
});
}
/**
* inflate sync.
* @param {!(Array.<number>|Uint8Array)} buffer deflated buffer.
* @param {Object=} opt_params option parameters.
* @return {!(Buffer|Array.<number>|Uint8Array)} inflated plain buffer.
*/
function gunzipSync(buffer, opt_params) {
/** @type {Zlib.Gunzip} deflate decompressor. */
var inflate;
/** @type {!(Buffer|Array.<number>|Uint8Array)} inflated plain buffer. */
var inflated;
buffer.subarray = buffer.slice;
inflate = new Zlib.Gunzip(buffer);
inflated = inflate.decompress();
if (!opt_params) {
opt_params = {};
}
return opt_params.noBuffer ? inflated : toBuffer(inflated);
}
/**
* convert to Buffer.
* @param {!(Array.<number>|Uint8Array)} array arraylike object.
* @return {!Buffer} Buffer object.
*/
function toBuffer(array) {
var buffer = new Buffer(array.length);
var i;
var il;
// TODO: loop unrolling for performance
for (i = 0, il = array.length; i < il; ++i) {
buffer[i] = array[i];
}
return buffer;
}

View File

@ -0,0 +1,13 @@
/** @type {Object} namespace. */
var exports;
/** @type {Object} namespace. */
var process = {};
/** @param {function()} func exec function. */
process.nextTick = function(func) {};
/** @constructor */
var Buffer = function(arg){};
var console = {};
console.log = function(){};

View File

@ -0,0 +1,29 @@
{
"author": {
"name": "Yuta Imaya"
},
"contributors": {
"name": "Yuta Imaya",
"email": "imaya.devel@gmail.com"
},
"name": "zlibjs",
"description": "zlib, gzip and zip implementation in JavaScript",
"version": "0.1.6",
"main": "./bin/node-zlib.js",
"homepage": "https://github.com/imaya/zlib.js",
"repository": {
"type": "git",
"url": "git://github.com/imaya/zlib.js.git"
},
"dependencies" : {
"buster" : ">=0.6.3"
},
"devDependencies": {},
"optionalDependencies": {},
"engines": {
"node": "*"
},
"scripts" : {
"test" : "node_modules/.bin/buster-test"
}
}

View File

@ -0,0 +1,68 @@
/**
* @fileoverview Adler32 checksum 実装.
*/
goog.provide('Zlib.Adler32');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.Util');
goog.scope(function() {
/**
* Adler32 ハッシュ値の作成
* @param {!(Array|Uint8Array|string)} array 算出に使用する byte array.
* @return {number} Adler32 ハッシュ値.
*/
Zlib.Adler32 = function(array) {
if (typeof(array) === 'string') {
array = Zlib.Util.stringToByteArray(array);
}
return Zlib.Adler32.update(1, array);
};
/**
* Adler32 ハッシュ値の更新
* @param {number} adler 現在のハッシュ値.
* @param {!(Array|Uint8Array)} array 更新に使用する byte array.
* @return {number} Adler32 ハッシュ値.
*/
Zlib.Adler32.update = function(adler, array) {
/** @type {number} */
var s1 = adler & 0xffff;
/** @type {number} */
var s2 = (adler >>> 16) & 0xffff;
/** @type {number} array length */
var len = array.length;
/** @type {number} loop length (don't overflow) */
var tlen;
/** @type {number} array index */
var i = 0;
while (len > 0) {
tlen = len > Zlib.Adler32.OptimizationParameter ?
Zlib.Adler32.OptimizationParameter : len;
len -= tlen;
do {
s1 += array[i++];
s2 += s1;
} while (--tlen);
s1 %= 65521;
s2 %= 65521;
}
return ((s2 << 16) | s1) >>> 0;
};
/**
* Adler32 最適化パラメータ
* 現状では 1024 程度が最適.
* @see http://jsperf.com/adler-32-simple-vs-optimized/3
* @define {number}
*/
Zlib.Adler32.OptimizationParameter = 1024;
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,202 @@
/**
* @fileoverview bit 単位での書き込み実装.
*/
goog.provide('Zlib.BitStream');
goog.require('USE_TYPEDARRAY');
goog.scope(function() {
/**
* ビットストリーム
* @constructor
* @param {!(Array|Uint8Array)=} buffer output buffer.
* @param {number=} bufferPosition start buffer pointer.
*/
Zlib.BitStream = function(buffer, bufferPosition) {
/** @type {number} buffer index. */
this.index = typeof bufferPosition === 'number' ? bufferPosition : 0;
/** @type {number} bit index. */
this.bitindex = 0;
/** @type {!(Array|Uint8Array)} bit-stream output buffer. */
this.buffer = buffer instanceof (USE_TYPEDARRAY ? Uint8Array : Array) ?
buffer :
new (USE_TYPEDARRAY ? Uint8Array : Array)(Zlib.BitStream.DefaultBlockSize);
// 入力された index が足りなかったら拡張するが、倍にしてもダメなら不正とする
if (this.buffer.length * 2 <= this.index) {
throw new Error("invalid index");
} else if (this.buffer.length <= this.index) {
this.expandBuffer();
}
};
/**
* デフォルトブロックサイズ.
* @const
* @type {number}
*/
Zlib.BitStream.DefaultBlockSize = 0x8000;
/**
* expand buffer.
* @return {!(Array|Uint8Array)} new buffer.
*/
Zlib.BitStream.prototype.expandBuffer = function() {
/** @type {!(Array|Uint8Array)} old buffer. */
var oldbuf = this.buffer;
/** @type {number} loop counter. */
var i;
/** @type {number} loop limiter. */
var il = oldbuf.length;
/** @type {!(Array|Uint8Array)} new buffer. */
var buffer =
new (USE_TYPEDARRAY ? Uint8Array : Array)(il << 1);
// copy buffer
if (USE_TYPEDARRAY) {
buffer.set(oldbuf);
} else {
// XXX: loop unrolling
for (i = 0; i < il; ++i) {
buffer[i] = oldbuf[i];
}
}
return (this.buffer = buffer);
};
/**
* 数値をビットで指定した数だけ書き込む.
* @param {number} number 書き込む数値.
* @param {number} n 書き込むビット数.
* @param {boolean=} reverse 逆順に書き込むならば true.
*/
Zlib.BitStream.prototype.writeBits = function(number, n, reverse) {
var buffer = this.buffer;
var index = this.index;
var bitindex = this.bitindex;
/** @type {number} current octet. */
var current = buffer[index];
/** @type {number} loop counter. */
var i;
/**
* 32-bit 整数のビット順を逆にする
* @param {number} n 32-bit integer.
* @return {number} reversed 32-bit integer.
* @private
*/
function rev32_(n) {
return (Zlib.BitStream.ReverseTable[n & 0xFF] << 24) |
(Zlib.BitStream.ReverseTable[n >>> 8 & 0xFF] << 16) |
(Zlib.BitStream.ReverseTable[n >>> 16 & 0xFF] << 8) |
Zlib.BitStream.ReverseTable[n >>> 24 & 0xFF];
}
if (reverse && n > 1) {
number = n > 8 ?
rev32_(number) >> (32 - n) :
Zlib.BitStream.ReverseTable[number] >> (8 - n);
}
// Byte 境界を超えないとき
if (n + bitindex < 8) {
current = (current << n) | number;
bitindex += n;
// Byte 境界を超えるとき
} else {
for (i = 0; i < n; ++i) {
current = (current << 1) | ((number >> n - i - 1) & 1);
// next byte
if (++bitindex === 8) {
bitindex = 0;
buffer[index++] = Zlib.BitStream.ReverseTable[current];
current = 0;
// expand
if (index === buffer.length) {
buffer = this.expandBuffer();
}
}
}
}
buffer[index] = current;
this.buffer = buffer;
this.bitindex = bitindex;
this.index = index;
};
/**
* ストリームの終端処理を行う
* @return {!(Array|Uint8Array)} 終端処理後のバッファを byte array で返す.
*/
Zlib.BitStream.prototype.finish = function() {
var buffer = this.buffer;
var index = this.index;
/** @type {!(Array|Uint8Array)} output buffer. */
var output;
// bitindex が 0 の時は余分に index が進んでいる状態
if (this.bitindex > 0) {
buffer[index] <<= 8 - this.bitindex;
buffer[index] = Zlib.BitStream.ReverseTable[buffer[index]];
index++;
}
// array truncation
if (USE_TYPEDARRAY) {
output = buffer.subarray(0, index);
} else {
buffer.length = index;
output = buffer;
}
return output;
};
/**
* 0-255 のビット順を反転したテーブル
* @const
* @type {!(Uint8Array|Array.<number>)}
*/
Zlib.BitStream.ReverseTable = (function(table) {
return table;
})((function() {
/** @type {!(Array|Uint8Array)} reverse table. */
var table = new (USE_TYPEDARRAY ? Uint8Array : Array)(256);
/** @type {number} loop counter. */
var i;
/** @type {number} loop limiter. */
var il;
// generate
for (i = 0; i < 256; ++i) {
table[i] = (function(n) {
var r = n;
var s = 7;
for (n >>>= 1; n; n >>>= 1) {
r <<= 1;
r |= n & 1;
--s;
}
return (r << s & 0xff) >>> 0;
})(i);
}
return table;
})());
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,142 @@
/**
* @fileoverview CRC32 実装.
*/
goog.provide('Zlib.CRC32');
goog.require('USE_TYPEDARRAY');
/** @define {boolean} */
var ZLIB_CRC32_COMPACT = false;
goog.scope(function() {
/**
* CRC32 ハッシュ値を取得
* @param {!(Array.<number>|Uint8Array)} data data byte array.
* @param {number=} pos data position.
* @param {number=} length data length.
* @return {number} CRC32.
*/
Zlib.CRC32.calc = function(data, pos, length) {
return Zlib.CRC32.update(data, 0, pos, length);
};
/**
* CRC32ハッシュ値を更新
* @param {!(Array.<number>|Uint8Array)} data data byte array.
* @param {number} crc CRC32.
* @param {number=} pos data position.
* @param {number=} length data length.
* @return {number} CRC32.
*/
Zlib.CRC32.update = function(data, crc, pos, length) {
var table = Zlib.CRC32.Table;
var i = (typeof pos === 'number') ? pos : (pos = 0);
var il = (typeof length === 'number') ? length : data.length;
crc ^= 0xffffffff;
// loop unrolling for performance
for (i = il & 7; i--; ++pos) {
crc = (crc >>> 8) ^ table[(crc ^ data[pos]) & 0xff];
}
for (i = il >> 3; i--; pos += 8) {
crc = (crc >>> 8) ^ table[(crc ^ data[pos ]) & 0xff];
crc = (crc >>> 8) ^ table[(crc ^ data[pos + 1]) & 0xff];
crc = (crc >>> 8) ^ table[(crc ^ data[pos + 2]) & 0xff];
crc = (crc >>> 8) ^ table[(crc ^ data[pos + 3]) & 0xff];
crc = (crc >>> 8) ^ table[(crc ^ data[pos + 4]) & 0xff];
crc = (crc >>> 8) ^ table[(crc ^ data[pos + 5]) & 0xff];
crc = (crc >>> 8) ^ table[(crc ^ data[pos + 6]) & 0xff];
crc = (crc >>> 8) ^ table[(crc ^ data[pos + 7]) & 0xff];
}
return (crc ^ 0xffffffff) >>> 0;
};
/**
* @param {number} num
* @param {number} crc
* @returns {number}
*/
Zlib.CRC32.single = function(num, crc) {
return (Zlib.CRC32.Table[(num ^ crc) & 0xff] ^ (num >>> 8)) >>> 0;
};
/**
* @type {Array.<number>}
* @const
* @private
*/
Zlib.CRC32.Table_ = [
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
];
/**
* @type {!(Array.<number>|Uint32Array)} CRC-32 Table.
* @const
*/
Zlib.CRC32.Table = ZLIB_CRC32_COMPACT ? (function() {
/** @type {!(Array.<number>|Uint32Array)} */
var table = new (USE_TYPEDARRAY ? Uint32Array : Array)(256);
/** @type {number} */
var c;
/** @type {number} */
var i;
/** @type {number} */
var j;
for (i = 0; i < 256; ++i) {
c = i;
for (j = 0; j < 8; ++j) {
c = (c & 1) ? (0xedB88320 ^ (c >>> 1)) : (c >>> 1);
}
table[i] = c >>> 0;
}
return table;
})() : USE_TYPEDARRAY ? new Uint32Array(Zlib.CRC32.Table_) : Zlib.CRC32.Table_;
});

View File

@ -0,0 +1,168 @@
/**
* @fileoverview Deflate (RFC1951) 実装.
* Deflateアルゴリズム本体は Zlib.RawDeflate で実装されている.
*/
goog.provide('Zlib.Deflate');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib');
goog.require('Zlib.Adler32');
goog.require('Zlib.RawDeflate');
goog.scope(function() {
/**
* Zlib Deflate
* @constructor
* @param {!(Array|Uint8Array)} input 符号化する対象の byte array.
* @param {Object=} opt_params option parameters.
*/
Zlib.Deflate = function(input, opt_params) {
/** @type {!(Array|Uint8Array)} */
this.input = input;
/** @type {!(Array|Uint8Array)} */
this.output =
new (USE_TYPEDARRAY ? Uint8Array : Array)(Zlib.Deflate.DefaultBufferSize);
/** @type {Zlib.Deflate.CompressionType} */
this.compressionType = Zlib.Deflate.CompressionType.DYNAMIC;
/** @type {Zlib.RawDeflate} */
this.rawDeflate;
/** @type {Object} */
var rawDeflateOption = {};
/** @type {string} */
var prop;
// option parameters
if (opt_params || !(opt_params = {})) {
if (typeof opt_params['compressionType'] === 'number') {
this.compressionType = opt_params['compressionType'];
}
}
// copy options
for (prop in opt_params) {
rawDeflateOption[prop] = opt_params[prop];
}
// set raw-deflate output buffer
rawDeflateOption['outputBuffer'] = this.output;
this.rawDeflate = new Zlib.RawDeflate(this.input, rawDeflateOption);
};
/**
* @const
* @type {number} デフォルトバッファサイズ.
*/
Zlib.Deflate.DefaultBufferSize = 0x8000;
/**
* @enum {number}
*/
Zlib.Deflate.CompressionType = Zlib.RawDeflate.CompressionType;
/**
* 直接圧縮に掛ける.
* @param {!(Array|Uint8Array)} input target buffer.
* @param {Object=} opt_params option parameters.
* @return {!(Array|Uint8Array)} compressed data byte array.
*/
Zlib.Deflate.compress = function(input, opt_params) {
return (new Zlib.Deflate(input, opt_params)).compress();
};
/**
* Deflate Compression.
* @return {!(Array|Uint8Array)} compressed data byte array.
*/
Zlib.Deflate.prototype.compress = function() {
/** @type {Zlib.CompressionMethod} */
var cm;
/** @type {number} */
var cinfo;
/** @type {number} */
var cmf;
/** @type {number} */
var flg;
/** @type {number} */
var fcheck;
/** @type {number} */
var fdict;
/** @type {number} */
var flevel;
/** @type {number} */
var clevel;
/** @type {number} */
var adler;
/** @type {boolean} */
var error = false;
/** @type {!(Array|Uint8Array)} */
var output;
/** @type {number} */
var pos = 0;
output = this.output;
// Compression Method and Flags
cm = Zlib.CompressionMethod.DEFLATE;
switch (cm) {
case Zlib.CompressionMethod.DEFLATE:
cinfo = Math.LOG2E * Math.log(Zlib.RawDeflate.WindowSize) - 8;
break;
default:
throw new Error('invalid compression method');
}
cmf = (cinfo << 4) | cm;
output[pos++] = cmf;
// Flags
fdict = 0;
switch (cm) {
case Zlib.CompressionMethod.DEFLATE:
switch (this.compressionType) {
case Zlib.Deflate.CompressionType.NONE: flevel = 0; break;
case Zlib.Deflate.CompressionType.FIXED: flevel = 1; break;
case Zlib.Deflate.CompressionType.DYNAMIC: flevel = 2; break;
default: throw new Error('unsupported compression type');
}
break;
default:
throw new Error('invalid compression method');
}
flg = (flevel << 6) | (fdict << 5);
fcheck = 31 - (cmf * 256 + flg) % 31;
flg |= fcheck;
output[pos++] = flg;
// Adler-32 checksum
adler = Zlib.Adler32(this.input);
this.rawDeflate.op = pos;
output = this.rawDeflate.compress();
pos = output.length;
if (USE_TYPEDARRAY) {
// subarray 分を元にもどす
output = new Uint8Array(output.buffer);
// expand buffer
if (output.length <= pos + 4) {
this.output = new Uint8Array(output.length + 4);
this.output.set(output);
output = this.output;
}
output = output.subarray(0, pos + 4);
}
// adler32
output[pos++] = (adler >> 24) & 0xff;
output[pos++] = (adler >> 16) & 0xff;
output[pos++] = (adler >> 8) & 0xff;
output[pos++] = (adler ) & 0xff;
return output;
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,33 @@
goog.provide('Zlib.exportObject');
goog.require('Zlib');
goog.scope(function() {
Zlib.exportObject = function(enumString, exportKeyValue) {
/** @type {Array.<string>} */
var keys;
/** @type {string} */
var key;
/** @type {number} */
var i;
/** @type {number} */
var il;
if (Object.keys) {
keys = Object.keys(exportKeyValue);
} else {
keys = [];
i = 0;
for (key in exportKeyValue) {
keys[i++] = key;
}
}
for (i = 0, il = keys.length; i < il; ++i) {
key = keys[i];
goog.exportSymbol(enumString + '.' + key, exportKeyValue[key])
}
};
});

View File

@ -0,0 +1,238 @@
/**
* @fileoverview GZIP (RFC1952) 展開コンテナ実装.
*/
goog.provide('Zlib.Gunzip');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.CRC32');
goog.require('Zlib.Gzip');
goog.require('Zlib.RawInflate');
goog.require('Zlib.GunzipMember');
goog.scope(function() {
/**
* @constructor
* @param {!(Array|Uint8Array)} input input buffer.
* @param {Object=} opt_params option parameters.
*/
Zlib.Gunzip = function(input, opt_params) {
/** @type {!(Array.<number>|Uint8Array)} input buffer. */
this.input = input;
/** @type {number} input buffer pointer. */
this.ip = 0;
/** @type {Array.<Zlib.GunzipMember>} */
this.member = [];
/** @type {boolean} */
this.decompressed = false;
};
/**
* @return {Array.<Zlib.GunzipMember>}
*/
Zlib.Gunzip.prototype.getMembers = function() {
if (!this.decompressed) {
this.decompress();
}
return this.member.slice();
};
/**
* inflate gzip data.
* @return {!(Array.<number>|Uint8Array)} inflated buffer.
*/
Zlib.Gunzip.prototype.decompress = function() {
/** @type {number} input length. */
var il = this.input.length;
while (this.ip < il) {
this.decodeMember();
}
this.decompressed = true;
return this.concatMember();
};
/**
* decode gzip member.
*/
Zlib.Gunzip.prototype.decodeMember = function() {
/** @type {Zlib.GunzipMember} */
var member = new Zlib.GunzipMember();
/** @type {number} */
var isize;
/** @type {Zlib.RawInflate} RawInflate implementation. */
var rawinflate;
/** @type {!(Array.<number>|Uint8Array)} inflated data. */
var inflated;
/** @type {number} inflate size */
var inflen;
/** @type {number} character code */
var c;
/** @type {number} character index in string. */
var ci;
/** @type {Array.<string>} character array. */
var str;
/** @type {number} modification time. */
var mtime;
/** @type {number} */
var crc32;
var input = this.input;
var ip = this.ip;
member.id1 = input[ip++];
member.id2 = input[ip++];
// check signature
if (member.id1 !== 0x1f || member.id2 !== 0x8b) {
throw new Error('invalid file signature:' + member.id1 + ',' + member.id2);
}
// check compression method
member.cm = input[ip++];
switch (member.cm) {
case 8: /* XXX: use Zlib const */
break;
default:
throw new Error('unknown compression method: ' + member.cm);
}
// flags
member.flg = input[ip++];
// modification time
mtime = (input[ip++]) |
(input[ip++] << 8) |
(input[ip++] << 16) |
(input[ip++] << 24);
member.mtime = new Date(mtime * 1000);
// extra flags
member.xfl = input[ip++];
// operating system
member.os = input[ip++];
// extra
if ((member.flg & Zlib.Gzip.FlagsMask.FEXTRA) > 0) {
member.xlen = input[ip++] | (input[ip++] << 8);
ip = this.decodeSubField(ip, member.xlen);
}
// fname
if ((member.flg & Zlib.Gzip.FlagsMask.FNAME) > 0) {
for(str = [], ci = 0; (c = input[ip++]) > 0;) {
str[ci++] = String.fromCharCode(c);
}
member.name = str.join('');
}
// fcomment
if ((member.flg & Zlib.Gzip.FlagsMask.FCOMMENT) > 0) {
for(str = [], ci = 0; (c = input[ip++]) > 0;) {
str[ci++] = String.fromCharCode(c);
}
member.comment = str.join('');
}
// fhcrc
if ((member.flg & Zlib.Gzip.FlagsMask.FHCRC) > 0) {
member.crc16 = Zlib.CRC32.calc(input, 0, ip) & 0xffff;
if (member.crc16 !== (input[ip++] | (input[ip++] << 8))) {
throw new Error('invalid header crc16');
}
}
// isize を事前に取得すると展開後のサイズが分かるため、
// inflate処理のバッファサイズが事前に分かり、高速になる
isize = (input[input.length - 4]) | (input[input.length - 3] << 8) |
(input[input.length - 2] << 16) | (input[input.length - 1] << 24);
// isize の妥当性チェック
// ハフマン符号では最小 2-bit のため、最大で 1/4 になる
// LZ77 符号では 長さと距離 2-Byte で最大 258-Byte を表現できるため、
// 1/128 になるとする
// ここから入力バッファの残りが isize の 512 倍以上だったら
// サイズ指定のバッファ確保は行わない事とする
if (input.length - ip - /* CRC-32 */4 - /* ISIZE */4 < isize * 512) {
inflen = isize;
}
// compressed block
rawinflate = new Zlib.RawInflate(input, {'index': ip, 'bufferSize': inflen});
member.data = inflated = rawinflate.decompress();
ip = rawinflate.ip;
// crc32
member.crc32 = crc32 =
((input[ip++]) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24)) >>> 0;
if (Zlib.CRC32.calc(inflated) !== crc32) {
throw new Error('invalid CRC-32 checksum: 0x' +
Zlib.CRC32.calc(inflated).toString(16) + ' / 0x' + crc32.toString(16));
}
// input size
member.isize = isize =
((input[ip++]) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24)) >>> 0;
if ((inflated.length & 0xffffffff) !== isize) {
throw new Error('invalid input size: ' +
(inflated.length & 0xffffffff) + ' / ' + isize);
}
this.member.push(member);
this.ip = ip;
};
/**
* サブフィールドのデコード
* XXX: 現在は何もせずスキップする
*/
Zlib.Gunzip.prototype.decodeSubField = function(ip, length) {
return ip + length;
};
/**
* @return {!(Array.<number>|Uint8Array)}
*/
Zlib.Gunzip.prototype.concatMember = function() {
/** @type {Array.<Zlib.GunzipMember>} */
var member = this.member;
/** @type {number} */
var i;
/** @type {number} */
var il;
/** @type {number} */
var p = 0;
/** @type {number} */
var size = 0;
/** @type {!(Array.<number>|Uint8Array)} */
var buffer;
for (i = 0, il = member.length; i < il; ++i) {
size += member[i].data.length;
}
if (USE_TYPEDARRAY) {
buffer = new Uint8Array(size);
for (i = 0; i < il; ++i) {
buffer.set(member[i].data, p);
p += member[i].data.length;
}
} else {
buffer = [];
for (i = 0; i < il; ++i) {
buffer[i] = member[i].data;
}
buffer = Array.prototype.concat.apply([], buffer);
}
return buffer;
};
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,51 @@
goog.provide('Zlib.GunzipMember');
goog.scope(function() {
/**
* @constructor
*/
Zlib.GunzipMember = function() {
/** @type {number} signature first byte. */
this.id1;
/** @type {number} signature second byte. */
this.id2;
/** @type {number} compression method. */
this.cm;
/** @type {number} flags. */
this.flg;
/** @type {Date} modification time. */
this.mtime;
/** @type {number} extra flags. */
this.xfl;
/** @type {number} operating system number. */
this.os;
/** @type {number} CRC-16 value for FHCRC flag. */
this.crc16;
/** @type {number} extra length. */
this.xlen;
/** @type {number} CRC-32 value for verification. */
this.crc32;
/** @type {number} input size modulo 32 value. */
this.isize;
/** @type {string} filename. */
this.name;
/** @type {string} comment. */
this.comment;
/** @type {!(Uint8Array|Array.<number>)} */
this.data;
};
Zlib.GunzipMember.prototype.getName = function() {
return this.name;
};
Zlib.GunzipMember.prototype.getData = function() {
return this.data;
};
Zlib.GunzipMember.prototype.getMtime = function() {
return this.mtime;
}
});

View File

@ -0,0 +1,225 @@
/**
* @fileoverview GZIP (RFC1952) 実装.
*/
goog.provide('Zlib.Gzip');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.CRC32');
goog.require('Zlib.RawDeflate');
goog.scope(function() {
/**
* @constructor
* @param {!(Array|Uint8Array)} input input buffer.
* @param {Object=} opt_params option parameters.
*/
Zlib.Gzip = function(input, opt_params) {
/** @type {!(Array.<number>|Uint8Array)} input buffer. */
this.input = input;
/** @type {number} input buffer pointer. */
this.ip = 0;
/** @type {!(Array.<number>|Uint8Array)} output buffer. */
this.output;
/** @type {number} output buffer. */
this.op = 0;
/** @type {!Object} flags option flags. */
this.flags = {};
/** @type {!string} filename. */
this.filename;
/** @type {!string} comment. */
this.comment;
/** @type {!Object} deflate options. */
this.deflateOptions;
// option parameters
if (opt_params) {
if (opt_params['flags']) {
this.flags = opt_params['flags'];
}
if (typeof opt_params['filename'] === 'string') {
this.filename = opt_params['filename'];
}
if (typeof opt_params['comment'] === 'string') {
this.comment = opt_params['comment'];
}
if (opt_params['deflateOptions']) {
this.deflateOptions = opt_params['deflateOptions'];
}
}
if (!this.deflateOptions) {
this.deflateOptions = {};
}
};
/**
* @type {number}
* @const
*/
Zlib.Gzip.DefaultBufferSize = 0x8000;
/**
* encode gzip members.
* @return {!(Array|Uint8Array)} gzip binary array.
*/
Zlib.Gzip.prototype.compress = function() {
/** @type {number} flags. */
var flg;
/** @type {number} modification time. */
var mtime;
/** @type {number} CRC-16 value for FHCRC flag. */
var crc16;
/** @type {number} CRC-32 value for verification. */
var crc32;
/** @type {!Zlib.RawDeflate} raw deflate object. */
var rawdeflate;
/** @type {number} character code */
var c;
/** @type {number} loop counter. */
var i;
/** @type {number} loop limiter. */
var il;
/** @type {!(Array|Uint8Array)} output buffer. */
var output =
new (USE_TYPEDARRAY ? Uint8Array : Array)(Zlib.Gzip.DefaultBufferSize);
/** @type {number} output buffer pointer. */
var op = 0;
var input = this.input;
var ip = this.ip;
var filename = this.filename;
var comment = this.comment;
// check signature
output[op++] = 0x1f;
output[op++] = 0x8b;
// check compression method
output[op++] = 8; /* XXX: use Zlib const */
// flags
flg = 0;
if (this.flags['fname']) flg |= Zlib.Gzip.FlagsMask.FNAME;
if (this.flags['fcomment']) flg |= Zlib.Gzip.FlagsMask.FCOMMENT;
if (this.flags['fhcrc']) flg |= Zlib.Gzip.FlagsMask.FHCRC;
// XXX: FTEXT
// XXX: FEXTRA
output[op++] = flg;
// modification time
mtime = (Date.now ? Date.now() : +new Date()) / 1000 | 0;
output[op++] = mtime & 0xff;
output[op++] = mtime >>> 8 & 0xff;
output[op++] = mtime >>> 16 & 0xff;
output[op++] = mtime >>> 24 & 0xff;
// extra flags
output[op++] = 0;
// operating system
output[op++] = Zlib.Gzip.OperatingSystem.UNKNOWN;
// extra
/* NOP */
// fname
if (this.flags['fname'] !== void 0) {
for (i = 0, il = filename.length; i < il; ++i) {
c = filename.charCodeAt(i);
if (c > 0xff) { output[op++] = (c >>> 8) & 0xff; }
output[op++] = c & 0xff;
}
output[op++] = 0; // null termination
}
// fcomment
if (this.flags['comment']) {
for (i = 0, il = comment.length; i < il; ++i) {
c = comment.charCodeAt(i);
if (c > 0xff) { output[op++] = (c >>> 8) & 0xff; }
output[op++] = c & 0xff;
}
output[op++] = 0; // null termination
}
// fhcrc
if (this.flags['fhcrc']) {
crc16 = Zlib.CRC32.calc(output, 0, op) & 0xffff;
output[op++] = (crc16 ) & 0xff;
output[op++] = (crc16 >>> 8) & 0xff;
}
// add compress option
this.deflateOptions['outputBuffer'] = output;
this.deflateOptions['outputIndex'] = op;
// compress
rawdeflate = new Zlib.RawDeflate(input, this.deflateOptions);
output = rawdeflate.compress();
op = rawdeflate.op;
// expand buffer
if (USE_TYPEDARRAY) {
if (op + 8 > output.buffer.byteLength) {
this.output = new Uint8Array(op + 8);
this.output.set(new Uint8Array(output.buffer));
output = this.output;
} else {
output = new Uint8Array(output.buffer);
}
}
// crc32
crc32 = Zlib.CRC32.calc(input);
output[op++] = (crc32 ) & 0xff;
output[op++] = (crc32 >>> 8) & 0xff;
output[op++] = (crc32 >>> 16) & 0xff;
output[op++] = (crc32 >>> 24) & 0xff;
// input size
il = input.length;
output[op++] = (il ) & 0xff;
output[op++] = (il >>> 8) & 0xff;
output[op++] = (il >>> 16) & 0xff;
output[op++] = (il >>> 24) & 0xff;
this.ip = ip;
if (USE_TYPEDARRAY && op < output.length) {
this.output = output = output.subarray(0, op);
}
return output;
};
/** @enum {number} */
Zlib.Gzip.OperatingSystem = {
FAT: 0,
AMIGA: 1,
VMS: 2,
UNIX: 3,
VM_CMS: 4,
ATARI_TOS: 5,
HPFS: 6,
MACINTOSH: 7,
Z_SYSTEM: 8,
CP_M: 9,
TOPS_20: 10,
NTFS: 11,
QDOS: 12,
ACORN_RISCOS: 13,
UNKNOWN: 255
};
/** @enum {number} */
Zlib.Gzip.FlagsMask = {
FTEXT: 0x01,
FHCRC: 0x02,
FEXTRA: 0x04,
FNAME: 0x08,
FCOMMENT: 0x10
};
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,135 @@
/**
* @fileoverview Heap Sort 実装. ハフマン符号化で使用する.
*/
goog.provide('Zlib.Heap');
goog.require('USE_TYPEDARRAY');
goog.scope(function() {
/**
* カスタムハフマン符号で使用するヒープ実装
* @param {number} length ヒープサイズ.
* @constructor
*/
Zlib.Heap = function(length) {
this.buffer = new (USE_TYPEDARRAY ? Uint16Array : Array)(length * 2);
this.length = 0;
};
/**
* 親ノードの index 取得
* @param {number} index 子ノードの index.
* @return {number} 親ノードの index.
*
*/
Zlib.Heap.prototype.getParent = function(index) {
return ((index - 2) / 4 | 0) * 2;
};
/**
* 子ノードの index 取得
* @param {number} index 親ノードの index.
* @return {number} 子ノードの index.
*/
Zlib.Heap.prototype.getChild = function(index) {
return 2 * index + 2;
};
/**
* Heap に値を追加する
* @param {number} index キー index.
* @param {number} value 値.
* @return {number} 現在のヒープ長.
*/
Zlib.Heap.prototype.push = function(index, value) {
var current, parent,
heap = this.buffer,
swap;
current = this.length;
heap[this.length++] = value;
heap[this.length++] = index;
// ルートノードにたどり着くまで入れ替えを試みる
while (current > 0) {
parent = this.getParent(current);
// 親ノードと比較して親の方が小さければ入れ替える
if (heap[current] > heap[parent]) {
swap = heap[current];
heap[current] = heap[parent];
heap[parent] = swap;
swap = heap[current + 1];
heap[current + 1] = heap[parent + 1];
heap[parent + 1] = swap;
current = parent;
// 入れ替えが必要なくなったらそこで抜ける
} else {
break;
}
}
return this.length;
};
/**
* Heapから一番大きい値を返す
* @return {{index: number, value: number, length: number}} {index: キーindex,
* value: 値, length: ヒープ長} の Object.
*/
Zlib.Heap.prototype.pop = function() {
var index, value,
heap = this.buffer, swap,
current, parent;
value = heap[0];
index = heap[1];
// 後ろから値を取る
this.length -= 2;
heap[0] = heap[this.length];
heap[1] = heap[this.length + 1];
parent = 0;
// ルートノードから下がっていく
while (true) {
current = this.getChild(parent);
// 範囲チェック
if (current >= this.length) {
break;
}
// 隣のノードと比較して、隣の方が値が大きければ隣を現在ノードとして選択
if (current + 2 < this.length && heap[current + 2] > heap[current]) {
current += 2;
}
// 親ノードと比較して親の方が小さい場合は入れ替える
if (heap[current] > heap[parent]) {
swap = heap[parent];
heap[parent] = heap[current];
heap[current] = swap;
swap = heap[parent + 1];
heap[parent + 1] = heap[current + 1];
heap[current + 1] = swap;
} else {
break;
}
parent = current;
}
return {index: index, value: value, length: this.length};
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,93 @@
goog.provide('Zlib.Huffman');
goog.require('USE_TYPEDARRAY');
goog.scope(function() {
/**
* build huffman table from length list.
* @param {!(Array.<number>|Uint8Array)} lengths length list.
* @return {!Array} huffman table.
*/
Zlib.Huffman.buildHuffmanTable = function(lengths) {
/** @type {number} length list size. */
var listSize = lengths.length;
/** @type {number} max code length for table size. */
var maxCodeLength = 0;
/** @type {number} min code length for table size. */
var minCodeLength = Number.POSITIVE_INFINITY;
/** @type {number} table size. */
var size;
/** @type {!(Array|Uint8Array)} huffman code table. */
var table;
/** @type {number} bit length. */
var bitLength;
/** @type {number} huffman code. */
var code;
/**
* サイズが 2^maxlength 個のテーブルを埋めるためのスキップ長.
* @type {number} skip length for table filling.
*/
var skip;
/** @type {number} reversed code. */
var reversed;
/** @type {number} reverse temp. */
var rtemp;
/** @type {number} loop counter. */
var i;
/** @type {number} loop limit. */
var il;
/** @type {number} loop counter. */
var j;
/** @type {number} loop limit. */
var jl;
// Math.max は遅いので最長の値は for-loop で取得する
for (i = 0, il = listSize; i < il; ++i) {
if (lengths[i] > maxCodeLength) {
maxCodeLength = lengths[i];
}
if (lengths[i] < minCodeLength) {
minCodeLength = lengths[i];
}
}
size = 1 << maxCodeLength;
table = new (USE_TYPEDARRAY ? Uint32Array : Array)(size);
// ビット長の短い順からハフマン符号を割り当てる
for (bitLength = 1, code = 0, skip = 2; bitLength <= maxCodeLength;) {
for (i = 0; i < listSize; ++i) {
if (lengths[i] === bitLength) {
// ビットオーダーが逆になるためビット長分並びを反転する
for (reversed = 0, rtemp = code, j = 0; j < bitLength; ++j) {
reversed = (reversed << 1) | (rtemp & 1);
rtemp >>= 1;
}
// 最大ビット長をもとにテーブルを作るため、
// 最大ビット長以外では 0 / 1 どちらでも良い箇所ができる
// そのどちらでも良い場所は同じ値で埋めることで
// 本来のビット長以上のビット数取得しても問題が起こらないようにする
for (j = reversed; j < size; j += skip) {
table[j] = (bitLength << 16) | i;
}
++code;
}
}
// 次のビット長へ
++bitLength;
code <<= 1;
skip <<= 1;
}
return [table, maxCodeLength, minCodeLength];
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,120 @@
goog.provide('Zlib.Inflate');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.Adler32');
goog.require('Zlib.RawInflate');
goog.scope(function() {
/**
* @constructor
* @param {!(Uint8Array|Array)} input deflated buffer.
* @param {Object=} opt_params option parameters.
*
* opt_params は以下のプロパティを指定する事ができます。
* - index: input buffer の deflate コンテナの開始位置.
* - blockSize: バッファのブロックサイズ.
* - verify: 伸張が終わった後 adler-32 checksum の検証を行うか.
* - bufferType: Zlib.Inflate.BufferType の値によってバッファの管理方法を指定する.
* Zlib.Inflate.BufferType は Zlib.RawInflate.BufferType のエイリアス.
*/
Zlib.Inflate = function(input, opt_params) {
/** @type {number} */
var bufferSize;
/** @type {Zlib.Inflate.BufferType} */
var bufferType;
/** @type {number} */
var cmf;
/** @type {number} */
var flg;
/** @type {!(Uint8Array|Array)} */
this.input = input;
/** @type {number} */
this.ip = 0;
/** @type {Zlib.RawInflate} */
this.rawinflate;
/** @type {(boolean|undefined)} verify flag. */
this.verify;
// option parameters
if (opt_params || !(opt_params = {})) {
if (opt_params['index']) {
this.ip = opt_params['index'];
}
if (opt_params['verify']) {
this.verify = opt_params['verify'];
}
}
// Compression Method and Flags
cmf = input[this.ip++];
flg = input[this.ip++];
// compression method
switch (cmf & 0x0f) {
case Zlib.CompressionMethod.DEFLATE:
this.method = Zlib.CompressionMethod.DEFLATE;
break;
default:
throw new Error('unsupported compression method');
}
// fcheck
if (((cmf << 8) + flg) % 31 !== 0) {
throw new Error('invalid fcheck flag:' + ((cmf << 8) + flg) % 31);
}
// fdict (not supported)
if (flg & 0x20) {
throw new Error('fdict flag is not supported');
}
// RawInflate
this.rawinflate = new Zlib.RawInflate(input, {
'index': this.ip,
'bufferSize': opt_params['bufferSize'],
'bufferType': opt_params['bufferType'],
'resize': opt_params['resize']
});
}
/**
* @enum {number}
*/
Zlib.Inflate.BufferType = Zlib.RawInflate.BufferType;
/**
* decompress.
* @return {!(Uint8Array|Array)} inflated buffer.
*/
Zlib.Inflate.prototype.decompress = function() {
/** @type {!(Array|Uint8Array)} input buffer. */
var input = this.input;
/** @type {!(Uint8Array|Array)} inflated buffer. */
var buffer;
/** @type {number} adler-32 checksum */
var adler32;
buffer = this.rawinflate.decompress();
this.ip = this.rawinflate.ip;
// verify adler-32
if (this.verify) {
adler32 = (
input[this.ip++] << 24 | input[this.ip++] << 16 |
input[this.ip++] << 8 | input[this.ip++]
) >>> 0;
if (adler32 !== Zlib.Adler32(buffer)) {
throw new Error('invalid adler-32 checksum');
}
}
return buffer;
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,119 @@
goog.provide('Zlib.InflateStream');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib');
//goog.require('Zlib.Adler32');
goog.require('Zlib.RawInflateStream');
goog.scope(function() {
/**
* @param {!(Uint8Array|Array)} input deflated buffer.
* @constructor
*/
Zlib.InflateStream = function(input) {
/** @type {!(Uint8Array|Array)} */
this.input = input === void 0 ? new (USE_TYPEDARRAY ? Uint8Array : Array)() : input;
/** @type {number} */
this.ip = 0;
/** @type {Zlib.RawInflateStream} */
this.rawinflate = new Zlib.RawInflateStream(this.input, this.ip);
/** @type {Zlib.CompressionMethod} */
this.method;
/** @type {!(Array|Uint8Array)} */
this.output = this.rawinflate.output;
};
/**
* decompress.
* @return {!(Uint8Array|Array)} inflated buffer.
*/
Zlib.InflateStream.prototype.decompress = function(input) {
/** @type {!(Uint8Array|Array)} inflated buffer. */
var buffer;
/** @type {number} adler-32 checksum */
var adler32;
// 新しい入力を入力バッファに結合する
// XXX Array, Uint8Array のチェックを行うか確認する
if (input !== void 0) {
if (USE_TYPEDARRAY) {
var tmp = new Uint8Array(this.input.length + input.length);
tmp.set(this.input, 0);
tmp.set(input, this.input.length);
this.input = tmp;
} else {
this.input = this.input.concat(input);
}
}
if (this.method === void 0) {
if(this.readHeader() < 0) {
return new (USE_TYPEDARRAY ? Uint8Array : Array)();
}
}
buffer = this.rawinflate.decompress(this.input, this.ip);
this.ip = this.rawinflate.ip;
// verify adler-32
/*
if (this.verify) {
adler32 =
input[this.ip++] << 24 | input[this.ip++] << 16 |
input[this.ip++] << 8 | input[this.ip++];
if (adler32 !== Zlib.Adler32(buffer)) {
throw new Error('invalid adler-32 checksum');
}
}
*/
return buffer;
};
/**
* @return {!(Uint8Array|Array)} current output buffer.
*/
Zlib.InflateStream.prototype.getBytes = function() {
return this.rawinflate.getBytes();
};
Zlib.InflateStream.prototype.readHeader = function() {
var ip = this.ip;
var input = this.input;
// Compression Method and Flags
var cmf = input[ip++];
var flg = input[ip++];
if (cmf === void 0 || flg === void 0) {
return -1;
}
// compression method
switch (cmf & 0x0f) {
case Zlib.CompressionMethod.DEFLATE:
this.method = Zlib.CompressionMethod.DEFLATE;
break;
default:
throw new Error('unsupported compression method');
}
// fcheck
if (((cmf << 8) + flg) % 31 !== 0) {
throw new Error('invalid fcheck flag:' + ((cmf << 8) + flg) % 31);
}
// fdict (not supported)
if (flg & 0x20) {
throw new Error('fdict flag is not supported');
}
this.ip = ip;
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,861 @@
goog.provide('Zlib.RawInflate');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.Huffman');
//-----------------------------------------------------------------------------
/** @define {number} buffer block size. */
var ZLIB_RAW_INFLATE_BUFFER_SIZE = 0x8000; // [ 0x8000 >= ZLIB_BUFFER_BLOCK_SIZE ]
//-----------------------------------------------------------------------------
goog.scope(function() {
var buildHuffmanTable = Zlib.Huffman.buildHuffmanTable;
/**
* @constructor
* @param {!(Uint8Array|Array.<number>)} input input buffer.
* @param {Object} opt_params option parameter.
*
* opt_params は以下のプロパティを指定する事ができます。
* - index: input buffer の deflate コンテナの開始位置.
* - blockSize: バッファのブロックサイズ.
* - bufferType: Zlib.RawInflate.BufferType の値によってバッファの管理方法を指定する.
* - resize: 確保したバッファが実際の大きさより大きかった場合に切り詰める.
*/
Zlib.RawInflate = function(input, opt_params) {
/** @type {!(Array.<number>|Uint8Array)} inflated buffer */
this.buffer;
/** @type {!Array.<(Array.<number>|Uint8Array)>} */
this.blocks = [];
/** @type {number} block size. */
this.bufferSize = ZLIB_RAW_INFLATE_BUFFER_SIZE;
/** @type {!number} total output buffer pointer. */
this.totalpos = 0;
/** @type {!number} input buffer pointer. */
this.ip = 0;
/** @type {!number} bit stream reader buffer. */
this.bitsbuf = 0;
/** @type {!number} bit stream reader buffer size. */
this.bitsbuflen = 0;
/** @type {!(Array.<number>|Uint8Array)} input buffer. */
this.input = USE_TYPEDARRAY ? new Uint8Array(input) : input;
/** @type {!(Uint8Array|Array.<number>)} output buffer. */
this.output;
/** @type {!number} output buffer pointer. */
this.op;
/** @type {boolean} is final block flag. */
this.bfinal = false;
/** @type {Zlib.RawInflate.BufferType} buffer management. */
this.bufferType = Zlib.RawInflate.BufferType.ADAPTIVE;
/** @type {boolean} resize flag for memory size optimization. */
this.resize = false;
// option parameters
if (opt_params || !(opt_params = {})) {
if (opt_params['index']) {
this.ip = opt_params['index'];
}
if (opt_params['bufferSize']) {
this.bufferSize = opt_params['bufferSize'];
}
if (opt_params['bufferType']) {
this.bufferType = opt_params['bufferType'];
}
if (opt_params['resize']) {
this.resize = opt_params['resize'];
}
}
// initialize
switch (this.bufferType) {
case Zlib.RawInflate.BufferType.BLOCK:
this.op = Zlib.RawInflate.MaxBackwardLength;
this.output =
new (USE_TYPEDARRAY ? Uint8Array : Array)(
Zlib.RawInflate.MaxBackwardLength +
this.bufferSize +
Zlib.RawInflate.MaxCopyLength
);
break;
case Zlib.RawInflate.BufferType.ADAPTIVE:
this.op = 0;
this.output = new (USE_TYPEDARRAY ? Uint8Array : Array)(this.bufferSize);
this.expandBuffer = this.expandBufferAdaptive;
this.concatBuffer = this.concatBufferDynamic;
this.decodeHuffman = this.decodeHuffmanAdaptive;
break;
default:
throw new Error('invalid inflate mode');
}
};
/**
* @enum {number}
*/
Zlib.RawInflate.BufferType = {
BLOCK: 0,
ADAPTIVE: 1
};
/**
* decompress.
* @return {!(Uint8Array|Array.<number>)} inflated buffer.
*/
Zlib.RawInflate.prototype.decompress = function() {
while (!this.bfinal) {
this.parseBlock();
}
return this.concatBuffer();
};
/**
* @const
* @type {number} max backward length for LZ77.
*/
Zlib.RawInflate.MaxBackwardLength = 32768;
/**
* @const
* @type {number} max copy length for LZ77.
*/
Zlib.RawInflate.MaxCopyLength = 258;
/**
* huffman order
* @const
* @type {!(Array.<number>|Uint8Array)}
*/
Zlib.RawInflate.Order = (function(table) {
return USE_TYPEDARRAY ? new Uint16Array(table) : table;
})([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
/**
* huffman length code table.
* @const
* @type {!(Array.<number>|Uint16Array)}
*/
Zlib.RawInflate.LengthCodeTable = (function(table) {
return USE_TYPEDARRAY ? new Uint16Array(table) : table;
})([
0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b,
0x000d, 0x000f, 0x0011, 0x0013, 0x0017, 0x001b, 0x001f, 0x0023, 0x002b,
0x0033, 0x003b, 0x0043, 0x0053, 0x0063, 0x0073, 0x0083, 0x00a3, 0x00c3,
0x00e3, 0x0102, 0x0102, 0x0102
]);
/**
* huffman length extra-bits table.
* @const
* @type {!(Array.<number>|Uint8Array)}
*/
Zlib.RawInflate.LengthExtraTable = (function(table) {
return USE_TYPEDARRAY ? new Uint8Array(table) : table;
})([
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5,
5, 5, 0, 0, 0
]);
/**
* huffman dist code table.
* @const
* @type {!(Array.<number>|Uint16Array)}
*/
Zlib.RawInflate.DistCodeTable = (function(table) {
return USE_TYPEDARRAY ? new Uint16Array(table) : table;
})([
0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0009, 0x000d, 0x0011,
0x0019, 0x0021, 0x0031, 0x0041, 0x0061, 0x0081, 0x00c1, 0x0101, 0x0181,
0x0201, 0x0301, 0x0401, 0x0601, 0x0801, 0x0c01, 0x1001, 0x1801, 0x2001,
0x3001, 0x4001, 0x6001
]);
/**
* huffman dist extra-bits table.
* @const
* @type {!(Array.<number>|Uint8Array)}
*/
Zlib.RawInflate.DistExtraTable = (function(table) {
return USE_TYPEDARRAY ? new Uint8Array(table) : table;
})([
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11,
11, 12, 12, 13, 13
]);
/**
* fixed huffman length code table
* @const
* @type {!Array}
*/
Zlib.RawInflate.FixedLiteralLengthTable = (function(table) {
return table;
})((function() {
var lengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(288);
var i, il;
for (i = 0, il = lengths.length; i < il; ++i) {
lengths[i] =
(i <= 143) ? 8 :
(i <= 255) ? 9 :
(i <= 279) ? 7 :
8;
}
return buildHuffmanTable(lengths);
})());
/**
* fixed huffman distance code table
* @const
* @type {!Array}
*/
Zlib.RawInflate.FixedDistanceTable = (function(table) {
return table;
})((function() {
var lengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(30);
var i, il;
for (i = 0, il = lengths.length; i < il; ++i) {
lengths[i] = 5;
}
return buildHuffmanTable(lengths);
})());
/**
* parse deflated block.
*/
Zlib.RawInflate.prototype.parseBlock = function() {
/** @type {number} header */
var hdr = this.readBits(3);
// BFINAL
if (hdr & 0x1) {
this.bfinal = true;
}
// BTYPE
hdr >>>= 1;
switch (hdr) {
// uncompressed
case 0:
this.parseUncompressedBlock();
break;
// fixed huffman
case 1:
this.parseFixedHuffmanBlock();
break;
// dynamic huffman
case 2:
this.parseDynamicHuffmanBlock();
break;
// reserved or other
default:
throw new Error('unknown BTYPE: ' + hdr);
}
};
/**
* read inflate bits
* @param {number} length bits length.
* @return {number} read bits.
*/
Zlib.RawInflate.prototype.readBits = function(length) {
var bitsbuf = this.bitsbuf;
var bitsbuflen = this.bitsbuflen;
var input = this.input;
var ip = this.ip;
/** @type {number} input and output byte. */
var octet;
// not enough buffer
while (bitsbuflen < length) {
// input byte
octet = input[ip++];
if (octet === void 0) {
throw new Error('input buffer is broken');
}
// concat octet
bitsbuf |= octet << bitsbuflen;
bitsbuflen += 8;
}
// output byte
octet = bitsbuf & /* MASK */ ((1 << length) - 1);
bitsbuf >>>= length;
bitsbuflen -= length;
this.bitsbuf = bitsbuf;
this.bitsbuflen = bitsbuflen;
this.ip = ip;
return octet;
};
/**
* read huffman code using table
* @param {Array} table huffman code table.
* @return {number} huffman code.
*/
Zlib.RawInflate.prototype.readCodeByTable = function(table) {
var bitsbuf = this.bitsbuf;
var bitsbuflen = this.bitsbuflen;
var input = this.input;
var ip = this.ip;
/** @type {!(Array.<number>|Uint8Array)} huffman code table */
var codeTable = table[0];
/** @type {number} */
var maxCodeLength = table[1];
/** @type {number} input byte */
var octet;
/** @type {number} code length & code (16bit, 16bit) */
var codeWithLength;
/** @type {number} code bits length */
var codeLength;
// not enough buffer
while (bitsbuflen < maxCodeLength) {
octet = input[ip++];
if (octet === void 0) {
break;
}
bitsbuf |= octet << bitsbuflen;
bitsbuflen += 8;
}
// read max length
codeWithLength = codeTable[bitsbuf & ((1 << maxCodeLength) - 1)];
codeLength = codeWithLength >>> 16;
this.bitsbuf = bitsbuf >> codeLength;
this.bitsbuflen = bitsbuflen - codeLength;
this.ip = ip;
return codeWithLength & 0xffff;
};
/**
* parse uncompressed block.
*/
Zlib.RawInflate.prototype.parseUncompressedBlock = function() {
var input = this.input;
var ip = this.ip;
var output = this.output;
var op = this.op;
/** @type {number} input byte. */
var octet;
/** @type {number} block length */
var len;
/** @type {number} number for check block length */
var nlen;
/** @type {number} output buffer length */
var olength = output.length;
/** @type {number} copy counter */
var preCopy;
// skip buffered header bits
this.bitsbuf = 0;
this.bitsbuflen = 0;
// len (1st)
octet = input[ip++];
if (octet === void 0) {
throw new Error('invalid uncompressed block header: LEN (first byte)');
}
len = octet;
// len (2nd)
octet = input[ip++];
if (octet === void 0) {
throw new Error('invalid uncompressed block header: LEN (second byte)');
}
len |= octet << 8;
// nlen (1st)
octet = input[ip++];
if (octet === void 0) {
throw new Error('invalid uncompressed block header: NLEN (first byte)');
}
nlen = octet;
// nlen (2nd)
octet = input[ip++];
if (octet === void 0) {
throw new Error('invalid uncompressed block header: NLEN (second byte)');
}
nlen |= octet << 8;
// check len & nlen
if (len === ~nlen) {
throw new Error('invalid uncompressed block header: length verify');
}
// check size
if (ip + len > input.length) { throw new Error('input buffer is broken'); }
// expand buffer
switch (this.bufferType) {
case Zlib.RawInflate.BufferType.BLOCK:
// pre copy
while (op + len > output.length) {
preCopy = olength - op;
len -= preCopy;
if (USE_TYPEDARRAY) {
output.set(input.subarray(ip, ip + preCopy), op);
op += preCopy;
ip += preCopy;
} else {
while (preCopy--) {
output[op++] = input[ip++];
}
}
this.op = op;
output = this.expandBuffer();
op = this.op;
}
break;
case Zlib.RawInflate.BufferType.ADAPTIVE:
while (op + len > output.length) {
output = this.expandBuffer({fixRatio: 2});
}
break;
default:
throw new Error('invalid inflate mode');
}
// copy
if (USE_TYPEDARRAY) {
output.set(input.subarray(ip, ip + len), op);
op += len;
ip += len;
} else {
while (len--) {
output[op++] = input[ip++];
}
}
this.ip = ip;
this.op = op;
this.output = output;
};
/**
* parse fixed huffman block.
*/
Zlib.RawInflate.prototype.parseFixedHuffmanBlock = function() {
this.decodeHuffman(
Zlib.RawInflate.FixedLiteralLengthTable,
Zlib.RawInflate.FixedDistanceTable
);
};
/**
* parse dynamic huffman block.
*/
Zlib.RawInflate.prototype.parseDynamicHuffmanBlock = function() {
/** @type {number} number of literal and length codes. */
var hlit = this.readBits(5) + 257;
/** @type {number} number of distance codes. */
var hdist = this.readBits(5) + 1;
/** @type {number} number of code lengths. */
var hclen = this.readBits(4) + 4;
/** @type {!(Uint8Array|Array.<number>)} code lengths. */
var codeLengths =
new (USE_TYPEDARRAY ? Uint8Array : Array)(Zlib.RawInflate.Order.length);
/** @type {!Array} code lengths table. */
var codeLengthsTable;
/** @type {!(Uint8Array|Array.<number>)} literal and length code lengths. */
var litlenLengths;
/** @type {!(Uint8Array|Array.<number>)} distance code lengths. */
var distLengths;
/** @type {number} loop counter. */
var i;
// decode code lengths
for (i = 0; i < hclen; ++i) {
codeLengths[Zlib.RawInflate.Order[i]] = this.readBits(3);
}
codeLengthsTable = buildHuffmanTable(codeLengths);
/**
* decode function
* @param {number} num number of lengths.
* @param {!Array} table code lengths table.
* @param {!(Uint8Array|Array.<number>)} lengths code lengths buffer.
* @return {!(Uint8Array|Array.<number>)} code lengths buffer.
*/
function decode(num, table, lengths) {
/** @type {number} */
var code;
/** @type {number} */
var prev;
/** @type {number} */
var repeat;
/** @type {number} */
var i;
for (i = 0; i < num;) {
code = this.readCodeByTable(table);
switch (code) {
case 16:
repeat = 3 + this.readBits(2);
while (repeat--) { lengths[i++] = prev; }
break;
case 17:
repeat = 3 + this.readBits(3);
while (repeat--) { lengths[i++] = 0; }
prev = 0;
break;
case 18:
repeat = 11 + this.readBits(7);
while (repeat--) { lengths[i++] = 0; }
prev = 0;
break;
default:
lengths[i++] = code;
prev = code;
break;
}
}
return lengths;
}
// literal and length code
litlenLengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(hlit);
// distance code
distLengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(hdist);
//return;
this.decodeHuffman(
buildHuffmanTable(decode.call(this, hlit, codeLengthsTable, litlenLengths)),
buildHuffmanTable(decode.call(this, hdist, codeLengthsTable, distLengths))
);
};
/**
* decode huffman code
* @param {!Array} litlen literal and length code table.
* @param {!Array} dist distination code table.
*/
Zlib.RawInflate.prototype.decodeHuffman = function(litlen, dist) {
var output = this.output;
var op = this.op;
this.currentLitlenTable = litlen;
/** @type {number} output position limit. */
var olength = output.length - Zlib.RawInflate.MaxCopyLength;
/** @type {number} huffman code. */
var code;
/** @type {number} table index. */
var ti;
/** @type {number} huffman code distination. */
var codeDist;
/** @type {number} huffman code length. */
var codeLength;
while ((code = this.readCodeByTable(litlen)) !== 256) {
// literal
if (code < 256) {
if (op >= olength) {
this.op = op;
output = this.expandBuffer();
op = this.op;
}
output[op++] = code;
continue;
}
// length code
ti = code - 257;
codeLength = Zlib.RawInflate.LengthCodeTable[ti];
if (Zlib.RawInflate.LengthExtraTable[ti] > 0) {
codeLength += this.readBits(Zlib.RawInflate.LengthExtraTable[ti]);
}
// dist code
code = this.readCodeByTable(dist);
codeDist = Zlib.RawInflate.DistCodeTable[code];
if (Zlib.RawInflate.DistExtraTable[code] > 0) {
codeDist += this.readBits(Zlib.RawInflate.DistExtraTable[code]);
}
// lz77 decode
if (op >= olength) {
this.op = op;
output = this.expandBuffer();
op = this.op;
}
while (codeLength--) {
output[op] = output[(op++) - codeDist];
}
}
while (this.bitsbuflen >= 8) {
this.bitsbuflen -= 8;
this.ip--;
}
this.op = op;
};
/**
* decode huffman code (adaptive)
* @param {!Array} litlen literal and length code table.
* @param {!Array} dist distination code table.
*/
Zlib.RawInflate.prototype.decodeHuffmanAdaptive = function(litlen, dist) {
var output = this.output;
var op = this.op;
this.currentLitlenTable = litlen;
/** @type {number} output position limit. */
var olength = output.length;
/** @type {number} huffman code. */
var code;
/** @type {number} table index. */
var ti;
/** @type {number} huffman code distination. */
var codeDist;
/** @type {number} huffman code length. */
var codeLength;
while ((code = this.readCodeByTable(litlen)) !== 256) {
// literal
if (code < 256) {
if (op >= olength) {
output = this.expandBuffer();
olength = output.length;
}
output[op++] = code;
continue;
}
// length code
ti = code - 257;
codeLength = Zlib.RawInflate.LengthCodeTable[ti];
if (Zlib.RawInflate.LengthExtraTable[ti] > 0) {
codeLength += this.readBits(Zlib.RawInflate.LengthExtraTable[ti]);
}
// dist code
code = this.readCodeByTable(dist);
codeDist = Zlib.RawInflate.DistCodeTable[code];
if (Zlib.RawInflate.DistExtraTable[code] > 0) {
codeDist += this.readBits(Zlib.RawInflate.DistExtraTable[code]);
}
// lz77 decode
if (op + codeLength > olength) {
output = this.expandBuffer();
olength = output.length;
}
while (codeLength--) {
output[op] = output[(op++) - codeDist];
}
}
while (this.bitsbuflen >= 8) {
this.bitsbuflen -= 8;
this.ip--;
}
this.op = op;
};
/**
* expand output buffer.
* @param {Object=} opt_param option parameters.
* @return {!(Array.<number>|Uint8Array)} output buffer.
*/
Zlib.RawInflate.prototype.expandBuffer = function(opt_param) {
/** @type {!(Array.<number>|Uint8Array)} store buffer. */
var buffer =
new (USE_TYPEDARRAY ? Uint8Array : Array)(
this.op - Zlib.RawInflate.MaxBackwardLength
);
/** @type {number} backward base point */
var backward = this.op - Zlib.RawInflate.MaxBackwardLength;
/** @type {number} copy index. */
var i;
/** @type {number} copy limit */
var il;
var output = this.output;
// copy to output buffer
if (USE_TYPEDARRAY) {
buffer.set(output.subarray(Zlib.RawInflate.MaxBackwardLength, buffer.length));
} else {
for (i = 0, il = buffer.length; i < il; ++i) {
buffer[i] = output[i + Zlib.RawInflate.MaxBackwardLength];
}
}
this.blocks.push(buffer);
this.totalpos += buffer.length;
// copy to backward buffer
if (USE_TYPEDARRAY) {
output.set(
output.subarray(backward, backward + Zlib.RawInflate.MaxBackwardLength)
);
} else {
for (i = 0; i < Zlib.RawInflate.MaxBackwardLength; ++i) {
output[i] = output[backward + i];
}
}
this.op = Zlib.RawInflate.MaxBackwardLength;
return output;
};
/**
* expand output buffer. (adaptive)
* @param {Object=} opt_param option parameters.
* @return {!(Array.<number>|Uint8Array)} output buffer pointer.
*/
Zlib.RawInflate.prototype.expandBufferAdaptive = function(opt_param) {
/** @type {!(Array.<number>|Uint8Array)} store buffer. */
var buffer;
/** @type {number} expantion ratio. */
var ratio = (this.input.length / this.ip + 1) | 0;
/** @type {number} maximum number of huffman code. */
var maxHuffCode;
/** @type {number} new output buffer size. */
var newSize;
/** @type {number} max inflate size. */
var maxInflateSize;
var input = this.input;
var output = this.output;
if (opt_param) {
if (typeof opt_param.fixRatio === 'number') {
ratio = opt_param.fixRatio;
}
if (typeof opt_param.addRatio === 'number') {
ratio += opt_param.addRatio;
}
}
// calculate new buffer size
if (ratio < 2) {
maxHuffCode =
(input.length - this.ip) / this.currentLitlenTable[2];
maxInflateSize = (maxHuffCode / 2 * 258) | 0;
newSize = maxInflateSize < output.length ?
output.length + maxInflateSize :
output.length << 1;
} else {
newSize = output.length * ratio;
}
// buffer expantion
if (USE_TYPEDARRAY) {
buffer = new Uint8Array(newSize);
buffer.set(output);
} else {
buffer = output;
}
this.output = buffer;
return this.output;
};
/**
* concat output buffer.
* @return {!(Array.<number>|Uint8Array)} output buffer.
*/
Zlib.RawInflate.prototype.concatBuffer = function() {
/** @type {number} buffer pointer. */
var pos = 0;
/** @type {number} buffer pointer. */
var limit = this.totalpos + (this.op - Zlib.RawInflate.MaxBackwardLength);
/** @type {!(Array.<number>|Uint8Array)} output block array. */
var output = this.output;
/** @type {!Array} blocks array. */
var blocks = this.blocks;
/** @type {!(Array.<number>|Uint8Array)} output block array. */
var block;
/** @type {!(Array.<number>|Uint8Array)} output buffer. */
var buffer = new (USE_TYPEDARRAY ? Uint8Array : Array)(limit);
/** @type {number} loop counter. */
var i;
/** @type {number} loop limiter. */
var il;
/** @type {number} loop counter. */
var j;
/** @type {number} loop limiter. */
var jl;
// single buffer
if (blocks.length === 0) {
return USE_TYPEDARRAY ?
this.output.subarray(Zlib.RawInflate.MaxBackwardLength, this.op) :
this.output.slice(Zlib.RawInflate.MaxBackwardLength, this.op);
}
// copy to buffer
for (i = 0, il = blocks.length; i < il; ++i) {
block = blocks[i];
for (j = 0, jl = block.length; j < jl; ++j) {
buffer[pos++] = block[j];
}
}
// current buffer
for (i = Zlib.RawInflate.MaxBackwardLength, il = this.op; i < il; ++i) {
buffer[pos++] = output[i];
}
this.blocks = [];
this.buffer = buffer;
return this.buffer;
};
/**
* concat output buffer. (dynamic)
* @return {!(Array.<number>|Uint8Array)} output buffer.
*/
Zlib.RawInflate.prototype.concatBufferDynamic = function() {
/** @type {Array.<number>|Uint8Array} output buffer. */
var buffer;
var op = this.op;
if (USE_TYPEDARRAY) {
if (this.resize) {
buffer = new Uint8Array(op);
buffer.set(this.output.subarray(0, op));
} else {
buffer = this.output.subarray(0, op);
}
} else {
if (this.output.length > op) {
this.output.length = op;
}
buffer = this.output;
}
this.buffer = buffer;
return this.buffer;
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,862 @@
goog.provide('Zlib.RawInflateStream');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.Huffman');
//-----------------------------------------------------------------------------
/** @define {number} buffer block size. */
var ZLIB_STREAM_RAW_INFLATE_BUFFER_SIZE = 0x8000;
//-----------------------------------------------------------------------------
goog.scope(function() {
var buildHuffmanTable = Zlib.Huffman.buildHuffmanTable;
/**
* @param {!(Uint8Array|Array.<number>)} input input buffer.
* @param {number} ip input buffer pointer.
* @param {number=} opt_buffersize buffer block size.
* @constructor
*/
Zlib.RawInflateStream = function(input, ip, opt_buffersize) {
/** @type {!(Array|Uint8Array)} inflated buffer */
this.buffer;
/** @type {!Array.<(Array|Uint8Array)>} */
this.blocks = [];
/** @type {number} block size. */
this.bufferSize =
opt_buffersize ? opt_buffersize : ZLIB_STREAM_RAW_INFLATE_BUFFER_SIZE;
/** @type {!number} total output buffer pointer. */
this.totalpos = 0;
/** @type {!number} input buffer pointer. */
this.ip = ip === void 0 ? 0 : ip;
/** @type {!number} bit stream reader buffer. */
this.bitsbuf = 0;
/** @type {!number} bit stream reader buffer size. */
this.bitsbuflen = 0;
/** @type {!(Array|Uint8Array)} input buffer. */
this.input = USE_TYPEDARRAY ? new Uint8Array(input) : input;
/** @type {!(Uint8Array|Array)} output buffer. */
this.output = new (USE_TYPEDARRAY ? Uint8Array : Array)(this.bufferSize);
/** @type {!number} output buffer pointer. */
this.op = 0;
/** @type {boolean} is final block flag. */
this.bfinal = false;
/** @type {number} uncompressed block length. */
this.blockLength;
/** @type {boolean} resize flag for memory size optimization. */
this.resize = false;
/** @type {Array} */
this.litlenTable;
/** @type {Array} */
this.distTable;
/** @type {number} */
this.sp = 0; // stream pointer
/** @type {Zlib.RawInflateStream.Status} */
this.status = Zlib.RawInflateStream.Status.INITIALIZED;
//
// backup
//
/** @type {!number} */
this.ip_;
/** @type {!number} */
this.bitsbuflen_;
/** @type {!number} */
this.bitsbuf_;
};
/**
* @enum {number}
*/
Zlib.RawInflateStream.BlockType = {
UNCOMPRESSED: 0,
FIXED: 1,
DYNAMIC: 2
};
/**
* @enum {number}
*/
Zlib.RawInflateStream.Status = {
INITIALIZED: 0,
BLOCK_HEADER_START: 1,
BLOCK_HEADER_END: 2,
BLOCK_BODY_START: 3,
BLOCK_BODY_END: 4,
DECODE_BLOCK_START: 5,
DECODE_BLOCK_END: 6
};
/**
* decompress.
* @return {!(Uint8Array|Array)} inflated buffer.
*/
Zlib.RawInflateStream.prototype.decompress = function(newInput, ip) {
/** @type {boolean} */
var stop = false;
if (newInput !== void 0) {
this.input = newInput;
}
if (ip !== void 0) {
this.ip = ip;
}
// decompress
while (!stop) {
switch (this.status) {
// block header
case Zlib.RawInflateStream.Status.INITIALIZED:
case Zlib.RawInflateStream.Status.BLOCK_HEADER_START:
if (this.readBlockHeader() < 0) {
stop = true;
}
break;
// block body
case Zlib.RawInflateStream.Status.BLOCK_HEADER_END: /* FALLTHROUGH */
case Zlib.RawInflateStream.Status.BLOCK_BODY_START:
switch(this.currentBlockType) {
case Zlib.RawInflateStream.BlockType.UNCOMPRESSED:
if (this.readUncompressedBlockHeader() < 0) {
stop = true;
}
break;
case Zlib.RawInflateStream.BlockType.FIXED:
if (this.parseFixedHuffmanBlock() < 0) {
stop = true;
}
break;
case Zlib.RawInflateStream.BlockType.DYNAMIC:
if (this.parseDynamicHuffmanBlock() < 0) {
stop = true;
}
break;
}
break;
// decode data
case Zlib.RawInflateStream.Status.BLOCK_BODY_END:
case Zlib.RawInflateStream.Status.DECODE_BLOCK_START:
switch(this.currentBlockType) {
case Zlib.RawInflateStream.BlockType.UNCOMPRESSED:
if (this.parseUncompressedBlock() < 0) {
stop = true;
}
break;
case Zlib.RawInflateStream.BlockType.FIXED: /* FALLTHROUGH */
case Zlib.RawInflateStream.BlockType.DYNAMIC:
if (this.decodeHuffman() < 0) {
stop = true;
}
break;
}
break;
case Zlib.RawInflateStream.Status.DECODE_BLOCK_END:
if (this.bfinal) {
stop = true;
} else {
this.status = Zlib.RawInflateStream.Status.INITIALIZED;
}
break;
}
}
return this.concatBuffer();
};
/**
* @const
* @type {number} max backward length for LZ77.
*/
Zlib.RawInflateStream.MaxBackwardLength = 32768;
/**
* @const
* @type {number} max copy length for LZ77.
*/
Zlib.RawInflateStream.MaxCopyLength = 258;
/**
* huffman order
* @const
* @type {!(Array.<number>|Uint8Array)}
*/
Zlib.RawInflateStream.Order = (function(table) {
return USE_TYPEDARRAY ? new Uint16Array(table) : table;
})([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
/**
* huffman length code table.
* @const
* @type {!(Array.<number>|Uint16Array)}
*/
Zlib.RawInflateStream.LengthCodeTable = (function(table) {
return USE_TYPEDARRAY ? new Uint16Array(table) : table;
})([
0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b,
0x000d, 0x000f, 0x0011, 0x0013, 0x0017, 0x001b, 0x001f, 0x0023, 0x002b,
0x0033, 0x003b, 0x0043, 0x0053, 0x0063, 0x0073, 0x0083, 0x00a3, 0x00c3,
0x00e3, 0x0102, 0x0102, 0x0102
]);
/**
* huffman length extra-bits table.
* @const
* @type {!(Array.<number>|Uint8Array)}
*/
Zlib.RawInflateStream.LengthExtraTable = (function(table) {
return USE_TYPEDARRAY ? new Uint8Array(table) : table;
})([
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5,
5, 5, 0, 0, 0
]);
/**
* huffman dist code table.
* @const
* @type {!(Array.<number>|Uint16Array)}
*/
Zlib.RawInflateStream.DistCodeTable = (function(table) {
return USE_TYPEDARRAY ? new Uint16Array(table) : table;
})([
0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0007, 0x0009, 0x000d, 0x0011,
0x0019, 0x0021, 0x0031, 0x0041, 0x0061, 0x0081, 0x00c1, 0x0101, 0x0181,
0x0201, 0x0301, 0x0401, 0x0601, 0x0801, 0x0c01, 0x1001, 0x1801, 0x2001,
0x3001, 0x4001, 0x6001
]);
/**
* huffman dist extra-bits table.
* @const
* @type {!(Array.<number>|Uint8Array)}
*/
Zlib.RawInflateStream.DistExtraTable = (function(table) {
return USE_TYPEDARRAY ? new Uint8Array(table) : table;
})([
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11,
11, 12, 12, 13, 13
]);
/**
* fixed huffman length code table
* @const
* @type {!Array}
*/
Zlib.RawInflateStream.FixedLiteralLengthTable = (function(table) {
return table;
})((function() {
var lengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(288);
var i, il;
for (i = 0, il = lengths.length; i < il; ++i) {
lengths[i] =
(i <= 143) ? 8 :
(i <= 255) ? 9 :
(i <= 279) ? 7 :
8;
}
return buildHuffmanTable(lengths);
})());
/**
* fixed huffman distance code table
* @const
* @type {!Array}
*/
Zlib.RawInflateStream.FixedDistanceTable = (function(table) {
return table;
})((function() {
var lengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(30);
var i, il;
for (i = 0, il = lengths.length; i < il; ++i) {
lengths[i] = 5;
}
return buildHuffmanTable(lengths);
})());
/**
* parse deflated block.
*/
Zlib.RawInflateStream.prototype.readBlockHeader = function() {
/** @type {number} header */
var hdr;
this.status = Zlib.RawInflateStream.Status.BLOCK_HEADER_START;
this.save_();
if ((hdr = this.readBits(3)) < 0) {
this.restore_();
return -1;
}
// BFINAL
if (hdr & 0x1) {
this.bfinal = true;
}
// BTYPE
hdr >>>= 1;
switch (hdr) {
case 0: // uncompressed
this.currentBlockType = Zlib.RawInflateStream.BlockType.UNCOMPRESSED;
break;
case 1: // fixed huffman
this.currentBlockType = Zlib.RawInflateStream.BlockType.FIXED;
break;
case 2: // dynamic huffman
this.currentBlockType = Zlib.RawInflateStream.BlockType.DYNAMIC;
break;
default: // reserved or other
throw new Error('unknown BTYPE: ' + hdr);
}
this.status = Zlib.RawInflateStream.Status.BLOCK_HEADER_END;
};
/**
* read inflate bits
* @param {number} length bits length.
* @return {number} read bits.
*/
Zlib.RawInflateStream.prototype.readBits = function(length) {
var bitsbuf = this.bitsbuf;
var bitsbuflen = this.bitsbuflen;
var input = this.input;
var ip = this.ip;
/** @type {number} input and output byte. */
var octet;
// not enough buffer
while (bitsbuflen < length) {
// input byte
octet = input[ip++];
if (octet === void 0) {
return -1;
}
// concat octet
bitsbuf |= octet << bitsbuflen;
bitsbuflen += 8;
}
// output byte
octet = bitsbuf & /* MASK */ ((1 << length) - 1);
bitsbuf >>>= length;
bitsbuflen -= length;
this.bitsbuf = bitsbuf;
this.bitsbuflen = bitsbuflen;
this.ip = ip;
return octet;
};
/**
* read huffman code using table
* @param {Array} table huffman code table.
* @return {number} huffman code.
*/
Zlib.RawInflateStream.prototype.readCodeByTable = function(table) {
var bitsbuf = this.bitsbuf;
var bitsbuflen = this.bitsbuflen;
var input = this.input;
var ip = this.ip;
/** @type {!(Array|Uint8Array)} huffman code table */
var codeTable = table[0];
/** @type {number} */
var maxCodeLength = table[1];
/** @type {number} input byte */
var octet;
/** @type {number} code */
var code;
/** @type {number} code length & code (16bit, 16bit) */
var codeWithLength;
/** @type {number} code bits length */
var codeLength;
// not enough buffer
while (bitsbuflen < maxCodeLength) {
octet = input[ip++];
if (octet === void 0) {
return -1;
}
bitsbuf |= octet << bitsbuflen;
bitsbuflen += 8;
}
// read max length
codeWithLength = codeTable[bitsbuf & ((1 << maxCodeLength) - 1)];
codeLength = codeWithLength >>> 16;
this.bitsbuf = bitsbuf >> codeLength;
this.bitsbuflen = bitsbuflen - codeLength;
this.ip = ip;
return codeWithLength & 0xffff;
};
/**
* read uncompressed block header
*/
Zlib.RawInflateStream.prototype.readUncompressedBlockHeader = function() {
/** @type {number} input byte. */
var octet;
/** @type {number} block length */
var len;
/** @type {number} number for check block length */
var nlen;
var input = this.input;
var ip = this.ip;
this.status = Zlib.RawInflateStream.Status.BLOCK_BODY_START;
// len (1st)
octet = input[ip++];
if (octet === void 0) {
return -1;
}
len = octet;
// len (2nd)
octet = input[ip++];
if (octet === void 0) {
return -1;
}
len |= octet << 8;
// nlen (1st)
octet = input[ip++];
if (octet === void 0) {
return -1;
}
nlen = octet;
// nlen (2nd)
octet = input[ip++];
if (octet === void 0) {
return -1;
}
nlen |= octet << 8;
// check len & nlen
if (len === ~nlen) {
throw new Error('invalid uncompressed block header: length verify');
}
// skip buffered header bits
this.bitsbuf = 0;
this.bitsbuflen = 0;
this.ip = ip;
this.blockLength = len;
this.status = Zlib.RawInflateStream.Status.BLOCK_BODY_END;
}
/**
* parse uncompressed block.
*/
Zlib.RawInflateStream.prototype.parseUncompressedBlock = function() {
var input = this.input;
var ip = this.ip;
var output = this.output;
var op = this.op;
var len = this.blockLength;
this.status = Zlib.RawInflateStream.Status.DECODE_BLOCK_START;
// copy
// XXX: とりあえず素直にコピー
while (len--) {
if (op === output.length) {
output = this.expandBuffer();
}
// not enough input buffer
if (input[ip] === void 0) {
this.ip = ip;
this.op = op;
this.blockLength = len + 1; // コピーしてないので戻す
return -1;
}
output[op++] = input[ip++];
}
if (len < 0) {
this.status = Zlib.RawInflateStream.Status.DECODE_BLOCK_END;
}
this.ip = ip;
this.op = op;
return 0;
};
/**
* parse fixed huffman block.
*/
Zlib.RawInflateStream.prototype.parseFixedHuffmanBlock = function() {
this.status = Zlib.RawInflateStream.Status.BLOCK_BODY_START;
this.litlenTable = Zlib.RawInflateStream.FixedLiteralLengthTable;
this.distTable = Zlib.RawInflateStream.FixedDistanceTable;
this.status = Zlib.RawInflateStream.Status.BLOCK_BODY_END;
return 0;
};
/**
* オブジェクトのコンテキストを別のプロパティに退避する.
* @private
*/
Zlib.RawInflateStream.prototype.save_ = function() {
this.ip_ = this.ip;
this.bitsbuflen_ = this.bitsbuflen;
this.bitsbuf_ = this.bitsbuf;
};
/**
* 別のプロパティに退避したコンテキストを復元する.
* @private
*/
Zlib.RawInflateStream.prototype.restore_ = function() {
this.ip = this.ip_;
this.bitsbuflen = this.bitsbuflen_;
this.bitsbuf = this.bitsbuf_;
};
/**
* parse dynamic huffman block.
*/
Zlib.RawInflateStream.prototype.parseDynamicHuffmanBlock = function() {
/** @type {number} number of literal and length codes. */
var hlit;
/** @type {number} number of distance codes. */
var hdist;
/** @type {number} number of code lengths. */
var hclen;
/** @type {!(Uint8Array|Array)} code lengths. */
var codeLengths =
new (USE_TYPEDARRAY ? Uint8Array : Array)(Zlib.RawInflateStream.Order.length);
/** @type {!Array} code lengths table. */
var codeLengthsTable;
/** @type {!(Uint32Array|Array)} literal and length code lengths. */
var litlenLengths;
/** @type {!(Uint32Array|Array)} distance code lengths. */
var distLengths;
/** @type {number} loop counter. */
var i = 0;
/** @type {number} loop counter. */
var j = 0;
this.status = Zlib.RawInflateStream.Status.BLOCK_BODY_START;
this.save_();
hlit = this.readBits(5) + 257;
hdist = this.readBits(5) + 1;
hclen = this.readBits(4) + 4;
if (hlit < 0 || hdist < 0 || hclen < 0) {
this.restore_();
return -1;
}
try {
parseDynamicHuffmanBlockImpl.call(this);
} catch(e) {
this.restore_();
return -1;
}
function parseDynamicHuffmanBlockImpl() {
/** @type {number} */
var bits;
// decode code lengths
for (i = 0; i < hclen; ++i) {
if ((bits = this.readBits(3)) < 0) {
throw new Error('not enough input');
}
codeLengths[Zlib.RawInflateStream.Order[i]] = bits;
}
codeLengthsTable = buildHuffmanTable(codeLengths);
// decode function
function decode(num, table, lengths) {
var code;
var prev;
var repeat;
var i = 0;
var bits;
for (i = 0; i < num;) {
code = this.readCodeByTable(table);
if (code < 0) {
throw new Error('not enough input');
}
switch (code) {
case 16:
if ((bits = this.readBits(2)) < 0) {
throw new Error('not enough input');
};
repeat = 3 + bits;
while (repeat--) { lengths[i++] = prev; }
break;
case 17:
if ((bits = this.readBits(3)) < 0) {
throw new Error('not enough input');
};
repeat = 3 + bits;
while (repeat--) { lengths[i++] = 0; }
prev = 0;
break;
case 18:
if ((bits = this.readBits(7)) < 0) {
throw new Error('not enough input');
};
repeat = 11 + bits;
while (repeat--) { lengths[i++] = 0; }
prev = 0;
break;
default:
lengths[i++] = code;
prev = code;
break;
}
}
return lengths;
}
// literal and length code
litlenLengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(hlit);
// distance code
distLengths = new (USE_TYPEDARRAY ? Uint8Array : Array)(hdist);
this.litlenTable = buildHuffmanTable(decode.call(this, hlit, codeLengthsTable, litlenLengths));
this.distTable = buildHuffmanTable(decode.call(this, hdist, codeLengthsTable, distLengths));
}
this.status = Zlib.RawInflateStream.Status.BLOCK_BODY_END;
return 0;
};
/**
* decode huffman code (dynamic)
* @return {(number|undefined)} -1 is error.
*/
Zlib.RawInflateStream.prototype.decodeHuffman = function() {
var output = this.output;
var op = this.op;
/** @type {number} huffman code. */
var code;
/** @type {number} table index. */
var ti;
/** @type {number} huffman code distination. */
var codeDist;
/** @type {number} huffman code length. */
var codeLength;
/** @type {number} buffer position. */
var bpos;
/** @type {number} pre-copy counter. */
var preCopy;
var litlen = this.litlenTable;
var dist = this.distTable;
var olength = output.length;
var bits;
this.status = Zlib.RawInflateStream.Status.DECODE_BLOCK_START;
while (true) {
this.save_();
code = this.readCodeByTable(litlen);
if (code < 0) {
this.op = op;
this.restore_();
return -1;
}
if (code === 256) {
break;
}
// literal
if (code < 256) {
if (op === olength) {
output = this.expandBuffer();
olength = output.length;
}
output[op++] = code;
continue;
}
// length code
ti = code - 257;
codeLength = Zlib.RawInflateStream.LengthCodeTable[ti];
if (Zlib.RawInflateStream.LengthExtraTable[ti] > 0) {
bits = this.readBits(Zlib.RawInflateStream.LengthExtraTable[ti]);
if (bits < 0) {
this.op = op;
this.restore_();
return -1;
}
codeLength += bits;
}
// dist code
code = this.readCodeByTable(dist);
if (code < 0) {
this.op = op;
this.restore_();
return -1;
}
codeDist = Zlib.RawInflateStream.DistCodeTable[code];
if (Zlib.RawInflateStream.DistExtraTable[code] > 0) {
bits = this.readBits(Zlib.RawInflateStream.DistExtraTable[code]);
if (bits < 0) {
this.op = op;
this.restore_();
return -1;
}
codeDist += bits;
}
// lz77 decode
if (op + codeLength >= olength) {
output = this.expandBuffer();
olength = output.length;
}
while (codeLength--) {
output[op] = output[(op++) - codeDist];
}
// break
if (this.ip === this.input.length) {
this.op = op;
return -1;
}
}
while (this.bitsbuflen >= 8) {
this.bitsbuflen -= 8;
this.ip--;
}
this.op = op;
this.status = Zlib.RawInflateStream.Status.DECODE_BLOCK_END;
};
/**
* expand output buffer. (dynamic)
* @param {Object=} opt_param option parameters.
* @return {!(Array|Uint8Array)} output buffer pointer.
*/
Zlib.RawInflateStream.prototype.expandBuffer = function(opt_param) {
/** @type {!(Array|Uint8Array)} store buffer. */
var buffer;
/** @type {number} expantion ratio. */
var ratio = (this.input.length / this.ip + 1) | 0;
/** @type {number} maximum number of huffman code. */
var maxHuffCode;
/** @type {number} new output buffer size. */
var newSize;
/** @type {number} max inflate size. */
var maxInflateSize;
var input = this.input;
var output = this.output;
if (opt_param) {
if (typeof opt_param.fixRatio === 'number') {
ratio = opt_param.fixRatio;
}
if (typeof opt_param.addRatio === 'number') {
ratio += opt_param.addRatio;
}
}
// calculate new buffer size
if (ratio < 2) {
maxHuffCode =
(input.length - this.ip) / this.litlenTable[2];
maxInflateSize = (maxHuffCode / 2 * 258) | 0;
newSize = maxInflateSize < output.length ?
output.length + maxInflateSize :
output.length << 1;
} else {
newSize = output.length * ratio;
}
// buffer expantion
if (USE_TYPEDARRAY) {
buffer = new Uint8Array(newSize);
buffer.set(output);
} else {
buffer = output;
}
this.output = buffer;
return this.output;
};
/**
* concat output buffer. (dynamic)
* @return {!(Array|Uint8Array)} output buffer.
*/
Zlib.RawInflateStream.prototype.concatBuffer = function() {
/** @type {!(Array|Uint8Array)} output buffer. */
var buffer;
var resize = this.resize;
var op = this.op;
if (resize) {
if (USE_TYPEDARRAY) {
buffer = new Uint8Array(op);
buffer.set(this.output.subarray(this.sp, op));
} else {
buffer = this.output.slice(this.sp, op);
}
} else {
buffer =
USE_TYPEDARRAY ? this.output.subarray(this.sp, op) : this.output.slice(this.sp, op);
}
this.buffer = buffer;
this.sp = op;
return this.buffer;
};
/**
* @return {!(Array|Uint8Array)} current output buffer.
*/
Zlib.RawInflateStream.prototype.getBytes = function() {
return USE_TYPEDARRAY ?
this.output.subarray(0, this.op) : this.output.slice(0, this.op);
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,565 @@
goog.provide('Zlib.Unzip');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.RawInflate');
goog.require('Zlib.CRC32');
goog.require('Zlib.Zip');
goog.scope(function() {
/**
* @param {!(Array.<number>|Uint8Array)} input input buffer.
* @param {Object=} opt_params options.
* @constructor
*/
Zlib.Unzip = function(input, opt_params) {
opt_params = opt_params || {};
/** @type {!(Array.<number>|Uint8Array)} */
this.input =
(USE_TYPEDARRAY && (input instanceof Array)) ?
new Uint8Array(input) : input;
/** @type {number} */
this.ip = 0;
/** @type {number} */
this.eocdrOffset;
/** @type {number} */
this.numberOfThisDisk;
/** @type {number} */
this.startDisk;
/** @type {number} */
this.totalEntriesThisDisk;
/** @type {number} */
this.totalEntries;
/** @type {number} */
this.centralDirectorySize;
/** @type {number} */
this.centralDirectoryOffset;
/** @type {number} */
this.commentLength;
/** @type {(Array.<number>|Uint8Array)} */
this.comment;
/** @type {Array.<Zlib.Unzip.FileHeader>} */
this.fileHeaderList;
/** @type {Object.<string, number>} */
this.filenameToIndex;
/** @type {boolean} */
this.verify = opt_params['verify'] || false;
/** @type {(Array.<number>|Uint8Array)} */
this.password = opt_params['password'];
};
Zlib.Unzip.CompressionMethod = Zlib.Zip.CompressionMethod;
/**
* @param {!(Array.<number>|Uint8Array)} input input buffer.
* @param {number} ip input position.
* @constructor
*/
Zlib.Unzip.FileHeader = function(input, ip) {
/** @type {!(Array.<number>|Uint8Array)} */
this.input = input;
/** @type {number} */
this.offset = ip;
/** @type {number} */
this.length;
/** @type {number} */
this.version;
/** @type {number} */
this.os;
/** @type {number} */
this.needVersion;
/** @type {number} */
this.flags;
/** @type {number} */
this.compression;
/** @type {number} */
this.time;
/** @type {number} */
this.date;
/** @type {number} */
this.crc32;
/** @type {number} */
this.compressedSize;
/** @type {number} */
this.plainSize;
/** @type {number} */
this.fileNameLength;
/** @type {number} */
this.extraFieldLength;
/** @type {number} */
this.fileCommentLength;
/** @type {number} */
this.diskNumberStart;
/** @type {number} */
this.internalFileAttributes;
/** @type {number} */
this.externalFileAttributes;
/** @type {number} */
this.relativeOffset;
/** @type {string} */
this.filename;
/** @type {!(Array.<number>|Uint8Array)} */
this.extraField;
/** @type {!(Array.<number>|Uint8Array)} */
this.comment;
};
Zlib.Unzip.FileHeader.prototype.parse = function() {
/** @type {!(Array.<number>|Uint8Array)} */
var input = this.input;
/** @type {number} */
var ip = this.offset;
// central file header signature
if (input[ip++] !== 0x50 || input[ip++] !== 0x4b ||
input[ip++] !== 0x01 || input[ip++] !== 0x02) {
throw new Error('invalid file header signature');
}
// version made by
this.version = input[ip++];
this.os = input[ip++];
// version needed to extract
this.needVersion = input[ip++] | (input[ip++] << 8);
// general purpose bit flag
this.flags = input[ip++] | (input[ip++] << 8);
// compression method
this.compression = input[ip++] | (input[ip++] << 8);
// last mod file time
this.time = input[ip++] | (input[ip++] << 8);
//last mod file date
this.date = input[ip++] | (input[ip++] << 8);
// crc-32
this.crc32 = (
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24)
) >>> 0;
// compressed size
this.compressedSize =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// uncompressed size
this.plainSize =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// file name length
this.fileNameLength = input[ip++] | (input[ip++] << 8);
// extra field length
this.extraFieldLength = input[ip++] | (input[ip++] << 8);
// file comment length
this.fileCommentLength = input[ip++] | (input[ip++] << 8);
// disk number start
this.diskNumberStart = input[ip++] | (input[ip++] << 8);
// internal file attributes
this.internalFileAttributes = input[ip++] | (input[ip++] << 8);
// external file attributes
this.externalFileAttributes =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// relative offset of local header
this.relativeOffset =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// file name
this.filename = String.fromCharCode.apply(null, USE_TYPEDARRAY ?
input.subarray(ip, ip += this.fileNameLength) :
input.slice(ip, ip += this.fileNameLength)
);
// extra field
this.extraField = USE_TYPEDARRAY ?
input.subarray(ip, ip += this.extraFieldLength) :
input.slice(ip, ip += this.extraFieldLength);
// file comment
this.comment = USE_TYPEDARRAY ?
input.subarray(ip, ip + this.fileCommentLength) :
input.slice(ip, ip + this.fileCommentLength);
this.length = ip - this.offset;
};
/**
* @param {!(Array.<number>|Uint8Array)} input input buffer.
* @param {number} ip input position.
* @constructor
*/
Zlib.Unzip.LocalFileHeader = function(input, ip) {
/** @type {!(Array.<number>|Uint8Array)} */
this.input = input;
/** @type {number} */
this.offset = ip;
/** @type {number} */
this.length;
/** @type {number} */
this.needVersion;
/** @type {number} */
this.flags;
/** @type {number} */
this.compression;
/** @type {number} */
this.time;
/** @type {number} */
this.date;
/** @type {number} */
this.crc32;
/** @type {number} */
this.compressedSize;
/** @type {number} */
this.plainSize;
/** @type {number} */
this.fileNameLength;
/** @type {number} */
this.extraFieldLength;
/** @type {string} */
this.filename;
/** @type {!(Array.<number>|Uint8Array)} */
this.extraField;
};
Zlib.Unzip.LocalFileHeader.Flags = Zlib.Zip.Flags;
Zlib.Unzip.LocalFileHeader.prototype.parse = function() {
/** @type {!(Array.<number>|Uint8Array)} */
var input = this.input;
/** @type {number} */
var ip = this.offset;
// local file header signature
if (input[ip++] !== 0x50 || input[ip++] !== 0x4b ||
input[ip++] !== 0x03 || input[ip++] !== 0x04) {
throw new Error('invalid local file header signature');
}
// version needed to extract
this.needVersion = input[ip++] | (input[ip++] << 8);
// general purpose bit flag
this.flags = input[ip++] | (input[ip++] << 8);
// compression method
this.compression = input[ip++] | (input[ip++] << 8);
// last mod file time
this.time = input[ip++] | (input[ip++] << 8);
//last mod file date
this.date = input[ip++] | (input[ip++] << 8);
// crc-32
this.crc32 = (
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24)
) >>> 0;
// compressed size
this.compressedSize =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// uncompressed size
this.plainSize =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// file name length
this.fileNameLength = input[ip++] | (input[ip++] << 8);
// extra field length
this.extraFieldLength = input[ip++] | (input[ip++] << 8);
// file name
this.filename = String.fromCharCode.apply(null, USE_TYPEDARRAY ?
input.subarray(ip, ip += this.fileNameLength) :
input.slice(ip, ip += this.fileNameLength)
);
// extra field
this.extraField = USE_TYPEDARRAY ?
input.subarray(ip, ip += this.extraFieldLength) :
input.slice(ip, ip += this.extraFieldLength);
this.length = ip - this.offset;
};
Zlib.Unzip.prototype.searchEndOfCentralDirectoryRecord = function() {
/** @type {!(Array.<number>|Uint8Array)} */
var input = this.input;
/** @type {number} */
var ip;
for (ip = input.length - 12; ip > 0; --ip) {
if (input[ip ] === 0x50 && input[ip+1] === 0x4b &&
input[ip+2] === 0x05 && input[ip+3] === 0x06) {
this.eocdrOffset = ip;
return;
}
}
throw new Error('End of Central Directory Record not found');
};
Zlib.Unzip.prototype.parseEndOfCentralDirectoryRecord = function() {
/** @type {!(Array.<number>|Uint8Array)} */
var input = this.input;
/** @type {number} */
var ip;
if (!this.eocdrOffset) {
this.searchEndOfCentralDirectoryRecord();
}
ip = this.eocdrOffset;
// signature
if (input[ip++] !== 0x50 || input[ip++] !== 0x4b ||
input[ip++] !== 0x05 || input[ip++] !== 0x06) {
throw new Error('invalid signature');
}
// number of this disk
this.numberOfThisDisk = input[ip++] | (input[ip++] << 8);
// number of the disk with the start of the central directory
this.startDisk = input[ip++] | (input[ip++] << 8);
// total number of entries in the central directory on this disk
this.totalEntriesThisDisk = input[ip++] | (input[ip++] << 8);
// total number of entries in the central directory
this.totalEntries = input[ip++] | (input[ip++] << 8);
// size of the central directory
this.centralDirectorySize =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// offset of start of central directory with respect to the starting disk number
this.centralDirectoryOffset =
(input[ip++] ) | (input[ip++] << 8) |
(input[ip++] << 16) | (input[ip++] << 24);
// .ZIP file comment length
this.commentLength = input[ip++] | (input[ip++] << 8);
// .ZIP file comment
this.comment = USE_TYPEDARRAY ?
input.subarray(ip, ip + this.commentLength) :
input.slice(ip, ip + this.commentLength);
};
Zlib.Unzip.prototype.parseFileHeader = function() {
/** @type {Array.<Zlib.Unzip.FileHeader>} */
var filelist = [];
/** @type {Object.<string, number>} */
var filetable = {};
/** @type {number} */
var ip;
/** @type {Zlib.Unzip.FileHeader} */
var fileHeader;
/*: @type {number} */
var i;
/*: @type {number} */
var il;
if (this.fileHeaderList) {
return;
}
if (this.centralDirectoryOffset === void 0) {
this.parseEndOfCentralDirectoryRecord();
}
ip = this.centralDirectoryOffset;
for (i = 0, il = this.totalEntries; i < il; ++i) {
fileHeader = new Zlib.Unzip.FileHeader(this.input, ip);
fileHeader.parse();
ip += fileHeader.length;
filelist[i] = fileHeader;
filetable[fileHeader.filename] = i;
}
if (this.centralDirectorySize < ip - this.centralDirectoryOffset) {
throw new Error('invalid file header size');
}
this.fileHeaderList = filelist;
this.filenameToIndex = filetable;
};
/**
* @param {number} index file header index.
* @return {!(Array.<number>|Uint8Array)} file data.
*/
Zlib.Unzip.prototype.getFileData = function(index) {
/** @type {!(Array.<number>|Uint8Array)} */
var input = this.input;
/** @type {Array.<Zlib.Unzip.FileHeader>} */
var fileHeaderList = this.fileHeaderList;
/** @type {Zlib.Unzip.LocalFileHeader} */
var localFileHeader;
/** @type {number} */
var offset;
/** @type {number} */
var length;
/** @type {!(Array.<number>|Uint8Array)} */
var buffer;
/** @type {number} */
var crc32;
/** @type {Array.<number>|Uint32Array} */
var key;
/** @type {number} */
var i;
/** @type {number} */
var il;
if (!fileHeaderList) {
this.parseFileHeader();
}
if (fileHeaderList[index] === void 0) {
throw new Error('wrong index');
}
offset = fileHeaderList[index].relativeOffset;
localFileHeader = new Zlib.Unzip.LocalFileHeader(this.input, offset);
localFileHeader.parse();
offset += localFileHeader.length;
length = localFileHeader.compressedSize;
// decryption
if ((localFileHeader.flags & Zlib.Unzip.LocalFileHeader.Flags.ENCRYPT) !== 0) {
if (!this.password) {
throw new Error('please set password');
}
key = this.createDecryptionKey(this.password);
// encryption header
for(i = offset, il = offset + 12; i < il; ++i) {
this.decode(key, input[i]);
}
offset += 12;
length -= 12;
// decryption
for (i = offset, il = offset + length; i < il; ++i) {
input[i] = this.decode(key, input[i]);
}
}
switch (localFileHeader.compression) {
case Zlib.Unzip.CompressionMethod.STORE:
buffer = USE_TYPEDARRAY ?
this.input.subarray(offset, offset + length) :
this.input.slice(offset, offset + length);
break;
case Zlib.Unzip.CompressionMethod.DEFLATE:
buffer = new Zlib.RawInflate(this.input, {
'index': offset,
'bufferSize': localFileHeader.plainSize
}).decompress();
break;
default:
throw new Error('unknown compression type');
}
if (this.verify) {
crc32 = Zlib.CRC32.calc(buffer);
if (localFileHeader.crc32 !== crc32) {
throw new Error(
'wrong crc: file=0x' + localFileHeader.crc32.toString(16) +
', data=0x' + crc32.toString(16)
);
}
}
return buffer;
};
/**
* @return {Array.<string>}
*/
Zlib.Unzip.prototype.getFilenames = function() {
/** @type {Array.<string>} */
var filenameList = [];
/** @type {number} */
var i;
/** @type {number} */
var il;
/** @type {Array.<Zlib.Unzip.FileHeader>} */
var fileHeaderList;
if (!this.fileHeaderList) {
this.parseFileHeader();
}
fileHeaderList = this.fileHeaderList;
for (i = 0, il = fileHeaderList.length; i < il; ++i) {
filenameList[i] = fileHeaderList[i].filename;
}
return filenameList;
};
/**
* @param {string} filename extract filename.
* @return {!(Array.<number>|Uint8Array)} decompressed data.
*/
Zlib.Unzip.prototype.decompress = function(filename) {
/** @type {number} */
var index;
if (!this.filenameToIndex) {
this.parseFileHeader();
}
index = this.filenameToIndex[filename];
if (index === void 0) {
throw new Error(filename + ' not found');
}
return this.getFileData(index);
};
/**
* @param {(Array.<number>|Uint8Array)} password
*/
Zlib.Unzip.prototype.setPassword = function(password) {
this.password = password;
};
/**
* @param {(Array.<number>|Uint32Array)} key
* @param {number} n
* @returns {number}
*/
Zlib.Unzip.prototype.decode = function(key, n) {
n ^= this.getByte(key);
this.updateKeys(key, n);
return n;
};
// common method
Zlib.Unzip.prototype.updateKeys = Zlib.Zip.prototype.updateKeys;
Zlib.Unzip.prototype.createDecryptionKey = Zlib.Zip.prototype.createEncryptionKey;
Zlib.Unzip.prototype.getByte = Zlib.Zip.prototype.getByte;
// end of scope
});

View File

@ -0,0 +1,31 @@
/**
* @fileoverview 雑多な関数群をまとめたモジュール実装.
*/
goog.provide('Zlib.Util');
goog.scope(function() {
/**
* Byte String から Byte Array に変換.
* @param {!string} str byte string.
* @return {!Array.<number>} byte array.
*/
Zlib.Util.stringToByteArray = function(str) {
/** @type {!Array.<(string|number)>} */
var tmp = str.split('');
/** @type {number} */
var i;
/** @type {number} */
var il;
for (i = 0, il = tmp.length; i < il; i++) {
tmp[i] = (tmp[i].charCodeAt(0) & 0xff) >>> 0;
}
return tmp;
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */

View File

@ -0,0 +1,571 @@
goog.provide('Zlib.Zip');
goog.require('USE_TYPEDARRAY');
goog.require('Zlib.RawDeflate');
goog.require('Zlib.CRC32');
goog.scope(function() {
/**
* @param {Object=} opt_params options.
* @constructor
*/
Zlib.Zip = function(opt_params) {
opt_params = opt_params || {};
/** @type {Array.<{
* buffer: !(Array.<number>|Uint8Array),
* option: Object,
* compressed: boolean,
* encrypted: boolean,
* size: number,
* crc32: number
* }>} */
this.files = [];
/** @type {(Array.<number>|Uint8Array)} */
this.comment = opt_params['comment'];
/** @type {(Array.<number>|Uint8Array)} */
this.password;
};
/**
* @enum {number}
*/
Zlib.Zip.CompressionMethod = {
STORE: 0,
DEFLATE: 8
};
/**
* @enum {number}
*/
Zlib.Zip.OperatingSystem = {
MSDOS: 0,
UNIX: 3,
MACINTOSH: 7
};
/**
* @enum {number}
*/
Zlib.Zip.Flags = {
ENCRYPT: 0x0001,
DESCRIPTER: 0x0008,
UTF8: 0x0800
};
/**
* @param {Array.<number>|Uint8Array} input
* @param {Object=} opt_params options.
*/
Zlib.Zip.prototype.addFile = function(input, opt_params) {
opt_params = opt_params || {};
/** @type {string} */
var filename = '' || opt_params['filename'];
/** @type {boolean} */
var compressed;
/** @type {number} */
var size = input.length;
/** @type {number} */
var crc32 = 0;
if (USE_TYPEDARRAY && input instanceof Array) {
input = new Uint8Array(input);
}
// default
if (typeof opt_params['compressionMethod'] !== 'number') {
opt_params['compressionMethod'] = Zlib.Zip.CompressionMethod.DEFLATE;
}
// その場で圧縮する場合
if (opt_params['compress']) {
switch (opt_params['compressionMethod']) {
case Zlib.Zip.CompressionMethod.STORE:
break;
case Zlib.Zip.CompressionMethod.DEFLATE:
crc32 = Zlib.CRC32.calc(input);
input = this.deflateWithOption(input, opt_params);
compressed = true;
break;
default:
throw new Error('unknown compression method:' + opt_params['compressionMethod']);
}
}
this.files.push({
buffer: input,
option: opt_params,
compressed: compressed,
encrypted: false,
size: size,
crc32: crc32
});
};
/**
* @param {(Array.<number>|Uint8Array)} password
*/
Zlib.Zip.prototype.setPassword = function(password) {
this.password = password;
};
Zlib.Zip.prototype.compress = function() {
/** @type {Array.<{
* buffer: !(Array.<number>|Uint8Array),
* option: Object,
* compressed: boolean,
* encrypted: boolean,
* size: number,
* crc32: number
* }>} */
var files = this.files;
/** @type {{
* buffer: !(Array.<number>|Uint8Array),
* option: Object,
* compressed: boolean,
* encrypted: boolean,
* size: number,
* crc32: number
* }} */
var file;
/** @type {!(Array.<number>|Uint8Array)} */
var output;
/** @type {number} */
var op1;
/** @type {number} */
var op2;
/** @type {number} */
var op3;
/** @type {number} */
var localFileSize = 0;
/** @type {number} */
var centralDirectorySize = 0;
/** @type {number} */
var endOfCentralDirectorySize;
/** @type {number} */
var offset;
/** @type {number} */
var needVersion;
/** @type {number} */
var flags;
/** @type {Zlib.Zip.CompressionMethod} */
var compressionMethod;
/** @type {Date} */
var date;
/** @type {number} */
var crc32;
/** @type {number} */
var size;
/** @type {number} */
var plainSize;
/** @type {number} */
var filenameLength;
/** @type {number} */
var extraFieldLength;
/** @type {number} */
var commentLength;
/** @type {(Array.<number>|Uint8Array)} */
var filename;
/** @type {(Array.<number>|Uint8Array)} */
var extraField;
/** @type {(Array.<number>|Uint8Array)} */
var comment;
/** @type {(Array.<number>|Uint8Array)} */
var buffer;
/** @type {*} */
var tmp;
/** @type {Array.<number>|Uint32Array} */
var key;
/** @type {number} */
var i;
/** @type {number} */
var il;
/** @type {number} */
var j;
/** @type {number} */
var jl;
// ファイルの圧縮
for (i = 0, il = files.length; i < il; ++i) {
file = files[i];
filenameLength =
(file.option['filename']) ? file.option['filename'].length : 0;
extraFieldLength =
(file.option['extraField']) ? file.option['extraField'].length : 0;
commentLength =
(file.option['comment']) ? file.option['comment'].length : 0;
// 圧縮されていなかったら圧縮
if (!file.compressed) {
// 圧縮前に CRC32 の計算をしておく
file.crc32 = Zlib.CRC32.calc(file.buffer);
switch (file.option['compressionMethod']) {
case Zlib.Zip.CompressionMethod.STORE:
break;
case Zlib.Zip.CompressionMethod.DEFLATE:
file.buffer = this.deflateWithOption(file.buffer, file.option);
file.compressed = true;
break;
default:
throw new Error('unknown compression method:' + file.option['compressionMethod']);
}
}
// encryption
if (this.password !== void 0) {
// init encryption
key = this.createEncryptionKey(this.password);
// add header
buffer = file.buffer;
if (USE_TYPEDARRAY) {
tmp = new Uint8Array(buffer.length + 12);
tmp.set(buffer, 12);
buffer = tmp;
} else {
buffer.unshift(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
}
for (j = 0; j < 12; ++j) {
buffer[j] = this.encode(
key,
i === 11 ? (file.crc32 & 0xff) : (Math.random() * 256 | 0)
);
}
// data encryption
for (jl = buffer.length; j < jl; ++j) {
buffer[j] = this.encode(key, buffer[j]);
}
file.buffer = buffer;
}
// 必要バッファサイズの計算
localFileSize +=
// local file header
30 + filenameLength +
// file data
file.buffer.length;
centralDirectorySize +=
// file header
46 + filenameLength + commentLength;
}
// end of central directory
endOfCentralDirectorySize = 46 + (this.comment ? this.comment.length : 0);
output = new (USE_TYPEDARRAY ? Uint8Array : Array)(
localFileSize + centralDirectorySize + endOfCentralDirectorySize
);
op1 = 0;
op2 = localFileSize;
op3 = op2 + centralDirectorySize;
// ファイルの圧縮
for (i = 0, il = files.length; i < il; ++i) {
file = files[i];
filenameLength =
file.option['filename'] ? file.option['filename'].length : 0;
extraFieldLength = 0; // TODO
commentLength =
file.option['comment'] ? file.option['comment'].length : 0;
//-------------------------------------------------------------------------
// local file header & file header
//-------------------------------------------------------------------------
offset = op1;
// signature
output[op1++] = output[op2++] = 0x50;
output[op1++] = output[op2++] = 0x4b;
output[op1++] = 0x03; // local file header
output[op1++] = 0x04;
output[op2++] = 0x01; // file header
output[op2++] = 0x02;
// compressor info
needVersion = 20;
output[op2++] = needVersion & 0xff;
output[op2++] =
/** @type {Zlib.Zip.OperatingSystem} */
(file.option['os']) ||
Zlib.Zip.OperatingSystem.MSDOS;
// need version
output[op1++] = output[op2++] = needVersion & 0xff;
output[op1++] = output[op2++] = (needVersion >> 8) & 0xff;
// general purpose bit flag
flags = 0;
if (this.password) {
flags |= Zlib.Zip.Flags.ENCRYPT;
}
output[op1++] = output[op2++] = flags & 0xff;
output[op1++] = output[op2++] = (flags >> 8) & 0xff;
// compression method
compressionMethod =
/** @type {Zlib.Zip.CompressionMethod} */
(file.option['compressionMethod']);
output[op1++] = output[op2++] = compressionMethod & 0xff;
output[op1++] = output[op2++] = (compressionMethod >> 8) & 0xff;
// date
date = /** @type {(Date|undefined)} */(file.option['date']) || new Date();
output[op1++] = output[op2++] =
((date.getMinutes() & 0x7) << 5) |
(date.getSeconds() / 2 | 0);
output[op1++] = output[op2++] =
(date.getHours() << 3) |
(date.getMinutes() >> 3);
//
output[op1++] = output[op2++] =
((date.getMonth() + 1 & 0x7) << 5) |
(date.getDate());
output[op1++] = output[op2++] =
((date.getFullYear() - 1980 & 0x7f) << 1) |
(date.getMonth() + 1 >> 3);
// CRC-32
crc32 = file.crc32;
output[op1++] = output[op2++] = crc32 & 0xff;
output[op1++] = output[op2++] = (crc32 >> 8) & 0xff;
output[op1++] = output[op2++] = (crc32 >> 16) & 0xff;
output[op1++] = output[op2++] = (crc32 >> 24) & 0xff;
// compressed size
size = file.buffer.length;
output[op1++] = output[op2++] = size & 0xff;
output[op1++] = output[op2++] = (size >> 8) & 0xff;
output[op1++] = output[op2++] = (size >> 16) & 0xff;
output[op1++] = output[op2++] = (size >> 24) & 0xff;
// uncompressed size
plainSize = file.size;
output[op1++] = output[op2++] = plainSize & 0xff;
output[op1++] = output[op2++] = (plainSize >> 8) & 0xff;
output[op1++] = output[op2++] = (plainSize >> 16) & 0xff;
output[op1++] = output[op2++] = (plainSize >> 24) & 0xff;
// filename length
output[op1++] = output[op2++] = filenameLength & 0xff;
output[op1++] = output[op2++] = (filenameLength >> 8) & 0xff;
// extra field length
output[op1++] = output[op2++] = extraFieldLength & 0xff;
output[op1++] = output[op2++] = (extraFieldLength >> 8) & 0xff;
// file comment length
output[op2++] = commentLength & 0xff;
output[op2++] = (commentLength >> 8) & 0xff;
// disk number start
output[op2++] = 0;
output[op2++] = 0;
// internal file attributes
output[op2++] = 0;
output[op2++] = 0;
// external file attributes
output[op2++] = 0;
output[op2++] = 0;
output[op2++] = 0;
output[op2++] = 0;
// relative offset of local header
output[op2++] = offset & 0xff;
output[op2++] = (offset >> 8) & 0xff;
output[op2++] = (offset >> 16) & 0xff;
output[op2++] = (offset >> 24) & 0xff;
// filename
filename = file.option['filename'];
if (filename) {
if (USE_TYPEDARRAY) {
output.set(filename, op1);
output.set(filename, op2);
op1 += filenameLength;
op2 += filenameLength;
} else {
for (j = 0; j < filenameLength; ++j) {
output[op1++] = output[op2++] = filename[j];
}
}
}
// extra field
extraField = file.option['extraField'];
if (extraField) {
if (USE_TYPEDARRAY) {
output.set(extraField, op1);
output.set(extraField, op2);
op1 += extraFieldLength;
op2 += extraFieldLength;
} else {
for (j = 0; j < commentLength; ++j) {
output[op1++] = output[op2++] = extraField[j];
}
}
}
// comment
comment = file.option['comment'];
if (comment) {
if (USE_TYPEDARRAY) {
output.set(comment, op2);
op2 += commentLength;
} else {
for (j = 0; j < commentLength; ++j) {
output[op2++] = comment[j];
}
}
}
//-------------------------------------------------------------------------
// file data
//-------------------------------------------------------------------------
if (USE_TYPEDARRAY) {
output.set(file.buffer, op1);
op1 += file.buffer.length;
} else {
for (j = 0, jl = file.buffer.length; j < jl; ++j) {
output[op1++] = file.buffer[j];
}
}
}
//-------------------------------------------------------------------------
// end of central directory
//-------------------------------------------------------------------------
// signature
output[op3++] = 0x50;
output[op3++] = 0x4b;
output[op3++] = 0x05;
output[op3++] = 0x06;
// number of this disk
output[op3++] = 0;
output[op3++] = 0;
// number of the disk with the start of the central directory
output[op3++] = 0;
output[op3++] = 0;
// total number of entries in the central directory on this disk
output[op3++] = il & 0xff;
output[op3++] = (il >> 8) & 0xff;
// total number of entries in the central directory
output[op3++] = il & 0xff;
output[op3++] = (il >> 8) & 0xff;
// size of the central directory
output[op3++] = centralDirectorySize & 0xff;
output[op3++] = (centralDirectorySize >> 8) & 0xff;
output[op3++] = (centralDirectorySize >> 16) & 0xff;
output[op3++] = (centralDirectorySize >> 24) & 0xff;
// offset of start of central directory with respect to the starting disk number
output[op3++] = localFileSize & 0xff;
output[op3++] = (localFileSize >> 8) & 0xff;
output[op3++] = (localFileSize >> 16) & 0xff;
output[op3++] = (localFileSize >> 24) & 0xff;
// .ZIP file comment length
commentLength = this.comment ? this.comment.length : 0;
output[op3++] = commentLength & 0xff;
output[op3++] = (commentLength >> 8) & 0xff;
// .ZIP file comment
if (this.comment) {
if (USE_TYPEDARRAY) {
output.set(this.comment, op3);
op3 += commentLength;
} else {
for (j = 0, jl = commentLength; j < jl; ++j) {
output[op3++] = this.comment[j];
}
}
}
return output;
};
/**
* @param {!(Array.<number>|Uint8Array)} input
* @param {Object=} opt_params options.
* @return {!(Array.<number>|Uint8Array)}
*/
Zlib.Zip.prototype.deflateWithOption = function(input, opt_params) {
/** @type {Zlib.RawDeflate} */
var deflator = new Zlib.RawDeflate(input, opt_params['deflateOption']);
return deflator.compress();
};
/**
* @param {(Array.<number>|Uint32Array)} key
* @returns {number}
*/
Zlib.Zip.prototype.getByte = function(key) {
/** @type {number} */
var tmp = ((key[2] & 0xffff) | 2);
return ((tmp * (tmp ^ 1)) >> 8) & 0xff;
};
/**
* @param {(Array.<number>|Uint32Array)} key
* @param {number} n
* @returns {number}
*/
Zlib.Zip.prototype.encode = function(key, n) {
/** @type {number} */
var tmp = this.getByte(key);
this.updateKeys(key, n);
return tmp ^ n;
};
/**
* @param {(Array.<number>|Uint32Array)} key
* @param {number} n
*/
Zlib.Zip.prototype.updateKeys = function(key, n) {
key[0] = Zlib.CRC32.single(key[0], n);
key[1] =
(((((key[1] + (key[0] & 0xff)) * 20173 >>> 0) * 6681) >>> 0) + 1) >>> 0;
key[2] = Zlib.CRC32.single(key[2], key[1] >>> 24);
};
/**
* @param {(Array.<number>|Uint8Array)}password
* @returns {Array.<number>|Uint32Array}
*/
Zlib.Zip.prototype.createEncryptionKey = function(password) {
/** @type {(Array.<number>|Uint32Array)} */
var key = [305419896, 591751049, 878082192];
/** @type {number} */
var i;
/** @type {number} */
var il;
if (USE_TYPEDARRAY) {
key = new Uint32Array(key);
}
for (i = 0, il = password.length; i < il; ++i) {
this.updateKeys(key, password[i] & 0xff);
}
return key;
};
});

View File

@ -0,0 +1,24 @@
/**
* @fileoverview Zlib namespace. Zlib の仕様に準拠した圧縮は Zlib.Deflate で実装
* されている. これは Inflate との共存を考慮している為.
*/
goog.provide('Zlib');
//-----------------------------------------------------------------------------
goog.scope(function() {
/**
* Compression Method
* @enum {number}
*/
Zlib.CompressionMethod = {
DEFLATE: 8,
RESERVED: 15
};
// end of scope
});
/* vim:set expandtab ts=2 sw=2 tw=80: */