Beispiel #1
0
        private ZipReturn ReadBody(bool deepScan)
        {
            using (BinaryReader zipBr = new BinaryReader(_zipFs, Encoding.UTF8, true))
            {
                byte ID1 = zipBr.ReadByte();
                byte ID2 = zipBr.ReadByte();

                if ((ID1 != 0x1f) || (ID2 != 0x8b))
                {
                    _zipFs.Close();
                    return(ZipReturn.ZipSignatureError);
                }

                byte CM = zipBr.ReadByte();
                if (CM != 8)
                {
                    _zipFs.Close();
                    return(ZipReturn.ZipUnsupportedCompression);
                }

                byte FLG = zipBr.ReadByte();


                uint MTime = zipBr.ReadUInt32();
                byte XFL   = zipBr.ReadByte();
                byte OS    = zipBr.ReadByte();

                //if FLG.FEXTRA set
                if ((FLG & 0x4) == 0x4)
                {
                    int    XLen  = zipBr.ReadInt16();
                    byte[] bytes = zipBr.ReadBytes(XLen);

                    if (XLen == 28)
                    {
                        md5Hash = new byte[16];
                        Array.Copy(bytes, 0, md5Hash, 0, 16);
                        crc = new byte[4];
                        Array.Copy(bytes, 16, crc, 0, 4);
                        uncompressedSize = BitConverter.ToUInt64(bytes, 20);
                    }

                    if (XLen == 77)
                    {
                        md5Hash = new byte[16];
                        Array.Copy(bytes, 0, md5Hash, 0, 16);
                        crc = new byte[4];
                        Array.Copy(bytes, 16, crc, 0, 4);
                        uncompressedSize = BitConverter.ToUInt64(bytes, 20);
                        altType          = (HeaderFileType)bytes[28];
                        altmd5Hash       = new byte[16];
                        Array.Copy(bytes, 29, altmd5Hash, 0, 16);
                        altsha1Hash = new byte[20];
                        Array.Copy(bytes, 45, altsha1Hash, 0, 20);
                        altcrc = new byte[4];
                        Array.Copy(bytes, 65, altcrc, 0, 4);
                        uncompressedAltSize = BitConverter.ToUInt64(bytes, 69);
                    }
                }

                //if FLG.FNAME set
                if ((FLG & 0x8) == 0x8)
                {
                    int    XLen  = zipBr.ReadInt16();
                    byte[] bytes = zipBr.ReadBytes(XLen);
                }

                //if FLG.FComment set
                if ((FLG & 0x10) == 0x10)
                {
                    int    XLen  = zipBr.ReadInt16();
                    byte[] bytes = zipBr.ReadBytes(XLen);
                }

                //if FLG.FHCRC set
                if ((FLG & 0x2) == 0x2)
                {
                    uint crc16 = zipBr.ReadUInt16();
                }
            }

            compressedSize = (ulong)(_zipFs.Length - _zipFs.Position) - 8;

            datapos = _zipFs.Position;
            if (deepScan)
            {
                if (Buffer0 == null)
                {
                    Buffer0 = new byte[Buffersize];
                }

                if (Buffer1 == null)
                {
                    Buffer1 = new byte[Buffersize];
                }

                Stream ds = new ZlibBaseStream(_zipFs, CompressionMode.Decompress, CompressionLevel.Default, ZlibStreamFlavor.DEFLATE, true);

                ThreadLoadBuffer lbuffer = new ThreadLoadBuffer(ds);

                ThreadCRC  crc32 = new ThreadCRC();
                ThreadMD5  md5   = new ThreadMD5();
                ThreadSHA1 sha1  = new ThreadSHA1();

                ulong uncompressedRead = 0;
                int   bufferRead       = ds.Read(Buffer0, 0, Buffersize);
                uncompressedRead += (ulong)bufferRead;
                bool whichBuffer = true;

                while (bufferRead > 0)
                {
                    // trigger the buffer loading worker
                    lbuffer.Trigger(whichBuffer ? Buffer1 : Buffer0, Buffersize);

                    byte[] buffer = whichBuffer ? Buffer0 : Buffer1;

                    // trigger the hashing workers
                    crc32.Trigger(buffer, bufferRead);
                    md5.Trigger(buffer, bufferRead);
                    sha1.Trigger(buffer, bufferRead);

                    lbuffer.Wait();
                    crc32.Wait();
                    md5.Wait();
                    sha1.Wait();

                    // setup next loop around
                    bufferRead        = lbuffer.SizeRead;
                    uncompressedRead += (ulong)bufferRead;
                    whichBuffer       = !whichBuffer;
                }

                // tell all the workers we are finished
                lbuffer.Finish();
                crc32.Finish();
                md5.Finish();
                sha1.Finish();

                // get the results
                byte[] testcrc  = crc32.Hash;
                byte[] testmd5  = md5.Hash;
                byte[] testsha1 = sha1.Hash;

                // cleanup
                lbuffer.Dispose();
                crc32.Dispose();
                md5.Dispose();
                sha1.Dispose();

                ds.Close();
                ds.Dispose();

                if (uncompressedSize != 0)
                {
                    if (uncompressedSize != uncompressedRead)
                    {
                        _zipFs.Close();
                        return(ZipReturn.ZipDecodeError);
                    }
                }
                else
                {
                    uncompressedSize = uncompressedRead;
                }

                if (crc != null)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (crc[i] == testcrc[i])
                        {
                            continue;
                        }

                        _zipFs.Close();
                        return(ZipReturn.ZipDecodeError);
                    }
                }
                else
                {
                    crc = testcrc;
                }

                if (md5Hash != null)
                {
                    for (int i = 0; i < 16; i++)
                    {
                        if (md5Hash[i] == testmd5[i])
                        {
                            continue;
                        }

                        _zipFs.Close();
                        return(ZipReturn.ZipDecodeError);
                    }
                }
                else
                {
                    md5Hash = testmd5;
                }

                if (sha1Hash != null)
                {
                    for (int i = 0; i < 20; i++)
                    {
                        if (sha1Hash[i] == testsha1[i])
                        {
                            continue;
                        }

                        _zipFs.Close();
                        return(ZipReturn.ZipDecodeError);
                    }
                }
                else
                {
                    sha1Hash = testsha1;
                }
            }

            _zipFs.Position = _zipFs.Length - 8;
            byte[] gzcrc;
            uint   gzLength;

            using (BinaryReader zipBr = new BinaryReader(_zipFs, Encoding.UTF8, true))
            {
                gzcrc    = zipBr.ReadBytes(4);
                gzLength = zipBr.ReadUInt32();
            }

            if (crc != null)
            {
                for (int i = 0; i < 4; i++)
                {
                    if (gzcrc[3 - i] == crc[i])
                    {
                        continue;
                    }

                    _zipFs.Close();
                    return(ZipReturn.ZipDecodeError);
                }
            }
            else
            {
                crc = new[] { gzcrc[3], gzcrc[2], gzcrc[1], gzcrc[0] };
            }

            if (uncompressedSize != 0)
            {
                if (gzLength != (uncompressedSize & 0xffffffff))
                {
                    _zipFs.Close();
                    return(ZipReturn.ZipDecodeError);
                }
            }

            return(ZipReturn.ZipGood);
        }
