Skip to content

Commit 6b4dac3

Browse files
joyeecheungtargos
authored andcommitted
zlib: expose zlib.crc32()
This patch exposes the crc32() function from zlib to user-land. It computes a 32-bit Cyclic Redundancy Check checksum of `data`. If `value` is specified, it is used as the starting value of the checksum, otherwise, 0 is used as the starting value. ```js const zlib = require('node:zlib'); const { Buffer } = require('node:buffer'); let crc = zlib.crc32('hello'); // 907060870 crc = zlib.crc32('world', crc); // 4192936109 crc = zlib.crc32(Buffer.from('hello')); // 907060870 crc = zlib.crc32(Buffer.from('world'), crc); // 4192936109 ``` PR-URL: #52692 Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent a0147ff commit 6b4dac3

File tree

4 files changed

+313
-0
lines changed

4 files changed

+313
-0
lines changed

doc/api/zlib.md

+62
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,67 @@ The `zlib.bytesWritten` property specifies the number of bytes written to
712712
the engine, before the bytes are processed (compressed or decompressed,
713713
as appropriate for the derived class).
714714

715+
### `zlib.crc32(data[, value])`
716+
717+
<!-- YAML
718+
added: REPLACEME
719+
-->
720+
721+
* `data` {string|Buffer|TypedArray|DataView} When `data` is a string,
722+
it will be encoded as UTF-8 before being used for computation.
723+
* `value` {integer} An optional starting value. It must be a 32-bit unsigned
724+
integer. **Default:** `0`
725+
* Returns: {integer} A 32-bit unsigned integer containing the checksum.
726+
727+
Computes a 32-bit [Cyclic Redundancy Check][] checksum of `data`. If
728+
`value` is specified, it is used as the starting value of the checksum,
729+
otherwise, 0 is used as the starting value.
730+
731+
The CRC algorithm is designed to compute checksums and to detect error
732+
in data transmission. It's not suitable for cryptographic authentication.
733+
734+
To be consistent with other APIs, if the `data` is a string, it will
735+
be encoded with UTF-8 before being used for computation. If users only
736+
use Node.js to compute and match the checksums, this works well with
737+
other APIs that uses the UTF-8 encoding by default.
738+
739+
Some third-party JavaScript libraries compute the checksum on a
740+
string based on `str.charCodeAt()` so that it can be run in browsers.
741+
If users want to match the checksum computed with this kind of library
742+
in the browser, it's better to use the same library in Node.js
743+
if it also runs in Node.js. If users have to use `zlib.crc32()` to
744+
match the checksum produced by such a third-party library:
745+
746+
1. If the library accepts `Uint8Array` as input, use `TextEncoder`
747+
in the browser to encode the string into a `Uint8Array` with UTF-8
748+
encoding, and compute the checksum based on the UTF-8 encoded string
749+
in the browser.
750+
2. If the library only takes a string and compute the data based on
751+
`str.charCodeAt()`, on the Node.js side, convert the string into
752+
a buffer using `Buffer.from(str, 'utf16le')`.
753+
754+
```mjs
755+
import zlib from 'node:zlib';
756+
import { Buffer } from 'node:buffer';
757+
758+
let crc = zlib.crc32('hello'); // 907060870
759+
crc = zlib.crc32('world', crc); // 4192936109
760+
761+
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
762+
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955
763+
```
764+
765+
```cjs
766+
const zlib = require('node:zlib');
767+
const { Buffer } = require('node:buffer');
768+
769+
let crc = zlib.crc32('hello'); // 907060870
770+
crc = zlib.crc32('world', crc); // 4192936109
771+
772+
crc = zlib.crc32(Buffer.from('hello', 'utf16le')); // 1427272415
773+
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc); // 4150509955
774+
```
775+
715776
### `zlib.close([callback])`
716777

717778
<!-- YAML
@@ -1221,6 +1282,7 @@ changes:
12211282
Decompress a chunk of data with [`Unzip`][].
12221283

12231284
[Brotli parameters]: #brotli-constants
1285+
[Cyclic redundancy check]: https://en.wikipedia.org/wiki/Cyclic_redundancy_check
12241286
[Memory usage tuning]: #memory-usage-tuning
12251287
[RFC 7932]: https://www.rfc-editor.org/rfc/rfc7932.txt
12261288
[Streams API]: stream.md

