Ejemplo 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);
        }
Ejemplo n.º 2
0
        public MTModel(BinaryReader Reader, MTMaterials MRLData, MTShaderEffects Shader) : this()
        {
            string Magic = Reader.ReadPaddedString(4);

            ushort Version               = Reader.ReadUInt16();
            ushort BonesCount            = Reader.ReadUInt16();
            ushort MeshesCount           = Reader.ReadUInt16();
            ushort MaterialsCount        = Reader.ReadUInt16();
            uint   TotalVerticesCount    = Reader.ReadUInt32();
            uint   TotalIndicesCount     = Reader.ReadUInt32();
            uint   TotalTrianglesCount   = Reader.ReadUInt32();
            uint   VerticesBufferLength  = Reader.ReadUInt32();
            uint   HeaderPadding1c       = Reader.ReadUInt32(); //?
            uint   MeshGroupsCount       = Reader.ReadUInt32();
            uint   BoneIndicesCount      = Reader.ReadUInt32();
            uint   SkeletonAddress       = Reader.ReadUInt32();
            uint   MeshGroupsAddr        = Reader.ReadUInt32();
            uint   MaterialNamesAddress  = Reader.ReadUInt32();
            uint   MeshesAddress         = Reader.ReadUInt32();
            uint   VerticesBufferAddress = Reader.ReadUInt32();
            uint   IndicesBufferAddress  = Reader.ReadUInt32();
            uint   ModelFileLength       = Reader.ReadUInt32();

            BoundingSphere = Reader.ReadVector4();
            BoundingBoxMin = Reader.ReadVector4();
            BoundingBoxMax = Reader.ReadVector4();

            string[] MaterialNames = new string[MaterialsCount];

            Reader.BaseStream.Seek(MaterialNamesAddress, SeekOrigin.Begin);

            for (int Index = 0; Index < MaterialsCount; Index++)
            {
                MaterialNames[Index] = Reader.ReadPaddedString(0x80);
            }

            for (int Index = 0; Index < MeshesCount; Index++)
            {
                Reader.BaseStream.Seek(MeshesAddress + Index * 0x30, SeekOrigin.Begin);

                MTMesh Mesh = new MTMesh(
                    Reader,
                    Shader,
                    VerticesBufferAddress,
                    IndicesBufferAddress);

                string MaterialName = MaterialNames[Mesh.MaterialIndex];

                uint MaterialHash = CRC32Hash.Hash(MaterialName);

                int MaterialIndex = Materials.FindIndex(x => x.NameHash == MaterialHash);

                if (MaterialIndex == -1)
                {
                    MTMaterial Mat = MRLData.Materials.FirstOrDefault(x => x.NameHash == MaterialHash);

                    if (Mat != null)
                    {
                        Mat.Name = MaterialName;

                        MaterialIndex = Materials.Count;

                        Materials.Add(Mat);
                    }
                    else
                    {
                        MaterialIndex = 0;
                    }
                }

                Mesh.MaterialIndex = (uint)MaterialIndex;

                Meshes.Add(Mesh);
            }

            for (int Index = 0; Index < BonesCount; Index++)
            {
                Reader.BaseStream.Seek(SkeletonAddress + Index * 0x18, SeekOrigin.Begin);

                sbyte BoneIndex = Reader.ReadSByte();
                sbyte Parent    = Reader.ReadSByte();
                sbyte Opposite  = Reader.ReadSByte();
                byte  Padding   = Reader.ReadByte();

                float ChildDistance  = Reader.ReadSingle();
                float ParentDistance = Reader.ReadSingle();

                Vector3 Position = Reader.ReadVector3();

                Skeleton.Add(new MTBone()
                {
                    ParentIndex    = Parent,
                    OppositeIndex  = Opposite,
                    ChildDistance  = ChildDistance,
                    ParentDistance = ParentDistance,
                    Position       = Position
                });
            }

            for (int Index = 0; Index < BonesCount; Index++)
            {
                Skeleton[Index].LocalTransform = Reader.ReadMatrix4x4RH();
            }

            for (int Index = 0; Index < BonesCount; Index++)
            {
                Skeleton[Index].WorldTransform = Reader.ReadMatrix4x4RH();
            }

            Reader.BaseStream.Seek(0x100, SeekOrigin.Current);

            BoneIndicesGroups = new byte[BoneIndicesCount][];

            for (int i = 0; i < BoneIndicesCount; i++)
            {
                int Count = Reader.ReadInt32();

                BoneIndicesGroups[i] = new byte[Count];

                for (int j = 0; j < Count; j++)
                {
                    BoneIndicesGroups[i][j] = Reader.ReadByte();
                }

                Reader.BaseStream.Seek(0x18 - Count, SeekOrigin.Current);
            }
        }
