forked from jbevain/cecil
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Condensed from https://github.com/gluck/cecil master branch
- Loading branch information
1 parent
56d4409
commit ebbba77
Showing
8 changed files
with
378 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Mono.Collections.Generic; | ||
|
||
namespace Mono.Cecil.PE | ||
{ | ||
public class ResourceEntry | ||
{ | ||
public uint Id { get; set; } | ||
public string Name { get; set; } | ||
public ResourceDirectory Directory { get; set; } | ||
public uint CodePage { get; set; } | ||
public uint Reserved { get; set; } | ||
public byte[] Data { get; set; } | ||
} | ||
|
||
public class ResourceDirectory | ||
{ | ||
private readonly Collection<ResourceEntry> _entries = new Collection<ResourceEntry>(); | ||
|
||
public Collection<ResourceEntry> Entries | ||
{ | ||
get | ||
{ | ||
return _entries; | ||
} | ||
} | ||
|
||
public ushort SortEntries() | ||
{ | ||
_entries.Sort(EntryComparer.Instance); | ||
for (ushort i = 0; i < _entries.Count; i++) | ||
if (_entries[i].Name == null) | ||
return i; | ||
return 0; | ||
} | ||
|
||
public ushort NumNameEntries { get; set; } | ||
public ushort NumIdEntries { get; set; } | ||
public ushort MinVersion { get; set; } | ||
public ushort MajorVersion { get; set; } | ||
public uint Characteristics { get; set; } | ||
public uint TimeDateStamp { get; set; } | ||
} | ||
|
||
class EntryComparer : IComparer<ResourceEntry> | ||
{ | ||
internal static readonly EntryComparer Instance = new EntryComparer(); | ||
public int Compare(ResourceEntry x, ResourceEntry y) | ||
{ | ||
if (x.Name != null && y.Name == null) | ||
return -1; | ||
if (x.Name == null && y.Name != null) | ||
return 1; | ||
if (x.Name == null) | ||
return (int)(x.Id - y.Id); | ||
return string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Mono.Cecil.PE | ||
{ | ||
internal class RsrcReader | ||
{ | ||
internal static ResourceDirectory ReadResourceDirectory(ByteBuffer b, uint baseAddress) | ||
{ | ||
return ReadResourceDirectory(b.buffer, baseAddress); | ||
} | ||
|
||
internal static ResourceDirectory ReadResourceDirectory(byte[] b, uint baseAddress) | ||
{ | ||
var dr = new MemoryStream(b); | ||
return ReadResourceDirectory(new BinaryReader(dr), baseAddress); | ||
} | ||
|
||
private static ResourceDirectory ReadResourceDirectory(BinaryReader dr, uint baseAddress) | ||
{ | ||
var d = ReadResourceDirectoryTable(dr); | ||
int ne = d.NumNameEntries + d.NumIdEntries; | ||
for (int i = 0; i < ne; i++) | ||
d.Entries.Add(ReadResourceEntry(dr, baseAddress)); | ||
return d; | ||
} | ||
|
||
private static ResourceEntry ReadResourceEntry(BinaryReader dr, uint baseAddress) | ||
{ | ||
var re = new ResourceEntry(); | ||
uint id = dr.ReadUInt32(); | ||
uint offset = dr.ReadUInt32(); | ||
long pos = dr.BaseStream.Position; | ||
if ((id & 0x80000000) != 0) | ||
{ | ||
dr.BaseStream.Position = (id & 0x7fffffff); | ||
var b = new StringBuilder(); | ||
int c; | ||
while ((c = dr.Read()) > 0) | ||
b.Append((char)c); | ||
re.Name = b.ToString(); | ||
} | ||
else | ||
{ | ||
re.Id = id; | ||
} | ||
if ((offset & 0x80000000) != 0) | ||
{ | ||
dr.BaseStream.Position = (offset & 0x7fffffff); | ||
re.Directory = ReadResourceDirectory(dr, baseAddress); | ||
} | ||
else | ||
{ | ||
dr.BaseStream.Position = offset; | ||
uint rva = dr.ReadUInt32(); | ||
uint size = dr.ReadUInt32(); | ||
uint cp = dr.ReadUInt32(); | ||
uint res = dr.ReadUInt32(); | ||
re.CodePage = cp; | ||
re.Reserved = res; | ||
dr.BaseStream.Position = (rva - baseAddress); | ||
re.Data = dr.ReadBytes((int)size); | ||
} | ||
dr.BaseStream.Position = pos; | ||
return re; | ||
} | ||
|
||
private static ResourceDirectory ReadResourceDirectoryTable(BinaryReader dr) | ||
{ | ||
var t = new ResourceDirectory | ||
{ | ||
Characteristics = dr.ReadUInt32(), | ||
TimeDateStamp = dr.ReadUInt32(), | ||
MajorVersion = dr.ReadUInt16(), | ||
MinVersion = dr.ReadUInt16(), | ||
NumNameEntries = dr.ReadUInt16(), | ||
NumIdEntries = dr.ReadUInt16() | ||
}; | ||
return t; | ||
} | ||
} | ||
} |
Oops, something went wrong.