@@ -1033,9 +1033,10 @@ pub mod netlink {
1033
1033
1034
1034
#[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
1035
1035
pub mod alg {
1036
- use libc:: { AF_ALG , sockaddr_alg} ;
1036
+ use libc:: { AF_ALG , sockaddr_alg, c_char } ;
1037
1037
use std:: { fmt, mem, str} ;
1038
1038
use std:: hash:: { Hash , Hasher } ;
1039
+ use std:: ffi:: CStr ;
1039
1040
1040
1041
#[ derive( Copy , Clone ) ]
1041
1042
pub struct AlgAddr ( pub sockaddr_alg ) ;
@@ -1058,13 +1059,6 @@ pub mod alg {
1058
1059
}
1059
1060
}
1060
1061
1061
- fn to_str ( bytes : & [ u8 ] ) -> & str {
1062
- let nul_position = bytes. iter ( )
1063
- . position ( |& c| c == b'\0' )
1064
- . unwrap_or ( bytes. len ( ) ) ;
1065
- unsafe { str:: from_utf8_unchecked ( & bytes[ 0 ..nul_position] ) }
1066
- }
1067
-
1068
1062
impl AlgAddr {
1069
1063
pub fn new ( alg_type : & str , alg_name : & str ) -> AlgAddr {
1070
1064
let mut addr: sockaddr_alg = unsafe { mem:: zeroed ( ) } ;
@@ -1076,20 +1070,20 @@ pub mod alg {
1076
1070
}
1077
1071
1078
1072
1079
- pub fn alg_type ( & self ) -> & str {
1080
- to_str ( & self . 0 . salg_type )
1073
+ pub fn alg_type ( & self ) -> & CStr {
1074
+ unsafe { CStr :: from_ptr ( self . 0 . salg_type . as_ptr ( ) as * const c_char ) }
1081
1075
}
1082
1076
1083
- pub fn alg_name ( & self ) -> & str {
1084
- to_str ( & self . 0 . salg_name )
1077
+ pub fn alg_name ( & self ) -> & CStr {
1078
+ unsafe { CStr :: from_ptr ( self . 0 . salg_name . as_ptr ( ) as * const c_char ) }
1085
1079
}
1086
1080
}
1087
1081
1088
1082
impl fmt:: Display for AlgAddr {
1089
1083
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1090
1084
write ! ( f, "type: {} alg: {}" ,
1091
- self . alg_name( ) ,
1092
- self . alg_type( ) )
1085
+ self . alg_name( ) . to_string_lossy ( ) ,
1086
+ self . alg_type( ) . to_string_lossy ( ) )
1093
1087
}
1094
1088
}
1095
1089
0 commit comments