Example #1
0
 public override ImageMetaData ReadMetaData(Stream stream)
 {
     var header = new byte[0x10];
     if (0x10 != stream.Read (header, 0, 0x10))
         return null;
     int unpacked_size = LittleEndian.ToInt32 (header, 8);
     using (var alb = new AlbStream (stream, unpacked_size))
     using (var file = new SeekableStream (alb))
     {
         uint signature = FormatCatalog.ReadSignature (file);
         file.Position = 0;
         ImageFormat format;
         if (ImageFormat.Png.Signature == signature)
             format = ImageFormat.Png;
         else if (Dds.Value.Signature == signature)
             format = Dds.Value;
         else
             format = ImageFormat.Jpeg;
         var info = format.ReadMetaData (file);
         if (null == info)
             return null;
         return new AlbMetaData
         {
             Width   = info.Width,
             Height  = info.Height,
             OffsetX = info.OffsetX,
             OffsetY = info.OffsetY,
             BPP     = info.BPP,
             Format  = format,
             Info    = info,
             UnpackedSize = unpacked_size,
         };
     }
 }
Example #2
0
        internal IBinaryStream OpenFg(IBinaryStream fg)
        {
            var fge_name = Path.ChangeExtension(fg.Name, ".fge");

            using (var fge = VFS.OpenView(fge_name))
            {
                if (fge.MaxOffset != 0x818)
                {
                    throw new InvalidFormatException();
                }
                int  chunk1_size   = fge.View.ReadInt32(0);
                int  chunk2_offset = fge.View.ReadInt32(0x404);
                int  chunk2_size   = fge.View.ReadInt32(0x408);
                bool is_compressed = fge.View.ReadInt32(0x810) != 0;
                int  part1_size    = chunk2_offset + chunk2_size;
                var  part1         = new byte[part1_size];
                fge.View.Read(4, part1, 0, (uint)chunk1_size);
                fg.Position = 5;
                fg.Read(part1, chunk1_size, chunk2_offset - chunk1_size);
                fge.View.Read(0x40C, part1, chunk2_offset, (uint)chunk2_size);
                var    part2     = new StreamRegion(fg.AsStream, fg.Position, true);
                Stream fg_stream = new PrefixStream(part1, part2);
                if (is_compressed)
                {
                    fg_stream = new ZLibStream(fg_stream, CompressionMode.Decompress);
                    fg_stream = new SeekableStream(fg_stream);
                }
                return(new BinaryStream(fg_stream, fg.Name));
            }
        }
Example #3
0
        }                                                                 // 'MGOB'

        public override ImageMetaData ReadMetaData(IBinaryStream file)
        {
            var header = file.ReadHeader(12);

            if (header.ToInt32(4) != 5)
            {
                return(null);
            }
            using (var lzss = new PackedStream <LzssDecompressor> (file.AsStream, true))
                using (var stream = new SeekableStream(lzss))
                    using (var mgo = new BinaryStream(stream, file.Name))
                    {
                        var name = mgo.ReadCString();
                        mgo.Position = ((int)(mgo.Position - 1) & ~3) + 4;
                        int count = mgo.ReadInt32();
                        mgo.Seek(count * 0x10, SeekOrigin.Current);
                        int bmp_position = (int)mgo.Position;
                        var bmp_header   = mgo.ReadBytes(0x36);
                        if (!bmp_header.AsciiEqual("BM"))
                        {
                            return(null);
                        }
                        return(new MgoMetaData {
                            Width = bmp_header.ToUInt32(0x12),
                            Height = bmp_header.ToUInt32(0x16),
                            BPP = bmp_header.ToInt32(0x1C),
                            UnpackedSize = header.ToInt32(8),
                            BmpOffset = bmp_position,
                        });
                    }
        }
Example #4
0
 public override ImageData Read(Stream stream, ImageMetaData info)
 {
     stream.Position = 4;
     using (var lzss = new LzssStream(stream, LzssMode.Decompress, true))
         using (var input = new SeekableStream(lzss))
             return(base.Read(input, info));
 }
