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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use crate::storage::{PlainStorage, Storage};
use crate::{Error, ErrorKind, Tag, Version};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
use std::convert::TryFrom;
use std::fmt;
use std::fs;
use std::io::prelude::*;
use std::io::{BufReader, Seek, SeekFrom};
use std::{convert::TryInto, io};

const TAG_LEN: u32 = 4; // Size of a tag.
const SIZE_LEN: u32 = 4; // Size of a 32 bits integer.
const CHUNK_HEADER_LEN: u32 = TAG_LEN + SIZE_LEN;

const ID3_TAG: ChunkTag = ChunkTag(*b"ID3 ");

/// Attempts to load a ID3 tag from the given chunk stream.
pub fn load_id3_chunk<F, R>(mut reader: R) -> crate::Result<Tag>
where
    F: ChunkFormat,
    R: io::Read + io::Seek,
{
    let root_chunk = ChunkHeader::read_root_chunk_header::<F, _>(&mut reader)?;

    // Prevent reading past the root chunk, as there may be non-standard trailing data.
    let eof = root_chunk
        .size
        .checked_sub(TAG_LEN) // We must disconsider the format tag that was already read.
        .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Invalid root chunk size"))?;

    let tag_chunk = ChunkHeader::find_id3::<F, _>(&mut reader, eof.into())?;
    let chunk_reader = reader.take(tag_chunk.size.into());
    Tag::read_from(chunk_reader)
}

/// Writes a tag to the given file. If the file contains no previous tag data, a new ID3
/// chunk is created. Otherwise, the tag is overwritten in place.
pub fn write_id3_chunk_file<F: ChunkFormat>(
    mut file: &mut fs::File,
    tag: &Tag,
    version: Version,
) -> crate::Result<()> {
    // Locate relevant chunks:
    let (mut root_chunk, id3_chunk_option) = locate_relevant_chunks::<F, _>(&mut file)?;

    let root_chunk_pos = SeekFrom::Start(0);
    let id3_chunk_pos;
    let mut id3_chunk;

    // Prepare and write the chunk:
    // We must scope the writer to be able to seek back and update the chunk sizes later.
    {
        let mut storage;
        let mut writer;
        let mut offset = 0;

        // If there is a ID3 chunk, use it. Otherwise, create one.
        id3_chunk = if let Some(chunk) = id3_chunk_option {
            let id3_tag_pos = file.stream_position()?;
            let id3_tag_end_pos = id3_tag_pos
                .checked_add(chunk.size.into())
                .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Invalid ID3 chunk size"))?;

            id3_chunk_pos = SeekFrom::Start(
                id3_tag_pos
                    .checked_sub(CHUNK_HEADER_LEN.into())
                    .expect("failed to calculate id3 chunk position"),
            );

            storage = PlainStorage::new(&mut file, id3_tag_pos..id3_tag_end_pos);
            writer = storage.writer()?;

            // As we'll overwrite the existing tag, we must subtract it's size and sum the
            // new size later.
            root_chunk.size = root_chunk
                .size
                .checked_sub(chunk.size)
                .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Invalid root chunk size"))?;

            chunk
        } else {
            let pos = file.stream_position()?;

            id3_chunk_pos = SeekFrom::Start(pos);

            storage = PlainStorage::new(&mut file, pos..pos);
            writer = storage.writer()?;

            // Create a new empty chunk at the end of the file:
            let chunk = ChunkHeader {
                tag: ID3_TAG,
                size: 0,
            };

            chunk.write_to::<F, _>(&mut writer)?;

            // Update the riff chunk size:
            root_chunk.size = root_chunk
                .size
                .checked_add(CHUNK_HEADER_LEN)
                .ok_or_else(|| {
                    Error::new(ErrorKind::InvalidInput, "root chunk max size reached")
                })?;

            // The AIFF header shouldn't be included in the chunk length
            offset = CHUNK_HEADER_LEN;

            chunk
        };

        // Write the tag:
        tag.write_to(&mut writer, version)?;

        id3_chunk.size = writer
            .stream_position()?
            .try_into()
            .expect("ID3 chunk max size reached");
        id3_chunk.size -= offset;

        // Add padding if necessary.
        if id3_chunk.size % 2 == 1 {
            let padding = [0];
            writer.write_all(&padding)?;
            id3_chunk.size = id3_chunk
                .size
                .checked_add(padding.len() as u32)
                .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "ID3 chunk max size reached"))?;
        }

        // We must flush manually to prevent silecing write errors.
        writer.flush()?;
    }

    // Update chunk sizes in the file:

    file.seek(id3_chunk_pos)?;
    id3_chunk.write_to::<F, _>(&mut file)?;

    root_chunk.size = root_chunk
        .size
        .checked_add(id3_chunk.size)
        .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "root chunk max size reached"))?;

    file.seek(root_chunk_pos)?;
    root_chunk.write_to::<F, _>(file)?;

    Ok(())
}

