sync-inflate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict';
  2. var assert = require('assert').ok;
  3. var zlib = require('zlib');
  4. var util = require('util');
  5. var kMaxLength = require('buffer').kMaxLength;
  6. function Inflate(opts) {
  7. if (!(this instanceof Inflate)) {
  8. return new Inflate(opts);
  9. }
  10. if (opts && opts.chunkSize < zlib.Z_MIN_CHUNK) {
  11. opts.chunkSize = zlib.Z_MIN_CHUNK;
  12. }
  13. zlib.Inflate.call(this, opts);
  14. // Node 8 --> 9 compatibility check
  15. this._offset = this._offset === undefined ? this._outOffset : this._offset;
  16. this._buffer = this._buffer || this._outBuffer;
  17. if (opts && opts.maxLength != null) {
  18. this._maxLength = opts.maxLength;
  19. }
  20. }
  21. function createInflate(opts) {
  22. return new Inflate(opts);
  23. }
  24. function _close(engine, callback) {
  25. if (callback) {
  26. process.nextTick(callback);
  27. }
  28. // Caller may invoke .close after a zlib error (which will null _handle).
  29. if (!engine._handle) {
  30. return;
  31. }
  32. engine._handle.close();
  33. engine._handle = null;
  34. }
  35. Inflate.prototype._processChunk = function(chunk, flushFlag, asyncCb) {
  36. if (typeof asyncCb === 'function') {
  37. return zlib.Inflate._processChunk.call(this, chunk, flushFlag, asyncCb);
  38. }
  39. var self = this;
  40. var availInBefore = chunk && chunk.length;
  41. var availOutBefore = this._chunkSize - this._offset;
  42. var leftToInflate = this._maxLength;
  43. var inOff = 0;
  44. var buffers = [];
  45. var nread = 0;
  46. var error;
  47. this.on('error', function(err) {
  48. error = err;
  49. });
  50. function handleChunk(availInAfter, availOutAfter) {
  51. if (self._hadError) {
  52. return;
  53. }
  54. var have = availOutBefore - availOutAfter;
  55. assert(have >= 0, 'have should not go down');
  56. if (have > 0) {
  57. var out = self._buffer.slice(self._offset, self._offset + have);
  58. self._offset += have;
  59. if (out.length > leftToInflate) {
  60. out = out.slice(0, leftToInflate);
  61. }
  62. buffers.push(out);
  63. nread += out.length;
  64. leftToInflate -= out.length;
  65. if (leftToInflate === 0) {
  66. return false;
  67. }
  68. }
  69. if (availOutAfter === 0 || self._offset >= self._chunkSize) {
  70. availOutBefore = self._chunkSize;
  71. self._offset = 0;
  72. self._buffer = Buffer.allocUnsafe(self._chunkSize);
  73. }
  74. if (availOutAfter === 0) {
  75. inOff += (availInBefore - availInAfter);
  76. availInBefore = availInAfter;
  77. return true;
  78. }
  79. return false;
  80. }
  81. assert(this._handle, 'zlib binding closed');
  82. do {
  83. var res = this._handle.writeSync(flushFlag,
  84. chunk, // in
  85. inOff, // in_off
  86. availInBefore, // in_len
  87. this._buffer, // out
  88. this._offset, //out_off
  89. availOutBefore); // out_len
  90. // Node 8 --> 9 compatibility check
  91. res = res || this._writeState;
  92. } while (!this._hadError && handleChunk(res[0], res[1]));
  93. if (this._hadError) {
  94. throw error;
  95. }
  96. if (nread >= kMaxLength) {
  97. _close(this);
  98. throw new RangeError('Cannot create final Buffer. It would be larger than 0x' + kMaxLength.toString(16) + ' bytes');
  99. }
  100. var buf = Buffer.concat(buffers, nread);
  101. _close(this);
  102. return buf;
  103. };
  104. util.inherits(Inflate, zlib.Inflate);
  105. function zlibBufferSync(engine, buffer) {
  106. if (typeof buffer === 'string') {
  107. buffer = Buffer.from(buffer);
  108. }
  109. if (!(buffer instanceof Buffer)) {
  110. throw new TypeError('Not a string or buffer');
  111. }
  112. var flushFlag = engine._finishFlushFlag;
  113. if (flushFlag == null) {
  114. flushFlag = zlib.Z_FINISH;
  115. }
  116. return engine._processChunk(buffer, flushFlag);
  117. }
  118. function inflateSync(buffer, opts) {
  119. return zlibBufferSync(new Inflate(opts), buffer);
  120. }
  121. module.exports = exports = inflateSync;
  122. exports.Inflate = Inflate;
  123. exports.createInflate = createInflate;
  124. exports.inflateSync = inflateSync;