Example #5
0
 public override ImageData Read(Stream stream, ImageMetaData info)
 {
     var meta = (AlbMetaData)info;
     stream.Position = 0x10;
     using (var alb = new AlbStream (stream, meta.UnpackedSize))
     using (var file = new SeekableStream (alb))
         return meta.Format.Read (file, meta.Info);
 }
Example #6
0
 public override ImageData Read(IBinaryStream file, ImageMetaData info)
 {
     file.Seek(8, SeekOrigin.Current);
     using (var zstream = new ZLibStream(file.AsStream, CompressionMode.Decompress, true))
         using (var input = new SeekableStream(zstream))
             using (var bmp = new BinaryStream(input, file.Name))
                 return(base.Read(bmp, info));
 }
 public static async Task SeekableStream_ReadOptional_UnknownLengthAsync()
 {
     using (Stream source = CreateMemoryStream((int)(WindowSize * 3) / 2))
         using (Stream sut = await SeekableStream.OptionalCreateAsync(new StreamWrapper(source), WindowSize))
         {
             await ValidateCopyAsync(sut);
         }
 }
 public static async Task SeekableStream_Read_KnownLengthAsync()
 {
     using (Stream source = CreateMemoryStream((int)(WindowSize * 3) / 2))
         using (SeekableStream sut = await SeekableStream.CreateAsync(source, WindowSize))
         {
             await ValidateCopyAsync(sut);
         }
 }
Example #9
0
        public override ImageData Read(Stream stream, ImageMetaData info)
        {
            var meta = (AlbMetaData)info;

            stream.Position = 0x10;
            using (var alb = new AlbStream(stream, meta.UnpackedSize))
                using (var file = new SeekableStream(alb))
                    return(meta.Format.Read(file, meta.Info));
        }
        public void Skip_SeekableStream_SeekIsCalled()
        {
            using (var seekableStream = new SeekableStream(4))
            {
                seekableStream.Skip(4);

                Assert.Equal(4, seekableStream.Offset);
                Assert.Equal(SeekOrigin.Current, seekableStream.Loc);
            }
        }
Example #11
0
 public override ImageData Read(IBinaryStream file, ImageMetaData info)
 {
     using (var lz = OpenLzStream(file))
     {
         var skip = new byte[4];
         lz.Read(skip, 0, 4);
         using (var input = new SeekableStream(lz))
             using (var dds = new BinaryStream(input, file.Name))
                 return(DdsFormat.Value.Read(dds, info));
     }
 }
Example #12
0
        internal IBinaryStream OpenBitmapStream(IBinaryStream input, bool seekable = false)
        {
            input.Position = 4;
            Stream bmp = new LzssStream(input.AsStream, LzssMode.Decompress, true);

            if (seekable)
            {
                bmp = new SeekableStream(bmp);
            }
            return(new BinaryStream(bmp, input.Name));
        }
Example #13
0
        internal IBinaryStream OpenEncrypted(IBinaryStream stream, bool seekable = false)
        {
            Stream input = new ProxyStream(stream.AsStream, true);

            input = new InputCryptoStream(input, new TigTransform());
            if (seekable)
            {
                input = new SeekableStream(input);
            }
            return(new BinaryStream(input, stream.Name));
        }
Example #14
0
        public override ImageMetaData ReadMetaData(Stream stream)
        {
            uint unpacked_size = FormatCatalog.ReadSignature(stream);

            if (unpacked_size <= 0x20 || unpacked_size > 0x5000000) // ~83MB
            {
                return(null);
            }
            using (var lzss = new LzssStream(stream, LzssMode.Decompress, true))
                using (var input = new SeekableStream(lzss))
                    return(base.ReadMetaData(input));
        }
Example #15
0
        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            uint unpacked_size = stream.Signature;

            if (unpacked_size <= 0x20 || unpacked_size > 0x5000000) // ~83MB
            {
                return(null);
            }
            stream.Position = 4;
            using (var lzss = new LzssStream(stream.AsStream, LzssMode.Decompress, true))
                using (var input = new SeekableStream(lzss))
                    using (var xtx = new BinaryStream(input, stream.Name))
                        return(base.ReadMetaData(xtx));
        }
