1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi/native_api.h"
17 #include "native_common.h"
18 #include "zconf.h"
19 #include "zlib.h"
20 #include <cstdio>
21 #include <cstdlib>
22 #include <cstring>
23
24 #define NO_ERR 0
25 #define SUCCESS 1
26 #define FAIL -1
27
28 struct mem_zone {
29 struct mem_item *first;
30 size_t total, highwater;
31 size_t limit;
32 int notlifo, rogue;
33 };
34
35 static const char DICTIONARY[] = "hello";
36 static unsigned int CALLOC_SIZE = 1;
37 static thread_local char HELLO[] = "hello, hello!";
38 static const char GARBAGE[] = "garbage";
39 static int GARBAGE_LEN = strlen(GARBAGE) + 1;
40 static const char TESTFILE[] = "/data/storage/el2/base/files/test.txt";
41 static int VALUE_ZERO = 0;
42 static int VALUE_ONE = 1;
43 static int VALUE_TWO = 2;
44 static int VALUE_THREE = 3;
45 static int VALUE_FOUR = 4;
46 static int VALUE_FIVE = 5;
47 static int VALUE_SIX = 6;
48 static int VALUE_EIGHT = 8;
49 static int VALUE_FOURTEEN = 14;
50 static int VALUE_FIFTEEN = 15;
51 static int VALUE_TWENTYSEVEN = 27;
52 static int VALUE_THIRTYONE = 31;
53 static int STREAM_STATE = -65536;
54 static unsigned BUFFER_SIZE = 8192;
55
Crc32(napi_env env, napi_callback_info info)56 static napi_value Crc32(napi_env env, napi_callback_info info)
57 {
58 size_t argc = 1;
59 napi_value args[1];
60 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
61 uint32_t bufferType;
62 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &bufferType));
63
64 uLong err = Z_ERRNO;
65 if (bufferType == Z_NULL) {
66 err = crc32(0L, Z_NULL, 0);
67 } else {
68 const Bytef *buf = reinterpret_cast<const Bytef *>(DICTIONARY);
69 err = crc32(0L, buf, 0);
70 }
71
72 NAPI_ASSERT(env, err == Z_OK, "crc32 error");
73
74 napi_value result = nullptr;
75 napi_create_int32(env, SUCCESS, &result);
76 return result;
77 }
78
Crc32_z(napi_env env, napi_callback_info info)79 static napi_value Crc32_z(napi_env env, napi_callback_info info)
80 {
81 uLong err = Z_ERRNO;
82 const Bytef *buf = reinterpret_cast<const Bytef *>(DICTIONARY);
83 err = crc32_z(0L, buf, 0);
84 NAPI_ASSERT(env, err == Z_OK, "crc32_z error");
85
86 napi_value result = nullptr;
87 napi_create_int32(env, SUCCESS, &result);
88 return result;
89 }
90
Crc32Combine(napi_env env, napi_callback_info info)91 static napi_value Crc32Combine(napi_env env, napi_callback_info info)
92 {
93 uLong err = Z_ERRNO;
94 err = crc32_combine(0L, 0L, 0);
95 NAPI_ASSERT(env, err == Z_OK, "crc32_combine error");
96
97 napi_value result = nullptr;
98 napi_create_int32(env, SUCCESS, &result);
99 return result;
100 }
101
Adler32(napi_env env, napi_callback_info info)102 static napi_value Adler32(napi_env env, napi_callback_info info)
103 {
104 size_t argc = 1;
105 napi_value args[1];
106 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
107 uint32_t bufferType;
108 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &bufferType));
109
110 uLong err = Z_ERRNO;
111 if (bufferType == Z_NULL) {
112 err = adler32(0L, Z_NULL, 0);
113 NAPI_ASSERT(env, err == VALUE_ONE, "adler32 error");
114 } else {
115 const Bytef *buf = reinterpret_cast<const Bytef *>(DICTIONARY);
116 err = adler32(0L, buf, 0);
117 NAPI_ASSERT(env, err == Z_OK, "adler32 error");
118 }
119
120 napi_value result = nullptr;
121 napi_create_int32(env, SUCCESS, &result);
122 return result;
123 }
124
Adler32_z(napi_env env, napi_callback_info info)125 static napi_value Adler32_z(napi_env env, napi_callback_info info)
126 {
127 uLong err = Z_ERRNO;
128 const Bytef *buf = reinterpret_cast<const Bytef *>(DICTIONARY);
129 err = adler32_z(0L, buf, 0);
130 NAPI_ASSERT(env, err == Z_OK, "adler32_z error");
131
132 napi_value result = nullptr;
133 napi_create_int32(env, SUCCESS, &result);
134 return result;
135 }
136
Adler32Combine(napi_env env, napi_callback_info info)137 static napi_value Adler32Combine(napi_env env, napi_callback_info info)
138 {
139 uLong expect = 65520;
140 uLong err = Z_ERRNO;
141 err = adler32_combine(0L, 0L, 0);
142 NAPI_ASSERT(env, err == expect, "adler32_combine error");
143
144 napi_value result = nullptr;
145 napi_create_int32(env, SUCCESS, &result);
146 return result;
147 }
148
Deflate(napi_env env, napi_callback_info info)149 static napi_value Deflate(napi_env env, napi_callback_info info)
150 {
151 Byte *comp;
152 uLong compLen = 100 * sizeof(int);
153 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
154 NAPI_ASSERT(env, comp != nullptr, "comp is nullptr");
155
156 z_stream c_stream;
157 int err = Z_OK;
158 uLong len = static_cast<uLong>(strlen(HELLO)) + VALUE_ONE;
159 c_stream.zalloc = nullptr;
160 c_stream.zfree = nullptr;
161 c_stream.opaque = nullptr;
162 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
163 NAPI_ASSERT(env, err == Z_OK, "deflateInit error");
164
165 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
166 c_stream.next_out = comp;
167 while (c_stream.total_in != len && c_stream.total_out < compLen) {
168 c_stream.avail_in = c_stream.avail_out = VALUE_ONE;
169 err = deflate(&c_stream, Z_NO_FLUSH);
170 NAPI_ASSERT(env, err == Z_OK, "deflate Z_NO_FLUSH error");
171 }
172
173 for (;;) {
174 c_stream.avail_out = VALUE_ONE;
175 err = deflate(&c_stream, Z_FINISH);
176 if (err == Z_STREAM_END) {
177 break;
178 }
179 NAPI_ASSERT(env, err == Z_OK, "deflate Z_FINISH error");
180 }
181
182 err = deflateEnd(&c_stream);
183 NAPI_ASSERT(env, err == Z_OK, "deflateEnd error");
184 free(comp);
185
186 napi_value result = nullptr;
187 napi_create_int32(env, SUCCESS, &result);
188 return result;
189 }
190
DeflateFullFlush(napi_env env, napi_callback_info info)191 static napi_value DeflateFullFlush(napi_env env, napi_callback_info info)
192 {
193 Byte *comp;
194 uLong compLen = 100 * sizeof(int);
195 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
196 NAPI_ASSERT(env, comp != nullptr, "comp is nullptr");
197
198 z_stream c_stream;
199 int err = Z_OK;
200 uInt len = static_cast<uInt>(strlen(HELLO)) + VALUE_ONE;
201 c_stream.zalloc = nullptr;
202 c_stream.zfree = nullptr;
203 c_stream.opaque = nullptr;
204 err = deflateInit(&c_stream, Z_DEFAULT_COMPRESSION);
205 NAPI_ASSERT(env, err == Z_OK, "deflateInit error");
206
207 int changeLen = VALUE_THREE;
208 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
209 c_stream.next_out = comp;
210 c_stream.avail_in = changeLen;
211 c_stream.avail_out = static_cast<uInt>(compLen);
212 err = deflate(&c_stream, Z_FULL_FLUSH);
213 NAPI_ASSERT(env, err == Z_OK, "deflate error");
214
215 comp[changeLen]++;
216 c_stream.avail_in = len - changeLen;
217 err = deflate(&c_stream, Z_FINISH);
218 if (err != Z_STREAM_END) {
219 NAPI_ASSERT(env, err == Z_OK, "deflate error");
220 }
221
222 err = deflateEnd(&c_stream);
223 NAPI_ASSERT(env, err == Z_OK, "deflateEnd error");
224 compLen = c_stream.total_out;
225 free(comp);
226
227 napi_value result = nullptr;
228 napi_create_int32(env, SUCCESS, &result);
229 return result;
230 }
231
DeflateBound(napi_env env, napi_callback_info info)232 static napi_value DeflateBound(napi_env env, napi_callback_info info)
233 {
234 z_stream defstream;
235 char *inBuf = reinterpret_cast<char *>(HELLO);
236 uint32_t inLen = strlen(inBuf) + VALUE_ONE;
237 uint8_t *outBuf = nullptr;
238 uint32_t outLen = VALUE_ZERO;
239 int err = Z_OK;
240
241 defstream.zalloc = nullptr;
242 defstream.zfree = nullptr;
243 defstream.opaque = nullptr;
244 defstream.avail_in = static_cast<uInt>(inLen);
245 defstream.next_in = reinterpret_cast<Bytef *>(inBuf);
246 defstream.avail_out = static_cast<uInt>(outLen);
247 defstream.next_out = reinterpret_cast<Bytef *>(outBuf);
248 err = deflateInit_(&defstream, Z_DEFAULT_COMPRESSION, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
249 NAPI_ASSERT(env, err == Z_OK, "deflateInit_ error");
250 uint32_t estimateLen = deflateBound(&defstream, inLen);
251 NAPI_ASSERT(env, estimateLen == VALUE_TWENTYSEVEN, "deflateCopy error");
252 napi_value result = nullptr;
253 napi_create_int32(env, SUCCESS, &result);
254 return result;
255 }
256
Compress(napi_env env, napi_callback_info info)257 static napi_value Compress(napi_env env, napi_callback_info info)
258 {
259 size_t argc = 1;
260 napi_value args[1];
261 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
262 uint32_t expect;
263 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &expect));
264
265 Byte *comp;
266 uLong compLen = expect == Z_OK ? 100 * sizeof(int) : VALUE_ONE;
267 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
268 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
269
270 int err = Z_OK;
271 uLong len = static_cast<uLong>(strlen(HELLO)) + VALUE_ONE;
272 err = compress(comp, &compLen, reinterpret_cast<const Bytef *>(HELLO), len);
273 NAPI_ASSERT(env, err == expect, "compress error");
274
275 free(comp);
276
277 napi_value result = nullptr;
278 napi_create_int32(env, SUCCESS, &result);
279 return result;
280 }
281
CompressBound(napi_env env, napi_callback_info info)282 static napi_value CompressBound(napi_env env, napi_callback_info info)
283 {
284 Byte *comp;
285
286 int err = Z_OK;
287 uLong len = static_cast<uLong>(strlen(HELLO)) + VALUE_ONE;
288 uLong compLen = compressBound(len);
289 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
290 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
291
292 err = compress(comp, &compLen, reinterpret_cast<const Bytef *>(HELLO), len);
293 NAPI_ASSERT(env, err == Z_OK, "compress error");
294
295 free(comp);
296
297 napi_value result = nullptr;
298 napi_create_int32(env, SUCCESS, &result);
299 return result;
300 }
301
Compress2(napi_env env, napi_callback_info info)302 static napi_value Compress2(napi_env env, napi_callback_info info)
303 {
304 size_t argc = 2;
305 napi_value args[2];
306 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
307 uint32_t CompressType;
308 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &CompressType));
309 uint32_t expect;
310 NAPI_CALL(env, napi_get_value_uint32(env, args[1], &expect));
311
312 Byte *comp;
313 uLong compLen = expect == Z_OK ? 100 * sizeof(int) : VALUE_ONE;
314 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
315 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
316
317 int err = Z_OK;
318 uLong len = static_cast<uLong>(strlen(HELLO)) + VALUE_ONE;
319 err = compress2(comp, &compLen, reinterpret_cast<const Bytef *>(HELLO), len, CompressType);
320 NAPI_ASSERT(env, err == expect, "compress error");
321
322 free(comp);
323
324 napi_value result = nullptr;
325 napi_create_int32(env, SUCCESS, &result);
326 return result;
327 }
328
UnCompress(napi_env env, napi_callback_info info)329 static napi_value UnCompress(napi_env env, napi_callback_info info)
330 {
331 size_t argc = 1;
332 napi_value args[1];
333 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
334 uint32_t expect;
335 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &expect));
336
337 Byte *comp;
338 uLong compLen = 100 * sizeof(int);
339 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
340 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
341
342 int err = Z_OK;
343 uLong len = static_cast<uLong>(strlen(HELLO)) + VALUE_ONE;
344 err = compress(comp, &compLen, reinterpret_cast<const Bytef *>(HELLO), len);
345 NAPI_ASSERT(env, err == Z_OK, "compress error");
346
347 Byte *unComp;
348 uLong unCompLen = compLen;
349 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(unCompLen), CALLOC_SIZE));
350 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
351 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
352 if (expect == Z_BUF_ERROR) {
353 unCompLen = VALUE_ONE;
354 err = uncompress(unComp, &unCompLen, comp, compLen);
355 } else if (expect == Z_DATA_ERROR) {
356 err = uncompress(unComp, &unCompLen, comp, VALUE_ONE);
357 } else {
358 err = uncompress(unComp, &unCompLen, comp, compLen);
359 }
360 NAPI_ASSERT(env, err == expect, "uncompress error");
361
362 free(comp);
363 free(unComp);
364
365 napi_value result = nullptr;
366 napi_create_int32(env, SUCCESS, &result);
367 return result;
368 }
369
ZLibVersion(napi_env env, napi_callback_info info)370 static napi_value ZLibVersion(napi_env env, napi_callback_info info)
371 {
372 static const char *myVersion = ZLIB_VERSION;
373 static const char *err;
374 err = zlibVersion();
375 NAPI_ASSERT(env, err == myVersion, "zlibVersion error");
376 napi_value result = nullptr;
377 napi_create_int32(env, SUCCESS, &result);
378 return result;
379 }
380
ZLibCompileFlags(napi_env env, napi_callback_info info)381 static napi_value ZLibCompileFlags(napi_env env, napi_callback_info info)
382 {
383 uLong ret = zlibCompileFlags();
384 NAPI_ASSERT(env, ret >= NO_ERR, "zlibVersion error");
385 napi_value result = nullptr;
386 napi_create_uint32(env, SUCCESS, &result);
387 return result;
388 }
389
ZError(napi_env env, napi_callback_info info)390 static napi_value ZError(napi_env env, napi_callback_info info)
391 {
392 const char *err;
393 err = zError(Z_DATA_ERROR);
394 napi_value result = nullptr;
395 napi_create_string_utf8(env, err, NAPI_AUTO_LENGTH, &result);
396 return result;
397 }
398
UnCompress2(napi_env env, napi_callback_info info)399 static napi_value UnCompress2(napi_env env, napi_callback_info info)
400 {
401 size_t argc = 1;
402 napi_value args[1];
403 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
404 uint32_t expect;
405 NAPI_CALL(env, napi_get_value_uint32(env, args[0], &expect));
406
407 Byte *comp;
408 uLong compLen = 100 * sizeof(int);
409 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
410 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
411
412 int err = Z_OK;
413 uLong len = static_cast<uLong>(strlen(HELLO)) + VALUE_ONE;
414 err = compress2(comp, &compLen, reinterpret_cast<const Bytef *>(HELLO), len, Z_BEST_COMPRESSION);
415 NAPI_ASSERT(env, err == Z_OK, "compress2 error");
416
417 Byte *unComp;
418 uLong unCompLen = compLen;
419 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(unCompLen), CALLOC_SIZE));
420 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
421 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
422 if (expect == Z_BUF_ERROR) {
423 unCompLen = VALUE_ONE;
424 err = uncompress2(unComp, &unCompLen, comp, &compLen);
425 } else if (expect == Z_DATA_ERROR) {
426 compLen = VALUE_ONE;
427 err = uncompress2(unComp, &unCompLen, comp, &compLen);
428 } else {
429 err = uncompress2(unComp, &unCompLen, comp, &compLen);
430 }
431 NAPI_ASSERT(env, err == expect, "uncompress2 error");
432
433 free(comp);
434 free(unComp);
435
436 napi_value result = nullptr;
437 napi_create_int32(env, SUCCESS, &result);
438 return result;
439 }
440
InflateValidate(napi_env env, napi_callback_info info)441 static napi_value InflateValidate(napi_env env, napi_callback_info info)
442 {
443 Byte *comp, *unComp;
444 uLong compLen = 100 * sizeof(int);
445 uLong unCompLen = compLen;
446 comp = static_cast<Byte *>(calloc(static_cast<uInt>(compLen), CALLOC_SIZE));
447 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(unCompLen), CALLOC_SIZE));
448 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
449 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
450 int err = Z_OK;
451 z_stream d_stream;
452 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
453 d_stream.zalloc = nullptr;
454 d_stream.zfree = nullptr;
455 d_stream.opaque = nullptr;
456 d_stream.next_in = comp;
457 d_stream.avail_in = static_cast<uInt>(compLen);
458 err = inflateInit(&d_stream);
459 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
460 d_stream.next_out = unComp;
461 d_stream.avail_out = static_cast<uInt>(unCompLen);
462 err = inflateValidate(&d_stream, VALUE_ONE);
463 NAPI_ASSERT(env, err == Z_OK, "inflateValidate error");
464 free(comp);
465 free(unComp);
466 napi_value result = nullptr;
467 napi_create_int32(env, SUCCESS, &result);
468 return result;
469 }
470
InflateUndermine(napi_env env, napi_callback_info info)471 static napi_value InflateUndermine(napi_env env, napi_callback_info info)
472 {
473 Byte *comp, *unComp;
474 uLong comprLen = 100 * sizeof(int);
475 uLong uncomprLen = comprLen;
476 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
477 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
478 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
479 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
480 int err = Z_OK;
481 z_stream d_stream;
482 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
483 d_stream.zalloc = nullptr;
484 d_stream.zfree = nullptr;
485 d_stream.opaque = nullptr;
486 d_stream.next_in = comp;
487 d_stream.avail_in = static_cast<uInt>(comprLen);
488 err = inflateInit(&d_stream);
489 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
490 d_stream.next_out = unComp;
491 d_stream.avail_out = static_cast<uInt>(uncomprLen);
492 err = inflateUndermine(&d_stream, VALUE_ONE);
493 NAPI_ASSERT(env, err == Z_DATA_ERROR, "inflateUndermine OK");
494 free(comp);
495 free(unComp);
496 napi_value result = nullptr;
497 napi_create_int32(env, SUCCESS, &result);
498 return result;
499 }
500
InflateSyncPoint(napi_env env, napi_callback_info info)501 static napi_value InflateSyncPoint(napi_env env, napi_callback_info info)
502 {
503 Byte *comp, *unComp;
504 uLong comprLen = 100 * sizeof(int);
505 uLong uncomprLen = comprLen;
506 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
507 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
508 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
509 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
510
511 int err = Z_OK;
512 z_stream d_stream;
513 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
514 d_stream.zalloc = nullptr;
515 d_stream.zfree = nullptr;
516 d_stream.opaque = nullptr;
517 d_stream.next_in = comp;
518 d_stream.avail_in = static_cast<uInt>(comprLen);
519 err = inflateInit(&d_stream);
520 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
521 d_stream.next_out = unComp;
522 d_stream.avail_out = static_cast<uInt>(uncomprLen);
523 err = inflateSyncPoint(&d_stream);
524 NAPI_ASSERT(env, err == Z_OK, "inflateSyncPoint error");
525 free(comp);
526 free(unComp);
527 napi_value result = nullptr;
528 napi_create_int32(env, SUCCESS, &result);
529 return result;
530 }
531
InflateSetDictionary(napi_env env, napi_callback_info info)532 static napi_value InflateSetDictionary(napi_env env, napi_callback_info info)
533 {
534 Byte *comp, *unComp;
535 uLong comprLen = 100 * sizeof(int);
536 uLong uncomprLen = comprLen;
537 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
538 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
539 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
540 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
541
542 int err = Z_OK;
543 z_stream d_stream;
544 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
545 d_stream.zalloc = nullptr;
546 d_stream.zfree = nullptr;
547 d_stream.opaque = nullptr;
548 d_stream.next_in = comp;
549 d_stream.avail_in = static_cast<uInt>(comprLen);
550 err = inflateInit(&d_stream);
551 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
552 d_stream.next_out = unComp;
553 d_stream.avail_out = static_cast<uInt>(uncomprLen);
554 inflate(&d_stream, Z_NO_FLUSH);
555 inflateSetDictionary(&d_stream, reinterpret_cast<const Bytef *>(DICTIONARY), static_cast<int>(sizeof(DICTIONARY)));
556 err = inflateEnd(&d_stream);
557 NAPI_ASSERT(env, err == Z_OK, "inflateEnd error");
558 free(comp);
559 free(unComp);
560 napi_value result = nullptr;
561 napi_create_int32(env, SUCCESS, &result);
562 return result;
563 }
564
InflateSync(napi_env env, napi_callback_info info)565 static napi_value InflateSync(napi_env env, napi_callback_info info)
566 {
567 Byte *comp, *unComp;
568 uLong comprLen = 100 * sizeof(int);
569 uLong uncomprLen = comprLen;
570 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
571 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
572 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
573 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
574
575 int err = Z_OK;
576 z_stream d_stream;
577 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
578 d_stream.zalloc = nullptr;
579 d_stream.zfree = nullptr;
580 d_stream.opaque = nullptr;
581 d_stream.next_in = comp;
582 d_stream.avail_in = VALUE_TWO;
583 err = inflateInit(&d_stream);
584 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
585
586 d_stream.next_out = unComp;
587 d_stream.avail_out = static_cast<uInt>(uncomprLen);
588
589 inflate(&d_stream, Z_NO_FLUSH);
590 d_stream.avail_in = static_cast<uInt>(comprLen) - VALUE_TWO;
591 inflateSync(&d_stream);
592 inflate(&d_stream, Z_FINISH);
593 err = inflateEnd(&d_stream);
594 NAPI_ASSERT(env, err == Z_OK, "inflateEnd error");
595 free(comp);
596 free(unComp);
597 napi_value result = nullptr;
598 napi_create_int32(env, SUCCESS, &result);
599 return result;
600 }
601
InflateResetKeep(napi_env env, napi_callback_info info)602 static napi_value InflateResetKeep(napi_env env, napi_callback_info info)
603 {
604 Byte *comp, *unComp;
605 uLong comprLen = 100 * sizeof(int);
606 uLong uncomprLen = comprLen;
607 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
608 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
609 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
610 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
611 int err = Z_OK;
612 z_stream d_stream;
613 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
614 d_stream.zalloc = nullptr;
615 d_stream.zfree = nullptr;
616 d_stream.opaque = nullptr;
617 d_stream.next_in = comp;
618 d_stream.avail_in = VALUE_TWO;
619 err = inflateInit(&d_stream);
620 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
621 d_stream.next_out = unComp;
622 d_stream.avail_out = static_cast<uInt>(uncomprLen);
623 inflate(&d_stream, Z_NO_FLUSH);
624 err = inflateResetKeep(&d_stream);
625 NAPI_ASSERT(env, err == Z_OK, "inflateResetKeep error");
626 free(comp);
627 free(unComp);
628 napi_value result = nullptr;
629 napi_create_int32(env, SUCCESS, &result);
630 return result;
631 }
632
InflateReset2(napi_env env, napi_callback_info info)633 static napi_value InflateReset2(napi_env env, napi_callback_info info)
634 {
635 Byte *comp, *unComp;
636 uLong comprLen = 100 * sizeof(int);
637 uLong uncomprLen = comprLen;
638 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
639 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
640 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
641 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
642 int err = Z_OK;
643 int windowBits = VALUE_EIGHT;
644 z_stream d_stream;
645 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
646 d_stream.zalloc = nullptr;
647 d_stream.zfree = nullptr;
648 d_stream.opaque = nullptr;
649 d_stream.next_in = comp;
650 d_stream.avail_in = VALUE_TWO;
651 err = inflateInit(&d_stream);
652 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
653 d_stream.next_out = unComp;
654 d_stream.avail_out = static_cast<uInt>(uncomprLen);
655 err = inflateInit2(&d_stream, windowBits);
656 inflate(&d_stream, Z_NO_FLUSH);
657 err = inflateReset2(&d_stream, windowBits);
658 NAPI_ASSERT(env, err == Z_OK, "inflateReset2 error");
659 free(comp);
660 free(unComp);
661 napi_value result = nullptr;
662 napi_create_int32(env, SUCCESS, &result);
663 return result;
664 }
InflateReset(napi_env env, napi_callback_info info)665 static napi_value InflateReset(napi_env env, napi_callback_info info)
666 {
667 Byte *comp, *unComp;
668 uLong comprLen = 100 * sizeof(int);
669 uLong uncomprLen = comprLen;
670 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
671 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
672 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
673 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
674 int err = Z_OK;
675 z_stream d_stream;
676 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
677 d_stream.zalloc = nullptr;
678 d_stream.zfree = nullptr;
679 d_stream.opaque = nullptr;
680 d_stream.next_in = comp;
681 d_stream.avail_in = VALUE_TWO;
682 err = inflateInit(&d_stream);
683 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
684 d_stream.next_out = unComp;
685 d_stream.avail_out = static_cast<uInt>(uncomprLen);
686 inflate(&d_stream, Z_NO_FLUSH);
687 err = inflateReset(&d_stream);
688 NAPI_ASSERT(env, err == Z_OK, "inflateReset error");
689 free(comp);
690 free(unComp);
691 napi_value result = nullptr;
692 napi_create_int32(env, SUCCESS, &result);
693 return result;
694 }
695
InflatePrime(napi_env env, napi_callback_info info)696 static napi_value InflatePrime(napi_env env, napi_callback_info info)
697 {
698 int ret;
699 z_stream strm;
700 struct mem_zone *zone;
701 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
702 NAPI_ASSERT(env, zone != Z_NULL, "zone is Z_NULL");
703 zone->first = nullptr;
704 zone->total = VALUE_ZERO;
705 zone->highwater = VALUE_ZERO;
706 zone->limit = VALUE_ZERO;
707 zone->notlifo = VALUE_ZERO;
708 zone->rogue = VALUE_ZERO;
709 strm.opaque = zone;
710 strm.zalloc = nullptr;
711 strm.zfree = nullptr;
712 strm.avail_in = VALUE_ZERO;
713 strm.next_in = nullptr;
714 ret = inflateInit(&strm);
715 NAPI_ASSERT(env, ret == Z_OK, "inflateInit error");
716 ret = inflatePrime(&strm, VALUE_FIVE, VALUE_THIRTYONE);
717 NAPI_ASSERT(env, ret == Z_OK, "inflatePrime error");
718 free(zone);
719 napi_value result = nullptr;
720 napi_create_int32(env, SUCCESS, &result);
721 return result;
722 }
723
InflateMark(napi_env env, napi_callback_info info)724 static napi_value InflateMark(napi_env env, napi_callback_info info)
725 {
726 Byte *comp, *unComp;
727 uLong comprLen = 100 * sizeof(int);
728 uLong uncomprLen = comprLen;
729 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
730 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
731 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
732 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
733
734 int err = Z_OK;
735 z_stream d_stream;
736 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
737 d_stream.zalloc = nullptr;
738 d_stream.zfree = nullptr;
739 d_stream.opaque = nullptr;
740 d_stream.next_in = comp;
741 d_stream.avail_in = static_cast<uInt>(comprLen);
742 err = inflateInit(&d_stream);
743 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
744 d_stream.next_out = unComp;
745 d_stream.avail_out = static_cast<uInt>(uncomprLen);
746 err = inflate(&d_stream, Z_NO_FLUSH);
747 err = inflateGetDictionary(&d_stream, unComp, nullptr);
748 NAPI_ASSERT(env, err == Z_OK, "inflateGetDictionary error");
749 long mark = inflateMark(&d_stream);
750 NAPI_ASSERT(env, mark == STREAM_STATE, "inflateEnd error");
751 free(comp);
752 free(unComp);
753 napi_value result = nullptr;
754 napi_create_int32(env, SUCCESS, &result);
755 return result;
756 }
757
InflateInit2(napi_env env, napi_callback_info info)758 static napi_value InflateInit2(napi_env env, napi_callback_info info)
759 {
760 int err = Z_OK;
761 int windowBits = VALUE_EIGHT;
762 z_stream strm;
763 struct mem_zone *zone;
764 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
765 NAPI_ASSERT(env, zone != Z_NULL, "zone is Z_NULL");
766 zone->first = nullptr;
767 zone->total = VALUE_ZERO;
768 zone->highwater = VALUE_ZERO;
769 zone->limit = VALUE_ZERO;
770 zone->notlifo = VALUE_ZERO;
771 zone->rogue = VALUE_ZERO;
772 strm.opaque = zone;
773 strm.zalloc = nullptr;
774 strm.zfree = nullptr;
775 strm.avail_in = VALUE_ZERO;
776 strm.next_in = nullptr;
777 err = inflateInit(&strm);
778 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
779 err = inflatePrime(&strm, VALUE_FIVE, VALUE_THIRTYONE);
780 NAPI_ASSERT(env, err == Z_OK, "inflatePrime error");
781 err = inflatePrime(&strm, -1, VALUE_ZERO);
782 NAPI_ASSERT(env, err == Z_OK, "inflatePrime error");
783 err = inflateSetDictionary(&strm, nullptr, VALUE_ZERO);
784 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "inflateSetDictionary error");
785 err = inflateEnd(&strm);
786 NAPI_ASSERT(env, err == Z_OK, "inflateEnd error");
787 strm.avail_in = VALUE_ZERO;
788 strm.next_in = nullptr;
789 err = inflateInit2_(&strm, windowBits, ZLIB_VERSION - VALUE_ONE, static_cast<int>(sizeof(z_stream)));
790 NAPI_ASSERT(env, err == Z_VERSION_ERROR, "inflateInit2_ error");
791 free(zone);
792 napi_value result = nullptr;
793 napi_create_int32(env, SUCCESS, &result);
794 return result;
795 }
796
InflateInit(napi_env env, napi_callback_info info)797 static napi_value InflateInit(napi_env env, napi_callback_info info)
798 {
799 int err = Z_OK;
800 z_stream strm;
801 struct mem_zone *zone;
802 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
803 NAPI_ASSERT(env, zone != Z_NULL, "zone is Z_NULL");
804 zone->first = nullptr;
805 zone->total = VALUE_ZERO;
806 zone->highwater = VALUE_ZERO;
807 zone->limit = VALUE_ZERO;
808 zone->notlifo = VALUE_ZERO;
809 zone->rogue = VALUE_ZERO;
810 strm.opaque = zone;
811 strm.zalloc = nullptr;
812 strm.zfree = nullptr;
813 strm.avail_in = VALUE_ZERO;
814 strm.next_in = nullptr;
815 err = inflateInit(&strm);
816 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
817 err = inflatePrime(&strm, VALUE_FIVE, VALUE_THIRTYONE);
818 NAPI_ASSERT(env, err == Z_OK, "inflatePrime error");
819 err = inflatePrime(&strm, -1, VALUE_ZERO);
820 NAPI_ASSERT(env, err == Z_OK, "inflatePrime error");
821 err = inflateSetDictionary(&strm, nullptr, VALUE_ZERO);
822 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "inflateSetDictionary error");
823 err = inflateEnd(&strm);
824 NAPI_ASSERT(env, err == Z_OK, "inflateEnd error");
825 strm.avail_in = VALUE_ZERO;
826 strm.next_in = nullptr;
827 err = inflateInit_(&strm, ZLIB_VERSION - VALUE_ONE, static_cast<int>(sizeof(z_stream)));
828 NAPI_ASSERT(env, err == Z_VERSION_ERROR, "inflateInit_ error");
829 free(zone);
830 napi_value result = nullptr;
831 napi_create_int32(env, SUCCESS, &result);
832 return result;
833 }
834
InflateGetHeader(napi_env env, napi_callback_info info)835 static napi_value InflateGetHeader(napi_env env, napi_callback_info info)
836 {
837 struct mem_zone *zone;
838 zone = (struct mem_zone *)malloc(sizeof(struct mem_zone));
839 NAPI_ASSERT(env, zone != Z_NULL, "zone is Z_NULL");
840 zone->first = nullptr;
841 zone->total = VALUE_ZERO;
842 zone->highwater = VALUE_ZERO;
843 zone->limit = VALUE_ZERO;
844 zone->notlifo = VALUE_ZERO;
845 zone->rogue = VALUE_ZERO;
846 int err = Z_OK;
847 unsigned len = VALUE_ONE;
848 unsigned char *out;
849 z_stream strm;
850 gz_header head;
851 strm.opaque = zone;
852 strm.zalloc = nullptr;
853 strm.zfree = nullptr;
854 strm.avail_in = VALUE_ZERO;
855 strm.next_in = nullptr;
856 err = inflateInit2(&strm, VALUE_ONE);
857 NAPI_ASSERT(env, err != Z_OK, "inflateInit2 error");
858 out = (unsigned char *)malloc(len);
859 NAPI_ASSERT(env, out != nullptr, "malloc error");
860 head.extra = out;
861 head.extra_max = len;
862 head.name = out;
863 head.name_max = len;
864 head.comment = out;
865 head.comm_max = len;
866 err = inflateGetHeader(&strm, &head);
867 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "inflateGetHeader error");
868 free(out);
869 free(zone);
870 napi_value result = nullptr;
871 napi_create_int32(env, SUCCESS, &result);
872 return result;
873 }
874
InflateGetDictionary(napi_env env, napi_callback_info info)875 static napi_value InflateGetDictionary(napi_env env, napi_callback_info info)
876 {
877 Byte *comp, *unComp;
878 uLong comprLen = 100 * sizeof(int);
879 uLong uncomprLen = comprLen;
880 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
881 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
882 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
883 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
884
885 int err = Z_OK;
886 z_stream d_stream;
887 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
888 d_stream.zalloc = nullptr;
889 d_stream.zfree = nullptr;
890 d_stream.opaque = nullptr;
891 d_stream.next_in = comp;
892 d_stream.avail_in = static_cast<uInt>(comprLen);
893 err = inflateInit(&d_stream);
894 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
895 d_stream.next_out = unComp;
896 d_stream.avail_out = static_cast<uInt>(uncomprLen);
897 err = inflate(&d_stream, Z_NO_FLUSH);
898 err = inflateGetDictionary(&d_stream, unComp, nullptr);
899 NAPI_ASSERT(env, err == Z_OK, "inflateGetDictionary error");
900 free(comp);
901 free(unComp);
902 napi_value result = nullptr;
903 napi_create_int32(env, SUCCESS, &result);
904 return result;
905 }
906
InflateEnd(napi_env env, napi_callback_info info)907 static napi_value InflateEnd(napi_env env, napi_callback_info info)
908 {
909 int err = Z_OK;
910 err = inflate(nullptr, VALUE_ZERO);
911 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "inflate error");
912 err = inflateEnd(nullptr);
913 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "inflateEnd error");
914 napi_value result = nullptr;
915 napi_create_int32(env, SUCCESS, &result);
916 return result;
917 }
918
InflateCopy(napi_env env, napi_callback_info info)919 static napi_value InflateCopy(napi_env env, napi_callback_info info)
920 {
921 int err = Z_OK;
922 err = inflate(nullptr, VALUE_ZERO);
923 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "inflate error");
924 err = inflateCopy(nullptr, nullptr);
925 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "inflateCopy error");
926 napi_value result = nullptr;
927 napi_create_int32(env, SUCCESS, &result);
928 return result;
929 }
930
InflateCodesUsed(napi_env env, napi_callback_info info)931 static napi_value InflateCodesUsed(napi_env env, napi_callback_info info)
932 {
933 Byte *comp, *unComp;
934 uLong comprLen = 100 * sizeof(int);
935 uLong uncomprLen = comprLen;
936 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
937 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
938 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
939 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
940 unsigned long err;
941 z_stream d_stream;
942 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
943 d_stream.zalloc = nullptr;
944 d_stream.zfree = nullptr;
945 d_stream.opaque = nullptr;
946 d_stream.next_in = comp;
947 d_stream.avail_in = VALUE_ZERO;
948 d_stream.next_out = unComp;
949 err = inflateCodesUsed(&d_stream);
950 NAPI_ASSERT(env, err != Z_OK, "inflateCodesUsed error");
951 free(comp);
952 free(unComp);
953 napi_value result = nullptr;
954 napi_create_int32(env, SUCCESS, &result);
955 return result;
956 }
957
InflateBackInit(napi_env env, napi_callback_info info)958 static napi_value InflateBackInit(napi_env env, napi_callback_info info)
959 {
960 int err = Z_OK;
961 unsigned char *window;
962 z_stream strm;
963 unsigned char match[65280 + 2];
964 window = match;
965 strm.zalloc = nullptr;
966 strm.zfree = nullptr;
967 strm.opaque = nullptr;
968 err = inflateBackInit_(&strm, VALUE_FIFTEEN, window, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
969 NAPI_ASSERT(env, err == Z_OK, "inflateBackInit_ error");
970 napi_value result = nullptr;
971 napi_create_int32(env, SUCCESS, &result);
972 return result;
973 }
pull(void *desc, unsigned char **buf)974 static unsigned pull(void *desc, unsigned char **buf)
975 {
976 static unsigned int next = VALUE_ZERO;
977 static unsigned char dat[] = {0x63, 0, 2, 0};
978
979 if (!desc) {
980 next = VALUE_ZERO;
981 return VALUE_ZERO;
982 }
983 return next < sizeof(dat) ? (*buf = dat + next++, VALUE_ONE) : 0;
984 }
985
push(void *desc, unsigned char *buf, unsigned len)986 static int push(void *desc, unsigned char *buf, unsigned len)
987 {
988 buf += len;
989 return desc != nullptr;
990 }
InflateBack(napi_env env, napi_callback_info info)991 static napi_value InflateBack(napi_env env, napi_callback_info info)
992 {
993 int err = Z_OK;
994 unsigned char *window;
995 z_stream strm;
996 unsigned char match[65280 + 2];
997 Byte *cunComp;
998 uLong uncomprLen = 100 * sizeof(int);
999 cunComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1000 window = match;
1001 strm.zalloc = nullptr;
1002 strm.zfree = nullptr;
1003 strm.opaque = nullptr;
1004 err = inflateBackInit_(&strm, VALUE_FIFTEEN, window, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
1005 NAPI_ASSERT(env, err == Z_OK, "inflateBackInit_ error");
1006 strm.next_in = cunComp;
1007 strm.avail_in = VALUE_ONE;
1008 err = inflateBack(&strm, pull, nullptr, push, &strm);
1009 NAPI_ASSERT(env, err == Z_BUF_ERROR, "inflateBack error");
1010 free(cunComp);
1011 napi_value result = nullptr;
1012 napi_create_int32(env, SUCCESS, &result);
1013 return result;
1014 }
1015
InflateBackEnd(napi_env env, napi_callback_info info)1016 static napi_value InflateBackEnd(napi_env env, napi_callback_info info)
1017 {
1018 int err = Z_OK;
1019 unsigned char *window;
1020 z_stream strm;
1021 unsigned char match[65280 + 2];
1022 Byte *cunComp;
1023 uLong uncomprLen = 100 * sizeof(int);
1024 cunComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1025
1026 window = match;
1027 strm.zalloc = nullptr;
1028 strm.zfree = nullptr;
1029 strm.opaque = nullptr;
1030 err = inflateBackInit_(&strm, VALUE_FIFTEEN, window, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
1031 NAPI_ASSERT(env, err == Z_OK, "inflateBackInit_ error");
1032 strm.next_in = cunComp;
1033 strm.avail_in = VALUE_ONE;
1034 err = inflateBackEnd(&strm);
1035 NAPI_ASSERT(env, err == Z_OK, "inflateBackEnd error");
1036 free(cunComp);
1037 napi_value result = nullptr;
1038 napi_create_int32(env, SUCCESS, &result);
1039 return result;
1040 }
1041
Inflate(napi_env env, napi_callback_info info)1042 static napi_value Inflate(napi_env env, napi_callback_info info)
1043 {
1044 Byte *comp, *unComp;
1045 uLong comprLen = 100 * sizeof(int);
1046 uLong uncomprLen = comprLen;
1047 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1048 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1049 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1050 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1051
1052 int err = Z_OK;
1053 z_stream d_stream;
1054 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
1055 d_stream.zalloc = nullptr;
1056 d_stream.zfree = nullptr;
1057 d_stream.opaque = nullptr;
1058 d_stream.next_in = comp;
1059 d_stream.avail_in = static_cast<uInt>(comprLen);
1060 err = inflateInit(&d_stream);
1061 NAPI_ASSERT(env, err == Z_OK, "inflateInit error");
1062 d_stream.next_out = unComp;
1063 d_stream.avail_out = static_cast<uInt>(uncomprLen);
1064 err = inflate(&d_stream, Z_NO_FLUSH);
1065 NAPI_ASSERT(env, err == Z_DATA_ERROR, "inflate error");
1066 free(comp);
1067 free(unComp);
1068 napi_value result = nullptr;
1069 napi_create_int32(env, SUCCESS, &result);
1070 return result;
1071 }
1072
GzWrite(napi_env env, napi_callback_info info)1073 static napi_value GzWrite(napi_env env, napi_callback_info info)
1074 {
1075 napi_value result = nullptr;
1076 int len = static_cast<int>(strlen(HELLO)) + VALUE_ONE;
1077 gzFile file;
1078 file = gzopen(TESTFILE, "wb");
1079 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1080 int err = gzwrite(file, HELLO, len);
1081 NAPI_ASSERT(env, err == len, "gzwrite error");
1082 gzclose(file);
1083 napi_create_int32(env, SUCCESS, &result);
1084 return result;
1085 }
TestGzPrintf(gzFile file, const char *format, ...)1086 static int TestGzPrintf(gzFile file, const char *format, ...)
1087 {
1088 va_list va;
1089 int ret;
1090
1091 va_start(va, format);
1092 ret = gzvprintf(file, format, va);
1093 va_end(va);
1094 return ret;
1095 }
1096
GzvPrintf(napi_env env, napi_callback_info info)1097 static napi_value GzvPrintf(napi_env env, napi_callback_info info)
1098 {
1099 napi_value result = nullptr;
1100 gzFile file;
1101 file = gzopen(TESTFILE, "wb");
1102 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1103 int err = TestGzPrintf(file, ", %s!", "hello");
1104 NAPI_ASSERT(env, err == VALUE_EIGHT, "gzopen error");
1105 gzclose(file);
1106 napi_create_int32(env, SUCCESS, &result);
1107 return result;
1108 }
1109
GzUnGetC(napi_env env, napi_callback_info info)1110 static napi_value GzUnGetC(napi_env env, napi_callback_info info)
1111 {
1112 napi_value result = nullptr;
1113 gzFile file;
1114 file = gzopen(TESTFILE, "wb");
1115 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1116 NAPI_ASSERT(env, gzungetc('a', file) != 'a', "gzungetc error");
1117 gzclose(file);
1118 napi_create_int32(env, SUCCESS, &result);
1119 return result;
1120 }
1121
GzTell64(napi_env env, napi_callback_info info)1122 static napi_value GzTell64(napi_env env, napi_callback_info info)
1123 {
1124 napi_value result = nullptr;
1125 gzFile file;
1126 file = gzopen(TESTFILE, "wb");
1127 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1128 gzseek(file, 1L, SEEK_CUR);
1129 gzclose(file);
1130 file = gzopen(TESTFILE, "rb");
1131 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1132 z_off_t pos;
1133 pos = gzseek(file, -8L, SEEK_CUR);
1134 NAPI_ASSERT(env, pos != VALUE_SIX, "gzopen error");
1135 #ifdef Z_LARGE64
1136 NAPI_ASSERT(env, gztell64(file) != pos, "gzopen error");
1137 #endif
1138 gzclose(file);
1139
1140 napi_create_int32(env, SUCCESS, &result);
1141 return result;
1142 }
1143
GzTell(napi_env env, napi_callback_info info)1144 static napi_value GzTell(napi_env env, napi_callback_info info)
1145 {
1146 napi_value result = nullptr;
1147 gzFile file;
1148 file = gzopen(TESTFILE, "wb");
1149 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1150 char ret = VALUE_ZERO;
1151 NAPI_ASSERT(env, gztell(file) == ret, "gzseek error");
1152 gzclose(file);
1153 napi_create_int32(env, SUCCESS, &result);
1154 return result;
1155 }
1156
GzSetParams(napi_env env, napi_callback_info info)1157 static napi_value GzSetParams(napi_env env, napi_callback_info info)
1158 {
1159 napi_value result = nullptr;
1160 int err = Z_OK;
1161 gzFile file;
1162 file = gzopen(TESTFILE, "wb");
1163 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1164 err = gzsetparams(file, Z_DEFAULT_COMPRESSION, Z_DEFAULT_STRATEGY);
1165 NAPI_ASSERT(env, err == Z_OK, "gzopen error");
1166 gzclose(file);
1167 napi_create_int32(env, SUCCESS, &result);
1168 return result;
1169 }
1170
GzSeek64(napi_env env, napi_callback_info info)1171 static napi_value GzSeek64(napi_env env, napi_callback_info info)
1172 {
1173 napi_value result = nullptr;
1174 long err = 0L;
1175 gzFile file;
1176 file = gzopen(TESTFILE, "wb");
1177 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1178 #ifdef Z_LARGE64
1179 err = gzseek64(file, 1L, SEEK_CUR);
1180 #endif
1181 char ret = VALUE_ZERO;
1182 NAPI_ASSERT(env, err == ret, "gzseek64 error");
1183 gzclose(file);
1184 napi_create_int32(env, SUCCESS, &result);
1185 return result;
1186 }
1187
GzSeek(napi_env env, napi_callback_info info)1188 static napi_value GzSeek(napi_env env, napi_callback_info info)
1189 {
1190 napi_value result = nullptr;
1191 long err = 0L;
1192 gzFile file;
1193 file = gzopen(TESTFILE, "wb");
1194 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1195 err = gzseek(file, 1L, SEEK_CUR);
1196 NAPI_ASSERT(env, err == 1L, "gzseek error");
1197 gzclose(file);
1198 napi_create_int32(env, SUCCESS, &result);
1199 return result;
1200 }
1201
GzRewind(napi_env env, napi_callback_info info)1202 static napi_value GzRewind(napi_env env, napi_callback_info info)
1203 {
1204 napi_value result = nullptr;
1205 int err = Z_OK;
1206 gzFile file;
1207 file = gzopen(TESTFILE, "wb");
1208 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1209 gzseek(file, 0L, SEEK_SET);
1210 err = gzrewind(file);
1211 NAPI_ASSERT(env, err == -1, "gzrewind error");
1212 gzclose(file);
1213 napi_create_int32(env, SUCCESS, &result);
1214 return result;
1215 }
1216
GzRead(napi_env env, napi_callback_info info)1217 static napi_value GzRead(napi_env env, napi_callback_info info)
1218 {
1219 napi_value result = nullptr;
1220 gzFile file;
1221 file = gzopen(TESTFILE, "wb");
1222 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1223 NAPI_ASSERT(env, gzungetc('a', file) != 'a', "gzungetc error");
1224 char sz_read[5] = {0};
1225 gzread(file, sz_read, VALUE_ONE);
1226 char ret = VALUE_ZERO;
1227 NAPI_ASSERT(env, sz_read[0] == ret, "gzread error");
1228 gzclose(file);
1229 napi_create_int32(env, SUCCESS, &result);
1230 return result;
1231 }
1232
GzPuts(napi_env env, napi_callback_info info)1233 static napi_value GzPuts(napi_env env, napi_callback_info info)
1234 {
1235 napi_value result = nullptr;
1236 gzFile file;
1237 file = gzopen(TESTFILE, "wb");
1238 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1239 NAPI_ASSERT(env, gzputs(file, "ello") == VALUE_FOUR, "gzputs error");
1240 gzclose(file);
1241 napi_create_int32(env, SUCCESS, &result);
1242 return result;
1243 }
1244
GzPutc(napi_env env, napi_callback_info info)1245 static napi_value GzPutc(napi_env env, napi_callback_info info)
1246 {
1247 napi_value result = nullptr;
1248 char err;
1249 gzFile file;
1250 file = gzopen(TESTFILE, "wb");
1251 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1252 err = gzputc(file, 'h');
1253 NAPI_ASSERT(env, err == 'h', "gzputc error");
1254 gzclose(file);
1255 napi_create_int32(env, SUCCESS, &result);
1256 return result;
1257 }
1258
GzPrintf(napi_env env, napi_callback_info info)1259 static napi_value GzPrintf(napi_env env, napi_callback_info info)
1260 {
1261 napi_value result = nullptr;
1262 gzFile file;
1263 file = gzopen(TESTFILE, "wb");
1264 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1265 NAPI_ASSERT(env, gzprintf(file, ", %s!", "hello") == VALUE_EIGHT, "gzprintf error");
1266 gzclose(file);
1267 napi_create_int32(env, SUCCESS, &result);
1268 return result;
1269 }
1270
GzOpenW(napi_env env, napi_callback_info info)1271 static napi_value GzOpenW(napi_env env, napi_callback_info info)
1272 {
1273 #if defined(_WIN32) && !defined(Z_SOLO)
1274 gzFile file = nullptr;
1275 file = gzopen_w(TESTFILE, "wb");
1276 NAPI_ASSERT(env, file != nullptr, "gzopen_w error");
1277 gzclose(file);
1278 #endif
1279 napi_value result = nullptr;
1280 napi_create_int32(env, SUCCESS, &result);
1281 return result;
1282 }
1283
GzOpen(napi_env env, napi_callback_info info)1284 static napi_value GzOpen(napi_env env, napi_callback_info info)
1285 {
1286 gzFile file;
1287 file = gzopen(TESTFILE, "wb");
1288 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1289 gzclose(file);
1290 napi_value result = nullptr;
1291 napi_create_int32(env, SUCCESS, &result);
1292 return result;
1293 }
1294
GzOpen64(napi_env env, napi_callback_info info)1295 static napi_value GzOpen64(napi_env env, napi_callback_info info)
1296 {
1297 #ifdef Z_LARGE64
1298 gzFile file = nullptr;
1299 file = gzopen64(TESTFILE, "wb");
1300 NAPI_ASSERT(env, file != nullptr, "gzopen64 error");
1301 gzclose(file);
1302 #endif
1303 napi_value result = nullptr;
1304 napi_create_int32(env, SUCCESS, &result);
1305 return result;
1306 }
1307
GzOffset64(napi_env env, napi_callback_info info)1308 static napi_value GzOffset64(napi_env env, napi_callback_info info)
1309 {
1310 int err = Z_OK;
1311 gzFile file;
1312 file = gzopen(TESTFILE, "rb");
1313 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1314 #ifdef Z_LARGE64
1315 err = gzoffset64(file);
1316 #endif
1317 NAPI_ASSERT(env, err == Z_OK, "gzoffset64 error");
1318 gzclose(file);
1319 napi_value result = nullptr;
1320 napi_create_int32(env, SUCCESS, &result);
1321 return result;
1322 }
1323
GzGets(napi_env env, napi_callback_info info)1324 static napi_value GzGets(napi_env env, napi_callback_info info)
1325 {
1326 gzFile file;
1327 file = gzopen(TESTFILE, "wb");
1328 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1329 Byte *unComp;
1330 uLong uncomprLen = 100 * sizeof(int);
1331 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1332 strncpy(reinterpret_cast<char *>(unComp), GARBAGE, GARBAGE_LEN);
1333 gzgets(file, reinterpret_cast<char *>(unComp), static_cast<int>(uncomprLen));
1334 NAPI_ASSERT(env, strcmp(reinterpret_cast<char *>(unComp), HELLO + VALUE_SIX), "gzgets error");
1335 gzclose(file);
1336 free(unComp);
1337 napi_value result = nullptr;
1338 napi_create_int32(env, SUCCESS, &result);
1339 return result;
1340 }
1341
GzGetC_(napi_env env, napi_callback_info info)1342 static napi_value GzGetC_(napi_env env, napi_callback_info info)
1343 {
1344 int err = Z_OK;
1345 gzFile file;
1346 file = gzopen(TESTFILE, "wb");
1347 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1348 err = gzgetc_(file);
1349 NAPI_ASSERT(env, err == FAIL, "gzgetc_ error");
1350 gzclose(file);
1351 napi_value result = nullptr;
1352 napi_create_int32(env, SUCCESS, &result);
1353 return result;
1354 }
1355
GzGetC(napi_env env, napi_callback_info info)1356 static napi_value GzGetC(napi_env env, napi_callback_info info)
1357 {
1358 int err = Z_OK;
1359 gzFile file;
1360 file = gzopen(TESTFILE, "wb");
1361 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1362 err = gzgetc(file);
1363 NAPI_ASSERT(env, err == FAIL, "gzgetc error");
1364 gzclose(file);
1365 napi_value result = nullptr;
1366 napi_create_int32(env, SUCCESS, &result);
1367 return result;
1368 }
1369
GzFWrite(napi_env env, napi_callback_info info)1370 static napi_value GzFWrite(napi_env env, napi_callback_info info)
1371 {
1372 int err = Z_OK;
1373 int len = static_cast<int>(strlen(HELLO)) + VALUE_ONE;
1374 gzFile file;
1375 file = gzopen(TESTFILE, "wb");
1376 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1377 err = gzfwrite(HELLO, len, len, file);
1378 NAPI_ASSERT(env, err == len, "gzfwrite error");
1379 gzclose(file);
1380 napi_value result = nullptr;
1381 napi_create_int32(env, SUCCESS, &result);
1382 return result;
1383 }
1384
GzFRead(napi_env env, napi_callback_info info)1385 static napi_value GzFRead(napi_env env, napi_callback_info info)
1386 {
1387 int err = Z_OK;
1388 int len = static_cast<int>(strlen(HELLO)) + VALUE_ONE;
1389 gzFile file;
1390 file = gzopen(TESTFILE, "rb");
1391 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1392 err = gzfread(HELLO, len, len, file);
1393 NAPI_ASSERT(env, err == VALUE_FOURTEEN, "gzfread error");
1394 gzclose(file);
1395 napi_value result = nullptr;
1396 napi_create_int32(env, SUCCESS, &result);
1397 return result;
1398 }
1399
GzFlush(napi_env env, napi_callback_info info)1400 static napi_value GzFlush(napi_env env, napi_callback_info info)
1401 {
1402 gzFile file;
1403 file = gzopen(TESTFILE, "wb");
1404 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1405
1406 gzputc(file, 'h');
1407 NAPI_ASSERT(env, gzputs(file, "ello") == VALUE_FOUR, "gzopen error");
1408 if (gzprintf(file, ", %s!", "hello") != VALUE_EIGHT) {
1409 NAPI_ASSERT(env, false, "gzprintf error");
1410 }
1411 gzseek(file, 1L, SEEK_CUR);
1412 gzflush(file, Z_FINISH);
1413 gzclose(file);
1414 napi_value result = nullptr;
1415 napi_create_int32(env, SUCCESS, &result);
1416 return result;
1417 }
1418
GzError(napi_env env, napi_callback_info info)1419 static napi_value GzError(napi_env env, napi_callback_info info)
1420 {
1421 int err = Z_OK;
1422 gzFile file;
1423 file = gzopen(TESTFILE, "wb");
1424 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1425
1426 const char *error = gzerror(file, &err);
1427 NAPI_ASSERT(env, error != nullptr, "gzopen error");
1428 gzclose(file);
1429 napi_value result = nullptr;
1430 napi_create_int32(env, SUCCESS, &result);
1431 return result;
1432 }
1433
GzEof(napi_env env, napi_callback_info info)1434 static napi_value GzEof(napi_env env, napi_callback_info info)
1435 {
1436 gzFile file;
1437 file = gzopen(TESTFILE, "wb");
1438 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1439 int err = gzeof(file);
1440 NAPI_ASSERT(env, err == VALUE_ZERO, "gzdopen error");
1441 gzclose(file);
1442 napi_value result = nullptr;
1443 napi_create_int32(env, SUCCESS, &result);
1444 return result;
1445 }
1446
GzDOpen(napi_env env, napi_callback_info info)1447 static napi_value GzDOpen(napi_env env, napi_callback_info info)
1448 {
1449 FILE *fp = fopen(TESTFILE, "r");
1450 NAPI_ASSERT(env, fp != nullptr, "fopen error");
1451 int fd = fileno(fp);
1452 gzFile file = gzdopen(fd, "r");
1453 NAPI_ASSERT(env, file != nullptr, "gzdopen error");
1454 fclose(fp);
1455 gzclose(file);
1456 napi_value result = nullptr;
1457 napi_create_int32(env, SUCCESS, &result);
1458 return result;
1459 }
1460
GzDirect(napi_env env, napi_callback_info info)1461 static napi_value GzDirect(napi_env env, napi_callback_info info)
1462 {
1463 gzFile file;
1464 file = gzopen(TESTFILE, "wb");
1465 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1466 int res = gzdirect(file);
1467 NAPI_ASSERT(env, res == VALUE_ZERO, "gzdirect error");
1468 gzclose(file);
1469 napi_value result = nullptr;
1470 napi_create_int32(env, SUCCESS, &result);
1471 return result;
1472 }
1473
GzCloseW(napi_env env, napi_callback_info info)1474 static napi_value GzCloseW(napi_env env, napi_callback_info info)
1475 {
1476 gzFile file;
1477 int err = Z_OK;
1478 file = gzopen(TESTFILE, "wb");
1479 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1480 gzclearerr(file);
1481 err = gzclose_w(file);
1482 NAPI_ASSERT(env, err == Z_OK, "gzclose_w error");
1483 napi_value result = nullptr;
1484 napi_create_int32(env, SUCCESS, &result);
1485 return result;
1486 }
1487
GzCloseR(napi_env env, napi_callback_info info)1488 static napi_value GzCloseR(napi_env env, napi_callback_info info)
1489 {
1490 napi_value result = nullptr;
1491 gzFile file;
1492 int err = Z_OK;
1493 file = gzopen(TESTFILE, "rb");
1494 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1495 err = gzclose_r(file);
1496 NAPI_ASSERT(env, err == Z_OK, "gzclose_r error");
1497 napi_create_int32(env, SUCCESS, &result);
1498 return result;
1499 }
1500
GzClearErr(napi_env env, napi_callback_info info)1501 static napi_value GzClearErr(napi_env env, napi_callback_info info)
1502 {
1503 napi_value result = nullptr;
1504 gzFile file;
1505 int err = Z_OK;
1506 file = gzopen(TESTFILE, "wb");
1507 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1508 err = gzbuffer(file, BUFFER_SIZE);
1509 NAPI_ASSERT(env, err == Z_OK, "gzbuffer error");
1510 gzclearerr(file);
1511 err = gzclose_w(file);
1512 NAPI_ASSERT(env, err == Z_OK, "gzclose_w error");
1513 napi_create_int32(env, SUCCESS, &result);
1514 return result;
1515 }
1516
GzClose(napi_env env, napi_callback_info info)1517 static napi_value GzClose(napi_env env, napi_callback_info info)
1518 {
1519 napi_value result = nullptr;
1520 int err = Z_OK;
1521 gzFile file;
1522 file = gzopen(TESTFILE, "wb");
1523 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1524 err = gzclose(file);
1525 NAPI_ASSERT(env, err == Z_OK, "gzopen error");
1526 napi_create_int32(env, SUCCESS, &result);
1527 return result;
1528 }
1529
GzBuffer(napi_env env, napi_callback_info info)1530 static napi_value GzBuffer(napi_env env, napi_callback_info info)
1531 {
1532 napi_value result = nullptr;
1533 int err = Z_OK;
1534 gzFile file;
1535 file = gzopen(TESTFILE, "wb");
1536 NAPI_ASSERT(env, file != nullptr, "gzopen error");
1537
1538 err = gzbuffer(file, BUFFER_SIZE);
1539 NAPI_ASSERT(env, err == Z_OK, "gzopen error");
1540 gzclose(file);
1541 napi_create_int32(env, SUCCESS, &result);
1542 return result;
1543 }
1544
GetCrcTable(napi_env env, napi_callback_info info)1545 static napi_value GetCrcTable(napi_env env, napi_callback_info info)
1546 {
1547 auto table = get_crc_table();
1548 NAPI_ASSERT(env, table != nullptr, "get_crc_table error");
1549 napi_value result = nullptr;
1550 napi_create_int32(env, SUCCESS, &result);
1551 return result;
1552 }
1553
DeflateTune(napi_env env, napi_callback_info info)1554 static napi_value DeflateTune(napi_env env, napi_callback_info info)
1555 {
1556 Byte *comp, *unComp;
1557 uLong comprLen = 100 * sizeof(int);
1558 uLong uncomprLen = comprLen;
1559 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1560 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1561 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1562 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1563
1564 gz_headerp headerp = nullptr;
1565 z_stream c_stream;
1566 int err = Z_OK;
1567 int windowBits = VALUE_EIGHT;
1568 int memLevel = VALUE_EIGHT;
1569 c_stream.zalloc = nullptr;
1570 c_stream.zfree = nullptr;
1571 c_stream.opaque = nullptr;
1572 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1573 static_cast<int>(sizeof(z_stream)));
1574 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1575 deflateSetHeader(&c_stream, headerp);
1576 err = deflateTune(&c_stream, VALUE_ONE, VALUE_FOUR, VALUE_EIGHT, VALUE_ONE);
1577 NAPI_ASSERT(env, err == Z_OK, "deflateTune error");
1578 free(comp);
1579 free(unComp);
1580 napi_value result = nullptr;
1581 napi_create_int32(env, SUCCESS, &result);
1582 return result;
1583 }
1584
DeflateSetHeader(napi_env env, napi_callback_info info)1585 static napi_value DeflateSetHeader(napi_env env, napi_callback_info info)
1586 {
1587 Byte *comp, *unComp;
1588 uLong comprLen = 100 * sizeof(int);
1589 uLong uncomprLen = comprLen;
1590 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1591 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1592 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1593 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1594
1595 gz_headerp headerp = nullptr;
1596 z_stream c_stream;
1597 int err = Z_OK;
1598 int windowBits = VALUE_EIGHT;
1599 int memLevel = VALUE_EIGHT;
1600 c_stream.zalloc = nullptr;
1601 c_stream.zfree = nullptr;
1602 c_stream.opaque = nullptr;
1603 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1604 static_cast<int>(sizeof(z_stream)));
1605 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1606 err = deflateSetHeader(&c_stream, headerp);
1607 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "deflateSetHeader error");
1608 free(comp);
1609 free(unComp);
1610 napi_value result = nullptr;
1611 napi_create_int32(env, SUCCESS, &result);
1612 return result;
1613 }
1614
DeflateSetDictionary(napi_env env, napi_callback_info info)1615 static napi_value DeflateSetDictionary(napi_env env, napi_callback_info info)
1616 {
1617 Byte *comp, *unComp;
1618 uLong comprLen = 100 * sizeof(int);
1619 uLong uncomprLen = comprLen;
1620 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1621 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1622 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1623 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1624
1625 z_stream c_stream;
1626 int err = Z_OK;
1627 c_stream.zalloc = nullptr;
1628 c_stream.zfree = nullptr;
1629 c_stream.opaque = nullptr;
1630 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
1631 NAPI_ASSERT(env, err == Z_OK, "deflateInit error");
1632 err = deflateSetDictionary(&c_stream, reinterpret_cast<const Bytef *>(DICTIONARY),
1633 static_cast<int>(sizeof(DICTIONARY)));
1634 NAPI_ASSERT(env, err == Z_OK, "deflateSetDictionary error");
1635 free(comp);
1636 free(unComp);
1637 napi_value result = nullptr;
1638 napi_create_int32(env, SUCCESS, &result);
1639 return result;
1640 }
1641
DeflateResetKeep(napi_env env, napi_callback_info info)1642 static napi_value DeflateResetKeep(napi_env env, napi_callback_info info)
1643 {
1644 Byte *comp, *unComp;
1645 int *bits = nullptr;
1646 uLong comprLen = 100 * sizeof(int);
1647 uLong uncomprLen = comprLen;
1648 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1649 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1650 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1651 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1652
1653 gz_headerp headerp = nullptr;
1654 z_stream c_stream;
1655 int err = Z_OK;
1656 int windowBits = VALUE_EIGHT;
1657 int memLevel = VALUE_EIGHT;
1658 c_stream.zalloc = nullptr;
1659 c_stream.zfree = nullptr;
1660 c_stream.opaque = nullptr;
1661 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1662 static_cast<int>(sizeof(z_stream)));
1663 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1664 deflateSetHeader(&c_stream, headerp);
1665 deflateTune(&c_stream, VALUE_ONE, VALUE_FOUR, VALUE_EIGHT, VALUE_ONE);
1666 memLevel = VALUE_ONE;
1667 err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
1668 NAPI_ASSERT(env, err == Z_OK, "deflateParams error");
1669 err = deflatePending(&c_stream, nullptr, bits);
1670 NAPI_ASSERT(env, err == Z_OK, "deflatePending error");
1671 err = deflateSetDictionary(&c_stream, reinterpret_cast<const Bytef *>(DICTIONARY),
1672 static_cast<int>(sizeof(DICTIONARY)));
1673 NAPI_ASSERT(env, err == Z_OK, "deflateSetDictionary error");
1674 err = deflateGetDictionary(&c_stream, unComp, nullptr);
1675 err = deflatePrime(&c_stream, VALUE_EIGHT, VALUE_ONE);
1676 c_stream.next_out = comp;
1677 c_stream.avail_out = static_cast<uInt>(comprLen);
1678 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
1679 c_stream.avail_in = static_cast<uInt>(strlen(HELLO)) + VALUE_ONE;
1680 err = deflate(&c_stream, Z_FINISH);
1681 NAPI_ASSERT(env, err == Z_STREAM_END, "deflate error");
1682 err = deflateEnd(&c_stream);
1683 NAPI_ASSERT(env, err == Z_OK, "deflateEnd error");
1684 err = deflateResetKeep(&c_stream);
1685 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "deflateResetKeep error");
1686 free(comp);
1687 free(unComp);
1688 napi_value result = nullptr;
1689 napi_create_int32(env, SUCCESS, &result);
1690 return result;
1691 }
1692
DeflateReset(napi_env env, napi_callback_info info)1693 static napi_value DeflateReset(napi_env env, napi_callback_info info)
1694 {
1695 Byte *comp, *unComp;
1696 int *bits = nullptr;
1697 uLong comprLen = 100 * sizeof(int);
1698 uLong uncomprLen = comprLen;
1699 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1700 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1701 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1702 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1703
1704 gz_headerp headerp = nullptr;
1705 z_stream c_stream;
1706 int err = Z_OK;
1707 int windowBits = VALUE_EIGHT;
1708 int memLevel = VALUE_EIGHT;
1709 c_stream.zalloc = nullptr;
1710 c_stream.zfree = nullptr;
1711 c_stream.opaque = nullptr;
1712 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1713 static_cast<int>(sizeof(z_stream)));
1714 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1715 deflateSetHeader(&c_stream, headerp);
1716 deflateTune(&c_stream, VALUE_ONE, VALUE_FOUR, VALUE_EIGHT, VALUE_ONE);
1717 memLevel = VALUE_ONE;
1718 err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
1719 NAPI_ASSERT(env, err == Z_OK, "deflateParams error");
1720 err = deflatePending(&c_stream, nullptr, bits);
1721 NAPI_ASSERT(env, err == Z_OK, "deflatePending error");
1722 err = deflateSetDictionary(&c_stream, reinterpret_cast<const Bytef *>(DICTIONARY),
1723 static_cast<int>(sizeof(DICTIONARY)));
1724 NAPI_ASSERT(env, err == Z_OK, "deflateSetDictionary error");
1725 err = deflateGetDictionary(&c_stream, unComp, nullptr);
1726 err = deflatePrime(&c_stream, VALUE_EIGHT, VALUE_ONE);
1727 c_stream.next_out = comp;
1728 c_stream.avail_out = static_cast<uInt>(comprLen);
1729 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
1730 c_stream.avail_in = static_cast<uInt>(strlen(HELLO)) + VALUE_ONE;
1731 err = deflate(&c_stream, Z_FINISH);
1732 NAPI_ASSERT(env, err == Z_STREAM_END, "deflate error");
1733 err = deflateEnd(&c_stream);
1734 NAPI_ASSERT(env, err == Z_OK, "deflateEnd error");
1735 err = deflateReset(&c_stream);
1736 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "deflateReset error");
1737 free(comp);
1738 free(unComp);
1739 napi_value result = nullptr;
1740 napi_create_int32(env, SUCCESS, &result);
1741 return result;
1742 }
1743
DeflatePrime(napi_env env, napi_callback_info info)1744 static napi_value DeflatePrime(napi_env env, napi_callback_info info)
1745 {
1746 Byte *comp, *unComp;
1747 int *bits = nullptr;
1748 uLong comprLen = 100 * sizeof(int);
1749 uLong uncomprLen = comprLen;
1750 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1751 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1752 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1753 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1754
1755 gz_headerp headerp = nullptr;
1756 z_stream c_stream;
1757 int err = Z_OK;
1758 int windowBits = VALUE_EIGHT;
1759 int memLevel = VALUE_EIGHT;
1760 c_stream.zalloc = nullptr;
1761 c_stream.zfree = nullptr;
1762 c_stream.opaque = nullptr;
1763 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1764 static_cast<int>(sizeof(z_stream)));
1765 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1766 deflateSetHeader(&c_stream, headerp);
1767 deflateTune(&c_stream, VALUE_ONE, VALUE_FOUR, VALUE_EIGHT, VALUE_ONE);
1768 memLevel = VALUE_ONE;
1769 err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
1770 NAPI_ASSERT(env, err == Z_OK, "deflateParams error");
1771 err = deflatePending(&c_stream, nullptr, bits);
1772 NAPI_ASSERT(env, err == Z_OK, "deflatePending error");
1773 err = deflateSetDictionary(&c_stream, reinterpret_cast<const Bytef *>(DICTIONARY),
1774 static_cast<int>(sizeof(DICTIONARY)));
1775 NAPI_ASSERT(env, err == Z_OK, "deflateSetDictionary error");
1776 err = deflateGetDictionary(&c_stream, unComp, nullptr);
1777 err = deflatePrime(&c_stream, VALUE_EIGHT, VALUE_ONE);
1778 NAPI_ASSERT(env, err == Z_OK, "deflatePrime error");
1779 free(comp);
1780 free(unComp);
1781 napi_value result = nullptr;
1782 napi_create_int32(env, SUCCESS, &result);
1783 return result;
1784 }
1785
DeflatePending(napi_env env, napi_callback_info info)1786 static napi_value DeflatePending(napi_env env, napi_callback_info info)
1787 {
1788 Byte *comp, *unComp;
1789 int *bits = nullptr;
1790 uLong comprLen = 100 * sizeof(int);
1791 uLong uncomprLen = comprLen;
1792 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1793 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1794 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1795 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1796
1797 gz_headerp headerp = nullptr;
1798 z_stream c_stream;
1799 int err = Z_OK;
1800 int windowBits = VALUE_EIGHT;
1801 int memLevel = VALUE_EIGHT;
1802 c_stream.zalloc = nullptr;
1803 c_stream.zfree = nullptr;
1804 c_stream.opaque = nullptr;
1805 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1806 static_cast<int>(sizeof(z_stream)));
1807 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1808 deflateSetHeader(&c_stream, headerp);
1809 deflateTune(&c_stream, VALUE_ONE, VALUE_FOUR, VALUE_EIGHT, VALUE_ONE);
1810 memLevel = VALUE_ONE;
1811 err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
1812 NAPI_ASSERT(env, err == Z_OK, "deflateParams error");
1813 err = deflatePending(&c_stream, nullptr, bits);
1814 NAPI_ASSERT(env, err == Z_OK, "deflatePending error");
1815 free(comp);
1816 free(unComp);
1817 napi_value result = nullptr;
1818 napi_create_int32(env, SUCCESS, &result);
1819 return result;
1820 }
1821
DeflateParams(napi_env env, napi_callback_info info)1822 static napi_value DeflateParams(napi_env env, napi_callback_info info)
1823 {
1824 Byte *comp, *unComp;
1825 uLong comprLen = 100 * sizeof(int);
1826 uLong uncomprLen = comprLen;
1827 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1828 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1829 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1830 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1831
1832 gz_headerp headerp = nullptr;
1833 z_stream c_stream;
1834 int err = Z_OK;
1835 int windowBits = VALUE_EIGHT;
1836 int memLevel = VALUE_EIGHT;
1837 c_stream.zalloc = nullptr;
1838 c_stream.zfree = nullptr;
1839 c_stream.opaque = nullptr;
1840 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1841 static_cast<int>(sizeof(z_stream)));
1842 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1843 deflateSetHeader(&c_stream, headerp);
1844 deflateTune(&c_stream, VALUE_ONE, VALUE_FOUR, VALUE_EIGHT, VALUE_ONE);
1845 memLevel = VALUE_ONE;
1846 err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
1847 NAPI_ASSERT(env, err == Z_OK, "deflateParams error");
1848 free(comp);
1849 free(unComp);
1850 napi_value result = nullptr;
1851 napi_create_int32(env, SUCCESS, &result);
1852 return result;
1853 }
1854
DeflateInit(napi_env env, napi_callback_info info)1855 static napi_value DeflateInit(napi_env env, napi_callback_info info)
1856 {
1857 z_stream defstream;
1858 char *inBuf = reinterpret_cast<char *>(HELLO);
1859 uint32_t inLen = strlen(inBuf) + VALUE_ONE;
1860 uint8_t *outBuf = nullptr;
1861 uint32_t outLen = VALUE_ZERO;
1862 int err = Z_OK;
1863
1864 defstream.zalloc = nullptr;
1865 defstream.zfree = nullptr;
1866 defstream.opaque = nullptr;
1867 defstream.avail_in = static_cast<uInt>(inLen);
1868 defstream.next_in = reinterpret_cast<Bytef *>(inBuf);
1869 defstream.avail_out = static_cast<uInt>(outLen);
1870 defstream.next_out = reinterpret_cast<Bytef *>(outBuf);
1871 err = deflateInit_(&defstream, Z_DEFAULT_COMPRESSION, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
1872 NAPI_ASSERT(env, err == Z_OK, "deflateInit_ error");
1873 napi_value result = nullptr;
1874 napi_create_int32(env, SUCCESS, &result);
1875 return result;
1876 }
1877
DeflateInit2(napi_env env, napi_callback_info info)1878 static napi_value DeflateInit2(napi_env env, napi_callback_info info)
1879 {
1880 z_stream c_stream;
1881 int err = Z_OK;
1882 int windowBits = VALUE_EIGHT;
1883 int memLevel = VALUE_EIGHT;
1884 c_stream.zalloc = nullptr;
1885 c_stream.zfree = nullptr;
1886 c_stream.opaque = nullptr;
1887 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1888 static_cast<int>(sizeof(z_stream)));
1889 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1890 napi_value result = nullptr;
1891 napi_create_int32(env, SUCCESS, &result);
1892 return result;
1893 }
1894
DeflateGetDictionary(napi_env env, napi_callback_info info)1895 static napi_value DeflateGetDictionary(napi_env env, napi_callback_info info)
1896 {
1897 Byte *comp, *unComp;
1898 int *bits = nullptr;
1899 uLong comprLen = 100 * sizeof(int);
1900 uLong uncomprLen = comprLen;
1901 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1902 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1903 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1904 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1905
1906 gz_headerp headerp = nullptr;
1907 z_stream c_stream;
1908 int err = Z_OK;
1909 int windowBits = VALUE_EIGHT;
1910 int memLevel = VALUE_EIGHT;
1911 c_stream.zalloc = nullptr;
1912 c_stream.zfree = nullptr;
1913 c_stream.opaque = nullptr;
1914 err = deflateInit2_(&c_stream, Z_BEST_COMPRESSION, Z_DEFLATED, windowBits, memLevel, Z_FILTERED, ZLIB_VERSION,
1915 static_cast<int>(sizeof(z_stream)));
1916 NAPI_ASSERT(env, err == Z_OK, "deflateInit2_ error");
1917 deflateSetHeader(&c_stream, headerp);
1918 deflateTune(&c_stream, VALUE_ONE, VALUE_FOUR, VALUE_EIGHT, VALUE_ONE);
1919 memLevel = VALUE_ONE;
1920 err = deflateParams(&c_stream, memLevel, Z_DEFAULT_STRATEGY);
1921 NAPI_ASSERT(env, err == Z_OK, "deflateParams error");
1922 err = deflatePending(&c_stream, nullptr, bits);
1923 NAPI_ASSERT(env, err == Z_OK, "deflatePending error");
1924 err = deflateSetDictionary(&c_stream, reinterpret_cast<const Bytef *>(DICTIONARY),
1925 static_cast<int>(sizeof(DICTIONARY)));
1926 NAPI_ASSERT(env, err == Z_OK, "deflateSetDictionary error");
1927 err = deflateGetDictionary(&c_stream, unComp, nullptr);
1928 NAPI_ASSERT(env, err == Z_OK, "deflateGetDictionary error");
1929 free(comp);
1930 free(unComp);
1931 napi_value result = nullptr;
1932 napi_create_int32(env, SUCCESS, &result);
1933 return result;
1934 }
1935
DeflateEnd(napi_env env, napi_callback_info info)1936 static napi_value DeflateEnd(napi_env env, napi_callback_info info)
1937 {
1938 Byte *comp, *unComp;
1939 uLong comprLen = 100 * sizeof(int);
1940 uLong uncomprLen = comprLen;
1941 comp = static_cast<Byte *>(calloc(static_cast<uInt>(comprLen), CALLOC_SIZE));
1942 unComp = static_cast<Byte *>(calloc(static_cast<uInt>(uncomprLen), CALLOC_SIZE));
1943 NAPI_ASSERT(env, comp != Z_NULL, "comp is Z_NULL");
1944 NAPI_ASSERT(env, unComp != Z_NULL, "unComp is Z_NULL");
1945
1946 z_stream c_stream;
1947 int err = Z_OK;
1948 c_stream.zalloc = nullptr;
1949 c_stream.zfree = nullptr;
1950 c_stream.opaque = nullptr;
1951 err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
1952 NAPI_ASSERT(env, err == Z_OK, "deflateInit error");
1953 err = deflateSetDictionary(&c_stream, reinterpret_cast<const Bytef *>(DICTIONARY),
1954 static_cast<int>(sizeof(DICTIONARY)));
1955 NAPI_ASSERT(env, err == Z_OK, "deflateSetDictionary error");
1956 c_stream.next_out = comp;
1957 c_stream.avail_out = static_cast<uInt>(comprLen);
1958 c_stream.next_in = reinterpret_cast<z_const unsigned char *>(HELLO);
1959 c_stream.avail_in = static_cast<uInt>(strlen(HELLO)) + VALUE_ONE;
1960 err = deflate(&c_stream, Z_FINISH);
1961 NAPI_ASSERT(env, err == Z_STREAM_END, "deflate error");
1962 err = deflateEnd(&c_stream);
1963 NAPI_ASSERT(env, err == Z_OK, "deflateEnd error");
1964 free(comp);
1965 free(unComp);
1966 napi_value result = nullptr;
1967 napi_create_int32(env, SUCCESS, &result);
1968 return result;
1969 }
1970
DeflateCopy(napi_env env, napi_callback_info info)1971 static napi_value DeflateCopy(napi_env env, napi_callback_info info)
1972 {
1973 z_stream defstream;
1974 char *inBuf = reinterpret_cast<char *>(HELLO);
1975 uint32_t inLen = strlen(inBuf) + VALUE_ONE;
1976 uint8_t *outBuf = nullptr;
1977 uint32_t outLen = VALUE_ZERO;
1978 int err = Z_OK;
1979
1980 defstream.zalloc = nullptr;
1981 defstream.zfree = nullptr;
1982 defstream.opaque = nullptr;
1983 defstream.avail_in = static_cast<uInt>(inLen);
1984 defstream.next_in = reinterpret_cast<Bytef *>(inBuf);
1985 defstream.avail_out = static_cast<uInt>(outLen);
1986 defstream.next_out = reinterpret_cast<Bytef *>(outBuf);
1987 err = deflateInit_(&defstream, Z_DEFAULT_COMPRESSION, ZLIB_VERSION, static_cast<int>(sizeof(z_stream)));
1988 NAPI_ASSERT(env, err == Z_OK, "deflateInit_ error");
1989 uint32_t estimateLen = deflateBound(&defstream, inLen);
1990 outBuf = reinterpret_cast<uint8_t *>(malloc(estimateLen));
1991 defstream.avail_out = static_cast<uInt>(estimateLen);
1992 deflate(&defstream, Z_FINISH);
1993 deflateEnd(&defstream);
1994 z_stream outStream;
1995 err = deflateCopy(&defstream, &outStream);
1996 NAPI_ASSERT(env, err == Z_STREAM_ERROR, "deflateCopy error");
1997 free(outBuf);
1998 napi_value result = nullptr;
1999 napi_create_int32(env, SUCCESS, &result);
2000 return result;
2001 }
2002
Crc32Combine64(napi_env env, napi_callback_info info)2003 static napi_value Crc32Combine64(napi_env env, napi_callback_info info)
2004 {
2005 uLong err;
2006 err = Z_ERRNO;
2007 uLong adler1;
2008 adler1 = 0L;
2009 uLong adler2;
2010 adler2 = 0L;
2011 #ifdef Z_LARGE64
2012 err = crc32_combine64(crc1, crc2, VALUE_ZERO);
2013 NAPI_ASSERT(env, err == Z_ERRNO, "crc32_combine64 error");
2014 #endif
2015 napi_value result = nullptr;
2016 napi_create_int32(env, SUCCESS, &result);
2017 return result;
2018 }
2019
Adler32Combine64(napi_env env, napi_callback_info info)2020 static napi_value Adler32Combine64(napi_env env, napi_callback_info info)
2021 {
2022 uLong err;
2023 err = Z_ERRNO;
2024 uLong adler1;
2025 adler1 = 0L;
2026 uLong adler2;
2027 adler2 = 0L;
2028 #ifdef Z_LARGE64
2029 err = adler32_combine64(adler1, adler2, VALUE_ZERO);
2030 NAPI_ASSERT(env, err == Z_ERRNO, "adler32_combine64 error");
2031 #endif
2032 napi_value result = nullptr;
2033 napi_create_int32(env, SUCCESS, &result);
2034 return result;
2035 }
2036
Types(napi_env env, napi_callback_info info)2037 static napi_value Types(napi_env env, napi_callback_info info)
2038 {
2039 #ifdef Z_PREFIX
2040 deflate_state state;
2041 _tr_init(&state);
2042 NAPI_ASSERT(env, state != nullptr, "_tr_init error");
2043
2044 _dist_code distCode = VALUE_SIX;
2045 NAPI_ASSERT(env, distCode == VALUE_SIX, "_dist_code error");
2046
2047 _length_code engthCode = VALUE_FOUR;
2048 NAPI_ASSERT(env, engthCode == VALUE_FOUR, "_tr_align error");
2049
2050 _tr_align trAlign;
2051 NAPI_ASSERT(env, sizeof(trAlign) >= VALUE_ZERO, "_tr_align error");
2052
2053 _tr_flush_bits bits;
2054 NAPI_ASSERT(env, sizeof(bits) >= VALUE_ZERO, "_tr_flush_bits error");
2055
2056 _tr_flush_block flushBlock;
2057 NAPI_ASSERT(env, sizeof(flushBlock) >= VALUE_ZERO, "_tr_flush_block error");
2058
2059 _tr_stored_block storedBlock;
2060 NAPI_ASSERT(env, sizeof(storedBlock) >= VALUE_ZERO, "_tr_stored_block error");
2061
2062 _tr_tally tally;
2063 NAPI_ASSERT(env, sizeof(tally) >= VALUE_ZERO, "_tr_tally error");
2064 #endif
2065 napi_value result = nullptr;
2066 napi_create_int32(env, SUCCESS, &result);
2067 return result;
2068 }
2069 EXTERN_C_START
Init(napi_env env, napi_value exports)2070 static napi_value Init(napi_env env, napi_value exports)
2071 {
2072 napi_property_descriptor desc[] = {
2073 {"compress", nullptr, Compress, nullptr, nullptr, nullptr, napi_default, nullptr},
2074 {"compress2", nullptr, Compress2, nullptr, nullptr, nullptr, napi_default, nullptr},
2075 {"compressBound", nullptr, CompressBound, nullptr, nullptr, nullptr, napi_default, nullptr},
2076 {"crc32", nullptr, Crc32, nullptr, nullptr, nullptr, napi_default, nullptr},
2077 {"crc32_z", nullptr, Crc32_z, nullptr, nullptr, nullptr, napi_default, nullptr},
2078 {"crc32Combine", nullptr, Crc32Combine, nullptr, nullptr, nullptr, napi_default, nullptr},
2079 {"adler32", nullptr, Adler32, nullptr, nullptr, nullptr, napi_default, nullptr},
2080 {"adler32_z", nullptr, Adler32_z, nullptr, nullptr, nullptr, napi_default, nullptr},
2081 {"adler32Combine", nullptr, Adler32Combine, nullptr, nullptr, nullptr, napi_default, nullptr},
2082 {"deflate", nullptr, Deflate, nullptr, nullptr, nullptr, napi_default, nullptr},
2083 {"deflateFullFlush", nullptr, DeflateFullFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
2084 {"deflateBound", nullptr, DeflateBound, nullptr, nullptr, nullptr, napi_default, nullptr},
2085 {"unCompress", nullptr, UnCompress, nullptr, nullptr, nullptr, napi_default, nullptr},
2086 {"zLibVersion", nullptr, ZLibVersion, nullptr, nullptr, nullptr, napi_default, nullptr},
2087 {"zLibCompileFlags", nullptr, ZLibCompileFlags, nullptr, nullptr, nullptr, napi_default, nullptr},
2088 {"zError", nullptr, ZError, nullptr, nullptr, nullptr, napi_default, nullptr},
2089 {"unCompress2", nullptr, UnCompress2, nullptr, nullptr, nullptr, napi_default, nullptr},
2090 {"inflateValidate", nullptr, InflateValidate, nullptr, nullptr, nullptr, napi_default, nullptr},
2091 {"inflateUndermine", nullptr, InflateUndermine, nullptr, nullptr, nullptr, napi_default, nullptr},
2092 {"inflateSyncPoint", nullptr, InflateSyncPoint, nullptr, nullptr, nullptr, napi_default, nullptr},
2093 {"inflateSync", nullptr, InflateSync, nullptr, nullptr, nullptr, napi_default, nullptr},
2094 {"inflateSetDictionary", nullptr, InflateSetDictionary, nullptr, nullptr, nullptr, napi_default, nullptr},
2095 {"inflateResetKeep", nullptr, InflateResetKeep, nullptr, nullptr, nullptr, napi_default, nullptr},
2096 {"inflateReset2", nullptr, InflateReset2, nullptr, nullptr, nullptr, napi_default, nullptr},
2097 {"inflateReset", nullptr, InflateReset, nullptr, nullptr, nullptr, napi_default, nullptr},
2098 {"inflateMark", nullptr, InflateMark, nullptr, nullptr, nullptr, napi_default, nullptr},
2099 {"inflateInit2", nullptr, InflateInit2, nullptr, nullptr, nullptr, napi_default, nullptr},
2100 {"inflateInit", nullptr, InflateInit, nullptr, nullptr, nullptr, napi_default, nullptr},
2101 {"inflatePrime", nullptr, InflatePrime, nullptr, nullptr, nullptr, napi_default, nullptr},
2102 {"inflateGetHeader", nullptr, InflateGetHeader, nullptr, nullptr, nullptr, napi_default, nullptr},
2103 {"inflateGetDictionary", nullptr, InflateGetDictionary, nullptr, nullptr, nullptr, napi_default, nullptr},
2104 {"inflateEnd", nullptr, InflateEnd, nullptr, nullptr, nullptr, napi_default, nullptr},
2105 {"inflateCopy", nullptr, InflateCopy, nullptr, nullptr, nullptr, napi_default, nullptr},
2106 {"inflateCodesUsed", nullptr, InflateCodesUsed, nullptr, nullptr, nullptr, napi_default, nullptr},
2107 {"inflateBackInit", nullptr, InflateBackInit, nullptr, nullptr, nullptr, napi_default, nullptr},
2108 {"inflateBack", nullptr, InflateBack, nullptr, nullptr, nullptr, napi_default, nullptr},
2109 {"inflateBackEnd", nullptr, InflateBackEnd, nullptr, nullptr, nullptr, napi_default, nullptr},
2110 {"inflate", nullptr, Inflate, nullptr, nullptr, nullptr, napi_default, nullptr},
2111 {"gzWrite", nullptr, GzWrite, nullptr, nullptr, nullptr, napi_default, nullptr},
2112 {"gzvPrintf", nullptr, GzvPrintf, nullptr, nullptr, nullptr, napi_default, nullptr},
2113 {"gzUnGetC", nullptr, GzUnGetC, nullptr, nullptr, nullptr, napi_default, nullptr},
2114 {"gzTell64", nullptr, GzTell64, nullptr, nullptr, nullptr, napi_default, nullptr},
2115 {"gzTell", nullptr, GzTell, nullptr, nullptr, nullptr, napi_default, nullptr},
2116 {"gzSetParams", nullptr, GzSetParams, nullptr, nullptr, nullptr, napi_default, nullptr},
2117 {"gzSeek64", nullptr, GzSeek64, nullptr, nullptr, nullptr, napi_default, nullptr},
2118 {"gzSeek", nullptr, GzSeek, nullptr, nullptr, nullptr, napi_default, nullptr},
2119 {"gzRewind", nullptr, GzRewind, nullptr, nullptr, nullptr, napi_default, nullptr},
2120 {"gzRead", nullptr, GzRead, nullptr, nullptr, nullptr, napi_default, nullptr},
2121 {"gzPuts", nullptr, GzPuts, nullptr, nullptr, nullptr, napi_default, nullptr},
2122 {"gzPutc", nullptr, GzPutc, nullptr, nullptr, nullptr, napi_default, nullptr},
2123 {"gzPrintf", nullptr, GzPrintf, nullptr, nullptr, nullptr, napi_default, nullptr},
2124 {"gzOpenW", nullptr, GzOpenW, nullptr, nullptr, nullptr, napi_default, nullptr},
2125 {"gzOpen64", nullptr, GzOpen64, nullptr, nullptr, nullptr, napi_default, nullptr},
2126 {"gzOpen", nullptr, GzOpen, nullptr, nullptr, nullptr, napi_default, nullptr},
2127 {"gzOffset64", nullptr, GzOffset64, nullptr, nullptr, nullptr, napi_default, nullptr},
2128 {"gzGets", nullptr, GzGets, nullptr, nullptr, nullptr, napi_default, nullptr},
2129 {"gzGetC_", nullptr, GzGetC_, nullptr, nullptr, nullptr, napi_default, nullptr},
2130 {"gzGetC", nullptr, GzGetC, nullptr, nullptr, nullptr, napi_default, nullptr},
2131 {"gzFWrite", nullptr, GzFWrite, nullptr, nullptr, nullptr, napi_default, nullptr},
2132 {"gzFRead", nullptr, GzFRead, nullptr, nullptr, nullptr, napi_default, nullptr},
2133 {"gzFlush", nullptr, GzFlush, nullptr, nullptr, nullptr, napi_default, nullptr},
2134 {"gzError", nullptr, GzError, nullptr, nullptr, nullptr, napi_default, nullptr},
2135 {"gzEof", nullptr, GzEof, nullptr, nullptr, nullptr, napi_default, nullptr},
2136 {"gzDOpen", nullptr, GzDOpen, nullptr, nullptr, nullptr, napi_default, nullptr},
2137 {"gzDirect", nullptr, GzDirect, nullptr, nullptr, nullptr, napi_default, nullptr},
2138 {"gzCloseW", nullptr, GzCloseW, nullptr, nullptr, nullptr, napi_default, nullptr},
2139 {"gzCloseR", nullptr, GzCloseR, nullptr, nullptr, nullptr, napi_default, nullptr},
2140 {"gzClose", nullptr, GzClose, nullptr, nullptr, nullptr, napi_default, nullptr},
2141 {"gzClearErr", nullptr, GzClearErr, nullptr, nullptr, nullptr, napi_default, nullptr},
2142 {"gzBuffer", nullptr, GzBuffer, nullptr, nullptr, nullptr, napi_default, nullptr},
2143 {"getCrcTable", nullptr, GetCrcTable, nullptr, nullptr, nullptr, napi_default, nullptr},
2144 {"deflateTune", nullptr, DeflateTune, nullptr, nullptr, nullptr, napi_default, nullptr},
2145 {"deflateSetHeader", nullptr, DeflateSetHeader, nullptr, nullptr, nullptr, napi_default, nullptr},
2146 {"deflateSetDictionary", nullptr, DeflateSetDictionary, nullptr, nullptr, nullptr, napi_default, nullptr},
2147 {"deflateResetKeep", nullptr, DeflateResetKeep, nullptr, nullptr, nullptr, napi_default, nullptr},
2148 {"deflateReset", nullptr, DeflateReset, nullptr, nullptr, nullptr, napi_default, nullptr},
2149 {"deflatePrime", nullptr, DeflatePrime, nullptr, nullptr, nullptr, napi_default, nullptr},
2150 {"deflatePending", nullptr, DeflatePending, nullptr, nullptr, nullptr, napi_default, nullptr},
2151 {"deflateParams", nullptr, DeflateParams, nullptr, nullptr, nullptr, napi_default, nullptr},
2152 {"deflateInit", nullptr, DeflateInit, nullptr, nullptr, nullptr, napi_default, nullptr},
2153 {"deflateInit2", nullptr, DeflateInit2, nullptr, nullptr, nullptr, napi_default, nullptr},
2154 {"deflateGetDictionary", nullptr, DeflateGetDictionary, nullptr, nullptr, nullptr, napi_default, nullptr},
2155 {"deflateEnd", nullptr, DeflateEnd, nullptr, nullptr, nullptr, napi_default, nullptr},
2156 {"deflateCopy", nullptr, DeflateCopy, nullptr, nullptr, nullptr, napi_default, nullptr},
2157 {"crc32Combine64", nullptr, Crc32Combine64, nullptr, nullptr, nullptr, napi_default, nullptr},
2158 {"adler32Combine64", nullptr, Adler32Combine64, nullptr, nullptr, nullptr, napi_default, nullptr},
2159 {"distCode", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2160 {"lengthCode", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2161 {"trAlign", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2162 {"trFlushBits", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2163 {"trFlushBlock", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2164 {"trInit", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2165 {"trStoredBlock", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2166 {"trTally", nullptr, Types, nullptr, nullptr, nullptr, napi_default, nullptr},
2167 };
2168 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
2169 return exports;
2170 }
2171 EXTERN_C_END
2172
2173 static napi_module demoModule = {
2174 .nm_version = 1,
2175 .nm_flags = 0,
2176 .nm_filename = nullptr,
2177 .nm_register_func = Init,
2178 .nm_modname = "zlibndk",
2179 .nm_priv = ((void *)0),
2180 .reserved = {0},
2181 };
2182
RegisterEntryModule(void)2183 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
2184