| 1 | |
| 2 | //! Specialized binary input and output. |
| 3 | //! Uses the error handling for this crate. |
| 4 | |
| 5 | #![doc(hidden)] |
| 6 | pub use ::std::io::{Read, Write}; |
| 7 | |
| 8 | use half::slice::{HalfFloatSliceExt}; |
| 9 | use lebe::prelude::*; |
| 10 | use ::half::f16; |
| 11 | use crate::error::{Error, Result, UnitResult, IoResult}; |
| 12 | use std::io::{Seek, SeekFrom}; |
| 13 | use std::path::Path; |
| 14 | use std::fs::File; |
| 15 | use std::convert::TryFrom; |
| 16 | use smallvec::{Array, SmallVec}; |
| 17 | |
| 18 | /// Skip reading uninteresting bytes without allocating. |
| 19 | #[inline] |
| 20 | pub fn skip_bytes(read: &mut impl Read, count: usize) -> IoResult<()> { |
| 21 | let count = u64::try_from(count).unwrap(); |
| 22 | |
| 23 | let skipped = std::io::copy( |
| 24 | &mut read.by_ref().take(count), |
| 25 | &mut std::io::sink() |
| 26 | )?; |
| 27 | |
| 28 | // the reader may have ended before we skipped the desired number of bytes |
| 29 | if skipped < count { |
| 30 | return Err(std::io::Error::new( |
| 31 | std::io::ErrorKind::UnexpectedEof, |
| 32 | "cannot skip more bytes than exist" |
| 33 | )); |
| 34 | } |
| 35 | |
| 36 | debug_assert_eq!(skipped, count, "skip bytes bug"); |
| 37 | Ok(()) |
| 38 | } |
| 39 | |
| 40 | /// If an error occurs while writing, attempts to delete the partially written file. |
| 41 | /// Creates a file just before the first write operation, not when this function is called. |
| 42 | #[inline] |
| 43 | pub fn attempt_delete_file_on_write_error<'p>(path: &'p Path, write: impl FnOnce(LateFile<'p>) -> UnitResult) -> UnitResult { |
| 44 | match write(LateFile::from(path)) { |
| 45 | Err(error) => { // FIXME deletes existing file if creation of new file fails? |
| 46 | let _deleted = std::fs::remove_file(path); // ignore deletion errors |
| 47 | Err(error) |
| 48 | }, |
| 49 | |
| 50 | ok => ok, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[derive(Debug)] |
| 55 | pub struct LateFile<'p> { |
| 56 | path: &'p Path, |
| 57 | file: Option<File> |
| 58 | } |
| 59 | |
| 60 | impl<'p> From<&'p Path> for LateFile<'p> { |
| 61 | fn from(path: &'p Path) -> Self { Self { path, file: None } } |
| 62 | } |
| 63 | |
| 64 | impl<'p> LateFile<'p> { |
| 65 | fn file(&mut self) -> std::io::Result<&mut File> { |
| 66 | if self.file.is_none() { self.file = Some(File::create(self.path)?); } |
| 67 | Ok(self.file.as_mut().unwrap()) // will not be reached if creation fails |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl<'p> std::io::Write for LateFile<'p> { |
| 72 | fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> { |
| 73 | self.file()?.write(buffer) |
| 74 | } |
| 75 | |
| 76 | fn flush(&mut self) -> std::io::Result<()> { |
| 77 | if let Some(file) = &mut self.file { file.flush() } |
| 78 | else { Ok(()) } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | impl<'p> Seek for LateFile<'p> { |
| 83 | fn seek(&mut self, position: SeekFrom) -> std::io::Result<u64> { |
| 84 | self.file()?.seek(position) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /// Peek a single byte without consuming it. |
| 90 | #[derive(Debug)] |
| 91 | pub struct PeekRead<T> { |
| 92 | |
| 93 | /// Cannot be exposed as it will not contain peeked values anymore. |
| 94 | inner: T, |
| 95 | |
| 96 | peeked: Option<IoResult<u8>>, |
| 97 | } |
| 98 | |
| 99 | impl<T: Read> PeekRead<T> { |
| 100 | |
| 101 | /// Wrap a reader to make it peekable. |
| 102 | #[inline] |
| 103 | pub fn new(inner: T) -> Self { |
| 104 | Self { inner, peeked: None } |
| 105 | } |
| 106 | |
| 107 | /// Read a single byte and return that without consuming it. |
| 108 | /// The next `read` call will include that byte. |
| 109 | #[inline] |
| 110 | pub fn peek_u8(&mut self) -> &IoResult<u8> { |
| 111 | self.peeked = self.peeked.take().or_else(|| Some(u8::read_from_little_endian(&mut self.inner))); |
| 112 | self.peeked.as_ref().unwrap() // unwrap cannot fail because we just set it |
| 113 | } |
| 114 | |
| 115 | /// Skip a single byte if it equals the specified value. |
| 116 | /// Returns whether the value was found. |
| 117 | /// Consumes the peeked result if an error occurred. |
| 118 | #[inline] |
| 119 | pub fn skip_if_eq(&mut self, value: u8) -> IoResult<bool> { |
| 120 | match self.peek_u8() { |
| 121 | Ok(peeked) if *peeked == value => { |
| 122 | self.peeked = None; // consume the byte |
| 123 | Ok(true) |
| 124 | }, |
| 125 | |
| 126 | Ok(_) => Ok(false), |
| 127 | |
| 128 | // return the error otherwise. |
| 129 | // unwrap is safe because this branch cannot be reached otherwise. |
| 130 | // we need to take() from self because io errors cannot be cloned. |
| 131 | Err(_) => Err(self.peeked.take().unwrap().err().unwrap()) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | impl<T: Read> Read for PeekRead<T> { |
| 138 | fn read(&mut self, target_buffer: &mut [u8]) -> IoResult<usize> { |
| 139 | if target_buffer.is_empty() { |
| 140 | return Ok(0) |
| 141 | } |
| 142 | |
| 143 | match self.peeked.take() { |
| 144 | None => self.inner.read(target_buffer), |
| 145 | Some(peeked) => { |
| 146 | target_buffer[0] = peeked?; |
| 147 | |
| 148 | // indexing [1..] is safe because an empty buffer already returned ok |
| 149 | Ok(1 + self.inner.read(&mut target_buffer[1..])?) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | impl<T: Read + Seek> PeekRead<Tracking<T>> { |
| 156 | |
| 157 | /// Seek this read to the specified byte position. |
| 158 | /// Discards any previously peeked value. |
| 159 | pub fn skip_to(&mut self, position: usize) -> std::io::Result<()> { |
| 160 | self.inner.seek_read_to(position)?; |
| 161 | self.peeked = None; |
| 162 | Ok(()) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | impl<T: Read> PeekRead<Tracking<T>> { |
| 167 | |
| 168 | /// Current number of bytes read. |
| 169 | pub fn byte_position(&self) -> usize { |
| 170 | self.inner.byte_position() |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// Keep track of what byte we are at. |
| 175 | /// Used to skip back to a previous place after writing some information. |
| 176 | #[derive(Debug)] |
| 177 | pub struct Tracking<T> { |
| 178 | |
| 179 | /// Do not expose to prevent seeking without updating position |
| 180 | inner: T, |
| 181 | |
| 182 | position: usize, |
| 183 | } |
| 184 | |
| 185 | impl<T: Read> Read for Tracking<T> { |
| 186 | fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> { |
| 187 | let count = self.inner.read(buffer)?; |
| 188 | self.position += count; |
| 189 | Ok(count) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | impl<T: Write> Write for Tracking<T> { |
| 194 | fn write(&mut self, buffer: &[u8]) -> std::io::Result<usize> { |
| 195 | let count = self.inner.write(buffer)?; |
| 196 | self.position += count; |
| 197 | Ok(count) |
| 198 | } |
| 199 | |
| 200 | fn flush(&mut self) -> std::io::Result<()> { |
| 201 | self.inner.flush() |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | impl<T> Tracking<T> { |
| 206 | |
| 207 | /// If `inner` is a reference, if must never be seeked directly, |
| 208 | /// but only through this `Tracking` instance. |
| 209 | pub fn new(inner: T) -> Self { |
| 210 | Tracking { inner, position: 0 } |
| 211 | } |
| 212 | |
| 213 | /// Current number of bytes written or read. |
| 214 | pub fn byte_position(&self) -> usize { |
| 215 | self.position |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | impl<T: Read + Seek> Tracking<T> { |
| 220 | |
| 221 | /// Set the reader to the specified byte position. |
| 222 | /// If it is only a couple of bytes, no seek system call is performed. |
| 223 | pub fn seek_read_to(&mut self, target_position: usize) -> std::io::Result<()> { |
| 224 | let delta = target_position as i128 - self.position as i128; // FIXME panicked at 'attempt to subtract with overflow' |
| 225 | debug_assert!(delta.abs() < usize::MAX as i128); |
| 226 | |
| 227 | if delta > 0 && delta < 16 { // TODO profile that this is indeed faster than a syscall! (should be because of bufread buffer discard) |
| 228 | skip_bytes(self, delta as usize)?; |
| 229 | self.position += delta as usize; |
| 230 | } |
| 231 | else if delta != 0 { |
| 232 | self.inner.seek(SeekFrom::Start(u64::try_from(target_position).unwrap()))?; |
| 233 | self.position = target_position; |
| 234 | } |
| 235 | |
| 236 | Ok(()) |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | impl<T: Write + Seek> Tracking<T> { |
| 241 | |
| 242 | /// Move the writing cursor to the specified target byte index. |
| 243 | /// If seeking forward, this will write zeroes. |
| 244 | pub fn seek_write_to(&mut self, target_position: usize) -> std::io::Result<()> { |
| 245 | if target_position < self.position { |
| 246 | self.inner.seek(SeekFrom::Start(u64::try_from(target_position).unwrap()))?; |
| 247 | } |
| 248 | else if target_position > self.position { |
| 249 | std::io::copy( |
| 250 | &mut std::io::repeat(0).take(u64::try_from(target_position - self.position).unwrap()), |
| 251 | self |
| 252 | )?; |
| 253 | } |
| 254 | |
| 255 | self.position = target_position; |
| 256 | Ok(()) |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /// Generic trait that defines common binary operations such as reading and writing for this type. |
| 262 | pub trait Data: Sized + Default + Clone { |
| 263 | |
| 264 | /// Number of bytes this would consume in an exr file. |
| 265 | const BYTE_SIZE: usize = ::std::mem::size_of::<Self>(); |
| 266 | |
| 267 | /// Read a value of type `Self` from a little-endian source. |
| 268 | fn read_le(read: &mut impl Read) -> Result<Self>; |
| 269 | |
| 270 | /// Read a value of type `Self` from a **native-endian** source (no conversion). |
| 271 | fn read_ne(read: &mut impl Read) -> Result<Self>; |
| 272 | |
| 273 | /// Read as many values of type `Self` as fit into the specified slice, from a little-endian source. |
| 274 | /// If the slice cannot be filled completely, returns `Error::Invalid`. |
| 275 | fn read_slice_le(read: &mut impl Read, slice: &mut[Self]) -> UnitResult; |
| 276 | |
| 277 | /// Read as many values of type `Self` as fit into the specified slice, from a **native-endian** source (no conversion). |
| 278 | /// If the slice cannot be filled completely, returns `Error::Invalid`. |
| 279 | fn read_slice_ne(read: &mut impl Read, slice: &mut[Self]) -> UnitResult; |
| 280 | |
| 281 | /// Read as many values of type `Self` as specified with `data_size`. |
| 282 | /// |
| 283 | /// This method will not allocate more memory than `soft_max` at once. |
| 284 | /// If `hard_max` is specified, it will never read any more than that. |
| 285 | /// Returns `Error::Invalid` if the reader does not contain the desired number of elements. |
| 286 | /// Reads from little-endian byte source. |
| 287 | #[inline] |
| 288 | fn read_vec_le(read: &mut impl Read, data_size: usize, soft_max: usize, hard_max: Option<usize>, purpose: &'static str) |
| 289 | -> Result<Vec<Self>> |
| 290 | { |
| 291 | if let Some(max) = hard_max { |
| 292 | if data_size > max { |
| 293 | return Err(Error::invalid(purpose)) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | let mut vec = Vec::with_capacity(data_size.min(soft_max)); |
| 298 | Self::read_into_vec_le(read, &mut vec, data_size, soft_max, hard_max, purpose)?; |
| 299 | Ok(vec) |
| 300 | } |
| 301 | |
| 302 | /// Write this value to the writer, converting to little-endian format. |
| 303 | fn write_le(self, write: &mut impl Write) -> UnitResult; |
| 304 | |
| 305 | /// Write this value to the writer, in **native-endian** format (no conversion). |
| 306 | fn write_ne(self, write: &mut impl Write) -> UnitResult; |
| 307 | |
| 308 | /// Write all values of that slice to the writer, converting to little-endian format. |
| 309 | fn write_slice_le(write: &mut impl Write, slice: &[Self]) -> UnitResult; |
| 310 | |
| 311 | /// Write all values of that slice to the writer, in **native-endian** format (no conversion). |
| 312 | fn write_slice_ne(write: &mut impl Write, slice: &[Self]) -> UnitResult; |
| 313 | |
| 314 | |
| 315 | /// Read as many values of type `Self` as specified with `data_size` into the provided vector. |
| 316 | /// |
| 317 | /// This method will not allocate more memory than `soft_max` at once. |
| 318 | /// If `hard_max` is specified, it will never read any more than that. |
| 319 | /// Returns `Error::Invalid` if reader does not contain the desired number of elements. |
| 320 | #[inline] |
| 321 | fn read_into_vec_le( |
| 322 | read: &mut impl Read, |
| 323 | data: &mut impl ResizableVec<Self>, |
| 324 | data_size: usize, soft_max: usize, hard_max: Option<usize>, |
| 325 | purpose: &'static str |
| 326 | ) -> UnitResult { |
| 327 | if let Some(max) = hard_max { |
| 328 | if data_size > max { |
| 329 | return Err(Error::invalid(purpose)) |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | let soft_max = hard_max.unwrap_or(soft_max).min(soft_max); |
| 334 | let end = data.len() + data_size; |
| 335 | |
| 336 | // do not allocate more than $chunks memory at once |
| 337 | // (most of the time, this loop will run only once) |
| 338 | while data.len() < end { |
| 339 | let chunk_start = data.len(); |
| 340 | let chunk_end = (chunk_start + soft_max).min(data_size); |
| 341 | |
| 342 | data.resize(chunk_end, Self::default()); |
| 343 | Self::read_slice_le(read, &mut data.as_mut()[chunk_start .. chunk_end])?; // safe because of `min(data_size)` |
| 344 | } |
| 345 | |
| 346 | Ok(()) |
| 347 | } |
| 348 | |
| 349 | /// Write the length of the slice and then its contents, converting to little-endian format. |
| 350 | #[inline] |
| 351 | fn write_i32_sized_slice_le<W: Write>(write: &mut W, slice: &[Self]) -> UnitResult { |
| 352 | i32::try_from(slice.len())?.write_le(write)?; |
| 353 | Self::write_slice_le(write, slice) |
| 354 | } |
| 355 | |
| 356 | /// Read the desired element count and then read that many items into a vector. |
| 357 | /// |
| 358 | /// This method will not allocate more memory than `soft_max` at once. |
| 359 | /// If `hard_max` is specified, it will never read any more than that. |
| 360 | /// Returns `Error::Invalid` if reader does not contain the desired number of elements. |
| 361 | #[inline] |
| 362 | fn read_i32_sized_vec_le(read: &mut impl Read, soft_max: usize, hard_max: Option<usize>, purpose: &'static str) -> Result<Vec<Self>> { |
| 363 | let size = usize::try_from(i32::read_le(read)?)?; |
| 364 | Self::read_vec_le(read, size, soft_max, hard_max, purpose) |
| 365 | } |
| 366 | |
| 367 | /// Fill the slice with this value. |
| 368 | #[inline] |
| 369 | fn fill_slice(self, slice: &mut [Self]) where Self: Copy { |
| 370 | // hopefully compiles down to a single memset call |
| 371 | for value in slice { |
| 372 | *value = self; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /// A unifying trait that is implemented for Vec and SmallVec, |
| 378 | /// focused on resizing capabilities. |
| 379 | pub trait ResizableVec<T>: AsMut<[T]> { |
| 380 | fn resize(&mut self, new_len: usize, value: T); |
| 381 | fn len(&self) -> usize; |
| 382 | } |
| 383 | |
| 384 | impl<T: Clone> ResizableVec<T> for Vec<T> { |
| 385 | fn resize(&mut self, new_len: usize, value: T) { |
| 386 | Vec::resize(self, new_len, value) |
| 387 | } |
| 388 | fn len(&self) -> usize { |
| 389 | Vec::len(self) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | impl<T: Clone, A: Array<Item=T>> ResizableVec<T> for SmallVec<A> { |
| 394 | fn resize(&mut self, new_len: usize, value: T) { |
| 395 | SmallVec::resize(self, new_len, value) |
| 396 | } |
| 397 | fn len(&self) -> usize { |
| 398 | SmallVec::len(self) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | |
| 404 | macro_rules! implement_data_for_primitive { |
| 405 | ($kind: ident) => { |
| 406 | impl Data for $kind { |
| 407 | #[inline] |
| 408 | fn read_le(read: &mut impl Read) -> Result<Self> { |
| 409 | Ok(read.read_from_little_endian()?) |
| 410 | } |
| 411 | |
| 412 | #[inline] |
| 413 | fn read_ne(read: &mut impl Read) -> Result<Self> { |
| 414 | Ok(read.read_from_native_endian()?) |
| 415 | } |
| 416 | |
| 417 | #[inline] |
| 418 | fn write_le(self, write: &mut impl Write) -> Result<()> { |
| 419 | write.write_as_little_endian(&self)?; |
| 420 | Ok(()) |
| 421 | } |
| 422 | |
| 423 | #[inline] |
| 424 | fn write_ne(self, write: &mut impl Write) -> Result<()> { |
| 425 | write.write_as_native_endian(&self)?; |
| 426 | Ok(()) |
| 427 | } |
| 428 | |
| 429 | #[inline] |
| 430 | fn read_slice_le(read: &mut impl Read, slice: &mut [Self]) -> Result<()> { |
| 431 | read.read_from_little_endian_into(slice)?; |
| 432 | Ok(()) |
| 433 | } |
| 434 | |
| 435 | #[inline] |
| 436 | fn read_slice_ne(read: &mut impl Read, slice: &mut [Self]) -> Result<()> { |
| 437 | read.read_from_native_endian_into(slice)?; |
| 438 | Ok(()) |
| 439 | } |
| 440 | |
| 441 | #[inline] |
| 442 | fn write_slice_le(write: &mut impl Write, slice: &[Self]) -> Result<()> { |
| 443 | write.write_as_little_endian(slice)?; |
| 444 | Ok(()) |
| 445 | } |
| 446 | |
| 447 | #[inline] |
| 448 | fn write_slice_ne(write: &mut impl Write, slice: &[Self]) -> Result<()> { |
| 449 | write.write_as_native_endian(slice)?; |
| 450 | Ok(()) |
| 451 | } |
| 452 | } |
| 453 | }; |
| 454 | } |
| 455 | |
| 456 | implement_data_for_primitive!(u8); |
| 457 | implement_data_for_primitive!(i8); |
| 458 | implement_data_for_primitive!(i16); |
| 459 | implement_data_for_primitive!(u16); |
| 460 | implement_data_for_primitive!(u32); |
| 461 | implement_data_for_primitive!(i32); |
| 462 | implement_data_for_primitive!(i64); |
| 463 | implement_data_for_primitive!(u64); |
| 464 | implement_data_for_primitive!(f32); |
| 465 | implement_data_for_primitive!(f64); |
| 466 | |
| 467 | |
| 468 | impl Data for f16 { |
| 469 | #[inline] |
| 470 | fn read_le(read: &mut impl Read) -> Result<Self> { |
| 471 | u16::read_le(read).map(f16::from_bits) |
| 472 | } |
| 473 | |
| 474 | #[inline] |
| 475 | fn read_ne(read: &mut impl Read) -> Result<Self> { |
| 476 | u16::read_ne(read).map(f16::from_bits) |
| 477 | } |
| 478 | |
| 479 | #[inline] |
| 480 | fn read_slice_le(read: &mut impl Read, slice: &mut [Self]) -> Result<()> { |
| 481 | let bits_mut = slice.reinterpret_cast_mut(); |
| 482 | u16::read_slice_le(read, bits_mut) |
| 483 | } |
| 484 | |
| 485 | #[inline] |
| 486 | fn read_slice_ne(read: &mut impl Read, slice: &mut [Self]) -> Result<()> { |
| 487 | let bits_mut = slice.reinterpret_cast_mut(); |
| 488 | u16::read_slice_ne(read, bits_mut) |
| 489 | } |
| 490 | |
| 491 | #[inline] |
| 492 | fn write_le(self, write: &mut impl Write) -> Result<()> { |
| 493 | self.to_bits().write_le(write) |
| 494 | } |
| 495 | |
| 496 | #[inline] |
| 497 | fn write_ne(self, write: &mut impl Write) -> Result<()> { |
| 498 | self.to_bits().write_ne(write) |
| 499 | } |
| 500 | |
| 501 | #[inline] |
| 502 | fn write_slice_le(write: &mut impl Write, slice: &[Self]) -> Result<()> { |
| 503 | let bits = slice.reinterpret_cast(); |
| 504 | u16::write_slice_le(write, bits) |
| 505 | } |
| 506 | |
| 507 | #[inline] |
| 508 | fn write_slice_ne(write: &mut impl Write, slice: &[Self]) -> Result<()> { |
| 509 | let bits = slice.reinterpret_cast(); |
| 510 | u16::write_slice_ne(write, bits) |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | |
| 515 | #[cfg(test)] |
| 516 | mod test { |
| 517 | use crate::io::PeekRead; |
| 518 | use std::io::Read; |
| 519 | |
| 520 | #[test] |
| 521 | fn peek(){ |
| 522 | use lebe::prelude::*; |
| 523 | let buffer: &[u8] = &[0,1,2,3]; |
| 524 | let mut peek = PeekRead::new(buffer); |
| 525 | |
| 526 | assert_eq!(peek.peek_u8().as_ref().unwrap(), &0); |
| 527 | assert_eq!(peek.peek_u8().as_ref().unwrap(), &0); |
| 528 | assert_eq!(peek.peek_u8().as_ref().unwrap(), &0); |
| 529 | assert_eq!(u8::read_from_little_endian(&mut peek).unwrap(), 0_u8); |
| 530 | |
| 531 | assert_eq!(peek.read(&mut [0,0]).unwrap(), 2); |
| 532 | |
| 533 | assert_eq!(peek.peek_u8().as_ref().unwrap(), &3); |
| 534 | assert_eq!(u8::read_from_little_endian(&mut peek).unwrap(), 3_u8); |
| 535 | |
| 536 | assert!(peek.peek_u8().is_err()); |
| 537 | assert!(peek.peek_u8().is_err()); |
| 538 | assert!(peek.peek_u8().is_err()); |
| 539 | assert!(peek.peek_u8().is_err()); |
| 540 | |
| 541 | assert!(u8::read_from_little_endian(&mut peek).is_err()); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 |