public static uint CaclulateCRC32(byte[] buffer) { using (FixedArray bufferPtr = new FixedArray(buffer)) { return(ZLib.crc32(0, bufferPtr, (uint)buffer.Length)); } }
public override void Write(byte[] buffer, int offset, int count) { pStream.Write(buffer, offset, count); using (FixedArray bufferPtr = new FixedArray(buffer)) { pCrcValue = ZLib.crc32(pCrcValue, bufferPtr[offset], (uint)count); } }
public override int Read(byte[] buffer, int offset, int count) { int readLen = pStream.Read(buffer, offset, count); using (FixedArray bufferPtr = new FixedArray(buffer)) { pCrcValue = ZLib.crc32(pCrcValue, bufferPtr[offset], (uint)readLen); } return(readLen); }
/// <summary>Reads a number of decompressed bytes into the specified byte array.</summary> /// <param name="array">The array used to store decompressed bytes.</param> /// <param name="offset">The location in the array to begin reading.</param> /// <param name="count">The number of bytes decompressed.</param> /// <returns>The number of bytes that were decompressed into the byte array. If the end of the stream has been reached, zero or the number of bytes read is returned.</returns> public override int Read(byte[] buffer, int offset, int count) { if (pCompMode == CompressionMode.Compress) { throw new NotSupportedException("Can't read on a compress stream!"); } int readLen = 0; if (pWorkDataPos != -1) { using (FixedArray workDataPtr = new FixedArray(pWorkData)) using (FixedArray bufferPtr = new FixedArray(buffer)) { pZstream.next_in = workDataPtr[pWorkDataPos]; pZstream.next_out = bufferPtr[offset]; pZstream.avail_out = (uint)count; while (pZstream.avail_out != 0) { if (pZstream.avail_in == 0) { pWorkDataPos = 0; pZstream.next_in = workDataPtr; pZstream.avail_in = (uint)pStream.Read(pWorkData, 0, WORK_DATA_SIZE); pBytesIn += pZstream.avail_in; } uint inCount = pZstream.avail_in; uint outCount = pZstream.avail_out; int zlibError = ZLib.inflate(ref pZstream, ZLibFlush.NoFlush); // flush method for inflate has no effect pWorkDataPos += (int)(inCount - pZstream.avail_in); readLen += (int)(outCount - pZstream.avail_out); if (zlibError == ZLibReturnCode.StreamEnd) { pWorkDataPos = -1; // magic for StreamEnd break; } else if (zlibError != ZLibReturnCode.Ok) { pSuccess = false; throw new ZLibException(zlibError, pZstream.lasterrormsg); } } // pCrcValue = crc32(pCrcValue, &bufferPtr[offset], (uint)readLen); pBytesOut += readLen; } } return(readLen); }
// Finished, free the resources used. private void FreeUnmanagedResources() { if (this.pCompMode == CompressionMode.Compress) { ZLib.deflateEnd(ref pZstream); } else { ZLib.inflateEnd(ref pZstream); } }
/// <summary>This property is not supported and always throws a NotSupportedException.</summary> /// <param name="array">The array used to store compressed bytes.</param> /// <param name="offset">The location in the array to begin reading.</param> /// <param name="count">The number of bytes compressed.</param> public override void Write(byte[] buffer, int offset, int count) { if (pCompMode == CompressionMode.Decompress) { throw new NotSupportedException("Can't write on a decompression stream!"); } pBytesIn += count; using (FixedArray writePtr = new FixedArray(pWorkData)) using (FixedArray bufferPtr = new FixedArray(buffer)) { pZstream.next_in = bufferPtr[offset]; pZstream.avail_in = (uint)count; pZstream.next_out = writePtr[pWorkDataPos]; pZstream.avail_out = (uint)(WORK_DATA_SIZE - pWorkDataPos); // pCrcValue = crc32(pCrcValue, &bufferPtr[offset], (uint)count); while (pZstream.avail_in != 0) { if (pZstream.avail_out == 0) { //rar logikk, men det betyr vel bare at den kun skriver hvis buffer ble fyllt helt, //dvs halvfyllt buffer vil kun skrives ved flush pStream.Write(pWorkData, 0, (int)WORK_DATA_SIZE); pBytesOut += WORK_DATA_SIZE; pWorkDataPos = 0; pZstream.next_out = writePtr; pZstream.avail_out = WORK_DATA_SIZE; } uint outCount = pZstream.avail_out; int zlibError = ZLib.deflate(ref pZstream, ZLibFlush.NoFlush); pWorkDataPos += (int)(outCount - pZstream.avail_out); if (zlibError != ZLibReturnCode.Ok) { pSuccess = false; throw new ZLibException(zlibError, pZstream.lasterrormsg); } } } }
/// <summary>Flushes the contents of the internal buffer of the current GZipStream object to the underlying stream.</summary> public override void Flush() { if (pCompMode == CompressionMode.Decompress) { throw new NotSupportedException("Can't flush a decompression stream."); } using (FixedArray workDataPtr = new FixedArray(pWorkData)) { pZstream.next_in = IntPtr.Zero; pZstream.avail_in = 0; pZstream.next_out = workDataPtr[pWorkDataPos]; pZstream.avail_out = (uint)(WORK_DATA_SIZE - pWorkDataPos); int zlibError = ZLibReturnCode.Ok; while (zlibError != ZLibReturnCode.StreamEnd) { if (pZstream.avail_out != 0) { uint outCount = pZstream.avail_out; zlibError = ZLib.deflate(ref pZstream, ZLibFlush.Finish); pWorkDataPos += (int)(outCount - pZstream.avail_out); if (zlibError == ZLibReturnCode.StreamEnd) { //ok. will break loop } else if (zlibError != ZLibReturnCode.Ok) { pSuccess = false; throw new ZLibException(zlibError, pZstream.lasterrormsg); } } pStream.Write(pWorkData, 0, pWorkDataPos); pBytesOut += pWorkDataPos; pWorkDataPos = 0; pZstream.next_out = workDataPtr; pZstream.avail_out = WORK_DATA_SIZE; } } this.pStream.Flush(); }
public DeflateStream(Stream stream, CompressionMode compMode, CompressionLevel level, bool leaveOpen) { this.pLeaveOpen = leaveOpen; this.pStream = stream; this.pCompMode = compMode; int ret; if (this.pCompMode == CompressionMode.Compress) { ret = ZLib.deflateInit(ref pZstream, level, this.WriteType); } else { ret = ZLib.inflateInit(ref pZstream, this.OpenType); } if (ret != ZLibReturnCode.Ok) { throw new ZLibException(ret, pZstream.lasterrormsg); } pSuccess = true; }