-
Notifications
You must be signed in to change notification settings - Fork 33
feat: Support multi-format integer parsing for CLI arguments #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+83
−65
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // This file contains custom u32/u64 parsers for supporting decimal, hexadecimal and binary formats. | ||
|
|
||
| use std::num::ParseIntError; | ||
|
|
||
| pub fn parse_int_auto_radix<T>(s: &str) -> Result<T, ParseIntError> | ||
| where | ||
| T: FromStrRadix, | ||
| { | ||
| if let Some(hex) = s.strip_prefix("0x") { | ||
| T::from_str_radix(hex, 16) | ||
| } else if let Some(bin) = s.strip_prefix("0b") { | ||
| T::from_str_radix(bin, 2) | ||
| } else { | ||
| T::from_str_radix(s, 10) | ||
| } | ||
| } | ||
|
|
||
| pub trait FromStrRadix: Sized { | ||
| fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>; | ||
| } | ||
|
|
||
| impl FromStrRadix for u32 { | ||
| fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> { | ||
| u32::from_str_radix(src, radix) | ||
| } | ||
| } | ||
|
|
||
| impl FromStrRadix for u64 { | ||
| fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> { | ||
| u64::from_str_radix(src, radix) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ mod preattestation; | |
| mod report; | ||
| mod verify; | ||
|
|
||
| mod clparser; | ||
|
|
||
| #[cfg(feature = "hyperv")] | ||
| mod hyperv; | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only other format that we might need to consider is Base64. Qemu takes in a lot of base 64 encoded values. That is why in some of the other commands we support that format. Besides that everything else looks good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! I agree that support for Base64 is worthwhile. The main challenge is that it is not possible to unambiguously detect Base64 purely from the input string (e.g., a hex literal like
0x00can also be parsed as valid Base64 bits:0x00→110100 110001 110100 110100).One approach I would propose is to assign an appropriate default format for each argument type (e.g., binary for bitfields, decimal for plain integers, hex for general byte strings) and then allow overriding via explicit flags such as
--lmv-format base64or--gfs-format hex. This way we can consistently support decimal, hex, binary, Base64 (and potentially Base64URL).In the case of the
keysubcommand, the default formats are set as follows:--vmpl: decimal--guest_field_select: binary--launch_mit_vector: hexDoes that approach make sense to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A point @tylerfanelli brought up is that maybe user's won't be inputting Base64 as much as they would require the output, so maybe we support Base64 outputs, but not necessarily inputs.
@tylerfanelli any thoughts on this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i suggest that because of different padding formats, etc, its best to just have users input hex. If QEMU depends on base64 inputs, then this tool's outputs can be in base64. But I think it's best to standardize around hex (and thus not needing a
0xprefix to inputs) and not worry about different encoding formats.