zlib-streams 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/Makefile +3 -3
  2. package/build/7zip/Archive/Zip/ZipAddCommon.o +0 -0
  3. package/build/7zip/Archive/Zip/ZipHandler.o +0 -0
  4. package/build/7zip/Archive/Zip/ZipHandlerOut.o +0 -0
  5. package/build/7zip/Archive/Zip/ZipIn.o +0 -0
  6. package/build/7zip/Archive/Zip/ZipItem.o +0 -0
  7. package/build/7zip/Archive/Zip/ZipOut.o +0 -0
  8. package/build/7zip/Archive/Zip/ZipRegister.o +0 -0
  9. package/build/7zip/Archive/Zip/ZipUpdate.o +0 -0
  10. package/build/7zip/Common/CWrappers.o +0 -0
  11. package/build/7zip/Common/CreateCoder.o +0 -0
  12. package/build/7zip/Common/FileStreams.o +0 -0
  13. package/build/7zip/Common/FilterCoder.o +0 -0
  14. package/build/7zip/Common/InBuffer.o +0 -0
  15. package/build/7zip/Common/InOutTempBuffer.o +0 -0
  16. package/build/7zip/Common/OffsetStream.o +0 -0
  17. package/build/7zip/Common/OutBuffer.o +0 -0
  18. package/build/7zip/Common/OutMemStream.o +0 -0
  19. package/build/7zip/Common/StreamObjects.o +0 -0
  20. package/build/7zip/Common/StreamUtils.o +0 -0
  21. package/build/7zip/Compress/CopyCoder.o +0 -0
  22. package/build/7zip/Compress/Deflate64Register.o +0 -0
  23. package/build/7zip/Compress/DeflateDecoder.o +0 -0
  24. package/build/7zip/Compress/DeflateEncoder.o +0 -0
  25. package/build/7zip/Compress/DeflateRegister.o +0 -0
  26. package/build/7zip/Compress/LzOutWindow.o +0 -0
  27. package/build/Common/IntToString.o +0 -0
  28. package/build/Common/MyString.o +0 -0
  29. package/build/Common/MyWindows.o +0 -0
  30. package/build/Common/StringConvert.o +0 -0
  31. package/build/Common/UTFConvert.o +0 -0
  32. package/build/Windows/FileFind.o +0 -0
  33. package/build/Windows/FileIO.o +0 -0
  34. package/build/Windows/FileName.o +0 -0
  35. package/build/debug/src/inflate9.d +1 -4
  36. package/build/debug/src/inflate9.o +0 -0
  37. package/build/src/inflate9.d +1 -4
  38. package/build/src/inflate9.o +0 -0
  39. package/dist/zlib-streams.js +17 -6
  40. package/dist/zlib-streams.wasm +0 -0
  41. package/package.json +1 -1
  42. package/src/README.md +1 -0
  43. package/src/inflate9.c +13 -12
  44. package/src/inflate9.h +1 -8
  45. package/src/wasm/api/zlib-streams.js +17 -6
  46. package/src/wasm/deflate_stream_wasm.c +6 -41
  47. package/src/wasm/inflate9_stream_wasm.c +7 -51
  48. package/src/wasm/inflate_stream_wasm.c +7 -50
  49. package/src/wasm/wasm_stream_common.c +63 -0
  50. package/src/wasm/wasm_stream_common.h +26 -0
  51. package/tmp/{all_runs/payload_decompress_ref_debug__100k_lines.deflate64.log → trace_ref.log} +1 -0
  52. package/build/debug/src/inffast9.d +0 -9
  53. package/build/debug/src/inffast9.o +0 -0
  54. package/build/src/inffast9.d +0 -9
  55. package/build/src/inffast9.o +0 -0
  56. package/src/inffast9.c +0 -312
  57. package/src/inffast9.h +0 -11
  58. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.log +0 -34
  59. package/tmp/all_runs/payload_decompress_nowindow_debug__100k_lines.deflate64.out +0 -100000
  60. package/tmp/all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.log +0 -2
  61. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.log +0 -1
  62. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_63k.deflate64.out +0 -0
  63. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.log +0 -1
  64. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_64k.deflate64.out +0 -0
  65. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.log +0 -2
  66. package/tmp/all_runs/payload_decompress_nowindow_debug__payload_65k.deflate64.out +0 -0
  67. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.log +0 -1
  68. package/tmp/all_runs/payload_decompress_nowindow_debug__rand_block_gap.deflate64.out +0 -0
  69. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.log +0 -1
  70. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_63k.deflate64.out +0 -0
  71. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.log +0 -1
  72. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_64k.deflate64.out +0 -0
  73. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.log +0 -2
  74. package/tmp/all_runs/payload_decompress_nowindow_debug__repeat_65k.deflate64.out +0 -0
  75. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.log +0 -2
  76. package/tmp/all_runs/payload_decompress_nowindow_debug__zeros_100k.deflate64.out +0 -0
  77. package/tmp/all_runs/payload_decompress_ref_debug__100k_lines.deflate64.out +0 -100000
  78. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.log +0 -1
  79. package/tmp/all_runs/payload_decompress_ref_debug__10k_lines.deflate64.out +0 -10000
  80. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.log +0 -1
  81. package/tmp/all_runs/payload_decompress_ref_debug__payload_63k.deflate64.out +0 -0
  82. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.log +0 -1
  83. package/tmp/all_runs/payload_decompress_ref_debug__payload_64k.deflate64.out +0 -0
  84. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.log +0 -1
  85. package/tmp/all_runs/payload_decompress_ref_debug__payload_65k.deflate64.out +0 -0
  86. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.log +0 -1
  87. package/tmp/all_runs/payload_decompress_ref_debug__rand_block_gap.deflate64.out +0 -0
  88. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.log +0 -1
  89. package/tmp/all_runs/payload_decompress_ref_debug__repeat_63k.deflate64.out +0 -0
  90. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.log +0 -1
  91. package/tmp/all_runs/payload_decompress_ref_debug__repeat_64k.deflate64.out +0 -0
  92. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.log +0 -1
  93. package/tmp/all_runs/payload_decompress_ref_debug__repeat_65k.deflate64.out +0 -0
  94. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.log +0 -1
  95. package/tmp/all_runs/payload_decompress_ref_debug__zeros_100k.deflate64.out +0 -0
  96. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.log +0 -34
  97. package/tmp/all_runs/payload_decompress_test_debug__100k_lines.deflate64.out +0 -100000
  98. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.log +0 -2
  99. package/tmp/all_runs/payload_decompress_test_debug__10k_lines.deflate64.out +0 -10000
  100. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.log +0 -1
  101. package/tmp/all_runs/payload_decompress_test_debug__payload_63k.deflate64.out +0 -0
  102. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.log +0 -1
  103. package/tmp/all_runs/payload_decompress_test_debug__payload_64k.deflate64.out +0 -0
  104. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.log +0 -2
  105. package/tmp/all_runs/payload_decompress_test_debug__payload_65k.deflate64.out +0 -0
  106. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.log +0 -1
  107. package/tmp/all_runs/payload_decompress_test_debug__rand_block_gap.deflate64.out +0 -0
  108. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.log +0 -1
  109. package/tmp/all_runs/payload_decompress_test_debug__repeat_63k.deflate64.out +0 -0
  110. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.log +0 -1
  111. package/tmp/all_runs/payload_decompress_test_debug__repeat_64k.deflate64.out +0 -0
  112. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.log +0 -2
  113. package/tmp/all_runs/payload_decompress_test_debug__repeat_65k.deflate64.out +0 -0
  114. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.log +0 -2
  115. package/tmp/all_runs/payload_decompress_test_debug__zeros_100k.deflate64.out +0 -0
  116. /package/tmp/{all_runs/payload_decompress_nowindow_debug__10k_lines.deflate64.out → out_ref.bin} +0 -0
