Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

binding.js 11KB

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