Skip to content

Commit 6c0928b

Browse files
committed
Updated Crc64Nvme to have a JS and VM implementation due to the int size difference between platforms
1 parent 83acdfb commit 6c0928b

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'package:crclib/crclib.dart';
5+
6+
/// CRC-64/NVME implementation for JavaScript/Web.
7+
///
8+
/// On JS, only 32-bit integers are supported, so we use BigInt for 64-bit values.
9+
/// Parameters are defined here:
10+
/// https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme
11+
/// https://nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0d-2023.12.28-Ratified.pdf
12+
class Crc64Nvme extends ParametricCrc {
13+
Crc64Nvme()
14+
: super(
15+
64,
16+
BigInt.parse('ad93d23594c93659', radix: 16),
17+
BigInt.parse('ffffffffffffffff', radix: 16),
18+
BigInt.parse('ffffffffffffffff', radix: 16),
19+
inputReflected: true,
20+
outputReflected: true,
21+
);
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'package:crclib/crclib.dart';
5+
6+
/// CRC-64/NVME implementation for Dart VM.
7+
///
8+
/// On the VM, 64-bit integers are supported natively, so we use int.
9+
/// Parameters are defined here:
10+
/// https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme
11+
/// https://nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0d-2023.12.28-Ratified.pdf
12+
class Crc64Nvme extends ParametricCrc {
13+
Crc64Nvme()
14+
: super(
15+
64,
16+
0xad93d23594c93659,
17+
0xffffffffffffffff,
18+
0xffffffffffffffff,
19+
inputReflected: true,
20+
outputReflected: true,
21+
);
22+
}

packages/smithy/smithy_aws/lib/src/http/interceptors/with_checksum.dart

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ import 'package:crclib/catalog.dart';
1010
import 'package:crclib/crclib.dart';
1111
import 'package:crypto/crypto.dart';
1212
import 'package:smithy/smithy.dart';
13+
import 'package:smithy_aws/src/http/interceptors/crc64nvme_js.dart'
14+
if (dart.library.io) 'crc64nvme_vm.dart';
1315

1416
class _CrcValueToHeaderConverter extends Converter<CrcValue, String> {
1517
const _CrcValueToHeaderConverter();
1618

1719
@override
1820
String convert(CrcValue input) {
19-
return base64Encode(hex.decode(input.toRadixString(16)));
21+
// Convert to BigInt to ensure unsigned representation
22+
final bigIntValue = input.toBigInt();
23+
final hexString = bigIntValue.toRadixString(16);
24+
25+
// Pad with leading zero if odd length (hex.decode requires even length)
26+
final paddedHex = hexString.length.isOdd ? '0$hexString' : hexString;
27+
return base64Encode(hex.decode(paddedHex));
2028
}
2129

2230
@override
@@ -91,18 +99,3 @@ class WithChecksum extends HttpRequestInterceptor {
9199
return request;
92100
}
93101
}
94-
95-
// Parameters are defined here
96-
// https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme
97-
// https://nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0d-2023.12.28-Ratified.pdf
98-
class Crc64Nvme extends ParametricCrc {
99-
Crc64Nvme()
100-
: super(
101-
64,
102-
BigInt.parse('ad93d23594c93659', radix: 16),
103-
BigInt.parse('ffffffffffffffff', radix: 16),
104-
BigInt.parse('ffffffffffffffff', radix: 16),
105-
inputReflected: true,
106-
outputReflected: true,
107-
);
108-
}

0 commit comments

Comments
 (0)