package/Makefile CHANGED
@@ -15,11 +15,11 @@ LIBS=-lz
15
15
 
16
16
  ## Build the payload decompressor test binary used for verification (deduplicated sources)
17
17
  PD_SRCS = test/payload_decompress.c \
18
- src/inflate9.c src/inffast9.c src/zlib/inffast.c src/zlib/inftrees.c src/zlib/zutil.c src/zlib/inflate.c src/zlib/infback.c \
18
+ src/inflate9.c src/zlib/inffast.c src/zlib/inftrees.c src/zlib/zutil.c src/zlib/inflate.c src/zlib/infback.c \
19
19
  src/zlib/contrib/infback9/infback9.c src/zlib/contrib/infback9/inftree9.c src/zlib/crc32.c src/zlib/adler32.c src/zlib/trees.c
20
20
 
21
21
  PD_NOWINDOW_SRCS = test/payload_decompress_nowindow.c \
22
- src/inflate9.c src/inffast9.c src/zlib/inffast.c src/zlib/inftrees.c src/zlib/zutil.c src/zlib/inflate.c src/zlib/infback.c \
22
+ src/inflate9.c src/zlib/inffast.c src/zlib/inftrees.c src/zlib/zutil.c src/zlib/inflate.c src/zlib/infback.c \
23
23
  src/zlib/contrib/infback9/infback9.c src/zlib/contrib/infback9/inftree9.c src/zlib/crc32.c src/zlib/adler32.c src/zlib/trees.c
