static void Main(string[] args) { if (args.Length < 1) { Usage(); } else { string filename = Path.GetFullPath(args[0]); using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read)) { Type dataType = FormatUtil.getObjectType(fs); if (dataType != null) { string tagHashValue; char[] trimNull = new char[] { '\0' }; // initialize data IFormat vgmData = (IFormat)Activator.CreateInstance(dataType); vgmData.Initialize(fs, filename); Dictionary <string, string> tagHash = vgmData.GetTagHash(); // loop over hash and output information foreach (string s in tagHash.Keys) { tagHashValue = tagHash[s]; if (!String.IsNullOrEmpty(tagHashValue)) { tagHashValue = tagHashValue.TrimEnd(trimNull); } else { tagHashValue = String.Empty; } Console.WriteLine(s + ": " + tagHashValue); } } } } }
private TreeNode getFileNode(string pFileName, StreamWriter pOutputFileStream, bool pCheckForLibs, DoWorkEventArgs e) { int progress = (++fileCount * 100) / maxFiles; this.progressStruct.Clear(); this.progressStruct.FileName = pFileName; ReportProgress(progress, this.progressStruct); TreeNode ret = new TreeNode(Path.GetFileName(pFileName)); VGMToolbox.util.NodeTagStruct nodeTag = new VGMToolbox.util.NodeTagStruct(); TreeNode tagNode; string tagHashValue = String.Empty; using (FileStream fs = File.OpenRead(pFileName)) { try { Type dataType = FormatUtil.getObjectType(fs); if (dataType != null) { pOutputFileStream.WriteLine(pFileName); IFormat vgmData = (IFormat)Activator.CreateInstance(dataType); vgmData.Initialize(fs, pFileName); Dictionary <string, string> tagHash = vgmData.GetTagHash(); // Add Path for possible future use. tagHash.Add("Path", vgmData.FilePath); // check for libs if (pCheckForLibs && vgmData.UsesLibraries()) { if (!vgmData.IsLibraryPresent()) { ret.ForeColor = Color.Red; ret.Text += " (Missing Library)"; } } char[] trimNull = new char[] { '\0' }; foreach (string s in tagHash.Keys) { tagHashValue = tagHash[s]; if (!String.IsNullOrEmpty(tagHashValue)) { tagHashValue = tagHashValue.TrimEnd(trimNull); } else { tagHashValue = String.Empty; } tagNode = new TreeNode(s + ": " + tagHashValue); ret.Nodes.Add(tagNode); pOutputFileStream.WriteLine(s + ": " + tagHashValue); } pOutputFileStream.WriteLine(Environment.NewLine); // add classname to nodeTag nodeTag.ObjectType = dataType.AssemblyQualifiedName; } } catch (Exception ex) { this.progressStruct.Clear(); this.progressStruct.ErrorMessage = String.Format("Error processing <{0}>. Error received: ", pFileName) + ex.Message; ReportProgress(progress, this.progressStruct); } } // using (FileStream fs = File.OpenRead(pFileName)) nodeTag.FilePath = pFileName; ret.Tag = nodeTag; return(ret); }
private void rebuildFile(string pFilePath, RebuildSetsStruct pRebuildSetsStruct, AuditingUtil pAuditingUtil, DoWorkEventArgs ea) { ArrayList pDestinationFiles = new ArrayList(); bool isFileLibrary = false; bool hasMultipleExtensions = false; bool fileMovedOrScannedOk = true; Type formatType = null; if (FormatUtil.IsZipFile(pFilePath)) { string tempDir = System.IO.Path.GetTempPath() + Path.GetFileNameWithoutExtension(pFilePath); CompressionUtil.ExtractAllFilesFromZipFile(pFilePath, tempDir); tempDirsForDeletion.Add(tempDir); // Add in case of cancel button RebuildSetsStruct zipRebuildSetsStruct = pRebuildSetsStruct; zipRebuildSetsStruct.pSourceDir = tempDir; zipRebuildSetsStruct.totalFiles = Directory.GetFiles(tempDir, "*.*", SearchOption.AllDirectories).Length; maxFiles += zipRebuildSetsStruct.totalFiles; this.rebuildSets(zipRebuildSetsStruct, pAuditingUtil, 0, ea); if (!CancellationPending) { Directory.Delete(tempDir, true); tempDirsForDeletion.Remove(tempDir); } } else { using (FileStream fs = File.OpenRead(pFilePath)) { formatType = FormatUtil.getObjectType(fs); fs.Seek(0, SeekOrigin.Begin); // CRC32 string crc32Value = String.Empty; Crc32 crc32Generator = new Crc32(); // @TODO - Change to file streams? Out of memory errors on repeat runs /* * // MD5 * string md5FileName = Path.GetTempFileName(); * MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider(); * //MemoryStream md5MemoryStream = new MemoryStream(); * FileStream md5MemoryStream = new FileStream(md5FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); * CryptoStream md5CryptoStream = new CryptoStream(md5MemoryStream, md5Hash, CryptoStreamMode.Write); * * // SHA1 * string sha1FileName = Path.GetTempFileName(); * SHA1CryptoServiceProvider sha1Hash = new SHA1CryptoServiceProvider(); * //MemoryStream sha1MemoryStream = new MemoryStream(); * FileStream sha1MemoryStream = new FileStream(sha1FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); * CryptoStream sha1CryptoStream = new CryptoStream(sha1MemoryStream, sha1Hash, CryptoStreamMode.Write); */ if ((pAuditingUtil.HasVgmtSizeFlag) && (formatType != null)) { try { IFormat vgmData = (IFormat)Activator.CreateInstance(formatType); vgmData.Initialize(fs, pFilePath); isFileLibrary = vgmData.IsFileLibrary(); hasMultipleExtensions = vgmData.HasMultipleFileExtensions(); // vgmData.getDatFileCrc32(pFilePath, ref libHash, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream, false, pStreamInput); vgmData.GetDatFileCrc32(ref crc32Generator); vgmData = null; } catch (EndOfStreamException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } catch (System.OutOfMemoryException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } catch (IOException e) { this.progressStruct.Clear(); this.progressStruct.FileName = pFilePath; this.progressStruct.ErrorMessage = String.Format("Error processing <{0}> as type [{1}], falling back to full file cheksum. Error received: {2}", pFilePath, formatType.Name, e.Message) + Environment.NewLine + Environment.NewLine; ReportProgress(Constants.IgnoreProgress, this.progressStruct); crc32Generator.Reset(); // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } } else { // ParseFile.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator, // ref md5CryptoStream, ref sha1CryptoStream); ChecksumUtil.AddChunkToChecksum(fs, 0, (int)fs.Length, ref crc32Generator); } // @TODO Add MD5/SHA1 to make checksum hash correct String(CRC32 + MD5 + SHA1) crc32Value = crc32Generator.Value.ToString("X8"); pDestinationFiles = (ArrayList)pAuditingUtil.ChecksumHash[crc32Value]; if (pDestinationFiles != null) { if (!pRebuildSetsStruct.ScanOnly) { fileMovedOrScannedOk = this.moveFile(pRebuildSetsStruct.pDestinationDir, pDestinationFiles, fs, pFilePath, pRebuildSetsStruct.pOverwriteExisting, pRebuildSetsStruct.pCompressOutput, hasMultipleExtensions); } pAuditingUtil.AddChecksumToCache(crc32Value); } else { pAuditingUtil.AddUnknownFile(pFilePath); } } /* * md5CryptoStream.Close(); * md5CryptoStream.Dispose(); * sha1CryptoStream.Close(); * sha1CryptoStream.Dispose(); * * md5MemoryStream.Close(); * md5MemoryStream.Dispose(); * sha1MemoryStream.Close(); * sha1MemoryStream.Dispose(); * * File.Delete(md5FileName); * File.Delete(sha1FileName); */ // Remove Source only if copied if (fileMovedOrScannedOk && pDestinationFiles != null && pRebuildSetsStruct.pRemoveSource && File.Exists(pFilePath)) { if (!isFileLibrary) { File.Delete(pFilePath); } else // Add to List for deletion later { libFilesForDeletion.Add(pFilePath); } } } // (FormatUtil.IsZipFile(pFilePath)) }
protected override void DoTaskForFile(string pPath, IVgmtWorkerStruct pExamineChecksumGeneratorStruct, DoWorkEventArgs e) { ExamineChecksumGeneratorStruct examineChecksumGeneratorStruct = (ExamineChecksumGeneratorStruct)pExamineChecksumGeneratorStruct; string crc32; string md5; string sha1; string vgmtCrc32 = "Not implemented for this format."; string vgmtMd5 = "Not implemented for this format."; string vgmtSha1 = "Not implemented for this format."; string checksumKey; Type formatType = null; IFormat vgmData = null; using (FileStream fs = File.OpenRead(pPath)) { crc32 = ChecksumUtil.GetCrc32OfFullFile(fs); md5 = ChecksumUtil.GetMd5OfFullFile(fs); sha1 = ChecksumUtil.GetSha1OfFullFile(fs); if (examineChecksumGeneratorStruct.CheckForDuplicates) { checksumKey = String.Format("{0}/{1}/{2}", crc32, md5, sha1); this.addChecksumToHash(checksumKey, pPath, true); } if (examineChecksumGeneratorStruct.DoVgmtChecksums) { formatType = FormatUtil.getObjectType(fs); if (formatType != null) { vgmData = (IFormat)Activator.CreateInstance(formatType); vgmData.Initialize(fs, pPath); } } } if (vgmData != null) { Crc32 crc32Generator = new Crc32(); MD5CryptoServiceProvider md5Hash = new MD5CryptoServiceProvider(); FileStream md5FileStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write); CryptoStream md5CryptoStream = new CryptoStream(md5FileStream, md5Hash, CryptoStreamMode.Write); SHA1CryptoServiceProvider sha1Hash = new SHA1CryptoServiceProvider(); FileStream sha1FileStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.Write); CryptoStream sha1CryptoStream = new CryptoStream(sha1FileStream, sha1Hash, CryptoStreamMode.Write); vgmData.GetDatFileChecksums(ref crc32Generator, ref md5CryptoStream, ref sha1CryptoStream); md5CryptoStream.FlushFinalBlock(); sha1CryptoStream.FlushFinalBlock(); vgmtCrc32 = crc32Generator.Value.ToString("X8"); vgmtMd5 = ParseFile.ByteArrayToString(md5Hash.Hash); vgmtSha1 = ParseFile.ByteArrayToString(sha1Hash.Hash); if (examineChecksumGeneratorStruct.CheckForDuplicates) { checksumKey = String.Format("{0}/{1}/{2}", vgmtCrc32, vgmtMd5, vgmtSha1); this.addChecksumToHash(checksumKey, pPath, false); } md5FileStream.Close(); md5FileStream.Dispose(); sha1FileStream.Close(); sha1FileStream.Dispose(); md5CryptoStream.Close(); md5CryptoStream.Dispose(); sha1CryptoStream.Close(); sha1CryptoStream.Dispose(); } this.outputBuffer.AppendFormat("<{0}>{1}", pPath, Environment.NewLine); this.outputBuffer.AppendFormat("CRC32: {0}{1}", crc32, Environment.NewLine); this.outputBuffer.AppendFormat("MD5: {0}{1}", md5, Environment.NewLine); this.outputBuffer.AppendFormat("SHA1: {0}{1}", sha1, Environment.NewLine); if (examineChecksumGeneratorStruct.DoVgmtChecksums) { this.outputBuffer.AppendFormat("CRC32 (VGMT): {0}{1}", vgmtCrc32, Environment.NewLine); this.outputBuffer.AppendFormat("MD5 (VGMT): {0}{1}", vgmtMd5, Environment.NewLine); this.outputBuffer.AppendFormat("SHA1 (VGMT): {0}{1}", vgmtSha1, Environment.NewLine); } this.outputBuffer.AppendLine(); }