private void CopyBytes(ZipUpdate update, Stream destination, Stream source, long bytesToCopy, bool updateCrc) { int num3; if (destination == source) { throw new InvalidOperationException("Destination and source are the same"); } Crc32 crc = new Crc32(); byte[] buffer = GetBuffer(); long num = bytesToCopy; long num2 = 0L; do { int length = buffer.Length; if (bytesToCopy < length) { length = (int)bytesToCopy; } num3 = source.Read(buffer, 0, length); if (num3 > 0) { if (updateCrc) { crc.Update(buffer, 0, num3); } destination.Write(buffer, 0, num3); bytesToCopy -= num3; num2 += num3; } } while ((num3 > 0) && (bytesToCopy > 0L)); if (num2 != num) { throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", num, num2)); } if (updateCrc) { update.OutEntry.Crc = crc.Value; } }
private void CopyEntryDataDirect(ZipUpdate update, Stream stream, bool updateCrc, ref long destinationPosition, ref long sourcePosition) { int num4; long compressedSize = update.Entry.CompressedSize; Crc32 crc = new Crc32(); byte[] buffer = GetBuffer(); long num2 = compressedSize; long num3 = 0L; do { int length = buffer.Length; if (compressedSize < length) { length = (int)compressedSize; } stream.Position = sourcePosition; num4 = stream.Read(buffer, 0, length); if (num4 > 0) { if (updateCrc) { crc.Update(buffer, 0, num4); } stream.Position = destinationPosition; stream.Write(buffer, 0, num4); destinationPosition += num4; sourcePosition += num4; compressedSize -= num4; num3 += num4; } } while ((num4 > 0) && (compressedSize > 0L)); if (num3 != num2) { throw new ZipException(string.Format("Failed to copy bytes expected {0} read {1}", num2, num3)); } if (updateCrc) { update.OutEntry.Crc = crc.Value; } }
public bool TestArchive(bool testData, TestStrategy strategy = TestStrategy.FindFirstError, ZipTestResultHandler resultHandler = null) { if (isDisposed_) { throw new ObjectDisposedException("ZipFile"); } TestStatus status = new TestStatus(this); if (resultHandler != null) { resultHandler(status, null); } HeaderTest tests = testData ? (HeaderTest.Header | HeaderTest.Extract) : HeaderTest.Header; bool flag = true; try { for (int i = 0; flag && (i < Count); i++) { if (resultHandler != null) { status.SetEntry(this[i]); status.SetOperation(TestOperation.EntryHeader); resultHandler(status, null); } try { TestLocalHeader(this[i], tests); } catch (ZipException exception) { status.AddError(); if (resultHandler != null) { resultHandler(status, string.Format("Exception during test - '{0}'", exception.Message)); } if (strategy == TestStrategy.FindFirstError) { flag = false; } } if ((flag && testData) && this[i].IsFile) { if (resultHandler != null) { status.SetOperation(TestOperation.EntryData); resultHandler(status, null); } Crc32 crc = new Crc32(); using (Stream stream = GetInputStream(this[i])) { int num3; byte[] buffer = new byte[DefaultBufferSize]; long num2 = 0L; while ((num3 = stream.Read(buffer, 0, buffer.Length)) > 0) { crc.Update(buffer, 0, num3); if (resultHandler != null) { num2 += num3; status.SetBytesTested(num2); resultHandler(status, null); } } } if (this[i].Crc != crc.Value) { status.AddError(); if (resultHandler != null) { resultHandler(status, "CRC mismatch"); } if (strategy == TestStrategy.FindFirstError) { flag = false; } } if ((this[i].Flags & 8) != 0) { ZipHelperStream stream2 = new ZipHelperStream(baseStream_); DescriptorData data = new DescriptorData(); stream2.ReadDataDescriptor(this[i].LocalHeaderRequiresZip64, data); if (this[i].Crc != data.Crc) { status.AddError(); } if (this[i].CompressedSize != data.CompressedSize) { status.AddError(); } if (this[i].Size != data.Size) { status.AddError(); } } } if (resultHandler != null) { status.SetOperation(TestOperation.EntryComplete); resultHandler(status, null); } } if (resultHandler != null) { status.SetOperation(TestOperation.MiscellaneousTests); resultHandler(status, null); } } catch (Exception exception2) { status.AddError(); if (resultHandler != null) { resultHandler(status, string.Format("Exception during test - '{0}'", exception2.Message)); } } if (resultHandler != null) { status.SetOperation(TestOperation.Complete); status.SetEntry(null); resultHandler(status, null); } return (status.ErrorCount == 0); }
private bool ReadHeader() { crc = new Crc32(); if (inputBuffer.Available <= 0) { inputBuffer.Fill(); if (inputBuffer.Available <= 0) { return false; } } Crc32 crc32 = new Crc32(); int num = inputBuffer.ReadLeByte(); if (num < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num); if (num != 0x1f) { throw new GZipException("Error GZIP header, first magic byte doesn't match"); } num = inputBuffer.ReadLeByte(); if (num < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } if (num != 0x8b) { throw new GZipException("Error GZIP header, second magic byte doesn't match"); } crc32.Update(num); int num2 = inputBuffer.ReadLeByte(); if (num2 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } if (num2 != 8) { throw new GZipException("Error GZIP header, data not in deflate format"); } crc32.Update(num2); int num3 = inputBuffer.ReadLeByte(); if (num3 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num3); if ((num3 & 0xe0) != 0) { throw new GZipException("Reserved flag bits in GZIP header != 0"); } for (int i = 0; i < 6; i++) { int num5 = inputBuffer.ReadLeByte(); if (num5 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num5); } if ((num3 & 4) != 0) { for (int j = 0; j < 2; j++) { int num7 = inputBuffer.ReadLeByte(); if (num7 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num7); } if ((inputBuffer.ReadLeByte() < 0) || (inputBuffer.ReadLeByte() < 0)) { throw new EndOfStreamException("EOS reading GZIP header"); } int num8 = inputBuffer.ReadLeByte(); int num9 = inputBuffer.ReadLeByte(); if ((num8 < 0) || (num9 < 0)) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num8); crc32.Update(num9); int num10 = (num8 << 8) | num9; for (int k = 0; k < num10; k++) { int num12 = inputBuffer.ReadLeByte(); if (num12 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num12); } } if ((num3 & 8) != 0) { int num13; while ((num13 = inputBuffer.ReadLeByte()) > 0) { crc32.Update(num13); } if (num13 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num13); } if ((num3 & GZipConstants.FCOMMENT) != 0) { int num14; while ((num14 = inputBuffer.ReadLeByte()) > 0) { crc32.Update(num14); } if (num14 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } crc32.Update(num14); } if ((num3 & 2) != 0) { int num16 = inputBuffer.ReadLeByte(); if (num16 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } int num15 = inputBuffer.ReadLeByte(); if (num15 < 0) { throw new EndOfStreamException("EOS reading GZIP header"); } num16 = (num16 << 8) | num15; if (num16 != (((int)crc32.Value) & 0xffff)) { throw new GZipException("Header CRC value mismatch"); } } readGZIPHeader = true; return true; }