From 6f011833ba7e8e5dd97a333d0e227e92a783fdd8 Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Sat, 25 Feb 2023 11:13:40 +0300 Subject: [PATCH 1/2] Utils to check the file size --- src/utils.rs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/utils.rs diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..1ba4dde --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,9 @@ +use crate::{Error, Result}; +use std::fs; + +/// Check the file size. The maximum file size is 4 GB. +pub(crate) fn check_file_size(file: &fs::File) -> Result<()> { + (file.metadata()?.len() > 4_294_967_295) + .then(|| Err(Error::LargeFileSize)) + .unwrap_or(Ok(())) +} From b5a9e7f4e00ecf0b245387adb3ab924941274706 Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Sat, 25 Feb 2023 11:13:59 +0300 Subject: [PATCH 2/2] Check the file size in encoded and decoded --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 47d3252..0f27765 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ use std::{ }; mod errors; +mod utils; pub use errors::*; /// The sample rate to use when encoding @@ -23,6 +24,7 @@ pub const SAMPLE_RATE: u32 = 202860; /// encode(file, "test.wav").unwrap(); /// ``` pub fn encode(file: fs::File, wav_output: impl AsRef) -> Result<()> { + utils::check_file_size(&file)?; let spec = hound::WavSpec { channels: 1, sample_rate: SAMPLE_RATE, @@ -56,7 +58,8 @@ pub fn encode(file: fs::File, wav_output: impl AsRef) -> Result<()> /// decode("test.wav", "test.txt").unwrap(); /// ``` pub fn decode(file: impl AsRef, output: impl AsRef) -> Result<()> { - let mut reader = hound::WavReader::open(file.as_ref()).unwrap(); + utils::check_file_size(&fs::File::open(&file)?)?; + let mut reader = hound::WavReader::open(file).unwrap(); let mut writer = BufWriter::new(fs::File::create(output).unwrap()); for sample in reader.samples() { writer.write_i16(sample?)?;