1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//! UDF extended attributes (ECMA-167 4/14.10).
//!
//! A File Entry's `L_EA` bytes hold an **Extended Attribute Header Descriptor**
//! (tag 262) followed by a contiguous run of typed attribute records. Until now
//! this crate read `L_EA` only to skip past it and reach the allocation
//! descriptors, so the attributes were located and discarded.
//!
//! ## These are not POSIX extended attributes
//!
//! Every other filesystem in this fleet stores attributes as name/value pairs.
//! UDF does not: an attribute is a TYPED record, and only two of the types carry
//! anything resembling a name.
//!
//! | type | meaning | named? |
//! |---|---|---|
//! | 1 | Character Set Information | no |
//! | 3 | Alternate Permissions | no |
//! | 5 | File Times | no |
//! | 6 | Information Times | no |
//! | 12 | Device Specification | no |
//! | 2048 | Implementation Use | **`EntityID`** |
//! | 65536 | Application Use | **`EntityID`** |
//!
//! `setfattr` cannot write any of them and the Linux UDF driver does not present
//! them as xattrs, so ground truth here comes from an authoring tool rather than
//! from a mount.
//!
//! ## What is validated, and what is not
//!
//! The header and the Implementation Use record are checked against bytes
//! `mkudffs` wrote (`tests/data/udf_vat.img`, reproducible from the generator
//! command recorded beside it).
//!
//! **No real sample of the other record types was obtainable** — `mkudffs`
//! emits none at any media type or revision that it accepts. So every record is
//! decoded to its type, subtype, length and raw bytes, and only the two named
//! forms are interpreted further. The rest are [`UdfEaKind::Other`]: present,
//! visible, and explicitly not interpreted. That is the honest position —
//! decoding a File Times record from the spec alone would produce timestamps
//! nothing here could check, and a wrong timestamp presented as fact is worse
//! than a raw record a reader can go and examine.
use ;
/// Length of the Extended Attribute Header Descriptor: a 16-byte descriptor tag
/// plus `ImplementationAttributesLocation` and `ApplicationAttributesLocation`.
const EA_HEADER_LEN: usize = 24;
/// `Extended Attribute Header Descriptor` tag identifier (ECMA-167 4/14.10.1).
const TAG_EA_HEADER: u16 = 262;
/// Minimum bytes of any attribute record: type(4) subtype(1) reserved(3)
/// length(4), plus the 4-byte use-length the two named forms carry.
const EA_RECORD_MIN: usize = 16;
/// Offset of the use-payload length within an Implementation/Application Use
/// record (`ImplementationUseLength` / `ApplicationUseLength`).
const USE_LEN_OFF: usize = 12;
/// Offset of the `EntityID` within an Implementation/Application Use record.
const ENTITY_ID_OFF: usize = 16;
/// An `EntityID` (ECMA-167 1/7.4): flags(1) identifier(23) suffix(8).
const ENTITY_ID_LEN: usize = 32;
/// Offset of the identifier text within an `EntityID`, past its flags byte.
const ENTITY_ID_TEXT: usize = 1;
/// Length of the identifier text within an `EntityID`.
const ENTITY_ID_TEXT_LEN: usize = 23;
/// `Implementation Use Extended Attribute` (ECMA-167 4/14.10.8).
const EA_TYPE_IMPL_USE: u32 = 2048;
/// `Application Use Extended Attribute` (ECMA-167 4/14.10.9).
const EA_TYPE_APP_USE: u32 = 65536;
/// A ceiling on how many records one EA space may yield.
///
/// The EA space is bounded by `L_EA` (a `u32`) and every record must advance the
/// cursor, so this cannot be reached by well-formed input; it exists so a
/// hostile image cannot turn a large `L_EA` into an unbounded allocation.
const MAX_RECORDS: usize = 4096;
/// What kind of UDF extended attribute a record is.
/// One extended attribute record from a File Entry's EA space.
/// Parse a File Entry's extended-attribute space.
///
/// `ea` is the `L_EA` bytes beginning at the File Entry's extended-attribute
/// area. An empty slice yields an empty list: a file with no EA space is
/// ordinary, not an error.
///
/// Returns `None` when the space is present but its header descriptor is not an
/// Extended Attribute Header Descriptor — that is a malformed structure, and
/// reporting "no attributes" for it would hide the damage.
///
/// Records are walked sequentially from the end of the header rather than by
/// following the header's `ImplementationAttributesLocation` /
/// `ApplicationAttributesLocation` offsets. The records are contiguous, so the
/// sequential walk sees every one of them, including any class the two offsets
/// do not point at.
///
/// A record whose declared length overruns the space, or which does not advance
/// the cursor, ENDS the walk and is dropped. The records are packed with no
/// terminator, so once a length is untrustworthy there is nowhere safe to
/// resume; emitting a truncated record would present partial bytes as the
/// attribute's real contents.
/// The `EntityID` identifier text of an Implementation/Application Use record.
///
/// `None` when the record is too short to hold one — the name is then unknown
/// rather than empty, and the record falls back to [`UdfEaKind::Other`] so the
/// caller is not handed a blank name that looks like a real one.