Example #16
0
        public override SoundInput TryOpen(IBinaryStream file)
        {
            uint   key   = file.Signature ^ 0x46464952u;
            Stream input = new InputCryptoStream(file.AsStream, new Ags32Transform(key));

            input = new SeekableStream(input);
            var header = new byte[12];

            input.Read(header, 0, 12);
            if (!header.AsciiEqual(8, "WAVE"))
            {
                return(null);
            }
            input.Position = 0;
            return(new WaveInput(input));
        }
Example #17
0
        public override ImageMetaData ReadMetaData(Stream stream)
        {
            var header = new byte[0x10];

            if (0x10 != stream.Read(header, 0, 0x10))
            {
                return(null);
            }
            int unpacked_size = LittleEndian.ToInt32(header, 8);

            using (var alb = new AlbStream(stream, unpacked_size))
                using (var file = new SeekableStream(alb))
                {
                    uint signature = FormatCatalog.ReadSignature(file);
                    file.Position = 0;
                    ImageFormat format;
                    if (ImageFormat.Png.Signature == signature)
                    {
                        format = ImageFormat.Png;
                    }
                    else if (Dds.Value.Signature == signature)
                    {
                        format = Dds.Value;
                    }
                    else
                    {
                        format = ImageFormat.Jpeg;
                    }
                    var info = format.ReadMetaData(file);
                    if (null == info)
                    {
                        return(null);
                    }
                    return(new AlbMetaData
                    {
                        Width = info.Width,
                        Height = info.Height,
                        OffsetX = info.OffsetX,
                        OffsetY = info.OffsetY,
                        BPP = info.BPP,
                        Format = format,
                        Info = info,
                        UnpackedSize = unpacked_size,
                    });
                }
        }
Example #18
0
        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            var header        = stream.ReadHeader(0x10);
            int unpacked_size = header.ToInt32(8);

            using (var alb = new AlbStream(stream, unpacked_size))
                using (var s = new SeekableStream(alb))
                    using (var file = new BinaryStream(s, stream.Name))
                    {
                        uint        signature = file.Signature;
                        ImageFormat format;
                        if (Png.Signature == signature)
                        {
                            format = ImageFormat.Png;
                        }
                        else if (Dds.Value.Signature == signature)
                        {
                            format = Dds.Value;
                        }
                        else
                        {
                            format = ImageFormat.Jpeg;
                        }
                        file.Position = 0;
                        var info = format.ReadMetaData(file);
                        if (null == info)
                        {
                            return(null);
                        }
                        return(new AlbMetaData
                        {
                            Width = info.Width,
                            Height = info.Height,
                            OffsetX = info.OffsetX,
                            OffsetY = info.OffsetY,
                            BPP = info.BPP,
                            Format = format,
                            Info = info,
                            UnpackedSize = unpacked_size,
                        });
                    }
        }
Example #19
0
        public override ArcFile TryOpen(ArcView file)
        {
            if (!file.Name.EndsWith(".spc", StringComparison.InvariantCultureIgnoreCase))
            {
                return(null);
            }
            uint unpacked_size = file.View.ReadUInt32(0);

            if (unpacked_size <= 0x20 || unpacked_size > 0x5000000)
            {
                return(null);
            }

            var backend = file.CreateStream();

            backend.Position = 4;
            var lzss  = new LzssStream(backend);
            var input = new SeekableStream(lzss);

            try
            {
                var base_name = Path.GetFileNameWithoutExtension(file.Name);
                using (var spc = new XtxIndexBuilder(input, base_name))
                {
                    spc.ReadIndex(0);
                    if (spc.Dir.Count > 0)
                    {
                        return(new SpcArchive(file, this, spc.Dir, input));
                    }
                    else
                    {
                        throw new InvalidFormatException();
                    }
                }
            }
            catch
            {
                input.Dispose();
                throw;
            }
        }
