Exemplo n.º 1
0
 public static uint CaclulateCRC32(byte[] buffer)
 {
     using (FixedArray bufferPtr = new FixedArray(buffer))
     {
         return(ZLib.crc32(0, bufferPtr, (uint)buffer.Length));
     }
 }
Exemplo n.º 2
0
 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);
     }
 }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        /// <summary>Gets and sets the default compresion level for zip file entries.  See <see cref="CompressionMethod"/> for a partial list of values.</summary>
        //public int Level
        //{
        //    get { return _level; }
        //    set
        //    {
        //        if (value < -1 || value > 9)
        //        {
        //            throw new ArgumentOutOfRangeException("Level", value, "Level value must be between -1 and 9.");
        //        }
        //        _level = value;
        //    }
        //}

        ///// <summary>Gets and sets the default compresion method for zip file entries.  See <see cref="CompressionMethod"/> for a list of possible values.</summary>
        //public CompressionMethod Method
        //{
        //    get { return _method; }
        //    set { _method = value; }
        //}


        /// <summary>Compress a block of bytes from the given buffer and writes them into the current zip entry.</summary>
        /// <param name="buffer">The array to read data from.</param>
        /// <param name="index">The byte offset in <paramref name="buffer"/> at which to begin reading.</param>
        /// <param name="count">The maximum number of bytes to write.</param>
        public void Write(byte[] buffer, int index, int count)
        {
            using (FixedArray fixedBuffer = new FixedArray(buffer))
            {
                int result = Minizip.zipWriteInFileInZip(_handle, fixedBuffer[index], (uint)count);
                if (result < 0)
                {
                    throw new ZipException("Write error.", result);
                }
            }
        }
Exemplo n.º 5
0
        /// <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);
        }
Exemplo n.º 6
0
 /// <summary>Uncompress a block of bytes from the current zip entry and writes the data in a given buffer.</summary>
 /// <param name="buffer">The array to write data into.</param>
 /// <param name="index">The byte offset in <paramref name="buffer"/> at which to begin writing.</param>
 /// <param name="count">The maximum number of bytes to read.</param>
 public int Read(byte[] buffer, int index, int count)
 {
     using (FixedArray fixedBuff = new FixedArray(buffer))
     {
         int bytesRead = Minizip.unzReadCurrentFile(_handle, fixedBuff[index], (uint)count);
         if (bytesRead < 0)
         {
             throw new ZipException("Error reading zip entry.", bytesRead);
         }
         return(bytesRead);
     }
 }
Exemplo n.º 7
0
        /// <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);
                        }
                    }
                }
        }
Exemplo n.º 8
0
        /// <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();
        }