/// <summary>
 /// Adds a new <see cref="WADEntry"/> to this <see cref="WADFile"/>
 /// </summary>
 /// <param name="path">The virtual path of the file being added</param>
 /// <param name="data">Data of file being added</param>
 /// <param name="compressedEntry">Whether the data needs to be GZip compressed inside WAD</param>
 public void AddEntry(string path, byte[] data, bool compressedEntry)
 {
     using (XXHash64 xxHash = XXHash64.Create())
     {
         AddEntry(BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower(new CultureInfo("en-US")))), 0), data, compressedEntry);
     }
 }
 /// <summary>
 /// Adds a new <see cref="EntryType.FileRedirection"/> <see cref="WADEntry"/> to this <see cref="WADFile"/>
 /// </summary>
 /// <param name="path">The virtual path of the file being added</param>
 /// <param name="fileRedirection">The file the game should load instead of this one</param>
 public void AddEntry(string path, string fileRedirection)
 {
     using (XXHash64 xxHash = XXHash64.Create())
     {
         AddEntry(BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower(new CultureInfo("en-US")))), 0), fileRedirection);
     }
 }
 /// <summary>
 /// Removes a <see cref="WADEntry"/> Entry with the specified path
 /// </summary>
 /// <param name="path">The path of the <see cref="WADEntry"/> to remove</param>
 public void RemoveEntry(string path)
 {
     using (XXHash64 xxHash = XXHash64.Create())
     {
         RemoveEntry(BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path)), 0));
     }
 }
Esempio n. 4
0
        public string addFile(string filepath, string path)
        {
            if (mapEntries.ContainsKey(path))
            {
                return("");
            }

            try
            {
                var result = this.activeWad.AddEntry(path, File.ReadAllBytes(filepath), true);

                using (XXHash64 xxHash = XXHash64.Create())
                {
                    StringDictionary.Add(BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower())), 0), path);
                }

                mapEntries.Add(path, result);

                return(path);
            }
            catch (Exception e) {
                Console.WriteLine(e.Message);
                return("");
            }
        }
Esempio n. 5
0
        private void menuAddFolder_Click(object sender, RoutedEventArgs e)
        {
            using (CommonOpenFileDialog dialog = new CommonOpenFileDialog())
            {
                dialog.IsFolderPicker = true;

                if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
                {
                    DirectoryInfo directory = new DirectoryInfo(dialog.FileName);

                    using (XXHash64 xxHash = XXHash64.Create())
                    {
                        foreach (FileInfo fileInfo in directory.EnumerateFiles("*", SearchOption.AllDirectories))
                        {
                            string path       = fileInfo.FullName.Substring(directory.FullName.Length + 1);
                            ulong  hashedPath = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower())), 0);

                            if (!StringDictionary.ContainsKey(hashedPath))
                            {
                                StringDictionary.Add(hashedPath, path);
                            }

                            this.WAD.AddEntry(hashedPath, File.ReadAllBytes(fileInfo.FullName), true);
                        }
                    }

                    Logger.Info("Added files from directory: " + dialog.FileName);
                }
            }
        }
Esempio n. 6
0
        private static string Hash(string key)
        {
            switch (FileNameStyle)
            {
            case KeyStyle.Hashed:
            {
                var         hash  = XXHash64.Hash(key);
                Span <byte> bytes = stackalloc byte[10];
                Utf8Formatter.TryFormat(hash, bytes, out var bytesWritten);
                // This requires netcoreapp3.1 or net5
                // return MemoryMarshal.AsRef<char>(bytes.Slice(0, bytesWritten)).ToString();
                return(DefaultEncoding.GetString(bytes.Slice(0, bytesWritten).ToArray()));
            }

            case KeyStyle.Base64:
            {
                var bytes = DefaultEncoding.GetBytes(key);
                return(UrlBase64.Encode(bytes));
            }

            case KeyStyle.PlainText:
            {
                if (key.ToCharArray().Any(c => Array.BinarySearch(IllegalCharacters, c) >= 0))
                {
                    throw new IllegalKeyException();
                }
                return(key);
            }
            }

            throw new Exception("Invalid key hashing style set!");
        }
