inflator.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2020 The noVNC Authors
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. import { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js";
  9. import ZStream from "../vendor/pako/lib/zlib/zstream.js";
  10. export default class Inflate {
  11. constructor() {
  12. this.strm = new ZStream();
  13. this.chunkSize = 1024 * 10 * 10;
  14. this.strm.output = new Uint8Array(this.chunkSize);
  15. inflateInit(this.strm);
  16. }
  17. setInput(data) {
  18. if (!data) {
  19. //FIXME: flush remaining data.
  20. /* eslint-disable camelcase */
  21. this.strm.input = null;
  22. this.strm.avail_in = 0;
  23. this.strm.next_in = 0;
  24. } else {
  25. this.strm.input = data;
  26. this.strm.avail_in = this.strm.input.length;
  27. this.strm.next_in = 0;
  28. /* eslint-enable camelcase */
  29. }
  30. }
  31. inflate(expected) {
  32. // resize our output buffer if it's too small
  33. // (we could just use multiple chunks, but that would cause an extra
  34. // allocation each time to flatten the chunks)
  35. if (expected > this.chunkSize) {
  36. this.chunkSize = expected;
  37. this.strm.output = new Uint8Array(this.chunkSize);
  38. }
  39. /* eslint-disable camelcase */
  40. this.strm.next_out = 0;
  41. this.strm.avail_out = expected;
  42. /* eslint-enable camelcase */
  43. let ret = inflate(this.strm, 0); // Flush argument not used.
  44. if (ret < 0) {
  45. throw new Error("zlib inflate failed");
  46. }
  47. if (this.strm.next_out != expected) {
  48. throw new Error("Incomplete zlib block");
  49. }
  50. return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
  51. }
  52. reset() {
  53. inflateReset(this.strm);
  54. }
  55. }