-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 校验码| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
it('basic', () => { | ||
expect(crc32('1234567890').toString(16)).to.equal('261daee5'); | ||
}); |