zlib-streams 1.1.0 → 1.1.1

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.
package/Makefile CHANGED
@@ -292,6 +292,8 @@ run_all_tests: dist/zlib-streams-dev.wasm
292
292
  # Output buffer filling before any input is consumed, including a deflate64
293
293
  # match longer than the buffer, which no encoder we have can produce
294
294
  @node src/wasm/tests/test_output_buffer_boundary.js dist/zlib-streams-dev.wasm
295
+ # Error codes packed the same way as a success, on both directions
296
+ @node src/wasm/tests/test_process_error_encoding.js dist/zlib-streams-dev.wasm
295
297
  @echo "Completed run_all_tests"
296
298
 
297
299
  dist/zlib-streams-dev.wasm: $(WASM_SRCS)
Binary file
@@ -138,18 +138,17 @@ function _make(isCompress, type, options = {}) {
138
138
  }
139
139
  heap.set(buffer.subarray(offset, offset + toRead), this.in);
140
140
  const result = process(this.streamHandle, this.in, toRead, out, outBufferSize, 0);
141
+ // checked before the byte count is used, so a status code can never be read as one
142
+ const code = (result >> 24) & 0xff;
143
+ const signedCode = (code & 0x80) ? code - 256 : code;
144
+ if (signedCode < 0) {
145
+ throw new Error("process error:" + signedCode);
146
+ }
141
147
  const prod = result & 0x00ffffff;
142
148
  if (prod) {
143
149
  scratch.set(heap.subarray(out, out + prod), 0);
144
150
  controller.enqueue(scratch.slice(0, prod));
145
151
  }
146
- if (!isCompress) {
147
- const code = (result >> 24) & 0xff;
148
- const signedCode = (code & 0x80) ? code - 256 : code;
149
- if (signedCode < 0) {
150
- throw new Error("process error:" + signedCode);
151
- }
152
- }
153
152
  const consumed = last_consumed(this.streamHandle);
154
153
  if (consumed === 0 && prod === 0) {
155
154
  break;
@@ -169,14 +168,12 @@ function _make(isCompress, type, options = {}) {
169
168
  const scratch = this._scratch;
170
169
  while (true) {
171
170
  const result = process(this.streamHandle, 0, 0, out, outBufferSize, 4);
172
- const produced = result & 0x00ffffff;
173
171
  const code = (result >> 24) & 0xff;
174
- if (!isCompress) {
175
- const signedCode = (code & 0x80) ? code - 256 : code;
176
- if (signedCode < 0) {
177
- throw new Error("process error:" + signedCode);
178
- }
172
+ const signedCode = (code & 0x80) ? code - 256 : code;
173
+ if (signedCode < 0) {
174
+ throw new Error("process error:" + signedCode);
179
175
  }
176
+ const produced = result & 0x00ffffff;
180
177
  if (produced) {
181
178
  scratch.set(heap.subarray(out, out + produced), 0);
182
179
  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.1.0",
6
+ "version": "1.1.1",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "deflate",
@@ -138,18 +138,17 @@ function _make(isCompress, type, options = {}) {
138
138
  }
139
139
  heap.set(buffer.subarray(offset, offset + toRead), this.in);
140
140
  const result = process(this.streamHandle, this.in, toRead, out, outBufferSize, 0);
141
+ // checked before the byte count is used, so a status code can never be read as one
142
+ const code = (result >> 24) & 0xff;
143
+ const signedCode = (code & 0x80) ? code - 256 : code;
144
+ if (signedCode < 0) {
145
+ throw new Error("process error:" + signedCode);
146
+ }
141
147
  const prod = result & 0x00ffffff;
142
148
  if (prod) {
143
149
  scratch.set(heap.subarray(out, out + prod), 0);
144
150
  controller.enqueue(scratch.slice(0, prod));
145
151
  }
146
- if (!isCompress) {
147
- const code = (result >> 24) & 0xff;
148
- const signedCode = (code & 0x80) ? code - 256 : code;
149
- if (signedCode < 0) {
150
- throw new Error("process error:" + signedCode);
151
- }
152
- }
153
152
  const consumed = last_consumed(this.streamHandle);
154
153
  if (consumed === 0 && prod === 0) {
155
154
  break;
@@ -169,14 +168,12 @@ function _make(isCompress, type, options = {}) {
169
168
  const scratch = this._scratch;
170
169
  while (true) {
171
170
  const result = process(this.streamHandle, 0, 0, out, outBufferSize, 4);
172
- const produced = result & 0x00ffffff;
173
171
  const code = (result >> 24) & 0xff;
174
- if (!isCompress) {
175
- const signedCode = (code & 0x80) ? code - 256 : code;
176
- if (signedCode < 0) {
177
- throw new Error("process error:" + signedCode);
178
- }
172
+ const signedCode = (code & 0x80) ? code - 256 : code;
173
+ if (signedCode < 0) {
174
+ throw new Error("process error:" + signedCode);
179
175
  }
176
+ const produced = result & 0x00ffffff;
180
177
  if (produced) {
181
178
  scratch.set(heap.subarray(out, out + produced), 0);
182
179
  controller.enqueue(scratch.slice(0, produced));
@@ -34,17 +34,22 @@ unsigned wasm_stream_last_consumed(unsigned zptr) {
34
34
  return c->last_consumed;
35
35
  }
36
36
 
37
+ /* the process functions pack the produced byte count and the zlib status code into one int, so
38
+ every return path has to use this shape: a bare negative code decodes as a huge byte count */
39
+ #define WASM_STREAM_RESULT(produced, code) \
40
+ (((produced) & 0x00ffffff) | (((code) & 0xff) << 24))
41
+
37
42
  int wasm_stream_process_common(unsigned zptr, unsigned in_ptr, unsigned in_len,
38
43
  unsigned out_ptr, unsigned out_len, int flush,
39
44
  int (*process_func)(z_streamp, int)) {
40
45
  struct wasm_stream_ctx *c = (struct wasm_stream_ctx *)(uintptr_t)zptr;
41
46
  if (!c)
42
- return Z_STREAM_ERROR;
47
+ return WASM_STREAM_RESULT(0, Z_STREAM_ERROR);
43
48
 
44
49
  if (in_len > c->inbuf_sz) {
45
50
  unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
46
51
  if (!nb)
47
- return Z_MEM_ERROR;
52
+ return WASM_STREAM_RESULT(0, Z_MEM_ERROR);
48
53
  c->inbuf = nb;
49
54
  c->inbuf_sz = in_len;
50
55
  }
@@ -58,6 +63,5 @@ int wasm_stream_process_common(unsigned zptr, unsigned in_ptr, unsigned in_len,
58
63
  int ret = process_func(&c->strm, flush);
59
64
  int produced = (int)(out_len - c->strm.avail_out);
60
65
  c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
61
- int code = ret & 0xff;
62
- return (produced & 0x00ffffff) | ((code & 0xff) << 24);
66
+ return WASM_STREAM_RESULT(produced, ret);
63
67
  }