Ejemplo n.º 3
0
        // This Function returns:
        // Good            : Everything Worked Correctly
        // RescanNeeded     : Something unexpectidly changed in the files, so Stop prompt user to rescan.
        // LogicError       : This Should never happen and is a logic problem in the code
        // FileSystemError  : Something is wrong with the files


        /// <summary>
        /// Performs the ROMVault File Copy, with the source and destination being files or zipped files
        /// </summary>
        /// <param name="fileIn">This is the file being copied, it may be a zipped file or a regular file system file</param>
        /// <param name="zipFileOut">This is the zip file that is being writen to, if it is null a new zip file will be made if we are coping to a zip</param>
        /// <param name="zipFilenameOut">This is the name of the .zip file to be made that will be created in zipFileOut</param>
        /// <param name="fileOut">This is the actual output filename</param>
        /// <param name="forceRaw">if true then we will do a raw copy, this is so that we can copy corrupt zips</param>
        /// <param name="error">This is the returned error message if this copy fails</param>
        /// <param name="foundFile">If we are SHA1/MD5 checking the source file for the first time, and it is different from what we expected the correct values for this file are returned in foundFile</param>
        /// <returns>ReturnCode.Good is the valid return code otherwire we have an error</returns>

        public static ReturnCode CopyFile(RvFile fileIn, ref ZipFile zipFileOut, string zipFilenameOut, RvFile fileOut, bool forceRaw, out string error, out RvFile foundFile)
        {
            foundFile = null;
            error     = "";

            if (_buffer == null)
            {
                _buffer = new byte[BufferSize];
            }

            bool rawCopy = RawCopy(fileIn, fileOut, forceRaw);

            ulong  streamSize        = 0;
            ushort compressionMethod = 8;
            bool   sourceTrrntzip    = false;


            ZipFile zipFileIn = null;

            System.IO.Stream readStream = null;


            bool isZeroLengthFile = DBHelper.IsZeroLengthFile(fileOut);

            if (!isZeroLengthFile)
            {
                #region check that the in and out file match
                if (fileOut.FileStatusIs(FileStatus.SizeFromDAT) && fileOut.Size != null && fileIn.Size != fileOut.Size)
                {
                    error = "Source and destination Size does not match. Logic Error.";
                    return(ReturnCode.LogicError);
                }
                if (fileOut.FileStatusIs(FileStatus.CRCFromDAT) && fileOut.CRC != null && !ArrByte.bCompare(fileIn.CRC, fileOut.CRC))
                {
                    error = "Source and destination CRC does not match. Logic Error.";
                    return(ReturnCode.LogicError);
                }

                if (fileOut.FileStatusIs(FileStatus.SHA1FromDAT) && fileIn.FileStatusIs(FileStatus.SHA1Verified))
                {
                    if (fileIn.SHA1 != null && fileOut.SHA1 != null && !ArrByte.bCompare(fileIn.SHA1, fileOut.SHA1))
                    {
                        error = "Source and destination SHA1 does not match. Logic Error.";
                        return(ReturnCode.LogicError);
                    }
                }
                if (fileOut.FileStatusIs(FileStatus.MD5CHDFromDAT) && fileIn.FileStatusIs(FileStatus.MD5Verified))
                {
                    if (fileIn.MD5 != null && fileOut.MD5 != null && !ArrByte.bCompare(fileIn.MD5, fileOut.MD5))
                    {
                        error = "Source and destination SHA1 does not match. Logic Error.";
                        return(ReturnCode.LogicError);
                    }
                }

                #endregion

                #region Find and Check/Open Input Files

                if (fileIn.FileType == FileType.ZipFile) // Input is a ZipFile
                {
                    RvDir zZipFileIn = fileIn.Parent;
                    if (zZipFileIn.FileType != FileType.Zip)
                    {
                        error = "Zip File Open but Source File is not a zip, Logic Error.";
                        return(ReturnCode.LogicError);
                    }

                    string fileNameIn = zZipFileIn.FullName;

                    sourceTrrntzip = (zZipFileIn.ZipStatus & ZipStatus.TrrntZip) == ZipStatus.TrrntZip;

                    //if (zZipFileIn.ZipFileType == RvZip.ZipType.Zip)
                    //{
                    zipFileIn = new ZipFile();

                    ZipReturn zr1;
                    if (fileIn.ZipFileHeaderPosition != null)
                    {
                        zr1 = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.TimeStamp, false);
                    }
                    else
                    {
                        zr1 = zipFileIn.ZipFileOpen(fileNameIn, zZipFileIn.TimeStamp, true);
                    }
                    switch (zr1)
                    {
                    case ZipReturn.ZipGood:
                        break;

                    case ZipReturn.ZipErrorFileNotFound:
                        error = "File not found, Rescan required before fixing " + fileIn.Name;
                        return(ReturnCode.FileSystemError);

                    case ZipReturn.ZipErrorTimeStamp:
                        error = "File has changed, Rescan required before fixing " + fileIn.Name;
                        return(ReturnCode.FileSystemError);

                    default:
                        error = "Error Open Zip" + zr1 + ", with File " + fileIn.DatFullName;
                        return(ReturnCode.FileSystemError);
                    }
                    if (fileIn.ZipFileHeaderPosition != null)
                    {
                        zipFileIn.ZipFileOpenReadStreamQuick((ulong)fileIn.ZipFileHeaderPosition, rawCopy, out readStream, out streamSize, out compressionMethod);
                    }
                    else
                    {
                        zipFileIn.ZipFileOpenReadStream(fileIn.ZipFileIndex, rawCopy, out readStream, out streamSize, out compressionMethod);
                    }
                }
                else // Input is a regular file
                {
                    string fileNameIn = fileIn.FullName;
                    if (!IO.File.Exists(fileNameIn))
                    {
                        error = "Rescan needed, File Changed :" + fileNameIn;
                        return(ReturnCode.RescanNeeded);
                    }
                    IO.FileInfo fileInInfo = new IO.FileInfo(fileNameIn);
                    if (fileInInfo.LastWriteTime != fileIn.TimeStamp)
                    {
                        error = "Rescan needed, File Changed :" + fileNameIn;
                        return(ReturnCode.RescanNeeded);
                    }
                    int errorCode = IO.FileStream.OpenFileRead(fileNameIn, out readStream);
                    if (errorCode != 0)
                    {
                        error = new Win32Exception(errorCode).Message + ". " + fileNameIn;
                        return(ReturnCode.FileSystemError);
                    }
                    if (fileIn.Size == null)
                    {
                        error = "Null File Size found in Fixing File :" + fileNameIn;
                        return(ReturnCode.LogicError);
                    }
                    streamSize = (ulong)fileIn.Size;
                    if ((ulong)readStream.Length != streamSize)
                    {
                        error = "Rescan needed, File Length Changed :" + fileNameIn;
                        return(ReturnCode.RescanNeeded);
                    }
                }
                #endregion
            }
            else
            {
                sourceTrrntzip = true;
            }

            if (!rawCopy && (Settings.FixLevel == eFixLevel.TrrntZipLevel1 || Settings.FixLevel == eFixLevel.TrrntZipLevel2 || Settings.FixLevel == eFixLevel.TrrntZipLevel3))
            {
                compressionMethod = 8;
            }

            #region Find and Check/Open Output Files

            System.IO.Stream writeStream;
            if (fileOut.FileType == FileType.ZipFile)
            {
                // if ZipFileOut == null then we have not open the output zip yet so open it from writing.
                if (zipFileOut == null)
                {
                    if (IO.Path.GetFileName(zipFilenameOut) == "__ROMVault.tmp")
                    {
                        if (IO.File.Exists(zipFilenameOut))
                        {
                            IO.File.Delete(zipFilenameOut);
                        }
                    }
                    else if (IO.File.Exists(zipFilenameOut))
                    {
                        error = "Rescan needed, File Changed :" + zipFilenameOut;
                        return(ReturnCode.RescanNeeded);
                    }

                    zipFileOut = new ZipFile();
                    ZipReturn zrf = zipFileOut.ZipFileCreate(zipFilenameOut);
                    if (zrf != ZipReturn.ZipGood)
                    {
                        error = "Error Opening Write Stream " + zrf;
                        return(ReturnCode.FileSystemError);
                    }
                }
                else
                {
                    if (zipFileOut.ZipOpen != ZipOpenType.OpenWrite)
                    {
                        error = "Output Zip File is not set to OpenWrite, Logic Error.";
                        return(ReturnCode.LogicError);
                    }

                    if (zipFileOut.ZipFilename != (new IO.FileInfo(zipFilenameOut).FullName))
                    {
                        error = "Output Zip file has changed name from " + zipFileOut.ZipFilename + " to " + zipFilenameOut + ". Logic Error";
                        return(ReturnCode.LogicError);
                    }
                }

                if (fileIn.Size == null)
                {
                    error = "Null File Size found in Fixing File :" + fileIn.FullName;
                    return(ReturnCode.LogicError);
                }
                ZipReturn zr = zipFileOut.ZipFileOpenWriteStream(rawCopy, sourceTrrntzip, fileOut.Name, (ulong)fileIn.Size, compressionMethod, out writeStream);
                if (zr != ZipReturn.ZipGood)
                {
                    error = "Error Opening Write Stream " + zr;
                    return(ReturnCode.FileSystemError);
                }
            }
            else
            {
                if (IO.File.Exists(zipFilenameOut) && fileOut.GotStatus != GotStatus.Corrupt)
                {
                    error = "Rescan needed, File Changed :" + zipFilenameOut;
                    return(ReturnCode.RescanNeeded);
                }
                int errorCode = IO.FileStream.OpenFileWrite(zipFilenameOut, out writeStream);
                if (errorCode != 0)
                {
                    error = new Win32Exception(errorCode).Message + ". " + zipFilenameOut;
                    return(ReturnCode.FileSystemError);
                }
            }
            #endregion

            byte[] bCRC;
            byte[] bMD5  = null;
            byte[] bSHA1 = null;
            if (!isZeroLengthFile)
            {
                #region Do Data Tranfer

                CRC32Hash crc32 = null;
                MD5       md5   = null;
                SHA1      sha1  = null;

                if (!rawCopy)
                {
                    crc32 = new CRC32Hash();
                    md5   = MD5.Create();
                    sha1  = SHA1.Create();
                }

                ulong sizetogo = streamSize;

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

                    try
                    {
                        readStream.Read(_buffer, 0, sizenow);
                    }
                    catch (ZlibException)
                    {
                        if (fileIn.FileType == FileType.ZipFile && zipFileIn != null)
                        {
                            ZipReturn zr = zipFileIn.ZipFileCloseReadStream();
                            if (zr != ZipReturn.ZipGood)
                            {
                                error = "Error Closing " + zr + " Stream :" + zipFileIn.Filename(fileIn.ReportIndex);
                                return(ReturnCode.FileSystemError);
                            }
                            zipFileIn.ZipFileClose();
                        }
                        else
                        {
                            readStream.Close();
                        }

                        if (fileOut.FileType == FileType.ZipFile)
                        {
                            ZipReturn zr = zipFileOut.ZipFileCloseWriteStream(new byte[] { 0, 0, 0, 0 });
                            if (zr != ZipReturn.ZipGood)
                            {
                                error = "Error Closing Stream " + zr;
                                return(ReturnCode.FileSystemError);
                            }
                            zipFileOut.ZipFileRollBack();
                        }
                        else
                        {
                            writeStream.Flush();
                            writeStream.Close();
                            IO.File.Delete(zipFilenameOut);
                        }

                        error = "Error in Data Stream";
                        return(ReturnCode.SourceCRCCheckSumError);
                    }
                    catch (Exception e)
                    {
                        error = "Error reading Source File " + e.Message;
                        return(ReturnCode.FileSystemError);
                    }

                    if (!rawCopy)
                    {
                        crc32.TransformBlock(_buffer, 0, sizenow, null, 0);
                        md5.TransformBlock(_buffer, 0, sizenow, null, 0);
                        sha1.TransformBlock(_buffer, 0, sizenow, null, 0);
                    }
                    try
                    {
                        writeStream.Write(_buffer, 0, sizenow);
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine(e.Message);
                        error = "Error writing out file. " + Environment.NewLine + e.Message;
                        return(ReturnCode.FileSystemError);
                    }
                    sizetogo = sizetogo - (ulong)sizenow;
                }
                writeStream.Flush();

                #endregion

                #region Collect Checksums


                // if we did a full copy then we just calculated all the checksums while doing the copy
                if (!rawCopy)
                {
                    crc32.TransformFinalBlock(_buffer, 0, 0);
                    md5.TransformFinalBlock(_buffer, 0, 0);
                    sha1.TransformFinalBlock(_buffer, 0, 0);

                    bCRC  = crc32.Hash;
                    bMD5  = md5.Hash;
                    bSHA1 = sha1.Hash;
                }
                // if we raw copied and the source file has been FileChecked then we can trust the checksums in the source file
                else
                {
                    bCRC = ArrByte.Copy(fileIn.CRC);
                    if (fileIn.FileStatusIs(FileStatus.MD5Verified))
                    {
                        bMD5 = ArrByte.Copy(fileIn.MD5);
                    }
                    if (fileIn.FileStatusIs(FileStatus.SHA1Verified))
                    {
                        bSHA1 = ArrByte.Copy(fileIn.SHA1);
                    }
                }

                #endregion

                #region close the ReadStream

                if (fileIn.FileType == FileType.ZipFile && zipFileIn != null)
                {
                    ZipReturn zr = zipFileIn.ZipFileCloseReadStream();
                    if (zr != ZipReturn.ZipGood)
                    {
                        error = "Error Closing " + zr + " Stream :" + zipFileIn.Filename(fileIn.ReportIndex);
                        return(ReturnCode.FileSystemError);
                    }
                    zipFileIn.ZipFileClose();
                }
                else
                {
                    readStream.Close();

                    //if (IO.File.Exists(tmpFilename))
                    //    IO.File.Delete(tmpFilename);
                }

                #endregion
            }
            else
            {
                // Zero Length File (Directory in a Zip)
                if (fileOut.FileType == FileType.ZipFile)
                {
                    zipFileOut.ZipFileAddDirectory();
                }
                bCRC  = VarFix.CleanMD5SHA1("00000000", 8);
                bMD5  = VarFix.CleanMD5SHA1("d41d8cd98f00b204e9800998ecf8427e", 32);
                bSHA1 = VarFix.CleanMD5SHA1("da39a3ee5e6b4b0d3255bfef95601890afd80709", 40);
            }


            #region close the WriteStream
            if (fileOut.FileType == FileType.ZipFile)
            {
                ZipReturn zr = zipFileOut.ZipFileCloseWriteStream(bCRC);
                if (zr != ZipReturn.ZipGood)
                {
                    error = "Error Closing Stream " + zr;
                    return(ReturnCode.FileSystemError);
                }
                fileOut.ZipFileIndex          = zipFileOut.LocalFilesCount() - 1;
                fileOut.ZipFileHeaderPosition = zipFileOut.LocalHeader(fileOut.ZipFileIndex);
            }
            else
            {
                writeStream.Flush();
                writeStream.Close();
                IO.FileInfo fi = new IO.FileInfo(zipFilenameOut);
                fileOut.TimeStamp = fi.LastWriteTime;
            }
            #endregion

            if (!isZeroLengthFile)
            {
                if (!rawCopy)
                {
                    if (!ArrByte.bCompare(bCRC, fileIn.CRC))
                    {
                        fileIn.GotStatus = GotStatus.Corrupt;
                        error            = "Source CRC does not match Source Data stream, corrupt Zip";

                        if (fileOut.FileType == FileType.ZipFile)
                        {
                            zipFileOut.ZipFileRollBack();
                        }
                        else
                        {
                            IO.File.Delete(zipFilenameOut);
                        }

                        return(ReturnCode.SourceCRCCheckSumError);
                    }

                    fileIn.FileStatusSet(FileStatus.CRCVerified | FileStatus.SizeVerified);

                    bool sourceFailed = false;

                    // check to see if we have a MD5 from the DAT file
                    if (fileIn.FileStatusIs(FileStatus.MD5FromDAT))
                    {
                        if (fileIn.MD5 == null)
                        {
                            error = "Should have an filein MD5 from Dat but not found. Logic Error.";
                            return(ReturnCode.LogicError);
                        }

                        if (!ArrByte.bCompare(fileIn.MD5, bMD5))
                        {
                            sourceFailed = true;
                        }
                        else
                        {
                            fileIn.FileStatusSet(FileStatus.MD5Verified);
                        }
                    }
                    // check to see if we have an MD5 (not from the DAT) so must be from previously scanning this file.
                    else if (fileIn.MD5 != null)
                    {
                        if (!ArrByte.bCompare(fileIn.MD5, bMD5))
                        {
                            // if we had an MD5 from a preview scan and it now does not match, something has gone really bad.
                            error = "The MD5 found does not match a previously scanned MD5, this should not happen, unless something got corrupt.";
                            return(ReturnCode.LogicError);
                        }
                    }
                    else // (FileIn.MD5 == null)
                    {
                        fileIn.MD5 = bMD5;
                        fileIn.FileStatusSet(FileStatus.MD5Verified);
                    }


                    // check to see if we have a SHA1 from the DAT file
                    if (fileIn.FileStatusIs(FileStatus.SHA1FromDAT))
                    {
                        if (fileIn.SHA1 == null)
                        {
                            error = "Should have an filein SHA1 from Dat but not found. Logic Error.";
                            return(ReturnCode.LogicError);
                        }

                        if (!ArrByte.bCompare(fileIn.SHA1, bSHA1))
                        {
                            sourceFailed = true;
                        }
                        else
                        {
                            fileIn.FileStatusSet(FileStatus.SHA1Verified);
                        }
                    }
                    // check to see if we have an SHA1 (not from the DAT) so must be from previously scanning this file.
                    else if (fileIn.SHA1 != null)
                    {
                        if (!ArrByte.bCompare(fileIn.SHA1, bSHA1))
                        {
                            // if we had an SHA1 from a preview scan and it now does not match, something has gone really bad.
                            error = "The SHA1 found does not match a previously scanned SHA1, this should not happen, unless something got corrupt.";
                            return(ReturnCode.LogicError);
                        }
                    }
                    else // (FileIn.SHA1 == null)
                    {
                        fileIn.SHA1 = bSHA1;
                        fileIn.FileStatusSet(FileStatus.SHA1Verified);
                    }



                    if (sourceFailed)
                    {
                        if (fileIn.FileType == FileType.ZipFile)
                        {
                            RvFile tZFile = new RvFile(FileType.ZipFile);
                            foundFile                    = tZFile;
                            tZFile.ZipFileIndex          = fileIn.ZipFileIndex;
                            tZFile.ZipFileHeaderPosition = fileIn.ZipFileHeaderPosition;
                        }
                        else
                        {
                            foundFile = new RvFile(fileIn.FileType);
                        }

                        foundFile.Name      = fileIn.Name;
                        foundFile.Size      = fileIn.Size;
                        foundFile.CRC       = bCRC;
                        foundFile.MD5       = bMD5;
                        foundFile.SHA1      = bSHA1;
                        foundFile.TimeStamp = fileIn.TimeStamp;
                        foundFile.SetStatus(DatStatus.NotInDat, GotStatus.Got);

                        foundFile.FileStatusSet(FileStatus.SizeVerified | FileStatus.CRCVerified | FileStatus.MD5Verified | FileStatus.SHA1Verified);

                        if (fileOut.FileType == FileType.ZipFile)
                        {
                            zipFileOut.ZipFileRollBack();
                        }
                        else
                        {
                            IO.File.Delete(zipFilenameOut);
                        }

                        return(ReturnCode.SourceCheckSumError);
                    }
                }
            }

            if (fileOut.FileType == FileType.ZipFile)
            {
                fileOut.FileStatusSet(FileStatus.SizeFromHeader | FileStatus.CRCFromHeader);
            }

            if (fileOut.FileStatusIs(FileStatus.CRCFromDAT) && fileOut.CRC != null && !ArrByte.bCompare(fileOut.CRC, bCRC))
            {
                //Rollback the file
                if (fileOut.FileType == FileType.ZipFile)
                {
                    zipFileOut.ZipFileRollBack();
                }
                else
                {
                    IO.File.Delete(zipFilenameOut);
                }

                return(ReturnCode.DestinationCheckSumError);
            }

            fileOut.CRC = bCRC;
            if (!rawCopy || fileIn.FileStatusIs(FileStatus.CRCVerified))
            {
                fileOut.FileStatusSet(FileStatus.CRCVerified);
            }


            if (bSHA1 != null)
            {
                if (fileOut.FileStatusIs(FileStatus.SHA1FromDAT) && fileOut.SHA1 != null && !ArrByte.bCompare(fileOut.SHA1, bSHA1))
                {
                    //Rollback the file
                    if (fileOut.FileType == FileType.ZipFile)
                    {
                        zipFileOut.ZipFileRollBack();
                    }
                    else
                    {
                        IO.File.Delete(zipFilenameOut);
                    }

                    return(ReturnCode.DestinationCheckSumError);
                }

                fileOut.SHA1 = bSHA1;
                fileOut.FileStatusSet(FileStatus.SHA1Verified);
            }

            if (bMD5 != null)
            {
                if (fileOut.FileStatusIs(FileStatus.MD5FromDAT) && fileOut.MD5 != null && !ArrByte.bCompare(fileOut.MD5, bMD5))
                {
                    //Rollback the file
                    if (fileOut.FileType == FileType.ZipFile)
                    {
                        zipFileOut.ZipFileRollBack();
                    }
                    else
                    {
                        IO.File.Delete(zipFilenameOut);
                    }

                    return(ReturnCode.DestinationCheckSumError);
                }
                fileOut.MD5 = bMD5;
                fileOut.FileStatusSet(FileStatus.MD5Verified);
            }

            if (fileIn.Size != null)
            {
                fileOut.Size = fileIn.Size;
                fileOut.FileStatusSet(FileStatus.SizeVerified);
            }



            if (fileIn.GotStatus == GotStatus.Corrupt)
            {
                fileOut.GotStatus = GotStatus.Corrupt;
            }
            else
            {
                fileOut.GotStatus = GotStatus.Got; // Changes RepStatus to Correct
            }
            fileOut.FileStatusSet(FileStatus.SizeVerified);

            if (fileOut.SHA1CHD == null && fileIn.SHA1CHD != null)
            {
                fileOut.SHA1CHD = fileIn.SHA1CHD;
            }
            if (fileOut.MD5CHD == null && fileIn.MD5CHD != null)
            {
                fileOut.MD5CHD = fileIn.MD5CHD;
            }


            fileOut.CHDVersion = fileIn.CHDVersion;

            fileOut.FileStatusSet(FileStatus.SHA1CHDFromHeader | FileStatus.MD5CHDFromHeader | FileStatus.SHA1CHDVerified | FileStatus.MD5CHDVerified, fileIn);


            return(ReturnCode.Good);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
        public MTShaderEffects(BinaryReader Reader)
        {
            string Magic = Reader.ReadNullTerminatedString(); //MFX

            Reader.BaseStream.Seek(8, SeekOrigin.Current);    //Unknown stuff

            uint DescriptorsCount = Reader.ReadUInt32();
            uint FragShaderCount  = Reader.ReadUInt32();
            uint VtxShaderCount   = Reader.ReadUInt32();
            uint FragShaderAddr   = Reader.ReadUInt32();
            uint VtxShaderAddr    = Reader.ReadUInt32();
            uint StringsTblAddr   = Reader.ReadUInt32();
            uint VtxProgramAddr   = Reader.ReadUInt32();

            long VtxFormatsAddr = Reader.BaseStream.Position;

            for (uint i = 0; i < DescriptorsCount; i++)
            {
                Reader.BaseStream.Seek(VtxFormatsAddr + i * 4, SeekOrigin.Begin);
                Reader.BaseStream.Seek(Reader.ReadUInt32(), SeekOrigin.Begin);

                if (Reader.BaseStream.Position == 0)
                {
                    continue;
                }

                string DescName = GetName(Reader, StringsTblAddr);
                string TypeName = GetName(Reader, StringsTblAddr);

                ushort DescType   = Reader.ReadUInt16();
                ushort MapLength  = Reader.ReadUInt16(); //Actual length is value / 2? Not sure
                ushort MapIndex   = Reader.ReadUInt16();
                ushort DescIndex  = Reader.ReadUInt16();
                uint   MapAddress = Reader.ReadUInt32(); //Not sure what this address actually points to

                uint Hash = (CRC32Hash.Hash(DescName) << 12) | DescIndex;

                MTShaderEffect Desc = new MTShaderEffect();

                if (TypeName == "__InputLayout")
                {
                    Desc = new MTAttributesGroup(Reader, StringsTblAddr);
                }
                else
                {
                    switch (DescType)
                    {
                    case 2: Desc = new MTTextureMap(Reader); break;

                    case 5: Desc = new MTAlphaBlend(Reader); break;

                    case 6: Desc = new MTDepthStencil(Reader); break;
                    }
                }

                Desc.Name = DescName;
                Desc.Type = TypeName;

                Descriptors.Add(Hash, Desc);
            }

            for (uint i = 0; i < FragShaderCount; i++)
            {
                Reader.BaseStream.Seek(FragShaderAddr + i * 4, SeekOrigin.Begin);
                Reader.BaseStream.Seek(Reader.ReadUInt32(), SeekOrigin.Begin);

                FragmentLighting.Add(new MTFragmentLighting(Reader));
            }
        }
Ejemplo n.º 7
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);
        }