zlib-streams 1.0.0
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/.clang-format +3 -0
- package/.gitmodules +3 -0
- package/.prettierignore +5 -0
- package/Makefile +441 -0
- package/README.md +35 -0
- package/dist/zlib-streams.js +157 -0
- package/dist/zlib-streams.wasm +0 -0
- package/package.json +31 -0
- package/src/inflate9.c +725 -0
- package/src/inflate9.h +16 -0
- package/src/wasm/api/zlib-streams.js +157 -0
- package/src/wasm/deflate_stream_wasm.c +103 -0
- package/src/wasm/inflate9_stream_wasm.c +89 -0
- package/src/wasm/inflate_stream_wasm.c +99 -0
package/src/inflate9.h
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#ifndef ZLIB_INFLATE9_H_COMPAT
|
|
2
|
+
#define ZLIB_INFLATE9_H_COMPAT
|
|
3
|
+
|
|
4
|
+
#include "zutil.h"
|
|
5
|
+
#include "inftree9.h"
|
|
6
|
+
#include "inflate.h"
|
|
7
|
+
|
|
8
|
+
int ZEXPORT inflate9Init(z_streamp strm);
|
|
9
|
+
int ZEXPORT inflate9(z_streamp strm, int flush);
|
|
10
|
+
int ZEXPORT inflate9End(z_streamp strm);
|
|
11
|
+
int ZEXPORT inflate9Init_(z_streamp strm, const char *version, int stream_size);
|
|
12
|
+
int ZEXPORT inflate9Init2_(z_streamp strm, int windowBits, const char *version,
|
|
13
|
+
int stream_size);
|
|
14
|
+
int ZEXPORT inflate9Reset2(z_streamp strm, int windowBits);
|
|
15
|
+
|
|
16
|
+
#endif /* ZLIB_INFLATE9_H_COMPAT */
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/* eslint-disable no-unused-vars */
|
|
2
|
+
/* global Buffer, process, TransformStream */
|
|
3
|
+
|
|
4
|
+
let wasm, malloc, free, memory;
|
|
5
|
+
|
|
6
|
+
export function setWasmExports(wasmAPI) {
|
|
7
|
+
wasm = wasmAPI;
|
|
8
|
+
({ malloc, free, memory } = wasm);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function _make(isCompress, type, options = {}) {
|
|
12
|
+
const level = (typeof options.level === "number") ? options.level : -1;
|
|
13
|
+
const outBufferSize = (typeof options.outBuffer === "number") ? options.outBuffer : 64 * 1024;
|
|
14
|
+
const inBufferSize = (typeof options.inBufferSize === "number") ? options.inBufferSize : 64 * 1024;
|
|
15
|
+
|
|
16
|
+
return new TransformStream({
|
|
17
|
+
start() {
|
|
18
|
+
let result;
|
|
19
|
+
this.out = malloc(outBufferSize);
|
|
20
|
+
this.in = malloc(inBufferSize);
|
|
21
|
+
this.inBufferSize = inBufferSize;
|
|
22
|
+
this._scratch = new Uint8Array(outBufferSize);
|
|
23
|
+
if (isCompress) {
|
|
24
|
+
this._process = wasm.deflate_process;
|
|
25
|
+
this._last_consumed = wasm.deflate_last_consumed;
|
|
26
|
+
this._end = wasm.deflate_end;
|
|
27
|
+
this.streamHandle = wasm.deflate_new();
|
|
28
|
+
if (type === "gzip") {
|
|
29
|
+
result = wasm.deflate_init_gzip(this.streamHandle, level);
|
|
30
|
+
} else if (type === "deflate-raw") {
|
|
31
|
+
result = wasm.deflate_init_raw(this.streamHandle, level);
|
|
32
|
+
} else {
|
|
33
|
+
result = wasm.deflate_init(this.streamHandle, level);
|
|
34
|
+
}
|
|
35
|
+
} else {
|
|
36
|
+
if (type === "deflate64-raw") {
|
|
37
|
+
this._process = wasm.inflate9_process;
|
|
38
|
+
this._last_consumed = wasm.inflate9_last_consumed;
|
|
39
|
+
this._end = wasm.inflate9_end;
|
|
40
|
+
this.streamHandle = wasm.inflate9_new();
|
|
41
|
+
result = wasm.inflate9_init_raw(this.streamHandle);
|
|
42
|
+
} else {
|
|
43
|
+
this._process = wasm.inflate_process;
|
|
44
|
+
this._last_consumed = wasm.inflate_last_consumed;
|
|
45
|
+
this._end = wasm.inflate_end;
|
|
46
|
+
this.streamHandle = wasm.inflate_new();
|
|
47
|
+
if (type === "deflate-raw") {
|
|
48
|
+
result = wasm.inflate_init_raw(this.streamHandle);
|
|
49
|
+
} else if (type === "gzip") {
|
|
50
|
+
result = wasm.inflate_init_gzip(this.streamHandle);
|
|
51
|
+
} else {
|
|
52
|
+
result = wasm.inflate_init(this.streamHandle);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (result !== 0) {
|
|
57
|
+
throw new Error("init failed:" + result);
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
transform(chunk, controller) {
|
|
61
|
+
try {
|
|
62
|
+
const buffer = chunk;
|
|
63
|
+
const heap = new Uint8Array(memory.buffer);
|
|
64
|
+
const process = this._process;
|
|
65
|
+
const last_consumed = this._last_consumed;
|
|
66
|
+
const out = this.out;
|
|
67
|
+
const scratch = this._scratch;
|
|
68
|
+
let offset = 0;
|
|
69
|
+
while (offset < buffer.length) {
|
|
70
|
+
const toRead = Math.min(buffer.length - offset, 32 * 1024);
|
|
71
|
+
if (!this.in || this.inBufferSize < toRead) {
|
|
72
|
+
if (this.in && free) {
|
|
73
|
+
free(this.in);
|
|
74
|
+
}
|
|
75
|
+
this.in = malloc(toRead);
|
|
76
|
+
this.inBufferSize = toRead;
|
|
77
|
+
}
|
|
78
|
+
heap.set(buffer.subarray(offset, offset + toRead), this.in);
|
|
79
|
+
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
|
+
const prod = result & 0x00ffffff;
|
|
84
|
+
if (prod) {
|
|
85
|
+
scratch.set(heap.subarray(out, out + prod), 0);
|
|
86
|
+
controller.enqueue(scratch.slice(0, prod));
|
|
87
|
+
}
|
|
88
|
+
const consumed = last_consumed(this.streamHandle);
|
|
89
|
+
if (consumed === 0) {
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
92
|
+
offset += consumed;
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (this._end && this.streamHandle) {
|
|
96
|
+
this._end(this.streamHandle);
|
|
97
|
+
}
|
|
98
|
+
if (this.in && free) {
|
|
99
|
+
free(this.in);
|
|
100
|
+
}
|
|
101
|
+
if (this.out && free) {
|
|
102
|
+
free(this.out);
|
|
103
|
+
}
|
|
104
|
+
controller.error(error);
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
flush(controller) {
|
|
108
|
+
try {
|
|
109
|
+
const heap = new Uint8Array(memory.buffer);
|
|
110
|
+
const process = this._process;
|
|
111
|
+
const out = this.out;
|
|
112
|
+
const scratch = this._scratch;
|
|
113
|
+
while (true) {
|
|
114
|
+
const result = process(this.streamHandle, 0, 0, out, outBufferSize, 4);
|
|
115
|
+
if (!isCompress && result < 0) {
|
|
116
|
+
throw new Error("process error:" + result);
|
|
117
|
+
}
|
|
118
|
+
const produced = result & 0x00ffffff;
|
|
119
|
+
const code = (result >> 24) & 0xff;
|
|
120
|
+
if (produced) {
|
|
121
|
+
scratch.set(heap.subarray(out, out + produced), 0);
|
|
122
|
+
controller.enqueue(scratch.slice(0, produced));
|
|
123
|
+
}
|
|
124
|
+
if (code === 1 || produced === 0) {
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
controller.error(error);
|
|
130
|
+
} finally {
|
|
131
|
+
if (this._end && this.streamHandle) {
|
|
132
|
+
const result = this._end(this.streamHandle);
|
|
133
|
+
if (result !== 0) {
|
|
134
|
+
controller.error(new Error("end error:" + result));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (this.in && free) {
|
|
138
|
+
free(this.in);
|
|
139
|
+
}
|
|
140
|
+
if (this.out && free) {
|
|
141
|
+
free(this.out);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export class CompressionStreamZlib {
|
|
149
|
+
constructor(type = "deflate", options) {
|
|
150
|
+
return _make(true, type, options);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export class DecompressionStreamZlib {
|
|
154
|
+
constructor(type = "deflate", options) {
|
|
155
|
+
return _make(false, type, options);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
#include <stdlib.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
#include "zlib.h"
|
|
5
|
+
|
|
6
|
+
#ifndef RAW_WBITS
|
|
7
|
+
#if defined(MAX_WBITS)
|
|
8
|
+
#define RAW_WBITS MAX_WBITS
|
|
9
|
+
#else
|
|
10
|
+
#define RAW_WBITS 15
|
|
11
|
+
#endif
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
struct wasm_deflate_ctx {
|
|
15
|
+
z_stream strm;
|
|
16
|
+
unsigned char *inbuf;
|
|
17
|
+
unsigned inbuf_sz;
|
|
18
|
+
unsigned last_consumed;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
unsigned deflate_new(void) {
|
|
22
|
+
struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)malloc(sizeof(*c));
|
|
23
|
+
if (!c)
|
|
24
|
+
return 0;
|
|
25
|
+
memset(c, 0, sizeof(*c));
|
|
26
|
+
c->strm.zalloc = Z_NULL;
|
|
27
|
+
c->strm.zfree = Z_NULL;
|
|
28
|
+
c->strm.opaque = Z_NULL;
|
|
29
|
+
c->inbuf = NULL;
|
|
30
|
+
c->inbuf_sz = 0;
|
|
31
|
+
return (unsigned)(uintptr_t)c;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
int deflate_init(unsigned zptr, int level) {
|
|
35
|
+
struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
|
|
36
|
+
if (!c)
|
|
37
|
+
return Z_STREAM_ERROR;
|
|
38
|
+
if (level < 0)
|
|
39
|
+
level = Z_DEFAULT_COMPRESSION;
|
|
40
|
+
return deflateInit(&c->strm, level);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
int deflate_init_raw(unsigned zptr, int level) {
|
|
44
|
+
struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
|
|
45
|
+
if (!c)
|
|
46
|
+
return Z_STREAM_ERROR;
|
|
47
|
+
if (level < 0)
|
|
48
|
+
level = Z_DEFAULT_COMPRESSION;
|
|
49
|
+
return deflateInit2(&c->strm, level, Z_DEFLATED, -RAW_WBITS, 8,
|
|
50
|
+
Z_DEFAULT_STRATEGY);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
int deflate_init_gzip(unsigned zptr, int level) {
|
|
54
|
+
struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
|
|
55
|
+
if (!c)
|
|
56
|
+
return Z_STREAM_ERROR;
|
|
57
|
+
if (level < 0)
|
|
58
|
+
level = Z_DEFAULT_COMPRESSION;
|
|
59
|
+
return deflateInit2(&c->strm, level, Z_DEFLATED, MAX_WBITS + 16, 8,
|
|
60
|
+
Z_DEFAULT_STRATEGY);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
int deflate_process(unsigned zptr, unsigned in_ptr, unsigned in_len,
|
|
64
|
+
unsigned out_ptr, unsigned out_len, int flush) {
|
|
65
|
+
struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
|
|
66
|
+
if (!c)
|
|
67
|
+
return Z_STREAM_ERROR;
|
|
68
|
+
if (in_len > c->inbuf_sz) {
|
|
69
|
+
unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
|
|
70
|
+
if (!nb)
|
|
71
|
+
return Z_MEM_ERROR;
|
|
72
|
+
c->inbuf = nb;
|
|
73
|
+
c->inbuf_sz = in_len;
|
|
74
|
+
}
|
|
75
|
+
memcpy(c->inbuf, (unsigned char *)(uintptr_t)in_ptr, in_len);
|
|
76
|
+
c->strm.next_in = c->inbuf;
|
|
77
|
+
c->strm.avail_in = in_len;
|
|
78
|
+
c->strm.next_out = (unsigned char *)(uintptr_t)out_ptr;
|
|
79
|
+
c->strm.avail_out = out_len;
|
|
80
|
+
|
|
81
|
+
int ret = deflate(&c->strm, flush);
|
|
82
|
+
int produced = (int)(out_len - c->strm.avail_out);
|
|
83
|
+
c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
|
|
84
|
+
int code = ret & 0xff;
|
|
85
|
+
return (produced & 0x00ffffff) | ((code & 0xff) << 24);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
int deflate_end(unsigned zptr) {
|
|
89
|
+
struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
|
|
90
|
+
if (!c)
|
|
91
|
+
return Z_STREAM_ERROR;
|
|
92
|
+
int r = deflateEnd(&c->strm);
|
|
93
|
+
free(c->inbuf);
|
|
94
|
+
free(c);
|
|
95
|
+
return r;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
unsigned deflate_last_consumed(unsigned zptr) {
|
|
99
|
+
struct wasm_deflate_ctx *c = (struct wasm_deflate_ctx *)(uintptr_t)zptr;
|
|
100
|
+
if (!c)
|
|
101
|
+
return 0;
|
|
102
|
+
return c->last_consumed;
|
|
103
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#include <stdlib.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
#include "zlib.h"
|
|
5
|
+
#include "inflate9.h"
|
|
6
|
+
#ifndef DEFLATE64_WBITS
|
|
7
|
+
#if defined(MAX_WBITS) && (MAX_WBITS >= 16)
|
|
8
|
+
#define DEFLATE64_WBITS MAX_WBITS
|
|
9
|
+
#else
|
|
10
|
+
#define DEFLATE64_WBITS 16
|
|
11
|
+
#endif
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
struct wasm_inflate9_ctx {
|
|
15
|
+
z_stream strm;
|
|
16
|
+
unsigned char *inbuf;
|
|
17
|
+
unsigned inbuf_sz;
|
|
18
|
+
unsigned last_consumed;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
unsigned inflate9_new(void) {
|
|
22
|
+
struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)malloc(sizeof(*c));
|
|
23
|
+
if (!c)
|
|
24
|
+
return 0;
|
|
25
|
+
memset(c, 0, sizeof(*c));
|
|
26
|
+
c->strm.zalloc = Z_NULL;
|
|
27
|
+
c->strm.zfree = Z_NULL;
|
|
28
|
+
c->strm.opaque = Z_NULL;
|
|
29
|
+
c->inbuf = NULL;
|
|
30
|
+
c->inbuf_sz = 0;
|
|
31
|
+
return (unsigned)(uintptr_t)c;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
int inflate9_init(unsigned zptr) {
|
|
35
|
+
struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
|
|
36
|
+
if (!c)
|
|
37
|
+
return Z_STREAM_ERROR;
|
|
38
|
+
return inflate9Init(&c->strm);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
int inflate9_init_raw(unsigned zptr) {
|
|
42
|
+
struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
|
|
43
|
+
if (!c)
|
|
44
|
+
return Z_STREAM_ERROR;
|
|
45
|
+
return inflate9Init2_(&c->strm, -DEFLATE64_WBITS, ZLIB_VERSION,
|
|
46
|
+
(int)sizeof(z_stream));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
int inflate9_process(unsigned zptr, unsigned in_ptr, unsigned in_len,
|
|
50
|
+
unsigned out_ptr, unsigned out_len, int flush) {
|
|
51
|
+
struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
|
|
52
|
+
if (!c)
|
|
53
|
+
return Z_STREAM_ERROR;
|
|
54
|
+
if (in_len > c->inbuf_sz) {
|
|
55
|
+
unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
|
|
56
|
+
if (!nb)
|
|
57
|
+
return Z_MEM_ERROR;
|
|
58
|
+
c->inbuf = nb;
|
|
59
|
+
c->inbuf_sz = in_len;
|
|
60
|
+
}
|
|
61
|
+
memcpy(c->inbuf, (unsigned char *)(uintptr_t)in_ptr, in_len);
|
|
62
|
+
c->strm.next_in = c->inbuf;
|
|
63
|
+
c->strm.avail_in = in_len;
|
|
64
|
+
c->strm.next_out = (unsigned char *)(uintptr_t)out_ptr;
|
|
65
|
+
c->strm.avail_out = out_len;
|
|
66
|
+
|
|
67
|
+
int ret = inflate9(&c->strm, flush);
|
|
68
|
+
int produced = (int)(out_len - c->strm.avail_out);
|
|
69
|
+
c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
|
|
70
|
+
int code = ret & 0xff;
|
|
71
|
+
return (produced & 0x00ffffff) | ((code & 0xff) << 24);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
int inflate9_end(unsigned zptr) {
|
|
75
|
+
struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
|
|
76
|
+
if (!c)
|
|
77
|
+
return Z_STREAM_ERROR;
|
|
78
|
+
int r = inflate9End(&c->strm);
|
|
79
|
+
free(c->inbuf);
|
|
80
|
+
free(c);
|
|
81
|
+
return r;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
unsigned inflate9_last_consumed(unsigned zptr) {
|
|
85
|
+
struct wasm_inflate9_ctx *c = (struct wasm_inflate9_ctx *)(uintptr_t)zptr;
|
|
86
|
+
if (!c)
|
|
87
|
+
return 0;
|
|
88
|
+
return c->last_consumed;
|
|
89
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
#include <stdlib.h>
|
|
2
|
+
#include <string.h>
|
|
3
|
+
#include <stdint.h>
|
|
4
|
+
#include "zlib.h"
|
|
5
|
+
|
|
6
|
+
#ifndef RAW_WBITS
|
|
7
|
+
#if defined(MAX_WBITS)
|
|
8
|
+
#define RAW_WBITS MAX_WBITS
|
|
9
|
+
#else
|
|
10
|
+
#define RAW_WBITS 15
|
|
11
|
+
#endif
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
struct wasm_inflate_ctx {
|
|
15
|
+
z_stream strm;
|
|
16
|
+
unsigned char *inbuf;
|
|
17
|
+
unsigned inbuf_sz;
|
|
18
|
+
unsigned last_consumed;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
unsigned inflate_new(void) {
|
|
22
|
+
struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)malloc(sizeof(*c));
|
|
23
|
+
if (!c)
|
|
24
|
+
return 0;
|
|
25
|
+
memset(c, 0, sizeof(*c));
|
|
26
|
+
c->strm.zalloc = Z_NULL;
|
|
27
|
+
c->strm.zfree = Z_NULL;
|
|
28
|
+
c->strm.opaque = Z_NULL;
|
|
29
|
+
c->inbuf = NULL;
|
|
30
|
+
c->inbuf_sz = 0;
|
|
31
|
+
return (unsigned)(uintptr_t)c;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
int inflate_init(unsigned zptr) {
|
|
35
|
+
struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
|
|
36
|
+
if (!c)
|
|
37
|
+
return Z_STREAM_ERROR;
|
|
38
|
+
return inflateInit(&c->strm);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
int inflate_init_raw(unsigned zptr) {
|
|
42
|
+
struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
|
|
43
|
+
if (!c)
|
|
44
|
+
return Z_STREAM_ERROR;
|
|
45
|
+
return inflateInit2(&c->strm, -RAW_WBITS);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
int inflate_init_gzip(unsigned zptr) {
|
|
49
|
+
struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
|
|
50
|
+
if (!c)
|
|
51
|
+
return Z_STREAM_ERROR;
|
|
52
|
+
#if defined(MAX_WBITS)
|
|
53
|
+
return inflateInit2(&c->strm, MAX_WBITS + 16);
|
|
54
|
+
#else
|
|
55
|
+
return inflateInit2(&c->strm, RAW_WBITS + 16);
|
|
56
|
+
#endif
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
int inflate_process(unsigned zptr, unsigned in_ptr, unsigned in_len,
|
|
60
|
+
unsigned out_ptr, unsigned out_len, int flush) {
|
|
61
|
+
struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
|
|
62
|
+
if (!c)
|
|
63
|
+
return Z_STREAM_ERROR;
|
|
64
|
+
if (in_len > c->inbuf_sz) {
|
|
65
|
+
unsigned char *nb = (unsigned char *)realloc(c->inbuf, in_len);
|
|
66
|
+
if (!nb)
|
|
67
|
+
return Z_MEM_ERROR;
|
|
68
|
+
c->inbuf = nb;
|
|
69
|
+
c->inbuf_sz = in_len;
|
|
70
|
+
}
|
|
71
|
+
memcpy(c->inbuf, (unsigned char *)(uintptr_t)in_ptr, in_len);
|
|
72
|
+
c->strm.next_in = c->inbuf;
|
|
73
|
+
c->strm.avail_in = in_len;
|
|
74
|
+
c->strm.next_out = (unsigned char *)(uintptr_t)out_ptr;
|
|
75
|
+
c->strm.avail_out = out_len;
|
|
76
|
+
|
|
77
|
+
int ret = inflate(&c->strm, flush);
|
|
78
|
+
int produced = (int)(out_len - c->strm.avail_out);
|
|
79
|
+
c->last_consumed = (unsigned)(in_len - c->strm.avail_in);
|
|
80
|
+
int code = ret & 0xff;
|
|
81
|
+
return (produced & 0x00ffffff) | ((code & 0xff) << 24);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
int inflate_end(unsigned zptr) {
|
|
85
|
+
struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
|
|
86
|
+
if (!c)
|
|
87
|
+
return Z_STREAM_ERROR;
|
|
88
|
+
int r = inflateEnd(&c->strm);
|
|
89
|
+
free(c->inbuf);
|
|
90
|
+
free(c);
|
|
91
|
+
return r;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
unsigned inflate_last_consumed(unsigned zptr) {
|
|
95
|
+
struct wasm_inflate_ctx *c = (struct wasm_inflate_ctx *)(uintptr_t)zptr;
|
|
96
|
+
if (!c)
|
|
97
|
+
return 0;
|
|
98
|
+
return c->last_consumed;
|
|
99
|
+
}
|