ZlibInflate.mjs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. import {INFLATE_BUFFER_TYPE} from "../lib/Zlib.mjs";
  8. import zlibAndGzip from "zlibjs/bin/zlib_and_gzip.min.js";
  9. const Zlib = zlibAndGzip.Zlib;
  10. const ZLIB_BUFFER_TYPE_LOOKUP = {
  11. "Adaptive": Zlib.Inflate.BufferType.ADAPTIVE,
  12. "Block": Zlib.Inflate.BufferType.BLOCK,
  13. };
  14. /**
  15. * Zlib Inflate operation
  16. */
  17. class ZlibInflate extends Operation {
  18. /**
  19. * ZlibInflate constructor
  20. */
  21. constructor() {
  22. super();
  23. this.name = "Zlib Inflate";
  24. this.module = "Compression";
  25. this.description = "Decompresses data which has been compressed using the deflate algorithm with zlib headers.";
  26. this.infoURL = "https://wikipedia.org/wiki/Zlib";
  27. this.inputType = "ArrayBuffer";
  28. this.outputType = "ArrayBuffer";
  29. this.args = [
  30. {
  31. name: "Start index",
  32. type: "number",
  33. value: 0
  34. },
  35. {
  36. name: "Initial output buffer size",
  37. type: "number",
  38. value: 0
  39. },
  40. {
  41. name: "Buffer expansion type",
  42. type: "option",
  43. value: INFLATE_BUFFER_TYPE
  44. },
  45. {
  46. name: "Resize buffer after decompression",
  47. type: "boolean",
  48. value: false
  49. },
  50. {
  51. name: "Verify result",
  52. type: "boolean",
  53. value: false
  54. }
  55. ];
  56. this.checks = {
  57. input: {
  58. regex: [
  59. {
  60. match: "^\\x78(\\x01|\\x9c|\\xda|\\x5e)",
  61. flags: "",
  62. args: [0, 0, "Adaptive", false, false]
  63. },
  64. ]
  65. }
  66. };
  67. }
  68. /**
  69. * @param {ArrayBuffer} input
  70. * @param {Object[]} args
  71. * @returns {ArrayBuffer}
  72. */
  73. run(input, args) {
  74. const inflate = new Zlib.Inflate(new Uint8Array(input), {
  75. index: args[0],
  76. bufferSize: args[1],
  77. bufferType: ZLIB_BUFFER_TYPE_LOOKUP[args[2]],
  78. resize: args[3],
  79. verify: args[4]
  80. });
  81. return new Uint8Array(inflate.decompress()).buffer;
  82. }
  83. }
  84. export default ZlibInflate;