/// <summary>
        /// 解壓處理的byte陣列
        /// </summary>
        /// <param name="bytes">傳入要處理的byte陣列</param>
        /// <returns>回傳位元陣列</returns>
        public static byte[] DeCompress(byte[] bytes)
        {
            //使用壓縮的輸入資料流
            //使用memory資料流讀入要處理的byte陣列 來處理 解壓輸入處理資料
            ZipProc.Streams.InflaterInputStream stream = new ZipProc.Streams.InflaterInputStream(
             new MemoryStream(bytes));

            //開起記憶體空間資料流
            MemoryStream memory = new MemoryStream();

            //宣告byte陣列,預設為4k
            byte[] writeData = new byte[4096];

            int size;

            while (true)
            {
                //讀取解壓的輸入資料流 ,	並將結果放到byte陣列	,每次處理4K		
                size = stream.Read(writeData, 0, writeData.Length);

                if (size > 0)	//還有解壓資料,將資料放到memory 資料流
                    memory.Write(writeData, 0, size);
                else //無解縮資料,離開迴圈
                    break;
            }

            //關閉解壓輸入資料流			
            stream.Close();

            //回傳記憶體資料流內的陣列
            return memory.ToArray();
        }
Example #2
0
            public override async Task <bool> Download(Archive a, string destination)
            {
                var(client, info, collected) = await ResolveDownloadInfo();

                using var tf         = new TempFile();
                await using var file = tf.File.Create();
                var max_chunks = info.depot_list[0].file_list[0].chunk_count;

                foreach (var chunk in info.depot_list[0].file_list[0].chunk_list.OrderBy(c => c.index))
                {
                    Utils.Status($"Downloading {a.Name}", chunk.index * 100 / max_chunks);
                    var got = await client.GetAsync(
                        $"https://content.cdp.bethesda.net/{collected.CDPProductId}/{collected.CDPPropertiesId}/{chunk.sha}");

                    var data = await got.Content.ReadAsByteArrayAsync();

                    AESCTRDecrypt(collected.AESKey, collected.AESIV, data);

                    if (chunk.uncompressed_size == chunk.chunk_size)
                    {
                        await file.WriteAsync(data, 0, data.Length);
                    }
                    else
                    {
                        using (var ms = new MemoryStream(data))
                            using (var zlibStream =
                                       new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms))
                                await zlibStream.CopyToAsync(file);
                    }
                }
                file.Close();
                await ConvertCKMToZip(file.Name, destination);

                return(true);
            }
Example #3
0
        private static byte[] ZlibDecompress(Stream data, int expectedLength)
        {
            using (MemoryStream output = new MemoryStream(expectedLength))
            {
#if WITH_DOTNETZIP
                using (var stream = new Ionic.Zlib.ZlibStream(data, Ionic.Zlib.CompressionMode.Decompress))
                {
                    stream.CopyTo(output);
                }
#elif WITH_SHARPCOMPRESS
                /*
                 * using (var stream = new SharpCompress.Compressors.Deflate.ZlibStream(data, SharpCompress.Compressors.CompressionMode.Decompress))
                 * {
                 *  stream.CopyTo(output);
                 * }
                 */
#elif WITH_SHARPZIPLIB
                using (var stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(data))
                {
                    stream.CopyTo(output);
                }
#else
                throw new NotImplementedException("Please define which compression library you want to use");
#endif
                return(output.ToArray());
            }
        }
Example #4
0
        public override byte[] Decode(
            byte[] data,
            int offset,
            int length,
            PdfDictionary parameters
            )
        {
/* TODO:IMPL This chunk of code is the canonical implementation using System.IO.Compression; it's going to substitute the current SharpZipLib-based implementation (see below).
 *    MemoryStream outputStream = new MemoryStream();
 *    {
 *      MemoryStream inputStream = new MemoryStream(data,offset,length);
 *      DeflateStream inputFilter = new DeflateStream(
 *        inputStream,
 *        CompressionMode.Decompress
 *        );
 *      inputStream.Position = 2; // Skips zlib's 2-byte header [RFC 1950] [FIX:0.0.8:JCT].
 *      Transform(inputFilter,outputStream);
 *    }
 *    return outputStream.ToArray();
 */

            /*
             * NOTE: This is an alternative implementation using ICSharpCode.SharpZipLib instead of
             * System.IO.Compression. It should be eventually replaced by the canonical implementation above.
             */

            Zip.Streams.InflaterInputStream inputFilter = new Zip.Streams.InflaterInputStream(
                new MemoryStream(data, offset, length)
                );
            MemoryStream outputStream = new MemoryStream();

            Transform(inputFilter, outputStream);
            return(DecodePredictor(outputStream.ToArray(), parameters));
        }
Example #5
0
        private bool ReadHeader()
        {
            // Read file signature
            this.signature = Encoding.ASCII.GetString(this.swf.ReadUI8(3));     // "FWS" or "CWS" for ZLIB compressed (v6.0 or later)
            if (this.signature != "FWS" &&
                this.signature != "CWS")
            {
                Trace.WriteLine("Not a valid SWF (Flash) file signature");
                return(false);
            }
            TraceManager.Add(String.Format("Signature   : {0}", this.Signature));

            // Read file version
            this.version = this.swf.ReadUI8();
            TraceManager.Add(String.Format("Version     : {0}", this.Version));

            // File length
            this.fileLength = this.swf.ReadUI32();
            TraceManager.Add(String.Format("File length : {0} bytes", this.FileLength));

            // If the file is compressed, this is where the ZLIB decompression ("inflate") begins
            if (this.signature == "CWS")
            {
                // Begin inflating stream
                ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream inflatedStream =
                    new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(this.stream);
                swf.Stream = inflatedStream;
            }

            // Frame size
            Rect frameSize = new Rect(swf);

            this.frameWidth  = frameSize.XMax;
            this.frameHeight = frameSize.YMax;
            TraceManager.Add(String.Format("Frame width : {0} twips ({1} pixels)", this.FrameWidth, this.FrameWidth / 20));
            TraceManager.Add(String.Format("Frame height: {0} twips ({1} pixels)", this.FrameHeight, this.FrameHeight / 20));

            // Frame rate (stored in UI8.UI8 format)
            ushort frameRateMinor = swf.ReadUI8();
            ushort frameRateMajor = swf.ReadUI8();

            this.frameRate = Convert.ToSingle(String.Format("{0}.{1}", frameRateMajor, frameRateMinor));    // TODO: Improve this later
            TraceManager.Add(String.Format("Frame rate  : {0} fps", this.FrameRate));

            // Frame count
            this.frameCount = swf.ReadUI16();
            TraceManager.Add(String.Format("Frame count : {0}", this.FrameCount));

            return(true);
        }
Example #6
0
        private void DecodeContent()
        {
            if (ContentEncoding == ContentEncodings.None)
            {
                return;
            }

            System.IO.Stream s;
            if (ContentEncoding == ContentEncodings.Deflate)
            {
                s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(
                    new System.IO.MemoryStream(Body),
                    new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(true));
            }
            else if (ContentEncoding == ContentEncodings.Gzip)
            {
                s = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(new System.IO.MemoryStream(Body));
            }
            else
            {
                throw new System.NotImplementedException("Unknown ContentEncoding of " + ContentEncoding);
            }

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            int readChunkSize         = 2048;

            byte[] uncompressedChunk = new byte[readChunkSize];
            int    sizeRead;

            do
            {
                sizeRead = s.Read(uncompressedChunk, 0, readChunkSize);
                if (sizeRead > 0)
                {
                    ms.Write(uncompressedChunk, 0, sizeRead);
                }
                else
                {
                    break; // break out of the while loop
                }
            } while(sizeRead > 0);

            s.Close();

            // copy the CompressedBody to CompressedBody
            CompressedBody = Body;

            // extract the compressed body over the existing body
            Body = ms.ToArray();
        }
Example #7
0
 public static byte[] Decompress(byte[] bytes)
 {
     MemoryStream memory = new MemoryStream();
     using(Zip.Streams.InflaterInputStream stream = new Zip.Streams.InflaterInputStream(new MemoryStream(bytes)))
     {
         byte[] writeData = new byte[4096];
         int size = 1;
         while(size > 0)
         {
             size = stream.Read(writeData, 0, writeData.Length);
             if(size > 0)
                 memory.Write(writeData, 0, size);
         }
     }
     return memory.ToArray();
 }
Example #8
0
 public static byte[] Decompress(byte[] byteInput)
 {
     byte[] bytResult = null;
     if (byteInput != null)
     {
         using (MemoryStream ms = new MemoryStream(byteInput, 0, byteInput.Length))
         {
             string strResult = String.Empty;
             byte[] writeData = new byte[4096];
             Stream s2        =
                 new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
             bytResult = ReadFullStream(s2);
         }
     }
     return(bytResult);
 }
Example #9
0
 public static byte[] Decompress(byte[] byteInput)
 {
   byte[] bytResult = null;
   if (byteInput != null)
   {
     using (MemoryStream ms = new MemoryStream(byteInput, 0, byteInput.Length))
     {
       string strResult = String.Empty;
       byte[] writeData = new byte[4096];
       Stream s2 =
          new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
       bytResult = ReadFullStream(s2);
     }
   }
   return bytResult;
 }
Example #10
0
        static public byte[] UnEncryptAndUnCompress(byte[] input, byte XORKey)
        {
            for (int i = 0; i < input.Length; i++)
            {
                input[i] ^= XORKey;
            }

            MemoryStream input_stream  = new MemoryStream(input);
            var          output_stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(input_stream);
            StreamReader rader         = new StreamReader(output_stream, System.Text.Encoding.UTF8);
            var          tmp           = rader.ReadToEnd();

            byte[] output = System.Text.Encoding.UTF8.GetBytes(tmp);

            return(output);
        }
