Пример #1
0
        public void IndependentBlockDecoder(string filename, string options)
        {
            var original = Tools.FindFile($".corpus/{filename}");
            var encoded  = Path.GetTempFileName();
            var decoded  = Path.GetTempFileName();

            try
            {
                ReferenceLZ4.Encode(options, original, encoded);
                using (var decoder = new LZ4DecoderStream(
                           File.OpenRead(encoded),
                           fi => new LZ4BlockDecoder(fi.BlockSize)))
                    using (var writer = File.OpenWrite(decoded))
                    {
                        decoder.CopyTo(writer);
                    }

                Tools.SameFiles(original, decoded);
            }
            finally
            {
                File.Delete(encoded);
                File.Delete(decoded);
            }
        }
Пример #2
0
 /// <inheritdoc />
 public byte[] Decompress(byte[] source)
 {
     using (LZ4DecoderStream stream = LZ4Stream.Decode(new MemoryStream(source)))
     {
         var output = new MemoryStream();
         stream.CopyTo(output);
         return(output.ToArray());
     }
 }
Пример #3
0
        private static byte[] DecodeLZ4(string path)
        {
            using (LZ4DecoderStream source = LZ4Stream.Decode(File.OpenRead(path)))
                using (MemoryStream target = new MemoryStream(440))
                {
                    source.CopyTo(target);

                    return(target.ToArray());
                }
        }
Пример #4
0
        public void ManagedLZ4()
        {
            byte[] buffer = _srcFileBytesDict[DecompMethod.ManagedLZ4];

            using (MemoryStream decompMs = new MemoryStream((int)_magicFileLen))
                using (MemoryStream compMs = new MemoryStream(buffer))
                    using (LZ4DecoderStream ls = LZ4Stream.Decode(compMs))
                    {
                        ls.CopyTo(decompMs);
                    }
        }
 public void BlockDecoder()
 {
     Input.Position = 0;
     using (var decoded = new LZ4DecoderStream(
                Input, f => new LZ4BlockDecoder(f.BlockSize), true))
     {
         var buffer = new byte[0x1000];
         while (true)
         {
             var read = decoded.Read(buffer, 0, buffer.Length);
             if (read == 0)
             {
                 break;
             }
         }
     }
 }
Пример #6
0
        // Creates a content stream to read from a debug item
        internal ContentStream(string item, FileStream fstream, uint rlen, uint uclen)
        {
            Item       = item;
            Offset     = DCI_HEADER_LENGTH;
            RealSize   = rlen;
            UCSize     = uclen;
            Compressed = (rlen != uclen);
            IsRelease  = false;

            _file = fstream;
            _file.Seek(Offset, SeekOrigin.Begin);             // Probably already there, but should make sure
            if (Compressed)
            {
                _decompressor = LZ4Stream.Decode(_file, 0, leaveOpen: true);
            }
            Reader    = new BinaryReader(Compressed ? (Stream)_decompressor : (Stream)_file, s_encoding, true);
            _ownsFile = false;
            _pos      = 0;
        }
Пример #7
0
        public static void Decode(string encoded, string decoded, int chunkSize)
        {
            using (var input = File.OpenRead(encoded))
                using (var output = File.Create(decoded))
                    using (var decode = new LZ4DecoderStream(input, i => new LZ4ChainDecoder(i.BlockSize, 0)))
                    {
                        var buffer = new byte[chunkSize];
                        while (true)
                        {
                            var read = decode.Read(buffer, 0, buffer.Length);
                            if (read == 0)
                            {
                                break;
                            }

                            output.Write(buffer, 0, read);
                        }
                    }
        }
        // TODO: add TExpected
        public static ImaginaryObject UnpackImaginaryObject(string path)
        {
            Encoding encoding = Encoding.UTF8;

            using (var fileStream = new FileStream(path, FileMode.Open))
#if DEBUG
                // BinaryReader without decompression if the program is being debugged.
                using (var reader = new BinaryReader(fileStream, encoding))
#else
                using (LZ4DecoderStream decompressionStream = LZ4Stream.Decode(fileStream))
                    using (BinaryReader reader = new BinaryReader(decompressionStream, encoding))
#endif
                {
                    // Read the version of CrystalClear that this pack file was created in.
                    var fileCreatedInVersion = new Version(reader.ReadString());

                    // Is the pack file from an older version of CrystalClear?
                    if (fileCreatedInVersion < CrystalClearInformation.CrystalClearVersion)
                    {
                        // The version that this file was created in is older than the current version.
                        Output.ErrorLog(
                            $"This file was created in an older version of the CrystalClear Engine. {fileCreatedInVersion} (file) < {CrystalClearInformation.CrystalClearVersion} (current)");
                    }

                    // Is the pack file from a newer version of CrystalClear?
                    else if (fileCreatedInVersion > CrystalClearInformation.CrystalClearVersion)
                    {
                        // The version that this file was created in is newer than the current version.
                        Output.ErrorLog(
                            $"This file was created in a newer version of the CrystalClear Engine. {fileCreatedInVersion} (file) > {CrystalClearInformation.CrystalClearVersion} (current)");
                    }

                    ImaginaryObject unpacked;

                    unpacked = ImaginaryObject.ReadImaginaryObject(reader, out _);

                    return(unpacked);
                }
        }