/// Locates the root and ID3 chunks, returning their headers. The ID3 chunk may not be
/// present. Returns a pair of (root chunk header, ID3 header).
fn locate_relevant_chunks<F, R>(mut input: R) -> crate::Result<(ChunkHeader, Option<ChunkHeader>)>
where
    F: ChunkFormat,
    R: Read + Seek,
{
    let mut reader = BufReader::new(&mut input);

    let root_chunk = ChunkHeader::read_root_chunk_header::<F, _>(&mut reader)?;

    // Prevent reading past the root chunk, as there may be non-standard trailing data.
    let eof = root_chunk
        .size
        .checked_sub(TAG_LEN) // We must disconsider the WAVE tag that was already read.
        .ok_or_else(|| Error::new(ErrorKind::InvalidInput, "Invalid root chunk size"))?;

    let id3_chunk = match ChunkHeader::find_id3::<F, _>(&mut reader, eof.into()) {
        Ok(chunk) => Some(chunk),
        Err(Error {
            kind: ErrorKind::NoTag,
            ..
        }) => None,
        Err(error) => return Err(error),
    };

    // BufReader may read past the id3 chunk. To seek back to the chunk, we must first
    // drop the BufReader, and then seek.
    let pos = reader.stream_position()?;
    drop(reader);
    input.seek(SeekFrom::Start(pos))?;

    Ok((root_chunk, id3_chunk))
}

#[derive(Debug, Clone, Copy, Eq)]
pub struct ChunkTag(pub [u8; TAG_LEN as usize]);

/// Equality for chunk tags is case insensitive.
impl PartialEq for ChunkTag {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq_ignore_ascii_case(&other.0)
    }
}

impl TryFrom<&[u8]> for ChunkTag {
    type Error = std::array::TryFromSliceError;

    fn try_from(tag: &[u8]) -> Result<Self, Self::Error> {
        let tag = tag.try_into()?;
        Ok(Self(tag))
    }
}

pub trait ChunkFormat {
    type Endianness: ByteOrder;
    const ROOT_TAG: ChunkTag;
    const ROOT_FORMAT: Option<ChunkTag>;
}

#[derive(Debug)]
pub struct AiffFormat;

impl ChunkFormat for AiffFormat {
    type Endianness = BigEndian;

    const ROOT_TAG: ChunkTag = ChunkTag(*b"FORM");
    // AIFF may have many formats, beign AIFF and AIFC the most common. Technically, it
    // can be anything, so we won't check those.
    const ROOT_FORMAT: Option<ChunkTag> = None;
}

#[derive(Debug)]
pub struct WavFormat;

impl ChunkFormat for WavFormat {
    type Endianness = LittleEndian;

    const ROOT_TAG: ChunkTag = ChunkTag(*b"RIFF");
    const ROOT_FORMAT: Option<ChunkTag> = Some(ChunkTag(*b"WAVE"));
}

#[derive(Clone, Copy, PartialEq, Eq)]
struct ChunkHeader {
    tag: ChunkTag,
    size: u32,
}

