-
Notifications
You must be signed in to change notification settings - Fork 1
/
sdk.ps1
102 lines (87 loc) · 2.15 KB
/
sdk.ps1
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# change your input and output file path HERE
$inputFilePath = "path/to/ThemidaSDK.h"
$outputFilePath = "src/sdk.rs"
$inputLines = Get-Content $inputFilePath
$inAssemblyBlock = $false
$assemblyCode = @()
$outputLines = @()
$macroName = $null
$gnuCount = 0
# we ignore those functions for now
$ignoreFunctions = @(
"CHECK_CODE_INTEGRITY",
"CHECK_REGISTRATION",
"CHECK_VIRTUAL_PC",
"CHECK_PROTECTION",
"VM_START_WITHLEVEL"
)
$generatedMacros = @{}
foreach ($line in $inputLines) {
if ($line -match "#ifdef __GNUC__") {
$gnuCount++
if ($gnuCount -eq 4) {
$inAssemblyBlock = $true
}
continue
}
elseif ($line -match "#else") {
if ($inAssemblyBlock) {
$inAssemblyBlock = $false
break
}
}
if (-not $inAssemblyBlock) {
continue
}
if ($line -match "NO_OPTIMIZATION" -or $line.StartsWith("//") -or $line.StartsWith("/*")) {
continue
}
if ($line -match "#define (.+?)\((.+?)\) \\") {
$macroName = $matches[1]
if ($ignoreFunctions -contains $macroName -or $generatedMacros.ContainsKey($macroName)) {
$macroName = $null
continue
}
$macroName = $macroName.ToLower()
continue
}
elseif ($line -match "#define (.+?) \\") {
$macroName = $matches[1]
if ($ignoreFunctions -contains $macroName -or $generatedMacros.ContainsKey($macroName)) {
$macroName = $null
continue
}
$macroName = $macroName.ToLower()
continue
}
if ($line -match "\);") {
$assemblyCode += $inputLines[[array]::IndexOf($inputLines, $line) - 1] -replace 'asm\(', '' -replace '\\\"', '`"' -replace '\\\\', '\\' -replace '\\$', ',' -replace '\);', ''
if ($null -ne $macroName) {
$rustMacro = @"
#[macro_export]
macro_rules! $macroName {
() => {
unsafe {
asm!(
$assemblyCode
);
}
};
}
"@
$outputLines += $rustMacro
$generatedMacros[$macroName] = $true
}
$macroName = $null
$assemblyCode = @()
continue
}
if ($macroName) {
$cleanedLine = $line -replace 'asm \(', '' -replace '\\\"', '`"' -replace '\\\\', '\\' -replace '\\$', ',' -replace '\);', ''
if ($line -match "\\n$") {
$cleanedLine += "\n"
}
$assemblyCode += $cleanedLine
}
}
$outputLines | Out-File $outputFilePath -Encoding utf8