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
use byteorder::{BigEndian, ReadBytesExt};
use std::io::{Cursor, Read, Seek};

struct ContentInfoBlock {
    signature: String,
    size: usize,
    class: u8,
    file_type: u8,
    code_type: u8,
    status: u8,
    counts: u8,
}

impl ContentInfoBlock {
    pub fn new() -> ContentInfoBlock {
        ContentInfoBlock {
            signature: String::new(),
            size: 0,
            class: 0,
            file_type: 0,
            code_type: 0,
            status: 0,
            counts: 0,
        }
    }
}

struct OptionalDataBlock {
    song_title: String,
    author: String,
    copyright: String,
}

impl OptionalDataBlock {
    pub fn new() -> OptionalDataBlock {
        OptionalDataBlock {
            song_title: String::new(),
            author: String::new(),
            copyright: String::new(),
        }
    }
}

struct TrackBlock {
    size: usize,
    track_no: u8,
    data: Vec<u8>,
}

impl TrackBlock {
    pub fn new() -> TrackBlock {
        TrackBlock {
            size: 0,
            track_no: 0,
            data: Vec::new(),
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum MmfParseResult {
    OK,
    NotFoundSmafHeader,
    UnknownError,
}

pub struct MmfFileInfo {
    data_size: usize,
    cnti_block: ContentInfoBlock,
    opda_block: OptionalDataBlock,
    midi_blocks: Vec<TrackBlock>,
    wave_blocks: Vec<TrackBlock>,
}

impl Default for MmfFileInfo {
    fn default() -> MmfFileInfo {
        MmfFileInfo {
            data_size: 0,
            cnti_block: ContentInfoBlock::new(),
            opda_block: OptionalDataBlock::new(),
            midi_blocks: Vec::new(),
            wave_blocks: Vec::new(),
        }
    }
}

fn read_opda_block_info(cursor: &mut Cursor<Vec<u8>>, signature: &[u8]) -> Option<String> {
    let mut buffer = Vec::new();
    loop {
        let mut byte_buffer = [0; 1];
        match cursor.read(&mut byte_buffer) {
            Ok(0) => break, // end of stream
            Ok(_) => {
                buffer.push(byte_buffer[0]);
                if buffer.ends_with(signature) {
                    let info_length = cursor.read_u16::<BigEndian>().unwrap() as usize;
                    let mut exact_data = vec![0; info_length];
                    let _ = cursor.read_exact(&mut exact_data);
                    let read_result = String::from_utf8(exact_data);
                    match read_result {
                        Ok(result) => {
                            return Some(result);
                        }
                        Err(_err) => break,
                    }
                }
            }
            Err(_) => break, // error
        }
    }
    None
}

fn read_track_block(cursor: &mut Cursor<Vec<u8>>, signature: &[u8]) -> Option<TrackBlock> {
    let mut buffer = Vec::new();
    loop {
        let mut byte_buffer = [0; 1];
        match cursor.read(&mut byte_buffer) {
            Ok(0) => break, // end of stream
            Ok(_) => {
                buffer.push(byte_buffer[0]);
                if buffer.ends_with(signature) {
                    let mut new_block = TrackBlock::new();
                    new_block.track_no = cursor.read_u8().unwrap();
                    new_block.size = cursor.read_u32::<BigEndian>().unwrap() as _;
                    let mut exact_data = vec![0; new_block.size];
                    let _ = cursor.read_exact(&mut exact_data);
                    new_block.data.extend_from_slice(&exact_data);
                    return Some(new_block);
                }
            }
            Err(_) => break, // error
        }
    }
    None
}

fn find_signature_from_cursor(stream: &mut Cursor<Vec<u8>>, signature: &str) -> bool {
    for sig_byte in signature.as_bytes() {
        if stream.read_u8().unwrap() != *sig_byte {
            return false;
        }
    }
    true
}

pub fn parse(file: Vec<u8>) -> Result<MmfFileInfo, MmfParseResult> {
    let mut file_info: MmfFileInfo = MmfFileInfo::default();
    let mut file_stream = Cursor::new(file);

    //If not found data in file bytes vector, Just return not found smaf header
    if !find_signature_from_cursor(&mut file_stream, "MMMD") {
        return Err(MmfParseResult::NotFoundSmafHeader);
    }

    let smaf_size = file_stream.read_u32::<BigEndian>();
    match smaf_size {
        Ok(size) => {
            file_info.data_size = size as _;
        }
        Err(_err) => {
            return Err(MmfParseResult::UnknownError);
        }
    }
    
    //Read content info block info
    if find_signature_from_cursor(&mut file_stream, "CNTI") {
        file_info.cnti_block.signature = String::from("CNTI");
        
        let cnti_block_size = file_stream.read_u32::<BigEndian>();
        match cnti_block_size {
            Ok(size) => {
                file_info.cnti_block.size = size as _;
            }
            Err(_err) => {
            }
        }

        let cnti_block_class = file_stream.read_u8();
        match cnti_block_class {
            Ok(class) => {
                file_info.cnti_block.class = class as _;
            }
            Err(_err) => {
            }
        }

        let cnti_block_file_type = file_stream.read_u8();
        match cnti_block_file_type {
            Ok(class) => {
                file_info.cnti_block.file_type = class as _;
            }
            Err(_err) => {
            }
        }

        let cnti_block_code_type = file_stream.read_u8();
        match cnti_block_code_type {
            Ok(class) => {
                file_info.cnti_block.code_type = class as _;
            }
            Err(_err) => {
            }
        }

        let cnti_block_status = file_stream.read_u8();
        match cnti_block_status {
            Ok(class) => {
                file_info.cnti_block.status = class as _;
            }
            Err(_err) => {
            }
        }

        let cnti_block_counts = file_stream.read_u8();
        match cnti_block_counts {
            Ok(class) => {
                file_info.cnti_block.counts = class as _;
            }
            Err(_err) => {
            }
        }
    }

    //Read optional data block info (Not required block)
    if find_signature_from_cursor(&mut file_stream, "OPDA") {
        let opda_block_size_parse = file_stream.read_u32::<BigEndian>();
        match opda_block_size_parse {
            Ok(block_size) => {
                let mut block_data = vec![0; block_size as _];
                file_stream.read_exact(&mut block_data).unwrap();
                let mut opda_block_stream = Cursor::new(block_data);

                //signature "ST" is song title
                let song_title_result = read_opda_block_info(&mut opda_block_stream, b"ST");
                if let Some(song_title) = song_title_result {
                    file_info.opda_block.song_title = song_title;
                }
                let _opda_stream_rewind = file_stream.rewind();

                //signature "CA" is copyright author?
                let author_result = read_opda_block_info(&mut opda_block_stream, b"CA");
                if let Some(author_title) = author_result {
                    file_info.opda_block.author = author_title;
                }
                let _opda_stream_rewind = file_stream.rewind();
                
                //signature "CR" is copyright
                let copyright_result = read_opda_block_info(&mut opda_block_stream, b"CR");
                if let Some(copyright_title) = copyright_result {
                    file_info.opda_block.copyright = copyright_title;
                }
                let _opda_stream_rewind = file_stream.rewind();

                //TODO: signature "A0"
            }
            Err(_err) => {

            }
        }
    }

    //Find and read MIDI track
    loop {
        let midi_result = read_track_block(&mut file_stream, b"MTR");
        match midi_result {
            Some(block_data) => {
                // Use the new function to create a new MidiTrackBlock instance
                file_info.midi_blocks.push(block_data);
            }
            None => {
                break;
            }
        }
    }

    let midi_rewind_result = file_stream.rewind();
    if let Ok(()) = midi_rewind_result {
        loop {
            let wave_result = read_track_block(&mut file_stream, b"ATR");
            match wave_result {
                Some(block_data) => {
                    file_info.wave_blocks.push(block_data);
                }
            None => {
                break;
            }
            }
        }
    }

    //Finally, All infos are set.
    Ok(file_info)
}

#[cfg(test)]
mod tests {
    use super::*;

    //https://www.reddit.com/r/rust/comments/dekpl5/how_to_read_binary_data_from_a_file_into_a_vecu8/
    fn get_file_as_byte_vec(filename: &String) -> Vec<u8> {
        match std::fs::read(filename) {
            Ok(bytes) => {
                bytes
            }
            Err(e) => {
                if e.kind() == std::io::ErrorKind::PermissionDenied {
                    eprintln!("please run again with appropriate permissions.");
                }
                panic!("{}", e);
            }
        }
    }

    #[test]
    fn test_mmf_parsing() {
        //4a1d512c7cf3845b664946749ff3e7162f92a768d91406e8a91fd0f3f37fa720  MachineWoman.mmf
        let info = parse(get_file_as_byte_vec(&String::from("MachineWoman.mmf")));
        match info {
            Ok(result) => {
                assert_eq!(result.data_size, 7408);
                assert_eq!(result.opda_block.song_title, "Machine Woman");
                assert_eq!(result.opda_block.author, "SMAF MA-3 Sample Data");
                assert_eq!(result.opda_block.copyright, "Copyright(c) 2002-2004 YAMAHA CORPORATION");
                assert_eq!(result.midi_blocks.len(), 1);
                assert_eq!(result.midi_blocks[0].size, 7242);
            }
            Err(e) => {
                assert_eq!(e, MmfParseResult::OK);
            }
        }
    }
}