| 1 |
// |
| 2 |
// ResourceReader.cs |
| 3 |
// |
| 4 |
// Author: |
| 5 |
// Jb Evain ([email protected]) |
| 6 |
// |
| 7 |
// (C) 2006 Jb Evain |
| 8 |
// |
| 9 |
// Permission is hereby granted, free of charge, to any person obtaining |
| 10 |
// a copy of this software and associated documentation files (the |
| 11 |
// "Software"), to deal in the Software without restriction, including |
| 12 |
// without limitation the rights to use, copy, modify, merge, publish, |
| 13 |
// distribute, sublicense, and/or sell copies of the Software, and to |
| 14 |
// permit persons to whom the Software is furnished to do so, subject to |
| 15 |
// the following conditions: |
| 16 |
// |
| 17 |
// The above copyright notice and this permission notice shall be |
| 18 |
// included in all copies or substantial portions of the Software. |
| 19 |
// |
| 20 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 21 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 22 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 23 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 24 |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 25 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 26 |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 27 |
// |
| 28 |
|
| 29 |
namespace Mono.Cecil.Binary { |
| 30 |
|
| 31 |
using System; |
| 32 |
using System.IO; |
| 33 |
using System.Text; |
| 34 |
|
| 35 |
sealed class ResourceReader { |
| 36 |
|
| 37 |
Image m_img; |
| 38 |
Section m_rsrc; |
| 39 |
BinaryReader m_reader; |
| 40 |
|
| 41 |
public ResourceReader (Image img) |
| 42 |
{ |
| 43 |
m_img = img; |
| 44 |
} |
| 45 |
|
| 46 |
public ResourceDirectoryTable Read () |
| 47 |
{ |
| 48 |
m_rsrc = GetResourceSection (); |
| 49 |
if (m_rsrc == null) |
| 50 |
return null; |
| 51 |
|
| 52 |
m_reader = new BinaryReader (new MemoryStream (m_rsrc.Data)); |
| 53 |
return ReadDirectoryTable (); |
| 54 |
} |
| 55 |
|
| 56 |
Section GetResourceSection () |
| 57 |
{ |
| 58 |
foreach (Section s in m_img.Sections) |
| 59 |
if (s.Name == Section.Resources) |
| 60 |
return s; |
| 61 |
|
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
int GetOffset () |
| 66 |
{ |
| 67 |
return (int) m_reader.BaseStream.Position; |
| 68 |
} |
| 69 |
|
| 70 |
ResourceDirectoryTable ReadDirectoryTable () |
| 71 |
{ |
| 72 |
ResourceDirectoryTable rdt = new ResourceDirectoryTable (GetOffset ()); |
| 73 |
rdt.Characteristics = m_reader.ReadUInt32 (); |
| 74 |
rdt.TimeDateStamp = m_reader.ReadUInt32 (); |
| 75 |
rdt.MajorVersion = m_reader.ReadUInt16 (); |
| 76 |
rdt.MinorVersion = m_reader.ReadUInt16 (); |
| 77 |
ushort nameEntries = m_reader.ReadUInt16 (); |
| 78 |
ushort idEntries = m_reader.ReadUInt16 (); |
| 79 |
|
| 80 |
for (int i = 0; i < nameEntries; i++) |
| 81 |
rdt.Entries.Add (ReadDirectoryEntry ()); |
| 82 |
|
| 83 |
for (int i = 0; i < idEntries; i++) |
| 84 |
rdt.Entries.Add (ReadDirectoryEntry ()); |
| 85 |
|
| 86 |
return rdt; |
| 87 |
} |
| 88 |
|
| 89 |
ResourceDirectoryEntry ReadDirectoryEntry () |
| 90 |
{ |
| 91 |
uint name = m_reader.ReadUInt32 (); |
| 92 |
uint child = m_reader.ReadUInt32 (); |
| 93 |
|
| 94 |
ResourceDirectoryEntry rde; |
| 95 |
if ((name & 0x80000000) != 0) |
| 96 |
rde = new ResourceDirectoryEntry (ReadDirectoryString ((int) name & 0x7fffffff), GetOffset ()); |
| 97 |
else |
| 98 |
rde = new ResourceDirectoryEntry ((int) name & 0x7fffffff, GetOffset ()); |
| 99 |
|
| 100 |
long pos = m_reader.BaseStream.Position; |
| 101 |
m_reader.BaseStream.Position = child & 0x7fffffff; |
| 102 |
|
| 103 |
if ((child & 0x80000000) != 0) |
| 104 |
rde.Child = ReadDirectoryTable (); |
| 105 |
else |
| 106 |
rde.Child = ReadDataEntry (); |
| 107 |
|
| 108 |
m_reader.BaseStream.Position = pos; |
| 109 |
|
| 110 |
return rde; |
| 111 |
} |
| 112 |
|
| 113 |
ResourceDirectoryString ReadDirectoryString (int offset) |
| 114 |
{ |
| 115 |
long pos = m_reader.BaseStream.Position; |
| 116 |
m_reader.BaseStream.Position = offset; |
| 117 |
|
| 118 |
byte [] str = m_reader.ReadBytes (m_reader.ReadUInt16 ()); |
| 119 |
|
| 120 |
ResourceDirectoryString rds = new ResourceDirectoryString ( |
| 121 |
Encoding.Unicode.GetString (str, 0, str.Length), |
| 122 |
GetOffset ()); |
| 123 |
|
| 124 |
m_reader.BaseStream.Position = pos; |
| 125 |
|
| 126 |
return rds; |
| 127 |
} |
| 128 |
|
| 129 |
ResourceNode ReadDataEntry () |
| 130 |
{ |
| 131 |
ResourceDataEntry rde = new ResourceDataEntry (GetOffset ()); |
| 132 |
rde.Data = m_reader.ReadUInt32 (); |
| 133 |
rde.Size = m_reader.ReadUInt32 (); |
| 134 |
rde.Codepage = m_reader.ReadUInt32 (); |
| 135 |
rde.Reserved = m_reader.ReadUInt32 (); |
| 136 |
|
| 137 |
Section sect = m_img.GetSectionAtVirtualAddress (rde.Data); |
| 138 |
byte [] data = new byte [rde.Size]; |
| 139 |
Buffer.BlockCopy (sect.Data, (int)(long)(rde.Data - sect.VirtualAddress), data, 0, (int)rde.Size); |
| 140 |
rde.ResourceData = data; |
| 141 |
|
| 142 |
return rde; |
| 143 |
} |
| 144 |
} |
| 145 |
} |