-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathProcessSample.cs
More file actions
89 lines (73 loc) · 3.45 KB
/
ProcessSample.cs
File metadata and controls
89 lines (73 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
namespace read_memory_64_bit;
public class ProcessSample
{
static public byte[] ZipArchiveFromProcessSample(
IImmutableList<SampleMemoryRegion> memoryRegions,
IImmutableList<string> logEntries,
byte[] beginMainWindowClientAreaScreenshotBmp,
byte[] endMainWindowClientAreaScreenshotBmp)
{
var screenshotEntriesCandidates =
new[]
{
(filePath: ImmutableList.Create("begin-main-window-client-area.bmp"), content: beginMainWindowClientAreaScreenshotBmp),
(filePath: ImmutableList.Create("end-main-window-client-area.bmp"), content: endMainWindowClientAreaScreenshotBmp),
};
var screenshotEntries =
screenshotEntriesCandidates
.Where(filePathAndContent => filePathAndContent.content is not null)
.Select(
filePathAndContent => new KeyValuePair<IImmutableList<string>, byte[]>(
filePathAndContent.filePath,
filePathAndContent.content))
.ToArray();
var zipArchiveEntries =
memoryRegions.ToImmutableDictionary(
region => (IImmutableList<string>)(["Process", "Memory", $"0x{region.baseAddress:X}"]),
region => region.content.Value.ToArray())
.Add(
new[] { "copy-memory-log" }.ToImmutableList(),
System.Text.Encoding.UTF8.GetBytes(string.Join("\n", logEntries)))
.AddRange(screenshotEntries);
return Pine.ZipArchive.ZipArchiveFromEntries(zipArchiveEntries);
}
static public (IImmutableList<SampleMemoryRegion> memoryRegions, IImmutableList<string> copyMemoryLog) ProcessSampleFromZipArchive(byte[] sampleFile)
{
var files =
Pine.ZipArchive.EntriesFromZipArchive(sampleFile);
IEnumerable<(IImmutableList<string> filePath, byte[] fileContent)> GetFilesInDirectory(IImmutableList<string> directory)
{
foreach (var fileFullPathAndContent in files)
{
var fullPath = fileFullPathAndContent.name.Split(['/', '\\']);
if (!fullPath.Take(directory.Count).SequenceEqual(directory))
continue;
yield return (fullPath.Skip(directory.Count).ToImmutableList(), fileFullPathAndContent.content);
}
}
var memoryRegions =
GetFilesInDirectory(ImmutableList.Create("Process", "Memory"))
.Where(fileSubpathAndContent => fileSubpathAndContent.filePath.Count is 1)
.Select(
fileSubpathAndContent =>
{
var baseAddressBase16 =
System.Text.RegularExpressions.Regex.Match(fileSubpathAndContent.filePath.Single(), @"0x(.+)").Groups[1].Value;
var baseAddress = ulong.Parse(baseAddressBase16, System.Globalization.NumberStyles.HexNumber);
return
new SampleMemoryRegion(
baseAddress,
length: (ulong)fileSubpathAndContent.fileContent.LongLength,
content: fileSubpathAndContent.fileContent);
}).ToImmutableList();
return (memoryRegions, null);
}
}
public record SampleMemoryRegion(
ulong baseAddress,
ulong length,
ReadOnlyMemory<byte>? content);