SampleTable.cpp: Fixed a regression caused by a fix for bug
28076789.

Detail: Before the original fix
(Id207f369ab7b27787d83f5d8fc48dc53ed9fcdc9) for 28076789, the
code allowed a time-to-sample table size to be 0. The change
made in that fix disallowed such situation, which in fact should
be allowed. This current patch allows it again while maintaining
the security of the previous fix.

Bug: 28288202
Bug: 28076789
Change-Id: I1c9a60c7f0cfcbd3d908f24998dde15d5136a295

status_t SampleTable::setSampleSizeParams(
 uint32_t type, off64_t data_offset, size_t data_size) {
 if (mSampleSizeOffset >= 0) {
 return ERROR_MALFORMED;
 }

    CHECK(type == kSampleSizeType32 || type == kSampleSizeTypeCompact);

    mSampleSizeOffset = data_offset;

 if (data_size < 12) {
 return ERROR_MALFORMED;
 }

 uint8_t header[12];
 if (mDataSource->readAt(
                data_offset, header, sizeof(header)) < (ssize_t)sizeof(header)) {
 return ERROR_IO;
 }

 if (U32_AT(header) != 0) {
 return ERROR_MALFORMED;
 }

    mDefaultSampleSize = U32_AT(&header[4]);
    mNumSampleSizes = U32_AT(&header[8]);
 if (mNumSampleSizes > (UINT32_MAX - 12) / 16) {
 return ERROR_MALFORMED;
 }

 if (type == kSampleSizeType32) {
        mSampleSizeFieldSize = 32;

 if (mDefaultSampleSize != 0) {
 return OK;
 }

 if (data_size < 12 + mNumSampleSizes * 4) {
 return ERROR_MALFORMED;
 }
 } else {
 if ((mDefaultSampleSize & 0xffffff00) != 0) {
 return ERROR_MALFORMED;
 }

        mSampleSizeFieldSize = mDefaultSampleSize & 0xff;
        mDefaultSampleSize = 0;

 if (mSampleSizeFieldSize != 4 && mSampleSizeFieldSize != 8
 && mSampleSizeFieldSize != 16) {
 return ERROR_MALFORMED;
 }

 if (data_size < 12 + (mNumSampleSizes * mSampleSizeFieldSize + 4) / 8) {
 return ERROR_MALFORMED;
 }
 }

 return OK;
}

status_t SampleTable::setSampleSizeParams(
 uint32_t type, off64_t data_offset, size_t data_size) {
 if (mSampleSizeOffset >= 0) {
 return ERROR_MALFORMED;
 }

    CHECK(type == kSampleSizeType32 || type == kSampleSizeTypeCompact);

    mSampleSizeOffset = data_offset;

 if (data_size < 12) {
 return ERROR_MALFORMED;
 }

 uint8_t header[12];
 if (mDataSource->readAt(
                data_offset, header, sizeof(header)) < (ssize_t)sizeof(header)) {
 return ERROR_IO;
 }

 if (U32_AT(header) != 0) {
 return ERROR_MALFORMED;
 }

    mDefaultSampleSize = U32_AT(&header[4]);
    mNumSampleSizes = U32_AT(&header[8]);
 if (mNumSampleSizes > (UINT32_MAX - 12) / 16) {
 return ERROR_MALFORMED;
 }

 if (type == kSampleSizeType32) {
        mSampleSizeFieldSize = 32;

 if (mDefaultSampleSize != 0) {
 return OK;
 }

 if (data_size < 12 + mNumSampleSizes * 4) {
 return ERROR_MALFORMED;
 }
 } else {
 if ((mDefaultSampleSize & 0xffffff00) != 0) {
 return ERROR_MALFORMED;
 }

        mSampleSizeFieldSize = mDefaultSampleSize & 0xff;
        mDefaultSampleSize = 0;

 if (mSampleSizeFieldSize != 4 && mSampleSizeFieldSize != 8
 && mSampleSizeFieldSize != 16) {
 return ERROR_MALFORMED;
 }

 if (data_size < 12 + (mNumSampleSizes * mSampleSizeFieldSize + 4) / 8) {
 return ERROR_MALFORMED;
 }
 }

 return OK;
}
