public void LoadFromStream(Stream stream)
        {
            int lengthToFill = (int)(stream.Length - stream.Position);

            while (m_buffer.Data.Length < lengthToFill)
               m_buffer.Grow();

            m_buffer.Position = 0;
            m_length = lengthToFill;
            stream.ReadAll(m_buffer.Data, 0, lengthToFill);

            Reset();
        }
 private void Read(Stream stream)
 {
     byte[] bytes = new byte[9];
     stream.ReadAll(bytes, 0, bytes.Length);
     Left = BitConverter.ToUInt16(bytes, 0);
     Top = BitConverter.ToUInt16(bytes, 2);
     Width = BitConverter.ToUInt16(bytes, 4);
     Height = BitConverter.ToUInt16(bytes, 6);
     byte packedFields = bytes[8];
     HasLocalColorTable = (packedFields & 0x80) != 0;
     Interlace = (packedFields & 0x40) != 0;
     IsLocalColorTableSorted = (packedFields & 0x20) != 0;
     LocalColorTableSize = 1 << ((packedFields & 0x07) + 1);
 }
Example #3
0
 public static GifColor[] ReadColorTable(Stream stream, int size)
 {
     int length = 3 * size;
     byte[] bytes = new byte[length];
     stream.ReadAll(bytes, 0, length);
     GifColor[] colorTable = new GifColor[size];
     for (int i = 0; i < size; i++)
     {
         byte r = bytes[3 * i];
         byte g = bytes[3 * i + 1];
         byte b = bytes[3 * i + 2];
         colorTable[i] = new GifColor(r, g, b);
     }
     return colorTable;
 }
Example #4
0
 public static GifColor[] ReadColorTable(Stream stream, int size)
 {
     var length = 3*size;
     var bytes = new byte[length];
     stream.ReadAll(bytes, 0, length);
     var colorTable = new GifColor[size];
     for (var i = 0; i < size; i++)
     {
         var r = bytes[3*i];
         var g = bytes[3*i + 1];
         var b = bytes[3*i + 2];
         colorTable[i] = new GifColor(r, g, b);
     }
     return colorTable;
 }
        private void Read(Stream stream)
        {
            // Note: at this point, the label (0xFF) has already been read

            byte[] bytes = new byte[12];
            stream.ReadAll(bytes, 0, bytes.Length);
            BlockSize = bytes[0]; // should always be 11
            if (BlockSize != 11)
                throw GifHelpers.InvalidBlockSizeException("Application Extension", 11, BlockSize);

            ApplicationIdentifier = Encoding.ASCII.GetString(bytes, 1, 8);
            byte[] authCode = new byte[3];
            Array.Copy(bytes, 9, authCode, 0, 3);
            AuthenticationCode = authCode;
            Data = GifHelpers.ReadDataBlocks(stream, false);
        }
        private void Read(Stream stream)
        {
            // Note: at this point, the label (0xF9) has already been read

            byte[] bytes = new byte[6];
            stream.ReadAll(bytes, 0, bytes.Length);
            BlockSize = bytes[0]; // should always be 4
            if (BlockSize != 4)
                throw GifHelpers.InvalidBlockSizeException("Graphic Control Extension", 4, BlockSize);
            byte packedFields = bytes[1];
            DisposalMethod = (packedFields & 0x1C) >> 2;
            UserInput = (packedFields & 0x02) != 0;
            HasTransparency = (packedFields & 0x01) != 0;
            Delay = BitConverter.ToUInt16(bytes, 2) * 10; // milliseconds
            TransparencyIndex = bytes[4];
        }