Esempio n. 7
0
        private void menuAddFolder_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog()
            {
                ShowNewFolderButton = false
            };

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DirectoryInfo directory = new DirectoryInfo(dialog.SelectedPath);

                using (XXHash64 xxHash = XXHash64.Create())
                {
                    foreach (FileInfo fileInfo in directory.EnumerateFiles("*", SearchOption.AllDirectories))
                    {
                        string path       = fileInfo.FullName.Substring(directory.FullName.Length + 1);
                        ulong  hashedPath = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower())), 0);

                        if (!StringDictionary.ContainsKey(hashedPath))
                        {
                            StringDictionary.Add(hashedPath, path);
                        }

                        this.Wad.AddEntry(hashedPath, File.ReadAllBytes(fileInfo.FullName), true);
                    }
                }

                Logger.Info("Added files from directory: " + dialog.SelectedPath);
            }
        }
Esempio n. 8
0
        private static string Hash(string plain)
        {
            ulong hash = XXHash64.Hash(plain);

            byte[] buffer = BitConverter.GetBytes(hash);
            return(BitConverter.ToString(buffer).Replace("-", "").ToLower());
        }
Esempio n. 9
0
        public void SingleBlockXxh64UsingSpanMatchesTheirs(string text)
        {
            var input    = System.Text.Encoding.UTF8.GetBytes(text);
            var expected = Theirs64(input);
            var actual   = XXHash64.DigestOf(input.AsSpan());

            Assert.Equal(expected, actual);
        }
Esempio n. 10
0
        public async Task GetHashOfEmptyAsync()
        {
            var hashF   = new XXHash64();
            var hashing = new HashingService(hashF);
            var stream  = new MemoryStream(new byte[] { });
            var hash    = await hashing.GetFileHash(stream, CancellationToken.None);

            hash.Should().Be(hashF.HashOfEmpty);
        }
Esempio n. 11
0
 public unsafe void SystemImpl()
 {
     for (int i = 0; i < N; i++)
     {
         fixed(byte *p = bytes)
         {
             var hash1 = XXHash64.Hash(bytes, 0, Size, 0);
         }
     }
 }
Esempio n. 12
0
        public static List <WadResult> GenerateWADStrings(WADFile wad, List <WadResult> wadResults)
        {
            List <string> strings = new List <string>();

            foreach (WADEntry entry in wad.Entries.Where(x => x.Type != EntryType.FileRedirection))
            {
                byte[]         entryContent = entry.GetContent(true);
                LeagueFileType fileType     = Utilities.GetLeagueFileExtensionType(entryContent);

                if (fileType == LeagueFileType.BIN)
                {
                    BINFile bin = null;
                    try
                    {
                        bin = new BINFile(new MemoryStream(entryContent));
                        strings.AddRange(ProcessBINLinkedFiles(bin.LinkedFiles));
                        strings.AddRange(ProcessBINFile(bin));
                    }
                    catch (Exception excp)
                    {
                        throw new Exception(TAG + "解析wad文件失败,创建BIN文件失败...|" + excp.Message);
                    }
                }
            }
            Dictionary <ulong, string> stringDictionary = new Dictionary <ulong, string>();

            strings = strings.Distinct().ToList();
            using (XXHash64 xxHash = XXHash64.Create())
            {
                foreach (string fetchedString in strings)
                {
                    if (!string.IsNullOrEmpty(fetchedString))
                    {
                        // 计算路径的哈希值
                        byte[] b       = Encoding.ASCII.GetBytes(fetchedString.ToLower());
                        byte[] hashVal = xxHash.ComputeHash(b);
                        ulong  hash    = BitConverter.ToUInt64(hashVal, 0);
                        string hex     = BitConverter.ToString(hashVal, 0);

                        if (!stringDictionary.ContainsKey(hash))
                        {
                            stringDictionary.Add(hash, fetchedString);
                            WadResult wadResult = new WadResult();
                            wadResult.hash = hash;
                            wadResult.hex  = hex;
                            wadResult.name = fetchedString;
                            //wadResult.type = Utilities.GetLeagueFileExtensionType(b);
                            wadResults.Add(wadResult);
                        }
                    }
                }
            }
            GC.Collect();
            return(wadResults);
        }
