Beispiel #1
0
        public static int CheckSumRead(string filename, bool testDeep, out byte[] crc, out byte[] bMD5, out byte[] bSHA1, out ulong size)
        {
            bMD5 = null;
            bSHA1 = null;
            crc = null;
            size = 0;

            Stream ds;
            int errorCode = IO.FileStream.OpenFileRead(filename, out ds);
            if (errorCode != 0)
                return errorCode;

            CRC32Hash crc32 = new CRC32Hash();

            MD5 md5 = null;
            if (testDeep) md5 = MD5.Create();
            SHA1 sha1 = null;
            if (testDeep) sha1 = SHA1.Create();

            size = (ulong)ds.Length;
            long sizetogo = ds.Length;

            while (sizetogo > 0)
            {
                int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo;

                ds.Read(Buffer, 0, sizenow);
                crc32.TransformBlock(Buffer, 0, sizenow, null, 0);
                if (testDeep) md5.TransformBlock(Buffer, 0, sizenow, null, 0);
                if (testDeep) sha1.TransformBlock(Buffer, 0, sizenow, null, 0);
                sizetogo -= sizenow;
            }

            crc32.TransformFinalBlock(Buffer, 0, 0);
            if (testDeep) md5.TransformFinalBlock(Buffer, 0, 0);
            if (testDeep) sha1.TransformFinalBlock(Buffer, 0, 0);

            ds.Close();

            crc = crc32.Hash;
            if (testDeep) bMD5 = md5.Hash;
            if (testDeep) bSHA1 = sha1.Hash;

            return 0;
        }
Beispiel #2
0
            public void LocalFileCheck()
            {
                if (FileStatus != ZipReturn.ZipUntested) return;

                try
                {
                    Stream sInput = null;
                    _zipFs.Seek((long)_dataLocation, SeekOrigin.Begin);

                    switch (_compressionMethod)
                    {
                        case 8:
                            sInput = new DeflateStream(_zipFs, CompressionMode.Decompress, true);
                            break;
                        case 0:
                            sInput = _zipFs;
                            break;
                    }

                    if (sInput == null)
                    {
                        FileStatus = ZipReturn.ZipErrorGettingDataStream;
                        return;
                    }

                    CRC32Hash crc32 = new CRC32Hash();
                    MD5 lmd5 = System.Security.Cryptography.MD5.Create();
                    SHA1 lsha1 = System.Security.Cryptography.SHA1.Create();

                    ulong sizetogo = UncompressedSize;
                    if (_buffer == null)
                        _buffer = new byte[Buffersize];

                    while (sizetogo > 0)
                    {
                        int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo;
                        sInput.Read(_buffer, 0, sizenow);

                        crc32.TransformBlock(_buffer, 0, sizenow, null, 0);
                        lmd5.TransformBlock(_buffer, 0, sizenow, null, 0);
                        lsha1.TransformBlock(_buffer, 0, sizenow, null, 0);

                        sizetogo = sizetogo - (ulong)sizenow;
                    }

                    crc32.TransformFinalBlock(_buffer, 0, 0);
                    lmd5.TransformFinalBlock(_buffer, 0, 0);
                    lsha1.TransformFinalBlock(_buffer, 0, 0);

                    byte[] testcrc = crc32.Hash;
                    md5 = lmd5.Hash;
                    sha1 = lsha1.Hash;

                    if (_compressionMethod == 8)
                    {
                        sInput.Close();
                        sInput.Dispose();
                    }

                    FileStatus = ByteArrCompare(CRC, testcrc) ? ZipReturn.ZipGood : ZipReturn.ZipCRCDecodeError;
                }
                catch
                {
                    FileStatus = ZipReturn.ZipDecodeError;
                }
            }
