forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbedAppNameInHost.cs
154 lines (136 loc) · 4.86 KB
/
EmbedAppNameInHost.cs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace Microsoft.NET.Build.Tasks
{
/// <summary>
/// Embeds the App Name into the AppHost.exe
/// </summary>
public class EmbedAppNameInHost : TaskBase
{
[Required]
public string AppHostSourcePath { get; set; }
[Required]
public string AppHostDestinationDirectoryPath { get; set; }
[Required]
public string AppBinaryName { get; set; }
[Output]
public string ModifiedAppHostPath { get; set; }
private static string placeHolder = "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"; //hash value embedded in default apphost executable
private static byte[] bytesToSearch = Encoding.UTF8.GetBytes(placeHolder);
protected override void ExecuteCore()
{
var hostExtension = Path.GetExtension(AppHostSourcePath);
var appbaseName = Path.GetFileNameWithoutExtension(AppBinaryName);
var bytesToWrite = Encoding.UTF8.GetBytes(AppBinaryName);
var destinationDirectory = Path.GetFullPath(AppHostDestinationDirectoryPath);
ModifiedAppHostPath = Path.Combine(destinationDirectory, $"{appbaseName}{hostExtension}");
if ( File.Exists(ModifiedAppHostPath))
{
//We have already done the required modification to apphost.exe
return;
}
if (bytesToWrite.Length > 1024)
{
throw new BuildErrorException(Strings.FileNameIsTooLong, AppBinaryName);
}
var array = File.ReadAllBytes(AppHostSourcePath);
SearchAndReplace(array, bytesToSearch, bytesToWrite, AppHostSourcePath);
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
File.WriteAllBytes(ModifiedAppHostPath, array);
}
// See: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
private static int[] ComputeKMPFailureFunction(byte[] pattern)
{
int[] table = new int[pattern.Length];
if (pattern.Length >= 1)
{
table[0] = -1;
}
if (pattern.Length >= 2)
{
table[1] = 0;
}
int pos = 2;
int cnd = 0;
while (pos < pattern.Length)
{
if (pattern[pos - 1] == pattern[cnd])
{
table[pos] = cnd + 1;
cnd++;
pos++;
}
else if (cnd > 0)
{
cnd = table[cnd];
}
else
{
table[pos] = 0;
pos++;
}
}
return table;
}
// See: https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
private static int KMPSearch(byte[] pattern, byte[] bytes)
{
int m = 0;
int i = 0;
int[] table = ComputeKMPFailureFunction(pattern);
while (m + i < bytes.Length)
{
if (pattern[i] == bytes[m + i])
{
if (i == pattern.Length - 1)
{
return m;
}
i++;
}
else
{
if (table[i] > -1)
{
m = m + i - table[i];
i = table[i];
}
else
{
m++;
i = 0;
}
}
}
return -1;
}
private static void SearchAndReplace(byte[] array, byte[] searchPattern, byte[] patternToReplace, string appHostSourcePath)
{
int offset = KMPSearch(searchPattern, array);
if (offset < 0)
{
throw new BuildErrorException(Strings.AppHostHasBeenModified, appHostSourcePath, placeHolder);
}
patternToReplace.CopyTo(array, offset);
if (patternToReplace.Length < searchPattern.Length)
{
for (int i = patternToReplace.Length; i < searchPattern.Length; i++)
{
array[i + offset] = 0x0;
}
}
}
}
}