Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for reading user strings from the #US heap

  • Loading branch information...
commit 3a335673a143df47ec342c58def9fd80d1abe152 1 parent 251eef7
@philiplaureano philiplaureano authored
View
49 src/Tao/Metadata/UserStringHeapReader.n
@@ -0,0 +1,49 @@
+using Nemerle;
+using Nemerle.Assertions;
+using Nemerle.Collections;
+using Nemerle.Text;
+using Nemerle.Utility;
+
+using Tao;
+using Tao.Interfaces;
+
+using System;
+using System.IO;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Tao
+{
+ public class UserStringHeapReader
+ {
+ public Read(index: uint, [NotNull] stream: Stream) : string
+ requires index <= stream.Length
+ {
+ // Seek the target offset
+ _ = stream.Seek(index :> int, SeekOrigin.Begin);
+
+ def integerReader = CompressedIntegerReader();
+ def length = integerReader.ReadInteger(stream);
+ def actualLength = length / 2 : uint;
+
+ def characters = List.[char]();
+ mutable charactersRead : uint= 0;
+ for(mutable i : uint = 0; i < length; i += 2)
+ {
+ def highByte = stream.ReadByte();
+ def lowByte = stream.ReadByte();
+
+ when (charactersRead < actualLength)
+ {
+ def currentCharacter : char = (lowByte << 8 | highByte) :> char;
+ characters.Add(currentCharacter);
+ }
+
+ charactersRead++;
+ }
+
+ def result = string(characters.ToArray());
+ result;
+ }
+ }
+}
View
3  src/Tao/Tao.nproj
@@ -181,6 +181,9 @@
<Compile Include="Metadata\StreamHeaderWriter.n">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Metadata\UserStringHeapReader.n">
+ <SubType>Code</SubType>
+ </Compile>
<Compile Include="NTHeader.n">
<SubType>Code</SubType>
</Compile>
View
BIN  src/Tests/SampleBinaries/HelloWorld.exe
Binary file not shown
View
6 src/Tests/Tests.nproj
@@ -160,12 +160,18 @@
<Compile Include="UserStringsTest.n">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="UserStringTests.n">
+ <SubType>Code</SubType>
+ </Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="SampleBinaries" />
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup>
+ <Content Include="SampleBinaries\HelloWorld.exe">
+ <SubType>Content</SubType>
+ </Content>
<Content Include="SampleBinaries\SamplePE32PlusApp.exe">
<SubType>Content</SubType>
</Content>
View
42 src/Tests/UserStringTests.n
@@ -0,0 +1,42 @@
+using Nemerle;
+using Nemerle.Collections;
+using Nemerle.Text;
+using Nemerle.Utility;
+
+using Tao;
+using Tests.Macros;
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Tests
+{
+ public class UserStringTests
+ {
+ public ShouldBeAbleToReadUserString() : void
+ {
+ TestRead(1 : uint, "Hello, World!");
+ TestRead(0x1D : uint, "Hello, World Again!");
+ }
+ public ShouldReturnEmptyStringOnZeroIndex() : void
+ {
+ def index : uint = 0;
+ def expectedString = string.Empty;
+
+ TestRead(index, expectedString);
+ }
+ private TestRead(index : uint, expectedString: string) : void
+ {
+ def imageFile = @"..\..\SampleBinaries\HelloWorld.exe";
+ def image = Image.ReadFrom(imageFile);
+ def metadataRoot = image.GetMetadataRoot();
+ def heap = metadataRoot.Heaps["#US"];
+
+ def reader = UserStringHeapReader();
+ def text = reader.Read(index, heap);
+
+ assert text equals expectedString otherwiseprint "Invalid user string";
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.