Esempio n. 13
0
        public void CompHas()
        {
            using (var sw = File.CreateText(EXPORT_PATH))
            {
                foreach (var key in hashes)
                {
                    sw.WriteLine($"{key}\t\t = {XXHash64.Hash(0, key)}");
                }
            }

            Assert.True(File.Exists(EXPORT_PATH));
        }
Esempio n. 14
0
        private void AddFile(string path, byte[] data, bool compressed, bool refresh)
        {
            using (XXHash64 xxHash = XXHash64.Create())
            {
                ulong hash = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower())), 0);
                if (!StringDictionary.ContainsKey(hash))
                {
                    StringDictionary.Add(hash, path);
                }

                AddFile(hash, data, compressed, refresh);
            }
        }
Esempio n. 15
0
        public unsafe static void HashRandomBytesFor()
        {
            Random rd    = new Random(Guid.NewGuid().GetHashCode());
            ulong  hash1 = 0;

            for (int n = 1; n <= 32; n++)
            {
                byte[] bytes = new byte[n];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)rd.Next(0, byte.MaxValue);
                }

                Stopwatch w = Stopwatch.StartNew();
                for (int i = 0; i < 100000000; i++)
                {
                    hash1 = FastHash.Hash(bytes);
                }
                w.Stop();
                long costA = w.ElapsedMilliseconds;

                w = Stopwatch.StartNew();
                for (int i = 0; i < 100000000; i++)
                {
                    hash1 = XXHash64.Hash(bytes);
                }
                w.Stop();
                long costB = w.ElapsedMilliseconds;
                Console.WriteLine($"Length:{n.ToString().PadRight(3,' ')}    " +
                                  $"FastHash Cost:{costA.ToString().PadRight(5, ' ')}    " +
                                  $"XXHash64 Cost:{costB.ToString().PadRight(5, ' ')}    " +
                                  $"Times:{((float)costB/costA).ToString("f3").PadRight(5,' ')}");
            }

            //rd = new Random(Guid.NewGuid().GetHashCode());
            //for (int n = 1; n <= 32; n++)
            //{
            //    byte[] bytes = new byte[n];
            //    for (int i = 0; i < bytes.Length; i++)
            //        bytes[i] = (byte)rd.Next(0, byte.MaxValue);

            //    Stopwatch w = Stopwatch.StartNew();
            //    for (int i = 0; i < 100000000; i++)
            //    {
            //        hash1 = XXHash64.Hash(bytes);
            //    }

            //    w.Stop();
            //    Console.WriteLine($"length:{n} XXHash64 cost:{w.ElapsedMilliseconds}");
            //}
        }
Esempio n. 16
0
        public static Dictionary <ulong, string> Generate(WADFile wad)
        {
            Dictionary <ulong, string> hashtable = new Dictionary <ulong, string>();
            List <string> strings = new List <string>();

            foreach (WADEntry entry in wad.Entries.Where(x => x.Type != EntryType.FileRedirection))
            {
                byte[]         entryContent = entry.GetContent(true);
                LeagueFileType fileType     = LeagueUtilities.GetExtension(entryContent);

                if (fileType == LeagueFileType.BIN)
                {
                    BINFile bin = null;
                    try
                    {
                        bin = new BINFile(new MemoryStream(entryContent));
                    }
                    catch (Exception)
                    {
                    }

                    if (bin != null)
                    {
                        strings.AddRange(ProcessBINLinkedFiles(bin.LinkedFiles));
                        strings.AddRange(ProcessBINFile(bin));
                    }
                }
                else if (IsLegacyDirList(entry.XXHash))
                {
                    strings.AddRange(ProcessLegacyDirList(entry));
                }
            }

            using (XXHash64 xxHash = XXHash64.Create())
            {
                foreach (string fetchedString in strings.Distinct())
                {
                    if (!string.IsNullOrEmpty(fetchedString))
                    {
                        ulong hash = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(fetchedString.ToLower())), 0);

                        if (!hashtable.ContainsKey(hash))
                        {
                            hashtable.Add(hash, fetchedString);
                        }
                    }
                }
            }

            return(hashtable);
        }
