Skip to content

Commit

Permalink
add: crc32
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Aug 14, 2019
1 parent 7627151 commit ca3341b
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
14 changes: 14 additions & 0 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,20 @@ CRC16 implementation.
crc16('1234567890').toString(16); // -> 'c57a'
```

## crc32

CRC32 implementation.

|Name |Type |Desc |
|----------|------------------------------------|---------------------|
|input |string Buffer ArrayBuffer Uint8Array|Data to calculate |
|[previous]|number |Previous CRC32 result|
|return |number |CRC16 result |

```javascript
crc32('1234567890').toString(16); // -> '261daee5'
```

## crc8

CRC8 implementation.
Expand Down
14 changes: 14 additions & 0 deletions DOC_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,20 @@ CRC16 算法实现。
crc16('1234567890').toString(16); // -> 'c57a'
```

## crc32

CRC32 算法实现。

|参数名|类型|说明|
|-----|----|---|
|input|string Buffer ArrayBuffer Uint8Array|信息码|
|[previous]|number|用于累积计算的 CRC32 校验码|
|返回值|number|CRC32 校验码|

```javascript
crc32('1234567890').toString(16); // -> '261daee5'
```

## crc8

CRC8 算法实现。
Expand Down
15 changes: 15 additions & 0 deletions index.json
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,21 @@
"browser"
]
},
"crc32": {
"description": "CRC32 implementation.",
"dependencies": [
"crc1"
],
"env": [
"node",
"browser",
"miniprogram"
],
"test": [
"node",
"browser"
]
},
"crc8": {
"description": "CRC8 implementation.",
"dependencies": [
Expand Down
9 changes: 9 additions & 0 deletions src/c/crc32.i18n.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## CN

CRC32 算法实现。

|参数名|类型|说明|
|-----|----|---|
|input|string Buffer ArrayBuffer Uint8Array|信息码|
|[previous]|number|用于累积计算的 CRC32 校验码|
|返回值|number|CRC32 校验码|
59 changes: 59 additions & 0 deletions src/c/crc32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* CRC32 implementation.
*
* |Name |Type |Desc |
* |----------|------------------------------------|---------------------|
* |input |string Buffer ArrayBuffer Uint8Array|Data to calculate |
* |[previous]|number |Previous CRC32 result|
* |return |number |CRC16 result |
*/

/* example
* crc32('1234567890').toString(16); // -> '261daee5'
*/

/* module
* env: all
* test: all
*/

/* typescript
* export declare function crc32(
* input: string | Buffer | ArrayBuffer | Uint8Array,
* previous?: number
* ): number;
*/

_('crc1');

let TABLE = [];

for (let n = 0; n < 256; n++) {
let c = n;
for (let k = 0; k < 8; k++) {
if (c & 1) {
c = 0xedb88320 ^ (c >>> 1);
} else {
c = c >>> 1;
}
}
TABLE[n] = c >>> 0;
}

if (typeof Int32Array !== 'undefined') TABLE = new Int32Array(TABLE);

exports = function(input, previous) {
return exports.signed(input, previous) >>> 0;
};

exports.signed = function(input, previous) {
input = crc1.transInput(input);

let crc = previous === 0 ? 0 : ~~previous ^ -1;

for (let i = 0, len = input.length; i < len; i++) {
const byte = input[i];
crc = TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8);
}

return crc ^ -1;
};
3 changes: 3 additions & 0 deletions src/c/crc32.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it('basic', () => {
expect(crc32('1234567890').toString(16)).to.equal('261daee5');
});

0 comments on commit ca3341b

Please sign in to comment.