Skip to content

Commit 6327312

Browse files
authored
Merge PR #202, Convert CRC checking to ArraySegment
Convert crc checking to use ArraySegment for parameter validation. Fixes #183.
2 parents 083ca7a + f11cb10 commit 6327312

File tree

14 files changed

+50
-130
lines changed

14 files changed

+50
-130
lines changed

src/ICSharpCode.SharpZipLib/Checksum/Adler32.cs

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -117,59 +117,21 @@ public void Update(byte[] buffer)
117117
throw new ArgumentNullException(nameof(buffer));
118118
}
119119

120-
Update(buffer, 0, buffer.Length);
120+
Update(new ArraySegment<byte>(buffer, 0, buffer.Length));
121121
}
122122

123123
/// <summary>
124124
/// Update Adler32 data checksum based on a portion of a block of data
125125
/// </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)
130130
{
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);
145134
}
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;
173135
}
174136
}
175137
}

src/ICSharpCode.SharpZipLib/Checksum/BZip2Crc.cs

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -161,39 +161,20 @@ public void Update(byte[] buffer)
161161
throw new ArgumentNullException(nameof(buffer));
162162
}
163163

164-
Update(buffer, 0, buffer.Length);
164+
Update(new ArraySegment<byte>(buffer, 0, buffer.Length));
165165
}
166166

167167
/// <summary>
168168
/// Update CRC data checksum based on a portion of a block of data
169169
/// </summary>
170-
/// <param name = "buffer">Contains the data to update the CRC with.</param>
171-
/// <param name = "offset">The offset into the buffer where the data starts</param>
172-
/// <param name = "count">The number of data bytes to update the CRC with.</param>
173-
public void Update(byte[] buffer, int offset, int count)
170+
/// <param name = "segment">
171+
/// The chunk of data to add
172+
/// </param>
173+
public void Update(ArraySegment<byte> segment)
174174
{
175-
if (buffer == null) {
176-
throw new ArgumentNullException(nameof(buffer));
177-
}
178-
179-
if (offset < 0) {
180-
throw new ArgumentOutOfRangeException(nameof(offset), "cannot be less than zero");
181-
}
182-
183-
if (offset >= buffer.Length) {
184-
throw new ArgumentOutOfRangeException(nameof(offset), "not a valid index into buffer");
185-
}
186-
187-
if (count < 0) {
188-
throw new ArgumentOutOfRangeException(nameof(count), "cannot be less than zero");
189-
}
190-
191-
if (offset + count > buffer.Length) {
192-
throw new ArgumentOutOfRangeException(nameof(count), "exceeds buffer size");
193-
}
194-
195-
for (int i = 0; i < count; ++i) {
196-
Update(buffer[offset++]);
175+
foreach (byte b in segment)
176+
{
177+
Update(b);
197178
}
198179
}
199180
}

src/ICSharpCode.SharpZipLib/Checksum/Crc32.cs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -150,39 +150,19 @@ public void Update(byte[] buffer)
150150
throw new ArgumentNullException(nameof(buffer));
151151
}
152152

153-
Update(buffer, 0, buffer.Length);
153+
Update(new ArraySegment<byte>(buffer, 0, buffer.Length));
154154
}
155155

156156
/// <summary>
157157
/// Update CRC data checksum based on a portion of a block of data
158158
/// </summary>
159-
/// <param name = "buffer">Contains the data to update the CRC with.</param>
160-
/// <param name = "offset">The offset into the buffer where the data starts</param>
161-
/// <param name = "count">The number of data bytes to update the CRC with.</param>
162-
public void Update(byte[] buffer, int offset, int count)
159+
/// <param name = "segment">
160+
/// The chunk of data to add
161+
/// </param>
162+
public void Update(ArraySegment<byte> segment)
163163
{
164-
if (buffer == null) {
165-
throw new ArgumentNullException(nameof(buffer));
166-
}
167-
168-
if (offset < 0) {
169-
throw new ArgumentOutOfRangeException(nameof(offset), "cannot be less than zero");
170-
}
171-
172-
if (offset >= buffer.Length) {
173-
throw new ArgumentOutOfRangeException(nameof(offset), "not a valid index into buffer");
174-
}
175-
176-
if (count < 0) {
177-
throw new ArgumentOutOfRangeException(nameof(count), "cannot be less than zero");
178-
}
179-
180-
if (offset + count > buffer.Length) {
181-
throw new ArgumentOutOfRangeException(nameof(count), "exceeds buffer size");
182-
}
183-
184-
for (int i = 0; i < count; ++i) {
185-
Update(buffer[offset++]);
164+
foreach (byte b in segment) {
165+
Update(b);
186166
}
187167
}
188168
}

