From 631833cd502ad27d4480a67caaea3af0ef23f2ca Mon Sep 17 00:00:00 2001
From: "Joel Sallow (/u/ta11ow)" <32407840+vexx32@users.noreply.github.com>
Date: Fri, 18 Jan 2019 14:33:36 -0500
Subject: [PATCH] Cleanup Format-Hex (#8683)
---
.../FormatAndOutput/format-hex/Format-Hex.cs | 86 ++++++++++---------
1 file changed, 47 insertions(+), 39 deletions(-)
diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs
index 44d47433e2b6..6bb1f37a9cd2 100644
--- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs
+++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/format-hex/Format-Hex.cs
@@ -26,14 +26,14 @@ public sealed class FormatHex : PSCmdlet
#region Parameters
///
- /// Path of file(s) to process.
+ /// Gets or sets the path of file(s) to process.
///
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "Path")]
[ValidateNotNullOrEmpty()]
public string[] Path { get; set; }
///
- /// Literal path of file to process.
+ /// Gets or sets the literal path of file to process.
///
[Parameter(Mandatory = true, ParameterSetName = "LiteralPath")]
[ValidateNotNullOrEmpty()]
@@ -41,13 +41,13 @@ public sealed class FormatHex : PSCmdlet
public string[] LiteralPath { get; set; }
///
- /// Object to process.
+ /// Gets or sets the object to process.
///
[Parameter(Mandatory = true, ParameterSetName = "ByInputObject", ValueFromPipeline = true)]
public PSObject InputObject { get; set; }
///
- /// Type of character encoding for InputObject.
+ /// Gets or sets the type of character encoding for InputObject.
///
[Parameter(ParameterSetName = "ByInputObject")]
[ArgumentToEncodingTransformationAttribute()]
@@ -60,17 +60,17 @@ public sealed class FormatHex : PSCmdlet
///
[Parameter]
[ValidateRange(ValidateRangeKind.Positive)]
- public Int64 Count { get; set; } = Int64.MaxValue;
+ public long Count { get; set; } = long.MaxValue;
///
/// Gets or sets offset of bytes to start reading the input stream from.
///
[Parameter]
[ValidateRange(ValidateRangeKind.NonNegative)]
- public Int64 Offset { get; set; }
+ public long Offset { get; set; }
///
- /// This parameter is no-op.
+ /// Gets or sets whether the file input should be swallowed as is. This parameter is no-op, deprecated.
///
[Parameter(ParameterSetName = "ByInputObject", DontShow = true)]
[Obsolete("Raw parameter is deprecated.", true)]
@@ -107,8 +107,8 @@ protected override void ProcessRecord()
/// If path is a literal path it is added to the array to process; we cannot validate them until we
/// try to process file contents.
///
- ///
- ///
+ /// The file path to resolve.
+ /// The paths to process.
///
private List ResolvePaths(string[] path, bool literalPath)
{
@@ -145,10 +145,11 @@ private List ResolvePaths(string[] path, bool literalPath)
{
// Write a non-terminating error message indicating that path specified is not supported.
string errorMessage = StringUtil.Format(UtilityCommonStrings.FormatHexOnlySupportsFileSystemPaths, currentPath);
- ErrorRecord errorRecord = new ErrorRecord(new ArgumentException(errorMessage),
- "FormatHexOnlySupportsFileSystemPaths",
- ErrorCategory.InvalidArgument,
- currentPath);
+ ErrorRecord errorRecord = new ErrorRecord(
+ new ArgumentException(errorMessage),
+ "FormatHexOnlySupportsFileSystemPaths",
+ ErrorCategory.InvalidArgument,
+ currentPath);
WriteError(errorRecord);
continue;
}
@@ -162,7 +163,7 @@ private List ResolvePaths(string[] path, bool literalPath)
///
/// Pass each valid path on to process its contents.
///
- ///
+ /// The paths to process.
private void ProcessPath(List pathsToProcess)
{
foreach (string path in pathsToProcess)
@@ -173,9 +174,9 @@ private void ProcessPath(List pathsToProcess)
///
/// Creates a binary reader that reads the file content into a buffer (byte[]) 16 bytes at a time, and
- /// passes a copy of that array on to the WriteHexidecimal method to output.
+ /// passes a copy of that array on to the WriteHexadecimal method to output.
///
- ///
+ /// The file path to retrieve content from for processing.
private void ProcessFileContent(string path)
{
Span buffer = stackalloc byte[BUFFERSIZE];
@@ -184,9 +185,9 @@ private void ProcessFileContent(string path)
{
using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
- Int64 offset = Offset;
- Int32 bytesRead = 0;
- Int64 count = 0;
+ long offset = Offset;
+ int bytesRead = 0;
+ long count = 0;
reader.BaseStream.Position = Offset;
@@ -196,11 +197,11 @@ private void ProcessFileContent(string path)
if (count > Count)
{
bytesRead -= (int)(count - Count);
- WriteHexidecimal(buffer.Slice(0, bytesRead), path, offset);
+ WriteHexadecimal(buffer.Slice(0, bytesRead), path, offset);
break;
}
- WriteHexidecimal(buffer.Slice(0, bytesRead), path, offset);
+ WriteHexadecimal(buffer.Slice(0, bytesRead), path, offset);
offset += bytesRead;
}
@@ -231,19 +232,19 @@ private void ProcessFileContent(string path)
///
/// Creates a byte array from the object passed to the cmdlet (based on type) and passes
- /// that array on to the WriteHexidecimal method to output.
+ /// that array on to the WriteHexadecimal method to output.
///
- ///
+ /// The pipeline input object being processed.
private void ProcessObjectContent(PSObject inputObject)
{
object obj = inputObject.BaseObject;
if (obj is System.IO.FileSystemInfo fsi)
{
- string[] path = { fsi.FullName };
- List pathsToProcess = ResolvePaths(path, true);
- ProcessPath(pathsToProcess);
- return;
+ string[] path = { fsi.FullName };
+ List pathsToProcess = ResolvePaths(path, true);
+ ProcessPath(pathsToProcess);
+ return;
}
byte[] inputBytes = ConvertToByteArray(obj);
@@ -254,24 +255,31 @@ private void ProcessObjectContent(PSObject inputObject)
int count = Math.Min(inputBytes.Length - offset, Count < (long)int.MaxValue ? (int)Count : int.MaxValue);
if (offset != 0 || count != inputBytes.Length)
{
- WriteHexidecimal(inputBytes.AsSpan().Slice(offset, count), null, 0);
+ WriteHexadecimal(inputBytes.AsSpan().Slice(offset, count), null, 0);
}
else
{
- WriteHexidecimal(inputBytes, null, 0);
+ WriteHexadecimal(inputBytes, null, 0);
}
}
else
{
string errorMessage = StringUtil.Format(UtilityCommonStrings.FormatHexTypeNotSupported, obj.GetType());
- ErrorRecord errorRecord = new ErrorRecord(new ArgumentException(errorMessage),
- "FormatHexTypeNotSupported",
- ErrorCategory.InvalidArgument,
- obj.GetType());
+ ErrorRecord errorRecord = new ErrorRecord(
+ new ArgumentException(errorMessage),
+ "FormatHexTypeNotSupported",
+ ErrorCategory.InvalidArgument,
+ obj.GetType());
WriteError(errorRecord);
}
}
+ ///
+ /// Converts the input object to a byte array based on the underlying type for basic value types and strings,
+ /// as well as enum values or arrays.
+ ///
+ /// The object to convert.
+ /// Returns a byte array of the input values, or null if there is no available conversion path.
private byte[] ConvertToByteArray(object inputObject)
{
if (inputObject is string str)
@@ -356,20 +364,20 @@ private byte[] ConvertToByteArray(object inputObject)
#region Output
///
- /// Outputs the hexadecimial representaion of the input data.
+ /// Outputs the hexadecimial representation of the input data.
///
- /// Bytes for the hexadecimial representaion.
+ /// Bytes for the hexadecimial representation.
/// File path.
/// Offset in the file.
- private void WriteHexidecimal(Span inputBytes, string path, Int64 offset)
+ private void WriteHexadecimal(Span inputBytes, string path, long offset)
{
- ByteCollection byteCollectionObject = new ByteCollection((UInt64)offset, inputBytes.ToArray(), path);
+ ByteCollection byteCollectionObject = new ByteCollection((ulong)offset, inputBytes.ToArray(), path);
WriteObject(byteCollectionObject);
}
- private void WriteHexidecimal(byte[] inputBytes, string path, Int64 offset)
+ private void WriteHexadecimal(byte[] inputBytes, string path, long offset)
{
- ByteCollection byteCollectionObject = new ByteCollection((UInt64)offset, inputBytes, path);
+ ByteCollection byteCollectionObject = new ByteCollection((ulong)offset, inputBytes, path);
WriteObject(byteCollectionObject);
}