Skip to content

Commit ba1b7c9

Browse files
committed
Change read_buf()'s size arg from usize -> u64
This simplifies a number of the callers and avoids try_into() calls. Also, simplify the bound on T from ReadBytesExt to Read, which is sufficient.
1 parent 733eba8 commit ba1b7c9

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

mp4parse/src/lib.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use boxes::{BoxType, FourCC};
3535
mod tests;
3636

3737
// Arbitrary buffer size limit used for raw read_bufs on a box.
38-
const BUF_SIZE_LIMIT: usize = 10 * 1024 * 1024;
38+
const BUF_SIZE_LIMIT: u64 = 10 * 1024 * 1024;
3939

4040
// Max table length. Calculating in worst case for one week long video, one
4141
// frame per table entry in 30 fps.
@@ -1663,7 +1663,7 @@ fn read_moov<T: Read>(f: &mut BMFFBox<T>, context: &mut MediaContext) -> Result<
16631663
}
16641664

16651665
fn read_pssh<T: Read>(src: &mut BMFFBox<T>) -> Result<ProtectionSystemSpecificHeaderBox> {
1666-
let len = src.bytes_left().try_into()?;
1666+
let len = src.bytes_left();
16671667
let mut box_content = read_buf(src, len)?;
16681668
let (system_id, kid, data) = {
16691669
let pssh = &mut Cursor::new(box_content.as_slice());
@@ -1681,8 +1681,8 @@ fn read_pssh<T: Read>(src: &mut BMFFBox<T>) -> Result<ProtectionSystemSpecificHe
16811681
}
16821682
}
16831683

1684-
let data_size = be_u32_with_limit(pssh)?.to_usize();
1685-
let data = read_buf(pssh, data_size)?;
1684+
let data_size = be_u32_with_limit(pssh)?;
1685+
let data = read_buf(pssh, data_size.into())?;
16861686

16871687
(system_id, kid, data)
16881688
};
@@ -2277,7 +2277,7 @@ fn read_vpcc<T: Read>(src: &mut BMFFBox<T>) -> Result<VPxConfigBox> {
22772277
};
22782278

22792279
let codec_init_size = be_u16(src)?;
2280-
let codec_init = read_buf(src, codec_init_size.to_usize())?;
2280+
let codec_init = read_buf(src, codec_init_size.into())?;
22812281

