protected void GetHeaderCRC() { if (!_validCrc) { _validCrc = CRC64.Hash(0, Encoding.ASCII.GetBytes("123456789")) == 0xe9c6d914c4b8d9ca; if (!_validCrc) { throw new Exception("CRC64 is invalid!"); } } byte[] buffer = new byte[36]; Stream.Read(buffer, 0, 36); _headerCrc = CRC64.Hash(0, buffer); }
public static ProjectData.HashList <ulong> LoadListsSubFatNames(this ProjectData.Project project, int bigVersion) { if (bigVersion >= 9) // TODO: check if this is right... { return(project.LoadLists("*.subfatlist", a => CRC64.Hash(a.ToLowerInvariant()), s => s.Replace("/", "\\"))); } return(project.LoadLists("*.subfatlist", a => (ulong)CRC32.Hash(a.ToLowerInvariant()), s => s.Replace("\\", "/"))); }
public static ProjectData.HashList <ulong> LoadListsFileNames(this ProjectData.Manager manager, int bigVersion) { if (bigVersion >= 9) // TODO: check if this is right... { return(manager.LoadLists("*.filelist", a => CRC64.Hash(a.ToLowerInvariant()), s => s.Replace("/", "\\"))); } return(manager.LoadLists("*.filelist", a => (ulong)CRC32.Hash(a.ToLowerInvariant()), s => s.Replace("\\", "/"))); }
public static byte[] Serialize(FieldType fieldType, string text) { switch (fieldType) { case FieldType.Boolean: { bool value; if (bool.TryParse(text, out value) == false) { throw new FormatException(); } return(new[] { (byte)(value == true ? 1 : 0) }); } case FieldType.UInt8: { byte value; if (TryParseUInt8(text, out value) == false) { throw new FormatException(); } return(new[] { value }); } case FieldType.Int8: { sbyte value; if (TryParseInt8(text, out value) == false) { throw new FormatException(); } return(new[] { (byte)value }); } case FieldType.UInt16: { ushort value; if (TryParseUInt16(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Int16: { short value; if (TryParseInt16(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.UInt32: { uint value; if (TryParseUInt32(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Int32: { int value; if (TryParseInt32(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.UInt64: { ulong value; if (TryParseUInt64(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Int64: { long value; if (TryParseInt64(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Float32: { float value; if (TryParseFloat32(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Float64: { double value; if (TryParseFloat64(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Vector2: { var parts = text.Split(','); if (parts.Length != 2) { throw new FormatException("field type Vector2 requires 2 float values delimited by a comma"); } float x, y; if (TryParseFloat32(parts[0], out x) == false) { throw new FormatException(); } if (TryParseFloat32(parts[1], out y) == false) { throw new FormatException(); } var data = new byte[8]; Array.Copy(BitConverter.GetBytes(x), 0, data, 0, 4); Array.Copy(BitConverter.GetBytes(y), 0, data, 4, 4); return(data); } case FieldType.Vector3: { var parts = text.Split(','); if (parts.Length != 3) { throw new FormatException("field type Vector3 requires 3 float values delimited by a comma"); } float x, y, z; if (TryParseFloat32(parts[0], out x) == false) { throw new FormatException(); } if (TryParseFloat32(parts[1], out y) == false) { throw new FormatException(); } if (TryParseFloat32(parts[2], out z) == false) { throw new FormatException(); } var data = new byte[12]; Array.Copy(BitConverter.GetBytes(x), 0, data, 0, 4); Array.Copy(BitConverter.GetBytes(y), 0, data, 4, 4); Array.Copy(BitConverter.GetBytes(z), 0, data, 8, 4); return(data); } case FieldType.Vector4: { var parts = text.Split(','); if (parts.Length != 4) { throw new FormatException("field type Vector4 requires 4 float values delimited by a comma"); } float x, y, z, w; if (TryParseFloat32(parts[0], out x) == false) { throw new FormatException(); } if (TryParseFloat32(parts[1], out y) == false) { throw new FormatException(); } if (TryParseFloat32(parts[2], out z) == false) { throw new FormatException(); } if (TryParseFloat32(parts[3], out w) == false) { throw new FormatException(); } var data = new byte[16]; Array.Copy(BitConverter.GetBytes(x), 0, data, 0, 4); Array.Copy(BitConverter.GetBytes(y), 0, data, 4, 4); Array.Copy(BitConverter.GetBytes(z), 0, data, 8, 4); Array.Copy(BitConverter.GetBytes(w), 0, data, 12, 4); return(data); } case FieldType.String: { var data = Encoding.UTF8.GetBytes(text); Array.Resize(ref data, data.Length + 1); return(data); } case FieldType.Hash32: { uint value; if (TryParseHash32(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Hash64: { ulong value; if (TryParseHash64(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Id32: { uint value; if (TryParseUInt32(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.Id64: { ulong value; if (TryParseUInt64(text, out value) == false) { throw new FormatException(); } return(BitConverter.GetBytes(value)); } case FieldType.ComputeHash32: { var value = CRC32.Hash(text); return(BitConverter.GetBytes(value)); } case FieldType.ComputeHash64: { var value = CRC64.Hash(text); return(BitConverter.GetBytes(value)); } } throw new NotSupportedException("unsupported field type"); }
private static void Main(string[] args) { bool showHelp = false; bool verbose = false; bool compress = false; int packageVersion = 9; Big.Platform packagePlatform = Big.Platform.PC; var options = new OptionSet() { { "v|verbose", "be verbose", v => verbose = v != null }, { "c|compress", "compress data with LZO1x", v => compress = v != null }, { "pv|package-version", "package version (default 9)", v => packageVersion = ParsePackageVersion(v) }, { "pp|package-platform", "package platform (default PC)", v => packagePlatform = ParsePackagePlatform(v) }, { "h|help", "show this message and exit", v => showHelp = v != null }, }; List <string> extras; try { extras = options.Parse(args); } catch (OptionException e) { Console.Write("{0}: ", GetExecutableName()); Console.WriteLine(e.Message); Console.WriteLine("Try `{0} --help' for more information.", GetExecutableName()); return; } if (extras.Count < 1 || showHelp == true) { Console.WriteLine("Usage: {0} [OPTIONS]+ output_fat input_directory+", GetExecutableName()); Console.WriteLine(); Console.WriteLine("Pack files from input directories into a Big File (FAT/DAT pair)."); Console.WriteLine(); Console.WriteLine("Options:"); options.WriteOptionDescriptions(Console.Out); return; } var inputPaths = new List <string>(); string fatPath, datPath; if (extras.Count == 1) { inputPaths.Add(extras[0]); fatPath = Path.ChangeExtension(extras[0], ".fat"); datPath = Path.ChangeExtension(extras[0], ".dat"); } else { fatPath = extras[0]; if (Path.GetExtension(fatPath) != ".fat") { datPath = fatPath; fatPath = Path.ChangeExtension(datPath, ".fat"); } else { datPath = Path.ChangeExtension(fatPath, ".dat"); } inputPaths.AddRange(extras.Skip(1)); } var pendingEntries = new SortedDictionary <ulong, PendingEntry>(); if (verbose == true) { Console.WriteLine("Finding files..."); } foreach (var relativePath in inputPaths) { string inputPath = Path.GetFullPath(relativePath); if (inputPath.EndsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)) == true) { inputPath = inputPath.Substring(0, inputPath.Length - 1); } foreach (string path in Directory.GetFiles(inputPath, "*", SearchOption.AllDirectories)) { PendingEntry pendingEntry; string fullPath = Path.GetFullPath(path); string partPath = fullPath.Substring(inputPath.Length + 1).ToLowerInvariant(); pendingEntry.FullPath = fullPath; pendingEntry.PartPath = partPath; var pieces = partPath.Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); int index = 0; if (index >= pieces.Length) { continue; } if (pieces[index].ToUpperInvariant() == "__SUBFAT") { Console.WriteLine("Sorry, packing of subfats is not currently implemented."); return; } if (index >= pieces.Length) { continue; } if (pieces[index].ToUpperInvariant() == "__UNKNOWN") { var partName = Path.GetFileNameWithoutExtension(partPath); if (string.IsNullOrEmpty(partName) == true) { continue; } if (partName.Length > 16) { partName = partName.Substring(0, 16); } pendingEntry.Name = null; pendingEntry.NameHash = ulong.Parse(partName, NumberStyles.AllowHexSpecifier); } else { pendingEntry.Name = string.Join("\\", pieces.Skip(index).ToArray()).ToLowerInvariant(); pendingEntry.NameHash = CRC64.Hash(pendingEntry.Name); } if (pendingEntries.ContainsKey(pendingEntry.NameHash) == true) { Console.WriteLine("Ignoring duplicate of {0:X}: {1}", pendingEntry.NameHash, partPath); if (verbose == true) { Console.WriteLine(" Previously added from: {0}", pendingEntries[pendingEntry.NameHash].PartPath); } continue; } pendingEntries[pendingEntry.NameHash] = pendingEntry; } } var fat = new BigFile { Version = packageVersion, Platform = packagePlatform }; // reasonable default? // need to figure out what this value actually does if (packagePlatform == Big.Platform.PC) { fat.Unknown74 = 3; } else if (packagePlatform == Big.Platform.PS3 || packagePlatform == Big.Platform.X360) { fat.Unknown74 = 4; } else { throw new InvalidOperationException(); } using (var output = File.Create(datPath)) { long current = 0; long total = pendingEntries.Count; var padding = total.ToString(CultureInfo.InvariantCulture).Length; foreach (var pendingEntry in pendingEntries.Select(kv => kv.Value)) { current++; if (verbose == true) { Console.WriteLine("[{0}/{1}] {2}", current.ToString(CultureInfo.InvariantCulture).PadLeft(padding), total, pendingEntry.PartPath); } var entry = new Big.Entry(); entry.NameHash = pendingEntry.NameHash; entry.Offset = output.Position; using (var input = File.OpenRead(pendingEntry.FullPath)) { EntryCompression.Compress(fat.Platform, ref entry, input, compress, output); output.Seek(output.Position.Align(16), SeekOrigin.Begin); } fat.Entries.Add(entry); } } using (var output = File.Create(fatPath)) { fat.Serialize(output); } }
/// <summary> /// Actions to do when user clicks the 'Go' button /// </summary> /// <param name="sender">Sender of Click event</param> /// <param name="e">Arguments for Click event</param> private void btGoClick(object sender, EventArgs e) { // No text to crypt/decrypt? finish if (txtText.Text == "") { return; } // According to selected crypt algorithm... switch (cbType.SelectedIndex) { case 0: // CryptStream3DES // No key and initializer values? finish if ((txtKey.Text == "") || (txtIV.Text == "")) { return; } // If we are encrypting, use according texts for encrypt and decrypt if (tbSide.Value == 0) { txtCrypt.Text = CryptStream3DES.Encrypt(txtText.Text, txtKey.Text, txtIV.Text); txtDecrypt.Text = CryptStream3DES.Decrypt(txtCrypt.Text, txtKey.Text, txtIV.Text); } else { // If we are decrypting, take the inverse text values txtCrypt.Text = CryptStream3DES.Decrypt(txtText.Text, txtKey.Text, txtIV.Text); txtDecrypt.Text = CryptStream3DES.Encrypt(txtCrypt.Text, txtKey.Text, txtIV.Text); } break; case 1: // Crypt3DES // No key? finish if (txtKey.Text == "") { return; } // If we are encrypting, use according text for encrypt and decrypt if (tbSide.Value == 0) { txtCrypt.Text = Crypt3DES.Encrypt(txtText.Text, txtKey.Text); txtDecrypt.Text = Crypt3DES.Decrypt(txtCrypt.Text, txtKey.Text); } else { // If we are decrypting, use the inverse text values txtCrypt.Text = Crypt3DES.Decrypt(txtText.Text, txtKey.Text); txtDecrypt.Text = Crypt3DES.Encrypt(txtCrypt.Text, txtKey.Text); } break; case 2: // CryptAES // No key and initializer? finish if ((txtKey.Text == "") || (txtIV.Text == "")) { return; } // Use right texts to encrypt/decrypt, based on our setup (also use provided key and salt) if (tbSide.Value == 0) { txtCrypt.Text = CryptAES.Encrypt(txtText.Text, txtKey.Text, txtIV.Text); txtDecrypt.Text = CryptAES.Decrypt(txtCrypt.Text, txtKey.Text, txtIV.Text); } else { txtCrypt.Text = CryptAES.Decrypt(txtText.Text, txtKey.Text, txtIV.Text); txtDecrypt.Text = CryptAES.Encrypt(txtCrypt.Text, txtKey.Text, txtIV.Text); } break; case 3: // UUCode // Apply encoding/decoding to right texts according to setup if (tbSide.Value == 0) { txtCrypt.Text = UUCoder.Encode(txtText.Text); txtDecrypt.Text = UUCoder.Decode(txtCrypt.Text); } else { txtCrypt.Text = UUCoder.Decode(txtText.Text); txtDecrypt.Text = UUCoder.Encode(txtCrypt.Text); } break; case 4: // Mime/Base64 // Apply encoding/decoding to right texts according to setup if (tbSide.Value == 0) { txtCrypt.Text = Base64.EncodeString(txtText.Text); txtDecrypt.Text = Base64.DecodeString(txtCrypt.Text); } else { txtCrypt.Text = Base64.DecodeString(txtText.Text); txtDecrypt.Text = Base64.EncodeString(txtCrypt.Text); } break; case 5: // Adler32 // Calculate hash for given text... txtCrypt.Text = string.Format("{0:X}", Adler32.AdlerHash(txtText.Text)); break; case 6: // CRC16 // Calculate hash for given text... txtCrypt.Text = string.Format("{0:X}", CRC16.Hash(txtText.Text)); break; case 7: // CRC32 // Calculate hash for given text... txtCrypt.Text = string.Format("{0:X}", CRC32.Hash(txtText.Text)); break; case 8: // CRC64 // Calculate hash for given text... txtCrypt.Text = string.Format("{0:X}", CRC64.Hash(txtText.Text)); break; case 9: // Salsa20 // No key and initializer? finish if ((txtKey.Text == "") || (txtIV.Text == "")) { return; } // Use cryptor to encrypt/decrypt data, according to setup using (var salsa = new CryptSalsa(txtKey.Text, txtIV.Text)) { // Encrypt -> Decrypt if (tbSide.Value == 0) { txtCrypt.Text = salsa.Encrypt(txtText.Text); txtDecrypt.Text = salsa.Decrypt(txtCrypt.Text); } else { // Or Decrypt -> Encrypt txtCrypt.Text = salsa.Decrypt(txtText.Text); txtDecrypt.Text = salsa.Encrypt(txtCrypt.Text); } } break; case 10: // ChaCha20 // No key/initializer? finish if ((txtKey.Text == "") || (txtIV.Text == "")) { return; } // Use cryptor to encrypt/decrypt data, according to setup using (var chacha = new CryptChaCha20(txtKey.Text, txtIV.Text)) { // Encrypt -> Decrypt if (tbSide.Value == 0) { txtCrypt.Text = chacha.Encrypt(txtText.Text); txtDecrypt.Text = chacha.Decrypt(txtCrypt.Text); } else { // Or Decrypt -> Encrypt txtCrypt.Text = chacha.Decrypt(txtText.Text); txtDecrypt.Text = chacha.Encrypt(txtCrypt.Text); } } break; case 11: // RSA // if no key/initializer value, finish if ((txtKey.Text == "") || (txtIV.Text == "")) { return; } using (var rsa = new CryptRSA(txtKey.Text, keySizes[cbKeySize.SelectedIndex])) { // Use private key? if (cbPrivateKey.Checked) { // Use cryptor to encrypt/decrypt data, according to setup if (tbSide.Value == 0) { // Encrypt -> Decrypt txtCrypt.Text = rsa.PrivateEncrypt(txtText.Text); txtDecrypt.Text = rsa.PublicDecrypt(txtCrypt.Text); } else { // Or Decrypt -> Encrypt txtCrypt.Text = rsa.PublicDecrypt(txtText.Text); txtDecrypt.Text = rsa.PrivateEncrypt(txtCrypt.Text); } } else { // Nope, use public key... // Use cryptor to encrypt/decrypt data, according to setup if (tbSide.Value == 0) { // Encrypt -> Decrypt txtCrypt.Text = rsa.PublicEncrypt(txtText.Text); txtDecrypt.Text = rsa.PrivateDecrypt(txtCrypt.Text); } else { // Or Decrypt -> Encrypt txtCrypt.Text = rsa.PrivateDecrypt(txtText.Text); txtDecrypt.Text = rsa.PublicEncrypt(txtCrypt.Text); } } } break; case 12: // Blowfish // No key? finish if (txtKey.Text == "") { return; } // Use cryptor to encrypt/decrypt data, according to setup using (var blowfish = new CryptBlowfish(txtKey.Text)) { // Encrypt -> Decrypt if (tbSide.Value == 0) { txtCrypt.Text = blowfish.Encrypt(txtText.Text); txtDecrypt.Text = blowfish.Decrypt(txtCrypt.Text); } else { // Or Decrypt -> Encrypt txtCrypt.Text = blowfish.Decrypt(txtText.Text); txtDecrypt.Text = blowfish.Encrypt(txtCrypt.Text); } } break; } }