Skip to content

Commit b2b9ece

Browse files
committed
zlib: expose zlib.crc32()
1 parent 7b2e3b8 commit b2b9ece

File tree

4 files changed

+267
-0
lines changed

4 files changed

+267
-0
lines changed

doc/api/zlib.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,22 @@ 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+
715731
### `zlib.close([callback])`
716732

717733
<!-- YAML
@@ -1221,6 +1237,7 @@ changes:
12211237
Decompress a chunk of data with [`Unzip`][].
12221238

12231239
[Brotli parameters]: #brotli-constants
1240+
[Cyclic redundancy check]: https://en.wikipedia.org/wiki/Cyclic_redundancy_check
12241241
[Memory usage tuning]: #memory-usage-tuning
12251242
[RFC 7932]: https://www.rfc-editor.org/rfc/rfc7932.txt
12261243
[Streams API]: stream.md

lib/zlib.js

Lines changed: 11 additions & 0 deletions
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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,35 @@ struct MakeClass {
12861286
}
12871287
};
12881288

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

1328+
SetMethod(context, target, "crc32", CRC32);
12991329
target->Set(env->context(),
13001330
FIXED_ONE_BYTE_STRING(env->isolate(), "ZLIB_VERSION"),
13011331
FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION)).Check();

test/parallel/test-zlib-crc32.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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 <[email protected]>
13+
// Matheus Castanho <[email protected]>
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+
[0x0, ')-._.:?[&:.=+}(*$/=!.${;(=$@!}', 30, 0x76430bac],
90+
[0x0, ':(_*&%/[[}+,?#$&*+#[([*-/#;%(]', 30, 0x6c80c388],
91+
[0x0, '{[#-;:$/{)(+[}#]/{&!%(@)%:@-$:', 30, 0xd54d977d],
92+
[0x0, '_{$*,}(&,@.)):=!/%(&(,,-?$}}}!', 30, 0xe3966ad5],
93+
[0x0,
94+
'e$98KNzqaV)Y:2X?]77].{gKRD4G5{mHZk,Z)SpU%L3FSgv!Wb8MLAFdi{+fp)c,@8m6v)yXg@]HBDFk?.4&}g5_udE*JHCiH=aL',
95+
100, 0xe7c71db9],
96+
[0x0,
97+
'r*Fd}ef+5RJQ;+W=4jTR9)R*p!B;]Ed7tkrLi;88U7g@3v!5pk2X6D)vt,.@N8c]@yyEcKi[[email protected]@C6%Mv*3Nw}Y,58_aH)',
98+
100, 0xeaa52777],
99+
[0x0,
100+
'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%&',
101+
100, 0xcd472048],
102+
[0x7a30360d, 'abacus', 6, 0xf8655a84],
103+
[0x6fd767ee, 'backlog', 7, 0x1ed834b1],
104+
[0xefeb7589, 'campfire', 8, 0x686cfca],
105+
[0x61cf7e6b, 'delta', 5, 0x1554e4b1],
106+
[0xdc712e2, 'executable', 10, 0x761b4254],
107+
[0xad23c7fd, 'file', 4, 0x7abdd09b],
108+
[0x85cb2317, 'greatest', 8, 0x4ba91c6b],
109+
[0x9eed31b0, 'inverter', 8, 0xd5e78ba5],
110+
[0xb94f34ca, 'jigsaw', 6, 0x23649109],
111+
[0xab058a2, 'karate', 6, 0xc5591f41],
112+
[0x5bff2b7a, 'landscape', 9, 0xf10eb644],
113+
[0x605c9a5f, 'machine', 7, 0xbaa0a636],
114+
[0x51bdeea5, 'nanometer', 9, 0x6af89afb],
115+
[0x85c21c79, 'oblivion', 8, 0xecae222b],
116+
[0x97216f56, 'panama', 6, 0x47dffac4],
117+
[0x18444af2, 'quest', 5, 0x70c2fe36],
118+
[0xbe6ce359, 'resource', 8, 0x1471d925],
119+
[0x843071f1, 'secret', 6, 0x50c9a0db],
120+
[0xf2480c60, 'ultimate', 8, 0xf973daf8],
121+
[0x2d2feb3d, 'vector', 6, 0x344ac03d],
122+
[0x7490310a, 'walrus', 6, 0x6d1408ef],
123+
[0x97d247d4, 'xeno', 4, 0xe62670b5],
124+
[0x93cf7599, 'yelling', 7, 0x1b36da38],
125+
[0x73c84278, 'zlib', 4, 0x6432d127],
126+
[0x228a87d1, '4BJD7PocN1VqX0jXVpWB', 20, 0x997107d0],
127+
[0xa7a048d0, 'F1rPWI7XvDs6nAIRx41l', 20, 0xdc567274],
128+
[0x1f0ded40, 'ldhKlsVkPFOveXgkGtC2', 20, 0xdcc63870],
129+
[0xa804a62f, '5KKnGOOrs8BvJ35iKTOS', 20, 0x6926cffd],
130+
[0x508fae6a, '0l1tw7GOcem06Ddu7yn4', 20, 0xb52b38bc],
131+
[0xe5adaf4f, 'MCr47CjPIn9R1IvE1Tm5', 20, 0xf83b8178],
132+
[0x67136a40, 'UcixbzPKTIv0SvILHVdO', 20, 0xc5213070],
133+
[0xb00c4a10, 'dGnAyAhRQDsWw0ESou24', 20, 0xbc7648b0],
134+
[0x2e0c84b5, 'di0nvmY9UYMYDh0r45XT', 20, 0xd8123a72],
135+
[0x81238d44, '2XKDwHfAhFsV0RhbqtvH', 20, 0xd5ac5620],
136+
[0xf853aa92, 'ZhrANFIiIvRnqClIVyeD', 20, 0xceae099d],
137+
[0x5a692325, 'v7Q9ehzioTOVeDIZioT1', 20, 0xb07d2b24],
138+
[0x3275b9f, 'Yod5hEeKcYqyhfXbhxj2', 20, 0x24ce91df],
139+
[0x38371feb, 'GehSWY2ay4uUKhehXYb0', 20, 0x707b3b30],
140+
[0xafc8bf62, 'kwytJmq6UqpflV8Y8GoE', 20, 0x16abc6a9],
141+
[0x9b07db73, '70684206568419061514', 20, 0xae1fb7b7],
142+
[0xe75b214, '42015093765128581010', 20, 0xd4eecd2d],
143+
[0x72d0fe6f, '88214814356148806939', 20, 0x4660ec7],
144+
[0xf857a4b1, '43472694284527343838', 20, 0xfd8afdf7],
145+
[0x54b8e14, '49769333513942933689', 20, 0xc6d1b5f2],
146+
[0xd6aa5616, '54979784887993251199', 20, 0x32476461],
147+
[0x11e63098, '58360544869206793220', 20, 0xd917cf1a],
148+
[0xbe92385, '27347953487840714234', 20, 0x4ad14a12],
149+
[0x49511de0, '07650690295365319082', 20, 0xe37b5c6c],
150+
[0x3db13bc1, '42655507906821911703', 20, 0x7cc497f1],
151+
[0xbb899bea, '29977409200786225655', 20, 0x99781bb2],
152+
[0xf6cd9436, '85181542907229116674', 20, 0x132256a1],
153+
[0x9109e6c3, '87963594337989416799', 20, 0xbfdb2c83],
154+
[0x75770fc, '21395988329504168551', 20, 0x8d9d1e81],
155+
[0x69b1d19b, '51991013580943379423', 20, 0x7b6d4404],
156+
[0xc6132975, '*]+@!);({_$;}[_},?{?;(_?,=-][@', 30, 0x8619f010],
157+
[0xd58cb00c, '_@:_).&(#.[:[{[:)$++-($_;@[)}+', 30, 0x15746ac3],
158+
[0xb63b8caa, '&[!,[$_==}+.]@!;*(+},[;:)$;)-@', 30, 0xaccf812f],
159+
[0x8a45a2b8, ']{.[.+?+[[=;[?}_#&;[=)__$$:+=_', 30, 0x78af45de],
160+
[0xcbe95b78, '-%.)=/[@].:.(:,()$;=%@-$?]{%+%', 30, 0x25b06b59],
161+
[0x4ef8a54b, '+]#$(@&.=:,*];/.!]%/{:){:@(;)$', 30, 0x4ba0d08f],
162+
[0x76ad267a, ')-._.:?[&:.=+}(*$/=!.${;(=$@!}', 30, 0xe26b6aac],
163+
[0x569e613c, ':(_*&%/[[}+,?#$&*+#[([*-/#;%(]', 30, 0x7e2b0a66],
164+
[0x36aa61da, '{[#-;:$/{)(+[}#]/{&!%(@)%:@-$:', 30, 0xb3430dc7],
165+
[0xf67222df, '_{$*,}(&,@.)):=!/%(&(,,-?$}}}!', 30, 0x626c17a],
166+
[0x74b34fd3,
167+
'e$98KNzqaV)Y:2X?]77].{gKRD4G5{mHZk,Z)SpU%L3FSgv!Wb8MLAFdi{+fp)c,@8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+
for (const [ crc, data, len, expected ] of tests) {
186+
if (data === 0) {
187+
continue;
188+
}
189+
const buf = Buffer.from(data, 'binary');
190+
assert.strictEqual(buf.length, len);
191+
assert.strictEqual(zlib.crc32(buf, crc), expected,
192+
`crc32('${data}', ${crc}) in buffer is not ${expected}`);
193+
assert.strictEqual(zlib.crc32(buf.toString(), crc), expected,
194+
`crc32('${data}', ${crc}) in string is not ${expected}`);
195+
if (crc === 0) {
196+
assert.strictEqual(zlib.crc32(buf), expected,
197+
`crc32('${data}') in buffer is not ${expected}`);
198+
assert.strictEqual(zlib.crc32(buf.toString()), expected,
199+
`crc32('${data}') in string is not ${expected}`);
200+
}
201+
}
202+
203+
[undefined, null, true, 1, () => {}, {}].forEach((invalid) => {
204+
assert.throws(() => { zlib.crc32(invalid); }, { code: 'ERR_INVALID_ARG_TYPE' });
205+
});
206+
207+
[null, true, () => {}, {}].forEach((invalid) => {
208+
assert.throws(() => { zlib.crc32('test', invalid); }, { code: 'ERR_INVALID_ARG_TYPE' });
209+
});

0 commit comments

Comments
 (0)