1
0
Fork 0

crc: move tbl out of class, more tests

pull/1063/head
Rob Herley 2022-04-26 17:31:17 +00:00 committed by GitHub
parent 3d61fe8000
commit fccc5ee6e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 284 additions and 262 deletions

View File

@ -2,8 +2,8 @@ import CRC64 from '../src/internal/crc64'
const fixtures = { const fixtures = {
data: data:
'🚀 👉😎👉 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', '🚀 👉😎👉 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\nUt enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\nExcepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n',
expected: 'D170FCD035F4D958' expected: '846CE4ADAD6223ED'
} }
describe('@actions/artifact/src/internal/crc64', () => { describe('@actions/artifact/src/internal/crc64', () => {
@ -19,4 +19,25 @@ describe('@actions/artifact/src/internal/crc64', () => {
crc.update(buf) crc.update(buf)
expect(crc.digest()).toEqual(fixtures.expected) expect(crc.digest()).toEqual(fixtures.expected)
}) })
it('CRC64 from split data', async () => {
const crc = new CRC64()
const splits = fixtures.data.split('\n').slice(0, -1)
for (const split of splits) {
crc.update(`${split}\n`)
}
expect(crc.digest()).toEqual(fixtures.expected)
})
it('flips 64 bits', async () => {
const tests = [
[BigInt(0), BigInt('0xffffffffffffffff')],
[BigInt('0xffffffffffffffff'), BigInt(0)],
[BigInt('0xdeadbeef'), BigInt('0xffffffff21524110')]
]
for (const [input, expected] of tests) {
expect(CRC64.flip64Bits(input)).toEqual(expected)
}
})
}) })

View File

@ -9,10 +9,8 @@
* is used for Azure Storage: https://github.com/Azure/azure-storage-net * is used for Azure Storage: https://github.com/Azure/azure-storage-net
*/ */
class CRC64 { // when transpile target is >= ES2020 (after dropping node 12) these can be changed to bigint literals - ts(2737)
private _crc: bigint const AZURE_TABLE = [
// when transpile target is >= ES2020 (after dropping node 12) these can be changed to bigint literals - ts(2737)
private _table = [
BigInt('0x0000000000000000'), BigInt('0x0000000000000000'),
BigInt('0x7F6EF0C830358979'), BigInt('0x7F6EF0C830358979'),
BigInt('0xFEDDE190606B12F2'), BigInt('0xFEDDE190606B12F2'),
@ -269,7 +267,10 @@ class CRC64 {
BigInt('0xD407B1D78F8795DA'), BigInt('0xD407B1D78F8795DA'),
BigInt('0x55B4A08FDFD90E51'), BigInt('0x55B4A08FDFD90E51'),
BigInt('0x2ADA5047EFEC8728') BigInt('0x2ADA5047EFEC8728')
] ]
class CRC64 {
private _crc: bigint
constructor() { constructor() {
this._crc = BigInt(0) this._crc = BigInt(0)
@ -281,7 +282,7 @@ class CRC64 {
for (const dataByte of buffer) { for (const dataByte of buffer) {
const crcByte = Number(crc & BigInt(0xff)) const crcByte = Number(crc & BigInt(0xff))
crc = this._table[crcByte ^ dataByte] ^ (crc >> BigInt(8)) crc = AZURE_TABLE[crcByte ^ dataByte] ^ (crc >> BigInt(8))
} }
this._crc = CRC64.flip64Bits(crc) this._crc = CRC64.flip64Bits(crc)