77
88use std:: ffi:: { OsStr , OsString } ;
99use std:: iter;
10- use std:: num:: ParseIntError ;
1110use std:: path:: Path ;
1211
1312use clap:: builder:: ValueParser ;
@@ -19,7 +18,10 @@ use uucore::checksum::compute::{
1918use uucore:: checksum:: validate:: {
2019 ChecksumValidateOptions , ChecksumVerbose , perform_checksum_validation,
2120} ;
22- use uucore:: checksum:: { AlgoKind , ChecksumError , SizedAlgoKind , calculate_blake2b_length_str} ;
21+ use uucore:: checksum:: {
22+ AlgoKind , ChecksumError , SizedAlgoKind , calculate_blake2b_length_str,
23+ sanitize_sha2_sha3_length_str,
24+ } ;
2325use uucore:: error:: UResult ;
2426use uucore:: line_ending:: LineEnding ;
2527use uucore:: { format_usage, translate} ;
@@ -74,9 +76,11 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<(AlgoKind, Optio
7476 set_or_err ( ( AlgoKind :: Blake3 , None ) ) ?;
7577 }
7678 if matches. get_flag ( "sha3" ) {
77- match matches. get_one :: < usize > ( "bits" ) {
78- Some ( bits @ ( 224 | 256 | 384 | 512 ) ) => set_or_err ( ( AlgoKind :: Sha3 , Some ( * bits) ) ) ?,
79- Some ( bits) => return Err ( ChecksumError :: InvalidLengthForSha ( bits. to_string ( ) ) . into ( ) ) ,
79+ match matches. get_one :: < String > ( options:: LENGTH ) {
80+ Some ( len) => set_or_err ( (
81+ AlgoKind :: Sha3 ,
82+ Some ( sanitize_sha2_sha3_length_str ( AlgoKind :: Sha3 , len) ?) ,
83+ ) ) ?,
8084 None => return Err ( ChecksumError :: LengthRequired ( "SHA3" . into ( ) ) . into ( ) ) ,
8185 }
8286 }
@@ -93,16 +97,10 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<(AlgoKind, Optio
9397 set_or_err ( ( AlgoKind :: Sha3 , Some ( 512 ) ) ) ?;
9498 }
9599 if matches. get_flag ( "shake128" ) {
96- match matches. get_one :: < usize > ( "bits" ) {
97- Some ( bits) => set_or_err ( ( AlgoKind :: Shake128 , Some ( * bits) ) ) ?,
98- None => return Err ( ChecksumError :: LengthRequired ( "SHAKE128" . into ( ) ) . into ( ) ) ,
99- }
100+ set_or_err ( ( AlgoKind :: Shake128 , Some ( 128 ) ) ) ?;
100101 }
101102 if matches. get_flag ( "shake256" ) {
102- match matches. get_one :: < usize > ( "bits" ) {
103- Some ( bits) => set_or_err ( ( AlgoKind :: Shake256 , Some ( * bits) ) ) ?,
104- None => return Err ( ChecksumError :: LengthRequired ( "SHAKE256" . into ( ) ) . into ( ) ) ,
105- }
103+ set_or_err ( ( AlgoKind :: Shake256 , Some ( 256 ) ) ) ?;
106104 }
107105
108106 if alg. is_none ( ) {
@@ -112,11 +110,6 @@ fn create_algorithm_from_flags(matches: &ArgMatches) -> UResult<(AlgoKind, Optio
112110 Ok ( alg. unwrap ( ) )
113111}
114112
115- // TODO: return custom error type
116- fn parse_bit_num ( arg : & str ) -> Result < usize , ParseIntError > {
117- arg. parse ( )
118- }
119-
120113#[ uucore:: main]
121114pub fn uumain ( mut args : impl uucore:: Args ) -> UResult < ( ) > {
122115 // if there is no program name for some reason, default to "hashsum"
@@ -139,17 +132,16 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
139132 // least somewhat better from a user's perspective.
140133 let matches = uucore:: clap_localization:: handle_clap_result ( command, args) ?;
141134
142- let input_length: Option < & String > = if binary_name == "b2sum" {
143- matches. get_one :: < String > ( options:: LENGTH )
135+ let length: Option < usize > = if binary_name == "b2sum" {
136+ if let Some ( len) = matches. get_one :: < String > ( options:: LENGTH ) {
137+ calculate_blake2b_length_str ( len) ?
138+ } else {
139+ None
140+ }
144141 } else {
145142 None
146143 } ;
147144
148- let length = match input_length {
149- Some ( length) => calculate_blake2b_length_str ( length) ?,
150- None => None ,
151- } ;
152-
153145 let ( algo_kind, length) = if is_hashsum_bin {
154146 create_algorithm_from_flags ( & matches) ?
155147 } else {
@@ -371,24 +363,8 @@ fn uu_app_opt_length(command: Command) -> Command {
371363 )
372364}
373365
374- pub fn uu_app_bits ( ) -> Command {
375- uu_app_opt_bits ( uu_app_common ( ) )
376- }
377-
378- fn uu_app_opt_bits ( command : Command ) -> Command {
379- // Needed for variable-length output sums (e.g. SHAKE)
380- command. arg (
381- Arg :: new ( "bits" )
382- . long ( "bits" )
383- . help ( translate ! ( "hashsum-help-bits" ) )
384- . value_name ( "BITS" )
385- // XXX: should we actually use validators? they're not particularly efficient
386- . value_parser ( parse_bit_num) ,
387- )
388- }
389-
390366pub fn uu_app_custom ( ) -> Command {
391- let mut command = uu_app_opt_bits ( uu_app_common ( ) ) ;
367+ let mut command = uu_app_opt_length ( uu_app_common ( ) ) ;
392368 let algorithms = & [
393369 ( "md5" , translate ! ( "hashsum-help-md5" ) ) ,
394370 ( "sha1" , translate ! ( "hashsum-help-sha1" ) ) ,
0 commit comments