Skip to content

Commit

Permalink
Parse data from either UTF-8 or UTF-16
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Nov 5, 2024
1 parent d65af24 commit 0b054c4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions musicxml/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::elements::{ScorePartwise, ScoreTimewise};
use alloc::{collections::BTreeMap, string::String, vec::Vec};
use musicxml_internal::{ElementDeserializer, ElementSerializer, XmlElement};
use musicxml_internal::{bytes_to_string, ElementDeserializer, ElementSerializer, XmlElement};

#[cfg(feature = "std")]
extern crate std;
Expand Down Expand Up @@ -45,7 +45,7 @@ fn get_musicxml_contents(data: Vec<u8>) -> Result<String, String> {
Err(String::from("Cannot find MusicXML file in compressed archive"))?;
}
} else {
contents = String::from_utf8(data).or_else(|_| Err(String::from("Invalid UTF-8 data in MusicXML content")))?;
contents = bytes_to_string(&data)?;
}
Ok(contents)
}
Expand Down
3 changes: 2 additions & 1 deletion musicxml/src/parser/zip_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use alloc::{collections::BTreeMap, vec::Vec};
use crc32fast;
use miniz_oxide::deflate::compress_to_vec;
use miniz_oxide::inflate::decompress_to_vec;
use musicxml_internal::bytes_to_string;

#[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -247,7 +248,7 @@ impl<'a> ZipArchive<'a> {
let decoded_data =
decompress_to_vec(&self.zip_data.content[file.relative_offset..(file.relative_offset + file.compressed_size)])
.map_err(|e| e.to_string())?;
String::from_utf8(decoded_data).map_err(|e| e.to_string())
bytes_to_string(&decoded_data).map_err(|e| e.to_string())
}

pub fn iter(&self) -> impl Iterator<Item = &String> {
Expand Down
Binary file added musicxml/tests/MozaChloSample.musicxml
Binary file not shown.
16 changes: 16 additions & 0 deletions musicxml_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

extern crate alloc;

use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

pub fn bytes_to_string(bytes: &[u8]) -> Result<String, String> {
String::from_utf8(bytes.to_owned()).or_else(|_| {
let convert = if bytes[0] == 0xFF && bytes[1] == 0xFE {
u16::from_le_bytes
} else {
u16::from_be_bytes
};
let u16_bytes: Vec<u16> = bytes
.chunks_exact(2)
.map(|bytes| convert([bytes[0], bytes[1]]))
.collect();
String::from_utf16(&u16_bytes).map_err(|_| String::from("Invalid UTF-8 or UTF-16 data in MusicXML content"))
})
}

#[derive(Debug, Default, PartialEq, Eq)]
pub struct XmlElement {
pub name: String,
Expand Down

0 comments on commit 0b054c4

Please sign in to comment.