| 1 |
use crate::error; |
| 2 |
|
| 3 |
// At time of writing, the biggest TZif data file is a few KB. And the |
| 4 |
// index block is tens of KB. So impose a limit that is a couple of orders |
| 5 |
// of magnitude bigger, but still overall pretty small for... some systems. |
| 6 |
// Anyway, I welcome improvements to this heuristic! |
| 7 |
pub(crate) const ALLOC_LIMIT: usize = 10 * 1 << 20; |
| 8 |
|
| 9 |
#[derive(Clone, Debug)] |
| 10 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 11 |
pub(crate) enum Error { |
| 12 |
AllocRequestOverLimit, |
| 13 |
AllocFailed, |
| 14 |
AllocOverflow, |
| 15 |
ExpectedFirstSixBytes, |
| 16 |
ExpectedIanaName, |
| 17 |
ExpectedLastByte, |
| 18 |
#[cfg(test)] |
| 19 |
ExpectedMoreData, |
| 20 |
ExpectedVersion, |
| 21 |
FailedReadData, |
| 22 |
FailedReadHeader, |
| 23 |
FailedReadIndex, |
| 24 |
#[cfg(all(feature = "std", all(not(unix), not(windows))))] |
| 25 |
FailedSeek, |
| 26 |
InvalidIndexDataOffsets, |
| 27 |
InvalidLengthIndexBlock, |
| 28 |
#[cfg(all(feature = "std", windows))] |
| 29 |
InvalidOffsetOverflowFile, |
| 30 |
#[cfg(test)] |
| 31 |
InvalidOffsetOverflowSlice, |
| 32 |
#[cfg(test)] |
| 33 |
InvalidOffsetTooBig, |
| 34 |
} |
| 35 |
|
| 36 |
impl From<Error> for error::Error { |
| 37 |
#[cold] |
| 38 |
#[inline(never)] |
| 39 |
fn from(err: Error) -> error::Error { |
| 40 |
error::ErrorKind::TzConcatenated(err).into() |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl error::IntoError for Error { |
| 45 |
fn into_error(self) -> error::Error { |
| 46 |
self.into() |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl core::fmt::Display for Error { |
| 51 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 52 |
use self::Error::*; |
| 53 |
|
| 54 |
match *self { |
| 55 |
AllocRequestOverLimit => write!( |
| 56 |
f, |
| 57 |
"attempted to allocate more than {ALLOC_LIMIT} bytes \ |
| 58 |
while reading concatenated TZif data, which \ |
| 59 |
exceeds a heuristic limit to prevent huge allocations \ |
| 60 |
(please file a bug if this error is inappropriate)", |
| 61 |
), |
| 62 |
AllocFailed => f.write_str( |
| 63 |
"failed to allocate additional room \ |
| 64 |
for reading concatenated TZif data", |
| 65 |
), |
| 66 |
AllocOverflow => { |
| 67 |
f.write_str("total allocation length overflowed `usize`") |
| 68 |
} |
| 69 |
ExpectedFirstSixBytes => f.write_str( |
| 70 |
"expected first 6 bytes of concatenated TZif header \ |
| 71 |
to be `tzdata`", |
| 72 |
), |
| 73 |
ExpectedIanaName => f.write_str( |
| 74 |
"expected IANA time zone identifier to be valid UTF-8", |
| 75 |
), |
| 76 |
ExpectedLastByte => f.write_str( |
| 77 |
"expected last byte of concatenated TZif header \ |
| 78 |
to be `NUL`", |
| 79 |
), |
| 80 |
#[cfg(test)] |
| 81 |
ExpectedMoreData => f.write_str( |
| 82 |
"unexpected EOF, expected more bytes based on size \ |
| 83 |
of caller provided buffer", |
| 84 |
), |
| 85 |
ExpectedVersion => f.write_str( |
| 86 |
"expected version in concatenated TZif header to \ |
| 87 |
be valid UTF-8", |
| 88 |
), |
| 89 |
FailedReadData => f.write_str("failed to read TZif data block"), |
| 90 |
FailedReadHeader => { |
| 91 |
f.write_str("failed to read concatenated TZif header") |
| 92 |
} |
| 93 |
FailedReadIndex => f.write_str("failed to read index block"), |
| 94 |
#[cfg(all(feature = "std", all(not(unix), not(windows))))] |
| 95 |
FailedSeek => { |
| 96 |
f.write_str("failed to seek to offset in `std::fs::File`") |
| 97 |
} |
| 98 |
InvalidIndexDataOffsets => f.write_str( |
| 99 |
"invalid index and data offsets, \ |
| 100 |
expected index offset to be less than or equal \ |
| 101 |
to data offset", |
| 102 |
), |
| 103 |
InvalidLengthIndexBlock => { |
| 104 |
f.write_str("length of index block is not a valid multiple") |
| 105 |
} |
| 106 |
#[cfg(all(feature = "std", windows))] |
| 107 |
InvalidOffsetOverflowFile => f.write_str( |
| 108 |
"offset overflow when reading from `std::fs::File`", |
| 109 |
), |
| 110 |
#[cfg(test)] |
| 111 |
InvalidOffsetOverflowSlice => { |
| 112 |
f.write_str("offset overflowed `usize`") |
| 113 |
} |
| 114 |
#[cfg(test)] |
| 115 |
InvalidOffsetTooBig => { |
| 116 |
f.write_str("offset too big for given slice of data") |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |