選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

binding.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. 'use strict';
  2. /* eslint camelcase: "off" */
  3. var assert = require('assert');
  4. var Zstream = require('pako/lib/zlib/zstream');
  5. var zlib_deflate = require('pako/lib/zlib/deflate.js');
  6. var zlib_inflate = require('pako/lib/zlib/inflate.js');
  7. var constants = require('pako/lib/zlib/constants');
  8. for (var key in constants) {
  9. exports[key] = constants[key];
  10. }
  11. // zlib modes
  12. exports.NONE = 0;
  13. exports.DEFLATE = 1;
  14. exports.INFLATE = 2;
  15. exports.GZIP = 3;
  16. exports.GUNZIP = 4;
  17. exports.DEFLATERAW = 5;
  18. exports.INFLATERAW = 6;
  19. exports.UNZIP = 7;
  20. var GZIP_HEADER_ID1 = 0x1f;
  21. var GZIP_HEADER_ID2 = 0x8b;
  22. /**
  23. * Emulate Node's zlib C++ layer for use by the JS layer in index.js
  24. */
  25. function Zlib(mode) {
  26. if (typeof mode !== 'number' || mode < exports.DEFLATE || mode > exports.UNZIP) {
  27. throw new TypeError('Bad argument');
  28. }
  29. this.dictionary = null;
  30. this.err = 0;
  31. this.flush = 0;
  32. this.init_done = false;
  33. this.level = 0;
  34. this.memLevel = 0;
  35. this.mode = mode;
  36. this.strategy = 0;
  37. this.windowBits = 0;
  38. this.write_in_progress = false;
  39. this.pending_close = false;
  40. this.gzip_id_bytes_read = 0;
  41. }
  42. Zlib.prototype.close = function () {
  43. if (this.write_in_progress) {
  44. this.pending_close = true;
  45. return;
  46. }
  47. this.pending_close = false;
  48. assert(this.init_done, 'close before init');
  49. assert(this.mode <= exports.UNZIP);
  50. if (this.mode === exports.DEFLATE || this.mode === exports.GZIP || this.mode === exports.DEFLATERAW) {
  51. zlib_deflate.deflateEnd(this.strm);
  52. } else if (this.mode === exports.INFLATE || this.mode === exports.GUNZIP || this.mode === exports.INFLATERAW || this.mode === exports.UNZIP) {
  53. zlib_inflate.inflateEnd(this.strm);
  54. }
  55. this.mode = exports.NONE;
  56. this.dictionary = null;
  57. };
  58. Zlib.prototype.write = function (flush, input, in_off, in_len, out, out_off, out_len) {
  59. return this._write(true, flush, input, in_off, in_len, out, out_off, out_len);
  60. };
  61. Zlib.prototype.writeSync = function (flush, input, in_off, in_len, out, out_off, out_len) {
  62. return this._write(false, flush, input, in_off, in_len, out, out_off, out_len);
  63. };
  64. Zlib.prototype._write = function (async, flush, input, in_off, in_len, out, out_off, out_len) {
  65. assert.equal(arguments.length, 8);
  66. assert(this.init_done, 'write before init');
  67. assert(this.mode !== exports.NONE, 'already finalized');
  68. assert.equal(false, this.write_in_progress, 'write already in progress');
  69. assert.equal(false, this.pending_close, 'close is pending');
  70. this.write_in_progress = true;
  71. assert.equal(false, flush === undefined, 'must provide flush value');
  72. this.write_in_progress = true;
  73. if (flush !== exports.Z_NO_FLUSH && flush !== exports.Z_PARTIAL_FLUSH && flush !== exports.Z_SYNC_FLUSH && flush !== exports.Z_FULL_FLUSH && flush !== exports.Z_FINISH && flush !== exports.Z_BLOCK) {
  74. throw new Error('Invalid flush value');
  75. }
  76. if (input == null) {
  77. input = Buffer.alloc(0);
  78. in_len = 0;
  79. in_off = 0;
  80. }
  81. this.strm.avail_in = in_len;
  82. this.strm.input = input;
  83. this.strm.next_in = in_off;
  84. this.strm.avail_out = out_len;
  85. this.strm.output = out;
  86. this.strm.next_out = out_off;
  87. this.flush = flush;
  88. if (!async) {
  89. // sync version
  90. this._process();
  91. if (this._checkError()) {
  92. return this._afterSync();
  93. }
  94. return;
  95. }
  96. // async version
  97. var self = this;
  98. process.nextTick(function () {
  99. self._process();
  100. self._after();
  101. });
  102. return this;
  103. };
  104. Zlib.prototype._afterSync = function () {
  105. var avail_out = this.strm.avail_out;
  106. var avail_in = this.strm.avail_in;
  107. this.write_in_progress = false;
  108. return [avail_in, avail_out];
  109. };
  110. Zlib.prototype._process = function () {
  111. var next_expected_header_byte = null;
  112. // If the avail_out is left at 0, then it means that it ran out
  113. // of room. If there was avail_out left over, then it means
  114. // that all of the input was consumed.
  115. switch (this.mode) {
  116. case exports.DEFLATE:
  117. case exports.GZIP:
  118. case exports.DEFLATERAW:
  119. this.err = zlib_deflate.deflate(this.strm, this.flush);
  120. break;
  121. case exports.UNZIP:
  122. if (this.strm.avail_in > 0) {
  123. next_expected_header_byte = this.strm.next_in;
  124. }
  125. switch (this.gzip_id_bytes_read) {
  126. case 0:
  127. if (next_expected_header_byte === null) {
  128. break;
  129. }
  130. if (this.strm.input[next_expected_header_byte] === GZIP_HEADER_ID1) {
  131. this.gzip_id_bytes_read = 1;
  132. next_expected_header_byte++;
  133. if (this.strm.avail_in === 1) {
  134. // The only available byte was already read.
  135. break;
  136. }
  137. } else {
  138. this.mode = exports.INFLATE;
  139. break;
  140. }
  141. // fallthrough
  142. case 1:
  143. if (next_expected_header_byte === null) {
  144. break;
  145. }
  146. if (this.strm.input[next_expected_header_byte] === GZIP_HEADER_ID2) {
  147. this.gzip_id_bytes_read = 2;
  148. this.mode = exports.GUNZIP;
  149. } else {
  150. // There is no actual difference between INFLATE and INFLATERAW
  151. // (after initialization).
  152. this.mode = exports.INFLATE;
  153. }
  154. break;
  155. default:
  156. throw new Error('invalid number of gzip magic number bytes read');
  157. }
  158. // fallthrough
  159. case exports.INFLATE:
  160. case exports.GUNZIP:
  161. case exports.INFLATERAW:
  162. this.err = zlib_inflate.inflate(this.strm, this.flush
  163. // If data was encoded with dictionary
  164. );if (this.err === exports.Z_NEED_DICT && this.dictionary) {
  165. // Load it
  166. this.err = zlib_inflate.inflateSetDictionary(this.strm, this.dictionary);
  167. if (this.err === exports.Z_OK) {
  168. // And try to decode again
  169. this.err = zlib_inflate.inflate(this.strm, this.flush);
  170. } else if (this.err === exports.Z_DATA_ERROR) {
  171. // Both inflateSetDictionary() and inflate() return Z_DATA_ERROR.
  172. // Make it possible for After() to tell a bad dictionary from bad
  173. // input.
  174. this.err = exports.Z_NEED_DICT;
  175. }
  176. }
  177. while (this.strm.avail_in > 0 && this.mode === exports.GUNZIP && this.err === exports.Z_STREAM_END && this.strm.next_in[0] !== 0x00) {
  178. // Bytes remain in input buffer. Perhaps this is another compressed
  179. // member in the same archive, or just trailing garbage.
  180. // Trailing zero bytes are okay, though, since they are frequently
  181. // used for padding.
  182. this.reset();
  183. this.err = zlib_inflate.inflate(this.strm, this.flush);
  184. }
  185. break;
  186. default:
  187. throw new Error('Unknown mode ' + this.mode);
  188. }
  189. };
  190. Zlib.prototype._checkError = function () {
  191. // Acceptable error states depend on the type of zlib stream.
  192. switch (this.err) {
  193. case exports.Z_OK:
  194. case exports.Z_BUF_ERROR:
  195. if (this.strm.avail_out !== 0 && this.flush === exports.Z_FINISH) {
  196. this._error('unexpected end of file');
  197. return false;
  198. }
  199. break;
  200. case exports.Z_STREAM_END:
  201. // normal statuses, not fatal
  202. break;
  203. case exports.Z_NEED_DICT:
  204. if (this.dictionary == null) {
  205. this._error('Missing dictionary');
  206. } else {
  207. this._error('Bad dictionary');
  208. }
  209. return false;
  210. default:
  211. // something else.
  212. this._error('Zlib error');
  213. return false;
  214. }
  215. return true;
  216. };
  217. Zlib.prototype._after = function () {
  218. if (!this._checkError()) {
  219. return;
  220. }
  221. var avail_out = this.strm.avail_out;
  222. var avail_in = this.strm.avail_in;
  223. this.write_in_progress = false;
  224. // call the write() cb
  225. this.callback(avail_in, avail_out);
  226. if (this.pending_close) {
  227. this.close();
  228. }
  229. };
  230. Zlib.prototype._error = function (message) {
  231. if (this.strm.msg) {
  232. message = this.strm.msg;
  233. }
  234. this.onerror(message, this.err
  235. // no hope of rescue.
  236. );this.write_in_progress = false;
  237. if (this.pending_close) {
  238. this.close();
  239. }
  240. };
  241. Zlib.prototype.init = function (windowBits, level, memLevel, strategy, dictionary) {
  242. assert(arguments.length === 4 || arguments.length === 5, 'init(windowBits, level, memLevel, strategy, [dictionary])');
  243. assert(windowBits >= 8 && windowBits <= 15, 'invalid windowBits');
  244. assert(level >= -1 && level <= 9, 'invalid compression level');
  245. assert(memLevel >= 1 && memLevel <= 9, 'invalid memlevel');
  246. assert(strategy === exports.Z_FILTERED || strategy === exports.Z_HUFFMAN_ONLY || strategy === exports.Z_RLE || strategy === exports.Z_FIXED || strategy === exports.Z_DEFAULT_STRATEGY, 'invalid strategy');
  247. this._init(level, windowBits, memLevel, strategy, dictionary);
  248. this._setDictionary();
  249. };
  250. Zlib.prototype.params = function () {
  251. throw new Error('deflateParams Not supported');
  252. };
  253. Zlib.prototype.reset = function () {
  254. this._reset();
  255. this._setDictionary();
  256. };
  257. Zlib.prototype._init = function (level, windowBits, memLevel, strategy, dictionary) {
  258. this.level = level;
  259. this.windowBits = windowBits;
  260. this.memLevel = memLevel;
  261. this.strategy = strategy;
  262. this.flush = exports.Z_NO_FLUSH;
  263. this.err = exports.Z_OK;
  264. if (this.mode === exports.GZIP || this.mode === exports.GUNZIP) {
  265. this.windowBits += 16;
  266. }
  267. if (this.mode === exports.UNZIP) {
  268. this.windowBits += 32;
  269. }
  270. if (this.mode === exports.DEFLATERAW || this.mode === exports.INFLATERAW) {
  271. this.windowBits = -1 * this.windowBits;
  272. }
  273. this.strm = new Zstream();
  274. switch (this.mode) {
  275. case exports.DEFLATE:
  276. case exports.GZIP:
  277. case exports.DEFLATERAW:
  278. this.err = zlib_deflate.deflateInit2(this.strm, this.level, exports.Z_DEFLATED, this.windowBits, this.memLevel, this.strategy);
  279. break;
  280. case exports.INFLATE:
  281. case exports.GUNZIP:
  282. case exports.INFLATERAW:
  283. case exports.UNZIP:
  284. this.err = zlib_inflate.inflateInit2(this.strm, this.windowBits);
  285. break;
  286. default:
  287. throw new Error('Unknown mode ' + this.mode);
  288. }
  289. if (this.err !== exports.Z_OK) {
  290. this._error('Init error');
  291. }
  292. this.dictionary = dictionary;
  293. this.write_in_progress = false;
  294. this.init_done = true;
  295. };
  296. Zlib.prototype._setDictionary = function () {
  297. if (this.dictionary == null) {
  298. return;
  299. }
  300. this.err = exports.Z_OK;
  301. switch (this.mode) {
  302. case exports.DEFLATE:
  303. case exports.DEFLATERAW:
  304. this.err = zlib_deflate.deflateSetDictionary(this.strm, this.dictionary);
  305. break;
  306. default:
  307. break;
  308. }
  309. if (this.err !== exports.Z_OK) {
  310. this._error('Failed to set dictionary');
  311. }
  312. };
  313. Zlib.prototype._reset = function () {
  314. this.err = exports.Z_OK;
  315. switch (this.mode) {
  316. case exports.DEFLATE:
  317. case exports.DEFLATERAW:
  318. case exports.GZIP:
  319. this.err = zlib_deflate.deflateReset(this.strm);
  320. break;
  321. case exports.INFLATE:
  322. case exports.INFLATERAW:
  323. case exports.GUNZIP:
  324. this.err = zlib_inflate.inflateReset(this.strm);
  325. break;
  326. default:
  327. break;
  328. }
  329. if (this.err !== exports.Z_OK) {
  330. this._error('Failed to reset stream');
  331. }
  332. };
  333. exports.Zlib = Zlib;