Beispiel #1
0
        private void Given_String(MemoryArea mem, Address addr, string str)
        {
            var w     = new LeImageWriter(mem, addr);
            var bytes = Encoding.ASCII.GetBytes(str);

            w.WriteBytes(bytes);
        }
Beispiel #2
0
        private byte[] CreateMsdosHeader()
        {
            ImageWriter stm = new LeImageWriter(new byte[16]);

            stm.WriteByte(0x4D);    // MZ
            stm.WriteByte(0x5A);
            stm.WriteBytes(0xCC, 4);
            stm.WriteLeUInt16(0x0090);
            stm.WriteBytes(0xCC, 0x12);
            stm.WriteByte(0x00);
            stm.WriteByte(0x00);
            stm.WriteByte(0x05);
            stm.WriteByte(0x21);
            stm.WriteString("PKLITE", Encoding.ASCII);
            stm.WriteBytes(0xCC, 0x0C);
            return(stm.Bytes);
        }
Beispiel #3
0
        private void Given_MzExeProgram(uint size)
        {
            this.writer = new LeImageWriter();
            writer.WriteBytes(0, size);
            var pos = writer.Position;

            writer.Position = 0;
            writer.WriteString("MZ", Encoding.ASCII);
            uint cPages     = size / ExeImageLoader.CbPageSize;
            uint cbLastPage = size % ExeImageLoader.CbPageSize;

            if (cbLastPage > 0)
            {
                ++cPages;
            }
            writer.WriteLeUInt16((ushort)cbLastPage);
            writer.WriteLeUInt16((ushort)cPages);
            writer.Position = pos;
        }
Beispiel #4
0
        // PE section headers are always 40 bytes.
        private void Given_Section(string section, uint virtAddress, uint rvaData)
        {
            var bytes = Encoding.UTF8.GetBytes(section);

            writer.WriteBytes(bytes).WriteBytes(0, 8 - (uint)bytes.Length);
            writer.WriteLeUInt32(0x1000);      // Section size in memory
            writer.WriteLeUInt32(virtAddress); // Where to load this
            writer.WriteLeUInt32(0x1000);      // raw data
            writer.WriteLeUInt32(rvaData);     // rva to raw data
            writer.WriteLeInt32(0);            // relocs
            writer.WriteLeInt32(0);            // line numbers
            writer.WriteLeInt16(0);            // #relocs
            writer.WriteLeInt16(0);            // #line numbers
            writer.WriteLeInt32(0);            // characteristics

            // Increment the section count in the optional header.
            short sections = MemoryArea.ReadLeInt16(fileImage, RvaPeHdr + 6);

            MemoryArea.WriteLeInt16(fileImage, RvaPeHdr + 6, (short)(sections + 1));
        }
Beispiel #5
0
 private Action B(params byte [] b)
 {
     return(() => { writer.WriteBytes(b); });
 }
Beispiel #6
0
        /// <summary>
        /// Unpacks the packed raw image into <paramref name="image" />.
        /// </summary>
        private void UnpackImage(FileHeader fileHeader, ByteMemoryArea image)
        {
            var w = new LeImageWriter(image.Bytes);
            //
            // Still looking for additional information on Pharlap file packing
            //
            // Packing implemented is currently based on reviewing code and interpretation of data against loading program in debugger.
            // Record is 16 bit word.
            // If bit 15 is clear ie 0-7FFF, load the next record number of bytes into memory
            //
            // If bit 15 is set, ie 8000-FFFF use value lower 15 bits for size of repeat area
            // Next byte (dataSize) defines size of item to be repeated
            // If dataSize size is larger than size of repeat area, corrupt file
            // if dataSize is 0, then is either fill with zero or skip, size of repeat area
            // Read itemSize number of bytes for the repeatData
            // copying repeatData until filled size of repeat area
            //

            var rdr = new LeImageReader(RawImage, FileHeaderOffset + fileHeader.offset_load_image);

            while (w.Position < fileHeader.memory_requirements)
            {
                if (!rdr.TryReadUInt16(out ushort us))
                {
                    throw new BadImageFormatException("Unexpected EOF while loading program.");
                }
                if ((us & 0x8000) == 0)
                {
                    rdr.ReadBytes(w.Bytes, w.Position, us);
                    w.Position += us;
                }
                else
                {
                    us &= 0x7FFF;
                    if (!rdr.TryReadByte(out var dataSize))
                    {
                        throw new BadImageFormatException("Unexpected EOF while loading program.");
                    }
                    if (dataSize > us)
                    {
                        throw new BadImageFormatException("Corrupt file");  // Corrupt file, Repeated data shouldn't be bigger than size of repeat block
                    }
                    if (dataSize == 0)
                    {
                        for (int i = 0; i < us; ++i)
                        {
                            w.WriteByte(dataSize);
                        }
                    }
                    else
                    {
                        var repeatData = new byte[dataSize];
                        for (int i = 0; i < dataSize; ++i)
                        {
                            if (!rdr.TryReadByte(out var b))
                            {
                                throw new BadImageFormatException("Unexpected EOF while loading program.");
                            }
                            repeatData[i] = b;
                        }
                        for (int i = 0; i < us; i += dataSize)
                        {
                            w.WriteBytes(repeatData);
                        }
                    }
                }
            }
        }