mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 15:33:12 +00:00
Bound shared SoundCraft session IO
This commit is contained in:
1 parent
88aa0bed6a
commit
a2e0f58632
4 files changed
+109
-12
No files matched your search
@@ -69,8 +69,7 @@ pub fn write_bundle(session: &Session, limits: BundleLimits) -> Result<(Vec<u8>,
|
||||
source.format = FileFormat::Wav;
|
||||
}
|
||||
|
||||
let text = fresh.to_json().map_err(|e| BundleError::Session(e.to_string()))?;
|
||||
check_session_size("the session", text.len() as u64, limits.session_bytes, true)?;
|
||||
let text = crate::write_session(&fresh, limits.session_bytes)?;
|
||||
account(&mut count, &mut total, text.len(), limits)?;
|
||||
let session_name = format!("{}.{}", sanitize_name(&fresh.name), SESSION_EXTENSION);
|
||||
zip.add(&session_name, text.as_bytes(), Compression::Stored).map_err(|e| BundleError::Invalid(e.to_string()))?;
|
||||
@@ -92,13 +91,11 @@ pub fn read_bundle(bytes: &[u8], limits: BundleLimits) -> Result<(String, HashMa
|
||||
if archive.has_overlapping_files().map_err(zip_error)? {
|
||||
return Err(BundleError::Invalid("files share data".into()));
|
||||
}
|
||||
let names: Vec<String> = archive.names().map(|n| n.into_owned()).collect();
|
||||
if names.len() != archive.len() {
|
||||
return Err(BundleError::Invalid("an entry name is not text".into()));
|
||||
}
|
||||
let len = archive.len();
|
||||
let mut session = None;
|
||||
let mut files = HashMap::new();
|
||||
for name in names {
|
||||
for index in 0..len {
|
||||
let name = archive.entry_name(index).map_err(zip_error)?.ok_or_else(|| BundleError::Invalid("missing entry".into()))?;
|
||||
if name.ends_with('/') {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -42,12 +42,82 @@ pub const MAX_SESSION_BYTES: u64 = 256 << 20;
|
||||
/// Read a bare `.scraft` file after enforcing the caller's byte limit.
|
||||
pub fn read_session_bytes(name: &str, bytes: &[u8], limit: u64) -> Result<String, BundleError> {
|
||||
check_session_size(name, u64::try_from(bytes.len()).unwrap_or(u64::MAX), limit, false)?;
|
||||
String::from_utf8(bytes.to_vec()).map_err(|_| BundleError::Session(format!("{name}: not a SoundCraft session")))
|
||||
read_session_data(name, bytes.to_vec(), limit)
|
||||
}
|
||||
|
||||
/// Serialize a `.scraft` session and refuse output larger than the caller's read limit.
|
||||
/// Turn already-read `.scraft` bytes into text without making another copy.
|
||||
pub fn read_session_data(name: &str, bytes: Vec<u8>, limit: u64) -> Result<String, BundleError> {
|
||||
check_session_size(name, u64::try_from(bytes.len()).unwrap_or(u64::MAX), limit, false)?;
|
||||
String::from_utf8(bytes).map_err(|_| BundleError::Session(format!("{name}: not a SoundCraft session")))
|
||||
}
|
||||
|
||||
/// The size of pretty JSON for `session`, measured without retaining the serialized document.
|
||||
pub fn session_json_len(session: &Session) -> Result<u64, BundleError> {
|
||||
struct Count(u64);
|
||||
impl std::io::Write for Count {
|
||||
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
|
||||
self.0 = self.0.saturating_add(u64::try_from(bytes.len()).unwrap_or(u64::MAX));
|
||||
Ok(bytes.len())
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
let mut count = Count(0);
|
||||
serde_json::to_writer_pretty(&mut count, session).map_err(|error| BundleError::Session(error.to_string()))?;
|
||||
Ok(count.0)
|
||||
}
|
||||
|
||||
/// Serialize a `.scraft` session, refusing output larger than the caller's read limit while
|
||||
/// keeping the temporary allocation within that limit.
|
||||
pub fn write_session(session: &Session, limit: u64) -> Result<String, BundleError> {
|
||||
let text = session.to_json().map_err(|error| BundleError::Session(error.to_string()))?;
|
||||
check_session_size("the session", u64::try_from(text.len()).unwrap_or(u64::MAX), limit, true)?;
|
||||
Ok(text)
|
||||
write_session_named(session, "the session", limit)
|
||||
}
|
||||
|
||||
/// [`write_session`] with the path or label used for the session-size error.
|
||||
pub fn write_session_named(session: &Session, name: &str, limit: u64) -> Result<String, BundleError> {
|
||||
struct Bounded(Vec<u8>, u64, bool);
|
||||
impl std::io::Write for Bounded {
|
||||
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
|
||||
let remaining = self.1.saturating_sub(u64::try_from(self.0.len()).unwrap_or(u64::MAX));
|
||||
let count = usize::try_from(remaining).unwrap_or(usize::MAX).min(bytes.len());
|
||||
self.0.extend_from_slice(&bytes[..count]);
|
||||
self.2 |= count < bytes.len();
|
||||
Ok(count)
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
let capacity = usize::try_from(limit).unwrap_or(usize::MAX);
|
||||
let mut output = Bounded(Vec::with_capacity(capacity.min(1024 * 1024)), limit, false);
|
||||
let serialized = serde_json::to_writer_pretty(&mut output, session);
|
||||
if output.2 {
|
||||
check_session_size(name, limit.saturating_add(1), limit, true)?;
|
||||
}
|
||||
serialized.map_err(|error| BundleError::Session(error.to_string()))?;
|
||||
String::from_utf8(output.0).map_err(|error| BundleError::Session(error.to_string()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
mod session_io_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn bounded_writer_matches_canonical_session_json_and_refuses_one_byte_under() {
|
||||
let session = Session::default();
|
||||
let canonical = session.to_json().unwrap();
|
||||
let len = u64::try_from(canonical.len()).unwrap();
|
||||
assert_eq!(session_json_len(&session).unwrap(), len);
|
||||
assert_eq!(write_session(&session, len).unwrap(), canonical);
|
||||
assert!(write_session(&session, len - 1).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bare_session_reader_checks_size_and_utf8() {
|
||||
assert!(read_session_bytes("song.scraft", b"{}", 1).is_err());
|
||||
assert!(read_session_bytes("song.scraft", &[0xff], 10).is_err());
|
||||
assert_eq!(read_session_bytes("song.scraft", b"{}", 2).unwrap(), "{}");
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,17 @@ impl<'a> Archive<'a> {
|
||||
self.zip.is_empty()
|
||||
}
|
||||
|
||||
/// The name at `index`, copied on demand so callers can process hostile directories without
|
||||
/// retaining a second copy of every potentially large name.
|
||||
pub fn entry_name(&mut self, index: usize) -> Result<Option<String>, Error> {
|
||||
if index >= self.zip.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let file = self.zip.by_index_raw(index).map_err(|error| Error::NotZip(error.to_string()))?;
|
||||
let name = file.name().map_err(|error| Error::NotZip(error.to_string()))?;
|
||||
Ok(Some(name.into_owned()))
|
||||
}
|
||||
|
||||
/// Do two entries share a name? `zip` then keeps one of them, and which one a writer meant
|
||||
/// is not known: a format that must not guess refuses the archive.
|
||||
pub fn has_duplicate_names(&self) -> bool {
|
||||
|
||||
@@ -219,4 +219,23 @@ mod tests {
|
||||
let mut archive = Archive::open(&bytes, Limits { max_entries: 2, max_total_bytes: 100 }).unwrap();
|
||||
assert!(archive.has_overlapping_files().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::unwrap_used, clippy::expect_used)]
|
||||
fn archive_can_copy_one_name_at_a_time_by_index() {
|
||||
use std::io::Write as _;
|
||||
|
||||
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
|
||||
let opts = zip::write::SimpleFileOptions::default();
|
||||
zip.start_file("first", opts).unwrap();
|
||||
zip.write_all(b"1").unwrap();
|
||||
zip.start_file("second", opts).unwrap();
|
||||
zip.write_all(b"2").unwrap();
|
||||
let bytes = zip.finish().unwrap().into_inner();
|
||||
|
||||
let mut archive = Archive::open(&bytes, Limits { max_entries: 2, max_total_bytes: 10 }).unwrap();
|
||||
assert_eq!(archive.entry_name(0).unwrap().as_deref(), Some("first"));
|
||||
assert_eq!(archive.entry_name(1).unwrap().as_deref(), Some("second"));
|
||||
assert_eq!(archive.entry_name(2).unwrap(), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user