public void Decompress(Stream input, Stream output)
 {
     using (var zstandardStream = new ZstandardStream(input, CompressionMode.Decompress))
     {
         zstandardStream.CopyTo(output);
     }
 }
Ejemplo n.º 2
0
        private int getSizeOfLastBlock()
        {
            if (lastBlockSize > -1)
            {
                return(lastBlockSize);
            }

            switch (compressionAlgorithm[amountOfBlocks - 1])
            {
            case 0:
                var rawBS = (int)compressedBlocks[amountOfBlocks - 1].GetSize();
                return(rawBS);                        //DON'T return bs here as the last block will be smaller!

            case 1:
                using (var decompressionStream = new ZstandardStream(compressedBlocks[amountOfBlocks - 1].AsStream(), CompressionMode.Decompress))
                    using (var memoryStream = new MemoryStream())
                    {
                        decompressionStream.CopyTo(memoryStream);
                        return((int)memoryStream.Length);
                    }

            default:
                throw new NotImplementedException(
                          "The specified compression algorithm isn't implemented yet!");
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc/>
        protected override void BaseDecompress(Stream inputStream, Stream outputStream)
        {
            using var gZipStream = new ZstandardStream(inputStream, CompressionMode.Decompress, true);
            gZipStream.CopyTo(outputStream);

            outputStream.Flush();
            gZipStream.Flush();
        }
Ejemplo n.º 4
0
 public static byte[] Decompress(byte[] data, int decompressedSize)
 {
     using (var memoryStream = new MemoryStream(data))
         using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
             using (var temp = new MemoryStream())
             {
                 compressionStream.CopyTo(temp);
                 return(temp.ToArray());
             }
 }
Ejemplo n.º 5
0
 private byte[] Decompress(byte[] compressed)
 {
     using (var inputStream = new MemoryStream(compressed))
         using (var zstandardStream = new ZstandardStream(inputStream, CompressionMode.Decompress))
             using (var outputStream = new MemoryStream())
             {
                 zstandardStream.CopyTo(outputStream);
                 return(outputStream.ToArray());
             }
 }
Ejemplo n.º 6
0
 /// <summary>
 /// decompresses given zstd compressed data
 /// </summary>
 /// <param name="compressed"></param>
 /// <returns></returns>
 public static byte[] DecompressZstd(byte[] compressed)
 {
     using (var memoryStream = new MemoryStream(compressed))
         using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
             using (var temp = new MemoryStream())
             {
                 compressionStream.CopyTo(temp);
                 return(temp.ToArray());
             }
 }
Ejemplo n.º 7
0
 /// <inheritdoc/>
 public override byte[] Decompress(byte[] compressedData, int blockLength)
 {
     using (var memoryStream = new MemoryStream(compressedData, 0, blockLength))
         using (var outputStream = new MemoryStream())
             using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
             {
                 compressionStream.CopyTo(outputStream);
                 compressionStream.Flush();
                 return(outputStream.ToArray());
             }
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Reads and decompresses ZSTD compressed data
 /// </summary>
 /// <param name="sizeInBytes"></param>
 /// <returns></returns>
 public byte[] ReadZstdCompressed(int sizeInBytes)
 {
     byte[] compressed = ReadBytes(sizeInBytes);
     using (var memoryStream = new MemoryStream(compressed))
         using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
             using (var temp = new MemoryStream())
             {
                 compressionStream.CopyTo(temp);
                 return(temp.ToArray());
             }
 }
Ejemplo n.º 9
0
 private byte[] Decompress(byte[] compressed, byte[] dictionaryRaw)
 {
     using (var memoryStream = new MemoryStream(compressed))
         using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
             using (var dictionary = new ZstandardDictionary(dictionaryRaw))
                 using (var temp = new MemoryStream())
                 {
                     compressionStream.CompressionDictionary = dictionary;
                     compressionStream.CopyTo(temp);
                     return(temp.ToArray());
                 }
 }
Ejemplo n.º 10
0
        /// <inheritdoc/>
        protected override byte[] BaseDecompress(byte[] compressedBytes)
        {
            using var inputStream  = new MemoryStream(compressedBytes);
            using var outputStream = new MemoryStream();
            using (var gZipStream = new ZstandardStream(inputStream, CompressionMode.Decompress))
            {
                gZipStream.CopyTo(outputStream, compressedBytes.Length);

                outputStream.Flush();
                gZipStream.Flush();
            }
            return(outputStream.ToArray());
        }
        /// <summary>
        /// Decompresses data using the zstd_stream compression algorithm.
        /// </summary>
        /// <param name="input">The data to decompress.</param>
        /// <param name="length">The expected length of the decompressed data.</param>
        /// <returns>A decompressed byte array.</returns>
        private byte[] DecompressUsingZstdStream(byte[] input, int length)
        {
            _buffer.Write(input, 0, input.Length);
            _buffer.Position -= input.Length;
            var target = new MemoryStream();

            _zstdDecompressStream.CopyTo(target, length);
            _zstdDecompressStream.Flush();
            var decompressedData = target.ToArray();

            target.Dispose();
            _buffer.SetLength(0);

            return(decompressedData);
        }
Ejemplo n.º 12
0
 /// <summary>
 /// decompresses given zstd compressed data
 /// </summary>
 /// <param name="compressed"></param>
 /// <returns></returns>
 public static byte[] DecompressZstd(byte[] compressed)
 {
     using (var memoryStream = new MemoryStream(compressed))
     {
         using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
         {
             using (var temp = new MemoryStream())
             {
                 compressionStream.CopyTo(temp);
                 // TODO: Try and avoid the additional copy.
                 return(temp.ToArray());
             }
         }
     }
 }
Ejemplo n.º 13
0
        //-----------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------

        private static void DictionaryCompression(byte[] input, ZstandardDictionary dictionary, int compressionLevel)
        {
            var stopwatch  = Stopwatch.StartNew();
            var compressed = default(byte[]);
            var output     = default(byte[]);

            // compress
            using (var memoryStream = new MemoryStream())
                using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
                {
                    compressionStream.CompressionLevel      = compressionLevel;
                    compressionStream.CompressionDictionary = dictionary;
                    compressionStream.Write(input, 0, input.Length);
                    compressionStream.Close();
                    compressed = memoryStream.ToArray();
                }

            // decompress
            using (var memoryStream = new MemoryStream(compressed))
                using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
                    using (var temp = new MemoryStream())
                    {
                        compressionStream.CompressionDictionary = dictionary;
                        compressionStream.CopyTo(temp);
                        output = temp.ToArray();
                    }

            // test output
            if (output.SequenceEqual(input) == false)
            {
                throw new Exception("Output is different from input!");
            }

            // write info
            Console.WriteLine($"Input       : {input.Length}");
            Console.WriteLine($"Compressed  : {compressed.Length}");
            Console.WriteLine($"Output      : {output.Length}");
            Console.WriteLine($"-------------------------------------------");
            Console.WriteLine($"Ratio       : {1.0f * input.Length / compressed.Length}");
            Console.WriteLine($"Time        : {stopwatch.Elapsed.TotalMilliseconds} ms");
            Console.WriteLine($"Is64Bit     : {Environment.Is64BitProcess}");
            Console.WriteLine();
        }
Ejemplo n.º 14
0
Archivo: CPU.cs Proyecto: c272/mCTF
        public mCPU(byte[] program)
        {
            //Initialize memory.
            mem = new Memory();
            byte[] memBlocks;

            //Check the type of mCTF program it is.
            byte[] signature = program.Take(4).ToArray();
            if (signature.SequenceEqual(Constants.STD_MCTF_HEADER))
            {
                //Copy the entire program into SCODE memory, it's just a standard program.
                Log.Debug("Standard mCTF header detected (no compression).");

                //Remove the header.
                memBlocks = program.Skip(4).ToArray();
            }
            else if (signature.SequenceEqual(Constants.ZCOMPRESSED_MCTF_HEADER))
            {
                Log.Debug("Compressed mCTF header detected (zstd compression).");

                //Use ZStandard to decompress (past first 4 bytes).
                using (var memoryStream = new MemoryStream(program.Skip(4).ToArray()))
                    using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
                        using (var temp = new MemoryStream())
                        {
                            compressionStream.CopyTo(temp);
                            memBlocks = temp.ToArray();
                        }
            }
            else
            {
                Log.Fatal("Invalid mCTF header: " + Encoding.Default.GetString(signature));
                return;
            }

            //Let the memory process the initial memory blocks.
            mem.ProcessBlocks(memBlocks);
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            byte[] input      = File.ReadAllBytes(Assembly.GetExecutingAssembly().Location);
            byte[] compressed = null;
            byte[] output     = null;

            // load a dictionary that is trained for the data (optional).
            // var dictionary = new ZstandardDictionary("loremipsum.zdict");

            // compress
            using (var memoryStream = new MemoryStream())
                using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
                {
                    compressionStream.CompressionLevel = 11;                           // optional!!
                    // compressionStream.CompressionDictionary = dictionary;  // optional!!
                    compressionStream.Write(input, 0, input.Length);
                    compressionStream.Close();
                    compressed = memoryStream.ToArray();
                }

            // decompress
            using (var memoryStream = new MemoryStream(compressed))
                using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
                    using (var temp = new MemoryStream())
                    {
                        // compressionStream.CompressionDictionary = dictionary;  // optional!!
                        compressionStream.CopyTo(temp);
                        output = temp.ToArray();
                    }

            // test output
            if (output.SequenceEqual(input) == false)
            {
                throw new Exception("Output is different from input!");
            }

            Console.ReadLine();
        }
Ejemplo n.º 16
0
        private void Decompress(byte[] data)
        {
            try
            {
                using (var memoryStream = new MemoryStream(data))
                    using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Decompress))
                        using (var temp = new MemoryStream())
                        {
                            compressionStream.CopyTo(temp);
                            var           output = temp.ToArray();
                            StringBuilder sb     = new StringBuilder(output.Length);
                            foreach (var b in output)
                            {
                                sb.Append($"{b} ");
                            }

                            Logging.Info($"Frame data: {sb}");
                        }
            }
            catch (Exception e)
            {
                Logging.Error(e, "An error occurred decompressing block.");
            }
        }
Ejemplo n.º 17
0
        public static FileArchive LoadFromFile(string filePath)
        {
            using var fileStream   = File.Open(filePath, FileMode.Open, FileAccess.Read);
            using var binaryReader = new BinaryReader(fileStream);

            // Archive header
            var magicNumber = binaryReader.ReadBytes(4);

            if (!Enumerable.SequenceEqual(magicNumber, new[] { (byte)'A', (byte)'L', (byte)'E', (byte)'X' }))
            {
                throw new Exception("Not a valid ALEX file.");
            }

            var fileMajor = binaryReader.ReadInt32();
            var fileMinor = binaryReader.ReadInt32();

            if (fileMajor != 01 || fileMinor != 00)
            {
                throw new Exception($"Unknown file version {fileMajor}.{fileMinor}");
            }

            binaryReader.ReadBytes(12); // Padding

            // Archive directory
            var files     = new List <FileArchiveFile>();
            var fileCount = binaryReader.ReadInt32();

            for (int i = 0; i < fileCount; ++i)
            {
                var fileName = binaryReader.ReadString();

                var fileLocation          = binaryReader.ReadInt64();
                var fileLength            = binaryReader.ReadInt64();
                var fileCompressionMethod = (CompressionMethod)binaryReader.ReadInt32();

                binaryReader.ReadBytes(8); // Padding

                files.Add(new FileArchiveFile(
                              fileName,
                              fileLocation,
                              fileLength,
                              fileCompressionMethod,
                              new byte[0]
                              ));
            }

            // File data
            foreach (var file in files)
            {
                binaryReader.BaseStream.Seek(file.FileLocation, SeekOrigin.Begin);
                binaryReader.ReadBytes(4); // Padding

                List <byte> fileData = new List <byte>();

                // Read in chunks
                for (long i = 0; i < file.FileLength;)
                {
                    var chunkLen = Math.Min(int.MaxValue, file.FileLength);
                    fileData.AddRange(binaryReader.ReadBytes((int)chunkLen));

                    i += chunkLen;
                }

                if (file.FileCompressionMethod == CompressionMethod.None)
                {
                    file.FileData = fileData.ToArray();
                }
                else if (file.FileCompressionMethod == CompressionMethod.Zstd)
                {
                    using var memoryStream      = new MemoryStream(fileData.ToArray());
                    using var dcmpMemoryStream  = new MemoryStream();
                    using var compressionStream = new ZstandardStream(memoryStream, System.IO.Compression.CompressionMode.Decompress);
                    compressionStream.CopyTo(dcmpMemoryStream);
                    file.FileData = dcmpMemoryStream.ToArray();
                }

                binaryReader.ReadBytes(4); // Padding
            }

            return(new FileArchive(files));
        }
Ejemplo n.º 18
0
        static void Main(string[] Args)
        {
            Console.WriteLine("\n BEA Extractor v0.1 - Ac_K:\n" +
                              "----------------------------\n");

            if (Args.Length == 0 || Args.Length > 2)
            {
                Console.WriteLine("BEAExtractor.exe archive_file.bea out_directory (optionnal - list files if not present)");
            }
            else
            {
                using (FileStream Stream = new FileStream(Args[0], FileMode.Open, FileAccess.Read))
                {
                    if (Stream.ReadMagic(0x4) == "SCNE")
                    {
                        BinaryReader Reader      = new BinaryReader(Stream);
                        BEAHeader    Header      = Stream.ReadStruct <BEAHeader>();
                        string       ArchiveName = Stream.ReadName(Header.ArchiveNameOffset);

                        Console.WriteLine("Header Info:\n" +
                                          $"\tReserved0: 0x{Header.Reserved0.ToString("X2")}\n" +
                                          $"\tUnknown0: 0x{Header.Unknown0.ToString("X2")}\n" +
                                          $"\tByteOrderMark: 0x{Header.ByteOrderMark.ToString("X2")}\n" +
                                          $"\tSectionsNumber: {Header.SectionsNumber}\n" +
                                          $"\tReserved1: 0x{Header.Reserved1.ToString("X2")}\n" +
                                          $"\tUnknown1: 0x{Header.Unknown1.ToString("X2")}\n" +
                                          $"\tFirstASSTSectionOffset0: 0x{Header.FirstASSTSectionOffset0.ToString("X2")}\n" +
                                          $"\t_RLTSectionOffset: 0x{Header._RLTSectionOffset.ToString("X2")}\n" +
                                          $"\tSectionsSize: 0x{Header.SectionsSize.ToString("X2")}\n" +
                                          $"\tFilesNumber: {Header.FilesNumber}\n" +
                                          $"\tASSTInfoSectionOffset: 0x{Header.ASSTInfoSectionOffset.ToString("X2")}\n" +
                                          $"\t_DICSectionOffset: 0x{Header._DICSectionOffset.ToString("X2")}\n" +
                                          $"\tUnknown2: 0x{Header.Unknown2.ToString("X2")}\n" +
                                          $"\tArchiveNameOffset: 0x{Header.ArchiveNameOffset.ToString("X2")}\n" +
                                          $"\tArchiveName: {ArchiveName}\n\n");

                        List <ASSTSection> FilesInfo = new List <ASSTSection>();

                        for (int i = 0; i < Header.FilesNumber; i++)
                        {
                            Stream.Seek(Header.ASSTInfoSectionOffset + sizeof(long) * i, SeekOrigin.Begin);
                            Stream.Seek(Reader.ReadInt64(), SeekOrigin.Begin);

                            if (Stream.ReadMagic(0x4) == "ASST")
                            {
                                ASSTSection FileInfo = Stream.ReadStruct <ASSTSection>();

                                FilesInfo.Add(FileInfo);
                            }
                            else
                            {
                                throw new Exception("ASST section not found!");
                            }
                        }

                        Console.WriteLine("Files Informations:");

                        foreach (ASSTSection FileInfo in FilesInfo)
                        {
                            string Filename = Stream.ReadName(FileInfo.FilenameOffset).Replace("/", "\\");

                            Console.WriteLine($"\t{Filename}:\n\n" +
                                              $"\t\tFileOffset: 0x{FileInfo.FileOffset.ToString("X2")}\n" +
                                              $"\t\tFilenameOffset: 0x{FileInfo.FilenameOffset.ToString("X2")}\n" +
                                              $"\t\tCompressionType: 0x{FileInfo.CompressionType}\n" +
                                              $"\t\tCompressedSize: 0x{FileInfo.CompressedSize.ToString("X2")}\n" +
                                              $"\t\tDecompressedSize: 0x{FileInfo.DecompressedSize.ToString("X2")}\n");

                            if (Args.Length > 1 && Args[1] != "")
                            {
                                Filename = Path.Combine(Args[1], Filename);
                                string DirectoryName = Path.GetDirectoryName(Filename);

                                Stream.Seek(FileInfo.FileOffset, SeekOrigin.Begin);

                                byte[] FileData = Reader.ReadBytes(FileInfo.CompressedSize);

                                if (FileInfo.CompressionType == CompressionType.Zstandard)
                                {
                                    using (MemoryStream MemStream = new MemoryStream(FileData))
                                        using (ZstandardStream CompStream = new ZstandardStream(MemStream, CompressionMode.Decompress))
                                            using (MemoryStream TempStream = new MemoryStream())
                                            {
                                                CompStream.CopyTo(TempStream);
                                                FileData = TempStream.ToArray();

                                                Console.Write("\t\tDecompressed! ");
                                            }
                                }
                                else
                                {
                                    Console.WriteLine($"\tUnknown compressed type: 0x{FileInfo.CompressionType.ToString("X2")}! File save as Raw.");
                                }

                                try
                                {
                                    Directory.CreateDirectory(DirectoryName);
                                }
                                catch (IOException Ex)
                                {
                                    if (Ex.HResult == -2147024713) //Rename when folder name is same as file name.
                                    {
                                        DirectoryName = Path.Combine(Path.GetDirectoryName(DirectoryName), Path.GetFileName(DirectoryName) + "_dir");
                                        Filename      = Path.Combine(DirectoryName, Path.GetFileName(Filename));

                                        Directory.CreateDirectory(DirectoryName);
                                    }
                                }

                                File.WriteAllBytes(Filename, FileData);

                                Console.WriteLine("Extracted!\n");
                            }
                        }

                        Console.WriteLine("Done.");
                    }
                    else
                    {
                        throw new Exception("BEA Archive file not found!");
                    }
                }
            }
        }