Beispiel #3
0
        public static RvFile CheckSumRead(Stream ds, int offset)
        {
            ds.Position = 0;
            RvFile file=new RvFile();

            CRC32Hash crc32 = new CRC32Hash();
            MD5 md5 = MD5.Create();
            SHA1 sha1 = SHA1.Create();

            CRC32Hash altCrc32 = null;
            MD5 altMd5 = null;
            SHA1 altSha1 = null;
            if (offset > 0)
            {
                altCrc32 = new CRC32Hash();
                altMd5 = MD5.Create();
                altSha1 = SHA1.Create();
            }

            file.Size = (ulong)ds.Length;
            long sizetogo = ds.Length;

            // just read header into main Hash
            if (offset > 0)
            {
                int sizenow = sizetogo > offset ? offset : (int)sizetogo;

                ds.Read(Buffer, 0, sizenow);
                crc32.TransformBlock(Buffer, 0, sizenow, null, 0);
                md5.TransformBlock(Buffer, 0, sizenow, null, 0);
                sha1.TransformBlock(Buffer, 0, sizenow, null, 0);
                sizetogo -= sizenow;

            }

            while (sizetogo > 0)
            {
                int sizenow = sizetogo > Buffersize ? Buffersize : (int)sizetogo;

                ds.Read(Buffer, 0, sizenow);
                crc32.TransformBlock(Buffer, 0, sizenow, null, 0);
                md5.TransformBlock(Buffer, 0, sizenow, null, 0);
                sha1.TransformBlock(Buffer, 0, sizenow, null, 0);

                if (offset > 0)
                {
                    altCrc32.TransformBlock(Buffer, 0, sizenow, null, 0);
                    altMd5.TransformBlock(Buffer, 0, sizenow, null, 0);
                    altSha1.TransformBlock(Buffer, 0, sizenow, null, 0);
                }

                sizetogo -= sizenow;
            }

            crc32.TransformFinalBlock(Buffer, 0, 0);
            md5.TransformFinalBlock(Buffer, 0, 0);
            sha1.TransformFinalBlock(Buffer, 0, 0);
            file.CRC = crc32.Hash;
            file.MD5 = md5.Hash;
            file.SHA1 = sha1.Hash;

            if (offset > 0)
            {
                altCrc32.TransformFinalBlock(Buffer, 0, 0);
                altMd5.TransformFinalBlock(Buffer, 0, 0);
                altSha1.TransformFinalBlock(Buffer, 0, 0);
                file.AltSize = (ulong?)(ds.Length - offset);
                file.AltCRC = altCrc32.Hash;
                file.AltMD5 = altMd5.Hash;
                file.AltSHA1 = altSha1.Hash;
            }
            else
            {
                file.AltSize = null;
                file.AltCRC = null;
                file.AltSHA1 = null;
                file.AltMD5 = null;
            }
            ds.Close();

            return file;
        }
Beispiel #4
0
        public ZipReturn ReadGZip(string filename, bool deepScan)
        {
            _filename = "";
            if (!IO.File.Exists(filename))
            {
                return ZipReturn.ZipErrorFileNotFound;
            }
            _filename = filename;

            int errorCode = IO.FileStream.OpenFileRead(filename, out _zipFs);
            if (errorCode != 0)
            {
                if (errorCode == 32)
                    return ZipReturn.ZipFileLocked;
                return ZipReturn.ZipErrorOpeningFile;
            }
            BinaryReader zipBr = new BinaryReader(_zipFs);

            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();

            UInt32 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 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)
            {

                Stream sInput = new DeflateStream(_zipFs, CompressionMode.Decompress, true);

                CRC32Hash crc32 = new CRC32Hash();
                MD5 lmd5 = MD5.Create();
                SHA1 lsha1 = SHA1.Create();

                if (_buffer == null)
                    _buffer = new byte[Buffersize];

                ulong uncompressedRead = 0;

                int sizeRead = 1;
                while (sizeRead > 0)
                {
                    sizeRead = sInput.Read(_buffer, 0, Buffersize);

                    if (sizeRead > 0)
                    {
                        crc32.TransformBlock(_buffer, 0, sizeRead, null, 0);
                        lmd5.TransformBlock(_buffer, 0, sizeRead, null, 0);
                        lsha1.TransformBlock(_buffer, 0, sizeRead, null, 0);
                    }
                    uncompressedRead += (ulong)sizeRead;
                }

                crc32.TransformFinalBlock(_buffer, 0, 0);
                lmd5.TransformFinalBlock(_buffer, 0, 0);
                lsha1.TransformFinalBlock(_buffer, 0, 0);

                sInput.Close();
                sInput.Dispose();

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

                byte[] testcrc = crc32.Hash;
                if (crc != null)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        if (crc[i] == testcrc[i]) continue;
                        _zipFs.Close();
                        return ZipReturn.ZipDecodeError;
                    }
                }
                else
                    crc = testcrc;

                byte[] testmd5 = lmd5.Hash;
                if (md5Hash != null)
                {
                    for (int i = 0; i < 16; i++)
                    {
                        if (md5Hash[i] == testmd5[i]) continue;
                        _zipFs.Close();
                        return ZipReturn.ZipDecodeError;
                    }
                }
                else
                    md5Hash = testmd5;

                byte[] testsha1 = lsha1.Hash;
                if (sha1Hash != null)
                {
                    for (int i = 0; i < 20; i++)
                    {
                        if (sha1Hash[i] == testsha1[i]) continue;
                        _zipFs.Close();
                        return ZipReturn.ZipDecodeError;
                    }
                }
                else
                    sha1Hash = testsha1;

                sInput.Close();
                sInput.Dispose();

            }
            _zipFs.Position = _zipFs.Length - 8;
            byte[] gzcrc = zipBr.ReadBytes(4);
            uint 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;
                }
            }

            _zipFs.Close();

            return ZipReturn.ZipGood;
        }