public void buildShareList() { _thWrk.Report("Building FileList"); List <RvFile> filesGot = new List <RvFile>(); List <RvFile> filesMissing = new List <RvFile>(); FindFixes.GetSelectedFiles(DB.DirRoot, true, filesGot, filesMissing); _thWrk.Report("Sorting Got CRC"); RvFile[] filesGotSortedCRC = FindFixesSort.SortCRC(filesGot); FindFixes.MergeGotFiles(filesGotSortedCRC, out FileGroup[] fileGroupsCRCSorted); _thWrk.Report("Sorting Got SHA1"); FindFixesSort.SortFamily(fileGroupsCRCSorted, FindSHA1, FamilySortSHA1, out fileGroupsSHA1Sorted); }
public static void Read(ThreadWorker thWrk) { ThWrk = thWrk; if (!File.Exists(Settings.rvSettings.CacheFile)) { OpenDefaultDB(); ThWrk = null; return; } DirRoot = new RvFile(FileType.Dir); using (FileStream fs = new FileStream(Settings.rvSettings.CacheFile, FileMode.Open, FileAccess.Read)) { if (fs.Length < 4) { ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup."); } using (BinaryReader br = new BinaryReader(fs, Encoding.UTF8, true)) { DivideProgress = fs.Length / 1000; DivideProgress = DivideProgress == 0 ? 1 : DivideProgress; ThWrk?.Report(new bgwSetRange(1000)); DBVersion.VersionNow = br.ReadInt32(); if (DBVersion.VersionNow != DBVersion.Version) { ReportError.Show( "Data Cache version is out of date you should now rescan your dat directory and roms directory."); br.Close(); fs.Close(); fs.Dispose(); OpenDefaultDB(); ThWrk = null; return; } else { DirRoot.Read(br, null); } if (fs.Position > fs.Length - 8) { ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup."); } ulong testEOF = br.ReadUInt64(); if (testEOF != EndCacheMarker) { ReportError.UnhandledExceptionHandler("Cache is Corrupt, revert to Backup."); } } } ThWrk = null; }
public static void ChdManCheck(RvFile tFile, string directory, ThreadWorker thWrk, ref bool fileErrorAbort) { string filename = Path.Combine(directory, tFile.Name); if (tFile.FileStatusIs(FileStatus.AltSHA1FromHeader | FileStatus.AltSHA1Verified)) { return; } thWrk.Report(new bgwText2(filename)); CHD.CHDManCheck res = CHD.ChdmanCheck(filename, thWrk, out string error); switch (res) { case CHD.CHDManCheck.Good: tFile.FileStatusSet( (tFile.AltSHA1 != null ? FileStatus.AltSHA1Verified : 0) | (tFile.AltMD5 != null ? FileStatus.AltMD5Verified : 0) ); return; case CHD.CHDManCheck.Corrupt: thWrk.Report(new bgwShowError(filename, error)); tFile.GotStatus = GotStatus.Corrupt; return; case CHD.CHDManCheck.CHDReturnError: case CHD.CHDManCheck.CHDUnknownError: thWrk.Report(new bgwShowError(filename, error)); return; case CHD.CHDManCheck.ChdmanNotFound: return; case CHD.CHDManCheck.CHDNotFound: ReportError.Show("File: " + filename + " Error: Not Found scan Aborted."); fileErrorAbort = true; return; default: ReportError.UnhandledExceptionHandler(error); return; } }
public void StartClient(ThreadWorker e) { _thWrk = e; session = Guid.NewGuid().ToByteArray(); _thWrk?.Report("Building FileList"); List <RvFile> filesGot = new List <RvFile>(); List <RvFile> filesMissing = new List <RvFile>(); FindFixes.GetSelectedFiles(DB.DirRoot, true, filesGot, filesMissing); _thWrk?.Report("Sorting Missing CRC"); RvFile[] filesMissingSortedCRC = FindFixesSort.SortCRC(filesMissing); FindFixes.MergeGotFiles(filesMissingSortedCRC, out FileGroup[] fileMissingGroupsCRCSorted); _thWrk?.Report("Sorting Missing SHA1"); FindFixesSort.SortFamily(fileMissingGroupsCRCSorted, FindSHA1, FamilySortSHA1, out fileMissingGroupsSHA1Sorted); Find(); }
public void StartSharing(ThreadWorker e) { _thWrk = e; buildShareList(); if (_thWrk.CancellationPending) { return; } _listeningClient = new NetClient(); _listeningClient.OnReceivedData += ReceivedData; _thWrk.Report($"Trying to Listen on port {listeningPort}"); bool listening = _listeningClient.StartListener(IPAddress.Any, listeningPort, out string error); if (listening) { _thWrk.Report($"Listening on port {listeningPort}"); } else { return; } }
public static void UpdateDat(ThreadWorker thWrk) { try { _thWrk = thWrk; if (_thWrk == null) { return; } _thWrk.Report(new bgwText("Clearing DB Status")); RepairStatus.ReportStatusReset(DB.DirTree); _datCount = 0; _thWrk.Report(new bgwText("Finding Dats")); RvFile datRoot = new RvFile(FileType.Dir) { Name = "RomVault", DatStatus = DatStatus.InDatCollect }; // build a datRoot tree of the DAT's in DatRoot, and count how many dats are found if (!RecursiveDatTree(datRoot, out _datCount)) { _thWrk.Report(new bgwText("Dat Update Complete")); _thWrk.Finished = true; _thWrk = null; return; } _thWrk.Report(new bgwText("Scanning Dats")); _datsProcessed = 0; // now compare the database DAT's with datRoot removing any old DAT's RemoveOldDats(DB.DirTree.Child(0), datRoot); // next clean up the File status removing any old DAT's RemoveOldDatsCleanUpFiles(DB.DirTree.Child(0)); _thWrk.Report(new bgwSetRange(_datCount - 1)); // next add in new DAT and update the files UpdateDatList(DB.DirTree.Child(0), datRoot); // finally remove any unneeded directories from the TreeView RemoveOldTree(DB.DirTree.Child(0)); _thWrk.Report(new bgwText("Updating Cache")); DB.Write(); _thWrk.Report(new bgwText("Dat Update Complete")); _thWrk.Finished = true; _thWrk = null; } catch (Exception exc) { ReportError.UnhandledExceptionHandler(exc); _thWrk?.Report(new bgwText("Updating Cache")); DB.Write(); _thWrk?.Report(new bgwText("Complete")); if (_thWrk != null) { _thWrk.Finished = true; } _thWrk = null; } }
private static void ShowDat(string message, string filename) { _thWrk.Report(new bgwShowError(filename, message)); }
public static void ScanFiles(ThreadWorker thWrk) { try { _thWrk = thWrk; if (_thWrk == null) { return; } _progressCounter = 0; Stopwatch sw = new Stopwatch(); sw.Reset(); sw.Start(); _thWrk.Report(new bgwSetRange(12)); _thWrk.Report(new bgwText("Clearing DB Status")); _thWrk.Report(_progressCounter++); RepairStatus.ReportStatusReset(DB.DirTree); ResetFileGroups(DB.DirTree); _thWrk.Report(new bgwText("Getting Selected Files")); _thWrk.Report(_progressCounter++); Debug.WriteLine("Start " + sw.ElapsedMilliseconds); List <RvFile> filesGot = new List <RvFile>(); List <RvFile> filesMissing = new List <RvFile>(); GetSelectedFiles(DB.DirTree, true, filesGot, filesMissing); Debug.WriteLine("GetSelected " + sw.ElapsedMilliseconds); _thWrk.Report(new bgwText("Sorting on CRC")); _thWrk.Report(_progressCounter++); RvFile[] filesGotSortedCRC = FindFixesSort.SortCRC(filesGot); Debug.WriteLine("SortCRC " + sw.ElapsedMilliseconds); // take the fileGot list and fileGroups list // this groups all the got files using there CRC _thWrk.Report(new bgwText("Index creation on got CRC")); _thWrk.Report(_progressCounter++); MergeGotFiles(filesGotSortedCRC, out FileGroup[] fileGroupsCRCSorted); Debug.WriteLine("Merge " + sw.ElapsedMilliseconds); _thWrk.Report(new bgwText("Index creation on got SHA1")); _thWrk.Report(_progressCounter++); FindFixesSort.SortFamily(fileGroupsCRCSorted, FindSHA1, FamilySortSHA1, out FileGroup[] fileGroupsSHA1Sorted); _thWrk.Report(new bgwText("Index creation on got MD5")); _thWrk.Report(_progressCounter++); FindFixesSort.SortFamily(fileGroupsCRCSorted, FindMD5, FamilySortMD5, out FileGroup[] fileGroupsMD5Sorted); // next make another sorted list of got files on the AltCRC // these are the same FileGroup classes as in the fileGroupsCRCSorted List, just sorted by AltCRC // if the files does not have an altCRC then it is not added to this list. _thWrk.Report(new bgwText("Index creation on got AltCRC")); _thWrk.Report(_progressCounter++); FindFixesSort.SortFamily(fileGroupsCRCSorted, FindAltCRC, FamilySortAltCRC, out FileGroup[] fileGroupsAltCRCSorted); _thWrk.Report(new bgwText("Index creation on got AltSHA1")); _thWrk.Report(_progressCounter++); FindFixesSort.SortFamily(fileGroupsCRCSorted, FindAltSHA1, FamilySortAltSHA1, out FileGroup[] fileGroupsAltSHA1Sorted); _thWrk.Report(new bgwText("Index creation on got AltMD5")); _thWrk.Report(_progressCounter++); FindFixesSort.SortFamily(fileGroupsCRCSorted, FindAltMD5, FamilySortAltMD5, out FileGroup[] fileGroupsAltMD5Sorted); _thWrk.Report(new bgwText("Merging in missing file list")); _thWrk.Report(_progressCounter++); // try and merge the missing File list into the FileGroup classes // using the altCRC sorted list and then the CRCSorted list MergeInMissingFiles(fileGroupsCRCSorted, fileGroupsSHA1Sorted, fileGroupsMD5Sorted, fileGroupsAltCRCSorted, fileGroupsAltSHA1Sorted, fileGroupsAltMD5Sorted, filesMissing); int totalAfterMerge = fileGroupsCRCSorted.Length; _thWrk.Report(new bgwText("Finding Fixes")); _thWrk.Report(_progressCounter++); FindFixesListCheck.GroupListCheck(fileGroupsCRCSorted); _thWrk.Report(new bgwText("Complete (Unique Files " + totalAfterMerge + ")")); _thWrk.Finished = true; _thWrk = null; } catch (Exception exc) { ReportError.UnhandledExceptionHandler(exc); _thWrk?.Report(new bgwText("Updating Cache")); DB.Write(); _thWrk?.Report(new bgwText("Complete")); if (_thWrk != null) { _thWrk.Finished = true; } _thWrk = null; } }
public static void ScanFiles(ThreadWorker e) { #if !DEBUG try { #endif _fileErrorAbort = false; _cacheSaveTimer = new Stopwatch(); _cacheSaveTimer.Reset(); if (Settings.rvSettings.CacheSaveTimerEnabled) { _cacheSaveTimer.Start(); } _thWrk = e; if (_thWrk == null) { return; } _thWrk.Report(new bgwText("Clearing DB Status")); RepairStatus.ReportStatusReset(DB.DirRoot); _thWrk.Report(new bgwText("Finding Dir's to Scan")); //Next get a list of all the directories to be scanned List <RvFile> lstDir = new List <RvFile>(); DBHelper.GetSelectedDirListStart(ref lstDir, StartAt); _thWrk.Report(new bgwText("Scanning Dir's")); _thWrk.Report(new bgwSetRange(lstDir.Count)); //Scan the list of directories. for (int i = 0; i < lstDir.Count; i++) { _thWrk.Report(i + 1); _thWrk.Report(new bgwText("Scanning Dir : " + lstDir[i].FullName)); string lDir = lstDir[i].FullName; Console.WriteLine(lDir); if (Directory.Exists(lDir)) { lstDir[i].GotStatus = GotStatus.Got; CheckADir(lstDir[i], true); } else { MarkAsMissing(lstDir[i]); } if (_thWrk.CancellationPending || _fileErrorAbort) { break; } } _thWrk.Report(new bgwText("Updating Cache")); DB.Write(); _thWrk.Report(new bgwText("File Scan Complete")); _thWrk.Finished = true; _thWrk = null; #if !DEBUG } catch (Exception exc) { ReportError.UnhandledExceptionHandler(exc); _thWrk?.Report(new bgwText("Updating Cache")); DB.Write(); _thWrk?.Report(new bgwText("Complete")); if (_thWrk != null) { _thWrk.Finished = true; } _thWrk = null; } #endif }
/// <summary> /// Called from 5 places: /// 1: ScanFiles: main top level loop. /// 2: MatchFound: called after a ZIP/SevenZip is matched to an item in the DB, where the zip has either changed or never been scanned or level 3 scanning /// 3: MatchFound: called when an directory is matched to an item in the DB that is not from a DAT. (This is a directory not found in the main tree, as main tree dir's are processes in top level loop /// 4: NewFileFound: called after a new unmatched ZIP/SevenZip is found. /// 5: NewFileFound: called after a new unmatched DIR is found. /// </summary> /// <param name="dbDir"></param> /// <param name="report"></param> private static void CheckADir(RvFile dbDir, bool report) { if (_cacheSaveTimer.Elapsed.TotalMinutes > Settings.rvSettings.CacheSaveTimePeriod) { _thWrk.Report("Saving Cache"); DB.Write(); _thWrk.Report("Saving Cache Complete"); _cacheSaveTimer.Reset(); _cacheSaveTimer.Start(); } string fullDir = dbDir.FullName; if (report) { _thWrk.Report(new bgwText2(fullDir)); } // this is a temporary rvDir structure to store the data about the actual directory/files we are scanning // we will first populate this variable with the real file data from the directory, and then compare it // with the data in dbDir. RvFile fileDir = null; Debug.WriteLine(fullDir); FileType ft = dbDir.FileType; #region "Populate fileDir" // if we are scanning a ZIP file then populate scanDir from the ZIP file switch (ft) { case FileType.Zip: case FileType.SevenZip: fileDir = Populate.FromAZipFile(dbDir, EScanLevel, _thWrk); break; case FileType.Dir: fileDir = Populate.FromADir(dbDir, EScanLevel, _thWrk, ref _fileErrorAbort); break; default: ReportError.SendAndShow("Un supported file type in CheckADir " + ft); break; } #endregion if (fileDir == null) { ReportError.SendAndShow("Unknown Reading File Type in Dir Scanner"); return; } if (report) { _thWrk.Report(new bgwSetRange2(fileDir.ChildCount)); _thWrk.Report(new bgwRange2Visible(true)); } if (!DBTypeGet.isCompressedDir(ft) && _thWrk.CancellationPending) { return; } Compare(dbDir, fileDir, report, true); }
private static void ReadError(string filename, string error) { _thWrk.Report(new bgwShowError(filename, error)); }
private static void CHDErrorHandler(object sendingProcess, DataReceivedEventArgs outLine) { // Collect the process command output. if (string.IsNullOrEmpty(outLine.Data)) { return; } // We can get fed multiple lines worth of data because of \r line feeds string[] sLines = outLine.Data.Split(new[] { "\r" }, StringSplitOptions.None); foreach (string sLine in sLines) { if (string.IsNullOrEmpty(sLine)) { continue; } ReportError.LogOut("CHDError: " + sLine); _thWrk.Report(new bgwText3(sLine)); if (_resultType != CHDManCheck.Unset) { if (_errorLines > 0) { _errorLines -= 1; _result += "\r\n" + sLine; } } else if (Regex.IsMatch(sLine, @"^No verification to be done; CHD has (uncompressed|no checksum)")) { _result = sLine; _resultType = CHDManCheck.Corrupt; } else if (Regex.IsMatch(sLine, @"^Error (opening|reading) CHD file.*")) { _result = sLine; _resultType = CHDManCheck.Corrupt; } else if (Regex.IsMatch(sLine, @"^Error opening parent CHD file .*:")) { _result = sLine; _resultType = CHDManCheck.Corrupt; } else if (Regex.IsMatch(sLine, @"^Error: (Raw|Overall) SHA1 in header")) { _result = sLine; _resultType = CHDManCheck.Corrupt; } else if (Regex.IsMatch(sLine, @"^Out of memory")) { _result = sLine; _resultType = CHDManCheck.Corrupt; } // Verifying messages are a non-error else if (Regex.IsMatch(sLine, @"Verifying, \d+\.\d+\% complete\.\.\.")) { } else { _result = "Unknown message : " + sLine; _resultType = CHDManCheck.CHDUnknownError; } } }
public static CHDManCheck ChdmanCheck(string filename, ThreadWorker thWrk, out string result) { _thWrk = thWrk; _result = ""; _resultType = CHDManCheck.Unset; string chdExe = "chdman.exe"; if (Settings.IsUnix) { chdExe = "chdman"; } string chdPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, chdExe); if (!File.Exists(chdPath)) { result = chdExe + " Not Found."; return(CHDManCheck.ChdmanNotFound); } if (!File.Exists(filename)) { result = filename + " Not Found."; return(CHDManCheck.CHDNotFound); } string shortName = NameFix.GetShortPath(filename); using (Process exeProcess = new Process()) { exeProcess.StartInfo.FileName = chdPath; ReportError.LogOut("CHD: FileName :" + exeProcess.StartInfo.FileName); exeProcess.StartInfo.Arguments = "verify -i \"" + shortName + "\""; ReportError.LogOut("CHD: Arguments :" + exeProcess.StartInfo.Arguments); // Set UseShellExecute to false for redirection. exeProcess.StartInfo.UseShellExecute = false; // Stops the Command window from popping up. exeProcess.StartInfo.CreateNoWindow = true; // Redirect the standard output. // This stream is read asynchronously using an event handler. exeProcess.StartInfo.RedirectStandardOutput = true; exeProcess.StartInfo.RedirectStandardError = true; // Set our event handler to asynchronously read the process output. exeProcess.OutputDataReceived += CHDOutputHandler; exeProcess.ErrorDataReceived += CHDErrorHandler; _outputLineCount = 0; _errorLines = 0; ReportError.LogOut("CHD: Scanning Starting"); exeProcess.Start(); // Start the asynchronous read of the process output stream. exeProcess.BeginOutputReadLine(); exeProcess.BeginErrorReadLine(); // Wait for the process finish. exeProcess.WaitForExit(); ReportError.LogOut("CHD: Scanning Finished"); } result = _result; if (_resultType == CHDManCheck.Unset) { _resultType = CHDManCheck.Good; } _thWrk.Report(new bgwText3("")); ReportError.LogOut("CHD: returning result " + _resultType + " " + result); return(_resultType); }
public static void ReportProgress(object prog) { _thWrk?.Report(prog); }
public static RvFile FromAZipFile(RvFile dbDir, EScanLevel eScanLevel, ThreadWorker thWrk) { RvFile fileDir = new RvFile(dbDir.FileType); DatStatus chechingDatStatus = dbDir.IsInToSort ? DatStatus.InToSort : DatStatus.NotInDat; string filename = dbDir.FullNameCase; ICompress checkZ = dbDir.FileType == FileType.Zip ? new Zip() : (ICompress) new SevenZ(); ZipReturn zr = checkZ.ZipFileOpen(filename, dbDir.FileModTimeStamp); if (zr == ZipReturn.ZipGood) { dbDir.ZipStatus = checkZ.ZipStatus; // to be Scanning a ZIP file means it is either new or has changed. // as the code below only calls back here if that is true. // // Level1: Only use header CRC's // Just get the CRC for the ZIP headers. // // Level2: Fully checksum changed only files // We know this file has been changed to do a full checksum scan. // // Level3: Fully checksum everything // So do a full checksum scan. if (_fs == null) { _fs = new FileScan(); } List <FileScan.FileResults> fr = _fs.Scan(checkZ, false, eScanLevel == EScanLevel.Level2 || eScanLevel == EScanLevel.Level3); // add all of the file information from the zip file into scanDir for (int i = 0; i < checkZ.LocalFilesCount(); i++) { LocalFile lf = checkZ.GetLocalFile(i); RvFile tFile = new RvFile(DBTypeGet.FileFromDir(dbDir.FileType)) { Name = lf.Filename, ZipFileIndex = i, ZipFileHeaderPosition = lf.LocalHead, Size = lf.UncompressedSize, CRC = lf.CRC, FileModTimeStamp = lf.LastModified }; // all levels read the CRC from the ZIP header tFile.SetStatus(chechingDatStatus, GotStatus.Got); tFile.FileStatusSet(FileStatus.SizeFromHeader | FileStatus.CRCFromHeader); if (fr[i].FileStatus != ZipReturn.ZipGood) { thWrk.Report(new bgwShowCorrupt(fr[i].FileStatus, filename + " : " + lf.Filename)); tFile.GotStatus = GotStatus.Corrupt; } else { tFile.HeaderFileType = fr[i].HeaderFileType; tFile.SHA1 = fr[i].SHA1; tFile.MD5 = fr[i].MD5; tFile.AltSize = fr[i].AltSize; tFile.AltCRC = fr[i].AltCRC; tFile.AltSHA1 = fr[i].AltSHA1; tFile.AltMD5 = fr[i].AltMD5; if (fr[i].CRC != null) { tFile.FileStatusSet(FileStatus.CRCFromHeader); if (eScanLevel == EScanLevel.Level2 || eScanLevel == EScanLevel.Level3) { tFile.FileStatusSet(FileStatus.CRCVerified); } } tFile.FileStatusSet( FileStatus.SizeVerified | (fr[i].HeaderFileType != HeaderFileType.Nothing ? FileStatus.HeaderFileTypeFromHeader : 0) | (fr[i].SHA1 != null ? FileStatus.SHA1Verified : 0) | (fr[i].MD5 != null ? FileStatus.MD5Verified : 0) | (fr[i].AltSize != null ? FileStatus.AltSizeVerified : 0) | (fr[i].AltCRC != null ? FileStatus.AltCRCVerified : 0) | (fr[i].AltSHA1 != null ? FileStatus.AltSHA1Verified : 0) | (fr[i].AltMD5 != null ? FileStatus.AltMD5Verified : 0) ); } fileDir.ChildAdd(tFile); } } else if (zr == ZipReturn.ZipFileLocked) { thWrk.Report(new bgwShowError(filename, "Zip File Locked")); dbDir.FileModTimeStamp = 0; dbDir.GotStatus = GotStatus.FileLocked; } else { thWrk.Report(new bgwShowCorrupt(zr, filename)); dbDir.GotStatus = GotStatus.Corrupt; } checkZ.ZipFileClose(); return(fileDir); }
private byte[] ReceivedData(string ip, byte[] buffer) { FileShareUiComm fs; if (buffer == null) { return(null); } _thWrk.Report($"Received {ip} data -> {BitConverter.ToString(buffer)}"); if (buffer.Length < 2) { // return an error command reply return(new byte[] { 0, 0 }); } int bOffset = 0; int command = (buffer[bOffset++] << 8) + buffer[bOffset++]; byte[] name = buffer.Copy(bOffset, 10); bOffset += 10; byte[] session = buffer.Copy(bOffset, 16); bOffset += 16; switch (command) { // 00 - 2 Command 1 (0,1) // 02 - 10 Name // 12 - 16 Session Guid case 1: // 00 - 2 Command 1 (0,1) // 02 - 1 01 fs = new FileShareUiComm { command = 1, username = name, session = session, message = "Hello" }; _thWrk?.Report(fs); return(new byte[] { 0, 1, 1 }); // 00 - 2 Command 2 (0, 2) // 02 - 10 Name // 12 - 16 Session Guid // 28 - 8 Size case 2: // 00 - 2 Command 2 (0, 2) // 02 - 1 File Not Found = 0 // 00 - 2 Command 2 (0, 2) // 02 - 1 File Found = 1 // 3 - 8 File Compressed Size // 11 - 4 File CRC { RvFile testFile = new RvFile(FileType.ZipFile); testFile.Size = BitConverter.ToUInt64(buffer, bOffset); bOffset += 8; comBits cb = (comBits)buffer[bOffset++]; if ((cb & comBits.cCRC) != 0) { testFile.CRC = buffer.Copy(bOffset, 4); bOffset += 4; } if ((cb & comBits.cSHA1) != 0) { testFile.SHA1 = buffer.Copy(bOffset, 20); bOffset += 20; } if ((cb & comBits.cMD5) != 0) { testFile.MD5 = buffer.Copy(bOffset, 16); bOffset += 16; } bool found = FindFixes.FindMatch(fileGroupsSHA1Sorted, testFile, CompareSHA1, FileGroup.FindExactMatch, out List <int> listIndex); if (!found) { fs = new FileShareUiComm { command = 2, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1 not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 2, 0 }); } FileGroup fgMatch = fileGroupsSHA1Sorted[listIndex[0]]; if (testFile.Size != fgMatch.Size) { fs = new FileShareUiComm { command = 2, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1,Size not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 2, 0 }); } if (!ArrByte.ECompare(testFile.CRC, fgMatch.CRC)) { fs = new FileShareUiComm { command = 2, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1,CRC not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 2, 0 }); } if (!ArrByte.ECompare(testFile.MD5, fgMatch.MD5)) { fs = new FileShareUiComm { command = 2, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1,MD5 not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 2, 0 }); } foreach (RvFile fMatch in fgMatch.Files) { Zip zFile = new Zip(); zFile.ZipFileOpen(fMatch.Parent.FullNameCase, fMatch.Parent.FileModTimeStamp, false); zFile.ZipFileOpenReadStreamQuick((ulong)fMatch.ZipFileHeaderPosition, true, out Stream stream, out ulong streamSize, out ushort compressionMethod); if (compressionMethod != 8) { zFile.ZipFileCloseReadStream(); zFile.ZipFileClose(); continue; } List <byte> ret = new List <byte>(); ret.AddRange(new byte[] { 0, 2, 1 }); ret.AddRange(BitConverter.GetBytes((ulong)streamSize)); ret.AddRange(fMatch.CRC); fs = new FileShareUiComm { command = 2, username = name, session = session, sha1 = testFile.SHA1, message = "Found File", filename = fMatch.Parent.FullName, zippedFilename = fMatch.Name, compressedSize = streamSize }; _thWrk?.Report(fs); zFile.ZipFileCloseReadStream(); zFile.ZipFileClose(); return(ret.ToArray()); } fs = new FileShareUiComm { command = 2, username = name, session = session, sha1 = testFile.SHA1, message = "No Zipped version found." }; _thWrk?.Report(fs); return(new byte[] { 0, 2, 0 }); } // 00 - 2 Command 3 (0, 3) // 02 - 10 Name // 12 - 16 Session Guid // 28 - 20 SHA1 // 48 - 8 Size // 56 - 8 long file offset block // 64 - 4 block length to read case 3: // 00 - 2 Command 2 (0, 3) // 02 - 1 File Not Found = 0 // 00 - 2 Command 2 (0, 3) // 02 - 1 File Found = 1 // 03 - 4: Block read length // 07 - X: Data { RvFile testFile = new RvFile(FileType.ZipFile); testFile.Size = BitConverter.ToUInt64(buffer, bOffset); bOffset += 8; comBits cb = (comBits)buffer[bOffset++]; if ((cb & comBits.cCRC) != 0) { testFile.CRC = buffer.Copy(bOffset, 4); bOffset += 4; } if ((cb & comBits.cSHA1) != 0) { testFile.SHA1 = buffer.Copy(bOffset, 20); bOffset += 20; } if ((cb & comBits.cMD5) != 0) { testFile.MD5 = buffer.Copy(bOffset, 16); bOffset += 16; } ulong offset = BitConverter.ToUInt64(buffer, bOffset); bOffset += 8; uint length = BitConverter.ToUInt32(buffer, bOffset); bOffset += 4; bool found = FindFixes.FindMatch(fileGroupsSHA1Sorted, testFile, CompareSHA1, FileGroup.FindExactMatch, out List <int> listIndex); if (!found) { fs = new FileShareUiComm { command = 3, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1 not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 3, 0 }); } FileGroup fgMatch = fileGroupsSHA1Sorted[listIndex[0]]; if (testFile.Size != fgMatch.Size) { fs = new FileShareUiComm { command = 3, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1,Size not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 3, 0 }); } if (!ArrByte.ECompare(testFile.CRC, fgMatch.CRC)) { fs = new FileShareUiComm { command = 3, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1,CRC not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 3, 0 }); } if (!ArrByte.ECompare(testFile.MD5, fgMatch.MD5)) { fs = new FileShareUiComm { command = 3, username = name, session = session, sha1 = testFile.SHA1, message = "SHA1,MD5 not found" }; _thWrk?.Report(fs); return(new byte[] { 0, 3, 0 }); } foreach (RvFile fMatch in fgMatch.Files) { Zip zFile = new Zip(); zFile.ZipFileOpen(fMatch.Parent.FullNameCase, fMatch.Parent.FileModTimeStamp, false); zFile.ZipFileOpenReadStreamQuick((ulong)fMatch.ZipFileHeaderPosition, true, out Stream stream, out ulong streamSize, out ushort compressionMethod); if (compressionMethod != 8) { zFile.ZipFileCloseReadStream(); zFile.ZipFileClose(); continue; } List <byte> ret = new List <byte>(); ret.AddRange(new byte[] { 0, 3, 1 }); stream.Seek((long)offset, SeekOrigin.Current); byte[] data = new byte[length]; int readLength = stream.Read(data, 0, (int)length); ret.AddRange(BitConverter.GetBytes(readLength)); ret.AddRange(data); fs = new FileShareUiComm { command = 2, username = name, session = session, filename = fMatch.Name, sha1 = testFile.SHA1, message = "Sending", zippedFilename = fMatch.Parent.FullName, compressedSize = streamSize, offset = offset, blockLength = length }; _thWrk?.Report(fs); zFile.ZipFileCloseReadStream(); zFile.ZipFileClose(); return(ret.ToArray()); } return(new byte[] { 0, 2, 0 }); } default: return(new byte[] { 0, 0 }); } }
private static void FileProcess(string filename) { _bgw.Report(new bgwText2(filename)); }
public bool FindSHA1(searchFiles f) { f.found = false; FileShareUiComm fs = new FileShareUiComm { username = name, session = session, filename = f.name, size = f.Size, crc = f.CRC, sha1 = f.SHA1, md5 = f.MD5, offset = 0, blockLength = 0 }; fs.command = 2; comBits cb = 0; if (f.CRC != null) { cb |= comBits.cCRC; } if (f.SHA1 != null) { cb |= comBits.cSHA1; } if (f.MD5 != null) { cb |= comBits.cMD5; } List <byte> list = new List <byte>(); list.Add(0); list.Add(2); list.AddRange(name); list.AddRange(session); list.AddRange(BitConverter.GetBytes(f.Size)); list.Add((byte)cb); if (f.CRC != null) { list.AddRange(f.CRC); } if (f.SHA1 != null) { list.AddRange(f.SHA1); } if (f.MD5 != null) { list.AddRange(f.MD5); } NetClient.SendToServer(list.ToArray(), ip, port, out byte[] bReply); if (bReply == null) { fs.message = "Server Lost"; _thWrk?.Report(fs); return(false); } if (bReply[0] != 0 || bReply[1] != 2) { // textBox2.Text = "Incorrect Command reply"; return(false); } if (bReply[2] != 1) { fs.message = "Not Found"; _thWrk?.Report(fs); return(false); } f.compressedSize = BitConverter.ToUInt64(bReply, 3); byte[] fCRC = bReply.Copy(11, 4); if (!ArrByte.ECompare(f.CRC, fCRC)) { return(false); } if (f.CRC == null) { f.CRC = fCRC; } f.found = true; return(true); }