lib/zlib.js

+11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const {
6363
isUint8Array,
6464
} = require('internal/util/types');
6565
const binding = internalBinding('zlib');
66+
const { crc32: crc32Native } = binding;
6667
const assert = require('internal/assert');
6768
const {
6869
Buffer,
@@ -72,6 +73,7 @@ const { owner_symbol } = require('internal/async_hooks').symbols;
7273
const {
7374
validateFunction,
7475
validateNumber,
76+
validateUint32,
7577
} = require('internal/validators');
7678

7779
const kFlushFlag = Symbol('kFlushFlag');
@@ -899,6 +901,14 @@ function createProperty(ctor) {
899901
};
900902
}
901903

904+
function crc32(data, value = 0) {
905+
if (typeof data !== 'string' && !isArrayBufferView(data)) {
906+
throw new ERR_INVALID_ARG_TYPE('data', ['Buffer', 'TypedArray', 'DataView', 'string'], data);
907+
}
908+
validateUint32(value, 'value');
909+
return crc32Native(data, value);
910+
}
911+
902912
// Legacy alias on the C++ wrapper object. This is not public API, so we may
903913
// want to runtime-deprecate it at some point. There's no hurry, though.
904914
ObjectDefineProperty(binding.Zlib.prototype, 'jsref', {
@@ -908,6 +918,7 @@ ObjectDefineProperty(binding.Zlib.prototype, 'jsref', {
908918
});
909919

910920
module.exports = {
921+
crc32,
911922
Deflate,
912923
Inflate,
913924
Gzip,

src/node_zlib.cc

+29
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,33 @@ struct MakeClass {
12861286
}
12871287
};
12881288

1289+
template <typename T, typename F>
1290+
T CallOnSequence(v8::Isolate* isolate, Local<Value> value, F callback) {
1291+
if (value->IsString()) {
1292+
Utf8Value data(isolate, value);
1293+
return callback(data.out(), data.length());
1294+
} else {
1295+
ArrayBufferViewContents<char> data(value);
1296+
return callback(data.data(), data.length());
1297+
}
1298+
}
1299+
1300+
// TODO(joyeecheung): use fast API
1301+
static void CRC32(const FunctionCallbackInfo<Value>& args) {
1302+
CHECK(args[0]->IsArrayBufferView() || args[0]->IsString());
1303+
CHECK(args[1]->IsUint32());
1304+
uint32_t value = args[1].As<v8::Uint32>()->Value();
1305+
1306+
uint32_t result = CallOnSequence<uint32_t>(
1307+
args.GetIsolate(),
1308+
args[0],
1309+
[&](const char* data, size_t size) -> uint32_t {
1310+
return crc32(value, reinterpret_cast<const Bytef*>(data), size);
1311+
});
1312+
1313+
args.GetReturnValue().Set(result);
1314+
}
1315+
12891316
void Initialize(Local<Object> target,
12901317
Local<Value> unused,
12911318
Local<Context> context,
@@ -1296,6 +1323,7 @@ void Initialize(Local<Object> target,
12961323
MakeClass<BrotliEncoderStream>::Make(env, target, "BrotliEncoder");
12971324
MakeClass<BrotliDecoderStream>::Make(env, target, "BrotliDecoder");
12981325

1326+
SetMethod(context, target, "crc32", CRC32);
12991327
target->Set(env->context(),
13001328
FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"),
13011329
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION)).Check();
@@ -1305,6 +1333,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
13051333
MakeClass<ZlibStream>::Make(registry);
13061334
MakeClass<BrotliEncoderStream>::Make(registry);
13071335
MakeClass<BrotliDecoderStream>::Make(registry);
1336+
registry->Register(CRC32);
13081337
}
13091338

13101339
} // anonymous namespace

test/parallel/test-zlib-crc32.js

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
'use strict';
2+
3+
require('../common');
4+
const zlib = require('zlib');
5+
const assert = require('assert');
6+
const { Buffer } = require('buffer');
7+
8+
// The following test data comes from
9+
// https://github.com/zlib-ng/zlib-ng/blob/5401b24/test/test_crc32.cc
10+
// test_crc32.cc -- crc32 unit test
11+
// Copyright (C) 2019-2021 IBM Corporation
12+
// Authors: Rogerio Alves <rogealve@br.ibm.com>
13+
// Matheus Castanho <msc@linux.ibm.com>
14+
// For conditions of distribution and use, see copyright notice in zlib.h
15+
//
16+
const tests = [
17+
[0x0, 0x0, 0, 0x0],
18+
[0xffffffff, 0x0, 0, 0x0],
19+
[0x0, 0x0, 255, 0x0], /* BZ 174799. */
20+
[0x0, 0x0, 256, 0x0],
21+
[0x0, 0x0, 257, 0x0],
22+
[0x0, 0x0, 32767, 0x0],
23+
[0x0, 0x0, 32768, 0x0],
24+
[0x0, 0x0, 32769, 0x0],
25+
[0x0, '', 0, 0x0],
26+
[0xffffffff, '', 0, 0xffffffff],
27+
[0x0, 'abacus', 6, 0xc3d7115b],
28+
[0x0, 'backlog', 7, 0x269205],
29+
[0x0, 'campfire', 8, 0x22a515f8],
30+
[0x0, 'delta', 5, 0x9643fed9],
31+
[0x0, 'executable', 10, 0xd68eda01],
32+
[0x0, 'file', 4, 0x8c9f3610],
33+
[0x0, 'greatest', 8, 0xc1abd6cd],
34+
[0x0, 'hello', 5, 0x3610a686],
35+
[0x0, 'inverter', 8, 0xc9e962c9],
36+
[0x0, 'jigsaw', 6, 0xce4e3f69],
37+
[0x0, 'karate', 6, 0x890be0e2],
38+
[0x0, 'landscape', 9, 0xc4e0330b],
39+
[0x0, 'machine', 7, 0x1505df84],
40+
[0x0, 'nanometer', 9, 0xd4e19f39],
41+
[0x0, 'oblivion', 8, 0xdae9de77],
42+
[0x0, 'panama', 6, 0x66b8979c],
43+
[0x0, 'quest', 5, 0x4317f817],
44+
[0x0, 'resource', 8, 0xbc91f416],
45+
[0x0, 'secret', 6, 0x5ca2e8e5],
46+
[0x0, 'test', 4, 0xd87f7e0c],
47+
[0x0, 'ultimate', 8, 0x3fc79b0b],
48+
[0x0, 'vector', 6, 0x1b6e485b],
49+
[0x0, 'walrus', 6, 0xbe769b97],
50+
[0x0, 'xeno', 4, 0xe7a06444],
51+
[0x0, 'yelling', 7, 0xfe3944e5],
52+
[0x0, 'zlib', 4, 0x73887d3a],
53+
[0x0, '4BJD7PocN1VqX0jXVpWB', 20, 0xd487a5a1],
54+
[0x0, 'F1rPWI7XvDs6nAIRx41l', 20, 0x61a0132e],
55+
[0x0, 'ldhKlsVkPFOveXgkGtC2', 20, 0xdf02f76],
56+
[0x0, '5KKnGOOrs8BvJ35iKTOS', 20, 0x579b2b0a],
57+
[0x0, '0l1tw7GOcem06Ddu7yn4', 20, 0xf7d16e2d],
58+
[0x0, 'MCr47CjPIn9R1IvE1Tm5', 20, 0x731788f5],
59+
[0x0, 'UcixbzPKTIv0SvILHVdO', 20, 0x7112bb11],
60+
[0x0, 'dGnAyAhRQDsWw0ESou24', 20, 0xf32a0dac],
61+
[0x0, 'di0nvmY9UYMYDh0r45XT', 20, 0x625437bb],
62+
[0x0, '2XKDwHfAhFsV0RhbqtvH', 20, 0x896930f9],
63+
[0x0, 'ZhrANFIiIvRnqClIVyeD', 20, 0x8579a37],
64+
[0x0, 'v7Q9ehzioTOVeDIZioT1', 20, 0x632aa8e0],
65+
[0x0, 'Yod5hEeKcYqyhfXbhxj2', 20, 0xc829af29],
66+
[0x0, 'GehSWY2ay4uUKhehXYb0', 20, 0x1b08b7e8],
67+
[0x0, 'kwytJmq6UqpflV8Y8GoE', 20, 0x4e33b192],
68+
[0x0, '70684206568419061514', 20, 0x59a179f0],
69+
[0x0, '42015093765128581010', 20, 0xcd1013d7],
70+
[0x0, '88214814356148806939', 20, 0xab927546],
71+
[0x0, '43472694284527343838', 20, 0x11f3b20c],
72+
[0x0, '49769333513942933689', 20, 0xd562d4ca],
73+
[0x0, '54979784887993251199', 20, 0x233395f7],
74+
[0x0, '58360544869206793220', 20, 0x2d167fd5],
75+
[0x0, '27347953487840714234', 20, 0x8b5108ba],
76+
[0x0, '07650690295365319082', 20, 0xc46b3cd8],
77+
[0x0, '42655507906821911703', 20, 0xc10b2662],
78+
[0x0, '29977409200786225655', 20, 0xc9a0f9d2],
79+
[0x0, '85181542907229116674', 20, 0x9341357b],
80+
[0x0, '87963594337989416799', 20, 0xf0424937],
81+
[0x0, '21395988329504168551', 20, 0xd7c4c31f],
82+
[0x0, '51991013580943379423', 20, 0xf11edcc4],
83+
[0x0, '*]+@!);({_$;}[_},?{?;(_?,=-][@', 30, 0x40795df4],
84+
[0x0, '_@:_).&(#.[:[{[:)$++-($_;@[)}+', 30, 0xdd61a631],
85+
[0x0, '&[!,[$_==}+.]@!;*(+},[;:)$;)-@', 30, 0xca907a99],
86+
[0x0, ']{.[.+?+[[=;[?}_#&;[=)__$$:+=_', 30, 0xf652deac],
87+
[0x0, '-%.)=/[@].:.(:,()$;=%@-$?]{%+%', 30, 0xaf39a5a9],
88+
[0x0, '+]#$(@&.=:,*];/.!]%/{:){:@(;)$', 30, 0x6bebb4cf],
89+
// eslint-disable-next-line no-template-curly-in-string
90+
[0x0, ')-._.:?[&:.=+}(*$/=!.${;(=$@!}', 30, 0x76430bac],
91+
[0x0, ':(_*&%/[[}+,?#$&*+#[([*-/#;%(]', 30, 0x6c80c388],
92+
[0x0, '{[#-;:$/{)(+[}#]/{&!%(@)%:@-$:', 30, 0xd54d977d],
93+
[0x0, '_{$*,}(&,@.)):=!/%(&(,,-?$}}}!', 30, 0xe3966ad5],
94+
[0x0,
95+
'e$98KNzqaV)Y:2X?]77].{gKRD4G5{mHZk,Z)SpU%L3FSgv!Wb8MLAFdi{+fp)c,@8m6v)yXg@]HBDFk?.4&}g5_udE*JHCiH=aL',
96+
100, 0xe7c71db9],
97+
[0x0,
98+
'r*Fd}ef+5RJQ;+W=4jTR9)R*p!B;]Ed7tkrLi;88U7g@3v!5pk2X6D)vt,.@N8c]@yyEcKi[vwUu@.Ppm@C6%Mv*3Nw}Y,58_aH)',
99+
100, 0xeaa52777],
100+
[0x0,
101+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&',
102+
100, 0xcd472048],
103+
[0x7a30360d, 'abacus', 6, 0xf8655a84],
104+
[0x6fd767ee, 'backlog', 7, 0x1ed834b1],
105+
[0xefeb7589, 'campfire', 8, 0x686cfca],
106+
[0x61cf7e6b, 'delta', 5, 0x1554e4b1],
107+
[0xdc712e2, 'executable', 10, 0x761b4254],
108+
[0xad23c7fd, 'file', 4, 0x7abdd09b],
109+
[0x85cb2317, 'greatest', 8, 0x4ba91c6b],
110+
[0x9eed31b0, 'inverter', 8, 0xd5e78ba5],
111+
[0xb94f34ca, 'jigsaw', 6, 0x23649109],
112+
[0xab058a2, 'karate', 6, 0xc5591f41],
113+
[0x5bff2b7a, 'landscape', 9, 0xf10eb644],
114+
[0x605c9a5f, 'machine', 7, 0xbaa0a636],
115+
[0x51bdeea5, 'nanometer', 9, 0x6af89afb],
116+
[0x85c21c79, 'oblivion', 8, 0xecae222b],
117+
[0x97216f56, 'panama', 6, 0x47dffac4],
118+
[0x18444af2, 'quest', 5, 0x70c2fe36],
119+
[0xbe6ce359, 'resource', 8, 0x1471d925],
120+
[0x843071f1, 'secret', 6, 0x50c9a0db],
121+
[0xf2480c60, 'ultimate', 8, 0xf973daf8],
122+
[0x2d2feb3d, 'vector', 6, 0x344ac03d],
123+
[0x7490310a, 'walrus', 6, 0x6d1408ef],
124+
[0x97d247d4, 'xeno', 4, 0xe62670b5],
125+
[0x93cf7599, 'yelling', 7, 0x1b36da38],
126+
[0x73c84278, 'zlib', 4, 0x6432d127],
127+
[0x228a87d1, '4BJD7PocN1VqX0jXVpWB', 20, 0x997107d0],
128+
[0xa7a048d0, 'F1rPWI7XvDs6nAIRx41l', 20, 0xdc567274],
129+
[0x1f0ded40, 'ldhKlsVkPFOveXgkGtC2', 20, 0xdcc63870],
130+
[0xa804a62f, '5KKnGOOrs8BvJ35iKTOS', 20, 0x6926cffd],
131+
[0x508fae6a, '0l1tw7GOcem06Ddu7yn4', 20, 0xb52b38bc],
132+
[0xe5adaf4f, 'MCr47CjPIn9R1IvE1Tm5', 20, 0xf83b8178],
133+
[0x67136a40, 'UcixbzPKTIv0SvILHVdO', 20, 0xc5213070],
134+
[0xb00c4a10, 'dGnAyAhRQDsWw0ESou24', 20, 0xbc7648b0],
135+
[0x2e0c84b5, 'di0nvmY9UYMYDh0r45XT', 20, 0xd8123a72],
136+
[0x81238d44, '2XKDwHfAhFsV0RhbqtvH', 20, 0xd5ac5620],
137+
[0xf853aa92, 'ZhrANFIiIvRnqClIVyeD', 20, 0xceae099d],
138+
[0x5a692325, 'v7Q9ehzioTOVeDIZioT1', 20, 0xb07d2b24],
139+
[0x3275b9f, 'Yod5hEeKcYqyhfXbhxj2', 20, 0x24ce91df],
140+
[0x38371feb, 'GehSWY2ay4uUKhehXYb0', 20, 0x707b3b30],
141+
[0xafc8bf62, 'kwytJmq6UqpflV8Y8GoE', 20, 0x16abc6a9],
142+
[0x9b07db73, '70684206568419061514', 20, 0xae1fb7b7],
143+
[0xe75b214, '42015093765128581010', 20, 0xd4eecd2d],
144+
[0x72d0fe6f, '88214814356148806939', 20, 0x4660ec7],
145+
[0xf857a4b1, '43472694284527343838', 20, 0xfd8afdf7],
146+
[0x54b8e14, '49769333513942933689', 20, 0xc6d1b5f2],
147+
[0xd6aa5616, '54979784887993251199', 20, 0x32476461],
148+
[0x11e63098, '58360544869206793220', 20, 0xd917cf1a],
149+
[0xbe92385, '27347953487840714234', 20, 0x4ad14a12],
150+
[0x49511de0, '07650690295365319082', 20, 0xe37b5c6c],
151+
[0x3db13bc1, '42655507906821911703', 20, 0x7cc497f1],
152+
[0xbb899bea, '29977409200786225655', 20, 0x99781bb2],
153+
[0xf6cd9436, '85181542907229116674', 20, 0x132256a1],
154+
[0x9109e6c3, '87963594337989416799', 20, 0xbfdb2c83],
155+
[0x75770fc, '21395988329504168551', 20, 0x8d9d1e81],
156+
[0x69b1d19b, '51991013580943379423', 20, 0x7b6d4404],
157+
[0xc6132975, '*]+@!);({_$;}[_},?{?;(_?,=-][@', 30, 0x8619f010],
158+
[0xd58cb00c, '_@:_).&(#.[:[{[:)$++-($_;@[)}+', 30, 0x15746ac3],
159+
[0xb63b8caa, '&[!,[$_==}+.]@!;*(+},[;:)$;)-@', 30, 0xaccf812f],
160+
[0x8a45a2b8, ']{.[.+?+[[=;[?}_#&;[=)__$$:+=_', 30, 0x78af45de],
161+
[0xcbe95b78, '-%.)=/[@].:.(:,()$;=%@-$?]{%+%', 30, 0x25b06b59],
162+
[0x4ef8a54b, '+]#$(@&.=:,*];/.!]%/{:){:@(;)$', 30, 0x4ba0d08f],
163+
// eslint-disable-next-line no-template-curly-in-string
164+
[0x76ad267a, ')-._.:?[&:.=+}(*$/=!.${;(=$@!}', 30, 0xe26b6aac],
165+
[0x569e613c, ':(_*&%/[[}+,?#$&*+#[([*-/#;%(]', 30, 0x7e2b0a66],
166+
[0x36aa61da, '{[#-;:$/{)(+[}#]/{&!%(@)%:@-$:', 30, 0xb3430dc7],
167+
[0xf67222df, '_{$*,}(&,@.)):=!/%(&(,,-?$}}}!', 30, 0x626c17a],
168+
[0x74b34fd3,
169+
'e$98KNzqaV)Y:2X?]77].{gKRD4G5{mHZk,Z)SpU%L3FSgv!Wb8MLAFdi{+fp)c,@8m6v)yXg@]HBDFk?.4&}g5_udE*JHCiH=aL',
170+
100, 0xccf98060],
171+
[0x351fd770,
172+
'r*Fd}ef+5RJQ;+W=4jTR9)R*p!B;]Ed7tkrLi;88U7g@3v!5pk2X6D)vt,.@N8c]@yyEcKi[vwUu@.Ppm@C6%Mv*3Nw}Y,58_aH)',
173+
100, 0xd8b95312],
174+
[0xc45aef77,
175+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&',
176+
100, 0xbb1c9912],
177+
[0xc45aef77,
178+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&' +
179+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&' +
180+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&' +
181+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&' +
182+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&' +
183+
'h{bcmdC+a;t+Cf{6Y_dFq-{X4Yu&7uNfVDh?q&_u.UWJU],-GiH7ADzb7-V.Q%4=+v!$L9W+T=bP]$_:]Vyg}A.ygD.r;h-D]m%&',
184+
600, 0x888AFA5B],
185+
];
186+
187+
for (const [ crc, data, len, expected ] of tests) {
188+
if (data === 0) {
189+
continue;
190+
}
191+
const buf = Buffer.from(data, 'utf8');
192+
assert.strictEqual(buf.length, len);
193+
assert.strictEqual(zlib.crc32(buf, crc), expected,
194+
`crc32('${data}', ${crc}) in buffer is not ${expected}`);
195+
assert.strictEqual(zlib.crc32(buf.toString(), crc), expected,
196+
`crc32('${data}', ${crc}) in string is not ${expected}`);
197+
if (crc === 0) {
198+
assert.strictEqual(zlib.crc32(buf), expected,
199+
`crc32('${data}') in buffer is not ${expected}`);
200+
assert.strictEqual(zlib.crc32(buf.toString()), expected,
201+
`crc32('${data}') in string is not ${expected}`);
202+
}
203+
}
204+
205+
[undefined, null, true, 1, () => {}, {}].forEach((invalid) => {
206+
assert.throws(() => { zlib.crc32(invalid); }, { code: 'ERR_INVALID_ARG_TYPE' });
207+
});
208+
209+
[null, true, () => {}, {}].forEach((invalid) => {
210+
assert.throws(() => { zlib.crc32('test', invalid); }, { code: 'ERR_INVALID_ARG_TYPE' });
211+
});

0 commit comments

Comments
 (0)