Skip to content

Commit ca3341b

Browse files
committed
add: crc32
1 parent 7627151 commit ca3341b

6 files changed

Lines changed: 114 additions & 0 deletions

File tree

DOC.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,20 @@ CRC16 implementation.
20522052
crc16('1234567890').toString(16); // -> 'c57a'
20532053
```
20542054

2055+
## crc32
2056+
2057+
CRC32 implementation.
2058+
2059+
|Name |Type |Desc |
2060+
|----------|------------------------------------|---------------------|
2061+
|input |string Buffer ArrayBuffer Uint8Array|Data to calculate |
2062+
|[previous]|number |Previous CRC32 result|
2063+
|return |number |CRC16 result |
2064+
2065+
```javascript
2066+
crc32('1234567890').toString(16); // -> '261daee5'
2067+
```
2068+
20552069
## crc8
20562070

20572071
CRC8 implementation.

DOC_CN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,20 @@ CRC16 算法实现。
20472047
crc16('1234567890').toString(16); // -> 'c57a'
20482048
```
20492049

2050+
## crc32
2051+
2052+
CRC32 算法实现。
2053+
2054+
|参数名|类型|说明|
2055+
|-----|----|---|
2056+
|input|string Buffer ArrayBuffer Uint8Array|信息码|
2057+
|[previous]|number|用于累积计算的 CRC32 校验码|
2058+
|返回值|number|CRC32 校验码|
2059+
2060+
```javascript
2061+
crc32('1234567890').toString(16); // -> '261daee5'
2062+
```
2063+
20502064
## crc8
20512065

20522066
CRC8 算法实现。

index.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,21 @@
12561256
"browser"
12571257
]
12581258
},
1259+
"crc32": {
1260+
"description": "CRC32 implementation.",
1261+
"dependencies": [
1262+
"crc1"
1263+
],
1264+
"env": [
1265+
"node",
1266+
"browser",
1267+
"miniprogram"
1268+
],
1269+
"test": [
1270+
"node",
1271+
"browser"
1272+
]
1273+
},
12591274
"crc8": {
12601275
"description": "CRC8 implementation.",
12611276
"dependencies": [

src/c/crc32.i18n.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## CN
2+
3+
CRC32 算法实现。
4+
5+
|参数名|类型|说明|
6+
|-----|----|---|
7+
|input|string Buffer ArrayBuffer Uint8Array|信息码|
8+
|[previous]|number|用于累积计算的 CRC32 校验码|
9+
|返回值|number|CRC32 校验码|

src/c/crc32.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* CRC32 implementation.
2+
*
3+
* |Name |Type |Desc |
4+
* |----------|------------------------------------|---------------------|
5+
* |input |string Buffer ArrayBuffer Uint8Array|Data to calculate |
6+
* |[previous]|number |Previous CRC32 result|
7+
* |return |number |CRC16 result |
8+
*/
9+
10+
/* example
11+
* crc32('1234567890').toString(16); // -> '261daee5'
12+
*/
13+
14+
/* module
15+
* env: all
16+
* test: all
17+
*/
18+
19+
/* typescript
20+
* export declare function crc32(
21+
* input: string | Buffer | ArrayBuffer | Uint8Array,
22+
* previous?: number
23+
* ): number;
24+
*/
25+
26+
_('crc1');
27+
28+
let TABLE = [];
29+
30+
for (let n = 0; n < 256; n++) {
31+
let c = n;
32+
for (let k = 0; k < 8; k++) {
33+
if (c & 1) {
34+
c = 0xedb88320 ^ (c >>> 1);
35+
} else {
36+
c = c >>> 1;
37+
}
38+
}
39+
TABLE[n] = c >>> 0;
40+
}
41+
42+
if (typeof Int32Array !== 'undefined') TABLE = new Int32Array(TABLE);
43+
44+
exports = function(input, previous) {
45+
return exports.signed(input, previous) >>> 0;
46+
};
47+
48+
exports.signed = function(input, previous) {
49+
input = crc1.transInput(input);
50+
51+
let crc = previous === 0 ? 0 : ~~previous ^ -1;
52+
53+
for (let i = 0, len = input.length; i < len; i++) {
54+
const byte = input[i];
55+
crc = TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8);
56+
}
57+
58+
return crc ^ -1;
59+
};

src/c/crc32.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it('basic', () => {
2+
expect(crc32('1234567890').toString(16)).to.equal('261daee5');
3+
});

0 commit comments

Comments
 (0)