Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/atom_rtp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,19 @@ void MP4RtpAtom::ReadHntiType()
uint64_t size = GetEnd() - m_File.GetPosition();
char* data = (char*)MP4Malloc(size + 1);
ASSERT(data != NULL);
m_File.ReadBytes((uint8_t*)data, size);
data[size] = '\0';
((MP4StringProperty*)m_pProperties[1])->SetValue(data);
MP4Free(data);
try
{
m_File.ReadBytes( (uint8_t*)data, size );
data[size] = '\0';
( (MP4StringProperty*)m_pProperties[1] )->SetValue( data );
MP4Free( data );
}
catch (Exception*)
{
// free memory and rethrow
MP4Free( data );
throw;
}
}

void MP4RtpAtom::Write()
Expand Down
17 changes: 13 additions & 4 deletions src/atom_sdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ void MP4SdpAtom::Read()
uint64_t size = GetEnd() - m_File.GetPosition();
char* data = (char*)MP4Malloc(size + 1);
ASSERT(data != NULL);
m_File.ReadBytes((uint8_t*)data, size);
data[size] = '\0';
((MP4StringProperty*)m_pProperties[0])->SetValue(data);
MP4Free(data);
try
{
m_File.ReadBytes( (uint8_t*)data, size );
data[size] = '\0';
( (MP4StringProperty*)m_pProperties[0] )->SetValue( data );
MP4Free( data );
}
catch (Exception*)
{
// free memory and rethrow
MP4Free( data );
throw;
}
}

void MP4SdpAtom::Write()
Expand Down
2 changes: 1 addition & 1 deletion src/mp4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ MP4FileHandle MP4ReadProvider( const char* fileName, const MP4FileProvider* file
&foo,
&bufsize)) {
uint8_t *ptr = foo;
while (bufsize > 0) {
while (bufsize >= 5) {
if (MP4V2_HTONL(*(uint32_t *)ptr) == 0x1b0) {
uint8_t ret = ptr[4];
free(foo);
Expand Down
155 changes: 95 additions & 60 deletions src/mp4file_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,29 @@ char* MP4File::ReadString()
uint32_t alloced = 64;
char* data = (char*)MP4Malloc(alloced);

do {
if (length == alloced) {
data = (char*)MP4Realloc(data, alloced * 2);
if (data == NULL) return NULL;
alloced *= 2;
}
ReadBytes((uint8_t*)&data[length], 1);
length++;
} while (data[length - 1] != 0);

data = (char*)MP4Realloc(data, length);
try
{
do {
if ( length == alloced )
{
data = (char*)MP4Realloc( data, alloced * 2 );
if ( data == NULL )
return NULL;
alloced *= 2;
}
ReadBytes( (uint8_t*)&data[length], 1 );
length++;
}
while ( data[length - 1] != 0 );

data = (char*)MP4Realloc( data, length );
}
catch (Exception*)
{
// free memory and retrhow
MP4Free( data );
throw;
}
return data;
}

Expand All @@ -368,55 +380,78 @@ void MP4File::WriteString(char* string)
}
}

char* MP4File::ReadCountedString(uint8_t charSize, bool allowExpandedCount, uint8_t fixedLength)
{
uint32_t charLength;
if (allowExpandedCount) {
uint8_t b;
uint32_t ix = 0;
charLength = 0;
do {
b = ReadUInt8();
charLength += b;
ix++;
if (ix > 25)
throw new PlatformException("Counted string too long 25 * 255",ERANGE,
__FILE__, __LINE__, __FUNCTION__);
} while (b == 255);
} else {
charLength = ReadUInt8();
}

if (fixedLength && (charLength > (uint8_t)(fixedLength - 1))) {
/*
* The counted length of this string is greater than the
* maxiumum fixed length, so truncate the string to the
* maximum fixed length amount (take 1 byte away from the
* fixedlength since we've already sacrificed one byte for
* reading the counted length, and there has been a bug where
* a non counted string has been used in the place of a
* counted string).
*/
WARNING( charLength > (uint8_t)( fixedLength - 1 ) );
charLength = fixedLength - 1U;
}

uint32_t byteLength = charLength * charSize;
char* data = (char*)MP4Malloc(byteLength + 1);
if (byteLength > 0) {
ReadBytes((uint8_t*)data, byteLength);
}
data[byteLength] = '\0';

// read padding
if (fixedLength) {
const uint8_t padsize = fixedLength - byteLength -1U;
if( padsize ) {
uint8_t* padbuf = (uint8_t*)malloc( padsize );
ReadBytes( padbuf, padsize );
free( padbuf );
}
}
char* MP4File::ReadCountedString( uint8_t charSize, bool allowExpandedCount, uint8_t fixedLength )
{
uint32_t charLength;
if ( allowExpandedCount )
{
uint8_t b;
uint32_t ix = 0;
charLength = 0;
do {
b = ReadUInt8();
charLength += b;
ix++;
if ( ix > 25 )
throw new PlatformException( "Counted string too long 25 * 255", ERANGE, __FILE__, __LINE__, __FUNCTION__ );
}
while ( b == 255 );
}
else
{
charLength = ReadUInt8();
}

if ( fixedLength && ( charLength > (uint8_t)( fixedLength - 1 ) ) )
{
/*
* The counted length of this string is greater than the
* maxiumum fixed length, so truncate the string to the
* maximum fixed length amount (take 1 byte away from the
* fixedlength since we've already sacrificed one byte for
* reading the counted length, and there has been a bug where
* a non counted string has been used in the place of a
* counted string).
*/
WARNING( charLength > (uint8_t)( fixedLength - 1 ) );
charLength = fixedLength - 1U;
}

uint32_t byteLength = charLength * charSize;
char* data = (char*)MP4Malloc( byteLength + 1 );
try
{
if ( byteLength > 0 )
{
ReadBytes( (uint8_t*)data, byteLength );
}
data[byteLength] = '\0';

// read padding
if ( fixedLength )
{
const uint8_t padsize = fixedLength - byteLength - 1U;
if ( padsize )
{
uint8_t* padbuf = (uint8_t*)MP4Malloc( padsize );
try
{
ReadBytes( padbuf, padsize );
MP4Free( padbuf );
}
catch ( Exception* )
{
MP4Free( padbuf );
throw;
}
}
}
}
catch (Exception*)
{
MP4Free( data );
throw;
}

return data;
}
Expand Down
14 changes: 11 additions & 3 deletions src/mp4property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,13 @@ MP4StringProperty::~MP4StringProperty()
}
}

