Debian Codegraph @

.cargo/vendor/tiff/src/decoder/ifd.rs

raw
1 //! Function for reading TIFF tags
2
3 use std::collections::HashMap;
4 use std::io::{self, Read, Seek};
5 use std::mem;
6 use std::str;
7
8 use super::stream::{ByteOrder, EndianReader};
9 use crate::tags::{IfdPointer, Tag, Type};
10 use crate::{TiffError, TiffFormatError, TiffResult};
11
12 use self::Value::{
13 Ascii, Byte, Double, Float, Ifd, IfdBig, List, Rational, RationalBig, SRational, SRationalBig,
14 Short, Signed, SignedBig, SignedByte, SignedShort, Unsigned, UnsignedBig,
15 };
16
17 #[allow(unused_qualifications)]
18 #[derive(Debug, Clone, PartialEq)]
19 #[non_exhaustive]
20 pub enum Value {
21 Byte(u8),
22 Short(u16),
23 SignedByte(i8),
24 SignedShort(i16),
25 Signed(i32),
26 SignedBig(i64),
27 Unsigned(u32),
28 UnsignedBig(u64),
29 Float(f32),
30 Double(f64),
31 List(Vec<Value>),
32 Rational(u32, u32),
33 RationalBig(u64, u64),
34 SRational(i32, i32),
35 SRationalBig(i64, i64),
36 Ascii(String),
37 Ifd(u32),
38 IfdBig(u64),
39 }
40
41 impl Value {
42 pub fn into_u8(self) -> TiffResult<u8> {
43 match self {
44 Byte(val) => Ok(val),
45 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
46 }
47 }
48 pub fn into_i8(self) -> TiffResult<i8> {
49 match self {
50 SignedByte(val) => Ok(val),
51 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
52 }
53 }
54
55 pub fn into_u16(self) -> TiffResult<u16> {
56 match self {
57 Byte(val) => Ok(val.into()),
58 Short(val) => Ok(val),
59 Unsigned(val) => Ok(u16::try_from(val)?),
60 UnsignedBig(val) => Ok(u16::try_from(val)?),
61 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
62 }
63 }
64
65 pub fn into_i16(self) -> TiffResult<i16> {
66 match self {
67 SignedByte(val) => Ok(val.into()),
68 SignedShort(val) => Ok(val),
69 Signed(val) => Ok(i16::try_from(val)?),
70 SignedBig(val) => Ok(i16::try_from(val)?),
71 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
72 }
73 }
74
75 pub fn into_u32(self) -> TiffResult<u32> {
76 match self {
77 Byte(val) => Ok(val.into()),
78 Short(val) => Ok(val.into()),
79 Unsigned(val) => Ok(val),
80 UnsignedBig(val) => Ok(u32::try_from(val)?),
81 Ifd(val) => Ok(val),
82 IfdBig(val) => Ok(u32::try_from(val)?),
83 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
84 }
85 }
86
87 pub fn into_i32(self) -> TiffResult<i32> {
88 match self {
89 SignedByte(val) => Ok(val.into()),
90 SignedShort(val) => Ok(val.into()),
91 Signed(val) => Ok(val),
92 SignedBig(val) => Ok(i32::try_from(val)?),
93 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
94 }
95 }
96
97 pub fn into_u64(self) -> TiffResult<u64> {
98 match self {
99 Byte(val) => Ok(val.into()),
100 Short(val) => Ok(val.into()),
101 Unsigned(val) => Ok(val.into()),
102 UnsignedBig(val) => Ok(val),
103 Ifd(val) => Ok(val.into()),
104 IfdBig(val) => Ok(val),
105 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
106 }
107 }
108
109 pub fn into_i64(self) -> TiffResult<i64> {
110 match self {
111 SignedByte(val) => Ok(val.into()),
112 SignedShort(val) => Ok(val.into()),
113 Signed(val) => Ok(val.into()),
114 SignedBig(val) => Ok(val),
115 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
116 }
117 }
118
119 pub fn into_f32(self) -> TiffResult<f32> {
120 match self {
121 Float(val) => Ok(val),
122 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
123 }
124 }
125
126 pub fn into_f64(self) -> TiffResult<f64> {
127 match self {
128 Double(val) => Ok(val),
129 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
130 }
131 }
132
133 /// Turn this value into an `IfdPointer`.
134 ///
135 /// Notice that this does not take an argument, a 64-bit IFD is always allowed. If the
136 /// difference is crucial and you do not want to be permissive you're expected to filter this
137 /// out before.
138 ///
139 /// For compatibility the smaller sized tags should always be allowed i.e. you might use a
140 /// non-bigtiff's directory and its tag types and move it straight to a bigtiff. For instance
141 /// the SubIFD tag is defined as `LONG or IFD`:
142 ///
143 /// <https://web.archive.org/web/20181105221012/https://www.awaresystems.be/imaging/tiff/tifftags/subifds.html>
144 pub fn into_ifd_pointer(self) -> TiffResult<IfdPointer> {
145 match self {
146 Unsigned(val) | Ifd(val) => Ok(IfdPointer(val.into())),
147 IfdBig(val) => Ok(IfdPointer(val)),
148 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
149 }
150 }
151
152 pub fn into_string(self) -> TiffResult<String> {
153 match self {
154 Ascii(val) => Ok(val),
155 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
156 }
157 }
158
159 pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
160 match self {
161 List(vec) => {
162 let mut new_vec = Vec::with_capacity(vec.len());
163 for v in vec {
164 new_vec.push(v.into_u32()?)
165 }
166 Ok(new_vec)
167 }
168 Byte(val) => Ok(vec![val.into()]),
169 Short(val) => Ok(vec![val.into()]),
170 Unsigned(val) => Ok(vec![val]),
171 UnsignedBig(val) => Ok(vec![u32::try_from(val)?]),
172 Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
173 RationalBig(numerator, denominator) => {
174 Ok(vec![u32::try_from(numerator)?, u32::try_from(denominator)?])
175 }
176 Ifd(val) => Ok(vec![val]),
177 IfdBig(val) => Ok(vec![u32::try_from(val)?]),
178 Ascii(val) => Ok(val.chars().map(u32::from).collect()),
179 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
180 }
181 }
182
183 pub fn into_u8_vec(self) -> TiffResult<Vec<u8>> {
184 match self {
185 List(vec) => {
186 let mut new_vec = Vec::with_capacity(vec.len());
187 for v in vec {
188 new_vec.push(v.into_u8()?)
189 }
190 Ok(new_vec)
191 }
192 Byte(val) => Ok(vec![val]),
193
194 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
195 }
196 }
197
198 pub fn into_u16_vec(self) -> TiffResult<Vec<u16>> {
199 match self {
200 List(vec) => {
201 let mut new_vec = Vec::with_capacity(vec.len());
202 for v in vec {
203 new_vec.push(v.into_u16()?)
204 }
205 Ok(new_vec)
206 }
207 Byte(val) => Ok(vec![val.into()]),
208 Short(val) => Ok(vec![val]),
209 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
210 }
211 }
212
213 pub fn into_i32_vec(self) -> TiffResult<Vec<i32>> {
214 match self {
215 List(vec) => {
216 let mut new_vec = Vec::with_capacity(vec.len());
217 for v in vec {
218 match v {
219 SRational(numerator, denominator) => {
220 new_vec.push(numerator);
221 new_vec.push(denominator);
222 }
223 SRationalBig(numerator, denominator) => {
224 new_vec.push(i32::try_from(numerator)?);
225 new_vec.push(i32::try_from(denominator)?);
226 }
227 _ => new_vec.push(v.into_i32()?),
228 }
229 }
230 Ok(new_vec)
231 }
232 SignedByte(val) => Ok(vec![val.into()]),
233 SignedShort(val) => Ok(vec![val.into()]),
234 Signed(val) => Ok(vec![val]),
235 SignedBig(val) => Ok(vec![i32::try_from(val)?]),
236 SRational(numerator, denominator) => Ok(vec![numerator, denominator]),
237 SRationalBig(numerator, denominator) => {
238 Ok(vec![i32::try_from(numerator)?, i32::try_from(denominator)?])
239 }
240 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
241 }
242 }
243
244 pub fn into_f32_vec(self) -> TiffResult<Vec<f32>> {
245 match self {
246 List(vec) => {
247 let mut new_vec = Vec::with_capacity(vec.len());
248 for v in vec {
249 new_vec.push(v.into_f32()?)
250 }
251 Ok(new_vec)
252 }
253 Float(val) => Ok(vec![val]),
254 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
255 }
256 }
257
258 pub fn into_f64_vec(self) -> TiffResult<Vec<f64>> {
259 match self {
260 List(vec) => {
261 let mut new_vec = Vec::with_capacity(vec.len());
262 for v in vec {
263 new_vec.push(v.into_f64()?)
264 }
265 Ok(new_vec)
266 }
267 Double(val) => Ok(vec![val]),
268 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
269 }
270 }
271
272 pub fn into_u64_vec(self) -> TiffResult<Vec<u64>> {
273 match self {
274 List(vec) => {
275 let mut new_vec = Vec::with_capacity(vec.len());
276 for v in vec {
277 new_vec.push(v.into_u64()?)
278 }
279 Ok(new_vec)
280 }
281 Byte(val) => Ok(vec![val.into()]),
282 Short(val) => Ok(vec![val.into()]),
283 Unsigned(val) => Ok(vec![val.into()]),
284 UnsignedBig(val) => Ok(vec![val]),
285 Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
286 RationalBig(numerator, denominator) => Ok(vec![numerator, denominator]),
287 Ifd(val) => Ok(vec![val.into()]),
288 IfdBig(val) => Ok(vec![val]),
289 Ascii(val) => Ok(val.chars().map(u32::from).map(u64::from).collect()),
290 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
291 }
292 }
293
294 pub fn into_i64_vec(self) -> TiffResult<Vec<i64>> {
295 match self {
296 List(vec) => {
297 let mut new_vec = Vec::with_capacity(vec.len());
298 for v in vec {
299 match v {
300 SRational(numerator, denominator) => {
301 new_vec.push(numerator.into());
302 new_vec.push(denominator.into());
303 }
304 SRationalBig(numerator, denominator) => {
305 new_vec.push(numerator);
306 new_vec.push(denominator);
307 }
308 _ => new_vec.push(v.into_i64()?),
309 }
310 }
311 Ok(new_vec)
312 }
313 SignedByte(val) => Ok(vec![val.into()]),
314 SignedShort(val) => Ok(vec![val.into()]),
315 Signed(val) => Ok(vec![val.into()]),
316 SignedBig(val) => Ok(vec![val]),
317 SRational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
318 SRationalBig(numerator, denominator) => Ok(vec![numerator, denominator]),
319 _ => Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
320 }
321 }
322
323 pub fn into_ifd_vec(self) -> TiffResult<Vec<IfdPointer>> {
324 let vec = match self {
325 Unsigned(val) | Ifd(val) => return Ok(vec![IfdPointer(val.into())]),
326 IfdBig(val) => return Ok(vec![IfdPointer(val)]),
327 List(vec) => vec,
328 _ => return Err(TiffError::FormatError(TiffFormatError::InvalidTypeForTag)),
329 };
330
331 vec.into_iter().map(Self::into_ifd_pointer).collect()
332 }
333 }
334
335 #[derive(Clone)]
336 pub struct Entry {
337 type_: Type,
338 count: u64,
339 offset: [u8; 8],
340 }
341
342 impl ::std::fmt::Debug for Entry {
343 fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
344 fmt.write_str(&format!(
345 "Entry {{ type_: {:?}, count: {:?}, offset: {:?} }}",
346 self.type_, self.count, &self.offset
347 ))
348 }
349 }
350
351 impl Entry {
352 pub fn new(type_: Type, count: u32, offset: [u8; 4]) -> Entry {
353 let mut entry_off = [0u8; 8];
354 entry_off[..4].copy_from_slice(&offset);
355 Entry::new_u64(type_, count.into(), entry_off)
356 }
357
358 pub fn new_u64(type_: Type, count: u64, offset: [u8; 8]) -> Entry {
359 Entry {
360 type_,
361 count,
362 offset,
363 }
364 }
365
366 pub fn field_type(&self) -> Type {
367 self.type_
368 }
369
370 pub fn count(&self) -> u64 {
371 self.count
372 }
373
374 pub(crate) fn offset(&self) -> &[u8] {
375 &self.offset
376 }
377
378 /// Returns a mem_reader for the offset/value field
379 fn r(&self, byte_order: ByteOrder) -> EndianReader<io::Cursor<Vec<u8>>> {
380 EndianReader::new(io::Cursor::new(self.offset.to_vec()), byte_order)
381 }
382
383 pub(crate) fn val<R: Read + Seek>(
384 &self,
385 limits: &super::Limits,
386 bigtiff: bool,
387 reader: &mut EndianReader<R>,
388 ) -> TiffResult<Value> {
389 // Case 1: there are no values so we can return immediately.
390 if self.count == 0 {
391 return Ok(List(Vec::new()));
392 }
393
394 let bo = reader.byte_order;
395
396 let tag_size = match self.type_ {
397 Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => 1,
398 Type::SHORT | Type::SSHORT => 2,
399 Type::LONG | Type::SLONG | Type::FLOAT | Type::IFD => 4,
400 Type::LONG8
401 | Type::SLONG8
402 | Type::DOUBLE
403 | Type::RATIONAL
404 | Type::SRATIONAL
405 | Type::IFD8 => 8,
406 };
407
408 let value_bytes = match self.count.checked_mul(tag_size) {
409 Some(n) => n,
410 None => {
411 return Err(TiffError::LimitsExceeded);
412 }
413 };
414
415 // Case 2: there is one value.
416 if self.count == 1 {
417 // 2a: the value is 5-8 bytes and we're in BigTiff mode.
418 if bigtiff && value_bytes > 4 && value_bytes <= 8 {
419 return Ok(match self.type_ {
420 Type::LONG8 => UnsignedBig(self.r(bo).read_u64()?),
421 Type::SLONG8 => SignedBig(self.r(bo).read_i64()?),
422 Type::DOUBLE => Double(self.r(bo).read_f64()?),
423 Type::RATIONAL => {
424 let mut r = self.r(bo);
425 Rational(r.read_u32()?, r.read_u32()?)
426 }
427 Type::SRATIONAL => {
428 let mut r = self.r(bo);
429 SRational(r.read_i32()?, r.read_i32()?)
430 }
431 Type::IFD8 => IfdBig(self.r(bo).read_u64()?),
432 Type::BYTE
433 | Type::SBYTE
434 | Type::ASCII
435 | Type::UNDEFINED
436 | Type::SHORT
437 | Type::SSHORT
438 | Type::LONG
439 | Type::SLONG
440 | Type::FLOAT
441 | Type::IFD => unreachable!(),
442 });
443 }
444
445 // 2b: the value is at most 4 bytes or doesn't fit in the offset field.
446 return Ok(match self.type_ {
447 Type::BYTE => Byte(self.offset[0]),
448 Type::SBYTE => SignedByte(self.offset[0] as i8),
449 Type::UNDEFINED => Byte(self.offset[0]),
450 Type::SHORT => Short(self.r(bo).read_u16()?),
451 Type::SSHORT => SignedShort(self.r(bo).read_i16()?),
452 Type::LONG => Unsigned(self.r(bo).read_u32()?),
453 Type::SLONG => Signed(self.r(bo).read_i32()?),
454 Type::FLOAT => Float(self.r(bo).read_f32()?),
455 Type::ASCII => {
456 if self.offset[0] == 0 {
457 Ascii("".to_string())
458 } else {
459 return Err(TiffError::FormatError(TiffFormatError::InvalidTag));
460 }
461 }
462 Type::LONG8 => {
463 reader.goto_offset(self.r(bo).read_u32()?.into())?;
464 UnsignedBig(reader.read_u64()?)
465 }
466 Type::SLONG8 => {
467 reader.goto_offset(self.r(bo).read_u32()?.into())?;
468 SignedBig(reader.read_i64()?)
469 }
470 Type::DOUBLE => {
471 reader.goto_offset(self.r(bo).read_u32()?.into())?;
472 Double(reader.read_f64()?)
473 }
474 Type::RATIONAL => {
475 reader.goto_offset(self.r(bo).read_u32()?.into())?;
476 Rational(reader.read_u32()?, reader.read_u32()?)
477 }
478 Type::SRATIONAL => {
479 reader.goto_offset(self.r(bo).read_u32()?.into())?;
480 SRational(reader.read_i32()?, reader.read_i32()?)
481 }
482 Type::IFD => Ifd(self.r(bo).read_u32()?),
483 Type::IFD8 => {
484 reader.goto_offset(self.r(bo).read_u32()?.into())?;
485 IfdBig(reader.read_u64()?)
486 }
487 });
488 }
489
490 // Case 3: There is more than one value, but it fits in the offset field.
491 if value_bytes <= 4 || bigtiff && value_bytes <= 8 {
492 match self.type_ {
493 Type::BYTE => return offset_to_bytes(self.count as usize, self),
494 Type::SBYTE => return offset_to_sbytes(self.count as usize, self),
495 Type::ASCII => {
496 let mut buf = vec![0; self.count as usize];
497 buf.copy_from_slice(&self.offset[..self.count as usize]);
498 if buf.is_ascii() && buf.ends_with(&[0]) {
499 let v = str::from_utf8(&buf)?;
500 let v = v.trim_matches(char::from(0));
501 return Ok(Ascii(v.into()));
502 } else {
503 return Err(TiffError::FormatError(TiffFormatError::InvalidTag));
504 }
505 }
506 Type::UNDEFINED => {
507 return Ok(List(
508 self.offset[0..self.count as usize]
509 .iter()
510 .map(|&b| Byte(b))
511 .collect(),
512 ));
513 }
514 Type::SHORT => {
515 let mut r = self.r(bo);
516 let mut v = Vec::new();
517 for _ in 0..self.count {
518 v.push(Short(r.read_u16()?));
519 }
520 return Ok(List(v));
521 }
522 Type::SSHORT => {
523 let mut r = self.r(bo);
524 let mut v = Vec::new();
525 for _ in 0..self.count {
526 v.push(SignedShort(r.read_i16()?));
527 }
528 return Ok(List(v));
529 }
530 Type::LONG => {
531 let mut r = self.r(bo);
532 let mut v = Vec::new();
533 for _ in 0..self.count {
534 v.push(Unsigned(r.read_u32()?));
535 }
536 return Ok(List(v));
537 }
538 Type::SLONG => {
539 let mut r = self.r(bo);
540 let mut v = Vec::new();
541 for _ in 0..self.count {
542 v.push(Signed(r.read_i32()?));
543 }
544 return Ok(List(v));
545 }
546 Type::FLOAT => {
547 let mut r = self.r(bo);
548 let mut v = Vec::new();
549 for _ in 0..self.count {
550 v.push(Float(r.read_f32()?));
551 }
552 return Ok(List(v));
553 }
554 Type::IFD => {
555 let mut r = self.r(bo);
556 let mut v = Vec::new();
557 for _ in 0..self.count {
558 v.push(Ifd(r.read_u32()?));
559 }
560 return Ok(List(v));
561 }
562 Type::LONG8
563 | Type::SLONG8
564 | Type::RATIONAL
565 | Type::SRATIONAL
566 | Type::DOUBLE
567 | Type::IFD8 => {
568 unreachable!()
569 }
570 }
571 }
572
573 // Case 4: there is more than one value, and it doesn't fit in the offset field.
574 match self.type_ {
575 // TODO check if this could give wrong results
576 // at a different endianess of file/computer.
577 Type::BYTE => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
578 let mut buf = [0; 1];
579 reader.inner().read_exact(&mut buf)?;
580 Ok(Byte(buf[0]))
581 }),
582 Type::SBYTE => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
583 Ok(SignedByte(reader.read_i8()?))
584 }),
585 Type::SHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
586 Ok(Short(reader.read_u16()?))
587 }),
588 Type::SSHORT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
589 Ok(SignedShort(reader.read_i16()?))
590 }),
591 Type::LONG => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
592 Ok(Unsigned(reader.read_u32()?))
593 }),
594 Type::SLONG => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
595 Ok(Signed(reader.read_i32()?))
596 }),
597 Type::FLOAT => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
598 Ok(Float(reader.read_f32()?))
599 }),
600 Type::DOUBLE => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
601 Ok(Double(reader.read_f64()?))
602 }),
603 Type::RATIONAL => {
604 self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
605 Ok(Rational(reader.read_u32()?, reader.read_u32()?))
606 })
607 }
608 Type::SRATIONAL => {
609 self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
610 Ok(SRational(reader.read_i32()?, reader.read_i32()?))
611 })
612 }
613 Type::LONG8 => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
614 Ok(UnsignedBig(reader.read_u64()?))
615 }),
616 Type::SLONG8 => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
617 Ok(SignedBig(reader.read_i64()?))
618 }),
619 Type::IFD => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
620 Ok(Ifd(reader.read_u32()?))
621 }),
622 Type::IFD8 => self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
623 Ok(IfdBig(reader.read_u64()?))
624 }),
625 Type::UNDEFINED => {
626 self.decode_offset(self.count, bo, bigtiff, limits, reader, |reader| {
627 let mut buf = [0; 1];
628 reader.inner().read_exact(&mut buf)?;
629 Ok(Byte(buf[0]))
630 })
631 }
632 Type::ASCII => {
633 let n = usize::try_from(self.count)?;
634 if n > limits.decoding_buffer_size {
635 return Err(TiffError::LimitsExceeded);
636 }
637
638 if bigtiff {
639 reader.goto_offset(self.r(bo).read_u64()?)?
640 } else {
641 reader.goto_offset(self.r(bo).read_u32()?.into())?
642 }
643
644 let mut out = vec![0; n];
645 reader.inner().read_exact(&mut out)?;
646 // Strings may be null-terminated, so we trim anything downstream of the null byte
647 if let Some(first) = out.iter().position(|&b| b == 0) {
648 out.truncate(first);
649 }
650 Ok(Ascii(String::from_utf8(out)?))
651 }
652 }
653 }
654
655 #[inline]
656 fn decode_offset<R, F>(
657 &self,
658 value_count: u64,
659 bo: ByteOrder,
660 bigtiff: bool,
661 limits: &super::Limits,
662 reader: &mut EndianReader<R>,
663 decode_fn: F,
664 ) -> TiffResult<Value>
665 where
666 R: Read + Seek,
667 F: Fn(&mut EndianReader<R>) -> TiffResult<Value>,
668 {
669 let value_count = usize::try_from(value_count)?;
670 if value_count > limits.decoding_buffer_size / mem::size_of::<Value>() {
671 return Err(TiffError::LimitsExceeded);
672 }
673
674 let mut v = Vec::with_capacity(value_count);
675
676 let offset = if bigtiff {
677 self.r(bo).read_u64()?
678 } else {
679 self.r(bo).read_u32()?.into()
680 };
681 reader.goto_offset(offset)?;
682
683 for _ in 0..value_count {
684 v.push(decode_fn(reader)?)
685 }
686 Ok(List(v))
687 }
688 }
689
690 /// Extracts a list of BYTE tags stored in an offset
691 #[inline]
692 fn offset_to_bytes(n: usize, entry: &Entry) -> TiffResult<Value> {
693 Ok(List(
694 entry.offset[0..n]
695 .iter()
696 .map(|&e| Unsigned(u32::from(e)))
697 .collect(),
698 ))
699 }
700
701 /// Extracts a list of SBYTE tags stored in an offset
702 #[inline]
703 fn offset_to_sbytes(n: usize, entry: &Entry) -> TiffResult<Value> {
704 Ok(List(
705 entry.offset[0..n]
706 .iter()
707 .map(|&e| Signed(i32::from(e as i8)))
708 .collect(),
709 ))
710 }
711
712 /// Type representing an Image File Directory
713 #[doc(hidden)]
714 #[deprecated = "Use struct `tiff::Directory` instead which contains all fields relevant to an Image File Directory, including the offset to the next directory"]
715 pub type Directory = HashMap<Tag, Entry>;