Beispiel #2
0
        public void DeepScan()
        {
            if (_buffer0 == null)
            {
                _buffer0 = new byte[Buffersize];
                _buffer1 = new byte[Buffersize];
            }

            for (int index = 0; index < _localFiles.Count; index++)
            {
                if (_localFiles[index].isDirectory || (_localFiles[index].UncompressedSize == 0))
                {
                    _localFiles[index].md5 = new byte[]
                    { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e };
                    _localFiles[index].sha1 = new byte[]
                    {
                        0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
                        0xaf, 0xd8, 0x07, 0x09
                    };
                    _localFiles[index].FileStatus = ZipReturn.ZipGood;

                    continue;
                }

                ulong     sizetogo;
                Stream    inStream;
                ZipReturn zr = ZipFileOpenReadStream(index, out inStream, out sizetogo);
                if (zr != ZipReturn.ZipGood)
                {
                    continue;
                }

                if (inStream == null)
                {
                    continue;
                }


                ThreadLoadBuffer lbuffer = new ThreadLoadBuffer(inStream);
                ThreadCRC        tcrc32  = new ThreadCRC();
                ThreadMD5        tmd5    = new ThreadMD5();
                ThreadSHA1       tsha1   = new ThreadSHA1();

                // Pre load the first buffer0
                int sizeNext = sizetogo > Buffersize ? Buffersize : (int)sizetogo;
                inStream.Read(_buffer0, 0, sizeNext);
                int sizebuffer = sizeNext;
                sizetogo -= (ulong)sizeNext;
                bool whichBuffer = true;

                while ((sizebuffer > 0) && !lbuffer.errorState)
                {
                    sizeNext = sizetogo > Buffersize ? Buffersize : (int)sizetogo;

                    if (sizeNext > 0)
                    {
                        lbuffer.Trigger(whichBuffer ? _buffer1 : _buffer0, sizeNext);
                    }

                    byte[] buffer = whichBuffer ? _buffer0 : _buffer1;
                    tcrc32.Trigger(buffer, sizebuffer);
                    tmd5.Trigger(buffer, sizebuffer);
                    tsha1.Trigger(buffer, sizebuffer);

                    if (sizeNext > 0)
                    {
                        lbuffer.Wait();
                    }
                    tcrc32.Wait();
                    tmd5.Wait();
                    tsha1.Wait();

                    sizebuffer  = sizeNext;
                    sizetogo   -= (ulong)sizeNext;
                    whichBuffer = !whichBuffer;
                }

                if (lbuffer.errorState)
                {
                    lbuffer.Dispose();
                    tcrc32.Dispose();
                    tmd5.Dispose();
                    tsha1.Dispose();
                    _localFiles[index].FileStatus = ZipReturn.ZipDecodeError;
                    continue;
                }

                lbuffer.Finish();
                tcrc32.Finish();
                tmd5.Finish();
                tsha1.Finish();

                byte[] testcrc = tcrc32.Hash;
                _localFiles[index].md5  = tmd5.Hash;
                _localFiles[index].sha1 = tsha1.Hash;

                lbuffer.Dispose();
                tcrc32.Dispose();
                tmd5.Dispose();
                tsha1.Dispose();

                _localFiles[index].FileStatus = Util.ByteArrCompare(_localFiles[index].crc, testcrc)
                    ? ZipReturn.ZipGood
                    : ZipReturn.ZipCRCDecodeError;
            }
        }