@@ -57,11 +57,12 @@ internal static ushort ComputeChecksum ( byte[] bytes, int start, int length ) {
57
57
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ) ; }
58
58
if ( bytes . Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes . Length ) ) ; }
59
59
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 ) ) ; }
62
61
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 ) {
65
66
crc = ( ushort ) ( ( crc >> 8 ) ^ table [ ( byte ) ( crc ^ bytes [ i ] ) ] ) ;
66
67
}
67
68
return crc ;
@@ -95,11 +96,12 @@ internal static ushort ComputeChecksum ( byte[] bytes, int start, int length ) {
95
96
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ) ; }
96
97
if ( bytes . Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes . Length ) ) ; }
97
98
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 ) ) ; }
100
100
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 ) {
103
105
crc = ( ushort ) ( ( crc >> 8 ) ^ table [ ( byte ) ( crc ^ bytes [ i ] ) ] ) ;
104
106
}
105
107
return crc . ByteSwapCompliment ( ) ;
@@ -133,11 +135,12 @@ internal static ushort ComputeChecksum ( byte[] bytes, int start, int length ) {
133
135
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ) ; }
134
136
if ( bytes . Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes . Length ) ) ; }
135
137
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 ) ) ; }
138
139
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 ) {
141
144
crc = ( ushort ) ( ( crc >> 8 ) ^ table [ ( byte ) ( crc ^ bytes [ i ] ) ] ) ;
142
145
}
143
146
return crc . ByteSwap ( ) ;
@@ -233,11 +236,12 @@ internal static ushort ComputeChecksum ( ushort initialValue, byte[] bytes, int
233
236
if ( bytes == null ) { throw new ArgumentNullException ( nameof ( bytes ) ) ; }
234
237
if ( bytes . Length == 0 ) { throw new ArgumentOutOfRangeException ( nameof ( bytes . Length ) ) ; }
235
238
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 ) ) ; }
238
240
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 ) {
241
245
crc = ( ushort ) ( ( crc << 8 ) ^ Table [ ( ( crc >> 8 ) ^ ( 0xff & bytes [ i ] ) ) ] ) ;
242
246
}
243
247
return crc ;
@@ -275,7 +279,7 @@ public static ushort ComputeChecksum ( Crc16Algorithm algorithm, byte[] bytes, i
275
279
case Crc16Algorithm . CcittInitialValue0x1D0F :
276
280
return CcittInitial0x1D0F . ComputeChecksum ( bytes , start , length ) ;
277
281
case Crc16Algorithm . Dnp :
278
- return Dnp . ComputeChecksum ( bytes ) ;
282
+ return Dnp . ComputeChecksum ( bytes , start , length ) ;
279
283
}
280
284
throw new UnknownAlgorithmException ( "Unknown Algorithm" ) ;
281
285
}
0 commit comments