Example #7
0
        public virtual void Load(Stream FileStream)
        {
            FileStream = new MemoryStream(FileStream.ReadAll());

            this.FileStream = FileStream;

            this.Header = FileStream.ReadStruct<Elf.HeaderStruct>();
            if (this.Header.Magic != Elf.HeaderStruct.MagicEnum.ExpectedValue)
            {
                throw(new InvalidProgramException("Not an ELF File"));
            }

            if (this.Header.Machine != Elf.HeaderStruct.MachineEnum.ALLEGREX)
            {
                throw (new InvalidProgramException("Invalid Elf.Header.Machine"));
            }

            this.ProgramHeaders = FileStream.ReadStructVectorAt<Elf.ProgramHeader>(Header.ProgramHeaderOffset, Header.ProgramHeaderCount, Header.ProgramHeaderEntrySize);
            this.SectionHeaders = FileStream.ReadStructVectorAt<Elf.SectionHeader>(Header.SectionHeaderOffset, Header.SectionHeaderCount, Header.SectionHeaderEntrySize);

            this.NamesSectionHeader = this.SectionHeaders[Header.SectionHeaderStringTable];
            this.StringTable = FileStream.SliceWithLength(this.NamesSectionHeader.Offset, this.NamesSectionHeader.Size).ReadAll();

            this.SectionHeadersByName = new Dictionary<string, Elf.SectionHeader>();
            foreach (var SectionHeader in this.SectionHeaders)
            {
                var SectionHeaderName = GetStringFromStringTable(SectionHeader.Name);
                this.SectionHeadersByName[SectionHeaderName] = SectionHeader;
            }

            Console.WriteLine("ProgramHeaders:{0}", this.ProgramHeaders.Length);
            foreach (var ProgramHeader in ProgramHeaders)
            {
                Console.WriteLine("{0}", ProgramHeader.ToStringDefault());
            }

            Console.WriteLine("SectionHeaders:{0}", this.SectionHeaders.Length);
            foreach (var SectionHeader in SectionHeaders)
            {
                Console.WriteLine("{0}:{1}", GetStringFromStringTable(SectionHeader.Name), SectionHeader.ToStringDefault());
            }

            if (NeedsRelocation && this.ProgramHeaders.Length > 1)
            {
                //throw (new NotImplementedException("Not implemented several ProgramHeaders yet using relocation"));
            }
        }
Example #8
0
 public static byte[] ReadDataBlocks(Stream stream, bool discard)
 {
     MemoryStream ms = discard ? null : new MemoryStream();
     using (ms)
     {
         int len;
         while ((len = stream.ReadByte()) > 0)
         {
             byte[] bytes = new byte[len];
             stream.ReadAll(bytes, 0, len);
             if (ms != null)
                 ms.Write(bytes, 0, len);
         }
         if (ms != null)
             return ms.ToArray();
         return null;
     }
 }
        private void Read(Stream stream)
        {
            byte[] bytes = new byte[7];
            stream.ReadAll(bytes, 0, bytes.Length);

            Width = BitConverter.ToUInt16(bytes, 0);
            Height = BitConverter.ToUInt16(bytes, 2);
            byte packedFields = bytes[4];
            HasGlobalColorTable = (packedFields & 0x80) != 0;
            ColorResolution = ((packedFields & 0x70) >> 4) + 1;
            IsGlobalColorTableSorted = (packedFields & 0x08) != 0;
            GlobalColorTableSize = 1 << ((packedFields & 0x07) + 1);
            BackgroundColorIndex = bytes[5];
            PixelAspectRatio =
                bytes[5] == 0
                    ? 0.0
                    : (15 + bytes[5]) / 64.0;
        }
        private void Read(Stream stream, IEnumerable<GifExtension> controlExtensions, bool metadataOnly)
        {
            // Note: at this point, the label (0x01) has already been read

            byte[] bytes = new byte[13];
            stream.ReadAll(bytes,0, bytes.Length);

            BlockSize = bytes[0];
            if (BlockSize != 12)
                throw GifHelpers.InvalidBlockSizeException("Plain Text Extension", 12, BlockSize);

            Left = BitConverter.ToUInt16(bytes, 1);
            Top = BitConverter.ToUInt16(bytes, 3);
            Width = BitConverter.ToUInt16(bytes, 5);
            Height = BitConverter.ToUInt16(bytes, 7);
            CellWidth = bytes[9];
            CellHeight = bytes[10];
            ForegroundColorIndex = bytes[11];
            BackgroundColorIndex = bytes[12];

            var dataBytes = GifHelpers.ReadDataBlocks(stream, metadataOnly);
            Text = Encoding.ASCII.GetString(dataBytes);
            Extensions = controlExtensions.ToList().AsReadOnly();
        }
