mirror of
https://github.com/alfg/mp4-rust.git
synced 2024-06-11 01:19:21 +00:00
Use size_of::<Type>() instead of magic numbers
This commit is contained in:
parent
ab412b063b
commit
013f184dd5
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -48,8 +49,10 @@ impl<R: Read + Seek> ReadBox<&mut R> for Co64Box {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let other_size = size_of::<u32>(); // entry_count
|
||||
let entry_size = size_of::<u64>(); // chunk_offset
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
if u64::from(entry_count) > size.saturating_sub(4) / 8 {
|
||||
if u64::from(entry_count) > size.saturating_sub(other_size as u64) / entry_size as u64 {
|
||||
return Err(Error::InvalidData(
|
||||
"co64 entry_count indicates more entries than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -55,7 +56,10 @@ impl<R: Read + Seek> ReadBox<&mut R> for CttsBox {
|
|||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
if u64::from(entry_count) > size.saturating_sub(4) / 8 {
|
||||
let entry_size = size_of::<u32>() + size_of::<i32>(); // sample_count + sample_offset
|
||||
// (sample_offset might be a u32, but the size is the same.)
|
||||
let other_size = size_of::<i32>(); // entry_count
|
||||
if u64::from(entry_count) > size.saturating_sub(other_size as u64) / entry_size as u64 {
|
||||
return Err(Error::InvalidData(
|
||||
"ctts entry_count indicates more entries than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -63,9 +64,18 @@ impl<R: Read + Seek> ReadBox<&mut R> for ElstBox {
|
|||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
let header_size = 4;
|
||||
let entry_size = if version == 1 { 20 } else { 12 };
|
||||
if u64::from(entry_count) > size.saturating_sub(header_size) / entry_size {
|
||||
let other_size = size_of::<i32>(); // entry_count
|
||||
let entry_size = {
|
||||
let mut entry_size = 0;
|
||||
entry_size += if version == 1 {
|
||||
size_of::<u64>() + size_of::<i64>() // segment_duration + media_time
|
||||
} else {
|
||||
size_of::<u32>() + size_of::<i32>() // segment_duration + media_time
|
||||
};
|
||||
entry_size += size_of::<i16>() + size_of::<i16>(); // media_rate_integer + media_rate_fraction
|
||||
entry_size
|
||||
};
|
||||
if u64::from(entry_count) > size.saturating_sub(other_size as u64) / entry_size as u64 {
|
||||
return Err(Error::InvalidData(
|
||||
"elst entry_count indicates more entries than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -48,8 +49,10 @@ impl<R: Read + Seek> ReadBox<&mut R> for StcoBox {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let other_size = size_of::<u32>(); // entry_count
|
||||
let entry_size = size_of::<u32>(); // chunk_offset
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
if u64::from(entry_count) > size.saturating_sub(4) / 4 {
|
||||
if u64::from(entry_count) > size.saturating_sub(other_size as u64) / entry_size as u64 {
|
||||
return Err(Error::InvalidData(
|
||||
"stco entry_count indicates more entries than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -56,8 +57,10 @@ impl<R: Read + Seek> ReadBox<&mut R> for StscBox {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let other_size = size_of::<u32>(); // entry_count
|
||||
let entry_size = size_of::<u32>() + size_of::<u32>() + size_of::<u32>(); // first_chunk + samples_per_chunk + sample_description_index
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
if u64::from(entry_count) > size.saturating_sub(4) / 12 {
|
||||
if u64::from(entry_count) > size.saturating_sub(other_size as u64) / entry_size as u64 {
|
||||
return Err(Error::InvalidData(
|
||||
"stsc entry_count indicates more entries than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -48,8 +49,10 @@ impl<R: Read + Seek> ReadBox<&mut R> for StssBox {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let other_size = size_of::<u32>(); // entry_count
|
||||
let entry_size = size_of::<u32>(); // sample_number
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
if u64::from(entry_count) > size.saturating_sub(4) / 4 {
|
||||
if u64::from(entry_count) > size.saturating_sub(other_size as u64) / entry_size as u64 {
|
||||
return Err(Error::InvalidData(
|
||||
"stss entry_count indicates more entries than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -55,11 +56,19 @@ impl<R: Read + Seek> ReadBox<&mut R> for StszBox {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let other_size = size_of::<u32>() + size_of::<u32>(); // sample_size + sample_count
|
||||
let sample_size = reader.read_u32::<BigEndian>()?;
|
||||
let stsz_item_size = if sample_size == 0 {
|
||||
size_of::<u32>() // entry_size
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let sample_count = reader.read_u32::<BigEndian>()?;
|
||||
let mut sample_sizes = Vec::new();
|
||||
if sample_size == 0 {
|
||||
if u64::from(sample_count) > size.saturating_sub(8) / 4 {
|
||||
if u64::from(sample_count)
|
||||
> size.saturating_sub(other_size as u64) / stsz_item_size as u64
|
||||
{
|
||||
return Err(Error::InvalidData(
|
||||
"stsz sample_count indicates more values than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -54,8 +55,12 @@ impl<R: Read + Seek> ReadBox<&mut R> for SttsBox {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let other_size = size_of::<u32>(); // entry_count
|
||||
let entry_size = size_of::<u32>() + size_of::<u32>(); // sample_count + sample_delta
|
||||
let entry_count = reader.read_u32::<BigEndian>()?;
|
||||
if u64::from(entry_count) > size.saturating_sub(4) / 8 {
|
||||
if u64::from(entry_count)
|
||||
> size.saturating_sub(other_size as u64) / entry_size as u64
|
||||
{
|
||||
return Err(Error::InvalidData(
|
||||
"stts entry_count indicates more entries than could fit in the box",
|
||||
));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
|
||||
use serde::Serialize;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::mem::size_of;
|
||||
|
||||
use crate::mp4box::*;
|
||||
|
||||
|
@ -83,6 +84,14 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
|
|||
|
||||
let (version, flags) = read_box_header_ext(reader)?;
|
||||
|
||||
let other_size = size_of::<u32>() // sample_count
|
||||
+ if TrunBox::FLAG_DATA_OFFSET & flags > 0 { size_of::<i32>() } else { 0 } // data_offset
|
||||
+ if TrunBox::FLAG_FIRST_SAMPLE_FLAGS & flags > 0 { size_of::<u32>() } else { 0 }; // first_sample_flags
|
||||
let sample_size = if TrunBox::FLAG_SAMPLE_DURATION & flags > 0 { size_of::<u32>() } else { 0 } // sample_duration
|
||||
+ if TrunBox::FLAG_SAMPLE_SIZE & flags > 0 { size_of::<u32>() } else { 0 } // sample_size
|
||||
+ if TrunBox::FLAG_SAMPLE_FLAGS & flags > 0 { size_of::<u32>() } else { 0 } // sample_flags
|
||||
+ if TrunBox::FLAG_SAMPLE_CTS & flags > 0 { size_of::<u32>() } else { 0 }; // sample_composition_time_offset
|
||||
|
||||
let sample_count = reader.read_u32::<BigEndian>()?;
|
||||
|
||||
let data_offset = if TrunBox::FLAG_DATA_OFFSET & flags > 0 {
|
||||
|
@ -101,10 +110,8 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
|
|||
let mut sample_sizes = Vec::new();
|
||||
let mut sample_flags = Vec::new();
|
||||
let mut sample_cts = Vec::new();
|
||||
let header_size = ((0x0000ff & flags).count_ones() + 1) * 4;
|
||||
let entry_size = (0x00ff00 & flags).count_ones() * 4;
|
||||
if u64::from(sample_count) * u64::from(entry_size)
|
||||
> size.saturating_sub(u64::from(header_size))
|
||||
if u64::from(sample_count) * sample_size as u64
|
||||
> size.saturating_sub(other_size as u64)
|
||||
{
|
||||
return Err(Error::InvalidData(
|
||||
"trun sample_count indicates more values than could fit in the box",
|
||||
|
@ -122,6 +129,7 @@ impl<R: Read + Seek> ReadBox<&mut R> for TrunBox {
|
|||
if TrunBox::FLAG_SAMPLE_CTS & flags > 0 {
|
||||
sample_cts.reserve(sample_count as usize);
|
||||
}
|
||||
|
||||
for _ in 0..sample_count {
|
||||
if TrunBox::FLAG_SAMPLE_DURATION & flags > 0 {
|
||||
let duration = reader.read_u32::<BigEndian>()?;
|
||||
|
|
Loading…
Reference in a new issue