@@ -5,7 +5,7 @@ use std::{
5
5
ffi:: CStr ,
6
6
fmt:: Debug ,
7
7
fs:: File ,
8
- io:: { Error , Read , Result , Seek , SeekFrom } ,
8
+ io:: { Error , ErrorKind , Read , Result , Seek , SeekFrom } ,
9
9
} ;
10
10
11
11
/// Get timezone data from the `tzdata` file of HarmonyOS NEXT.
@@ -34,7 +34,7 @@ fn open_android_tz_data_file() -> Result<File> {
34
34
}
35
35
}
36
36
}
37
- Err ( std :: io :: Error :: from ( std :: io :: ErrorKind :: NotFound ) )
37
+ Err ( Error :: from ( ErrorKind :: NotFound ) )
38
38
}
39
39
40
40
/// Get timezone data from the `tzdata` file reader of HarmonyOS NEXT.
@@ -77,7 +77,7 @@ impl TzDataHeader {
77
77
let mut magic = [ 0 ; TZDATA_VERSION_SIZE ] ;
78
78
data. read_exact ( & mut magic) ?;
79
79
if !magic. starts_with ( b"tzdata" ) || magic[ TZDATA_VERSION_SIZE - 1 ] != 0 {
80
- return Err ( Error :: other ( "invalid tzdata header magic" ) ) ;
80
+ return Err ( Error :: new ( ErrorKind :: Other , "invalid tzdata header magic" ) ) ;
81
81
}
82
82
let mut version = [ 0 ; 5 ] ;
83
83
version. copy_from_slice ( & magic[ 6 ..11 ] ) ;
@@ -118,7 +118,7 @@ impl TzDataIndexes {
118
118
indexes : buf
119
119
. chunks ( INDEX_SIZE )
120
120
. filter_map ( |chunk| {
121
- if let Ok ( name) = CStr :: from_bytes_until_nul ( & chunk[ ..SIZEOF_TZNAME ] ) {
121
+ if let Some ( name) = from_bytes_until_nul ( & chunk[ ..SIZEOF_TZNAME ] ) {
122
122
let name = name. to_bytes ( ) . to_vec ( ) . into_boxed_slice ( ) ;
123
123
let offset = u32:: from_be_bytes (
124
124
chunk[ SIZEOF_TZNAME ..SIZEOF_TZNAME + 4 ] . try_into ( ) . unwrap ( ) ,
@@ -155,6 +155,15 @@ impl TzDataIndexes {
155
155
}
156
156
}
157
157
158
+ /// Change this `CStr::from_bytes_until_nul` once MSRV was bumped above 1.72.0
159
+ fn from_bytes_until_nul ( bytes : & [ u8 ] ) -> Option < & CStr > {
160
+ let nul_pos = bytes. iter ( ) . position ( |& b| b == 0 ) ?;
161
+ // SAFETY:
162
+ // 1. nul_pos + 1 <= bytes.len()
163
+ // 2. We know there is a nul byte at nul_pos, so this slice (ending at the nul byte) is a well-formed C string.
164
+ Some ( unsafe { CStr :: from_bytes_with_nul_unchecked ( & bytes[ 0 ..=nul_pos] ) } )
165
+ }
166
+
158
167
/// Ohos tzdata index entry size: `name + offset + length`
159
168
#[ cfg( any( test, target_env = "ohos" ) ) ]
160
169
const SIZEOF_INDEX_OHOS : usize = SIZEOF_TZNAME + 2 * size_of :: < u32 > ( ) ;
0 commit comments