Beispiel #1
0
        internal static (int modId, int mapClassId, int hmSignature, MemoryStream dataStream) ReadPreHeaderAndDecompressDataStream(Stream inputStream)
        {
            (int modId, int mapClassId, int hmSignature, int headerLength) = ReadPreHeader(inputStream);

            //	Header stream
            inputStream.Position += headerLength;

            //	CFS signature
            ValidateSignature(new BinaryReader(inputStream), _signatureCFS);

            //	Data stream
            MemoryStream dataStream = new MemoryStream();

            using (ZlibStream zlib = new ZlibStream(inputStream, CompressionMode.Decompress, leaveOpen: true))
            {
                zlib.CopyTo(dataStream);
            }

            dataStream.Position = 0;
            return(modId, mapClassId, hmSignature, dataStream);
        }
Beispiel #2
0
 public byte[] GetAssetData(bool zlibCompressed)
 {
     using (WebResponse response = WebRequest.Create(this.RemoteURL).GetResponse())
     {
         using (MemoryStream ms = new MemoryStream())
         {
             if (this.IsCompressed == zlibCompressed)
             {
                 response.GetResponseStream().CopyTo(ms);
             }
             else
             {
                 using (ZlibStream zlibStream = new ZlibStream(response.GetResponseStream(), this.IsCompressed ? CompressionMode.Decompress : CompressionMode.Compress))
                 {
                     zlibStream.CopyTo(ms);
                 }
             }
             return(ms.ToArray());
         }
     }
 }
Beispiel #3
0
        private string GetText()
        {
            var r = new StringBuilder(numTextPages);

            for (var i = 1; i <= numTextPages; i++)
            {
                var    encryptedSection = pdbReader.GetSection(i);
                var    decryptedSection = contentDecryptor.TransformFinalBlock(encryptedSection, 0, encryptedSection.Length);
                byte[] decompressedSection;
                using (var inStream = new MemoryStream(decryptedSection))
                    using (var zipStream = new ZlibStream(inStream, CompressionMode.Decompress))
                        using (var outStream = new MemoryStream())
                        {
                            zipStream.CopyTo(outStream);
                            decompressedSection = outStream.ToArray();
                        }
                var text = Encoding.GetEncoding(1252).GetString(decompressedSection);
                r.Append(text);
            }
            return(r.ToString());
        }
Beispiel #4
0
        static void Decompress(string path)
        {
            string tmpPath = path + ".tmp";

            File.Delete(tmpPath);
            using (BinaryReader reader = new BinaryReader(File.OpenRead(path)))
                using (Stream output = File.OpenWrite(tmpPath)) {
                    while (IsCompressed(reader.BaseStream))
                    {
                        reader.ReadUInt32(); // magic
                        int   blockSize        = reader.ReadInt32();
                        int   compressedSize   = reader.ReadInt32();
                        int   uncompressedSize = reader.ReadInt32();
                        int   numBlocks        = (uncompressedSize + blockSize - 1) / blockSize;
                        int[] sizes            = new int[numBlocks * 2];
                        for (int i = 0; i < sizes.Length; i++)
                        {
                            sizes[i] = reader.ReadInt32();
                        }
                        int maxIdxLength = numBlocks.ToString().Length;
                        int sumBlocks    = 0;
                        for (int i = 0; i < numBlocks; i++)
                        {
                            Console.Write("\r[{0," + maxIdxLength + "}/" + numBlocks + "] Decompressing block... (" + sizes[i * 2] + " bytes to " + sizes[i * 2 + 1] + " bytes)", i);
                            byte[] buffer = new byte[sizes[i * 2]];
                            reader.Read(buffer, 0, sizes[i * 2]);
                            using (ZlibStream stream = new ZlibStream(new MemoryStream(buffer, 0, sizes[i * 2]), CompressionMode.Decompress)) {
                                stream.CopyTo(output);
                            }
                            sumBlocks += sizes[i * 2 + 1];
                        }
                        Console.WriteLine("\r[" + numBlocks + "/" + numBlocks + "] Decompressed " + numBlocks + " blocks: " + compressedSize + " bytes to " + uncompressedSize + " bytes");
                    }
                }
            string sizePath = path + ".uncompressed_size";

            File.Delete(sizePath);
            File.Delete(path);
            File.Move(tmpPath, path);
        }