Esempio n. 17
0
        public static Dictionary <ulong, string> Generate(Wad wad)
        {
            Dictionary <ulong, string> hashtable = new Dictionary <ulong, string>();
            List <string> strings = new List <string>();

            foreach (WadEntry entry in wad.Entries.Values.Where(x => x.Type != WadEntryType.FileRedirection))
            {
                using Stream entryStream = entry.GetDataHandle().GetDecompressedStream();
                LeagueFileType fileType = LeagueUtilities.GetExtensionType(entryStream);

                if (fileType == LeagueFileType.BIN)
                {
                    BinTree bin = null;
                    try
                    {
                        bin = new BinTree(entryStream);
                    }
                    catch (Exception)
                    {
                    }

                    if (bin != null)
                    {
                        strings.AddRange(ProcessBinDependencies(bin.Dependencies));
                        strings.AddRange(ProcessBinTree(bin));
                    }
                }
                else if (IsLegacyDirList(entry.XXHash))
                {
                    strings.AddRange(ProcessLegacyDirList(entry));
                }
            }

            using (XXHash64 xxHash = XXHash64.Create())
            {
                foreach (string fetchedString in strings.Distinct())
                {
                    if (!string.IsNullOrEmpty(fetchedString))
                    {
                        ulong hash = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(fetchedString.ToLower())), 0);

                        if (!hashtable.ContainsKey(hash))
                        {
                            hashtable.Add(hash, fetchedString);
                        }
                    }
                }
            }

            return(hashtable);
        }
Esempio n. 18
0
        public async Task ComputeHashAsync(byte[] expected, byte[] data)
        {
            //Arrange
            var expectedV = new HashValue(expected);
            var stream    = new MemoryStream(data);
            var hash      = new XXHash64();

            //Act
            var result = await hash.ComputeHashAsync(stream, CancellationToken.None);

            //Assert
            Assert.Equal(expectedV, result);
            Assert.Equal <byte>(expectedV.Value, result.Value);
        }
Esempio n. 19
0
        public void HashMatchesPregeneratedHashesForSmallBlocks(int length, ulong expected)
        {
            var bytes = new byte[length];

            for (var i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)i;
            }

            var actual  = XXHash64.DigestOf(bytes, 0, bytes.Length);
            var confirm = Theirs64(bytes);

            Assert.Equal(expected, confirm);
            Assert.Equal(expected, actual);
        }
Esempio n. 20
0
        private void menuImportHashtable_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog
            {
                Title       = "Select the Hashtable files you want to load",
                Multiselect = true,
                Filter      = "Text Files (*.txt)|*.txt"
            };

            if (dialog.ShowDialog() == true)
            {
                try
                {
                    foreach (string fileName in dialog.FileNames)
                    {
                        foreach (string line in File.ReadAllLines(fileName))
                        {
                            string[] lineSplit = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                            if (ulong.TryParse(lineSplit[0], out ulong hash) && !StringDictionary.ContainsKey(hash))
                            {
                                StringDictionary.Add(ulong.Parse(lineSplit[0]), lineSplit[1]);
                            }
                            else
                            {
                                using (XXHash64 xxHash = XXHash64.Create())
                                {
                                    ulong key = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(lineSplit[0].ToLower())), 0);
                                    if (!StringDictionary.ContainsKey(key))
                                    {
                                        StringDictionary.Add(key, lineSplit[0].ToLower());
                                    }
                                }
                            }
                        }

                        Logger.Info("Imported Hashtable from: " + fileName);
                    }
                }
                catch (Exception excp)
                {
                    Logging.LogException(Logger, "Failed to Import Hashtable", excp);
                    return;
                }

                CollectionViewSource.GetDefaultView(this.datagridWadEntries.ItemsSource).Refresh();
            }
        }