Example #11
0
        public void  uncompress(string algorithm = null)
        {
            position = 0;
            var inStream  = getRawStream();
            var outStream = new MemoryStream();

            switch (algorithm)
            {
            case null:
            case CompressionAlgorithm.ZLIB:
            {
                // create inflater
                var inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(inStream);
                inStream.Position = 0;

                // copy stream
                inflater.CopyTo(outStream);
            }
            break;

            case CompressionAlgorithm.LZMA:
            case CompressionAlgorithm.DEFLATE:
            {
                throw new NotImplementedException();

                /*
                 * var outStream:MemoryStream = new MemoryStream();
                 * mStream.Position = 0;
                 * using (DeflateStream decompressionStream = new DeflateStream(mStream, CompressionMode.Decompress))
                 * {
                 *      decompressionStream.CopyTo(outStream);
                 * }
                 */
            }
            }

            // resize to be just the right length
            this.length = 0;
            this.length = (uint)outStream.Length;
            // read from stream
            outStream.Position = 0;
            outStream.Read(mData, 0, (int)this.length);
            position = 0;
        }
        public void Deserialize(Stream input, Endian endian, bool isLocalized)
        {
            var type     = (FormType)input.ReadValueU32(endian);
            var size     = input.ReadValueU32(endian);
            var flags    = input.ReadValueU32(endian);
            var id       = input.ReadValueU32(endian);
            var revision = input.ReadValueU32(endian);
            var version  = input.ReadValueU16(endian);
            var unknown  = input.ReadValueU16(endian);

            if (type != this.Type)
            {
                throw new FormatException();
            }

            this._Flags    = flags;
            this._Id       = id;
            this._Revision = revision;
            this._Unknown  = unknown;

            byte[] bytes;
            if ((flags & BaseFormFlags.IsCompressed) == 0)
            {
                bytes = input.ReadBytes(size);
            }
            else
            {
                if (size < 4)
                {
                    throw new FormatException();
                }

                var uncompressedSize = input.ReadValueU32(endian);
                var zlib             = new InflaterInputStream(input);
                bytes = zlib.ReadBytes(uncompressedSize);
            }

            using (var reader = new FormReader(version, isLocalized, bytes, endian))
            {
                this.ReadFields(reader);
            }
        }
Example #13
0
        /// <summary>
        /// 解压字符串
        /// </summary>
        /// <param name="pBytes"></param>
        /// <returns></returns>
        public byte[] DeCompress(byte[] pBytes)
        {
            ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream mStream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(new MemoryStream(pBytes));
            MemoryStream mMemory = new MemoryStream();
            Int32        mSize;

            byte[] mWriteData = new byte[4096];
            while (true)
            {
                mSize = mStream.Read(mWriteData, 0, mWriteData.Length);
                if (mSize > 0)
                {
                    mMemory.Write(mWriteData, 0, mSize);
                }
                else
                {
                    break;
                }
            }
            mStream.Close();
            return(mMemory.ToArray());
        }
Example #14
0
        static void DownloadFile(string FilePath, string DataUrl, API.CDPUser.FileList FileInfo, byte[] AESKey, byte[] AESIV)
        {
            using (Stream output = File.Open(FilePath, FileMode.Create))
            {
                foreach (var chunk in FileInfo.chunk_list)
                {
                    HttpWebRequest request  = (HttpWebRequest)WebRequest.Create(DataUrl + chunk.sha);
                    WebResponse    response = request.GetResponse();

                    int contentLength = int.Parse(response.Headers[HttpResponseHeader.ContentLength]);

                    if (contentLength != chunk.chunk_size)
                    {
                        throw new Exception("Invalid chunk size detected");
                    }

                    using (Stream input = response.GetResponseStream())
                        using (var memoryStream = new MemoryStream())
                        {
                            input.CopyTo(memoryStream);
                            memoryStream.Position = 0;

                            AESCTRDecrypt(AESKey, AESIV, memoryStream.GetBuffer());

                            if (chunk.uncompressed_size == chunk.chunk_size)
                            {
                                memoryStream.CopyTo(output);
                            }
                            else
                            {
                                using (var zlibStream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(memoryStream))
                                    zlibStream.CopyTo(output);
                            }
                        }
                }
            }
        }
Example #15
0
        /// <summary>
        /// Decompress a byte
        /// </summary>
        public static byte[] Decompress(byte[] data)
        {
            try
            {
                if (data == null)
                {
                    return(null);
                }

                using (var zipStream = new Zip.Streams.InflaterInputStream(new MemoryStream(data)))
                {
                    using (var stream = new MemoryStream())
                    {
                        byte[] buffer = new byte[1024];
                        while (true)
                        {
                            var size = zipStream.Read(buffer, 0, buffer.Length);
                            if (size > 0)
                            {
                                stream.Write(buffer, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        data = stream.ToArray();
                    }
                }
                return(data);
            }
            catch
            {
                return(data);
            }
        }
Example #16
0
        public void Parse(Stream stream)
        {
            BinaryReader reader = new BinaryReader(stream);

            try
            {
                magic_       = new string(reader.ReadChars(3));
                version_     = reader.ReadByte();
                file_length_ = reader.ReadUInt32();
            }
            catch (Exception)
            {
                throw new Exception("NMMファイルではありません。");
            }

            data_stream_ = new MemoryStream((int)stream.Length);

            Stream zis = null;

            try
            {
                if (magic_ == "CWS")
                {
                    zis = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(stream);
                }
                else if (magic_ == "FWS")
                {
                    zis = stream;
                }
                else
                {
                    throw new Exception("NMMファイルではありません。");
                }
                int    s;
                byte[] b = new byte[1024];
                while ((s = zis.Read(b, 0, b.Length)) > 0)
                {
                    data_stream_.Write(b, 0, s);
                }
            }
            finally
            {
                if (magic_ == "CWS" && zis != null)
                {
                    zis.Close();
                }
            }
            data_stream_.Seek(0, SeekOrigin.Begin);
            data_reader_ = new BinaryReader(data_stream_);

            frame_size_.Parse(data_reader_.BaseStream);
            frame_rate_  = data_reader_.ReadUInt16();
            frame_count_ = data_reader_.ReadUInt16();

            while (data_reader_.BaseStream.Position < data_reader_.BaseStream.Length)
            {
                Tags   tags = new Tags();
                ushort v    = data_reader_.ReadUInt16();
                if (v == 0)
                {
                    break;
                }
                tags.tag  = (byte)(v >> 6);
                tags.size = (uint)v & 0x3F;
                if (tags.size == 63)
                {
                    tags.size = data_reader_.ReadUInt32();
                }
                tags.position = (int)data_reader_.BaseStream.Position;
                data_reader_.BaseStream.Seek(tags.size, SeekOrigin.Current);
                tag_list_.Add(tags);
            }
        }
Example #17
0
        fileTableEntry readTableEntry(byte[] data, int size, bool compressed)
        {
            fileTableEntry fte = new fileTableEntry();
            MemoryStream ms = new MemoryStream(data,0,data.Length);

            byte[] filePath = new byte[260];
            if(compressed)
            {
                Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
                byte[] dataDec = new byte[276];
                s2.Read(dataDec,0,276);
                ms = new MemoryStream(dataDec,0,dataDec.Length);
                //ms = mms;
                /*
                wxByte* pBuffer = buffer;
                buffer = deflate(buffer, size, 276);
                wxDELETEA(pBuffer);
                */
            }
            ms.Read(filePath, 0, 260);

            byte[] fileDataOffset = new byte[4];
            byte[] fileDataDecompressedSize = new byte[4];
            byte[] fileDataCompressedSize = new byte[4];

            ms.Read(fileDataOffset, 0, 4);
            ms.Read(fileDataDecompressedSize, 0, 4);
            ms.Read(fileDataCompressedSize, 0, 4);

            //wxMemoryInputStream mis(buffer, 276);
            //wxDataInputStream bis(mis);
            //ms.Read(fte.filePath, 260);
            //fte.filePath = BitConverter.ToString(filePath, 0, 260);
            int length = 0;
            for (int i = 0; i < 260; i++) {
                if ( filePath[i] == (byte)0 ){
                    length = i;
                    break;
                }
            }
            byte[] filePathOk = new byte[length];
            Array.Copy(filePath, filePathOk, length);
            //fte.filePath = Encoding.UTF8.GetString(filePathOk);
            fte.filePath = Encoding.GetEncoding(936).GetString(filePathOk);

            fte.fileDataOffset = BitConverter.ToInt32(fileDataOffset, 0);
            fte.fileDataDecompressedSize = BitConverter.ToInt32(fileDataDecompressedSize, 0);
            fte.fileDataCompressedSize = BitConverter.ToInt32(fileDataCompressedSize, 0);
            //if ( fte.fileDataDecompressedSize < fte.fileDataCompressedSize)
               		//textBox1.Text += "\r\n__"+fte.fileDataDecompressedSize.ToString()+" -- "+fte.filePath+" -- ";
            return fte;
        }
Example #18
0
        private async static Task MeasureStringFor(FontDownload loader, string path)
        {
            byte[] data = null;

            data = await loader.LoadFrom(path);


#if UseOpenFont
            ZlibDecompressStreamFunc zipfunc = (byte[] dataIn, byte[] output) =>
            {
                using (var streamIn = new MemoryStream(dataIn))
                {
#if NET6_0
                    ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream input = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(streamIn);

                    using (var streamOut = new MemoryStream(output))
                        input.CopyTo(streamOut);
#endif
                    return(true);
                }
            };
            WoffDefaultZlibDecompressFunc.DecompressHandler = zipfunc;

            BrotliDecompressStreamFunc brotlifunc = (byte[] dataIn, Stream dataOut) =>
            {
                using (var streamIn = new MemoryStream(dataIn))
                {
                    System.IO.Compression.BrotliStream deflate = new System.IO.Compression.BrotliStream(streamIn, System.IO.Compression.CompressionMode.Decompress);

                    deflate.CopyTo(dataOut);

                    return(true);
                }
            };
            Woff2DefaultBrotliDecompressFunc.DecompressHandler = brotlifunc;


            using (var ms = new System.IO.MemoryStream(data))
            {
                var reader  = new Typography.OpenFont.OpenFontReader();
                var preview = reader.ReadPreview(ms);

                Console.WriteLine("Loaded font reference " + preview.Name);
            }

            using (var ms = new System.IO.MemoryStream(data))
            {
                var    reader   = new Typography.OpenFont.OpenFontReader();
                var    full     = reader.Read(ms);
                string text     = "This is the text to measure";
                var    encoding = Scryber.OpenType.SubTables.CMapEncoding.WindowsUnicode;
                int    fitted;
                var    size = full.MeasureString(text, 0, 12, 10000, true, out fitted);


                Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length);
            }
#else
            TypefaceReader tfreader = new TypefaceReader();
            ITypefaceInfo  info;

            using (var ms = new System.IO.MemoryStream(data))
            {
                info = tfreader.ReadTypeface(ms, path);
                if (null == info)
                {
                    ExitClean("Could not read the info from the font file");
                    return;
                }
                else if (info.FontCount == 0)
                {
                    ExitClean("No fonts could be read from the data: " + info.ErrorMessage ?? "Unknown error");
                    return;
                }
                else
                {
                    Console.WriteLine("Read  " + info.FontCount + " typefaces from the font file " + info.Source);
                    foreach (var reference in info.Fonts)
                    {
                        Console.WriteLine("    " + reference.FamilyName + " (weight: " + reference.FontWeight.ToString() + ", width: " + reference.FontWidth + ", restrictions : " + reference.Restrictions + ", selections : " + reference.Selections.ToString() + ")");
                    }
                }
            }
            TypeMeasureOptions options = new TypeMeasureOptions();

            using (var ms = new System.IO.MemoryStream(data))
            {
                foreach (var fref in info.Fonts)
                {
                    Console.WriteLine();

                    ms.Position = 0;
                    var typeface = tfreader.GetFont(ms, info.Source, fref);

                    if (null == typeface || typeface.IsValid == false)
                    {
                        ExitClean("The font " + fref.ToString() + " was not valid");
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Loaded font reference " + typeface.ToString());
                    }

                    var metrics = typeface.GetMetrics(options);

                    if (null != metrics)
                    {
                        var line = metrics.MeasureLine("This is the text to measure", 0, 12.0, 10000, options);
                        Console.WriteLine("Measured the string to " + line.RequiredWidth + ", " + line.RequiredHeight + " points, and fitted " + line.CharsFitted + " chars" + (line.OnWordBoudary ? " breaking on the word boundary." : "."));
                    }
                    else
                    {
                        ExitClean("No metrics were returned for the font " + typeface.ToString());
                        return;
                    }

                    var ttfData = typeface.GetFileData(DataFormat.TTF);
                    if (null == ttfData)
                    {
                        ExitClean("No data was returned in TTF format for the font " + typeface.ToString());
                    }
                    else
                    {
                        Console.WriteLine("TrueType font data was extracted at " + ttfData.Length + " bytes from the original data of " + data.Length);
                    }

                    var name = typeface.FamilyName;
                    if (typeface.FontWeight != WeightClass.Normal)
                    {
                        if (name.IndexOf(typeface.FontWeight.ToString()) < 0)
                        {
                            name += " " + typeface.FontWeight.ToString();
                        }
                    }
                    if ((typeface.Selections & FontSelection.Italic) > 0)
                    {
                        if (name.IndexOf("Italic") < 0)
                        {
                            name += " Italic";
                        }
                    }

                    loader.SaveToLocal("Output", name + ".ttf", ttfData);
                }
            }

#if Legacy
            var ttf = new Scryber.OpenType.TTFFile(data, 0);

            Console.WriteLine("Loaded font file " + ttf.ToString());

            var    encoding = Scryber.OpenType.SubTables.CMapEncoding.WindowsUnicode;
            string text     = "This is the text to measure";
            int    fitted;

            var size = ttf.MeasureString(encoding, text, 0, 12, 1000, true, out fitted);

            Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length);

            //Measure 90 wiout word boundary
            size = ttf.MeasureString(encoding, text, 0, 12, 90, false, out fitted);

            Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length + " to string '" + text.Substring(0, fitted) + "'");

            //Measure 90 with word boundary
            size = ttf.MeasureString(encoding, text, 0, 12, 90, true, out fitted);

            Console.WriteLine("String Measured to " + size.ToString() + " and fitted " + fitted + " characters out of " + text.Length + " to string '" + text.Substring(0, fitted) + "'");
#endif

#if Performance
            var stopWatch = System.Diagnostics.Stopwatch.StartNew();

            MeasureStringMeasurer(ttf);

            stopWatch.Stop();

            Console.WriteLine("To measure 4 different strings " + maxRepeat + " times took " + stopWatch.Elapsed.TotalMilliseconds + "ms");
#endif
#endif
        }
Example #19
0
        public bool LoadRom(string path, CoreComm nextComm, bool forceAccurateCore = false,
                            int recursiveCount = 0) // forceAccurateCore is currently just for Quicknes vs Neshawk but could be used for other situations
        {
            if (recursiveCount > 1)                 // hack to stop recursive calls from endlessly rerunning if we can't load it
            {
                DoLoadErrorCallback("Failed multiple attempts to load ROM.", "");
                return(false);
            }

            bool cancel = false;

            if (path == null)
            {
                return(false);
            }

            using (var file = new HawkFile())
            {
                //only try mounting a file if a filename was given
                if (!string.IsNullOrEmpty(path))
                {
                    // lets not use this unless we need to
                    // file.NonArchiveExtensions = romExtensions;
                    file.Open(path);

                    // if the provided file doesnt even exist, give up!
                    if (!file.Exists)
                    {
                        return(false);
                    }
                }

                CanonicalFullPath = file.CanonicalFullPath;

                IEmulator nextEmulator = null;
                RomGame   rom          = null;
                GameInfo  game         = null;

                try
                {
                    string ext = null;

                    if (AsLibretro)
                    {
                        string codePathPart = Path.GetFileNameWithoutExtension(nextComm.LaunchLibretroCore);

                        var retro = new LibRetroEmulator(nextComm, nextComm.LaunchLibretroCore);
                        nextEmulator = retro;

                        //kind of dirty.. we need to stash this, and then we can unstash it in a moment, in case the core doesnt fail
                        var oldGame = Global.Game;

                        if (retro.Description.SupportsNoGame && string.IsNullOrEmpty(path))
                        {
                            //must be done before LoadNoGame (which triggers retro_init and the paths to be consumed by the core)
                            //game name == name of core
                            var gameName = codePathPart;
                            Global.Game = game = new GameInfo {
                                Name = gameName, System = "Libretro"
                            };

                            //if we are allowed to run NoGame and we dont have a game, boot up the core that way
                            bool ret = retro.LoadNoGame();

                            Global.Game = oldGame;

                            if (!ret)
                            {
                                DoLoadErrorCallback("LibretroNoGame failed to load. This is weird", "Libretro");
                                retro.Dispose();
                                return(false);
                            }
                        }
                        else
                        {
                            bool ret;

                            //must be done before LoadNoGame (which triggers retro_init and the paths to be consumed by the core)
                            //game name == name of core + extensionless_game_filename
                            var gameName = Path.Combine(codePathPart, Path.GetFileNameWithoutExtension(file.Name));
                            Global.Game = game = new GameInfo {
                                Name = gameName, System = "Libretro"
                            };

                            //if the core requires an archive file, then try passing the filename of the archive
                            //(but do we ever need to actually load the contents of the archive file into ram?)
                            if (retro.Description.NeedsArchives)
                            {
                                if (file.IsArchiveMember)
                                {
                                    throw new InvalidOperationException("Should not have bound file member for libretro block_extract core");
                                }
                                ret = retro.LoadPath(file.FullPathWithoutMember);
                            }
                            else
                            {
                                //otherwise load the data or pass the filename, as requested. but..
                                if (retro.Description.NeedsRomAsPath && file.IsArchiveMember)
                                {
                                    throw new InvalidOperationException("Cannot pass archive member to libretro needs_fullpath core");
                                }

                                if (retro.Description.NeedsRomAsPath)
                                {
                                    ret = retro.LoadPath(file.FullPathWithoutMember);
                                }
                                else
                                {
                                    ret = HandleArchiveBinding(file);
                                    if (ret)
                                    {
                                        ret = retro.LoadData(file.ReadAllBytes());
                                    }
                                }
                            }

                            Global.Game = oldGame;

                            if (!ret)
                            {
                                DoLoadErrorCallback("Libretro failed to load the given file. This is probably due to a core/content mismatch. Moreover, the process is now likely to be hosed. We suggest you restart the program.", "Libretro");
                                retro.Dispose();
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        //if not libretro:

                        //do extension checknig
                        ext = file.Extension.ToLowerInvariant();

                        //do the archive binding we had to skip
                        if (!HandleArchiveBinding(file))
                        {
                            return(false);
                        }
                    }

                    if (string.IsNullOrEmpty(ext))
                    {
                    }
                    else if (ext == ".m3u")
                    {
                        //HACK ZONE - currently only psx supports m3u
                        M3U_File m3u;
                        using (var sr = new StreamReader(path))
                            m3u = M3U_File.Read(sr);
                        if (m3u.Entries.Count == 0)
                        {
                            throw new InvalidOperationException("Can't load an empty M3U");
                        }
                        //load discs for all the m3u
                        m3u.Rebase(Path.GetDirectoryName(path));
                        List <Disc>   discs     = new List <Disc>();
                        List <string> discNames = new List <string>();
                        StringWriter  sw        = new StringWriter();
                        foreach (var e in m3u.Entries)
                        {
                            Disc   disc     = null;
                            string discPath = e.Path;

                            //--- load the disc in a context which will let us abort if it's going to take too long
                            var discMountJob = new DiscMountJob {
                                IN_FromPath = discPath
                            };
                            discMountJob.IN_SlowLoadAbortThreshold = 8;
                            discMountJob.Run();
                            disc = discMountJob.OUT_Disc;

                            if (discMountJob.OUT_SlowLoadAborted)
                            {
                                DoLoadErrorCallback("This disc would take too long to load. Run it through discohawk first, or find a new rip because this one is probably junk", "", LoadErrorType.DiscError);
                                return(false);
                            }

                            if (discMountJob.OUT_ErrorLevel)
                            {
                                throw new InvalidOperationException("\r\n" + discMountJob.OUT_Log);
                            }

                            if (disc == null)
                            {
                                throw new InvalidOperationException("Can't load one of the files specified in the M3U");
                            }

                            var discName = Path.GetFileNameWithoutExtension(discPath);
                            discNames.Add(discName);
                            discs.Add(disc);

                            var discType = new DiscIdentifier(disc).DetectDiscType();
                            sw.WriteLine("{0}", Path.GetFileName(discPath));
                            if (discType == DiscType.SonyPSX)
                            {
                                string discHash = new DiscHasher(disc).Calculate_PSX_BizIDHash().ToString("X8");
                                game = Database.CheckDatabase(discHash);
                                if (game == null || game.IsRomStatusBad() || game.Status == RomStatus.NotInDatabase)
                                {
                                    sw.WriteLine("Disc could not be identified as known-good. Look for a better rip.");
                                }
                                else
                                {
                                    sw.WriteLine("Disc was identified (99.99% confidently) as known good with disc id hash CRC32:{0:X8}", discHash);
                                    sw.WriteLine("Nonetheless it could be an unrecognized romhack or patched version.");
                                    sw.WriteLine("According to redump.org, the ideal hash for entire disc is: CRC32:{0:X8}", game.GetStringValue("dh"));
                                    sw.WriteLine("The file you loaded hasn't been hashed entirely (it would take too long)");
                                    sw.WriteLine("Compare it with the full hash calculated by the PSX menu's Hash Discs tool");
                                }
                            }
                            else
                            {
                                sw.WriteLine("Not a PSX disc");
                            }
                            sw.WriteLine("-------------------------");
                        }

                        nextEmulator = new Octoshock(nextComm, discs, discNames, null, GetCoreSettings <Octoshock>(), GetCoreSyncSettings <Octoshock>());
                        nextEmulator.CoreComm.RomStatusDetails = sw.ToString();
                        game = new GameInfo {
                            Name = Path.GetFileNameWithoutExtension(file.Name)
                        };
                        game.System = "PSX";
                    }
                    else if (ext == ".iso" || ext == ".cue" || ext == ".ccd")
                    {
                        if (file.IsArchive)
                        {
                            throw new InvalidOperationException("Can't load CD files from archives!");
                        }

                        string discHash = null;

                        //--- load the disc in a context which will let us abort if it's going to take too long
                        var discMountJob = new DiscMountJob {
                            IN_FromPath = path
                        };
                        discMountJob.IN_SlowLoadAbortThreshold = 8;
                        discMountJob.Run();

                        if (discMountJob.OUT_SlowLoadAborted)
                        {
                            DoLoadErrorCallback("This disc would take too long to load. Run it through discohawk first, or find a new rip because this one is probably junk", "", LoadErrorType.DiscError);
                            return(false);
                        }

                        if (discMountJob.OUT_ErrorLevel)
                        {
                            throw new InvalidOperationException("\r\n" + discMountJob.OUT_Log);
                        }

                        var disc = discMountJob.OUT_Disc;
                        //-----------

                        //TODO - use more sophisticated IDer
                        var discType = new DiscIdentifier(disc).DetectDiscType();
                        if (discType == DiscType.SonyPSX)
                        {
                            discHash = new DiscHasher(disc).Calculate_PSX_BizIDHash().ToString("X8");
                        }
                        else
                        {
                            discHash = new DiscHasher(disc).OldHash();
                        }

                        game = Database.CheckDatabase(discHash);
                        if (game == null)
                        {
                            // try to use our wizard methods
                            game = new GameInfo {
                                Name = Path.GetFileNameWithoutExtension(file.Name), Hash = discHash
                            };

                            switch (new DiscIdentifier(disc).DetectDiscType())
                            {
                            case DiscType.SegaSaturn:
                                game.System = "SAT";
                                break;

                            case DiscType.SonyPSP:
                                game.System = "PSP";
                                break;

                            default:
                            case DiscType.SonyPSX:
                                game.System = "PSX";
                                break;

                            case DiscType.MegaCD:
                                game.System = "GEN";
                                break;

                            case DiscType.AudioDisc:
                            case DiscType.TurboCD:
                            case DiscType.UnknownCDFS:
                            case DiscType.UnknownFormat:
                                game.System = "PCECD";
                                break;
                            }
                        }

                        switch (game.System)
                        {
                        case "GEN":
                            var genesis = new GPGX(
                                nextComm, null, disc, GetCoreSettings <GPGX>(), GetCoreSyncSettings <GPGX>());
                            nextEmulator = genesis;
                            break;

                        case "SAT":
                            nextEmulator = new Yabause(nextComm, disc, GetCoreSyncSettings <Yabause>());
                            break;

                        case "PSP":
                            nextEmulator = new PSP(nextComm, file.Name);
                            break;

                        case "PSX":
                            nextEmulator = new Octoshock(nextComm, new List <Disc>(new[] { disc }), new List <string>(new[] { Path.GetFileNameWithoutExtension(path) }), null, GetCoreSettings <Octoshock>(), GetCoreSyncSettings <Octoshock>());
                            if (game.IsRomStatusBad() || game.Status == RomStatus.NotInDatabase)
                            {
                                nextEmulator.CoreComm.RomStatusDetails = "Disc could not be identified as known-good. Look for a better rip.";
                            }
                            else
                            {
                                StringWriter sw = new StringWriter();
                                sw.WriteLine("Disc was identified (99.99% confidently) as known good with disc id hash CRC32:{0:X8}", discHash);
                                sw.WriteLine("Nonetheless it could be an unrecognized romhack or patched version.");
                                sw.WriteLine("According to redump.org, the ideal hash for entire disc is: CRC32:{0:X8}", game.GetStringValue("dh"));
                                sw.WriteLine("The file you loaded hasn't been hashed entirely (it would take too long)");
                                sw.WriteLine("Compare it with the full hash calculated by the PSX menu's Hash Discs tool");
                                nextEmulator.CoreComm.RomStatusDetails = sw.ToString();
                            }
                            break;

                        case "PCE":
                        case "PCECD":
                            nextEmulator = new PCEngine(nextComm, game, disc, GetCoreSettings <PCEngine>(), GetCoreSyncSettings <PCEngine>());
                            break;
                        }
                    }
                    else if (file.Extension.ToLowerInvariant() == ".xml")
                    {
                        try
                        {
                            var xmlGame = XmlGame.Create(file);                             // if load fails, are we supposed to retry as a bsnes XML????????
                            game = xmlGame.GI;

                            switch (game.System)
                            {
                            case "GB":
                            case "DGB":
                                // adelikat: remove need for tags to be hardcoded to left and right, we should clean this up, also maybe the DGB core should just take the xml file and handle it itself
                                var leftBytes  = xmlGame.Assets.First().Value;
                                var rightBytes = xmlGame.Assets.Skip(1).First().Value;

                                var left  = Database.GetGameInfo(leftBytes, "left.gb");
                                var right = Database.GetGameInfo(rightBytes, "right.gb");
                                nextEmulator = new GambatteLink(
                                    nextComm,
                                    left,
                                    leftBytes,
                                    right,
                                    rightBytes,
                                    GetCoreSettings <GambatteLink>(),
                                    GetCoreSyncSettings <GambatteLink>(),
                                    Deterministic);

                                // other stuff todo
                                break;

                            case "AppleII":
                                var assets = xmlGame.Assets.Select(a => Database.GetGameInfo(a.Value, a.Key));
                                var roms   = xmlGame.Assets.Select(a => a.Value);
                                nextEmulator = new AppleII(
                                    nextComm,
                                    assets,
                                    roms,
                                    (AppleII.Settings)GetCoreSettings <AppleII>());
                                break;

                            case "C64":
                                nextEmulator = new C64(
                                    nextComm,
                                    xmlGame.Assets.Select(a => a.Value),
                                    (C64.C64Settings)GetCoreSettings <C64>(),
                                    (C64.C64SyncSettings)GetCoreSyncSettings <C64>()
                                    );
                                break;

                            case "PSX":
                                var entries   = xmlGame.AssetFullPaths;
                                var discs     = new List <Disc>();
                                var discNames = new List <string>();
                                var sw        = new StringWriter();
                                foreach (var e in entries)
                                {
                                    Disc   disc     = null;
                                    string discPath = e;

                                    //--- load the disc in a context which will let us abort if it's going to take too long
                                    var discMountJob = new DiscMountJob {
                                        IN_FromPath = discPath
                                    };
                                    discMountJob.IN_SlowLoadAbortThreshold = 8;
                                    discMountJob.Run();
                                    disc = discMountJob.OUT_Disc;

                                    if (discMountJob.OUT_SlowLoadAborted)
                                    {
                                        DoLoadErrorCallback("This disc would take too long to load. Run it through discohawk first, or find a new rip because this one is probably junk", "PSX", LoadErrorType.DiscError);
                                        return(false);
                                    }

                                    if (discMountJob.OUT_ErrorLevel)
                                    {
                                        throw new InvalidOperationException("\r\n" + discMountJob.OUT_Log);
                                    }

                                    if (disc == null)
                                    {
                                        throw new InvalidOperationException("Can't load one of the files specified in the M3U");
                                    }

                                    var discName = Path.GetFileNameWithoutExtension(discPath);
                                    discNames.Add(discName);
                                    discs.Add(disc);

                                    var discType = new DiscIdentifier(disc).DetectDiscType();
                                    sw.WriteLine("{0}", Path.GetFileName(discPath));
                                    if (discType == DiscType.SonyPSX)
                                    {
                                        string discHash = new DiscHasher(disc).Calculate_PSX_BizIDHash().ToString("X8");
                                        game = Database.CheckDatabase(discHash);
                                        if (game == null || game.IsRomStatusBad() || game.Status == RomStatus.NotInDatabase)
                                        {
                                            sw.WriteLine("Disc could not be identified as known-good. Look for a better rip.");
                                        }
                                        else
                                        {
                                            sw.WriteLine("Disc was identified (99.99% confidently) as known good with disc id hash CRC32:{0:X8}", discHash);
                                            sw.WriteLine("Nonetheless it could be an unrecognized romhack or patched version.");
                                            sw.WriteLine("According to redump.org, the ideal hash for entire disc is: CRC32:{0:X8}", game.GetStringValue("dh"));
                                            sw.WriteLine("The file you loaded hasn't been hashed entirely (it would take too long)");
                                            sw.WriteLine("Compare it with the full hash calculated by the PSX menu's Hash Discs tool");
                                        }
                                    }
                                    else
                                    {
                                        sw.WriteLine("Not a PSX disc");
                                    }
                                    sw.WriteLine("-------------------------");
                                }

                                // todo: copy pasta from PSX .cue section
                                nextEmulator = new Octoshock(nextComm, discs, discNames, null, GetCoreSettings <Octoshock>(), GetCoreSyncSettings <Octoshock>());
                                nextEmulator.CoreComm.RomStatusDetails = sw.ToString();
                                game = new GameInfo {
                                    Name = Path.GetFileNameWithoutExtension(file.Name)
                                };
                                game.System = "PSX";

                                break;

                            default:
                                return(false);
                            }
                        }
                        catch (Exception ex)
                        {
                            try
                            {
                                // need to get rid of this hack at some point
                                rom = new RomGame(file);
                                ((CoreFileProvider)nextComm.CoreFileProvider).SubfileDirectory = Path.GetDirectoryName(path.Replace("|", string.Empty));                                 // Dirty hack to get around archive filenames (since we are just getting the directory path, it is safe to mangle the filename
                                byte[] romData = null;
                                byte[] xmlData = rom.FileData;

                                game        = rom.GameInfo;
                                game.System = "SNES";

                                var snes = new LibsnesCore(game, romData, Deterministic, xmlData, nextComm, GetCoreSettings <LibsnesCore>(), GetCoreSyncSettings <LibsnesCore>());
                                nextEmulator = snes;
                            }
                            catch
                            {
                                DoLoadErrorCallback(ex.ToString(), "DGB", LoadErrorType.XML);
                                return(false);
                            }
                        }
                    }
                    else if (file.Extension.ToLowerInvariant() == ".psf" || file.Extension.ToLowerInvariant() == ".minipsf")
                    {
                        Func <Stream, int, byte[]> cbDeflater = (Stream instream, int size) =>
                        {
                            var          inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(false);
                            var          iis      = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(instream, inflater);
                            MemoryStream ret      = new MemoryStream();
                            iis.CopyTo(ret);
                            return(ret.ToArray());
                        };
                        PSF psf = new PSF();
                        psf.Load(path, cbDeflater);
                        nextEmulator = new Octoshock(nextComm, psf, GetCoreSettings <Octoshock>(), GetCoreSyncSettings <Octoshock>());
                        nextEmulator.CoreComm.RomStatusDetails = "It's a PSF, what do you want. Oh, tags maybe?";

                        //total garbage, this
                        rom  = new RomGame(file);
                        game = rom.GameInfo;
                    }
                    else if (ext != null)                     // most extensions
                    {
                        rom = new RomGame(file);

                        //hacky for now
                        if (file.Extension.ToLowerInvariant() == ".exe")
                        {
                            rom.GameInfo.System = "PSX";
                        }
                        else if (file.Extension.ToLowerInvariant() == ".nsf")
                        {
                            rom.GameInfo.System = "NES";
                        }


                        if (string.IsNullOrEmpty(rom.GameInfo.System))
                        {
                            // Has the user picked a preference for this extension?
                            if (PreferredPlatformIsDefined(rom.Extension.ToLowerInvariant()))
                            {
                                rom.GameInfo.System = Global.Config.PreferredPlatformsForExtensions[rom.Extension.ToLowerInvariant()];
                            }
                            else if (ChoosePlatform != null)
                            {
                                var result = ChoosePlatform(rom);
                                if (!string.IsNullOrEmpty(result))
                                {
                                    rom.GameInfo.System = result;
                                }
                                else
                                {
                                    cancel = true;
                                }
                            }
                        }

                        game = rom.GameInfo;

                        var isXml = false;

                        // other xml has already been handled
                        if (file.Extension.ToLowerInvariant() == ".xml")
                        {
                            game.System = "SNES";
                            isXml       = true;
                        }


                        CoreInventory.Core core = null;

                        switch (game.System)
                        {
                        default:
                            core = CoreInventory.Instance[game.System];
                            break;

                        case null:
                            // The user picked nothing in the Core picker
                            break;

                        case "83P":
                            var ti83Bios     = ((CoreFileProvider)nextComm.CoreFileProvider).GetFirmware("TI83", "Rom", true);
                            var ti83BiosPath = ((CoreFileProvider)nextComm.CoreFileProvider).GetFirmwarePath("TI83", "Rom", true);
                            using (var ti83AsHawkFile = new HawkFile())
                            {
                                ti83AsHawkFile.Open(ti83BiosPath);
                                var ti83BiosAsRom = new RomGame(ti83AsHawkFile);
                                var ti83          = new TI83(nextComm, ti83BiosAsRom.GameInfo, ti83Bios, GetCoreSettings <TI83>());
                                ti83.LinkPort.SendFileToCalc(File.OpenRead(path), false);
                                nextEmulator = ti83;
                            }
                            break;

                        case "SNES":
                            if (Global.Config.SNES_InSnes9x && VersionInfo.DeveloperBuild)
                            {
                                core = CoreInventory.Instance["SNES", "Snes9x"];
                            }
                            else
                            {
                                // need to get rid of this hack at some point
                                ((CoreFileProvider)nextComm.CoreFileProvider).SubfileDirectory = Path.GetDirectoryName(path.Replace("|", String.Empty));                                         // Dirty hack to get around archive filenames (since we are just getting the directory path, it is safe to mangle the filename
                                var romData = isXml ? null : rom.FileData;
                                var xmlData = isXml ? rom.FileData : null;
                                var snes    = new LibsnesCore(game, romData, Deterministic, xmlData, nextComm, GetCoreSettings <LibsnesCore>(), GetCoreSyncSettings <LibsnesCore>());
                                nextEmulator = snes;
                            }

                            break;

                        case "NES":
                            if (!Global.Config.NES_InQuickNES || forceAccurateCore)
                            {
                                core = CoreInventory.Instance["NES", "NesHawk"];
                            }
                            else
                            {
                                core = CoreInventory.Instance["NES", "QuickNes"];
                            }

                            break;

                        case "GB":
                        case "GBC":
                            if (!Global.Config.GB_AsSGB)
                            {
                                core = CoreInventory.Instance["GB", "Gambatte"];
                            }
                            else
                            {
                                try
                                {
                                    game.System = "SNES";
                                    game.AddOption("SGB");
                                    var snes = new LibsnesCore(game, rom.FileData, Deterministic, null, nextComm, GetCoreSettings <LibsnesCore>(), GetCoreSyncSettings <LibsnesCore>());
                                    nextEmulator = snes;
                                }
                                catch
                                {
                                    // failed to load SGB bios or game does not support SGB mode.
                                    // To avoid catch-22, disable SGB mode
                                    Global.Config.GB_AsSGB = false;
                                    throw;
                                }
                            }

                            break;

                        case "A78":
                            var gamedbpath = Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "EMU7800.csv");
                            nextEmulator = new Atari7800(nextComm, game, rom.RomData, gamedbpath);
                            break;

                        case "C64":
                            var c64 = new C64(nextComm, Enumerable.Repeat(rom.RomData, 1), GetCoreSettings <C64>(), GetCoreSyncSettings <C64>());
                            nextEmulator = c64;
                            break;

                        case "GBA":
                            //core = CoreInventory.Instance["GBA", "Meteor"];
                            if (Global.Config.GBA_UsemGBA)
                            {
                                core = CoreInventory.Instance["GBA", "mGBA"];
                            }
                            else
                            {
                                core = CoreInventory.Instance["GBA", "VBA-Next"];
                            }
                            break;

                        case "PSX":
                            nextEmulator = new Octoshock(nextComm, null, null, rom.FileData, GetCoreSettings <Octoshock>(), GetCoreSyncSettings <Octoshock>());
                            nextEmulator.CoreComm.RomStatusDetails = "PSX etc.";
                            break;
                        }

                        if (core != null)
                        {
                            // use coreinventory
                            nextEmulator = core.Create(nextComm, game, rom.RomData, rom.FileData, Deterministic, GetCoreSettings(core.Type), GetCoreSyncSettings(core.Type));
                        }
                    }

                    if (nextEmulator == null)
                    {
                        if (!cancel)
                        {
                            DoLoadErrorCallback("No core could load the rom.", null);
                        }
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    string system = null;
                    if (game != null)
                    {
                        system = game.System;
                    }

                    // all of the specific exceptions we're trying to catch here aren't expected to have inner exceptions,
                    // so drill down in case we got a TargetInvocationException or something like that
                    while (ex.InnerException != null)
                    {
                        ex = ex.InnerException;
                    }

                    // Specific hack here, as we get more cores of the same system, this isn't scalable
                    if (ex is UnsupportedGameException)
                    {
                        if (system == "NES")
                        {
                            DoMessageCallback("Unable to use quicknes, using NESHawk instead");
                        }

                        return(LoadRom(path, nextComm, true, recursiveCount + 1));
                    }
                    else if (ex is MissingFirmwareException)
                    {
                        DoLoadErrorCallback(ex.Message, system, path, Deterministic, LoadErrorType.MissingFirmware);
                    }
                    else if (ex is CGBNotSupportedException)
                    {
                        // Note: GB as SGB was set to false by this point, otherwise we would want to do it here
                        DoMessageCallback("Failed to load a GB rom in SGB mode.  Disabling SGB Mode.");
                        return(LoadRom(path, nextComm, false, recursiveCount + 1));
                    }
                    else
                    {
                        DoLoadErrorCallback("A core accepted the rom, but threw an exception while loading it:\n\n" + ex, system);
                    }

                    return(false);
                }

                Rom            = rom;
                LoadedEmulator = nextEmulator;
                Game           = game;
                return(true);
            }
        }
 /// <summary>
 /// Decompresses a byte array 
 /// </summary>
 /// <param name="bytDs">Byte array to be decompressed.</param>
 /// <returns>byte[].</returns>
 public static byte[] DeCompressFileContent(byte[] bytDs)
 {
     byte[] blobContent = new byte[100];
     MemoryStream ms = new MemoryStream(bytDs);
     ms.Position = 0;
     Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);// Inflater(); // SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
     blobContent = ReadFully(s2, 100);
     return (byte[]) blobContent;
 }
Example #21
0
        public void Code(Stream inStream, Stream outStream, long inSize, long outSize, ICodeProgress progress)
        {
            //outStream.ReadByte();
            if (outSize > 0)
            {
                outStream.SetLength(outSize);
            }
            using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream DStream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(inStream))
            {
                DStream.CopyTo(outStream);// incompatibilité avec zlib
            }

            /*
             * System.IO.Compression.GZipStream DStream = new System.IO.Compression.GZipStream(inStream, System.IO.Compression.CompressionMode.Decompress,true);
             * outStream=DStream;
             */
        }
Example #22
0
        private bool ReadHeader()
        {
            // Read file signature
            this.signature = Encoding.ASCII.GetString(this.swf.ReadUI8(3));     // "FWS" or "CWS" for ZLIB compressed (v6.0 or later)
            if (this.signature != "FWS" &&
                this.signature != "CWS")
            {
                Trace.WriteLine("Not a valid SWF (Flash) file signature");
                return false;
            }
            Trace.WriteLine(String.Format("Signature   : {0}", this.Signature));

            // Read file version
            this.version = this.swf.ReadUI8();
            Trace.WriteLine(String.Format("Version     : {0}", this.Version));

            // File length
            this.fileLength = this.swf.ReadUI32();
            Trace.WriteLine(String.Format("File length : {0} bytes", this.FileLength));

            // If the file is compressed, this is where the ZLIB decompression ("inflate") begins
            if (this.signature == "CWS")
            {
                // Begin inflating stream
                ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream inflatedStream = 
                    new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(this.stream);
                swf.Stream = inflatedStream;
            }

            // Frame size
            Rect frameSize = new Rect(swf);
            this.frameWidth = frameSize.XMax;
            this.frameHeight = frameSize.YMax;
            Trace.WriteLine(String.Format("Frame width : {0} twips ({1} pixels)", this.FrameWidth, this.FrameWidth / 20));
            Trace.WriteLine(String.Format("Frame height: {0} twips ({1} pixels)", this.FrameHeight, this.FrameHeight / 20));

            // Frame rate (stored in UI8.UI8 format)
            ushort frameRateMinor = swf.ReadUI8();
            ushort frameRateMajor = swf.ReadUI8();
            this.frameRate = Convert.ToSingle(String.Format("{0},{1}", frameRateMajor, frameRateMinor));    // TODO: Improve this later
            Trace.WriteLine(String.Format("Frame rate  : {0} fps", this.FrameRate));

            // Frame count
            this.frameCount = swf.ReadUI16();
            Trace.WriteLine(String.Format("Frame count : {0}", this.FrameCount));

            return true;
        }
