forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilityCommon.cs
More file actions
344 lines (300 loc) · 12.8 KB
/
Copy pathUtilityCommon.cs
File metadata and controls
344 lines (300 loc) · 12.8 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
[module: SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Scope = "type", Target = "Microsoft.PowerShell.Commands.ByteCollection")]
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// The base class for all command processor classes. It provides
/// abstract methods to execute a command.
/// </summary>
internal static class UtilityCommon
{
#if CORECLR
// AccessViolationException/StackOverflowException Not In CoreCLR.
// The CoreCLR team told us to not check for these exceptions because they
// usually won't be caught.
internal static void CheckForSevereException(PSCmdlet cmdlet, Exception e) { }
#else
// Keep in sync:
// S.M.A.CommandProcessorBase.CheckForSevereException
// S.M.A.Internal.ConsoleHost.CheckForSevereException
// S.M.A.Commands.CommandsCommon.CheckForSevereException
// S.M.A.Commands.UtilityCommon.CheckForSevereException
/// <summary>
/// Checks whether the exception is a severe exception which should
/// cause immediate process failure.
/// </summary>
/// <param name="cmdlet">can be null</param>
/// <param name="e"></param>
/// <remarks>
/// CB says 02/23/2005: I personally would err on the side
/// of treating OOM like an application exception, rather than
/// a critical system failure.I think this will be easier to justify
/// in Orcas, if we tease apart the two cases of OOM better.
/// But even in Whidbey, how likely is it that we couldnt JIT
/// some backout code? At that point, the process or possibly
/// the machine is likely to stop executing soon no matter
/// what you do in this routine. So I would just consider
/// AccessViolationException. (I understand why you have SO here,
/// at least temporarily).
/// </remarks>
internal static void CheckForSevereException(PSCmdlet cmdlet, Exception e)
{
if (e is AccessViolationException || e is StackOverflowException)
{
try
{
if (!alreadyFailing)
{
alreadyFailing = true;
// Get the ExecutionContext from the thread.
ExecutionContext context =
(null != cmdlet)
? cmdlet.Context
: LocalPipeline.GetExecutionContextFromTLS();
// Log a command health event for this critical error.
MshLog.LogCommandHealthEvent(context, e, Severity.Critical);
}
}
finally
{
if (!designForTestability_SkipFailFast)
WindowsErrorReporting.FailFast(e);
}
}
}
private static bool alreadyFailing = false;
// 2005/04/22-JonN Adding this capability so that we can
// get some test coverage on this function
private static bool designForTestability_SkipFailFast = false;
#endif
/// <summary>
/// Converts the textencodingtype enum value to the corresponding encoding
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal static Encoding GetEncodingFromEnum(TextEncodingType type)
{
Encoding encoding = Encoding.ASCII;
switch (type)
{
case TextEncodingType.String:
encoding = Encoding.Unicode;
break;
case TextEncodingType.Unicode:
encoding = Encoding.Unicode;
break;
case TextEncodingType.BigEndianUnicode:
encoding = Encoding.BigEndianUnicode;
break;
case TextEncodingType.Utf8:
encoding = Encoding.UTF8;
break;
case TextEncodingType.Utf7:
encoding = Encoding.UTF7;
break;
case TextEncodingType.Ascii:
encoding = Encoding.ASCII;
break;
default:
// Default to unicode encoding
encoding = Encoding.ASCII;
break;
}
return encoding;
}
} // class UtilityCommon
/// <summary>
/// abc
/// </summary>
public enum TextEncodingType
{
/// <summary>
/// No encoding.
/// </summary>
Unknown,
/// <summary>
/// Unicode encoding.
/// </summary>
String,
/// <summary>
/// Unicode encoding.
/// </summary>
Unicode,
/// <summary>
/// Byte encoding.
/// </summary>
Byte,
/// <summary>
/// Big Endian Unicode encoding.
/// </summary>
BigEndianUnicode,
/// <summary>
/// UTF8 encoding.
/// </summary>
Utf8,
/// <summary>
/// UTF7 encoding.
/// </summary>
Utf7,
/// <summary>
/// ASCII encoding.
/// </summary>
Ascii,
}
/// <summary>
/// Utility class to contain resources for the Microsoft.PowerShell.Utility module
/// </summary>
public static class UtilityResources
{
/// <summary>
/// </summary>
public static string PathDoesNotExist { get { return UtilityCommonStrings.PathDoesNotExist; } }
/// <summary>
/// </summary>
public static string FileReadError { get { return UtilityCommonStrings.FileReadError; } }
/// <summary>
/// Error message to indicate that Format-Hex cmdlet does not directly support the type provided as input.
/// </summary>
public static string FormatHexTypeNotSupported { get { return UtilityCommonStrings.FormatHexTypeNotSupported; } }
/// <summary>
/// Error message to indicate that Format-Hex cmdlet does not directly support the type provided as input.
/// </summary>
public static string FormatHexResolvePathError { get { return UtilityCommonStrings.FormatHexResolvePathError; } }
/// <summary>
/// The resource string used to indicate 'PATH:' in the formating header.
/// </summary>
public static string FormatHexPathPrefix { get { return UtilityCommonStrings.FormatHexPathPrefix; } }
/// <summary>
/// Error message to indicate that requested algorithm is not supported on the target platform.
/// </summary>
public static string AlgorithmTypeNotSupported { get { return UtilityCommonStrings.AlgorithmTypeNotSupported; } }
/// <summary>
/// The file '{0}' could not be parsed as a PowerShell Data File.
/// </summary>
public static string CouldNotParseAsPowerShellDataFile { get { return UtilityCommonStrings.CouldNotParseAsPowerShellDataFile; } }
}
/// <summary>
/// ByteCollection is used as a wrapper class for the collection of bytes.
/// </summary>
public class ByteCollection
{
/// <summary>
/// ByteCollection constructor.
/// </summary>
/// <param name="offset">The Offset address to be used while displaying the bytes in the collection.</param>
/// <param name="value">Underlying bytes stored in the collection.</param>
/// <param name="path">Indicates the path of the file whose contents are wrapped in the ByteCollection.</param>
public ByteCollection(UInt32 offset, Byte[] value, string path)
{
this.Offset = offset;
_initialOffSet = offset;
this.Bytes = value;
this.Path = path;
}
/// <summary>
/// ByteCollection constructor.
/// </summary>
/// <param name="offset">The Offset address to be used while displaying the bytes in the collection.</param>
/// <param name="value">Underlying bytes stored in the collection.</param>
public ByteCollection(UInt32 offset, Byte[] value)
{
this.Offset = offset;
_initialOffSet = offset;
this.Bytes = value;
}
/// <summary>
/// ByteCollection constructor.
/// </summary>
/// <param name="value">Underlying bytes stored in the collection.</param>
public ByteCollection(Byte[] value)
{
this.Bytes = value;
}
/// <summary>
/// The Offset address to be used while displaying the bytes in the collection.
/// </summary>
public UInt32 Offset { get; private set; }
private UInt32 _initialOffSet = 0;
/// <summary>
/// Underlying bytes stored in the collection.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public Byte[] Bytes { get; private set; }
/// <summary>
/// Indicates the path of the file whose contents are wrapped in the ByteCollection.
/// </summary>
public string Path { get; private set; }
/// <summary>
/// Displays the hexadecimal format of the bytes stored in the collection.
/// </summary>
/// <returns></returns>
public override string ToString()
{
StringBuilder result = new StringBuilder();
StringBuilder nextLine = new StringBuilder();
StringBuilder asciiEnd = new StringBuilder();
if (Bytes.Length > 0)
{
UInt32 charCounter = 0;
// ToString() in invoked thrice by the F&O for the same content.
// Hence making sure that Offset is not getting incremented thrice for the same bytes being displayed.
Offset = _initialOffSet;
nextLine.AppendFormat("{0:X2} ", CultureInfo.InvariantCulture.TextInfo.ToUpper(Convert.ToString(Offset, 16)).PadLeft(8, '0'));
foreach (Byte currentByte in Bytes)
{
// Display each byte, in 2-digit hexadecimal, and add that to the left-hand side.
nextLine.AppendFormat("{0:X2} ", currentByte);
// If the character is printable, add its ascii representation to
// the right-hand side. Otherwise, add a dot to the right hand side.
if ((currentByte >= 0x20) && (currentByte <= 0xFE))
{
asciiEnd.Append((char)currentByte);
}
else
{
asciiEnd.Append('.');
}
charCounter++;
// If we've hit the end of a line, combine the right half with the
// left half, and start a new line.
if ((charCounter % 16) == 0)
{
result.Append(nextLine.ToString() + " " + asciiEnd.ToString());
nextLine.Clear();
asciiEnd.Clear();
Offset += 0x10;
nextLine.AppendFormat("{0:X2} ", CultureInfo.InvariantCulture.TextInfo.ToUpper(Convert.ToString(Offset, 16)).PadLeft(8, '0'));
// Adding a newline to support long inputs strings flowing through InputObject parameterset.
if ((charCounter <= Bytes.Length) && string.IsNullOrEmpty(this.Path))
{
result.Append("\r\n");
}
}
}
// At the end of the file, we might not have had the chance to output
// the end of the line yet. Only do this if we didn't exit on the 16-byte
// boundary, though.
if ((charCounter % 16) != 0)
{
while ((charCounter % 16) != 0)
{
nextLine.Append(" ");
asciiEnd.Append(' ');
charCounter++;
}
result.Append(nextLine.ToString() + " " + asciiEnd.ToString());
}
}
return result.ToString();
}
}
} // namespace Microsoft.PowerShell.Commands