Esempio n. 21
0
        public static void GenerateWADStrings(WADFile wad, Dictionary <ulong, string> stringDictionary)
        {
            List <string> strings = new List <string>();

            foreach (WADEntry entry in wad.Entries.Where(x => x.Type != EntryType.FileRedirection))
            {
                byte[]         entryContent = entry.GetContent(true);
                LeagueFileType fileType     = Utilities.GetLeagueFileExtensionType(entryContent);

                if (fileType == LeagueFileType.BIN)
                {
                    BINFile bin = null;
                    try
                    {
                        bin = new BINFile(new MemoryStream(entryContent));
                    }
                    catch (Exception excp)
                    {
                        Console.WriteLine(excp.Message);
                    }

                    if (bin != null)
                    {
                        strings.AddRange(ProcessBINLinkedFiles(bin.LinkedFiles));
                        strings = strings.Distinct().ToList();

                        strings.AddRange(ProcessBINFile(bin));
                    }
                }
            }

            using (XXHash64 xxHash = XXHash64.Create())
            {
                for (int i = 0; i < strings.Count; i++)
                {
                    string fetchedString = strings[i];
                    if (!string.IsNullOrEmpty(fetchedString))
                    {
                        ulong hash = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(fetchedString.ToLower())), 0);

                        if (!stringDictionary.ContainsKey(hash))
                        {
                            stringDictionary.Add(hash, fetchedString);
                        }
                    }
                }
            }
        }
Esempio n. 22
0
        public void CalculatingHashInChunksReturnsSameResultAsInOneGo(int length, int chuck)
        {
            var bytes  = new byte[length];
            var random = new Random(0);

            random.NextBytes(bytes);

            var transform = new XXHash64();
            var i         = 0;

            while (i < length)
            {
                var l = Math.Min(chuck, length - i);
                transform.Update(bytes, i, l);
                i += l;
            }

            Assert.Equal(XXHash64.DigestOf(bytes, 0, bytes.Length), transform.Digest());
        }
Esempio n. 23
0
        public unsafe void EmptyHash()
        {
            var input = Array.Empty <byte>();

            var expected = Theirs64(input);

            var actual1 = XXHash64.DigestOf(input, 0, input.Length);

            Assert.Equal(expected, actual1);

            fixed(byte *inputPtr = input)
            {
                var actual2 = XXHash64.DigestOf(inputPtr, 0);

                Assert.Equal(expected, actual2);
            }

            var actual3 = XXHash64.EmptyHash;

            Assert.Equal(expected, actual3);
        }
Esempio n. 24
0
        private void menuImportHashtable_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog
            {
                Title            = "Select the Hashtable files you want to load",
                Multiselect      = true,
                Filter           = "Text Files (*.txt)|*.txt",
                InitialDirectory = (string)Config.Get("HashtableOpenDialogStartPath")
            };

            if (dialog.ShowDialog() == true)
            {
                try
                {
                    foreach (string fileName in dialog.FileNames)
                    {
                        foreach (string line in File.ReadAllLines(fileName))
                        {
                            using (XXHash64 xxHash = XXHash64.Create())
                            {
                                ulong hash = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(line.ToLower())), 0);
                                if (!StringDictionary.ContainsKey(hash))
                                {
                                    StringDictionary.Add(hash, line);
                                }
                            }
                        }

                        Logger.Info("Imported Hashtable from: " + fileName);
                    }
                }
                catch (Exception excp)
                {
                    Logging.LogException(Logger, "Failed to Import Hashtable", excp);
                    return;
                }

                CollectionViewSource.GetDefaultView(this.datagridWadEntries.ItemsSource).Refresh();
            }
        }
