-
Notifications
You must be signed in to change notification settings - Fork 3
/
internal.rs
296 lines (247 loc) · 8.83 KB
/
internal.rs
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! This module provides generic reusable structures to easily store
//! and lookup a CD's structure in an image format implementation.
//!
//! Those are only useful for implementing a new image format backend.
use std::fmt;
use std::cmp;
use std::path::PathBuf;
use bcd::Bcd;
use msf::Msf;
use TrackFormat;
use CdError;
/// A generic CD index implementation. Each image format can
/// specialize it by adding its own `private` implementation.
pub struct Index<T> {
/// Sector pointed at by this index. Stored as an absolute sector
/// index.
sector_index: u32,
/// Index number
index: Bcd,
/// Track number this index belongs to
track: Bcd,
/// Track format this index belongs to
format: TrackFormat,
/// Session number this index belongs to
session: u8,
/// Generic private data associated with this index
private: T,
}
impl<T> Index<T> {
/// Create a new index
pub fn new(index: Bcd,
start: Msf,
track: Bcd,
format: TrackFormat,
session: u8,
private: T) -> Index<T> {
Index {
sector_index: start.sector_index(),
index: index,
track: track,
format: format,
session: session,
private: private,
}
}
/// Retrieve the absolute `sector_index` of the sector referenced
/// by this index
pub fn sector_index(&self) -> u32 {
self.sector_index
}
/// Retrieve the MSF of the sector referenced by this index
pub fn msf(&self) -> Msf {
Msf::from_sector_index(self.sector_index).unwrap()
}
/// Retrieve a reference to the `private` data
pub fn private(&self) -> &T {
&self.private
}
/// Retrieve a mutable reference to the `private` data
pub fn private_mut(&mut self) -> &mut T {
&mut self.private
}
/// Retrieve the index number in BCD
pub fn index(&self) -> Bcd {
self.index
}
/// Retrieve the track number in BCD
pub fn track(&self) -> Bcd {
self.track
}
/// Retrieve the format of the track containing this index
pub fn format(&self) -> TrackFormat {
self.format
}
/// Retrieve the session number
pub fn session(&self) -> u8 {
self.session
}
/// Return `true` if the index number is 0
pub fn is_pregap(&self) -> bool {
self.index.bcd() == 0
}
}
impl<T> cmp::PartialEq for Index<T> {
fn eq(&self, other: &Index<T>) -> bool {
self.sector_index == other.sector_index
}
}
impl<T> cmp::Eq for Index<T> {
}
impl<T> cmp::PartialOrd for Index<T> {
fn partial_cmp(&self, other: &Index<T>) -> Option<cmp::Ordering> {
self.sector_index.partial_cmp(&other.sector_index)
}
}
impl<T> Ord for Index<T> {
fn cmp(&self, other: &Index<T>) -> cmp::Ordering {
self.sector_index.cmp(&other.sector_index)
}
}
/// A simple cache structure used to quickly look up where an
/// arbitrary MSF lives on the disc.
pub struct IndexCache<T> {
/// Ordered vector containing all the indices in the CD
indices: Vec<Index<T>>,
/// First sector in the lead-out, given as a sector index instead
/// of an MSF to avoid converting back and forth all the time.
lead_out: u32,
}
impl<T> IndexCache<T> {
/// Create a new `IndexCache` from a vector of indices and the MSF
/// of the first sector in the lead-out. This method will return
/// an error if the disc structure makes no sense (duplicate
/// tracks, indices in the wrong order etc...).
pub fn new(file: PathBuf,
mut indices: Vec<Index<T>>,
lead_out: Msf) -> Result<IndexCache<T>, CdError> {
if indices.is_empty() {
return Err(CdError::BadImage(file, "Empty disc".to_string()));
}
// Make sure the list is sorted
indices.sort();
{
let index0 = &indices[0];
if index0.sector_index != 0 {
let error =
format!("Track 01's pregap starts at {}", index0.msf());
return Err(CdError::BadImage(file, error));
}
}
// TODO: Add more validation here.
Ok(IndexCache {
indices: indices,
lead_out: lead_out.sector_index(),
})
}
/// Return the MSF of the first sector in the lead out.
pub fn lead_out(&self) -> Msf {
Msf::from_sector_index(self.lead_out).unwrap()
}
/// Return a reference to the index at position `pos` or `None` if
/// it's out of bounds
pub fn get(&self, pos: usize) -> Option<&Index<T>> {
self.indices.get(pos)
}
/// Locate the index directly before `msf` and return its
/// position along with a reference to the `Index` struct.
pub fn find_index_for_msf(&self, msf: Msf) -> Option<(usize, &Index<T>)> {
let sector = msf.sector_index();
if sector >= self.lead_out {
return None;
}
let pos =
match self.indices.binary_search_by(
|index| index.sector_index.cmp(§or)) {
// The MSF matched an index exactly
Ok(i) => i,
// No exact match, the function returns the index of
// the first element greater than `sector` (on one
// past the end if no greater element is found).
Err(i) => i - 1,
};
Some((pos, &self.indices[pos]))
}
/// Locate `index` for `track` and return its position along with
/// a reference to the `Index` struct.
pub fn find_index_for_track(&self,
track: Bcd,
index: Bcd) -> Result<(usize, &Index<T>),
CdError> {
match self.indices.binary_search_by(
|idx| match idx.track().cmp(&track) {
cmp::Ordering::Equal => idx.index().cmp(&index),
o => o,
}) {
Ok(i) => Ok((i, &self.indices[i])),
Err(_) => Err(CdError::BadTrack),
}
}
/// Locate index1 for `track` and return its position along with a
/// reference to the `Index` struct.
pub fn find_index01_for_track(&self,
track: Bcd) -> Result<(usize, &Index<T>),
CdError> {
self.find_index_for_track(track, Bcd::one())
}
/// Return the length of the given track starting at INDEX 01, not
/// counting the pregap. Also returns the position and a reference
/// to the INDEX 01 for this track.
pub fn track_length(&self, track: Bcd) -> Result<(Msf, usize, &Index<T>),
CdError> {
let (pos01, index01) =
try!(self.find_index01_for_track(track));
// Iterate over the remaining indices to find the beginning of
// the next track
let next_track =
self.indices[pos01 + 1..].iter().find(|i| i.track() != track);
let end =
match next_track {
// We found the next track, the previous sector is the
// last one in our track.
Some(next) => next.sector_index(),
// Seems like we got the last track
None => self.lead_out,
};
let len =
Msf::from_sector_index(end - index01.sector_index()).unwrap();
Ok((len, pos01, index01))
}
/// Return the absolute Msf for the position `track_msf` in
/// `track`. Will return an error if the `track_msf` is outside of
/// the track or if `track` doesn't exist.
pub fn track_msf(&self,
track: Bcd,
track_msf: Msf) -> Result<Msf, CdError> {
// We need to make sure that `track_msf` is not bigger than
// this tracks' length.
let (len, _, index01) =
try!(self.track_length(track));
if track_msf < len {
Ok(index01.msf() + track_msf)
} else {
Err(CdError::EndOfTrack)
}
}
}
impl<T> fmt::Debug for IndexCache<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut force_display = true;
let mut session = 0;
let mut track = Bcd::zero();
for i in &self.indices {
if i.session != session || force_display {
try!(writeln!(f, "Session {}:", i.session));
session = i.session;
force_display = true;
}
if i.track != track || force_display {
try!(writeln!(f, " Track {} {:?}:", i.track, i.format));
track = i.track;
force_display = false;
}
try!(writeln!(f, " Index {}: {}", i.index, i.msf()));
}
writeln!(f, "Lead-out: {}", self.lead_out())
}
}