24
24
 
25
25
  PD_NOWINDOW_DEBUG_OBJS = $(PD_NOWINDOW_SRCS:%.c=build/debug/%.o)
@@ -129,7 +129,7 @@ ci:
129
129
  # -----------------------------------------------------------------------------
130
130
  EMCC ?= emsdk/upstream/emscripten/emcc
131
131
 
132
- WASM_SRCS = src/wasm/inflate9_stream_wasm.c src/wasm/inflate_stream_wasm.c src/wasm/deflate_stream_wasm.c src/wasm/allocator.c src/inflate9.c src/inffast9.c \
132
+ WASM_SRCS = src/wasm/inflate9_stream_wasm.c src/wasm/inflate_stream_wasm.c src/wasm/deflate_stream_wasm.c src/wasm/wasm_stream_common.c src/wasm/allocator.c src/inflate9.c \
133
133
  src/zlib/contrib/infback9/inftree9.c \
134
134
  src/zlib/inffast.c src/zlib/inflate.c src/zlib/inftrees.c src/zlib/zutil.c \
135
135
  src/zlib/crc32.c src/zlib/adler32.c src/zlib/trees.c src/zlib/deflate.c
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,12 +1,9 @@
1
1
  build/debug/src/inflate9.o: src/inflate9.c src/zlib/zutil.h \
2
2
  src/zlib/zlib.h src/zlib/zconf.h src/zlib/contrib/infback9/inftree9.h \
3
- src/zlib/inflate.h src/inffast9.h src/zlib/contrib/infback9/infback9.h \
4
- src/zlib/inffixed.h
3
+ src/zlib/inflate.h src/zlib/inffixed.h
5
4
  src/zlib/zutil.h:
6
5
  src/zlib/zlib.h:
7
6
  src/zlib/zconf.h:
8
7
  src/zlib/contrib/infback9/inftree9.h:
9
8
  src/zlib/inflate.h:
10
- src/inffast9.h:
11
- src/zlib/contrib/infback9/infback9.h:
12
9
  src/zlib/inffixed.h:
Binary file
@@ -1,12 +1,9 @@
1
1
  build/src/inflate9.o: src/inflate9.c src/zlib/zutil.h src/zlib/zlib.h \
2
2
  src/zlib/zconf.h src/zlib/contrib/infback9/inftree9.h \
3
- src/zlib/inflate.h src/inffast9.h src/zlib/contrib/infback9/infback9.h \
4
- src/zlib/inffixed.h
3
+ src/zlib/inflate.h src/zlib/inffixed.h
5
4
  src/zlib/zutil.h:
6
5
  src/zlib/zlib.h:
7
6
  src/zlib/zconf.h:
8
7
  src/zlib/contrib/infback9/inftree9.h:
9
8
  src/zlib/inflate.h:
10
- src/inffast9.h:
11
- src/zlib/contrib/infback9/infback9.h:
12
9
  src/zlib/inffixed.h:
Binary file
@@ -6,6 +6,10 @@ let wasm, malloc, free, memory;
6
6
  export function setWasmExports(wasmAPI) {
7
7
  wasm = wasmAPI;
8
8
  ({ malloc, free, memory } = wasm);
9
+ if (typeof malloc !== "function" || typeof free !== "function" || !memory) {
10
+ wasm = malloc = free = memory = null;
11
+ throw new Error("Invalid WASM module");
12
+ }
9
13
  }
10
14
 