impl ChunkHeader {
    /// Reads a root chunk from the input stream. Such header is composed of:
    ///
    /// | Field   | Size | Type            |
    /// |---------+------+-----------------|
    /// | tag     |    4 | ChunkTag        |
    /// | size    |    4 | 32 bits integer |
    /// | format  |    4 | ChunkTag        |
    pub fn read_root_chunk_header<F, R>(mut reader: R) -> crate::Result<Self>
    where
        F: ChunkFormat,
        R: io::Read,
    {
        let invalid_header_error = Error::new(ErrorKind::InvalidInput, "invalid chunk header");

        const BUFFER_SIZE: usize = (CHUNK_HEADER_LEN + TAG_LEN) as usize;

        let mut buffer = [0; BUFFER_SIZE];

        // Use a single read call to improve performance on unbuffered readers.
        reader.read_exact(&mut buffer)?;

        let tag = buffer[0..4]
            .try_into()
            .expect("slice with incorrect length");

        let size = F::Endianness::read_u32(&buffer[4..8]);

        if tag != F::ROOT_TAG {
            return Err(invalid_header_error);
        }

        let chunk_format: ChunkTag = buffer[8..12]
            .try_into()
            .expect("slice with incorrect length");

        if let Some(format_tag) = F::ROOT_FORMAT {
            if chunk_format != format_tag {
                return Err(invalid_header_error);
            }
        }

        Ok(Self { tag, size })
    }

    /// Reads a chunk header from the input stream. A header is composed of:
    ///
    /// | Field | Size | Value           |
    /// |-------+------+-----------------|
    /// | tag   |    4 | chunk type      |
    /// | size  |    4 | 32 bits integer |
    pub fn read<F, R>(mut reader: R) -> io::Result<Self>
    where
        F: ChunkFormat,
        R: io::Read,
    {
        const BUFFER_SIZE: usize = CHUNK_HEADER_LEN as usize;

        let mut header = [0; BUFFER_SIZE];

        // Use a single read call to improve performance on unbuffered readers.
        reader.read_exact(&mut header)?;

        let tag = header[0..4]
            .try_into()
            .expect("slice with incorrect length");

        let size = F::Endianness::read_u32(&header[4..8]);

        Ok(Self { tag, size })
    }

    /// Finds an ID3 chunk in a flat sequence of chunks. This should be called after reading
    /// the root chunk.
    ///
    /// # Arguments
    ///
    /// * `reader` - The input stream. The reader must be positioned right after the root
    ///              chunk header.
    /// * `end` - The stream position where the chunk sequence ends. This is used to
    ///           prevent searching past the end.
    pub fn find_id3<F, R>(reader: R, end: u64) -> crate::Result<Self>
    where
        F: ChunkFormat,
        R: io::Read + io::Seek,
    {
        Self::find::<F, _>(&ID3_TAG, reader, end)?
            .ok_or_else(|| Error::new(ErrorKind::NoTag, "No tag chunk found!"))
    }

    /// Finds a chunk in a flat sequence of chunks. This won't search chunks recursively.
    ///
    /// # Arguments
    ///
    /// * `tag` - The chunk tag to search for.
    /// * `reader` - The input stream. The reader must be positioned at the start of a
    ///              sequence of chunks.
    /// * `end` - The stream position where the chunk sequence ends. This is used to
    ///           prevent searching past the end.
    fn find<F, R>(tag: &ChunkTag, mut reader: R, end: u64) -> crate::Result<Option<Self>>
    where
        F: ChunkFormat,
        R: io::Read + io::Seek,
    {
        let mut pos = 0;

        while pos < end {
            let chunk = Self::read::<F, _>(&mut reader)?;

            if &chunk.tag == tag {
                return Ok(Some(chunk));
            }

            // Skip the chunk's contents, and padding if any.
            let skip = chunk.size + (chunk.size % 2);

            pos = reader.seek(SeekFrom::Current(skip as i64))?;
        }

        Ok(None)
    }

    /// Writes a chunk header to the given stream. A header is composed of:
    ///
    /// | Field | Size | Value                         |
    /// |-------+------+-------------------------------|
    /// | tag   |    4 | chunk type                    |
    /// | size  |    4 | 32 bits little endian integer |
    pub fn write_to<F, W>(&self, mut writer: W) -> io::Result<()>
    where
        F: ChunkFormat,
        W: io::Write,
    {
        const BUFFER_SIZE: usize = CHUNK_HEADER_LEN as usize;

        let mut buffer = [0; BUFFER_SIZE];

        buffer[0..4].copy_from_slice(&self.tag.0);

        F::Endianness::write_u32(&mut buffer[4..8], self.size);

        // Use a single write call to improve performance on unbuffered writers.
        writer.write_all(&buffer)
    }
}

impl fmt::Debug for ChunkHeader {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let tag = String::from_utf8_lossy(&self.tag.0);

        f.debug_struct(std::any::type_name::<Self>())
            .field("tag", &tag)
            .field("size", &self.size)
            .finish()
    }
}