| 1 |
//! Extended Timestamp extra field |
| 2 |
//! Defined in <https://libzip.org/specifications/extrafld.txt> |
| 3 |
|
| 4 |
use crate::result::invalid; |
| 5 |
use crate::result::{ZipError, ZipResult}; |
| 6 |
use crate::unstable::LittleEndianReadExt; |
| 7 |
use core::mem; |
| 8 |
use std::io::Read; |
| 9 |
|
| 10 |
/// `ExtendedTimestamp` Flags |
| 11 |
#[rustfmt::skip] |
| 12 |
#[repr(u8)] |
| 13 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 14 |
enum ExtendedTimestampFlags { |
| 15 |
/// modification time is present |
| 16 |
Modified = 0b0000_0001_u8, |
| 17 |
/// access time is present |
| 18 |
Accessed = 0b0000_0010_u8, |
| 19 |
/// creation time is present |
| 20 |
Created = 0b0000_0100_u8, |
| 21 |
// others bytes reversed |
| 22 |
} |
| 23 |
|
| 24 |
impl ExtendedTimestampFlags { |
| 25 |
#[inline(always)] |
| 26 |
fn matching(flags: u8, matching_flag: Self) -> bool { |
| 27 |
flags & u8::from(matching_flag) != 0 |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl From<ExtendedTimestampFlags> for u8 { |
| 32 |
fn from(extended_timestamp: ExtendedTimestampFlags) -> u8 { |
| 33 |
extended_timestamp as u8 |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/// Extended timestamp, as described in <https://libzip.org/specifications/extrafld.txt> |
| 38 |
#[derive(Debug, Clone)] |
| 39 |
pub struct ExtendedTimestamp { |
| 40 |
modified: Option<u32>, |
| 41 |
accessed: Option<u32>, |
| 42 |
created: Option<u32>, |
| 43 |
} |
| 44 |
|
| 45 |
impl ExtendedTimestamp { |
| 46 |
const MAX_LENGTH: u16 = (mem::size_of::<u8>() |
| 47 |
+ mem::size_of::<u32>() |
| 48 |
+ mem::size_of::<u32>() |
| 49 |
+ mem::size_of::<u32>()) as u16; |
| 50 |
/// creates an extended timestamp struct by reading the required bytes from the reader. |
| 51 |
/// |
| 52 |
/// This method assumes that the length has already been read, therefore |
| 53 |
/// it must be passed as an argument |
| 54 |
/// |
| 55 |
/// # Errors |
| 56 |
/// |
| 57 |
/// Fails if the len is empty or a read fails |
| 58 |
/// |
| 59 |
pub fn try_from_reader<R>(reader: &mut R, len: u16) -> ZipResult<Self> |
| 60 |
where |
| 61 |
R: Read, |
| 62 |
{ |
| 63 |
if len == 0 { |
| 64 |
return Err(invalid!("Extended timestamp field is empty")); |
| 65 |
} |
| 66 |
let mut flags = [0u8]; |
| 67 |
let mut bytes_to_read = len as usize; |
| 68 |
reader.read_exact(&mut flags)?; |
| 69 |
bytes_to_read = bytes_to_read |
| 70 |
.checked_sub(flags.len()) |
| 71 |
.ok_or(invalid!("Extended timestamp field too short for flags"))?; |
| 72 |
let flags = flags[0]; |
| 73 |
|
| 74 |
// the `flags` field refers to the local headers and might not correspond |
| 75 |
// to the len field. If the length field is 1+4, we assume that only |
| 76 |
// the modification time has been set |
| 77 |
|
| 78 |
// > Those times that are present will appear in the order indicated, but |
| 79 |
// > any combination of times may be omitted. (Creation time may be |
| 80 |
// > present without access time, for example.) TSize should equal |
| 81 |
// > (1 + 4*(number of set bits in Flags)), as the block is currently |
| 82 |
// > defined. |
| 83 |
if len != 5 && u32::from(len) != 1 + 4 * flags.count_ones() { |
| 84 |
return Err(ZipError::Io(std::io::Error::other(format!( |
| 85 |
"flags and len don't match in extended timestamp field len={len} flags={flags:08b}" |
| 86 |
)))); |
| 87 |
} |
| 88 |
|
| 89 |
let modified = |
| 90 |
if (ExtendedTimestampFlags::matching(flags, ExtendedTimestampFlags::Modified) |
| 91 |
&& bytes_to_read >= mem::size_of::<u32>()) |
| 92 |
|| len == Self::MAX_LENGTH |
| 93 |
{ |
| 94 |
bytes_to_read = bytes_to_read.checked_sub(mem::size_of::<u32>()).ok_or( |
| 95 |
invalid!( |
| 96 |
"Extended timestamp field too short for mod_time len={} flags={flags:08b}", |
| 97 |
len |
| 98 |
), |
| 99 |
)?; |
| 100 |
Some(reader.read_u32_le()?) |
| 101 |
} else { |
| 102 |
None |
| 103 |
}; |
| 104 |
|
| 105 |
let accessed = |
| 106 |
if (ExtendedTimestampFlags::matching(flags, ExtendedTimestampFlags::Accessed) |
| 107 |
&& bytes_to_read >= mem::size_of::<u32>()) |
| 108 |
|| len == Self::MAX_LENGTH |
| 109 |
{ |
| 110 |
bytes_to_read = bytes_to_read.checked_sub(mem::size_of::<u32>()).ok_or( |
| 111 |
invalid!( |
| 112 |
"Extended timestamp field too short for ac_time len={} flags={flags:08b}", |
| 113 |
len |
| 114 |
), |
| 115 |
)?; |
| 116 |
Some(reader.read_u32_le()?) |
| 117 |
} else { |
| 118 |
None |
| 119 |
}; |
| 120 |
|
| 121 |
let created = if (ExtendedTimestampFlags::matching(flags, ExtendedTimestampFlags::Created) |
| 122 |
&& bytes_to_read >= mem::size_of::<u32>()) |
| 123 |
|| len == Self::MAX_LENGTH |
| 124 |
{ |
| 125 |
bytes_to_read = bytes_to_read |
| 126 |
.checked_sub(mem::size_of::<u32>()) |
| 127 |
.ok_or(invalid!( |
| 128 |
"Extended timestamp field too short for cr_time len={} flags={flags:08b}", |
| 129 |
len |
| 130 |
))?; |
| 131 |
Some(reader.read_u32_le()?) |
| 132 |
} else { |
| 133 |
None |
| 134 |
}; |
| 135 |
|
| 136 |
if bytes_to_read > 0 { |
| 137 |
// ignore undocumented bytes |
| 138 |
reader.read_exact(&mut vec![0; bytes_to_read])?; |
| 139 |
} |
| 140 |
|
| 141 |
Ok(Self { |
| 142 |
modified, |
| 143 |
accessed, |
| 144 |
created, |
| 145 |
}) |
| 146 |
} |
| 147 |
|
| 148 |
/// returns the last modification timestamp, if defined, as UNIX epoch seconds |
| 149 |
#[must_use] |
| 150 |
pub fn mod_time(&self) -> Option<u32> { |
| 151 |
self.modified |
| 152 |
} |
| 153 |
|
| 154 |
/// returns the last access timestamp, if defined, as UNIX epoch seconds |
| 155 |
#[must_use] |
| 156 |
pub fn ac_time(&self) -> Option<u32> { |
| 157 |
self.accessed |
| 158 |
} |
| 159 |
|
| 160 |
/// returns the creation timestamp, if defined, as UNIX epoch seconds |
| 161 |
#[must_use] |
| 162 |
pub fn cr_time(&self) -> Option<u32> { |
| 163 |
self.created |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
#[cfg(test)] |
| 168 |
mod tests { |
| 169 |
use super::ExtendedTimestamp; |
| 170 |
|
| 171 |
use std::io::Cursor; |
| 172 |
|
| 173 |
/// Ensure we don't panic or read garbage data if the field body is empty |
| 174 |
#[test] |
| 175 |
pub fn test_bad_extended_timestamp() { |
| 176 |
use crate::ZipArchive; |
| 177 |
|
| 178 |
assert!( |
| 179 |
ZipArchive::new(Cursor::new(include_bytes!( |
| 180 |
"../../tests/data/extended_timestamp_bad.zip" |
| 181 |
))) |
| 182 |
.is_err() |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
/// Ensure that a truncated extended timestamp (len too short for flags) |
| 187 |
/// returns an error instead of panicking from a subtraction overflow. |
| 188 |
#[test] |
| 189 |
fn test_extended_timestamp_overflow() { |
| 190 |
let tests_args = [ |
| 191 |
(vec![0b0000_0001_u8, 0x00, 0x00, 0x00], 0), // len is 0 |
| 192 |
(vec![0b0000_0001_u8, 0x00, 0x00, 0x00], 2), // len is 2 (not enough) |
| 193 |
(vec![0b0000_0010_u8, 0x00, 0x00, 0x00], 3), // len is 3 (not enough) |
| 194 |
(vec![0b0000_0100_u8, 0x00, 0x00, 0x00], 4), // len is 4 (not enough) |
| 195 |
// len is 8 (not enough) |
| 196 |
( |
| 197 |
vec![ |
| 198 |
0b0000_0011_u8, |
| 199 |
0x00, |
| 200 |
0x00, |
| 201 |
0x00, |
| 202 |
0x00, |
| 203 |
0x00, |
| 204 |
0x00, |
| 205 |
0x00, |
| 206 |
0x00, |
| 207 |
], |
| 208 |
8, |
| 209 |
), |
| 210 |
// len is too long and missing 4 bytes |
| 211 |
(vec![0b0000_0011_u8, 0x00, 0x00, 0x00, 0x00], 8), |
| 212 |
(vec![0b0000_0000_u8], 5), // too long len |
| 213 |
]; |
| 214 |
for (data, len) in tests_args { |
| 215 |
let mut cursor = Cursor::new(data); |
| 216 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, len); |
| 217 |
assert!(result.is_err()); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn check_extended_timestamp_value() { |
| 223 |
let mut cursor = Cursor::new(&[0b0000_0001_u8, 0x00, 0x00, 0x00, 0x01]); |
| 224 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, 5).unwrap(); |
| 225 |
assert_eq!(result.mod_time(), Some(16777216)); |
| 226 |
assert_eq!(result.ac_time(), None); |
| 227 |
assert_eq!(result.cr_time(), None); |
| 228 |
|
| 229 |
let mut cursor = Cursor::new(&[0b0000_0010_u8, 0x00, 0x00, 0x00, 0x02]); |
| 230 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, 5).unwrap(); |
| 231 |
assert_eq!(result.mod_time(), None); |
| 232 |
assert_eq!(result.ac_time(), Some(33554432)); |
| 233 |
assert_eq!(result.cr_time(), None); |
| 234 |
|
| 235 |
let mut cursor = Cursor::new(&[0b0000_0100_u8, 0x00, 0x00, 0x00, 0x03]); |
| 236 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, 5).unwrap(); |
| 237 |
assert_eq!(result.mod_time(), None); |
| 238 |
assert_eq!(result.ac_time(), None); |
| 239 |
assert_eq!(result.cr_time(), Some(50331648)); |
| 240 |
|
| 241 |
let mut cursor = Cursor::new(&[ |
| 242 |
0b0000_0011_u8, |
| 243 |
0x00, |
| 244 |
0x00, |
| 245 |
0x00, |
| 246 |
0x01, |
| 247 |
0x00, |
| 248 |
0x00, |
| 249 |
0x00, |
| 250 |
0x02, |
| 251 |
]); |
| 252 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, 9).unwrap(); |
| 253 |
assert_eq!(result.mod_time(), Some(16777216)); |
| 254 |
assert_eq!(result.ac_time(), Some(33554432)); |
| 255 |
assert_eq!(result.cr_time(), None); |
| 256 |
|
| 257 |
let mut cursor = Cursor::new(&[ |
| 258 |
0b0000_0111_u8, |
| 259 |
0x00, |
| 260 |
0x00, |
| 261 |
0x00, |
| 262 |
0x01, |
| 263 |
0x00, |
| 264 |
0x00, |
| 265 |
0x00, |
| 266 |
0x02, |
| 267 |
0x00, |
| 268 |
0x00, |
| 269 |
0x00, |
| 270 |
0x03, |
| 271 |
]); |
| 272 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, 13).unwrap(); |
| 273 |
assert_eq!(result.mod_time(), Some(16777216)); |
| 274 |
assert_eq!(result.ac_time(), Some(33554432)); |
| 275 |
assert_eq!(result.cr_time(), Some(50331648)); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn test_extended_timestamp() { |
| 280 |
// in the central header |
| 281 |
let mut cursor = Cursor::new(&[0b0000_0111_u8, 0x00, 0x00, 0x00, 0x01]); |
| 282 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, 5).unwrap(); |
| 283 |
assert_eq!(result.mod_time(), Some(16777216)); |
| 284 |
assert_eq!(result.ac_time(), None); |
| 285 |
assert_eq!(result.cr_time(), None); |
| 286 |
|
| 287 |
// in the local header |
| 288 |
let mut cursor = Cursor::new(&[ |
| 289 |
0b0000_0111_u8, |
| 290 |
0x00, |
| 291 |
0x00, |
| 292 |
0x00, |
| 293 |
0x01, |
| 294 |
0x00, |
| 295 |
0x00, |
| 296 |
0x00, |
| 297 |
0x02, |
| 298 |
0x00, |
| 299 |
0x00, |
| 300 |
0x00, |
| 301 |
0x03, |
| 302 |
]); |
| 303 |
let result = ExtendedTimestamp::try_from_reader(&mut cursor, 13).unwrap(); |
| 304 |
assert_eq!(result.mod_time(), Some(16777216)); |
| 305 |
assert_eq!(result.ac_time(), Some(33554432)); |
| 306 |
assert_eq!(result.cr_time(), Some(50331648)); |
| 307 |
} |
| 308 |
} |