Example #23
0
        /// <summary>
        /// Open the record, decompressing it as necessary.
        /// </summary>
        /// <returns>A <see cref="Stream"/> for reading the record.</returns>
        public override Stream Open()
        {
            var archive = Archive;
            var reader  = archive.DataReader;

            reader.BaseStream.Position = Offset;

            if (fixedUncompressedSize != Size && fixedUncompressedSize != 0)
            {
                if (Size == fixedUncompressedSize)
                {
                    return(new MemoryStream(reader.ReadBytes(checked ((int)fixedUncompressedSize)), false));
                }
                else
                {
                    byte[] data = new byte[fixedUncompressedSize];

                    var deflate = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(reader.BaseStream);
                    var read    = deflate.Read(data, 0, data.Length);
                    if (read != fixedUncompressedSize)
                    {
                        throw new InvalidDataException();
                    }
                    return(new MemoryStream(data, false));
                }
            }

            var magic = reader.ReadString(4, Encoding.ASCII);

            if (magic == "DCX\0")
            {
                reader.Require(0x100);
                reader.RequireBE(0x18);
                reader.RequireBE(0x24);
                reader.RequireBE(0x24);
                int totalHeaderLength = reader.ReadInt32BE();
                reader.RequireMagic("DCS\0");
                int uncompressedSize = reader.ReadInt32BE();
                int compressedSize   = reader.ReadInt32BE();
                reader.RequireMagic("DCP\0");

                string compressionMethod = reader.ReadStringz(4, Encoding.ASCII);

                reader.RequireBE(0x20);
                reader.Require(9);
                reader.Require(archive.IsDarkSoulsII ? 0x100 : 0);
                reader.RequireZeroes(8);
                reader.Require(archive.IsDarkSoulsII ? 0x11000 : 0x10100);
                reader.RequireMagic("DCA\0");
                int compressionHeaderLength = reader.ReadInt32BE();

                byte[] data = new byte[uncompressedSize];
                switch (compressionMethod)
                {
                case "DFLT":
                    if (true)
                    {
                        var deflate = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(reader.BaseStream);

                        var read = deflate.Read(data, 0, data.Length);
                        if (read != uncompressedSize)
                        {
                            throw new Exception();
                        }
                        return(new MemoryStream(data, false));
                    }

                case "EDGE":
                    if (true)
                    {
                        reader.RequireMagic("EgdT");
                        reader.RequireBE(0x10100);
                        reader.RequireBE(0x24);                                 // sectionHeaderStart
                        int alignment = reader.ReadInt32BE();
                        if (alignment != 0x10)
                        {
                            throw new InvalidDataException();
                        }
                        int normalBlockSize  = reader.ReadInt32BE();
                        int lastBlockSize    = reader.ReadInt32BE();
                        int headerLength     = reader.ReadInt32BE();
                        int sectionCount     = reader.ReadInt32BE();
                        int sectionEntrySize = reader.ReadInt16BE();
                        if (headerLength != 36 + sectionCount * sectionEntrySize)
                        {
                            throw new InvalidDataException();
                        }
                        reader.RequireZeroes(2);
                        long blockHeaderStart = reader.BaseStream.Position;
                        long blockDataStart   = blockHeaderStart + sectionCount * sectionEntrySize;
                        int  offset           = 0;

                        for (int index = 0; index < sectionCount; index++)
                        {
                            reader.BaseStream.Position = blockHeaderStart + index * sectionEntrySize;
                            long blockOffset           = reader.ReadInt64BE();
                            int  blockSize             = reader.ReadInt32BE();
                            int  blockMode             = reader.ReadInt32BE();
                            int  uncompressedBlockSize = index < sectionCount - 1 ? normalBlockSize : lastBlockSize;

                            reader.BaseStream.Position = blockDataStart + blockOffset;

                            switch (blockMode)
                            {
                            case 0:
                                if (blockSize < uncompressedBlockSize)
                                {
                                    throw new InvalidDataException();
                                }
                                reader.Read(data, offset, uncompressedBlockSize);
                                break;

                            case 1:
                                var deflate     = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(reader.BaseStream, new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(true));
                                int deflateRead = deflate.Read(data, offset, uncompressedBlockSize);
                                if (deflateRead != uncompressedBlockSize)
                                {
                                    throw new InvalidDataException();
                                }
                                break;

                            default:
                                throw new NotSupportedException("Unsupported block mode " + blockMode);
                            }

                            offset += uncompressedBlockSize;
                        }

                        if (offset != data.Length)
                        {
                            throw new InvalidDataException();
                        }
                        return(new MemoryStream(data, false));
                    }

                default:
                    throw new NotImplementedException(compressionMethod + " compression is not implemented.");
                }
            }
            else
            {
                reader.BaseStream.Seek(-4, SeekOrigin.Current);
                byte[] data = reader.ReadBytes(checked ((int)Size));
                return(new MemoryStream(data, false));
            }
        }