Esempio n. 25
0
        private void buttonAddFileAdd_Click(object sender, RoutedEventArgs e)
        {
            if (this.buttonAddFileOpen.Visibility == Visibility.Visible)
            {
                try
                {
                    this.Wad.AddEntry(this.textboxAddFilePath.Text, File.ReadAllBytes(this.textboxAddFileFilePath.Text), this.checkboxAddFileCompressed.IsChecked.Value);

                    using (XXHash64 xxHash = XXHash64.Create())
                    {
                        StringDictionary.Add(BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(this.textboxAddFilePath.Text.ToLower())), 0), this.textboxAddFilePath.Text);
                    }

                    CollectionViewSource.GetDefaultView(this.datagridWadEntries.ItemsSource).Refresh();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Please choose a different Path", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            else
            {
                try
                {
                    this.Wad.AddEntry(this.textboxAddFileFilePath.Text, this.textboxAddFilePath.Text);

                    using (XXHash64 xxHash = XXHash64.Create())
                    {
                        StringDictionary.Add(BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(this.textboxAddFileFilePath.Text.ToLower())), 0), this.textboxAddFileFilePath.Text);
                    }

                    CollectionViewSource.GetDefaultView(this.datagridWadEntries.ItemsSource).Refresh();
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message, "Please choose a different Path", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
        }
Esempio n. 26
0
        public unsafe static void HashRandomBytes()
        {
            Random rd = new Random(Guid.NewGuid().GetHashCode());

            byte[] bytes = new byte[1024];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = (byte)rd.Next(0, byte.MaxValue);
            }

            ulong hash1 = 0;
            ulong hash2 = 0;
            ulong hash3 = 0;
            ulong hash4 = 0;

            //Stopwatch w = Stopwatch.StartNew();
            //fixed (Byte* @in = &bytes[0])
            //{
            //    for (int i = 0; i < 10000000; i++)
            //    {
            //        //hash1 = XXHash64.Hash(@in, bytes.Length, 0);
            //        //XXHash64.Hash(@in, bytes.Length, ref hash1,ref hash2,0,0);
            //        //XXHash64.Hash(@in, bytes.Length, ref hash1, ref hash2,ref hash3, 0, 0,0);
            //        XXHash64.Hash(@in, bytes.Length, ref hash1, ref hash2, ref hash3,ref hash4, 0, 0, 0,0);
            //    }
            //}

            Stopwatch w = Stopwatch.StartNew();

            for (int i = 0; i < 10000000; i++)
            {
                hash1 = XXHash64.Hash(bytes);
                //hash2 = XXHash32.Hash(bytes);
            }

            w.Stop();
            Console.WriteLine($"cost:{w.ElapsedMilliseconds}");
            Console.WriteLine(hash1 == hash2);
        }
Esempio n. 27
0
        private static void AddColCheck(this CommandLineApplication app)
        {
            app.Command("collcheck", command =>
            {
                command.Description = "Checks files for collisions";
                command.HelpOption("-?|-h|--help");
                var pathArg = command.Argument("[path]", "Path to folder");

                command.OnExecute(() =>
                {
                    var pathStr          = pathArg.Value ?? ".";
                    var path             = Path.Combine(Directory.GetCurrentDirectory(), pathStr);
                    IHashFunction xxhash = new XXHash64();
                    var hashing          = new HashingService(xxhash);
                    hashing.GetFiles(new DirectoryInfo(path))
                    .Select(f =>
                    {
                        var hash = hashing.GetFileHash(f, CancellationToken.None).GetAwaiter().GetResult();
                        Console.WriteLine($"{hash} {Path.GetRelativePath(path, f.FullName)}");
                        return(new { f, hash });
                    })
                    .GroupBy(a => a.hash.ToString())
                    .Where(g => g.Count() > 1)
                    .ForEach(g =>
                    {
                        Console.WriteLine(g.Key);
                        foreach (var i in g)
                        {
                            Console.WriteLine($"    {i.f.FullName}");
                        }
                        Console.WriteLine();
                    });

                    Console.WriteLine("Done.");
                    return(0);
                });
            });
        }
