Skip to content

Add new Offset and Count parameters to Format-Hex and refactor the cmdlet - #7877

Merged
Ilya (iSazonov) merged 6 commits into
PowerShell:masterfrom
iSazonov:format-hex-offset
Oct 18, 2018
Merged

Add new Offset and Count parameters to Format-Hex and refactor the cmdlet#7877
Ilya (iSazonov) merged 6 commits into
PowerShell:masterfrom
iSazonov:format-hex-offset

Conversation

@iSazonov

@iSazonov Ilya (iSazonov) commented Sep 27, 2018

Copy link
Copy Markdown
Collaborator

PR Summary

  1. Add new Offset and Count parameters. Resolve Format-Hex should have Count and Offset parameters #3383.
  2. Modify ByteCollection class to support offsets up to UInt64.MaxValue size.
  3. Hide/obsolete Raw parameter because the behavior is by default. Related Can we hide obsolete or internal-use-only helper parameters from syntax diagrams? #7868.
  4. Optimize conversion to bytes to reduce allocations for large input data.

Additional comments

  1. For review: Now Offset parameter does a shift in output byte stream. Since we accept objects as input we could consider Offset as "skip the Offset-count objects from input (array/pipeline)" although Skip parameter is more suitable for this (or Select-Object -Skip) and this is contrary to the behavior for input streams (Path parameter or FileInfo input object).

  2. I don't consider a scenario "get last count bytes". As a minimum, the PR should not block this enhancement.

  3. New ReFS file system support files up to UInt64.MaxValue. So ByteCollection is modified to support UInt64.MaxValue too.
    On the other hand current .Net Core limitation is Int64.MaxValue for file size/offset. In result we have to use Int64 type for Offset and Count parameters.

/cc Keith Hill (@rkeithhill) Michael Klement (@mklement0) Adam Driscoll (@adamdriscoll)

PR Checklist

@iSazonov Ilya (iSazonov) added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Sep 27, 2018
@iSazonov Ilya (iSazonov) added the Documentation Needed in this repo Documentation is needed in this repo label Sep 27, 2018

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant from at the end of the sentence.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@rkeithhill Keith Hill (rkeithhill) Sep 28, 2018

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@iSazonov Ilya (iSazonov) changed the title WIP: Add new Offset and Count parameters to Format-Hex and refactor the cmdlet Add new Offset and Count parameters to Format-Hex and refactor the cmdlet Sep 28, 2018
@SteveL-MSFT Steve Lee (SteveL-MSFT) added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Oct 3, 2018
@SteveL-MSFT

Copy link
Copy Markdown
Member

@PowerShell/powershell-committee reviewed this and ok with the proposed changes

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Doc Issue created.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not a breaking change?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PowerShell/powershell-committee discussed this and formatting changes have not been considered breaking changes. The extra zeros are acceptable.

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) Travis Plunk (@TravisEz13) Could you please review the PR?

case Int32 iInt32:
inputBytes = BitConverter.GetBytes(iInt32);
break;
case Int32[] inputInt32s:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: variable naming is inconsistent with line 257, perhaps just use i32 and i32s? Same below for int64

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

/// <param name="inputBytes">Bytes for the hexadecimial representaion.</param>
/// <param name="path">File path.</param>
/// <param name="offset">Offset in the file.</param>
private void ConvertToHexidecimal(Span<byte> inputBytes, string path, Int64 offset)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this method does the actual WriteObject(), perhaps rename as WriteHexidecimal()?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

StringBuilder asciiEnd = new StringBuilder(BytesPerLine);

// '+1' comes from 'result.Append(nextLine.ToString() + " " + asciiEnd.ToString());' below.
StringBuilder result = new StringBuilder(nextLine.Capacity+asciiEnd.Capacity+1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: have whitespace between operators: nextLine.Capacity + asciiEnd.Capacity + 1

@iSazonov Ilya (iSazonov) Oct 15, 2018

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

{ Format-Hex -Path $inputFile4 -Count 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.FormatHex"
}

It "Offset should be >= 0" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be interesting to read from end of file, but is out of scope of this PR :)

@SteveL-MSFT Steve Lee (SteveL-MSFT) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@iSazonov
Ilya (iSazonov) merged commit 745e305 into PowerShell:master Oct 18, 2018
@iSazonov
Ilya (iSazonov) deleted the format-hex-offset branch October 18, 2018 05:04
@iSazonov Ilya (iSazonov) mentioned this pull request Oct 19, 2018
11 tasks
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
…dlet (PowerShell#7877)

- Add new Offset and Count parameters.
- Modify ByteCollection class to support offsets up to UInt64.MaxValue size.
- Hide/obsolete Raw parameter because the behavior is by default.
- Optimize conversion to bytes to reduce allocations for large input data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Committee-Reviewed PS-Committee has reviewed this and made a decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Format-Hex should have Count and Offset parameters

5 participants