| 1 |
//! Logic was mainly ported from https://android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/libcore/util/ZoneInfoDB.java |
| 2 |
|
| 3 |
use core::{cmp::Ordering, convert::TryInto}; |
| 4 |
use std::{ |
| 5 |
fs::File, |
| 6 |
io::{self, ErrorKind, Read, Seek, SeekFrom}, |
| 7 |
}; |
| 8 |
|
| 9 |
// The database uses 32-bit (4 byte) integers. |
| 10 |
const TZ_INT_SIZE: usize = 4; |
| 11 |
// The first 12 bytes contain a special version string. |
| 12 |
const MAGIC_SIZE: usize = 12; |
| 13 |
const HEADER_SIZE: usize = MAGIC_SIZE + 3 * TZ_INT_SIZE; |
| 14 |
// The database reserves 40 bytes for each id. |
| 15 |
const TZ_NAME_SIZE: usize = 40; |
| 16 |
const INDEX_ENTRY_SIZE: usize = TZ_NAME_SIZE + 3 * TZ_INT_SIZE; |
| 17 |
const TZDATA_LOCATIONS: [TzdataLocation; 2] = [ |
| 18 |
TzdataLocation { |
| 19 |
env_var: "ANDROID_DATA", |
| 20 |
path: "/misc/zoneinfo/", |
| 21 |
}, |
| 22 |
TzdataLocation { |
| 23 |
env_var: "ANDROID_ROOT", |
| 24 |
path: "/usr/share/zoneinfo/", |
| 25 |
}, |
| 26 |
]; |
| 27 |
|
| 28 |
#[derive(Debug)] |
| 29 |
struct TzdataLocation { |
| 30 |
env_var: &'static str, |
| 31 |
path: &'static str, |
| 32 |
} |
| 33 |
|
| 34 |
#[derive(Debug, Clone, Copy)] |
| 35 |
struct Header { |
| 36 |
index_offset: usize, |
| 37 |
data_offset: usize, |
| 38 |
_zonetab_offset: usize, |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Debug)] |
| 42 |
struct Index(Vec<u8>); |
| 43 |
|
| 44 |
#[derive(Debug, Clone, Copy)] |
| 45 |
struct IndexEntry<'a> { |
| 46 |
_name: &'a [u8], |
| 47 |
offset: usize, |
| 48 |
length: usize, |
| 49 |
_raw_utc_offset: usize, |
| 50 |
} |
| 51 |
|
| 52 |
pub(super) fn find_file() -> Result<File, io::Error> { |
| 53 |
for location in &TZDATA_LOCATIONS { |
| 54 |
if let Ok(env_value) = std::env::var(location.env_var) { |
| 55 |
if let Ok(file) = File::open(format!("{}{}tzdata", env_value, location.path)) { |
| 56 |
return Ok(file); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
Err(io::Error::from(io::ErrorKind::NotFound)) |
| 61 |
} |
| 62 |
|
| 63 |
pub(super) fn find_tz_data_in_file( |
| 64 |
mut file: impl Read + Seek, |
| 65 |
tz_name: &str, |
| 66 |
) -> Result<Vec<u8>, io::Error> { |
| 67 |
let header = Header::new(&mut file)?; |
| 68 |
let index = Index::new(&mut file, header)?; |
| 69 |
if let Some(entry) = index.find_entry(tz_name) { |
| 70 |
file.seek(SeekFrom::Start((entry.offset + header.data_offset) as u64))?; |
| 71 |
let mut tz_data = vec![0u8; entry.length]; |
| 72 |
file.read_exact(&mut tz_data)?; |
| 73 |
Ok(tz_data) |
| 74 |
} else { |
| 75 |
Err(io::Error::from(ErrorKind::NotFound)) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
impl Header { |
| 80 |
fn new(mut file: impl Read + Seek) -> Result<Self, io::Error> { |
| 81 |
let mut buf = [0; HEADER_SIZE]; |
| 82 |
file.read_exact(&mut buf)?; |
| 83 |
if !buf.starts_with(b"tzdata") || buf[MAGIC_SIZE - 1] != 0u8 { |
| 84 |
return Err(io::Error::new( |
| 85 |
io::ErrorKind::InvalidData, |
| 86 |
"invalid magic number", |
| 87 |
)); |
| 88 |
} |
| 89 |
Ok(Self { |
| 90 |
index_offset: parse_tz_int(&buf, MAGIC_SIZE) as usize, |
| 91 |
data_offset: parse_tz_int(&buf, MAGIC_SIZE + TZ_INT_SIZE) as usize, |
| 92 |
_zonetab_offset: parse_tz_int(&buf, MAGIC_SIZE + 2 * TZ_INT_SIZE) as usize, |
| 93 |
}) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl Index { |
| 98 |
fn new(mut file: impl Read + Seek, header: Header) -> Result<Self, io::Error> { |
| 99 |
file.seek(SeekFrom::Start(header.index_offset as u64))?; |
| 100 |
let size = header.data_offset - header.index_offset; |
| 101 |
let mut bytes = vec![0; size]; |
| 102 |
file.read_exact(&mut bytes)?; |
| 103 |
Ok(Self(bytes)) |
| 104 |
} |
| 105 |
|
| 106 |
fn find_entry(&self, name: &str) -> Option<IndexEntry> { |
| 107 |
let name_bytes = name.as_bytes(); |
| 108 |
let name_len = name_bytes.len(); |
| 109 |
if name_len > TZ_NAME_SIZE { |
| 110 |
return None; |
| 111 |
} |
| 112 |
|
| 113 |
let zeros = [0u8; TZ_NAME_SIZE]; |
| 114 |
let cmp = |chunk: &&[u8]| -> Ordering { |
| 115 |
// tz names always have TZ_NAME_SIZE bytes and are right-padded with 0s |
| 116 |
// so we check that a chunk starts with `name` and the remaining bytes are 0 |
| 117 |
chunk[..name_len] |
| 118 |
.cmp(name_bytes) |
| 119 |
.then_with(|| chunk[name_len..TZ_NAME_SIZE].cmp(&zeros[name_len..])) |
| 120 |
}; |
| 121 |
|
| 122 |
let chunks: Vec<_> = self.0.chunks_exact(INDEX_ENTRY_SIZE).collect(); |
| 123 |
chunks |
| 124 |
.binary_search_by(cmp) |
| 125 |
.map(|idx| IndexEntry::new(chunks[idx])) |
| 126 |
.ok() |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl<'a> IndexEntry<'a> { |
| 131 |
fn new(bytes: &'a [u8]) -> Self { |
| 132 |
Self { |
| 133 |
_name: bytes[..TZ_NAME_SIZE] |
| 134 |
.splitn(2, |&b| b == 0u8) |
| 135 |
.next() |
| 136 |
.unwrap(), |
| 137 |
offset: parse_tz_int(bytes, TZ_NAME_SIZE) as usize, |
| 138 |
length: parse_tz_int(bytes, TZ_NAME_SIZE + TZ_INT_SIZE) as usize, |
| 139 |
_raw_utc_offset: parse_tz_int(bytes, TZ_NAME_SIZE + 2 * TZ_INT_SIZE) as usize, |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/// Panics if slice does not contain [TZ_INT_SIZE] bytes beginning at start. |
| 145 |
fn parse_tz_int(slice: &[u8], start: usize) -> u32 { |
| 146 |
u32::from_be_bytes(slice[start..start + TZ_INT_SIZE].try_into().unwrap()) |
| 147 |
} |
| 148 |
|
| 149 |
#[cfg(test)] |
| 150 |
mod test { |
| 151 |
use super::*; |
| 152 |
use std::fs::File; |
| 153 |
use std::io::Cursor; |
| 154 |
|
| 155 |
#[test] |
| 156 |
fn parse() { |
| 157 |
let mut archive = File::open("tests/resources/tzdata.zip").unwrap(); |
| 158 |
let mut zip = zip::ZipArchive::new(&mut archive).unwrap(); |
| 159 |
let mut file = zip.by_index(0).unwrap(); |
| 160 |
let mut data = Vec::new(); |
| 161 |
file.read_to_end(&mut data).unwrap(); |
| 162 |
let cursor = Cursor::new(data); |
| 163 |
let tz = find_tz_data_in_file(cursor, "Europe/Kiev").unwrap(); |
| 164 |
assert!(tz.starts_with(b"TZif2")); |
| 165 |
} |
| 166 |
} |