summaryrefslogtreecommitdiff
path: root/src/id3v2.rs
blob: 934bdf3285f8f8110ca6f1e57a92569f425dcd4b (plain)
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
// http://id3.org/id3v2.3.0

use std::convert::TryFrom;
use std::io::{self, Read};
use std::iter;
use std::num::TryFromIntError;
use crate::encoding::{from_latin_1, from_ucs_2_bom};
use crate::error::IntoIOError;
use crate::util::read_big_endian;

fn parse_id3v2_header(bytes: &[u8]) -> Option<(u16, u8, usize)> {
    if bytes.len() == 10 &&
            bytes[0] == b'I' &&
            bytes[1] == b'D' &&
            bytes[2] == b'3' &&
            bytes[3] != 0xff &&
            bytes[4] != 0xff &&
            bytes[6..10].iter().all(|b| (b & 0x80) == 0) {
        Some((
            read_big_endian(&bytes[3..5], 8) as u16,
            bytes[5],
            read_big_endian(&bytes[6..10], 7)
        ))
    } else {
        None
    }
}

#[derive(Debug)]
pub struct ID3v2 {
    header_size: usize,
    pub frames: Vec<RawFrame>,
}

#[derive(Debug)]
pub struct RawFrame {
    id: String,
    flags: u16,
    body: Vec<u8>,
}

#[derive(Debug)]
pub enum Frame {
    TIT2(String),
    TYER(String),
    TPE1(String),
    TALB(String),
    TRCK(String),
}

struct ArrayIter2<T> {
    arr: [T; 2],
    cursor: u8,
}

impl<T> ArrayIter2<T> {
    fn new(arr: [T; 2]) -> Self {
        Self { arr, cursor: 0 }
    }
}

impl<T: Clone> Iterator for ArrayIter2<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        if (self.cursor as usize) < self.arr.len() {
            let i = self.cursor;
            self.cursor += 1;
            Some(self.arr[i as usize].clone())
        } else {
            None
        }
    }
}

impl RawFrame {
    fn parse(data: &[u8]) -> Result<Option<(Self, usize)>, String> {
        if data.len() < 10 {
            return Err(String::from("Frame buffer too short"));
        }

        if data[0..4].iter().all(|&b| b == 0) {
            return Ok(None)
        }

        if !data[0..4].iter().all(|&b| (b'A' <= b && b <= b'Z') || (b'0' <= b && b <= b'9')) {
            return Err(format!("Invalid frame type {:?}", &data[0..4]));
        }

        let id = String::from_utf8(data[0..4].to_vec()).unwrap();
        let size = read_big_endian(&data[4..8], 8);
        let flags = read_big_endian(&data[8..10], 8) as u16;

        let body = data[10..10+size].to_vec();

        Ok(Some((RawFrame { id, flags, body }, 10 + size)))
    }

    fn interpret_encoded_string(&self) -> io::Result<String> {
        match self.body.get(0).ok_or("String field too small".ioerr())? {
            0 => {  // Latin-1
                let mut i = self.body.len();
                while i > 0 && self.body[i-1] == 0 { i -= 1; }
                from_latin_1(&self.body[1..i]).ok_or("Invalid Latin-1 string field".ioerr())
            }

            1 => {  // UCS-2
                let mut i = self.body.len();
                while i > 1 && self.body[i-2] == 0 && self.body[i-1] == 0 { i -= 2; }
                from_ucs_2_bom(&self.body[1..i]).ok_or("Invalid UCS-2 string field".ioerr())
            }

            enc => {
                Err(format!("Unknown string encoding {}", enc).ioerr())
            }
        }
    }

    fn encode_string(s: &str) -> io::Result<Vec<u8>> {
        let as_latin1 = || iter::once(Ok(0))
                                .chain(s.chars().map(|c| u8::try_from(u32::from(c))))
                                .collect::<Result<Vec<u8>, _>>();

        let as_ucs2 = || {
            let nibbles = s.chars()
                                .map(|c| u16::try_from(u32::from(c)))
                                .collect::<Result<Vec<u16>, _>>()?;
            Ok(iter::once(1)
                    .chain(nibbles.iter().flat_map(|&n| ArrayIter2::new([(n >> 8) as u8, n as u8])))
                    .collect::<Vec<u8>>())
        };

        as_latin1()
            .or_else(|_| as_ucs2())
            .map_err(|e: TryFromIntError| e.ioerr())
    }

    pub fn interpret(&self) -> io::Result<Option<Frame>> {
        let type_t = |typ: fn(String) -> Frame| self.interpret_encoded_string().map(typ).map(Some);

        if      self.id == "TIT2" { type_t(Frame::TIT2) }
        else if self.id == "TYER" { type_t(Frame::TYER) }
        else if self.id == "TPE1" { type_t(Frame::TPE1) }
        else if self.id == "TALB" { type_t(Frame::TALB) }
        else if self.id == "TRCK" { type_t(Frame::TRCK) }
        else {
            Ok(None)
        }
    }

    pub fn map_string<F: Fn(String) -> String>(self, f: F) -> io::Result<Option<Self>> {
        let type_t = |id: &str, body: String| -> io::Result<Self> {
            Ok(Self {
                id: id.to_string(),
                flags: 0,
                body: Self::encode_string(&body)?,
            })
        };

        match self.interpret()? {
            Some(Frame::TIT2(s)) => Ok(Some(type_t("TIT2", f(s))?)),
            Some(Frame::TYER(s)) => Ok(Some(type_t("TYER", f(s))?)),
            Some(Frame::TPE1(s)) => Ok(Some(type_t("TPE1", f(s))?)),
            Some(Frame::TALB(s)) => Ok(Some(type_t("TALB", f(s))?)),
            Some(Frame::TRCK(s)) => Ok(Some(type_t("TRCK", f(s))?)),
            None => Ok(Some(self)),
        }
    }
}

impl ID3v2 {
    pub fn from_stream<R: Read>(stream: &mut R) -> io::Result<Self> {
        let mut header = [0u8; 10];
        stream.read_exact(&mut header)?;

        let (id3version, flags, header_size) = parse_id3v2_header(&header).ok_or("Invalid ID3 header".ioerr())?;
        if id3version != 0x0300 {
            return Err(format!("ID3 header version {}.{} not supported", id3version / 256, id3version % 256).ioerr())
        }

        if flags != 0 {
            return Err(format!("No ID3 header flags supported ({:x})", flags).ioerr());
        }

        let body = {
            let mut body = Vec::new();
            body.resize(header_size, 0u8);
            stream.read_exact(&mut body)?;
            body
        };

        let mut frames = Vec::new();
        let mut cursor = 0;

        while cursor < body.len() {
            match RawFrame::parse(&body[cursor..]).map_err(|e| e.ioerr())? {
                Some((frame, consumed)) => {
                    frames.push(frame);
                    cursor += consumed;
                }

                None => {
                    break;
                }
            }
        }

        Ok(ID3v2 { frames, header_size })
    }
}