Debian Codegraph @

src/decoder/cycles.rs

raw symbols SCIP
1 use craterust-analyzer cargo tiff 0.11.3 crate/::{tagsrust-analyzer cargo tiff 0.11.3 tags/::IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#, TiffErrorrust-analyzer cargo tiff 0.11.3 error/TiffError#, TiffFormatErrorrust-analyzer cargo tiff 0.11.3 error/TiffFormatError#};
2 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#;
3
4 /// The IFD structure of a TIFF file should be limited to a forest of entries, and a tree when we
5 /// only consider having a primary IFD and its children. There is up to one primary child of a node
6 /// that is the `next` entry in an IFD itself. Since we offer iteration over this chain we want to
7 /// detect when a new edge would introduce a cycle, such that callers can be guaranteed to
8 /// terminate iteration at some point even in malicious images.
9 ///
10 /// However, we are not necessarily visiting the IFDs in their topological order.
11 ///
12 /// Since we only consider one child, we run union find to assign each pointer to a known chain.
13 /// Then, when we insert a new edge we check if they belong to the same component.
14 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
15 pub struct IfdCyclesrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles# {
16 /// The root of each component union.
17 component_unionrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#component_union.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<ComponentIdrust-analyzer cargo tiff 0.11.3 decoder/cycles/ComponentId#, ComponentIdrust-analyzer cargo tiff 0.11.3 decoder/cycles/ComponentId#>,
18 linksrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#links.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#, u64>,
19 chainsrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#chains.: HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#<IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#, ComponentIdrust-analyzer cargo tiff 0.11.3 decoder/cycles/ComponentId#>,
20 }
21
22 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
23 struct ComponentIdrust-analyzer cargo tiff 0.11.3 decoder/cycles/ComponentId#(u64);
24
25 impl IfdCyclesrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles# {
26 pub fn newrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]new().() -> Selfrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles] {
27 IfdCyclesrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#::defaultrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles][Default]default().()
28 }
29
30 pub fn insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(
31 &mut self,
32 from: IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#,
33 to: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#>,
34 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, TiffErrorrust-analyzer cargo tiff 0.11.3 error/TiffError#> {
35 let to_offset = to.map_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map_or().(0, |p| p.0rust-analyzer cargo tiff 0.11.3 tags/IfdPointer#0.);
36
37 // For some reason we got
38 match self.linksrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#links..getrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]get().(&from) {
39 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(existing) if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().existing rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u64][`PartialEq<Self>`]eq().to_offset => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false),
40 // We got here twice with two different reads of the same IFD. That is .. unusual and
41 // we interpret it as a cycle.
42 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(TiffErrorrust-analyzer cargo tiff 0.11.3 error/TiffError#::FormatErrorrust-analyzer cargo tiff 0.11.3 error/TiffError#FormatError#(TiffFormatErrorrust-analyzer cargo tiff 0.11.3 error/TiffFormatError#::CycleInOffsetsrust-analyzer cargo tiff 0.11.3 error/TiffFormatError#CycleInOffsets#)),
43 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => self.linksrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#links..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(from, to_offset),
44 };
45
46 self.ensure_noderust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]ensure_node().(from);
47
48 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(to) = to {
49 self.ensure_noderust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]ensure_node().(to);
50
51 let parent = self.nominal_componentrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]nominal_component().(from);
52 let child = self.nominal_componentrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]nominal_component().(to);
53
54 if parent rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq().==rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq(). rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq().child {
55 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(TiffErrorrust-analyzer cargo tiff 0.11.3 error/TiffError#::FormatErrorrust-analyzer cargo tiff 0.11.3 error/TiffError#FormatError#(TiffFormatErrorrust-analyzer cargo tiff 0.11.3 error/TiffFormatError#::CycleInOffsetsrust-analyzer cargo tiff 0.11.3 error/TiffFormatError#CycleInOffsets#));
56 }
57
58 self.component_unionrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#component_union..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(child, parent);
59 }
60
61 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true)
62 }
63
64 fn ensure_noderust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]ensure_node().(&mut self, ifd: IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#) {
65 self.chainsrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#chains..entryrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]entry().(ifd).or_insert_withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`Entry<'a, K, V, A>`]or_insert_with().(|| {
66 let id = ComponentIdrust-analyzer cargo tiff 0.11.3 decoder/cycles/ComponentId#(self.component_unionrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#component_union..lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]len().() as u64);
67 self.component_unionrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#component_union..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(id, id);
68 id
69 });
70 }
71
72 fn nominal_componentrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]nominal_component().(&mut self, node: IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#) -> ComponentIdrust-analyzer cargo tiff 0.11.3 decoder/cycles/ComponentId# {
73 let id = self.chainsrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#chains.[rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index().&node]rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index().;
74
75 let nomimal = {
76 let mut iter = id;
77
78 loop {
79 let parent = self.component_unionrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#component_union.[rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index().&iter]rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index().;
80
81 if parent rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq().==rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq(). rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq().iter {
82 break parent;
83 }
84
85 iter = parent;
86 }
87 };
88
89 if nomimal rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().id {
90 // Compress.
91 let mut iter = id;
92
93 loop {
94 let parent = self.component_unionrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#component_union.[rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index().&iter]rust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`][`Index<&Q>`]index().;
95
96 if parent rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq().==rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq(). rust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[ComponentId][`PartialEq<Self>`]eq().iter {
97 break;
98 }
99
100 self.component_unionrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#component_union..insertrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/impl#[`HashMap<K, V, S, A>`]insert().(iter, nomimal);
101 iter = parent;
102 }
103 }
104
105 nomimal
106 }
107 }
108
109 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
110 fn cycles_are_detectedrust-analyzer cargo tiff 0.11.3 decoder/cycles/cycles_are_detected().() {
111 let mut cycles = IfdCyclesrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#::newrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]new().();
112
113 cycles
114 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x800)))
115 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("non-existing link is valid");
116
117 cycles
118 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x800), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20)))
119 .expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("cycle must be detected");
120 }
121
122 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
123 fn reflective_cyclerust-analyzer cargo tiff 0.11.3 decoder/cycles/reflective_cycle().() {
124 let mut cycles = IfdCyclesrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#::newrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]new().();
125
126 cycles
127 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20)))
128 .expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("self-referential cycle must be detected");
129 }
130
131 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
132 fn late_cyclerust-analyzer cargo tiff 0.11.3 decoder/cycles/late_cycle().() {
133 let mut cycles = IfdCyclesrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#::newrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]new().();
134
135 cycles
136 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x40)))
137 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("non-existing link is valid");
138
139 cycles
140 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x60), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x80)))
141 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("non-existing link is valid");
142 cycles
143 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x80), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20)))
144 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("non-existing link is valid");
145
146 cycles
147 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x40), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x60)))
148 .expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("non-existing link is valid");
149 }
150
151 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
152 fn odd_cyclerust-analyzer cargo tiff 0.11.3 decoder/cycles/odd_cycle().() {
153 let mut cycles = IfdCyclesrust-analyzer cargo tiff 0.11.3 decoder/cycles/IfdCycles#::newrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]new().();
154
155 cycles
156 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x40)))
157 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("non-existing link is valid");
158
159 cycles
160 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x60), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x80)))
161 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("non-existing link is valid");
162 cycles
163 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x80), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x20)))
164 .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("non-existing link is valid");
165
166 cycles
167 .insert_nextrust-analyzer cargo tiff 0.11.3 decoder/cycles/impl#[IfdCycles]insert_next().(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x40), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(IfdPointerrust-analyzer cargo tiff 0.11.3 tags/IfdPointer#(0x80)))
168 .expect_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect_err().("non-existing link is valid");
169 }