-
Notifications
You must be signed in to change notification settings - Fork 143
/
Out-CSharpDataClass.ps1
93 lines (74 loc) · 2.18 KB
/
Out-CSharpDataClass.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
param(
[Parameter(Position=0, Mandatory=$true)]
$Target,
[Parameter(Position=1, Mandatory=$true)]
$SolutionDir
)
function Get-ContentBytesAndXor {
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$true)]
[string]
$Path,
[Parameter(Position=1, Mandatory=$false)]
[int]
$XorKey = 0
)
Write-Verbose "XorKey: $XorKey"
if(Test-Path -Path $Path -ErrorAction Stop -PathType Leaf) {
$Fullname = (Get-ChildItem $Path).FullName
if($XorKey -ne 0) {
Write-Verbose "Using XorKey"
foreach($Byte in ([System.IO.File]::ReadAllBytes($Fullname))) {
$Byte -bxor $XorKey
}
} else {
[System.IO.File]::ReadAllBytes($FullName)
}
}
}
function ConvertTo-CSharpDataClass {
[CmdletBinding()]
Param(
[Parameter(Position=0, Mandatory=$true)]
[byte[]]
$ByteArray,
[Parameter(Position=1, Mandatory=$false)]
[int]
$Columns = 20,
[Parameter(Position=2, Mandatory=$false)]
[string]
$Namespace = 'Test',
[Parameter(Position=3, Mandatory=$false)]
[string]
$Class = 'Data',
[Parameter(Position=4, Mandatory=$false)]
[string]
$Variable = 'Data'
)
$sb = New-Object System.Text.StringBuilder
$null = $sb.Append(@"
namespace SpoolSample
{
static class Data
{
public static byte[] $($Variable) = new byte[] {
"@)
for($i = 0; $i -lt $ByteArray.Count; $i++) {
if(($i % $Columns) -eq 0) {
$null = $sb.Append("`n ")
}
$null = $sb.Append("0x{0:X2}," -f $ByteArray[$i])
}
$null = $sb.Remove($sb.Length-1, 1) #Remove the last comman
$null = $sb.AppendLine(@"
`n };
}
}
"@)
$sb.ToString()
}
# For whatever reason, VS keeps including a double quote in the pathst
$SolutionDir = $SolutionDir.Replace('"','')
$bytes = Get-ContentBytesAndXor -Path $Target
ConvertTo-CSharpDataClass -Namespace 'SpoolSample' -Class 'Data' -Variable 'RprnDll' -ByteArray $bytes | Out-File -Encoding ascii -FilePath "$($SolutionDir)\SpoolSample\Data.cs"