void MP4StringProperty::SetCount(uint32_t count)
void MP4StringProperty::SetCount( uint32_t count )
{
uint32_t oldCount = m_values.Size();
uint32_t oldCount = m_values.Size();
for (uint32_t i = count; i < oldCount; ++i)
{
MP4Free( m_values[i] );
}

m_values.Resize(count);

Expand Down Expand Up @@ -508,7 +512,11 @@ MP4BytesProperty::~MP4BytesProperty()

void MP4BytesProperty::SetCount(uint32_t count)
{
uint32_t oldCount = m_values.Size();
uint32_t oldCount = m_values.Size();
for ( uint32_t i = count; i < oldCount; ++i )
{
MP4Free( m_values[i] );
}

m_values.Resize(count);
m_valueSizes.Resize(count);
Expand Down
6 changes: 6 additions & 0 deletions src/mp4track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,8 @@ uint64_t MP4Track::GetSampleFileOffset(MP4SampleId sampleId)

uint32_t samplesPerChunk =
m_pStscSamplesPerChunkProperty->GetValue(stscIndex);
if ( samplesPerChunk == 0u )
throw new Exception( "Invalid number of samples in stsc entry", __FILE__, __LINE__, __FUNCTION__ );

// chunkId tells which is the absolute chunk number that this sample
// is stored in.
Expand Down Expand Up @@ -1390,6 +1392,10 @@ bool MP4Track::IsSyncSample(MP4SampleId sampleId)
}

uint32_t numStss = m_pStssCountProperty->GetValue();
if (numStss == 0)
{
return false;
}
uint32_t stssLIndex = 0;
uint32_t stssRIndex = numStss - 1;

Expand Down