22822282
// TODO(rillian): validate field value ranges.
22832283
Ok(VPxConfigBox {
@@ -2325,7 +2325,7 @@ fn read_av1c<T: Read>(src: &mut BMFFBox<T>) -> Result<AV1ConfigBox> {
23252325
};
23262326

23272327
let config_obus_size = src.bytes_left();
2328-
let config_obus = read_buf(src, config_obus_size.try_into()?)?;
2328+
let config_obus = read_buf(src, config_obus_size)?;
23292329

23302330
Ok(AV1ConfigBox {
23312331
profile,
@@ -2345,12 +2345,12 @@ fn read_av1c<T: Read>(src: &mut BMFFBox<T>) -> Result<AV1ConfigBox> {
23452345
fn read_flac_metadata<T: Read>(src: &mut BMFFBox<T>) -> Result<FLACMetadataBlock> {
23462346
let temp = src.read_u8()?;
23472347
let block_type = temp & 0x7f;
2348-
let length = be_u24(src)?;
2349-
if u64::from(length) > src.bytes_left() {
2348+
let length = be_u24(src)?.into();
2349+
if length > src.bytes_left() {
23502350
return Err(Error::InvalidData(
23512351
"FLACMetadataBlock larger than parent box"));
23522352
}
2353-
let data = read_buf(src, length.to_usize())?;
2353+
let data = read_buf(src, length)?;
23542354
Ok(FLACMetadataBlock {
23552355
block_type,
23562356
data,
@@ -2632,7 +2632,7 @@ fn read_esds<T: Read>(src: &mut BMFFBox<T>) -> Result<ES_Descriptor> {
26322632
// Subtract 4 extra to offset the members of fullbox not accounted for in
26332633
// head.offset
26342634
let esds_size = src.head.size.checked_sub(src.head.offset + 4).expect("offset invalid");
2635-
let esds_array = read_buf(src, esds_size.try_into()?)?;
2635+
let esds_array = read_buf(src, esds_size)?;
26362636

26372637
let mut es_data = ES_Descriptor::default();
26382638
find_descriptor(&esds_array, &mut es_data)?;
@@ -2691,7 +2691,7 @@ fn read_dops<T: Read>(src: &mut BMFFBox<T>) -> Result<OpusSpecificBox> {
26912691
} else {
26922692
let stream_count = src.read_u8()?;
26932693
let coupled_count = src.read_u8()?;
2694-
let channel_mapping = read_buf(src, output_channel_count.to_usize())?;
2694+
let channel_mapping = read_buf(src, output_channel_count.into())?;
26952695

26962696
Some(ChannelMappingTable {
26972697
stream_count,
@@ -2766,7 +2766,7 @@ fn read_alac<T: Read>(src: &mut BMFFBox<T>) -> Result<ALACSpecificBox> {
27662766
}
27672767

27682768
let length = match src.bytes_left() {
2769-
x @ 24 | x @ 48 => x.try_into().expect("infallible conversion to usize"),
2769+
x @ 24 | x @ 48 => x,
27702770
_ => return Err(Error::InvalidData("ALACSpecificBox magic cookie is the wrong size")),
27712771
};
27722772
let data = read_buf(src, length)?;
@@ -2841,7 +2841,7 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
28412841
return Err(Error::InvalidData("malformed video sample entry"));
28422842
}
28432843
let avcc_size = b.head.size.checked_sub(b.head.offset).expect("offset invalid");
2844-
let avcc = read_buf(&mut b.content, avcc_size.try_into()?)?;
2844+
let avcc = read_buf(&mut b.content, avcc_size)?;
28452845
debug!("{:?} (avcc)", avcc);
28462846
// TODO(kinetik): Parse avcC box? For now we just stash the data.
28472847
codec_specific = Some(VideoCodecSpecific::AVCConfig(avcc));
@@ -2871,7 +2871,7 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
28712871
// Subtract 4 extra to offset the members of fullbox not
28722872
// accounted for in head.offset
28732873
let esds_size = b.head.size.checked_sub(b.head.offset + 4).expect("offset invalid");
2874-
let esds = read_buf(&mut b.content, esds_size.try_into()?)?;
2874+
let esds = read_buf(&mut b.content, esds_size)?;
28752875
codec_specific = Some(VideoCodecSpecific::ESDSConfig(esds));
28762876
}
28772877
BoxType::ProtectionSchemeInfoBox => {
@@ -3156,7 +3156,7 @@ fn read_tenc<T: Read>(src: &mut BMFFBox<T>) -> Result<TrackEncryptionBox> {
31563156
let default_constant_iv = match (default_is_encrypted, default_iv_size) {
31573157
(1, 0) => {
31583158
let default_constant_iv_size = src.read_u8()?;
3159-
Some(read_buf(src, default_constant_iv_size.to_usize())?)
3159+
Some(read_buf(src, default_constant_iv_size.into())?)
31603160
},
31613161
_ => None,
31623162
};
@@ -3351,7 +3351,7 @@ fn read_ilst_multiple_u8_data<T: Read>(src: &mut BMFFBox<T>) -> Result<Vec<Vec<u
33513351
fn read_ilst_data<T: Read>(src: &mut BMFFBox<T>) -> Result<Vec<u8>> {
33523352
// Skip past the padding bytes
33533353
skip(&mut src.content, src.head.offset)?;
3354-
let size = src.content.limit().try_into()?;
3354+
let size = src.content.limit();
33553355
read_buf(&mut src.content, size)
33563356
}
33573357

@@ -3362,13 +3362,13 @@ fn skip<T: Read>(src: &mut T, bytes: u64) -> Result<()> {
33623362
}
33633363

33643364
/// Read size bytes into a Vector or return error.
3365-
fn read_buf<T: ReadBytesExt>(src: &mut T, size: usize) -> Result<Vec<u8>> {
3365+
fn read_buf<T: Read>(src: &mut T, size: u64) -> Result<Vec<u8>> {
33663366
if size > BUF_SIZE_LIMIT {
33673367
return Err(Error::InvalidData("read_buf size exceeds BUF_SIZE_LIMIT"));
33683368
}
3369-
if let Ok(mut buf) = allocate_read_buf(size) {
3369+
if let Ok(mut buf) = allocate_read_buf(size.try_into()?) {
33703370
let r = src.read(&mut buf)?;
3371-
if r != size {
3371+
if r != size.try_into()? {
33723372
return Err(Error::InvalidData("failed buffer read"));
33733373
}
33743374
return Ok(buf);

0 commit comments

Comments
 (0)