Exemplo n.º 1
0
        static internal List <RESOURCE> ReadResFile(Stream stream)
        {
            var reader        = new BinaryReader(stream, Encoding.Unicode);
            var resourceNames = new List <RESOURCE>();

            var startPos = stream.Position;

            var initial32Bits = reader.ReadUInt32();

            //RC.EXE output starts with a resource that contains no data.
            if (initial32Bits != 0)
            {
                throw new ResourceException("Stream does not begin with a null resource and is not in .RES format.");
            }

            stream.Position = startPos;

            // Build up Type and Name directories

            while (stream.Position < stream.Length)
            {
                // Get the sizes from the file

                var cbData = reader.ReadUInt32();
                var cbHdr  = reader.ReadUInt32();

                if (cbHdr < 2 * sizeof(DWORD))
                {
                    throw new ResourceException(String.Format("Resource header beginning at offset 0x{0:x} is malformed.", stream.Position - 8));
                    //ErrorPrint(ERR_FILECORRUPT, szFilename);
                }

                // Discard null resource

                if (cbData == 0)
                {
                    stream.Position += cbHdr - 2 * sizeof(DWORD);
                    continue;
                }

                var pAdditional = new RESOURCE()
                {
                    HeaderSize = cbHdr,
                    DataSize   = cbData
                };

                // Read the TYPE and NAME

                pAdditional.pstringType = ReadStringOrID(reader);
                pAdditional.pstringName = ReadStringOrID(reader);

                //round up to dword boundary.
                stream.Position = (stream.Position + 3) & ~3;

                // Read the rest of the header
                pAdditional.DataVersion     = reader.ReadUInt32();
                pAdditional.MemoryFlags     = reader.ReadUInt16();
                pAdditional.LanguageId      = reader.ReadUInt16();
                pAdditional.Version         = reader.ReadUInt32();
                pAdditional.Characteristics = reader.ReadUInt32();

                pAdditional.data = new byte[pAdditional.DataSize];
                reader.Read(pAdditional.data, 0, pAdditional.data.Length);

                stream.Position = (stream.Position + 3) & ~3;

                if (pAdditional.pstringType.theString == null && (pAdditional.pstringType.Ordinal == (WORD)RT_DLGINCLUDE))
                {
                    // Ignore DLGINCLUDE resources
                    continue;
                }

                resourceNames.Add(pAdditional);
            }

            return(resourceNames);
        }
Exemplo n.º 2
0
        static internal List<RESOURCE> ReadResFile(Stream stream)
        {
            var reader = new BinaryReader(stream, Encoding.Unicode);
            var resourceNames = new List<RESOURCE>();

            var startPos = stream.Position;

            var initial32Bits = reader.ReadUInt32();

            //RC.EXE output starts with a resource that contains no data.
            if (initial32Bits != 0)
                throw new ResourceException("Stream does not begin with a null resource and is not in .RES format.");

            stream.Position = startPos;

            // Build up Type and Name directories

            while (stream.Position < stream.Length)
            {
                // Get the sizes from the file

                var cbData = reader.ReadUInt32();
                var cbHdr = reader.ReadUInt32();

                if (cbHdr < 2 * sizeof(DWORD))
                {
                    throw new ResourceException(String.Format("Resource header beginning at offset 0x{0:x} is malformed.", stream.Position - 8));
                    //ErrorPrint(ERR_FILECORRUPT, szFilename);
                }

                // Discard null resource

                if (cbData == 0)
                {
                    stream.Position += cbHdr - 2 * sizeof(DWORD);
                    continue;
                }

                var pAdditional = new RESOURCE()
                {
                    HeaderSize = cbHdr,
                    DataSize = cbData
                };

                // Read the TYPE and NAME

                pAdditional.pstringType = ReadStringOrID(reader);
                pAdditional.pstringName = ReadStringOrID(reader);

                //round up to dword boundary.
                stream.Position = (stream.Position + 3) & ~3;

                // Read the rest of the header
                pAdditional.DataVersion = reader.ReadUInt32();
                pAdditional.MemoryFlags = reader.ReadUInt16();
                pAdditional.LanguageId = reader.ReadUInt16();
                pAdditional.Version = reader.ReadUInt32();
                pAdditional.Characteristics = reader.ReadUInt32();

                pAdditional.data = new byte[pAdditional.DataSize];
                reader.Read(pAdditional.data, 0, pAdditional.data.Length);

                stream.Position = (stream.Position + 3) & ~3;

                if (pAdditional.pstringType.theString == null && (pAdditional.pstringType.Ordinal == (WORD)RT_DLGINCLUDE))
                {
                    // Ignore DLGINCLUDE resources
                    continue;
                }

                resourceNames.Add(pAdditional);
            }

            return resourceNames;
        }