src/ICSharpCode.SharpZipLib/Checksum/IChecksum.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
13
namespace ICSharpCode.SharpZipLib.Checksum
24
{
35
/// <summary>
@@ -40,15 +42,9 @@ long Value {
4042
/// <summary>
4143
/// Adds the byte array to the data checksum.
4244
/// </summary>
43-
/// <param name = "buffer">
44-
/// The buffer which contains the data
45-
/// </param>
46-
/// <param name = "offset">
47-
/// The offset in the buffer where the data starts
48-
/// </param>
49-
/// <param name = "count">
50-
/// the number of data bytes to add.
45+
/// <param name = "segment">
46+
/// The chunk of data to add
5147
/// </param>
52-
void Update(byte[] buffer, int offset, int count);
48+
void Update(ArraySegment<byte> segment);
5349
}
5450
}

src/ICSharpCode.SharpZipLib/GZip/GzipInputStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public override int Read(byte[] buffer, int offset, int count)
127127
// Try to read compressed data
128128
int bytesRead = base.Read(buffer, offset, count);
129129
if (bytesRead > 0) {
130-
crc.Update(buffer, offset, bytesRead);
130+
crc.Update(new ArraySegment<byte>(buffer, offset, bytesRead));
131131
}
132132

133133
// If this is the end of stream, read the footer

src/ICSharpCode.SharpZipLib/GZip/GzipOutputStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public override void Write(byte[] buffer, int offset, int count)
123123
throw new InvalidOperationException("Write not permitted in current state");
124124
}
125125

126-
crc.Update(buffer, offset, count);
126+
crc.Update(new ArraySegment<byte>(buffer, offset, count));
127127
base.Write(buffer, offset, count);
128128
}
129129

src/ICSharpCode.SharpZipLib/Zip/Compression/DeflaterEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void SetDictionary(byte[] buffer, int offset, int length)
174174
throw new InvalidOperationException("strstart not 1");
175175
}
176176
#endif
177-
adler.Update(buffer, offset, length);
177+
adler.Update(new ArraySegment<byte>(buffer, offset, length));
178178
if (length < DeflaterConstants.MIN_MATCH) {
179179
return;
180180
}
@@ -336,7 +336,7 @@ public void FillWindow()
336336
}
337337

338338
System.Array.Copy(inputBuf, inputOff, window, strstart + lookahead, more);
339-
adler.Update(inputBuf, inputOff, more);
339+
adler.Update(new ArraySegment<byte>(inputBuf, inputOff, more));
340340

341341
inputOff += more;
342342
totalIn += more;

src/ICSharpCode.SharpZipLib/Zip/Compression/Inflater.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ public void SetDictionary(byte[] buffer, int index, int count)
546546
throw new InvalidOperationException("Dictionary is not needed");
547547
}
548548

549-
adler.Update(buffer, index, count);
549+
adler.Update(new ArraySegment<byte>(buffer, index, count));
550550

551551
if ((int)adler.Value != readAdler) {
552552
throw new SharpZipBaseException("Wrong adler checksum");
@@ -687,7 +687,7 @@ public int Inflate(byte[] buffer, int offset, int count)
687687
*/
688688
int more = outputWindow.CopyOutput(buffer, offset, count);
689689
if (more > 0) {
690-
adler.Update(buffer, offset, more);
690+
adler.Update(new ArraySegment<byte>(buffer, offset, more));
691691
offset += more;
692692
bytesCopied += more;
693693
totalOut += (long)more;

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandl
837837
long totalBytes = 0;
838838
int bytesRead;
839839
while ((bytesRead = entryStream.Read(buffer, 0, buffer.Length)) > 0) {
840-
crc.Update(buffer, 0, bytesRead);
840+
crc.Update(new ArraySegment<byte>(buffer, 0, bytesRead));
841841

842842
if (resultHandler != null) {
843843
totalBytes += bytesRead;
@@ -2069,7 +2069,7 @@ void CopyBytes(ZipUpdate update, Stream destination, Stream source,
20692069
bytesRead = source.Read(buffer, 0, readSize);
20702070
if (bytesRead > 0) {
20712071
if (updateCrc) {
2072-
crc.Update(buffer, 0, bytesRead);
2072+
crc.Update(new ArraySegment<byte>(buffer, 0, bytesRead));
20732073
}
20742074
destination.Write(buffer, 0, bytesRead);
20752075
bytesToCopy -= bytesRead;
@@ -2149,7 +2149,7 @@ void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref lo
21492149
bytesRead = stream.Read(buffer, 0, readSize);
21502150
if (bytesRead > 0) {
21512151
if (updateCrc) {
2152-
crc.Update(buffer, 0, bytesRead);
2152+
crc.Update(new ArraySegment<byte>(buffer, 0, bytesRead));
21532153
}
21542154
stream.Position = destinationPosition;
21552155
stream.Write(buffer, 0, bytesRead);

src/ICSharpCode.SharpZipLib/Zip/ZipInputStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ int BodyRead(byte[] buffer, int offset, int count)
585585
}
586586

587587
if (count > 0) {
588-
crc.Update(buffer, offset, count);
588+
crc.Update(new ArraySegment<byte>(buffer, offset, count));
589589
}
590590

591591
if (finished) {

0 commit comments

Comments
 (0)