Пример #9
0
        private Dictionary <string, FPakEntry> GetOldFiles(EPakLoader mode)
        {
            var diff = new Dictionary <string, FPakEntry>();
            var ofd  = new OpenFileDialog()
            {
                Title            = Properties.Resources.SelectFile,
                InitialDirectory = Properties.Settings.Default.OutputPath + "\\Backups\\",
                Filter           = Properties.Resources.FbkpFilter,
                Multiselect      = false
            };

            if ((bool)ofd.ShowDialog())
            {
                string n = Path.GetFileName(ofd.FileName);
                StatusBarVm.statusBarViewModel.Set(string.Format(Properties.Resources.Analyzing, n), Properties.Resources.Processing);
                DebugHelper.WriteLine("{0} {1} {2} {3}", "[FModel]", "[PakMenuItemViewModel]", "[Loader]", $"Backup file is {n}");

                var oldFilesTemp = new Dictionary <string, FPakEntry>();
                using FileStream fileStream = new FileStream(ofd.FileName, FileMode.Open);
                BinaryReader checkReader = new BinaryReader(fileStream);
                bool         isLz4       = checkReader.ReadUInt32() == 0x184D2204u;
                fileStream.Seek(0, SeekOrigin.Begin);
                var target = new MemoryStream();
                if (isLz4)
                {
                    using LZ4DecoderStream compressionStream = LZ4Stream.Decode(fileStream);
                    compressionStream.CopyTo(target);
                }
                else
                {
                    fileStream.CopyTo(target);
                }
                using (target)
                {
                    target.Position           = 0;
                    using BinaryReader reader = new BinaryReader(target);
                    while (reader.BaseStream.Position < reader.BaseStream.Length)
                    {
                        // we must follow this order
                        long   offset                 = reader.ReadInt64();
                        long   size                   = reader.ReadInt64();
                        long   uncompressedSize       = reader.ReadInt64();
                        bool   encrypted              = reader.ReadBoolean();
                        long   structSize             = reader.ReadInt32();
                        string name                   = reader.ReadString();
                        int    compressionMethodIndex = reader.ReadInt32();

                        // we only need name and uncompressedSize to compare
                        FPakEntry entry = new FPakEntry("CatsWillDominateTheWorld.pak", name, offset, size, uncompressedSize, new byte[20], null, 0, (uint)compressionMethodIndex, 0);
                        oldFilesTemp[entry.Name] = entry;
                    }
                }

                var newFiles = new Dictionary <string, FPakEntry>();
                foreach (var fileReader in Globals.CachedPakFiles)
                {
                    foreach (var files in fileReader.Value)
                    {
                        newFiles.Add(files.Key, files.Value);
                    }
                }

                Paks.Merge(oldFilesTemp, out var oldFiles, string.Empty);

                switch (mode)
                {
                case EPakLoader.New:
                    foreach (var kvp in newFiles)
                    {
                        if (!oldFiles.TryGetValue(kvp.Key, out var entry))
                        {
                            diff.Add(kvp.Key, kvp.Value);
                        }
                    }
                    break;

                case EPakLoader.Modified:
                    foreach (var kvp in newFiles)
                    {
                        if (oldFiles.TryGetValue(kvp.Key, out var entry))
                        {
                            if (entry.UncompressedSize != kvp.Value.UncompressedSize)
                            {
                                diff.Add(kvp.Key, kvp.Value);
                            }
                        }
                    }
                    break;

                case EPakLoader.NewModified:
                    foreach (var kvp in newFiles)
                    {
                        if (oldFiles.TryGetValue(kvp.Key, out var entry))
                        {
                            if (entry.UncompressedSize != kvp.Value.UncompressedSize)
                            {
                                diff.Add(kvp.Key, kvp.Value);
                            }
                        }
                        else
                        {
                            diff.Add(kvp.Key, kvp.Value);
                        }
                    }
                    break;
                }

                var deleted = oldFiles.Where(kvp => !newFiles.TryGetValue(kvp.Key, out var _) && kvp.Key.StartsWith("/FortniteGame/Content/Athena/Items/Cosmetics/")).ToDictionary(x => x.Key, x => x.Value);
                if (deleted.Count > 0)
                {
                    FConsole.AppendText(Properties.Resources.RemovedRenamedCosmetics, FColors.Red, true);
                    foreach (var kvp in deleted)
                    {
                        FConsole.AppendText($"    - {kvp.Value.Name.Substring(1)}", FColors.LightGray, true);
                    }
                }
            }
            return(diff);
        }
