Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ public sealed class FormatHex : PSCmdlet
#region Parameters

/// <summary>
/// Path of file(s) to process.
/// Gets or sets the path of file(s) to process.
/// </summary>
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "Path")]
[ValidateNotNullOrEmpty()]
public string[] Path { get; set; }

/// <summary>
/// Literal path of file to process.
/// Gets or sets the literal path of file to process.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "LiteralPath")]
[ValidateNotNullOrEmpty()]
[Alias("PSPath", "LP")]
public string[] LiteralPath { get; set; }

/// <summary>
/// Object to process.
/// Gets or sets the object to process.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "ByInputObject", ValueFromPipeline = true)]
public PSObject InputObject { get; set; }

/// <summary>
/// Type of character encoding for InputObject.
/// Gets or sets the type of character encoding for InputObject.
/// </summary>
[Parameter(ParameterSetName = "ByInputObject")]
[ArgumentToEncodingTransformationAttribute()]
Expand All @@ -60,17 +60,17 @@ public sealed class FormatHex : PSCmdlet
/// </summary>
[Parameter]
[ValidateRange(ValidateRangeKind.Positive)]
public Int64 Count { get; set; } = Int64.MaxValue;
public long Count { get; set; } = long.MaxValue;

/// <summary>
/// Gets or sets offset of bytes to start reading the input stream from.
/// </summary>
[Parameter]
[ValidateRange(ValidateRangeKind.NonNegative)]
public Int64 Offset { get; set; }
public long Offset { get; set; }

/// <summary>
/// This parameter is no-op.
/// Gets or sets whether the file input should be swallowed as is. This parameter is no-op, deprecated.
/// </summary>
[Parameter(ParameterSetName = "ByInputObject", DontShow = true)]
[Obsolete("Raw parameter is deprecated.", true)]
Expand Down Expand Up @@ -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.
/// </summary>
/// <param name="path"></param>
/// <param name="literalPath"></param>
/// <param name="path">The file path to resolve.</param>
/// <param name="literalPath">The paths to process.</param>
/// <returns></returns>
private List<string> ResolvePaths(string[] path, bool literalPath)
{
Expand Down Expand Up @@ -145,10 +145,11 @@ private List<string> 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;
}
Expand All @@ -162,7 +163,7 @@ private List<string> ResolvePaths(string[] path, bool literalPath)
/// <summary>
/// Pass each valid path on to process its contents.
/// </summary>
/// <param name="pathsToProcess"></param>
/// <param name="pathsToProcess">The paths to process.</param>
private void ProcessPath(List<string> pathsToProcess)
{
foreach (string path in pathsToProcess)
Expand All @@ -173,9 +174,9 @@ private void ProcessPath(List<string> pathsToProcess)

/// <summary>
/// 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.
/// </summary>
/// <param name="path"></param>
/// <param name="path">The file path to retrieve content from for processing.</param>
private void ProcessFileContent(string path)
{
Span<byte> buffer = stackalloc byte[BUFFERSIZE];
Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -231,19 +232,19 @@ private void ProcessFileContent(string path)

/// <summary>
/// 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.
/// </summary>
/// <param name="inputObject"></param>
/// <param name="inputObject">The pipeline input object being processed.</param>
private void ProcessObjectContent(PSObject inputObject)
{
object obj = inputObject.BaseObject;

if (obj is System.IO.FileSystemInfo fsi)
{
string[] path = { fsi.FullName };
List<string> pathsToProcess = ResolvePaths(path, true);
ProcessPath(pathsToProcess);
return;
string[] path = { fsi.FullName };
List<string> pathsToProcess = ResolvePaths(path, true);
ProcessPath(pathsToProcess);
return;
}

byte[] inputBytes = ConvertToByteArray(obj);
Expand All @@ -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);
}
}

/// <summary>
/// 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.
/// </summary>
/// <param name="inputObject">The object to convert.</param>
/// <returns>Returns a byte array of the input values, or null if there is no available conversion path.</returns>
private byte[] ConvertToByteArray(object inputObject)
{
if (inputObject is string str)
Expand Down Expand Up @@ -356,20 +364,20 @@ private byte[] ConvertToByteArray(object inputObject)
#region Output

/// <summary>
/// Outputs the hexadecimial representaion of the input data.
/// Outputs the hexadecimial representation of the input data.
/// </summary>
/// <param name="inputBytes">Bytes for the hexadecimial representaion.</param>
/// <param name="inputBytes">Bytes for the hexadecimial representation.</param>
/// <param name="path">File path.</param>
/// <param name="offset">Offset in the file.</param>
private void WriteHexidecimal(Span<byte> inputBytes, string path, Int64 offset)
private void WriteHexadecimal(Span<byte> 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);
}

Expand Down