Example #24
0
        private static void DecodeString(string str)
        {
            DateTime datetime = DateTime.Now;

            using (FileStream fs = new FileStream($"output_{datetime.ToShortDateString().Replace("/", "")}_{datetime.ToShortTimeString().Replace(":", "")}.chunk", FileMode.Create))
            {
                MemoryStream ms = new MemoryStream();

                CEBase85.TryBase85ToBin(str, ref ms);

                using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream zs = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms))
                {
                    zs.CopyTo(fs);
                    fs.Flush();
                }

                ms.Dispose();
            }
        }
Example #25
0
        public void Load()
        {
            if (IsLoaded)
            {
                return;
            }
            if (IsUnloaded)
            {
                throw new InvalidOperationException("Cannot reload object once it's unloaded");
            }

            if ((NumGlommed > 0) || (ObjectSizeInFile > 0))
            {
                byte[] buffer;

                if (IsCompressed)
                {
                    int dataLen = 8 * NumGlommed + ObjectSizeInFile;
                    int maxLen  = dataLen + 8;
                    buffer = new byte[maxLen];

                    // Decompress DataBuffer
                    using (var ms = new System.IO.MemoryStream(DataBuffer))
                        using (var istream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms, new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(false)))
                        {
                            int readBytes = istream.Read(buffer, 0, maxLen);
                            Zeroes = readBytes - dataLen;
                            //istream.Read(buffer, 0, 0xF);
                        }
                }
                else
                {
                    string      path      = String.Format("/resources/systemgenerated/prototypes/{0}.node", this.Id);
                    TorLib.File protoFile = TorLib.Assets.FindFile(path);
                    using (var fs = protoFile.Open())
                        using (var br = new GomBinaryReader(fs, Encoding.UTF8))
                        {
                            br.ReadBytes(NodeDataOffset);
                            buffer = br.ReadBytes(ObjectSizeInFile);
                            Zeroes = 0;
                        }
                }

                // Load data from decompressed buffer
                using (var ms = new System.IO.MemoryStream(buffer))
                    using (var br = new GomBinaryReader(ms))
                    {
                        ms.Position         = Zeroes;
                        this.GlommedClasses = new List <DomClass>();
                        for (var glomIdx = 0; glomIdx < NumGlommed; glomIdx++)
                        {
                            var glomClassId = br.ReadUInt64();
                            var glomClass   = DataObjectModel.Get <DomClass>(glomClassId);
                            this.GlommedClasses.Add(glomClass);
                        }

                        this._data = ScriptObjectReader.ReadObject(this.DomClass, br);
                    }

                //FirstBytes = buffer.Take(0xF).ToArray();
            }

            this.DataBuffer = null; // Since we're loaded, we don't need to hold on to the compressed data anymore
            IsLoaded        = true;
        }
