-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
utils.cpp
210 lines (177 loc) · 5.75 KB
/
utils.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ============================================================
//
// Utils.cpp
//
//
// Implements a bunch of binder auxilary functions
//
// ============================================================
#include "utils.hpp"
#include "strongnameinternal.h"
#include "corpriv.h"
#include "clr/fs/path.h"
using namespace clr::fs;
namespace BINDER_SPACE
{
namespace
{
inline const WCHAR *GetPlatformPathSeparator()
{
#ifdef TARGET_UNIX
return W("/");
#else
return W("\\");
#endif // TARGET_UNIX
}
}
void CombinePath(const SString &pathA,
const SString &pathB,
SString &combinedPath)
{
SString platformPathSeparator(SString::Literal, GetPlatformPathSeparator());
combinedPath.Set(pathA);
if (!combinedPath.IsEmpty() && !combinedPath.EndsWith(platformPathSeparator))
{
combinedPath.Append(platformPathSeparator);
}
combinedPath.Append(pathB);
}
HRESULT GetTokenFromPublicKey(SBuffer &publicKeyBLOB,
SBuffer &publicKeyTokenBLOB)
{
HRESULT hr = S_OK;
const BYTE *pByteKey = publicKeyBLOB;
DWORD dwKeyLen = publicKeyBLOB.GetSize();
BYTE *pByteToken = NULL;
DWORD dwTokenLen = 0;
IF_FAIL_GO(StrongNameTokenFromPublicKey(
const_cast<BYTE*>(pByteKey),
dwKeyLen,
&pByteToken,
&dwTokenLen));
_ASSERTE(pByteToken != NULL);
publicKeyTokenBLOB.Set(pByteToken, dwTokenLen);
StrongNameFreeBuffer(pByteToken);
Exit:
return hr;
}
BOOL IsFileNotFound(HRESULT hr)
{
return RuntimeFileNotFound(hr);
}
HRESULT GetNextPath(const SString& paths, SString::CIterator& startPos, SString& outPath)
{
HRESULT hr = S_OK;
bool wrappedWithQuotes = false;
// Skip any leading spaces or path separators
while (paths.Skip(startPos, W(' ')) || paths.Skip(startPos, PATH_SEPARATOR_CHAR_W)) {}
if (startPos == paths.End())
{
// No more paths in the string and we just skipped over some white space
outPath.Set(W(""));
return S_FALSE;
}
// Support paths being wrapped with quotations
if (paths.Skip(startPos, W('\"')))
{
wrappedWithQuotes = true;
}
SString::CIterator iEnd = startPos; // Where current path ends
SString::CIterator iNext; // Where next path starts
if (wrappedWithQuotes)
{
if (paths.Find(iEnd, W('\"')))
{
iNext = iEnd;
// Find where the next path starts - there should be a path separator right after the closing quotation mark
if (paths.Find(iNext, PATH_SEPARATOR_CHAR_W))
{
iNext++;
}
else
{
iNext = paths.End();
}
}
else
{
// There was no terminating quotation mark - that's bad
GO_WITH_HRESULT(E_INVALIDARG);
}
}
else if (paths.Find(iEnd, PATH_SEPARATOR_CHAR_W))
{
iNext = iEnd + 1;
}
else
{
iNext = iEnd = paths.End();
}
// Skip any trailing spaces
while (iEnd[-1] == W(' '))
{
iEnd--;
}
_ASSERTE(startPos < iEnd);
outPath.Set(paths, startPos, iEnd);
startPos = iNext;
Exit:
return hr;
}
HRESULT GetNextTPAPath(const SString& paths, SString::CIterator& startPos, bool dllOnly, SString& outPath, SString& simpleName, bool& isNativeImage)
{
HRESULT hr = S_OK;
isNativeImage = false;
HRESULT pathResult = S_OK;
IF_FAIL_GO(pathResult = GetNextPath(paths, startPos, outPath));
if (pathResult == S_FALSE)
{
return S_FALSE;
}
if (Path::IsRelative(outPath))
{
GO_WITH_HRESULT(E_INVALIDARG);
}
{
// Find the beginning of the simple name
SString::CIterator iSimpleNameStart = outPath.End();
if (!outPath.FindBack(iSimpleNameStart, DIRECTORY_SEPARATOR_CHAR_W))
{
iSimpleNameStart = outPath.Begin();
}
else
{
// Advance past the directory separator to the first character of the file name
iSimpleNameStart++;
}
if (iSimpleNameStart == outPath.End())
{
GO_WITH_HRESULT(E_INVALIDARG);
}
const SString sNiDll(SString::Literal, W(".ni.dll"));
const SString sNiExe(SString::Literal, W(".ni.exe"));
const SString sDll(SString::Literal, W(".dll"));
const SString sExe(SString::Literal, W(".exe"));
if (!dllOnly && (outPath.EndsWithCaseInsensitive(sNiDll) ||
outPath.EndsWithCaseInsensitive(sNiExe)))
{
simpleName.Set(outPath, iSimpleNameStart, outPath.End() - 7);
isNativeImage = true;
}
else if (outPath.EndsWithCaseInsensitive(sDll) ||
(!dllOnly && outPath.EndsWithCaseInsensitive(sExe)))
{
simpleName.Set(outPath, iSimpleNameStart, outPath.End() - 4);
}
else
{
// Invalid filename
GO_WITH_HRESULT(E_INVALIDARG);
}
}
Exit:
return hr;
}
};