Example #11
0
        public HleModuleGuest LoadModule(Stream FileStream, Stream MemoryStream, MemoryPartition MemoryPartition, HleModuleManager ModuleManager, String GameTitle, string ModuleName, bool IsMainModule)
        {
            this.HleModuleGuest = new HleModuleGuest(PspEmulatorContext);

            this.ElfLoader = new ElfLoader();
            this.ModuleManager = ModuleManager;

            var Magic = FileStream.SliceWithLength(0, 4).ReadString(4);
            Logger.Info("Magic: '{0}'", Magic);
            if (Magic == "~PSP")
            {
                try
                {
                    var DecryptedData = new EncryptedPrx().Decrypt(FileStream.ReadAll(), true);
                    File.WriteAllBytes("last_decoded_prx.bin", DecryptedData);
                    FileStream = new MemoryStream(DecryptedData);
                }
                catch (Exception Exception)
                {
                    Logger.Error(Exception);
                    throw (Exception);
                }
            }

            this.ElfLoader.Load(FileStream, ModuleName);

            PspEmulatorContext.PspConfig.InfoExeHasRelocation = this.ElfLoader.NeedsRelocation;

            if (this.ElfLoader.NeedsRelocation)
            {
                var DummyPartition = MemoryPartition.Allocate(
                    0x4000,
                    Name: "Dummy"
                );
                BaseAddress = MemoryPartition.ChildPartitions.OrderByDescending(Partition => Partition.Size).First().Low;
                Logger.Info("BASE ADDRESS (Try    ): 0x{0:X}", BaseAddress);
                BaseAddress = MathUtils.NextAligned(BaseAddress, 0x1000);
                Logger.Info("BASE ADDRESS (Aligned): 0x{0:X}", BaseAddress);
            }
            else
            {
                BaseAddress = 0;
            }

            PspEmulatorContext.PspConfig.RelocatedBaseAddress = BaseAddress;
            PspEmulatorContext.PspConfig.GameTitle = GameTitle;

            this.ElfLoader.AllocateAndWrite(MemoryStream, MemoryPartition, BaseAddress);

            if (this.ElfLoader.NeedsRelocation)
            {
                RelocateFromHeaders();
            }

            if (!ElfLoader.SectionHeadersByName.ContainsKey(".rodata.sceModuleInfo"))
            {
                throw(new Exception("Can't find segment '.rodata.sceModuleInfo'"));
            }

            HleModuleGuest.ModuleInfo = ElfLoader.SectionHeaderFileStream(ElfLoader.SectionHeadersByName[".rodata.sceModuleInfo"]).ReadStruct<ElfPsp.ModuleInfo>(); ;

            //Console.WriteLine(this.ModuleInfo.ToStringDefault());

            HleModuleGuest.InitInfo = new InitInfoStruct()
            {
                PC = ElfLoader.Header.EntryPoint + BaseAddress,
                GP = HleModuleGuest.ModuleInfo.GP + BaseAddress,
            };

            UpdateModuleImports();
            UpdateModuleExports();

            ModuleManager.LoadedGuestModules.Add(HleModuleGuest);

            return HleModuleGuest;
        }
Example #12
0
 public static string ReadString(Stream stream, int length)
 {
     byte[] bytes = new byte[length];
     stream.ReadAll(bytes, 0, length);
     return Encoding.ASCII.GetString(bytes);
 }
Example #13
0
        /// <summary>
        /// Reads the content.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns></returns>
        public byte[] ReadContent(Stream stream)
        {
            int? contentLength = null;
            IList<string> literalContentLength;
            if (Headers.TryGetValue("Content-Length", out literalContentLength))
            {
                int fContentLength;
                if (int.TryParse(literalContentLength.First(), out fContentLength))
                    contentLength = fContentLength;
            }

            if (!contentLength.HasValue)
            {
                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    return memoryStream.ToArray();
                }
            }

            var contentBytes = new byte[contentLength.Value];
            stream.ReadAll(contentBytes, 0, contentBytes.Length);
            return contentBytes;
        }
Example #14
0
 /// <summary>
 /// Builds a FormData from the specified input stream, 
 /// which is assumed to be in x-www-form-urlencoded format.
 /// </summary>
 /// <param name="stream">the input stream</param>
 /// <returns>a populated FormData object</returns>
 public static Task<FormData> ParseUrlEncoded(Stream stream) {
     var form = new FormData();
     string input = stream.ReadAll();
     var pairs = input.Split(UrlSplitTokens, StringSplitOptions.RemoveEmptyEntries);
     foreach (var pair in pairs) {
         var nameValue = pair.Split('=');
         form[nameValue[0]] = UrlHelper.Decode(nameValue[1]);
     }
     return TaskHelper.Completed(form);
 }