Esempio n. 1
0
        public static int CheckSumRead(string filename, bool testDeep, out byte[] crc, out byte[] bMD5, out byte[] bSHA1)
        {
            bMD5  = null;
            bSHA1 = null;
            crc   = null;

            Stream    ds    = null;
            CRC32Hash crc32 = new CRC32Hash();

            MD5 md5 = null;

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

            if (testDeep)
            {
                sha1 = SHA1.Create();
            }

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

                long sizetogo = ds.Length;

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

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

                    Thread t0 = null;
                    if (sizeNext > 0)
                    {
                        t0 = new Thread(() => { ds.Read(whichBuffer ? Buffer1 : Buffer0, 0, sizeNext); });
                        t0.Start();
                    }

                    byte[] buffer = whichBuffer ? Buffer0 : Buffer1;
                    Thread t1     = new Thread(() => { crc32.TransformBlock(buffer, 0, sizebuffer, null, 0); });
                    t1.Start();
                    if (testDeep)
                    {
                        Thread t2 = new Thread(() => { md5.TransformBlock(buffer, 0, sizebuffer, null, 0); });
                        Thread t3 = new Thread(() => { sha1.TransformBlock(buffer, 0, sizebuffer, null, 0); });
                        t2.Start();
                        t3.Start();
                        t2.Join();
                        t3.Join();
                    }
                    if (t0 != null)
                    {
                        t0.Join();
                    }
                    t1.Join();

                    sizebuffer  = sizeNext;
                    sizetogo   -= sizeNext;
                    whichBuffer = !whichBuffer;
                }

                crc32.TransformFinalBlock(Buffer0, 0, 0);
                if (testDeep)
                {
                    md5.TransformFinalBlock(Buffer0, 0, 0);
                }
                if (testDeep)
                {
                    sha1.TransformFinalBlock(Buffer0, 0, 0);
                }

                ds.Close();
            }
            catch
            {
                if (ds != null)
                {
                    ds.Close();
                }

                return(0x17);
            }

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

            return(0);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public static int CheckSumRead(string filename, bool testDeep, out byte[] crc, out byte[] bMD5, out byte[] bSHA1)
        {
            bMD5  = null;
            bSHA1 = null;
            crc   = null;

            Stream    ds    = null;
            CRC32Hash crc32 = new CRC32Hash();

            MD5 md5 = null;

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

            if (testDeep)
            {
                sha1 = SHA1.Create();
            }

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

                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();
            }
            catch
            {
                if (ds != null)
                {
                    ds.Close();
                }

                return(0x17);
            }


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

            return(0);
        }
Esempio n. 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);
        }