@@ -117,59 +117,21 @@ public void Update(byte[] buffer)
117
117
throw new ArgumentNullException ( nameof ( buffer ) ) ;
118
118
}
119
119
120
- Update ( buffer , 0 , buffer . Length ) ;
120
+ Update ( new ArraySegment < byte > ( buffer , 0 , buffer . Length ) ) ;
121
121
}
122
122
123
123
/// <summary>
124
124
/// Update Adler32 data checksum based on a portion of a block of data
125
125
/// </summary>
126
- /// <param name = "buffer">Contains the data to update the CRC with.</param >
127
- /// <param name = "offset"> The offset into the buffer where the data starts</param>
128
- /// <param name = "count">The number of data bytes to update the CRC with.< /param>
129
- public void Update ( byte [ ] buffer , int offset , int count )
126
+ /// <param name = "segment" >
127
+ /// The chunk of data to add
128
+ /// </param>
129
+ public void Update ( ArraySegment < byte > segment )
130
130
{
131
- if ( buffer == null ) {
132
- throw new ArgumentNullException ( nameof ( buffer ) ) ;
133
- }
134
-
135
- if ( offset < 0 ) {
136
- throw new ArgumentOutOfRangeException ( nameof ( offset ) , "cannot be less than zero" ) ;
137
- }
138
-
139
- if ( offset >= buffer . Length ) {
140
- throw new ArgumentOutOfRangeException ( nameof ( offset ) , "not a valid index into buffer" ) ;
141
- }
142
-
143
- if ( count < 0 ) {
144
- throw new ArgumentOutOfRangeException ( nameof ( count ) , "cannot be less than zero" ) ;
131
+ foreach ( byte b in segment )
132
+ {
133
+ Update ( b ) ;
145
134
}
146
-
147
- if ( offset + count > buffer . Length ) {
148
- throw new ArgumentOutOfRangeException ( nameof ( count ) , "exceeds buffer size" ) ;
149
- }
150
-
151
- //(By Per Bothner)
152
- uint s1 = checkValue & 0xFFFF ;
153
- uint s2 = checkValue >> 16 ;
154
-
155
- while ( count > 0 ) {
156
- // We can defer the modulo operation:
157
- // s1 maximally grows from 65521 to 65521 + 255 * 3800
158
- // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31
159
- int n = 3800 ;
160
- if ( n > count ) {
161
- n = count ;
162
- }
163
- count -= n ;
164
- while ( -- n >= 0 ) {
165
- s1 = s1 + ( uint ) ( buffer [ offset ++ ] & 0xff ) ;
166
- s2 = s2 + s1 ;
167
- }
168
- s1 %= BASE ;
169
- s2 %= BASE ;
170
- }
171
-
172
- checkValue = ( s2 << 16 ) | s1 ;
173
135
}
174
136
}
175
137
}
0 commit comments