Example #26
0
        /// <summary>
        /// Decompresses the specified bytes, using the specified compression type and output encoding
        /// </summary>
        /// <param name="bytes">The bytes to decompress.</param>
        /// <param name="offset">The amount of offset to apply to the byte array before beginning decompression.</param>
        /// <param name="length">The length of bytes to decompress in the byte array.</param>
        /// <param name="compressionType">Type of the compression applied to the input string.</param>
        /// <param name="outputEncoding">The output encoding to apply.</param>
        /// <returns>Returns a string representing the uncompressed verison of the input</returns>
        public static string Decompress(byte[] bytes, int offset, int length, CompressionType compressionType, Encoding outputEncoding)
        {
            if (bytes == null || bytes.Length == 0)
            {
                return(string.Empty);
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "offset cannot be less than zero");
            }
            if (length > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("length", "length cannot be greater than bytes.length");
            }
            if (length + offset > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("length", "length + offset cannot be greater than bytes.length");
            }

            using (MemoryStream memoryStream = new MemoryStream(bytes)) {
                Stream stream = null;
                switch (compressionType)
                {
                case CompressionType.BZip:
                    stream = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(memoryStream);
                    break;

                case CompressionType.GZip:
                    stream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(memoryStream);
                    break;

                // case CompressionType.Tar:
                //    stream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream);
                //    break;
                case CompressionType.Zip:
                    stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(memoryStream);
                    break;
                }

                if (stream != null)
                {
                    var           decoder = outputEncoding.GetDecoder();
                    StringBuilder builder = new StringBuilder();
                    byte[]        buffer  = new byte[2048];
                    char[]        chars   = new char[2048];

                    while (true)
                    {
                        int size = stream.Read(buffer, 0, buffer.Length);
                        if (size == 0)
                        {
                            break;
                        }
                        int totalChars = decoder.GetChars(buffer, 0, size, chars, 0);
                        builder.Append(chars, 0, totalChars);
                    }

                    stream.Dispose();

                    return(builder.ToString());
                }
            }

            return(string.Empty);
        }
Example #27
0
		public void  uncompress(string algorithm = null) {

			var inStream = getRawStream();
			var outStream = new MemoryStream();

			switch (algorithm)
			{
			case null:
			case CompressionAlgorithm.ZLIB:
			{
				// create inflater
				var inflater =  new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(inStream);

				// copy stream				
				inflater.CopyTo(outStream);
			}
			break;

			case CompressionAlgorithm.LZMA:
			case CompressionAlgorithm.DEFLATE:
			{
				throw new NotImplementedException();
				/*
				var outStream:MemoryStream = new MemoryStream();
				mStream.Position = 0;
				using (DeflateStream decompressionStream = new DeflateStream(mStream, CompressionMode.Decompress))
				{
					decompressionStream.CopyTo(outStream);
				}
				*/
			}
			}

			// resize to be just the right length
			this.length = 0;
			this.length = (uint)outStream.Length;
			// read from stream
			outStream.Read(mData, 0, (int)this.length);
		}
Example #28
0
        void Button3Click(object sender, EventArgs e)
        {
            byte[] data = System.Text.Encoding.GetEncoding(936).GetBytes("abcdefghijklmnopqrstyhdzayhdzaydhazudzaduvwxyz");
            int dataSize = data.Length;
            MemoryStream ms = new MemoryStream();
            ICSharpCode.SharpZipLib.Zip.Compression.Deflater defl = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(9,false);
            Stream s = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.DeflaterOutputStream(ms,defl);
            s.Write(data, 0, data.Length);
            s.Close();

            byte[] dataComp = (byte[])ms.ToArray();

            MemoryStream msb = new MemoryStream(dataComp,0,dataComp.Length);
            Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(msb);
            byte[] dataDec = new byte[dataSize];
            s2.Read(dataDec,0,dataSize);

            string res = Encoding.GetEncoding(936).GetString(dataDec);
            textBox1.Text = dataComp.Length.ToString();
        }
Example #29
0
            public void Parse(Tags tags, BinaryReader reader, int size)
            {
                reader.BaseStream.Seek(tags.position, SeekOrigin.Begin);
                image_id_ = (int)reader.ReadUInt16();

                if ((int)tags.tag == (int)ObjectType.DefineBitsLossLess2)
                {
                    is_loss_less_ = true;

                    byte format = reader.ReadByte();
                    width_  = reader.ReadUInt16();
                    height_ = reader.ReadUInt16();

                    MemoryStream ms = new MemoryStream();

                    StreamUtil.Copy(reader.BaseStream, ms, size - 7);
                    ms.Seek(0, SeekOrigin.Begin);

                    ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream zis =
                        new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);

                    MemoryStream ms2 = new MemoryStream();

                    byte[] b = new byte[1024];
                    int    s;
                    while ((s = zis.Read(b, 0, b.Length)) > 0)
                    {
                        ms2.Write(b, 0, s);
                    }
                    zis.Close();

                    ms2.Seek(0, SeekOrigin.Begin);
                    raw_data_ = new byte[ms2.Length];
                    ms2.Read(raw_data_, 0, raw_data_.Length);
                }
                else
                {
                    reader.BaseStream.Seek(tags.position, SeekOrigin.Begin);
                    image_id_ = (int)reader.ReadUInt16();
                    MemoryStream ms = new MemoryStream();
                    StreamUtil.Copy(reader.BaseStream, ms, size - 2);
                    image_ = new Bitmap(ms);
                }
            }
Example #30
0
        void ExtractPck()
        {
            //openFileDialog1.ShowDialog();
            string pckFile = textBox2.Text;

            //folderBrowserDialog1.ShowDialog();
            string pckFolder = textBox3.Text + "\\";

            FileStream  fs = File.OpenRead(pckFile);
            BinaryReader br = new BinaryReader(fs);

            fs.Seek(-8,SeekOrigin.End);
            int entryCount = br.ReadInt32();

            fs.Seek(-272,SeekOrigin.End);
            int fileTableOffset = br.ReadInt32() ^ KEY_1;
            fs.Seek(fileTableOffset,SeekOrigin.Begin);

            UpdateLeftText("Reading files...");

            int entrySize;
            fileTableEntry[] fileTable = new fileTableEntry[entryCount];
            int t = 0;
            for (int i = 0; i < entryCount; i++) {
                entrySize = br.ReadInt32() ^ KEY_1;
                entrySize = br.ReadInt32() ^ KEY_2;

                byte[] data = br.ReadBytes(entrySize);
               // textBox1.Text += entrySize.ToString()+"\r\n";
                if(entrySize < 276)
                {
                    fileTable[i] = readTableEntry(data, entrySize, true);
                    t++;
                }
                // no zlib
                else
                {
                    fileTable[i] = readTableEntry(data, entrySize, false);
                }
                ShowProgress(entryCount, i+1);
            }

            //UpdateLeftText("Extracting files...");
            //textBox1.Text += fileTable[0].filePath+"\r\n"+fileTableOffset.ToString()+"\r\n"+t.ToString();

            for (int i = 0; i < entryCount; i++) {
                string path = Path.GetDirectoryName(pckFolder+fileTable[i].filePath);
                if ( !Directory.Exists(path) )
                    Directory.CreateDirectory(path);
                BinaryWriter bw = new BinaryWriter(File.Create(pckFolder+fileTable[i].filePath));

                fs.Seek(fileTable[i].fileDataOffset,SeekOrigin.Begin);
                byte[] data = br.ReadBytes(fileTable[i].fileDataCompressedSize);

                if(fileTable[i].fileDataCompressedSize < fileTable[i].fileDataDecompressedSize)
                {
                    MemoryStream ms = new MemoryStream(data,0,data.Length);
                    Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
                    byte[] dataDec = new byte[fileTable[i].fileDataDecompressedSize];
                    s2.Read(dataDec,0,fileTable[i].fileDataDecompressedSize);
                    data = dataDec;
                }

                bw.Write(data);

                bw.Close();

                ShowProgress(entryCount, i+1);
                //UpdateRightText((i+1).ToString() + "/" + entryCount.ToString());
            }

            //UpdateLeftText(entryCount.ToString()+" file(s) extracted");

            label4.Text = entryCount.ToString()+" file(s) extracted";

            br.Close();
            fs.Close();
        }
