Skip to content

Commit 784d8d2

Browse files
committed
fixed CRC 16 DNP which was missing start and length somehow and therefore not calculating correct values ever outside of full crc calculations (discovered this while updating the swift version of this library). Fixed the CRC 16 Segment validation tests. Updated Assembly, sonar and package versions
1 parent 87e8cc7 commit 784d8d2

File tree

7 files changed

+39
-32
lines changed

7 files changed

+39
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ PublishScripts/
162162
# NuGet v3's project.json files produces more ignoreable files
163163
*.nuget.props
164164
*.nuget.targets
165+
**/nuget.config
165166

166167
# Microsoft Azure Build Output
167168
csx/

NullFX.CRC.Tests/Crc16Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class Crc16Tests {
4040
static ushort CrcCcittFCrc = 0xF7B6;
4141

4242
static ushort Crc16Standard_StartLessThanLength = 0x29D8;
43-
static ushort Crc16Dnp_StartLessThanLength = 0xF23F;
43+
static ushort Crc16Dnp_StartLessThanLength = 0x9B9A;
4444
static ushort Crc16CcittKermit_StartLessThanLength = 0x3579;
4545
static ushort Crc16Ccitt_StartLessThanLength = 0xC35B;
4646
static ushort Crc16Ccitt1D04_StartLessThanLength = 0x3295;

NullFX.CRC/Crc16.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ internal static ushort ComputeChecksum ( byte[] bytes, int start, int length ) {
5757
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ); }
5858
if ( bytes.Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes.Length ) ); }
5959
if ( start < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
60-
if ( start + length > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
61-
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
60+
if ( start >= bytes.Length && length > 1 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
6261
var crc = InitialValue;
63-
var end = start + length;
64-
for ( int i = start; i < end; ++i ) {
62+
var end = start + length - 1;
63+
if ( end > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
64+
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
65+
for ( int i = start; i <= end; ++i ) {
6566
crc = ( ushort )( ( crc >> 8 ) ^ table[( byte )( crc ^ bytes[i] )] );
6667
}
6768
return crc;
@@ -95,11 +96,12 @@ internal static ushort ComputeChecksum ( byte[] bytes, int start, int length ) {
9596
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ); }
9697
if ( bytes.Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes.Length ) ); }
9798
if ( start < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
98-
if ( start + length > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
99-
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
99+
if ( start >= bytes.Length && length > 1 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
100100
var crc = InitialValue;
101-
var end = start + length;
102-
for ( int i = start; i < end; ++i ) {
101+
var end = start + length - 1;
102+
if ( end > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
103+
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
104+
for ( int i = start; i <= end; ++i ) {
103105
crc = ( ushort )( ( crc >> 8 ) ^ table[( byte )( crc ^ bytes[i] )] );
104106
}
105107
return crc.ByteSwapCompliment ( );
@@ -133,11 +135,12 @@ internal static ushort ComputeChecksum ( byte[] bytes, int start, int length ) {
133135
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ); }
134136
if ( bytes.Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes.Length ) ); }
135137
if ( start < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
136-
if ( start + length > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
137-
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
138+
if ( start >= bytes.Length && length > 1 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
138139
var crc = InitialValue;
139-
var end = start + length;
140-
for ( int i = start; i < end; ++i ) {
140+
var end = start + length - 1;
141+
if ( end > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
142+
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
143+
for ( int i = start; i <= end; ++i ) {
141144
crc = ( ushort )( ( crc >> 8 ) ^ table[( byte )( crc ^ bytes[i] )] );
142145
}
143146
return crc.ByteSwap ( );
@@ -233,11 +236,12 @@ internal static ushort ComputeChecksum ( ushort initialValue, byte[] bytes, int
233236
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ); }
234237
if ( bytes.Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes.Length ) ); }
235238
if ( start < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
236-
if ( start + length > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
237-
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
239+
if ( start >= bytes.Length && length > 1 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
238240
var crc = initialValue;
239-
var end = start + length;
240-
for ( int i = start; i < end; ++i ) {
241+
var end = start + length - 1;
242+
if ( end > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
243+
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
244+
for ( int i = start; i <= end; ++i ) {
241245
crc = ( ushort )( ( crc << 8 ) ^ Table[( ( crc >> 8 ) ^ ( 0xff & bytes[i] ) )] );
242246
}
243247
return crc;
@@ -275,7 +279,7 @@ public static ushort ComputeChecksum ( Crc16Algorithm algorithm, byte[] bytes, i
275279
case Crc16Algorithm.CcittInitialValue0x1D0F:
276280
return CcittInitial0x1D0F.ComputeChecksum ( bytes, start, length );
277281
case Crc16Algorithm.Dnp:
278-
return Dnp.ComputeChecksum ( bytes );
282+
return Dnp.ComputeChecksum ( bytes, start, length );
279283
}
280284
throw new UnknownAlgorithmException ( "Unknown Algorithm" );
281285
}

NullFX.CRC/Crc32.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ public static uint ComputeChecksum( byte[] bytes, int start, int length ) {
5454
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ); }
5555
if ( bytes.Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes.Length ) ); }
5656
if ( start < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
57-
if ( start + length > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
58-
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
57+
if ( start >= bytes.Length && length > 1 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
5958
var crc = InitialValue;
60-
var end = start + length;
61-
for ( int i = start; i < end; ++i ) {
59+
var end = start + length - 1;
60+
if ( end > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
61+
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
62+
for ( int i = start; i <= end; ++i ) {
6263
crc = ( uint )( ( crc >> 8 ) ^ table[( byte )( ( ( crc ) & 0xff ) ^ bytes[i] )] );
6364
}
6465
return ~crc;

NullFX.CRC/Crc8.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ public static byte ComputeChecksum ( byte[] bytes, int start, int length ) {
5353
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ); }
5454
if ( bytes.Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes.Length ) ); }
5555
if ( start < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
56-
if ( start + length > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
57-
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
56+
if ( start >= bytes.Length && length > 1 ) { throw new ArgumentOutOfRangeException ( nameof ( start ) ); }
5857
var crc = InitialValue;
59-
var end = start + length;
60-
for ( int i = start; i < end; ++i ) {
58+
var end = start + length - 1;
59+
if ( end > bytes.Length ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
60+
if ( length < 0 ) { throw new ArgumentOutOfRangeException ( nameof ( length ) ); }
61+
for ( int i = start; i <= end; ++i ) {
6162
crc = table[crc ^ bytes[i]];
6263
}
6364
return crc;

NullFX.CRC/NullFX.CRC.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212
<PackageTags>CRC8 CRC16 CRC32</PackageTags>
1313
<PackageProjectUrl>https://github.com/nullfx/NullFX.CRC</PackageProjectUrl>
1414
<RepositoryUrl>https://github.com/nullfx/NullFX.CRC</RepositoryUrl>
15-
<PackageVersion>1.1.0</PackageVersion>
15+
<PackageVersion>1.1.1</PackageVersion>
1616
<Owners>Steve Whitley</Owners>
17-
<Summary>A collection of common CRC algorithms implemented in .NET</Summary>
17+
<Summary>NullFX CRC is a small set of CRC utilities (crc8, crc16, and crc32) written in C# and released under the MIT License</Summary>
1818
<Title>NullFX CRC</Title>
1919
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2020
<IncludeSymbols>true</IncludeSymbols>
2121
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
22-
<AssemblyVersion>1.1.0.0</AssemblyVersion>
23-
<FileVersion>1.1.0.0</FileVersion>
22+
<AssemblyVersion>1.1.1.0</AssemblyVersion>
23+
<FileVersion>1.1.1.0</FileVersion>
2424
<PackageLicenseExpression>MIT</PackageLicenseExpression>
25-
<Version>1.1.0</Version>
25+
<Version>1.1.1</Version>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sonar.projectKey=nullfx_NullFX.CRC
22
sonar.organization=nullfx
33
sonar.projectName=NullFX.CRC
4-
sonar.projectVersion=1.1.0
4+
sonar.projectVersion=1.1.1
55
sonar.sources=./NullFX.CRC
66
sonar.sourceEncoding=UTF-8

0 commit comments

Comments
 (0)