Esempio n. 28
0
        public void importHashTable(string fileName)
        {
            foreach (string line in File.ReadAllLines(fileName))
            {
                string[] lineSplit = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                if (ulong.TryParse(lineSplit[0], out ulong hash) && !StringDictionary.ContainsKey(hash))
                {
                    StringDictionary.Add(ulong.Parse(lineSplit[0]), lineSplit[1]);
                }
                else
                {
                    using (XXHash64 xxHash = XXHash64.Create())
                    {
                        ulong key = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(lineSplit[0].ToLower())), 0);
                        if (!StringDictionary.ContainsKey(key))
                        {
                            StringDictionary.Add(key, lineSplit[0].ToLower());
                        }
                    }
                }
            }
        }
Esempio n. 29
0
        public List <String> importFolder(string folderPath)
        {
            var           list      = new List <string>();
            DirectoryInfo directory = new DirectoryInfo(folderPath);

            using (XXHash64 xxHash = XXHash64.Create())
            {
                foreach (FileInfo fileInfo in directory.EnumerateFiles("*", SearchOption.AllDirectories))
                {
                    string path       = fileInfo.FullName.Substring(directory.FullName.Length + 1);
                    ulong  hashedPath = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower())), 0);

                    if (!StringDictionary.ContainsKey(hashedPath))
                    {
                        StringDictionary.Add(hashedPath, path);
                    }

                    var entry = this.activeWad.AddEntry(hashedPath, File.ReadAllBytes(fileInfo.FullName), true);
                    mapEntries.Add(path, entry);
                    list.Add(path);
                }
            }
            return(list);
        }
Esempio n. 30
0
        private void menuCreateFromDirectory_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog()
            {
                ShowNewFolderButton = false
            };

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Wad?.Dispose();
                this.Wad         = new WADFile();
                StringDictionary = new Dictionary <ulong, string>();

                DirectoryInfo directory = new DirectoryInfo(dialog.SelectedPath);

                using (XXHash64 xxHash = XXHash64.Create())
                {
                    foreach (FileInfo fileInfo in directory.EnumerateFiles("*", SearchOption.AllDirectories))
                    {
                        string path = fileInfo.FullName.Substring(directory.FullName.Length + 1);
                        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
                        bool   hasUnknownPath           = fileNameWithoutExtension.All(c => "ABCDEF0123456789".Contains(c)) && fileNameWithoutExtension.Length <= 16;

                        if (hasUnknownPath)
                        {
                            ulong hashedPath = HexStringToUInt64(fileNameWithoutExtension);
                            this.Wad.AddEntry(hashedPath, File.ReadAllBytes(fileInfo.FullName), true);
                        }
                        else
                        {
                            ulong hashedPath = BitConverter.ToUInt64(xxHash.ComputeHash(Encoding.ASCII.GetBytes(path.ToLower())), 0);
                            if (!StringDictionary.ContainsKey(hashedPath))
                            {
                                StringDictionary.Add(hashedPath, path);
                            }

                            this.Wad.AddEntry(hashedPath, File.ReadAllBytes(fileInfo.FullName), true);
                        }
                    }
                }

                if ((bool)this.Config["GenerateWadDictionary"])
                {
                    try
                    {
                        WADHashGenerator.GenerateWADStrings(Logger, this.Wad, StringDictionary);
                    }
                    catch (Exception excp)
                    {
                        Logging.LogException(Logger, "Failed to Generate WAD String Dictionary", excp);
                    }
                }
                this.previewExpander.IsEnabled = true;

                this.menuSave.IsEnabled               = true;
                this.menuImportHashtable.IsEnabled    = true;
                this.menuExportHashtable.IsEnabled    = true;
                this.menuExportAll.IsEnabled          = true;
                this.menuAddFile.IsEnabled            = true;
                this.menuAddFileRedirection.IsEnabled = true;
                this.menuAddFolder.IsEnabled          = true;
                this.CurrentlySelectedEntry           = null;
                this.datagridWadEntries.ItemsSource   = this.Wad.Entries;

                Logger.Info("Created WAD file from directory: " + dialog.SelectedPath);
                CollectionViewSource.GetDefaultView(this.datagridWadEntries.ItemsSource).Refresh();
            }
        }