Beispiel #5
0
        public void UnZip(string zipFilePath, string targetFolderPath, bool recursive)
        {
            if (recursive)
            {
                throw new ArgumentException("recursive parameter cannot be true!");
            }
            if (Directory.Exists(targetFolderPath))
            {
                throw new ArgumentException("sourcePath parameter cannot be directory!");
            }

            using (var compressedFile = File.OpenRead(zipFilePath))
            {
                using (var source = new ZlibStream(compressedFile, CompressionMode.Decompress, CompressionLevel))
                {
                    using (var target = File.OpenWrite(targetFolderPath))
                    {
                        source.CopyTo(target, BufferSize);
                    }
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Decompresses the given data stream from its source ZIP or GZIP format.
        /// </summary>
        /// <param name="dataBytes"></param>
        /// <returns></returns>
        internal static byte[] Inflate(Stream dataStream)
        {
            byte[] outputBytes = null;
            try
            {
                using (var deflateStream = new ZlibStream(dataStream, MonoGame.Utilities.CompressionMode.Decompress))
                {
                    using (MemoryStream zipoutStream = new MemoryStream())
                    {
                        deflateStream.CopyTo(zipoutStream);
                        outputBytes = zipoutStream.ToArray();
                    }
                }
            }
            catch
            {
                try
                {
                    dataStream.Seek(0, SeekOrigin.Begin);
                    #if !WINDOWS_PHONE
                    var gzipInputStream = new GZipInputStream(dataStream, System.IO.Compression.CompressionMode.Decompress);
                    #else
                    var gzipInputStream = new GZipInputStream(dataStream);
                    #endif

                    MemoryStream zipoutStream = new MemoryStream();
                    gzipInputStream.CopyTo(zipoutStream);
                    outputBytes = zipoutStream.ToArray();
                }
                catch (Exception exc)
                {
                    CCLog.Log("Error decompressing image data: " + exc.Message);
                }
            }


            return(outputBytes);
        }
Beispiel #7
0
        private byte[] DecompressPayload(IByteBuffer payload)
        {
            var totalLength = payload.ReadableBytes;

            var decompressedStream = new MemoryStream(256);

            using (var compressedStream = new MemoryStream(totalLength))
            {
                payload.GetBytes(0, compressedStream, totalLength);
                compressedStream.Position = 0;
                using (var zlibStream = new ZlibStream(compressedStream, CompressionMode.Decompress, true))
                {
                    zlibStream.CopyTo(decompressedStream);
                }
            }

            var data = new byte[decompressedStream.Length];

            decompressedStream.Position = 0;
            decompressedStream.Read(data, 0, data.Length);
            decompressedStream.Dispose();
            return(data);
        }
        private void DecodeSubtitle(int subtitleId, string ivBase64, string dataBase64, ref CrunchySubtitleInfo si)
        {
            byte[]     key  = GenerateKey(subtitleId).Concat(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).ToArray();
            byte[]     iv   = Convert.FromBase64String(ivBase64);
            byte[]     data = Convert.FromBase64String(dataBase64);
            AesManaged mng  = new AesManaged();

            mng.Mode    = CipherMode.CBC;
            mng.Padding = PaddingMode.None;
            ICryptoTransform tr = mng.CreateDecryptor(key, iv);

            byte[]       kk     = tr.TransformFinalBlock(data, 0, data.Length);
            MemoryStream ms     = new MemoryStream();
            ZlibStream   stream = new ZlibStream(new MemoryStream(kk), Ionic.Zlib.CompressionMode.Decompress);

            stream.CopyTo(ms);
            ms.Position = 0;
            XmlSerializer  serializer = new XmlSerializer(typeof(SubtitleScript));
            SubtitleScript script     = (SubtitleScript)serializer.Deserialize(ms);

            si.Title = script.Title;
            si.Ass   = script.ToAss();
        }
Beispiel #9
0
        private static byte[] uncompressToFWS(byte[] bytes)
        {
            var mem      = new MemoryStream(bytes);
            var reader   = new BinaryReader(mem);
            var filetype = reader.ReadBytes(3);
            var version  = reader.ReadByte();
            var size     = reader.ReadUInt32();

            var memout = new MemoryStream();
            var writer = new BinaryWriter(memout);

            writer.Write((byte)'F');
            writer.Write((byte)'W');
            writer.Write((byte)'S');
            writer.Write(version);
            writer.Write(size);

            var zlib = new ZlibStream(mem, CompressionMode.Decompress);

            zlib.CopyTo(memout);

            return(memout.ToArray());
        }
        private void DecompressMdfData()
        {
            byte[] compressedData = new byte[mdfData.Length - MdfHeader.MDF_HEADER_LENGTH];
            byte[] decompressedData;

            // Copy the current mdf data to compressed data without the MDF header
            Array.Copy(mdfData, MdfHeader.MDF_HEADER_LENGTH, compressedData, 0, mdfData.Length - MdfHeader.MDF_HEADER_LENGTH);

            using (MemoryStream compressedStream = new MemoryStream(compressedData))
            {
                using (ZlibStream deflateStream = new ZlibStream(compressedStream, CompressionMode.Decompress))
                {
                    using (MemoryStream decompressedStream = new MemoryStream())
                    {
                        deflateStream.CopyTo(decompressedStream);
                        decompressedData = decompressedStream.ToArray();
                    }
                }
            }

            // Write all of the decompressed data to the decompressedPath
            File.WriteAllBytes(decompressedPath, decompressedData);
        }
        public static BlueprintRoot Decode(string encodedString)
        {
            byte[] decompressedData = null;
            byte[] compressedData   = Convert.FromBase64String(encodedString.Substring(1));

            using (var outputStream = new MemoryStream())
                using (var ms = new MemoryStream(compressedData))
                    using (var ds = new ZlibStream(ms, CompressionMode.Decompress))
                    {
                        ds.CopyTo(outputStream);
                        decompressedData = outputStream.ToArray();
                    }

            string decodedString = Encoding.UTF8.GetString(decompressedData);

            var contractResolver = new DefaultContractResolver {
                NamingStrategy = new SnakeCaseNamingStrategy()
            };
            var blueprint = JsonConvert.DeserializeObject <BlueprintRoot>(decodedString, new JsonSerializerSettings {
                ContractResolver = contractResolver
            });

            return(blueprint);
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            if (args.Length > 0 && File.Exists(args[0]))
            {
                string path = args[0];
                using (StreamReader sr = new StreamReader(path))
                {
                    // Decompress the old packet..
                    Stream       dataStream   = sr.BaseStream;
                    MemoryStream decompressed = new MemoryStream();

                    using (ZlibStream zlibStream = new ZlibStream(dataStream, CompressionMode.Decompress))
                    {
                        zlibStream.CopyTo(decompressed);
                    }

                    byte[] decompressedData = decompressed.ToArray();

                    FileStream output = new FileStream("output.bin", FileMode.Create);
                    output.Write(decompressedData, 0, decompressedData.Length);
                    output.Close();
                }
            }
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            bool   useSpeed3                       = false;
            int    minPumpjacksPerBeacon           = 0;
            int    maxIterationsWithoutImprovement = 100;
            bool   showTimeUsedPercent             = false;
            Random rng = new Random();

            foreach (string arg in args.Select(s => s.ToLowerInvariant()))
            {
                if (Regex.IsMatch(arg, "-s(peed)?3"))
                {
                    useSpeed3 = true;
                }
                else if (Regex.IsMatch(arg, "-b(eacon)?(=\\d+)?"))
                {
                    if (arg.Contains("="))
                    {
                        minPumpjacksPerBeacon = int.Parse(arg.Substring(arg.IndexOf('=') + 1));
                        if (minPumpjacksPerBeacon < 1)
                        {
                            minPumpjacksPerBeacon = 1;
                        }
                    }
                    else
                    {
                        minPumpjacksPerBeacon = 2;
                    }
                    useSpeed3 = true;
                }
                else if (Regex.IsMatch(arg, "-(h(elp)?|\\?)"))
                {
                    PrintHelp();
                    return;
                }
                else if (Regex.IsMatch(arg, "-i=\\d+"))
                {
                    maxIterationsWithoutImprovement = int.Parse(arg.Substring(3));
                }
                else if (Regex.IsMatch(arg, "-seed=\\d+"))
                {
                    rng = new Random(int.Parse(arg.Substring(6)));
                }
                else if (Regex.IsMatch(arg, "-t(ime)?"))
                {
                    showTimeUsedPercent = true;
                }
                else if (Regex.IsMatch(arg, "-json"))
                {
                    string blueprintJSON = null;
                    try
                    {
                        using (var msi = new MemoryStream(Convert.FromBase64String(Clipboard.GetText().Substring(1))))
                        {
                            using (var mso = new MemoryStream())
                            {
                                using (var gs = new ZlibStream(msi, CompressionMode.Decompress))
                                {
                                    gs.CopyTo(mso);
                                }
                                blueprintJSON = Encoding.UTF8.GetString(mso.ToArray());
                            }
                        }
                    }
                    catch (FormatException)
                    {
                        return;
                    }
                    blueprintJSON = blueprintJSON.Substring(13, blueprintJSON.Length - 14);
                    string s = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(blueprintJSON), Formatting.Indented);
                    Console.WriteLine(s);
                    return;
                }
                else
                {
                    Console.WriteLine("Unknown option: " + arg);
                }
            }

            Profiler.StartSection("importBlueprint");
            Blueprint originalBp = Blueprint.ImportBlueprintString(Clipboard.GetText());

            Profiler.EndSection();

            if (originalBp == null)
            {
                Console.WriteLine("Could not load blueprint");
                return;
            }

            int iterationsWithoutImprovement = 0;

            Profiler.StartSection("copyBP");
            Blueprint bestBp = originalBp.DeepCopy();

            Profiler.EndSection();
            Blueprint bestFinishedBp = LayPipes(originalBp, useSpeed3, minPumpjacksPerBeacon);
            double    bestFitness    = bestFinishedBp.extraData.Fitness;

            Console.WriteLine("Found layout with " + bestFinishedBp.extraData.PipeCount + " pipes and " +
                              bestFinishedBp.extraData.OilProduction + " oil flow.");

            while (++iterationsWithoutImprovement <= maxIterationsWithoutImprovement)
            {
                Profiler.StartSection("copyBP");
                Blueprint bp = bestBp.DeepCopy();
                Profiler.EndSection();
                Profiler.StartSection("randomizeRotation");
                var pumpjackIdMap   = bp.Entities.Where(e => "pumpjack".Equals(e.Name)).ToDictionary(e => e.EntityNumber);
                var pumpjackIds     = bp.Entities.Where(e => "pumpjack".Equals(e.Name)).Select(p => p.EntityNumber).ToList();
                int randomizeAmount = rng.Next(5);
                for (int i = 0; i <= randomizeAmount; i++)
                {
                    int id        = rng.Next(pumpjackIds.Count);
                    int direction = rng.Next(4) * 2;
                    pumpjackIdMap[pumpjackIds[id]].Direction = direction;
                }
                Profiler.EndSection();
                var    test        = LayPipes(bp, useSpeed3, minPumpjacksPerBeacon);
                double testFitness = test.extraData.Fitness;

                if (testFitness > bestFitness)
                {
                    bestFitness    = testFitness;
                    bestBp         = bp;
                    bestFinishedBp = test;
                    Console.WriteLine("Found layout with " + bestFinishedBp.extraData.PipeCount + " pipes and " +
                                      bestFinishedBp.extraData.OilProduction + " oil flow after " + iterationsWithoutImprovement + " iterations.");
                    iterationsWithoutImprovement = 0;
                }
            }

            PlacePowerPoles(bestFinishedBp);

            if (showTimeUsedPercent)
            {
                Profiler.PrintTimeUsedPercent();
            }

            Clipboard.SetText(bestFinishedBp.ExportBlueprintString());
        }
Beispiel #14
0
        //http://www.minecraftwiki.net/wiki/Region_file_format
        public void Read(String path)
        {
            Chunks = new Chunk[32, 32];
            Match m = Regex.Match(path, @"r\.(-?\d+)\.(-?\d+)\.mc[ar]");

            Coords.X = int.Parse(m.Groups[1].Value);
            Coords.Z = int.Parse(m.Groups[2].Value);

            byte[] header = new byte[8192];

            using (BinaryReader file = new BinaryReader(File.Open(path, FileMode.Open)))
            {
                file.Read(header, 0, 8192);

                for (int chunkZ = 0; chunkZ < 32; chunkZ++)
                {
                    for (int chunkX = 0; chunkX < 32; chunkX++)
                    {
                        Chunk c = new Chunk();
                        c.Coords.X = Coords.X;
                        c.Coords.Z = Coords.Z;
                        c.Coords.RegiontoChunk();
                        c.Coords.Add(chunkX, chunkZ);


                        int i = 4 * (chunkX + chunkZ * 32);

                        byte[] temp = new byte[4];
                        temp[0] = 0;
                        Array.Copy(header, i, temp, 1, 3);
                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(temp);
                        }
                        long offset = ((long)BitConverter.ToInt32(temp, 0)) * 4096;
                        int  length = header[i + 3] * 4096;

                        temp = new byte[4];
                        Array.Copy(header, i + 4096, temp, 0, 4);
                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(temp);
                        }
                        c.Timestamp = BitConverter.ToInt32(temp, 0);

                        if (offset == 0 && length == 0)
                        {
                            Chunks[chunkX, chunkZ] = c;
                            continue;
                        }

                        file.BaseStream.Seek(offset, SeekOrigin.Begin);

                        temp = new byte[4];
                        file.Read(temp, 0, 4);
                        if (BitConverter.IsLittleEndian)
                        {
                            Array.Reverse(temp);
                        }
                        int exactLength = BitConverter.ToInt32(temp, 0);

                        c.CompressionType = file.ReadByte();
                        if (c.CompressionType == 1) //GZip
                        {
                            c.RawData = new byte[exactLength - 1];
                            file.Read(c.RawData, 0, exactLength - 1);

                            GZipStream   decompress = new GZipStream(new MemoryStream(c.RawData), CompressionMode.Decompress);
                            MemoryStream mem        = new MemoryStream();
                            decompress.CopyTo(mem);
                            mem.Seek(0, SeekOrigin.Begin);
                            c.Root = new TAG_Compound(mem);
                        }
                        else if (c.CompressionType == 2) //Zlib
                        {
                            c.RawData = new byte[exactLength - 1];
                            file.Read(c.RawData, 0, exactLength - 1);

                            ZlibStream   decompress = new ZlibStream(new MemoryStream(c.RawData), CompressionMode.Decompress);
                            MemoryStream mem        = new MemoryStream();
                            decompress.CopyTo(mem);
                            mem.Seek(0, SeekOrigin.Begin);
                            c.Root = new TAG_Compound(mem);
                        }
                        else
                        {
                            throw new Exception("Unrecognized compression type");
                        }

                        Chunks[chunkX, chunkZ] = c;
                    }
                }

                file.Close();
            }
        }