Example #31
0
        /// <summary>
        /// Open the record, decompressing it as necessary.
        /// </summary>
        /// <returns>A <see cref="Stream"/> for reading the record.</returns>
        public override Stream Open()
        {
            var archive = Archive;
            var reader = archive.DataReader;
            reader.BaseStream.Position = Offset;

            if (fixedUncompressedSize != Size && fixedUncompressedSize != 0) {
                if (Size == fixedUncompressedSize)
                    return new MemoryStream(reader.ReadBytes(checked((int)fixedUncompressedSize)), false);
                else {
                    byte[] data = new byte[fixedUncompressedSize];

                    var deflate = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(reader.BaseStream);
                    var read = deflate.Read(data, 0, data.Length);
                    if (read != fixedUncompressedSize)
                        throw new InvalidDataException();
                    return new MemoryStream(data, false);
                }
            }

            var magic = reader.ReadString(4, Encoding.ASCII);

            if (magic == "DCX\0") {
                reader.Require(0x100);
                reader.RequireBE(0x18);
                reader.RequireBE(0x24);
                reader.RequireBE(0x24);
                int totalHeaderLength = reader.ReadInt32BE();
                reader.RequireMagic("DCS\0");
                int uncompressedSize = reader.ReadInt32BE();
                int compressedSize = reader.ReadInt32BE();
                reader.RequireMagic("DCP\0");

                string compressionMethod = reader.ReadStringz(4, Encoding.ASCII);

                reader.RequireBE(0x20);
                reader.Require(9);
                reader.Require(archive.IsDarkSoulsII ? 0x100 : 0);
                reader.RequireZeroes(8);
                reader.Require(archive.IsDarkSoulsII ? 0x11000 : 0x10100);
                reader.RequireMagic("DCA\0");
                int compressionHeaderLength = reader.ReadInt32BE();

                byte[] data = new byte[uncompressedSize];
                switch (compressionMethod) {
                    case "DFLT":
                        if (true) {
                            var deflate = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(reader.BaseStream);

                            var read = deflate.Read(data, 0, data.Length);
                            if (read != uncompressedSize)
                                throw new Exception();
                            return new MemoryStream(data, false);
                        }

                    case "EDGE":
                        if (true) {
                            reader.RequireMagic("EgdT");
                            reader.RequireBE(0x10100);
                            reader.RequireBE(0x24); // sectionHeaderStart
                            int alignment = reader.ReadInt32BE();
                            if (alignment != 0x10)
                                throw new InvalidDataException();
                            int normalBlockSize = reader.ReadInt32BE();
                            int lastBlockSize = reader.ReadInt32BE();
                            int headerLength = reader.ReadInt32BE();
                            int sectionCount = reader.ReadInt32BE();
                            int sectionEntrySize = reader.ReadInt16BE();
                            if (headerLength != 36 + sectionCount * sectionEntrySize)
                                throw new InvalidDataException();
                            reader.RequireZeroes(2);
                            long blockHeaderStart = reader.BaseStream.Position;
                            long blockDataStart = blockHeaderStart + sectionCount * sectionEntrySize;
                            int offset = 0;

                            for (int index = 0; index < sectionCount; index++) {
                                reader.BaseStream.Position = blockHeaderStart + index * sectionEntrySize;
                                long blockOffset = reader.ReadInt64BE();
                                int blockSize = reader.ReadInt32BE();
                                int blockMode = reader.ReadInt32BE();
                                int uncompressedBlockSize = index < sectionCount - 1 ? normalBlockSize : lastBlockSize;

                                reader.BaseStream.Position = blockDataStart + blockOffset;

                                switch (blockMode) {
                                    case 0:
                                        if (blockSize < uncompressedBlockSize)
                                            throw new InvalidDataException();
                                        reader.Read(data, offset, uncompressedBlockSize);
                                        break;

                                    case 1:
                                        var deflate = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(reader.BaseStream, new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(true));
                                        int deflateRead = deflate.Read(data, offset, uncompressedBlockSize);
                                        if (deflateRead != uncompressedBlockSize)
                                            throw new InvalidDataException();
                                        break;

                                    default:
                                        throw new NotSupportedException("Unsupported block mode " + blockMode);
                                }

                                offset += uncompressedBlockSize;
                            }

                            if (offset != data.Length)
                                throw new InvalidDataException();
                            return new MemoryStream(data, false);
                        }

                    default:
                        throw new NotImplementedException(compressionMethod + " compression is not implemented.");
                }
            } else {
                reader.BaseStream.Seek(-4, SeekOrigin.Current);
                byte[] data = reader.ReadBytes(checked((int)Size));
                return new MemoryStream(data, false);
            }
        }
Example #32
0
        public async Task <IActionResult> ImportDoc(List <IFormFile> file)
        {
            string fileName  = "document.word.pb";
            string ext       = Path.GetExtension(fileName);
            var    unzipPath = Path.Combine("uploads/doc/unzip", fileName);

            List <int> serializedData = new List <int>();

            using (FileStream stream = new FileStream(unzipPath, FileMode.Open, FileAccess.Read))
            {
                stream.Seek(16, SeekOrigin.Begin);

                //using (ZLIBStream zs = new ZLIBStream(stream, CompressionMode.Decompress, true))
                //{
                //    int bytesLeidos = 0;

                //    byte[] buffer = new byte[1024];
                //    while ((bytesLeidos = zs.Read(buffer, 0, buffer.Length)) > 0)
                //    {
                //        for (int i = 0; i < bytesLeidos; i++)
                //        {
                //            serializedData.Add(buffer[i] & 0xFF);
                //        }
                //    }
                //}
                using (ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream ifis = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(stream)) {
                    int bytesLeidos = 0;

                    byte[] buffer = new byte[1024];
                    while ((bytesLeidos = ifis.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        for (int i = 0; i < bytesLeidos; i++)
                        {
                            serializedData.Add(buffer[i] & 0xFF);
                        }
                    }
                }
            }

            return(Ok(new { serializedData }));
        }
        /// <summary>
        /// Decompresses the specified bytes, using the specified compression type and output encoding
        /// </summary>
        /// <param name="bytes">The bytes to decompress.</param>
        /// <param name="offset">The amount of offset to apply to the byte array before beginning decompression.</param>
        /// <param name="length">The length of bytes to decompress in the byte array.</param>
        /// <param name="compressionType">Type of the compression applied to the input string.</param>
        /// <param name="outputEncoding">The output encoding to apply.</param>
        /// <returns>Returns a string representing the uncompressed verison of the input</returns>
        public static string Decompress(byte[] bytes, int offset, int length, CompressionType compressionType, Encoding outputEncoding)
        {
            if (bytes == null || bytes.Length == 0)
                return string.Empty;
            if (offset < 0)
                throw new ArgumentOutOfRangeException("offset", "offset cannot be less than zero");
            if (length > bytes.Length)
                throw new ArgumentOutOfRangeException("length", "length cannot be greater than bytes.length");
            if (length + offset > bytes.Length)
                throw new ArgumentOutOfRangeException("length", "length + offset cannot be greater than bytes.length");

            using (MemoryStream memoryStream = new MemoryStream(bytes)) {
                Stream stream = null;
                switch (compressionType) {
                    case CompressionType.BZip:
                        stream = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(memoryStream);
                        break;
                    case CompressionType.GZip:
                        stream = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(memoryStream);
                        break;

                    // case CompressionType.Tar:
                    //    stream = new ICSharpCode.SharpZipLib.Tar.TarInputStream(memoryStream);
                    //    break;
                    case CompressionType.Zip:
                        stream = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(memoryStream);
                        break;
                }

                if (stream != null) {
                    var decoder = outputEncoding.GetDecoder();
                    StringBuilder builder = new StringBuilder();
                    byte[] buffer = new byte[2048];
                    char[] chars = new char[2048];

                    while (true) {
                        int size = stream.Read(buffer, 0, buffer.Length);
                        if (size == 0)
                            break;
                        int totalChars = decoder.GetChars(buffer, 0, size, chars, 0);
                        builder.Append(chars, 0, totalChars);
                    }

                    stream.Dispose();

                    return builder.ToString();
                }
            }

            return string.Empty;
        }
		/// <summary>
		/// Decompresses a byte array representation of a DataSet.
		/// </summary>
		/// <param name="bytDs">Byte array to be decompressed.</param>
		/// <param name="bytSchema"></param>
		/// <returns>DataSet.</returns>
		public static DataSet DecompressDataSet(byte[] bytDs, byte[] bytSchema)
		{
			MemoryStream ms = new MemoryStream(bytDs);
			Stream s2 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);// Inflater(); // SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
			DataSet ds = new DataSet();
            
			if (bytSchema != null)
			{
				MemoryStream ms10 = new MemoryStream(bytSchema);
				Stream s10 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms10);// Inflater(); // SharpZipLib.Zip.Compression.Streams.InflaterInputStream(ms);
				ds.ReadXmlSchema(s10);
				ds.ReadXml(s2, XmlReadMode.DiffGram);
			}
			else
			{
				ds.ReadXml(s2);
			}
			SetDefaultDataTable(ds);
			return ds;
		}