Пример #10
0
        public bool ProcessFile(string sourcePath, string destDir, bool respectFileNameInMeta = true)
        {
            string destPath = null;

            try
            {
                FileItem fi = new FileItem(sourcePath);
                using (CloudSyncFile cloudSyncFile = new CloudSyncFile(fi, _handlerFactory))
                {
                    cloudSyncFile.InitParsing();
                    FileMeta3 fileMeta = cloudSyncFile.GetFileMeta();

                    //Generate session key and make sure it matches the file
                    byte[] sessionKeyComputed =
                        CryptoUtils.RsaOaepDeciper(fileMeta.EncKey2, this._cloudSyncKey.KeyPair.Private);
                    string sessionKeyHashStrComputed = CryptoUtils.SaltedMd5(
                        fileMeta.SessionKeyHash.Substring(0, 10), sessionKeyComputed);

                    if (!fileMeta.SessionKeyHash.Equals(sessionKeyHashStrComputed))
                    {
                        throw new InvalidDataException($"File {fi.Name}, Computed session key is incorrect.");
                    }

                    //decrypt content
                    byte[] sessionKeyBytes = BytesUtils.HexStringToByteArray(
                        Encoding.ASCII.GetString(sessionKeyComputed));
                    ParametersWithIV keys =
                        CryptoUtils.DeriveAESKeyParameters(sessionKeyBytes, null);
                    AesCbcCryptor decryptor =
                        new AesCbcCryptor(((KeyParameter)keys.Parameters).GetKey(), keys.GetIV());

                    destPath = Path.Join(destDir,
                                         respectFileNameInMeta ? fileMeta.FileName : Path.GetFileName(sourcePath));

                    using (var hasher = MD5.Create())
                    {
                        using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                        {
                            aes.Mode = CipherMode.CBC;
                            aes.Key  = ((KeyParameter)keys.Parameters).GetKey();
                            aes.IV   = keys.GetIV();
                            //Stopwatch stopwatch =  new Stopwatch();
                            //stopwatch.Start();
                            //byte[] buffer = new byte[1024 * 1024];
                            long             bytesRead = 0;
                            ICryptoTransform decoder   = aes.CreateDecryptor();
                            using (CloudSyncPayloadStream cspls =
                                       new CloudSyncPayloadStream(cloudSyncFile.GetDataBlocks(decryptor)))
                                using (CryptoStream aesStream = new CryptoStream(cspls, decoder, CryptoStreamMode.Read))
                                    using (LZ4DecoderStream lz4ds = LZ4Stream.Decode(aesStream))
                                        using (FileStream writeFs = new FileStream(destPath, FileMode.OpenOrCreate,
                                                                                   FileAccess.ReadWrite, FileShare.ReadWrite, 1024 * 1024))
                                            using (CryptoStream md5HashStream =
                                                       new CryptoStream(writeFs, hasher, CryptoStreamMode.Write))
                                            {
                                                lz4ds.CopyTo(md5HashStream, 1024 * 1024);
                                                // int read;
                                                // while ((read = md5HashStream.Read(buffer, 0, buffer.Length)) > 0)
                                                // {
                                                //     //do nothing
                                                //     bytesRead += read;
                                                //     long elapsed = stopwatch.ElapsedMilliseconds;
                                                //     if (elapsed > 1000)
                                                //     {
                                                //         double readInM = (double) bytesRead / 1024.0 / 1024.0/elapsed*1000.0;
                                                //         bytesRead = 0;
                                                //         Console.WriteLine($"Speed:{readInM} M/s");
                                                //         stopwatch.Reset();
                                                //         stopwatch.Start();
                                                //     }
                                                // }
                                            }

                            //stopwatch.Stop();
                            if (!cloudSyncFile.VerifyContentHash(hasher.Hash))
                            {
                                throw new InvalidDataException("File Md5 doesn't match.");
                            }
                        }
                    }

                    return(true);
                }
            }
            catch (Exception ex)
            {
                if (File.Exists(destPath))
                {
                    File.Delete(destPath);
                }

                this._exceptionHandler.Handle(ex);
            }

            return(false);
        }