Skip to content

Commit b629e2a

Browse files
committed
Generalized method to accept custom encodings.
1 parent a803779 commit b629e2a

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

ReClass.NET/Memory/RemoteProcess.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.Contracts;
44
using System.Linq;
@@ -10,6 +10,7 @@
1010
using ReClassNET.Core;
1111
using ReClassNET.Debugger;
1212
using ReClassNET.Extensions;
13+
using ReClassNET.MemoryScanner;
1314
using ReClassNET.Native;
1415
using ReClassNET.Symbols;
1516
using ReClassNET.Util;
@@ -366,24 +367,24 @@ public string ReadRemoteString(Encoding encoding, IntPtr address, int length)
366367
}
367368
}
368369

369-
/// <summary>Reads a string from the address in the remote process with the given length using UTF8 encoding. The string gets truncated at the first zero character.</summary>
370+
/// <summary>Reads a string from the address in the remote process with the given length and encoding. The string gets truncated at the first zero character.</summary>
371+
/// <param name="encoding">The encoding used by the string.</param>
370372
/// <param name="address">The address of the string.</param>
371373
/// <param name="length">The length of the string.</param>
372374
/// <returns>The string.</returns>
373-
public string ReadRemoteUTF8StringUntilFirstNullCharacter(IntPtr address, int length)
375+
public string ReadRemoteStringUntilFirstNullCharacter(Encoding encoding, IntPtr address, int length)
374376
{
377+
Contract.Requires(encoding != null);
375378
Contract.Requires(length >= 0);
376379
Contract.Ensures(Contract.Result<string>() != null);
377380

378-
var data = ReadRemoteMemory(address, length);
381+
var data = ReadRemoteMemory(address, length * encoding.GetSimpleByteCountPerChar());
379382

380-
int index = 0;
381-
for (; index < data.Length; ++index)
383+
// TODO We should cache the pattern per encoding.
384+
var index = PatternScanner.FindPattern(BytePattern.From(new byte[encoding.GetSimpleByteCountPerChar()]), data);
385+
if (index == -1)
382386
{
383-
if (data[index] == 0)
384-
{
385-
break;
386-
}
387+
index = data.Length;
387388
}
388389

389390
try
@@ -444,7 +445,7 @@ private string ReadRemoteRuntimeTypeInformation32(IntPtr address)
444445
var typeDescriptorPtr = ReadRemoteIntPtr(baseClassDescriptorPtr);
445446
if (typeDescriptorPtr.MayBeValid())
446447
{
447-
var name = ReadRemoteUTF8StringUntilFirstNullCharacter(typeDescriptorPtr + 0x0C, 60);
448+
var name = ReadRemoteStringUntilFirstNullCharacter(Encoding.UTF8, typeDescriptorPtr + 0x0C, 60);
448449
if (name.EndsWith("@@"))
449450
{
450451
name = NativeMethods.UndecorateSymbolName("?" + name);
@@ -506,7 +507,7 @@ private string ReadRemoteRuntimeTypeInformation64(IntPtr address)
506507
{
507508
var typeDescriptorPtr = baseAddress + typeDescriptorOffset;
508509

509-
var name = ReadRemoteUTF8StringUntilFirstNullCharacter(typeDescriptorPtr + 0x14, 60);
510+
var name = ReadRemoteStringUntilFirstNullCharacter(Encoding.UTF8, typeDescriptorPtr + 0x14, 60);
510511
if (string.IsNullOrEmpty(name))
511512
{
512513
break;

0 commit comments

Comments
 (0)