Esempio n. 1
0
        internal static async Task <MS2File> Load(MS2CryptoMode cryptoMode, Stream headerStream, Stream dataStream, MemoryMappedFile dataMemoryMappedFile)
        {
            MS2FileInfoHeader fileInfoHeader = await MS2FileInfoHeader.Load(headerStream).ConfigureAwait(false);

            MS2FileHeader fileHeader = await MS2FileHeader.Load(cryptoMode, dataStream).ConfigureAwait(false);

            DLogger.Write($"Id={fileInfoHeader.Id}-{fileHeader.Id}, CompressionId={fileHeader.CompressionType}, RootFolder={fileInfoHeader.RootFolderId}, Name={fileInfoHeader.Name}, Size={FileEx.FormatStorage(fileHeader.EncodedSize)}->{FileEx.FormatStorage(fileHeader.CompressedSize)}->{FileEx.FormatStorage(fileHeader.Size)}");

            var file = new MS2File(fileInfoHeader, fileHeader, cryptoMode, dataMemoryMappedFile);

            return(file);
        }
Esempio n. 2
0
        private static async Task <List <MS2File> > LoadFiles(MS2CryptoMode cryptoMode, MS2SizeHeader header, MS2SizeHeader data, uint fileCount, BinaryReader br, MemoryMappedFile dataMemoryMappedFile)
        {
            var files = new List <MS2File>((int)fileCount);

            // TODO: are those always compressed?
            using (Stream headerStream = await DecryptStreamToStreamAsync(cryptoMode, header, true, br.BaseStream).ConfigureAwait(false))
                using (Stream dataStream = await DecryptStreamToStreamAsync(cryptoMode, data, true, br.BaseStream).ConfigureAwait(false))
                {
                    for (int i = 0; i < fileCount; i++)
                    {
                        files.Add(await MS2File.Load(cryptoMode, headerStream, dataStream, dataMemoryMappedFile).ConfigureAwait(false));
                    }

                    return(files);
                }
        }
Esempio n. 3
0
 public static MS2File CreateUpdate(MS2File existing, Stream dataStream)
 => new MS2File(existing.InfoHeader, existing.CompressionType, existing.ArchiveCryptoMode, dataStream);
Esempio n. 4
0
 public static MS2File CreateUpdate(MS2File existing, string dataFilePath)
 => new MS2File(existing.InfoHeader, existing.CompressionType, existing.ArchiveCryptoMode, File.OpenRead(dataFilePath));
Esempio n. 5
0
        private static async Task SaveSync(MS2CryptoMode cryptoMode, MS2File[] files, Stream headerStream, Stream dataStream)
        {
            MS2SizeHeader header;
            MS2SizeHeader dataHeader;
            Stream        encryptedHeaderStream;
            Stream        encryptedDataHeaderStream;

            using (var archiveInfoHeaderStream = new MemoryStream())
                using (var archiveHeaderStream = new MemoryStream())
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        MS2File file   = files[i];
                        uint    offset = (uint)dataStream.Position;

                        (Stream Stream, bool ShouldDispose, MS2SizeHeader Header)data = await file.GetEncryptedStreamAsync().ConfigureAwait(false);

                        try
                        {
                            var fileHeader = MS2FileHeader.Create(file.Id, offset, file.CompressionType, data.Header);

                            await file.InfoHeader.Save(archiveInfoHeaderStream).ConfigureAwait(false);

                            await fileHeader.Save(cryptoMode, archiveHeaderStream).ConfigureAwait(false);

                            await data.Stream.CopyToAsync(dataStream).ConfigureAwait(false);
                        }
                        finally
                        {
                            if (data.ShouldDispose)
                            {
                                data.Stream.Dispose();
                            }
                        }
                    }

                    archiveInfoHeaderStream.Position = 0;
                    archiveHeaderStream.Position     = 0;
                    // TODO: are those always compressed?
                    (encryptedHeaderStream, header) = await EncryptStreamToStreamAsync(cryptoMode, true, archiveInfoHeaderStream, (uint)archiveInfoHeaderStream.Length).ConfigureAwait(false);

                    (encryptedDataHeaderStream, dataHeader) = await EncryptStreamToStreamAsync(cryptoMode, true, archiveHeaderStream, (uint)archiveHeaderStream.Length).ConfigureAwait(false);
                }

            using (var bwHeader = new BinaryWriter(headerStream, Encoding.ASCII, true))
            {
                switch (cryptoMode)
                {
                case MS2CryptoMode.MS2F:
                    await SaveMS2F(cryptoMode, (uint)files.Length, header, dataHeader, bwHeader).ConfigureAwait(false);

                    break;

                case MS2CryptoMode.NS2F:
                    await SaveNS2F(cryptoMode, (uint)files.Length, header, dataHeader, bwHeader).ConfigureAwait(false);

                    break;

                default:
                case MS2CryptoMode.OS2F:
                case MS2CryptoMode.PS2F:
                    throw new NotImplementedException();
                }
            }

            using (encryptedHeaderStream)
                using (encryptedDataHeaderStream)
                {
                    await encryptedHeaderStream.CopyToAsync(headerStream).ConfigureAwait(false);

                    await encryptedDataHeaderStream.CopyToAsync(headerStream).ConfigureAwait(false);
                }
        }