Example #20
0
        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            var    header     = stream.ReadHeader(8);
            bool   compressed = 'Z' == header[3];
            uint   key        = header.ToUInt32(4) ^ EencKey;
            Stream input      = new StreamRegion(stream.AsStream, 8, true);

            try
            {
                input = new EencStream(input, key);
                if (compressed)
                {
                    input = new ZLibStream(input, CompressionMode.Decompress);
                    input = new SeekableStream(input);
                }
                using (var bin = new BinaryStream(input, stream.Name, true))
                {
                    var format = FindFormat(bin);
                    if (null == format)
                    {
                        return(null);
                    }
                    return(new EencMetaData
                    {
                        Width = format.Item2.Width,
                        Height = format.Item2.Height,
                        BPP = format.Item2.BPP,
                        Key = key,
                        Info = format.Item2,
                        Format = format.Item1,
                        Compressed = compressed,
                    });
                }
            }
            finally
            {
                input.Dispose();
            }
        }
Example #21
0
 public override ImageMetaData ReadMetaData(Stream stream)
 {
     var header = new byte[8];
     if (8 != stream.Read (header, 0, 8))
         return null;
     bool compressed = 'Z' == header[3];
     uint key = LittleEndian.ToUInt32 (header, 4) ^ EencKey;
     Stream input = new StreamRegion (stream, 8, true);
     try
     {
         input = new EencStream (input, key);
         if (compressed)
         {
             input = new ZLibStream (input, CompressionMode.Decompress);
             input = new SeekableStream (input);
         }
         var format = FindFormat (input);
         if (null == format)
             return null;
         return new EencMetaData
         {
             Width = format.Item2.Width,
             Height = format.Item2.Height,
             BPP = format.Item2.BPP,
             Key = key,
             Info = format.Item2,
             Format = format.Item1,
             Compressed = compressed,
         };
     }
     finally
     {
         input.Dispose();
     }
 }
        // The inputStream will be disposed if this returns <c>true</c> but will not otherwise
        private bool ProcessShortcodes(Stream inputStream, IDocument input, IExecutionContext context, out IDocument result)
        {
            // Parse the input stream looking for shortcodes
            ShortcodeParser parser = new ShortcodeParser(_startDelimiter, _endDelimiter, context.Shortcodes);

            if (!inputStream.CanSeek)
            {
                inputStream = new SeekableStream(inputStream, true);
            }
            List <ShortcodeLocation> locations = parser.Parse(inputStream);

            // Reset the position because we're going to use the stream again when we do replacements
            inputStream.Position = 0;

            // Return the original document if we didn't find any
            if (locations.Count == 0)
            {
                result = null;
                return(false);
            }

            // Otherwise, create a shortcode instance for each named shortcode
            Dictionary <string, IShortcode> shortcodes =
                locations
                .Select(x => x.Name)
                .Distinct(StringComparer.OrdinalIgnoreCase)
                .ToDictionary(x => x, x => context.Shortcodes.CreateInstance(x), StringComparer.OrdinalIgnoreCase);

            // Execute each of the shortcodes in order
            List <InsertingStreamLocation> insertingLocations = locations
                                                                .Select(x =>
            {
                // Execute the shortcode
                IShortcodeResult shortcodeResult = shortcodes[x.Name].Execute(x.Arguments, x.Content, input, context);

                // Merge output metadata with the current input document
                // Creating a new document is the easiest way to ensure all the metadata from shortcodes gets accumulated correctly
                if (shortcodeResult?.Metadata != null)
                {
                    input = context.GetDocument(input, shortcodeResult.Metadata);
                }

                // Recursively parse shortcodes
                Stream shortcodeResultStream = shortcodeResult?.Stream;
                if (shortcodeResultStream != null)
                {
                    // Don't process nested shortcodes if it's the raw shortcode
                    if (!x.Name.Equals(nameof(Core.Shortcodes.Contents.Raw), StringComparison.OrdinalIgnoreCase))
                    {
                        if (!shortcodeResultStream.CanSeek)
                        {
                            shortcodeResultStream = new SeekableStream(shortcodeResultStream, true);
                        }
                        if (ProcessShortcodes(shortcodeResultStream, input, context, out IDocument nestedResult))
                        {
                            input = nestedResult;
                            shortcodeResultStream = nestedResult.GetStream();      // Will get disposed in the replacement operation below
                        }
                        else
                        {
                            shortcodeResultStream.Position = 0;
                        }
                    }
                    return(new InsertingStreamLocation(x.FirstIndex, x.LastIndex, shortcodeResultStream));
                }

                return(new InsertingStreamLocation(x.FirstIndex, x.LastIndex, null));
            })
                                                                .ToList();

            // Dispose any shortcodes that implement IDisposable
            foreach (IDisposable disposableShortcode
                     in shortcodes.Values.Select(x => x as IDisposable).Where(x => x != null))
            {
                disposableShortcode.Dispose();
            }

            // Construct a new stream with the shortcode results inserted
            // We have to use all TextWriter/TextReaders over the streams to ensure consistent encoding
            Stream resultStream = context.GetContentStream();

            char[] buffer = new char[4096];
            using (TextWriter writer = new StreamWriter(resultStream, Encoding.UTF8, 4096, true))
            {
                // The input stream will get disposed when the reader is
                using (TextReader reader = new StreamReader(inputStream))
                {
                    int position = 0;
                    int length   = 0;
                    foreach (InsertingStreamLocation insertingLocation in insertingLocations)
                    {
                        // Copy up to the start of this shortcode
                        length = insertingLocation.FirstIndex - position;
                        Read(reader, writer, length, ref buffer);
                        position += length;

                        // Copy the shortcode content to the result stream
                        if (insertingLocation.Stream != null)
                        {
                            // This will dispose the shortcode content stream when done
                            using (TextReader insertingReader = new StreamReader(insertingLocation.Stream))
                            {
                                Read(insertingReader, writer, null, ref buffer);
                            }
                        }

                        // Skip the shortcode text
                        length = insertingLocation.LastIndex - insertingLocation.FirstIndex + 1;
                        Read(reader, null, length, ref buffer);
                        position += length;
                    }

                    // Copy remaining
                    Read(reader, writer, null, ref buffer);
                }
            }
            result = context.GetDocument(input, resultStream);
            return(true);
        }