Beispiel #15
0
        public static byte[] DecompressBytes(byte[] bytesToDecompress, int decompressedSize)
        {
            byte[] decompressedBytes = new byte[decompressedSize];

            var compressionLevel = CompressionLevel.Default;

            if (bytesToDecompress.Length < 2)
            {
                return(bytesToDecompress);
            }

            if (bytesToDecompress[0] == 0x78)
            {
                if (bytesToDecompress[1] == 0x01)
                {
                    // No Compression/ Low
                    // 1
                    compressionLevel = CompressionLevel.Level1;
                }
                else if (bytesToDecompress[1] == 0x5E)
                {
                    // Low
                    // 2 - 5
                    compressionLevel = CompressionLevel.Level2;
                }
                else if (bytesToDecompress[1] == 0x9C)
                {
                    // Default
                    // 6
                    compressionLevel = CompressionLevel.Level6;
                }
                else if (bytesToDecompress[1] == 0xDA)
                {
                    // Best
                    // 7 - 9
                    compressionLevel = CompressionLevel.Level9;
                }
            }
            else
            {
                return(bytesToDecompress); // We aren't compressed
            }

            try
            {
                using (var stream = new MemoryStream(bytesToDecompress))
                {
                    using (var decompressor = new ZlibStream(stream, CompressionMode.Decompress, compressionLevel))
                    {
                        if (decompressedSize != 0)
                        {
                            decompressor.Read(decompressedBytes, 0, decompressedSize);
                        }
                        else
                        {
                            using (var outputStream = new MemoryStream())
                            {
                                decompressor.CopyTo(outputStream);
                                decompressedBytes = outputStream.ToArray();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                decompressedBytes = bytesToDecompress;
            }

            return(decompressedBytes);
        }
Beispiel #16
0
        /*
         * RedEngine header in front of the compressed collision cache files with CSound Materials?
         * Uint32 unk1;
         * Uint32 unk2;
         * Uint32 unk3; //NULL
         * Uint32 itemcount;
         * ... dynamic number of items
         *
         * Uint32 FileSize; // not always :(
         * byte unk9 ?
         */

        public void Extract(Stream output)
        {
            using (var file = MemoryMappedFile.CreateFromFile(Bundle.FileName, FileMode.Open))
                using (var viewstream = file.CreateViewStream(PageOFfset, ZSize, MemoryMappedFileAccess.Read))
                {
                    var zlib = new ZlibStream(viewstream, CompressionMode.Decompress);

                    // get magic bytes
                    var MAGIC = new byte[4];
                    switch (Comtype)
                    {
                    case 2:     // w2mesh: NXS mesh
                    case 5:     // reddest: NXS mesh
                        MAGIC = new byte[] { 0x4e, 0x58, 0x53 };
                        break;

                    case 3:     // redcloth: apb
                    case 4:     // redapex: apb
                        MAGIC = new byte[] { 0x5a, 0x5b, 0x5c, 0x5d };
                        break;

                    default:
                        break;
                    }

                    // seek magic bytes
                    if (!(Comtype > 4 || Comtype == 1))
                    {
                        Queue <byte> qBuffer = new Queue <byte>();

                        // auxiliary stream since zlibstreams don't support seeking.
                        using (MemoryStream ms = new MemoryStream())
                        {
                            zlib.CopyTo(ms);

                            var p = zlib.Position;
                            ms.Seek(0, SeekOrigin.Begin);
                            var testnumberofbytes = Math.Min(4096, ms.Length); // test only first max 4096 bytes

                            do
                            {
                                qBuffer.Enqueue(Convert.ToByte(ms.ReadByte()));
                                if (qBuffer.Count > MAGIC.Length)
                                {
                                    qBuffer.Dequeue();
                                }
                                testnumberofbytes--;
                            } while (!Enumerable.SequenceEqual(qBuffer.ToArray(), MAGIC) && testnumberofbytes > 0);

                            // reposition stream
                            var fileBegin = 0;
                            if (testnumberofbytes > 0)
                            {
                                fileBegin = Math.Max(0, (int)ms.Position - MAGIC.Length);
                            }
                            ms.Seek(0, SeekOrigin.Begin);

                            // save header
                            var buffer = new byte[fileBegin];
                            ms.Read(buffer, 0, fileBegin);
                            REDheader.AddRange(buffer);

                            ms.CopyTo(output);
                        }
                    }
                    else
                    {
                        zlib.CopyTo(output);
                    }
                }
        }
Beispiel #17
0
        /// <summary>
        /// Encodes the raw data
        /// </summary>
        /// <param name="inputStream">The raw data stream</param>
        /// <param name="outputStream">The output stream</param>
        /// <returns>The encrypted data</returns>
        public void Encode(Stream inputStream, Stream outputStream)
        {
            using var zStream = new ZlibStream(inputStream, CompressionMode.Compress, true);

            zStream.CopyTo(outputStream);
        }
Beispiel #18
0
 public void Extract(Stream output)
 {
     using (var file = MemoryMappedFile.CreateFromFile(this.ParentFile, FileMode.Open))
     {
         using (var viewstream = file.CreateViewStream((PageOffset * 4096) + 9, ZSize, MemoryMappedFileAccess.Read))
         {
             //TODO: Finish this once we have a proper dds reader/writer
             byte Dxt = BitConverter.GetBytes(Type)[0];
             uint fmt = 0;
             if (Dxt == 7)
             {
                 fmt = 1;
             }
             else if (Dxt == 8)
             {
                 fmt = 4;
             }
             else if (Dxt == 10)
             {
                 fmt = 4;
             }
             else if (Dxt == 13)
             {
                 fmt = 3;
             }
             else if (Dxt == 14)
             {
                 fmt = 6;
             }
             else if (Dxt == 15)
             {
                 fmt = 4;
             }
             else if (Dxt == 253)
             {
                 fmt = 0;
             }
             else if (Dxt == 0)
             {
                 fmt = 0;
             }
             else
             {
                 throw new Exception("Invalid image!");
             }
             var  cubemap = (Type == 3 || Type == 0) && (SliceCount == 6);
             uint depth   = 0;
             if (SliceCount > 1 && Type == 4)
             {
                 depth = SliceCount;
             }
             if (Type == 3 && Dxt == 253)
             {
                 BaseAlignment = 32;
             }
             var header = new DDSHeader().generate(
                 BaseWidth,
                 BaseHeight,
                 TotalMipsCount,
                 fmt,
                 BaseAlignment,
                 IsCube == 1,
                 depth)
                          .Concat(BitConverter.GetBytes((Int32)0)).ToArray();
             output.Write(header, 0, header.Length);
             if (!(SliceCount == 6 && (Type == 253 || Type == 0)))
             {
                 using (var zs = new ZlibStream(viewstream, CompressionMode.Decompress))
                 {
                     zs.CopyTo(output);
                 }
             }
         }
     }
 }
Beispiel #19
0
 public void Extract(Stream output)
 {
     using (var file = MemoryMappedFile.CreateFromFile(this.ParentFile, FileMode.Open))
     {
         using (var viewstream = file.CreateViewStream((Offset * 4096) + 9, ZSize, MemoryMappedFileAccess.Read))
         {
             uint fmt = 0;
             if (Dxt == 7)
             {
                 fmt = 1;
             }
             else if (Dxt == 8)
             {
                 fmt = 4;
             }
             else if (Dxt == 10)
             {
                 fmt = 4;
             }
             else if (Dxt == 13)
             {
                 fmt = 3;
             }
             else if (Dxt == 14)
             {
                 fmt = 6;
             }
             else if (Dxt == 15)
             {
                 fmt = 4;
             }
             else if (Dxt == 253)
             {
                 fmt = 0;
             }
             else if (Dxt == 0)
             {
                 fmt = 0;
             }
             else
             {
                 throw new Exception("Invalid image!");
             }
             var  cubemap = (Type == 3 || Type == 0) && (Typeinfo == 6);
             uint depth   = 0;
             if (Typeinfo > 1 && Type == 4)
             {
                 depth = Typeinfo;
             }
             if (Type == 3 && Dxt == 253)
             {
                 Bpp = 32;
             }
             var header = new DDSHeader().generate(Width, Height, 1, fmt, Bpp, cubemap, depth)
                          .Concat(new[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }).ToArray();
             output.Write(header, 0, header.Length);
             if (!(Typeinfo == 6 && (Dxt == 253 || Dxt == 0)))
             {
                 var zlib = new ZlibStream(viewstream, CompressionMode.Decompress);
                 zlib.CopyTo(output);
             }
         }
     }
 }
Beispiel #20
0
        public static void Process(string filename, string paramfile)
        {
            var firmware = File.ReadAllText(filename);
            var param    = File.ReadAllText(paramfile).Replace("\r", "");

            var fw_json   = JsonConvert.DeserializeObject <Hashtable>(firmware);
            var fw_base64 = fw_json["image"].ToString();
            var fw_binary = new MemoryStream();

            ZlibStream decompressionStream = new ZlibStream(new MemoryStream(Convert.FromBase64String(fw_base64)),
                                                            CompressionMode.Decompress);

            decompressionStream.CopyTo(fw_binary);

            var magic_str   = "PARMDEF";
            var param_magic = new byte[] { 0x55, 0x37, 0xf4, 0xa0, 0x38, 0x5d, 0x48, 0x5b };

            fw_binary.Position = 0;
            var offset = ReadOneSrch(fw_binary, magic_str.Select(a => (byte)a).ToArray());

            if (offset == -1)
            {
                throw new Exception("No param area found");
            }

            var magicoffset = ReadOneSrch(fw_binary, param_magic);

            if (magicoffset != -1)
            {
                var foffset = fw_binary.Position;
                var br      = new BinaryReader(fw_binary);

                var max_len = br.ReadInt16();
                var length  = br.ReadInt16();
                Console.WriteLine("Found param defaults max_length={0} length={1}", max_len, length);

                if (param.Length > max_len)
                {
                    throw new Exception(String.Format("Error: Length {0} larger than maximum {1}", length, max_len));
                }

                var paramdata = new byte[length];
                br.Read(paramdata, 0, length);

                var paramstring = ASCIIEncoding.ASCII.GetString(paramdata);

                var new_fwms = new MemoryStream(fw_binary.ToArray());
                var new_fw   = new BinaryWriter(new_fwms);

                new_fw.Seek((int)foffset + 2, SeekOrigin.Begin);
                new_fw.Write((short)param.Length);
                new_fw.Write(ASCIIEncoding.ASCII.GetBytes(param), 0, param.Length);

                fw_json["image"] = Convert.ToBase64String(ZlibStream.CompressBuffer(new_fwms.ToArray()));

                //File.WriteAllBytes(filename + "orig.bin", fw_binary.ToArray());
                //File.WriteAllBytes(filename + "new.bin", new_fwms.ToArray());
                File.WriteAllText(filename + "new.apj", JsonConvert.SerializeObject(fw_json, Formatting.Indented));

                return;
            }

            throw new Exception("Error: Param defaults support not found in firmware");
        }
Beispiel #21
0
        public Packet(EQStream stream, byte[] packet, bool combined = false)
        {
            Baked = packet;

            Opcode = packet.NetU16(0);
            var off = 2;

            switch ((SessionOp)Opcode)
            {
            case SessionOp.Ack:
            case SessionOp.OutOfOrder:
            case SessionOp.Single:
            case SessionOp.Fragment:
            case SessionOp.Combined:
                var plen = packet.Length - off;
                if (!combined && stream.Validating)
                {
                    plen -= 2;
                    var mcrc = CalculateCRC(packet.Sub(0, packet.Length - 2), stream.CRCKey);
                    var pcrc = packet.NetU16(packet.Length - 2);
                    Valid = mcrc == pcrc;
                }
                if (!combined && stream.Compressing)
                {
                    if (packet[off] == 0x5a)
                    {
                        using (var ms = new MemoryStream(packet, 3, packet.Length - 3 - 2)) {
                            using (var ds = new ZlibStream(ms, CompressionMode.Decompress)) {
                                using (var tms = new MemoryStream()) {
                                    ds.CopyTo(tms);
                                    packet = tms.ToArray();
                                    plen   = packet.Length;
                                    off    = 0;
                                }
                            }
                        }
                    }
                    else if (packet[off] == 0xa5)
                    {
                        off++;
                        plen--;
                    }
                }
                if ((SessionOp)Opcode != SessionOp.Combined)
                {
                    Sequence = packet.NetU16(off);
                    off     += 2;
                    plen    -= 2;
                }
                Data = packet.Sub(off, off + plen);
                Bare = false;
                break;

            case SessionOp.Response:
                Data = packet.Sub(2);
                break;

            default:
                Opcode = (ushort)SessionOp.Bare;
                if (packet.Length > 2 && packet[1] == 0xA5)
                {
                    Data    = packet.Sub(1);
                    Data[0] = packet[0];
                }
                else
                {
                    Data = packet;
                }
                Bare = true;
                break;
            }
        }
Beispiel #22
0
        public WilayRead(byte[] file)
        {
            var stream = new MemoryStream(file);
            var reader = new BinaryReader(stream);

            string magic = reader.ReadUTF8(4);

            if (magic == "xbc1")
            {
                reader.BaseStream.Position += 4;
                int uncompressedSize = reader.ReadInt32();
                stream.Position = 0x30;

                file = new byte[uncompressedSize];
                var uncompressedData = new MemoryStream(file);

                using (var deflate = new ZlibStream(stream, CompressionMode.Decompress, true))
                {
                    deflate.CopyTo(uncompressedData, uncompressedSize);
                }

                uncompressedData.Position = 0;
                stream = uncompressedData;
                reader = new BinaryReader(stream);
            }
            else if (magic != "LAHD")
            {
                throw new NotSupportedException($"Can't read type {magic}");
            }

            int texturesOffset = BitConverter.ToInt32(file, 36);

            stream.Position = texturesOffset;

            int offset = reader.ReadInt32();
            int length = reader.ReadInt32();

            stream.Position = texturesOffset + offset;
            var offsets = new TextureOffset[length];

            Textures = new LahdTexture[length];

            for (int i = 0; i < length; i++)
            {
                offsets[i] = new TextureOffset
                {
                    Field0 = reader.ReadInt32(),
                    Offset = reader.ReadInt32(),
                    Length = reader.ReadInt32()
                };
            }

            for (int i = 0; i < length; i++)
            {
                stream.Position = texturesOffset + offsets[i].Offset + offsets[i].Length - 56;

                var texture = new byte[offsets[i].Length];
                Array.Copy(file, texturesOffset + offsets[i].Offset, texture, 0, offsets[i].Length);
                Textures[i] = new LahdTexture(texture);
            }
        }
Beispiel #23
0
        public void Execute()
        {
            //var fileStreamBuffer = FileData.Slice(0, 8);

            // This signature indicates that the remainder of the datastream contains a single PNG image, consisting of a series of chunks beginning with an IHDR chunk and ending with an IEND chunk.
            //Debug.Assert(fileStreamBuffer.SequenceEqual(pngSignature), "This doesn't seem to be a PNG");
            bool lastChunk;
            var  metadata = new Metadata(FileData.Length);
            var  offset   = 8;

            do
            {
                var length       = BitConverterBigEndian.ToUInt32(FileData.Slice(offset, 4));
                var signedLength = checked ((int)length);

                lastChunk = ChunkGenerator.GenerateChunk(FileData.GetSubArray(offset + 4, signedLength + 8), signedLength, ref metadata);
                offset   += 12 + signedLength;
            } while (!lastChunk);

            //var tmpScanlineColor = new Color32[metadata.Width];

            NativeArray <byte> uncompressedData;

            var metadataData = metadata.Data;
            var buffer       = metadataData.ToArray();

            using (var zlibStream = new ZlibStream(new MemoryStream(buffer), Ionic.Zlib.CompressionMode.Decompress)) {
                using (var memoryStream = new MemoryStream()) {
                    zlibStream.CopyTo(memoryStream);
                    uncompressedData = new NativeArray <byte>(memoryStream.ToArray(), Allocator.Temp);
                }
            }

            Debug.Assert(uncompressedData.Length >= 1);

            int bytesPerPixel;

            switch (metadata.ColourType)
            {
            case Metadata.ColourTypeEnum.GREYSCALE:
                bytesPerPixel = 1;
                throw new NotImplementedException();

            case Metadata.ColourTypeEnum.TRUECOLOUR:
                bytesPerPixel = 3;
                break;

            case Metadata.ColourTypeEnum.INDEXED_COLOUR:
                bytesPerPixel = 1;
                throw new NotImplementedException();

            case Metadata.ColourTypeEnum.GREYSCALE_WITH_ALPHA:
                bytesPerPixel = 2;
                throw new NotImplementedException();

            case Metadata.ColourTypeEnum.TRUECOLOUR_WITH_ALPHA:
                bytesPerPixel = 4;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            Width[0]  = checked ((int)metadata.Width);
            Height[0] = checked ((int)metadata.Height);
            RawTextureData.Resize(Width[0] * Height[0] * 4, NativeArrayOptions.UninitializedMemory);

            for (var aktLine = 0; aktLine < metadata.Height; aktLine++)
            {
                var firstRowBytePosition = aktLine * (Width[0] * bytesPerPixel + 1);
                //Color32[] lineColors = null;
                switch ((FilterType)uncompressedData[firstRowBytePosition])
                {
                case FilterType.NONE:
                    break;

                case FilterType.SUB:
                    FilterLineSub(ref uncompressedData, firstRowBytePosition, Width[0], bytesPerPixel);
                    break;

                case FilterType.UP:
                    FilterLineUp(ref uncompressedData, firstRowBytePosition, Width[0], bytesPerPixel);
                    break;

                case FilterType.AVERAGE:
                    FilterLineAverage(ref uncompressedData, firstRowBytePosition, Width[0], bytesPerPixel);
                    break;

                case FilterType.PAETH:
                    FilterLinePaeth(ref uncompressedData, firstRowBytePosition, Width[0], bytesPerPixel);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                for (var i = 0; i < Width[0]; i++)
                {
                    RawTextureData[(int)((metadata.Height - 1 - aktLine) * metadata.Width * 4 + i * 4)]     = uncompressedData[firstRowBytePosition + 1 + i * bytesPerPixel];
                    RawTextureData[(int)((metadata.Height - 1 - aktLine) * metadata.Width * 4 + i * 4 + 1)] = uncompressedData[firstRowBytePosition + 2 + i * bytesPerPixel];
                    RawTextureData[(int)((metadata.Height - 1 - aktLine) * metadata.Width * 4 + i * 4 + 2)] = uncompressedData[firstRowBytePosition + 3 + i * bytesPerPixel];
                    RawTextureData[(int)((metadata.Height - 1 - aktLine) * metadata.Width * 4 + i * 4 + 3)] = bytesPerPixel == 4 ? uncompressedData[firstRowBytePosition + 4 + i * bytesPerPixel] : byte.MaxValue;
                }
            }
        }
Beispiel #24
0
        /// <inheritdoc/>
        public ConcurrentDictionary <string, ConcurrentQueue <string> > Scan(Scanner scanner, Stream stream, string file)
        {
            // If the BFPK file itself fails
            try
            {
                string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                Directory.CreateDirectory(tempPath);

                using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true))
                {
                    br.ReadBytes(4); // Skip magic number

                    int  version = br.ReadInt32();
                    int  files   = br.ReadInt32();
                    long current = br.BaseStream.Position;

                    for (int i = 0; i < files; i++)
                    {
                        br.BaseStream.Seek(current, SeekOrigin.Begin);

                        int    nameSize = br.ReadInt32();
                        string name     = new string(br.ReadChars(nameSize));

                        uint uncompressedSize = br.ReadUInt32();
                        int  offset           = br.ReadInt32();

                        current = br.BaseStream.Position;

                        br.BaseStream.Seek(offset, SeekOrigin.Begin);
                        uint compressedSize = br.ReadUInt32();

                        // Some files can lack the length prefix
                        if (compressedSize > br.BaseStream.Length)
                        {
                            br.BaseStream.Seek(-4, SeekOrigin.Current);
                            compressedSize = uncompressedSize;
                        }

                        // If an individual entry fails
                        try
                        {
                            string tempFile = Path.Combine(tempPath, name);
                            if (!Directory.Exists(Path.GetDirectoryName(tempFile)))
                            {
                                Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
                            }

                            if (compressedSize == uncompressedSize)
                            {
                                using (FileStream fs = File.OpenWrite(tempFile))
                                {
                                    fs.Write(br.ReadBytes((int)uncompressedSize), 0, (int)uncompressedSize);
                                }
                            }
                            else
                            {
                                using (FileStream fs = File.OpenWrite(tempFile))
                                {
                                    try
                                    {
                                        ZlibStream zs = new ZlibStream(br.BaseStream, CompressionMode.Decompress);
                                        zs.CopyTo(fs);
                                    }
                                    catch (ZlibException)
                                    {
                                        br.BaseStream.Seek(offset + 4, SeekOrigin.Begin);
                                        fs.Write(br.ReadBytes((int)compressedSize), 0, (int)compressedSize);
                                    }
                                }
                            }
                        }
                        catch { }

                        br.BaseStream.Seek(current, SeekOrigin.Begin);
                    }
                }

                // Collect and format all found protections
                var protections = scanner.GetProtections(tempPath);

                // If temp directory cleanup fails
                try
                {
                    Directory.Delete(tempPath, true);
                }
                catch { }

                // Remove temporary path references
                Utilities.StripFromKeys(protections, tempPath);

                return(protections);
            }
            catch { }

            return(null);
        }
Beispiel #25
0
 private static void DecompressToStream(Stream input, Stream output)
 {
     using (var zlib = new ZlibStream(input, CompressionMode.Decompress))
         zlib.CopyTo(output);
 }
Beispiel #26
0
        public void Extract(Stream output)
        {
            using (var file = MemoryMappedFile.CreateFromFile(Bundle.FileName, FileMode.Open))
            {
                using (var viewstream = file.CreateViewStream(PageOffset, ZSize, MemoryMappedFileAccess.Read))
                {
                    switch (CompressionType)
                    {
                    case CompressionType.None:
                    {
                        viewstream.CopyTo(output);
                        break;
                    }

                    case CompressionType.LZ4:
                    {
                        var buffer       = new byte[ZSize];
                        var c            = viewstream.Read(buffer, 0, buffer.Length);
                        var uncompressed = LZ4Codec.Decode(buffer, 0, c, (int)Size);
                        output.Write(uncompressed, 0, uncompressed.Length);
                        break;
                    }

                    case CompressionType.LZ4HC:
                    {
                        var buffer       = new byte[ZSize];
                        var c            = viewstream.Read(buffer, 0, buffer.Length);
                        var uncompressed = LZ4Codec.Decode(buffer, 0, c, (int)Size);
                        output.Write(uncompressed, 0, uncompressed.Length);
                        break;
                    }

                    case CompressionType.Snappy:
                    {
                        var buffer       = new byte[ZSize];
                        var c            = viewstream.Read(buffer, 0, buffer.Length);
                        var uncompressed = SnappyCodec.Uncompress(buffer);
                        output.Write(uncompressed, 0, uncompressed.Length);
                        break;
                    }

                    case CompressionType.Doboz:
                    {
                        var buffer       = new byte[ZSize];
                        var c            = viewstream.Read(buffer, 0, buffer.Length);
                        var uncompressed = DobozCodec.Decode(buffer, 0, c);
                        output.Write(uncompressed, 0, uncompressed.Length);
                        break;
                    }

                    case CompressionType.ZLib:
                    {
                        var zlib = new ZlibStream(viewstream, CompressionMode.Decompress);
                        zlib.CopyTo(output);
                        break;
                    }

                    default:
                        throw new MissingCompressionException("Unhandled compression algorithm.")
                              {
                                  Compression = Compression
                              };
                    }

                    viewstream.Close();
                }
            }
        }
Beispiel #27
0
        /// <summary>
        ///     Open a savefile from disk
        /// </summary>
        /// <param name="file">Full path to the .sav file, usually found in %localappdata%/FactoryGame/Saved/SaveGames</param>
        public SatisfactorySave(string file)
        {
            log.Info($"Opening save file: {file}");

            FileName = Environment.ExpandEnvironmentVariables(file);

            using (var stream = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                using (var reader = new BinaryReader(stream))
                {
                    Header = FSaveHeader.Parse(reader);

                    if (Header.SaveVersion < FSaveCustomVersion.SaveFileIsCompressed)
                    {
                        LoadData(reader);
                    }
                    else
                    {
                        using (var buffer = new MemoryStream())
                        {
                            var uncompressedSize = 0L;

                            while (stream.Position < stream.Length)
                            {
                                var header = reader.ReadChunkInfo();
                                Trace.Assert(header.CompressedSize == ChunkInfo.Magic);

                                var summary = reader.ReadChunkInfo();

                                var subChunk = reader.ReadChunkInfo();
                                Trace.Assert(subChunk.UncompressedSize == summary.UncompressedSize);

                                var startPosition = stream.Position;
                                using (var zStream = new ZlibStream(stream, CompressionMode.Decompress, true))
                                {
                                    zStream.CopyTo(buffer);
                                }

                                // ZlibStream appears to read more bytes than it uses (because of buffering probably) so we need to manually fix the input stream position
                                stream.Position = startPosition + subChunk.CompressedSize;

                                uncompressedSize += subChunk.UncompressedSize;
                            }


                            buffer.Position = 0;

#if DEBUG
                            //File.WriteAllBytes(Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".bin"), buffer.ToArray());
#endif


                            using (var bufferReader = new BinaryReader(buffer))
                            {
                                var dataLength = bufferReader.ReadInt32();
                                Trace.Assert(uncompressedSize == dataLength + 4);

                                LoadData(bufferReader);
                            }
                        }
                    }
                }
        }
Beispiel #28
0
        /// <summary>
        /// Processes the next segment of data and gives the packet id and packet data (if any)
        /// </summary>
        /// <param name="nextSegment">The next segment to be processed</param>
        /// <param name="offset">The offset to start at</param>
        /// <param name="len">The length of the segment to process</param>
        /// <param name="completed">Set to true if the segment processed was a complete packet, otherwise false</param>
        /// <returns>True if more needs to be processed, false if complete</returns>
        public bool ProcessNextSegment(byte[] nextSegment, int offset, int len, out bool completed)
        {
            CurrentPacketData = null;
            completed         = false;

            if (nextSegment.Length > 0)
            {
                PacketBuffer.AddRange(new ByteArraySegment(nextSegment, offset, len));
            }

            if (PacketBuffer.Count <= 1)
            {
                return(false);
            }

            CurrentPacketId = PacketBuffer[0];

            bool compressed = _length.HasValue && _length.Value < 0;

            if (!_length.HasValue)
            {
                bool success;
                _length = VLQ.FromEnumerableSigned(PacketBuffer, 1, PacketBuffer.Count, out _position, out success); //the length of the packet
                _position++;                                                                                         //account for the packet id

                if (!success)
                {
                    _length = null;

                    return(false);
                }

                compressed = _length.Value < 0;
            }

            int lenPos = Math.Abs((int)_length.Value);

            if (PacketBuffer.Count < lenPos + _position)
            {
                return(false);
            }

            byte[] data = PacketBuffer.Skip(_position).Take(lenPos).ToArray();

            _position += data.Length;

            if (compressed) //decompress the packet if it has been compressed
            {
                using (MemoryStream ms = new MemoryStream(data))
                {
                    using (MemoryStream outStream = new MemoryStream())
                    {
                        using (ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress))
                        {
                            zs.CopyTo(outStream);
                        }

                        data = outStream.ToArray();
                    }
                }
            }

            CurrentPacketData = data;

            //remove the data already processed
            PacketBuffer.RemoveRange(0, _position);

            //reset length
            _length = null;

            completed = true;

            //return true if there are any more packets needing to be processed
            return(PacketBuffer.Count > 0);
        }