| 1 | use std::io::{Read, Seek}; |
| 2 | |
| 3 | use crate::{ |
| 4 | tags::{Tag, ValueBuffer}, |
| 5 | Directory, TiffError, TiffFormatError, TiffResult, |
| 6 | }; |
| 7 | |
| 8 | use super::ifd::{Entry, Value}; |
| 9 | use super::ValueReader; |
| 10 | |
| 11 | pub(crate) struct TagReader<'lt, R: EntryDecoder + ?Sized> { |
| 12 | pub(crate) decoder: &'lt mut R, |
| 13 | pub(crate) ifd: &'lt Directory, |
| 14 | } |
| 15 | |
| 16 | pub(crate) trait EntryDecoder { |
| 17 | /// Turn an entry into a value by fetching the required bytes in the stream. |
| 18 | fn entry_val(&mut self, entry: &Entry) -> TiffResult<Value>; |
| 19 | /// Fill a `ValueBuffer` by fetching the required bytes. |
| 20 | fn entry_buf(&mut self, entry: &Entry, _: &mut ValueBuffer) -> TiffResult<()>; |
| 21 | /// Fill the prefix of the buffer and return the actual bytes. Start interpreting the inner |
| 22 | /// value at `offset`. |
| 23 | fn entry_raw(&mut self, entry: &Entry, _: &mut [u8], offset: u64) -> TiffResult<usize>; |
| 24 | } |
| 25 | |
| 26 | impl<R: Seek + Read> EntryDecoder for ValueReader<R> { |
| 27 | fn entry_val(&mut self, entry: &Entry) -> TiffResult<Value> { |
| 28 | entry.val(&self.limits, self.bigtiff, &mut self.reader) |
| 29 | } |
| 30 | |
| 31 | fn entry_buf(&mut self, entry: &Entry, buf: &mut ValueBuffer) -> TiffResult<()> { |
| 32 | entry.buffered_value(buf, &self.limits, self.bigtiff, &mut self.reader) |
| 33 | } |
| 34 | |
| 35 | fn entry_raw(&mut self, entry: &Entry, buf: &mut [u8], offset: u64) -> TiffResult<usize> { |
| 36 | entry.raw_value_at(buf, self.bigtiff, &mut self.reader, offset) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl<'a, R: EntryDecoder + ?Sized> TagReader<'a, R> { |
| 41 | pub(crate) fn find_tag(&mut self, tag: Tag) -> TiffResult<Option<Value>> { |
| 42 | let entry = match self.ifd.get(tag) { |
| 43 | None => return Ok(None), |
| 44 | Some(entry) => entry.clone(), |
| 45 | }; |
| 46 | |
| 47 | self.decoder.entry_val(&entry).map(Some) |
| 48 | } |
| 49 | |
| 50 | /// Find a tag and fill the provided buffer with its raw data. |
| 51 | /// |
| 52 | /// Returns the entry of the tag if found. This signals that the buffer was overwritten. |
| 53 | /// Otherwise the buffer is preserved as-is. |
| 54 | pub(crate) fn find_tag_buf( |
| 55 | &mut self, |
| 56 | tag: Tag, |
| 57 | buf: &mut ValueBuffer, |
| 58 | ) -> TiffResult<Option<Entry>> { |
| 59 | let entry = match self.ifd.get(tag) { |
| 60 | None => return Ok(None), |
| 61 | Some(entry) => entry.clone(), |
| 62 | }; |
| 63 | |
| 64 | self.decoder.entry_buf(&entry, buf)?; |
| 65 | Ok(Some(entry)) |
| 66 | } |
| 67 | |
| 68 | pub(crate) fn find_tag_raw( |
| 69 | &mut self, |
| 70 | tag: Tag, |
| 71 | buf: &mut [u8], |
| 72 | at: u64, |
| 73 | ) -> TiffResult<Option<usize>> { |
| 74 | let entry = match self.ifd.get(tag) { |
| 75 | None => return Ok(None), |
| 76 | Some(entry) => entry.clone(), |
| 77 | }; |
| 78 | |
| 79 | let len = self.decoder.entry_raw(&entry, buf, at)?; |
| 80 | Ok(Some(len)) |
| 81 | } |
| 82 | |
| 83 | pub(crate) fn require_tag(&mut self, tag: Tag) -> TiffResult<Value> { |
| 84 | match self.find_tag(tag)? { |
| 85 | Some(val) => Ok(val), |
| 86 | None => Err(TiffError::FormatError( |
| 87 | TiffFormatError::RequiredTagNotFound(tag), |
| 88 | )), |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pub fn find_tag_uint_vec<T: TryFrom<u64>>(&mut self, tag: Tag) -> TiffResult<Option<Vec<T>>> { |
| 93 | self.find_tag(tag)? |
| 94 | .map(|v| v.into_u64_vec()) |
| 95 | .transpose()? |
| 96 | .map(|v| { |
| 97 | v.into_iter() |
| 98 | .map(|u| { |
| 99 | T::try_from(u).map_err(|_| TiffFormatError::InvalidTagValueType(tag).into()) |
| 100 | }) |
| 101 | .collect() |
| 102 | }) |
| 103 | .transpose() |
| 104 | } |
| 105 | } |