Example #23
0
        /// <summary>
        /// Opens a TurtlePackage from the specified uri with the specified credentials
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="verificationMode">The verification mode.</param>
        /// <param name="credentials">The credentials.</param>
        /// <returns></returns>
        public static TPack OpenFrom(Uri uri, VerificationMode verificationMode, ICredentials credentials)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            WebRequest request = WebRequest.Create(uri);

            HttpWebRequest httpRequest = request as HttpWebRequest;

            if (httpRequest != null)
            {
                httpRequest.AllowAutoRedirect = true;
                httpRequest.CachePolicy       = new System.Net.Cache.RequestCachePolicy(RequestCacheLevel.Revalidate);
                if (credentials != null)
                {
                    httpRequest.Credentials = credentials;
                }

                httpRequest.Pipelined = true;
                httpRequest.Method    = "GET";
            }
            FtpWebRequest ftpRequest = request as FtpWebRequest;

            if (ftpRequest != null)
            {
                ftpRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(RequestCacheLevel.Revalidate);
                ftpRequest.EnableSsl   = true;
                if (credentials != null)
                {
                    ftpRequest.Credentials = credentials;
                }
            }

            WebResponse rsp = request.GetResponse();

            try
            {
                Stream sr = rsp.GetResponseStream();
                try
                {
                    if (!sr.CanSeek)
                    {
                        sr = new SeekableStream(sr, rsp.ContentLength);
                    }

                    return(Open(sr, verificationMode));
                }
                catch (Exception)
                {
                    sr.Close();
                    throw;
                }
            }
            catch (Exception)
            {
                rsp.Close();
                throw;
            }
        }