11
15
  function _make(isCompress, type, options = {}) {
@@ -77,14 +81,18 @@ function _make(isCompress, type, options = {}) {
77
81
  }
78
82
  heap.set(buffer.subarray(offset, offset + toRead), this.in);
79
83
  const result = process(this.streamHandle, this.in, toRead, out, outBufferSize, 0);
80
- if (!isCompress && result < 0) {
81
- throw new Error("process error:" + result);
82
- }
83
84
  const prod = result & 0x00ffffff;
84
85
  if (prod) {
85
86
  scratch.set(heap.subarray(out, out + prod), 0);
86
87
  controller.enqueue(scratch.slice(0, prod));
87
88
  }
89
+ if (!isCompress) {
90
+ const code = (result >> 24) & 0xff;
91
+ const signedCode = (code & 0x80) ? code - 256 : code;
92
+ if (signedCode < 0) {
93
+ throw new Error("process error:" + signedCode);
94
+ }
95
+ }
88
96
  const consumed = last_consumed(this.streamHandle);
89
97
  if (consumed === 0) {
90
98
  break;
@@ -112,11 +120,14 @@ function _make(isCompress, type, options = {}) {
112
120
  const scratch = this._scratch;
113
121
  while (true) {
114
122
  const result = process(this.streamHandle, 0, 0, out, outBufferSize, 4);
115
- if (!isCompress && result < 0) {
116
- throw new Error("process error:" + result);
117
- }
118
123
  const produced = result & 0x00ffffff;
119
124
  const code = (result >> 24) & 0xff;
125
+ if (!isCompress) {
126
+ const signedCode = (code & 0x80) ? code - 256 : code;
127
+ if (signedCode < 0) {
128
+ throw new Error("process error:" + signedCode);
129
+ }
130
+ }
120
131
  if (produced) {
121
132
  scratch.set(heap.subarray(out, out + produced), 0);
122
133
  controller.enqueue(scratch.slice(0, produced));
Binary file
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "WASM-based Compression Streams API implementation using zlib, with support for deflate64 decompression.",
4
4
  "author": "Gildas Lormeau",
5
5
  "license": "BSD-3-Clause",
6
- "version": "1.0.2",
6
+ "version": "1.0.4",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "deflate",
package/src/README.md ADDED
@@ -0,0 +1 @@
1
+ See https://github.com/gildas-lormeau/inflate9.c
package/src/inflate9.c CHANGED
@@ -6,8 +6,6 @@
6
6
  #include "zutil.h"
7
7
  #include "inftree9.h"
8
8
  #include "inflate.h"
9
- #include "inffast9.h"
10
- #include "infback9.h"
11
9
 
12
10
  #ifdef MAKEFIXED
13
11
  #ifndef BUILDFIXED
@@ -650,14 +648,6 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
650
648
  state->mode = LEN;
651
649
  /* fallthrough */
652
650
  case LEN:
653
- if (have >= 6 && left >= 65536) {
654
- RESTORE();
655
- inflate_fast9(strm, out);
656
- LOAD();
657
- if (state->mode == TYPE)
658
- state->back = -1;
659
- break;
660
- }
661
651
  state->back = 0;
662
652
  for (;;) {
663
653
  here = state->lencode[BITS(state->lenbits)];
@@ -774,7 +764,7 @@ int ZEXPORT inflate9(z_streamp strm, int flush) {
774
764
  break;
775
765
  }
776
766
  #ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
777
- Trace((stderr, "inflate.c too far\n"));
767
+ Trace((stderr, "inflate9.c too far\n"));
778
768
  copy -= state->whave;
779
769
  if (copy > state->length)
780
770
  copy = state->length;
@@ -855,4 +845,15 @@ inf_leave:
855
845
  return ret;
856
846
  }
857
847
 
858
- int ZEXPORT inflate9End(z_streamp strm) { return inflateEnd(strm); }
848
+ int ZEXPORT inflate9End(z_streamp strm) {
849
+ struct inflate_state FAR *state;
850
+ if (inflateStateCheck(strm))
851
+ return Z_STREAM_ERROR;
852
+ state = (struct inflate_state FAR *)strm->state;
853
+ if (state->window != Z_NULL)
854
+ ZFREE(strm, state->window);
855
+ ZFREE(strm, strm->state);
856
+ strm->state = Z_NULL;
857
+ Tracev((stderr, "inflate: end\n"));
858
+ return Z_OK;
859
+ }
package/src/inflate9.h CHANGED
@@ -1,9 +1,4 @@
1
- #ifndef ZLIB_INFLATE9_H_COMPAT
2
- #define ZLIB_INFLATE9_H_COMPAT
3
-
4
1
  #include "zutil.h"
5
- #include "inftree9.h"
6
- #include "inflate.h"
7
2
 
8
3
  int ZEXPORT inflate9Init(z_streamp strm);
9
4
  int ZEXPORT inflate9(z_streamp strm, int flush);
@@ -11,6 +6,4 @@ int ZEXPORT inflate9End(z_streamp strm);
11
6
  int ZEXPORT inflate9Init_(z_streamp strm, const char *version, int stream_size);
12
7
  int ZEXPORT inflate9Init2_(z_streamp strm, int windowBits, const char *version,
13
8
  int stream_size);
14
- int ZEXPORT inflate9Reset2(z_streamp strm, int windowBits);
15
-
16
- #endif /* ZLIB_INFLATE9_H_COMPAT */
9
+ int ZEXPORT inflate9Reset2(z_streamp strm, int windowBits);
@@ -6,6 +6,10 @@ let wasm, malloc, free, memory;
6
6
  export function setWasmExports(wasmAPI) {
7
7
  wasm = wasmAPI;
8
8
  ({ malloc, free, memory } = wasm);
9
+ if (typeof malloc !== "function" || typeof free !== "function" || !memory) {
10
+ wasm = malloc = free = memory = null;
11
+ throw new Error("Invalid WASM module");
12
+ }
9
13
  }
10
14
 
11
15
  function _make(isCompress, type, options = {}) {
@@ -77,14 +81,18 @@ function _make(isCompress, type, options = {}) {
77
81
  }
78
82
  heap.set(buffer.subarray(offset, offset + toRead), this.in);
79
83
  const result = process(this.streamHandle, this.in, toRead, out, outBufferSize, 0);
80
- if (!isCompress && result < 0) {
81
- throw new Error("process error:" + result);
82
- }
83
84
  const prod = result & 0x00ffffff;
84
85
  if (prod) {
85
86
  scratch.set(heap.subarray(out, out + prod), 0);
86
87
  controller.enqueue(scratch.slice(0, prod));
87
88
  }
89
+ if (!isCompress) {
90
+ const code = (result >> 24) & 0xff;
91
+ const signedCode = (code & 0x80) ? code - 256 : code;
92
+ if (signedCode < 0) {
93
+ throw new Error("process error:" + signedCode);
94
+ }
95
+ }
88
96
  const consumed = last_consumed(this.streamHandle);
89
97
  if (consumed === 0) {
90
98
  break;
@@ -112,11 +120,14 @@ function _make(isCompress, type, options = {}) {
112
120
  const scratch = this._scratch;
113
121
  while (true) {
114
122
  const result = process(this.streamHandle, 0, 0, out, outBufferSize, 4);
115
- if (!isCompress && result < 0) {
116
- throw new Error("process error:" + result);
117
- }
118
123
  const produced = result & 0x00ffffff;
119
124
  const code = (result >> 24) & 0xff;
125
+ if (!isCompress) {
126
+ const signedCode = (code & 0x80) ? code - 256 : code;
127
+ if (signedCode < 0) {
128
+ throw new Error("process error:" + signedCode);
129
+ }
130
+ }
120
131
  if (produced) {
121
132
  scratch.set(heap.subarray(out, out + produced), 0);
122
133
  controller.enqueue(scratch.slice(0, produced));
@@ -3,6 +3,7 @@
3
3
  #include <stdint.h>
4
4
  #include "zlib.h"
5
5
  #include "allocator.h"
6
+ #include "wasm_stream_common.h"
6
7
 
7
8
  #ifndef RAW_WBITS
8
9
  #if defined(MAX_WBITS)
@@ -13,24 +14,10 @@
13
14
  #endif
14
15
 
15
16
  struct wasm_deflate_ctx {
16
- z_stream strm;
17
- unsigned char *inbuf;
18
- unsigned inbuf_sz;
19
- unsigned last_consumed;
17
+ WASM_STREAM_COMMON_FIELDS
20
18
  };
21
19
 
22
- unsigned deflate_new(void) {
23
- struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)malloc(sizeof(*c));
24
- if (!c)
25
- return 0;
26
- memset(c, 0, sizeof(*c));
27
- c->strm.zalloc = my_zalloc;
28
- c->strm.zfree = my_zfree;
29
- c->strm.opaque = Z_NULL;
30
- c->inbuf = NULL;
31
- c->inbuf_sz = 0;
32
- return (unsigned)(uintptr_t)c;
33
- }
20
+ unsigned deflate_new(void) { return wasm_stream_new(); }
34
21
 
35
22
  int deflate_init(unsigned zptr, int level) {
36
23
  struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
@@ -63,27 +50,8 @@ int deflate_init_gzip(unsigned zptr, int level) {
63
50
 
64
51
  int deflate_process(unsigned zptr, unsigned in_ptr, unsigned in_len,
65
52
  unsigned out_ptr, unsigned out_len, int flush) {
66
- struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
67
- if (!c)
68
- return Z_STREAM_ERROR;
69
- if (in_len > c->inbuf_sz) {
70
- unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
71
- if (!nb)
72
- return Z_MEM_ERROR;
73
- c->inbuf = nb;
74
- c->inbuf_sz = in_len;
75
- }
76
- memcpy(c->inbuf, (unsigned char *)(uintptr_t)in_ptr, in_len);
77
- c->strm.next_in = c->inbuf;
78
- c->strm.avail_in = in_len;
79
- c->strm.next_out = (unsigned char *)(uintptr_t)out_ptr;
80
- c->strm.avail_out = out_len;
81
-
82
- int ret = deflate(&c->strm, flush);
83
- int produced = (int)(out_len - c->strm.avail_out);
84
- c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
85
- int code = ret & 0xff;
86
- return (produced & 0x00ffffff) | ((code & 0xff) << 24);
53
+ return wasm_stream_process_common(zptr, in_ptr, in_len, out_ptr, out_len,
54
+ flush, deflate);
87
55
  }
88
56
 
89
57
  int deflate_end(unsigned zptr) {
@@ -97,8 +65,5 @@ int deflate_end(unsigned zptr) {
97
65
  }
98
66
 
99
67
  unsigned deflate_last_consumed(unsigned zptr) {
100
- struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
101
- if (!c)
102
- return 0;
103
- return c->last_consumed;
68
+ return wasm_stream_last_consumed(zptr);
104
69
  }
@@ -3,7 +3,7 @@
3
3
  #include <stdint.h>
4
4
  #include "zlib.h"
5
5
  #include "inflate9.h"
6
- #include "allocator.h"
6
+ #include "wasm_stream_common.h"
7
7
  #ifndef DEFLATE64_WBITS
8
8
  #if defined(MAX_WBITS) && (MAX_WBITS >= 16)
9
9
  #define DEFLATE64_WBITS MAX_WBITS
@@ -13,24 +13,10 @@
13
13
  #endif
14
14
 
15
15
  struct wasm_inflate9_ctx {
16
- z_stream strm;
17
- unsigned char *inbuf;
18
- unsigned inbuf_sz;
19
- unsigned last_consumed;
16
+ WASM_STREAM_COMMON_FIELDS;
20
17
  };
21
18
 
22
- unsigned inflate9_new(void) {
23
- struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)malloc(sizeof(*c));
24
- if (!c)
25
- return 0;
26
- memset(c, 0, sizeof(*c));
27
- c->strm.zalloc = my_zalloc;
28
- c->strm.zfree = my_zfree;
29
- c->strm.opaque = Z_NULL;
30
- c->inbuf = NULL;
31
- c->inbuf_sz = 0;
32
- return (unsigned)(uintptr_t)c;
33
- }
19
+ unsigned inflate9_new(void) { return wasm_stream_new(); }
34
20
 
35
21
  int inflate9_init(unsigned zptr) {
36
22
  struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
@@ -49,42 +35,12 @@ int inflate9_init_raw(unsigned zptr) {
49
35
 
50
36
  int inflate9_process(unsigned zptr, unsigned in_ptr, unsigned in_len,
51
37
  unsigned out_ptr, unsigned out_len, int flush) {
52
- struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
53
- if (!c)
54
- return Z_STREAM_ERROR;
55
- if (in_len > c->inbuf_sz) {
56
- unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
57
- if (!nb)
58
- return Z_MEM_ERROR;
59
- c->inbuf = nb;
60
- c->inbuf_sz = in_len;
61
- }
62
- memcpy(c->inbuf, (unsigned char *)(uintptr_t)in_ptr, in_len);
63
- c->strm.next_in = c->inbuf;
64
- c->strm.avail_in = in_len;
65
- c->strm.next_out = (unsigned char *)(uintptr_t)out_ptr;
66
- c->strm.avail_out = out_len;
67
-
68
- int ret = inflate9(&c->strm, flush);
69
- int produced = (int)(out_len - c->strm.avail_out);
70
- c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
71
- int code = ret & 0xff;
72
- return (produced & 0x00ffffff) | ((code & 0xff) << 24);
38
+ return wasm_stream_process_common(zptr, in_ptr, in_len, out_ptr, out_len,
39
+ flush, inflate9);
73
40
  }
74
41
 
75
- int inflate9_end(unsigned zptr) {
76
- struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
77
- if (!c)
78
- return Z_STREAM_ERROR;
79
- int r = inflate9End(&c->strm);
80
- free(c->inbuf);
81
- free(c);
82
- return r;
83
- }
42
+ int inflate9_end(unsigned zptr) { return wasm_stream_end(zptr, inflate9End); }
84
43
 
85
44
  unsigned inflate9_last_consumed(unsigned zptr) {
86
- struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
87
- if (!c)
88
- return 0;
89
- return c->last_consumed;
45
+ return wasm_stream_last_consumed(zptr);
90
46
  }
@@ -3,6 +3,7 @@
3
3
  #include <stdint.h>
4
4
  #include "zlib.h"
5
5
  #include "allocator.h"
6
+ #include "wasm_stream_common.h"
6
7
 
7
8
  #ifndef RAW_WBITS
8
9
  #if defined(MAX_WBITS)
@@ -13,24 +14,10 @@
13
14
  #endif
14
15
 
15
16
  struct wasm_inflate_ctx {
16
- z_stream strm;
17
- unsigned char *inbuf;
18
- unsigned inbuf_sz;
19
- unsigned last_consumed;
17
+ WASM_STREAM_COMMON_FIELDS
20
18
  };
21
19
 
22
- unsigned inflate_new(void) {
23
- struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)malloc(sizeof(*c));
24
- if (!c)
25
- return 0;
26
- memset(c, 0, sizeof(*c));
27
- c->strm.zalloc = my_zalloc;
28
- c->strm.zfree = my_zfree;
29
- c->strm.opaque = NULL;
30
- c->inbuf = NULL;
31
- c->inbuf_sz = 0;
32
- return (unsigned)(uintptr_t)c;
33
- }
20
+ unsigned inflate_new(void) { return wasm_stream_new(); }
34
21
 
35
22
  int inflate_init(unsigned zptr) {
36
23
  struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
@@ -59,42 +46,12 @@ int inflate_init_gzip(unsigned zptr) {
59
46
 
60
47
  int inflate_process(unsigned zptr, unsigned in_ptr, unsigned in_len,
61
48
  unsigned out_ptr, unsigned out_len, int flush) {
62
- struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
63
- if (!c)
64
- return Z_STREAM_ERROR;
65
- if (in_len > c->inbuf_sz) {
66
- unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
67
- if (!nb)
68
- return Z_MEM_ERROR;
69
- c->inbuf = nb;
70
- c->inbuf_sz = in_len;
71
- }
72
- memcpy(c->inbuf, (unsigned char *)(uintptr_t)in_ptr, in_len);
73
- c->strm.next_in = c->inbuf;
74
- c->strm.avail_in = in_len;
75
- c->strm.next_out = (unsigned char *)(uintptr_t)out_ptr;
76
- c->strm.avail_out = out_len;
77
-
78
- int ret = inflate(&c->strm, flush);
79
- int produced = (int)(out_len - c->strm.avail_out);
80
- c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
81
- int code = ret & 0xff;
82
- return (produced & 0x00ffffff) | ((code & 0xff) << 24);
49
+ return wasm_stream_process_common(zptr, in_ptr, in_len, out_ptr, out_len,
50
+ flush, inflate);
83
51
  }
84
52
 
85
- int inflate_end(unsigned zptr) {
86
- struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
87
- if (!c)
88
- return Z_STREAM_ERROR;
89
- int r = inflateEnd(&c->strm);
90
- free(c->inbuf);
91
- free(c);
92
- return r;
93
- }
53
+ int inflate_end(unsigned zptr) { return wasm_stream_end(zptr, inflateEnd); }
94
54
 
95
55
  unsigned inflate_last_consumed(unsigned zptr) {
96
- struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
97
- if (!c)
98
- return 0;
99
- return c->last_consumed;
56
+ return wasm_stream_last_consumed(zptr);
100
57
  }
@@ -0,0 +1,63 @@
1
+ #include <stdlib.h>
2
+ #include <string.h>
3
+ #include <stdint.h>
4
+ #include "wasm_stream_common.h"
5
+ #include "allocator.h"
6
+
7
+ unsigned wasm_stream_new(void) {
8
+ struct wasm_stream_ctx *c = (struct wasm_stream_ctx *)malloc(sizeof(*c));
9
+ if (!c)
10
+ return 0;
11
+ memset(c, 0, sizeof(*c));
12
+ c->strm.zalloc = my_zalloc;
13
+ c->strm.zfree = my_zfree;
14
+ c->strm.opaque = Z_NULL;
15
+ c->inbuf = NULL;
16
+ c->inbuf_sz = 0;
17
+ return (unsigned)(uintptr_t)c;
18
+ }
19
+
20
+ int wasm_stream_end(unsigned zptr, int (*end_func)(z_stream *)) {
21
+ struct wasm_stream_ctx *c = (struct wasm_stream_ctx *)(uintptr_t)zptr;
22
+ if (!c)
23
+ return Z_STREAM_ERROR;
24
+ int r = end_func(&c->strm);
25
+ free(c->inbuf);
26
+ free(c);
27
+ return r;
28
+ }
29
+
30
+ unsigned wasm_stream_last_consumed(unsigned zptr) {
31
+ struct wasm_stream_ctx *c = (struct wasm_stream_ctx *)(uintptr_t)zptr;
32
+ if (!c)
33
+ return 0;
34
+ return c->last_consumed;
35
+ }
36
+
37
+ int wasm_stream_process_common(unsigned zptr, unsigned in_ptr, unsigned in_len,
38
+ unsigned out_ptr, unsigned out_len, int flush,
39
+ int (*process_func)(z_streamp, int)) {
40
+ struct wasm_stream_ctx *c = (struct wasm_stream_ctx *)(uintptr_t)zptr;
41
+ if (!c)
42
+ return Z_STREAM_ERROR;
43
+
44
+ if (in_len > c->inbuf_sz) {
45
+ unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
46
+ if (!nb)
47
+ return Z_MEM_ERROR;
48
+ c->inbuf = nb;
49
+ c->inbuf_sz = in_len;
50
+ }
51
+
52
+ memcpy(c->inbuf, (unsigned char *)(uintptr_t)in_ptr, in_len);
53
+ c->strm.next_in = c->inbuf;
54
+ c->strm.avail_in = in_len;
55
+ c->strm.next_out = (unsigned char *)(uintptr_t)out_ptr;
56
+ c->strm.avail_out = out_len;
57
+
58
+ int ret = process_func(&c->strm, flush);
59
+ int produced = (int)(out_len - c->strm.avail_out);
60
+ c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
61
+ int code = ret & 0xff;
62
+ return (produced & 0x00ffffff) | ((code & 0xff) << 24);
63
+ }
@@ -0,0 +1,26 @@
1
+ #ifndef WASM_STREAM_COMMON_H
2
+ #define WASM_STREAM_COMMON_H
3
+
4
+ #include "zlib.h"
5
+ #include "allocator.h"
6
+
7
+ // Common fields for all WASM stream contexts
8
+ #define WASM_STREAM_COMMON_FIELDS \
9
+ z_stream strm; \
10
+ unsigned char *inbuf; \
11
+ unsigned inbuf_sz; \
12
+ unsigned last_consumed;
13
+
14
+ struct wasm_stream_ctx {
15
+ WASM_STREAM_COMMON_FIELDS;
16
+ };
17
+
18
+ // Common function declarations
19
+ unsigned wasm_stream_new(void);
20
+ int wasm_stream_end(unsigned zptr, int (*end_func)(z_stream *));
21
+ unsigned wasm_stream_last_consumed(unsigned zptr);
22
+ int wasm_stream_process_common(unsigned zptr, unsigned in_ptr, unsigned in_len,
23
+ unsigned out_ptr, unsigned out_len, int flush,
24
+ int (*process_func)(z_stream *, int));
25
+
26
+ #endif // WASM_STREAM_COMMON_H