diff options
Diffstat (limited to 'files/source/mjpeg_validate.cc')
-rw-r--r-- | files/source/mjpeg_validate.cc | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/files/source/mjpeg_validate.cc b/files/source/mjpeg_validate.cc index 80c2cc0c..ba0a03ab 100644 --- a/files/source/mjpeg_validate.cc +++ b/files/source/mjpeg_validate.cc @@ -18,10 +18,10 @@ extern "C" { #endif // Helper function to scan for EOI marker (0xff 0xd9). -static LIBYUV_BOOL ScanEOI(const uint8_t* sample, size_t sample_size) { - if (sample_size >= 2) { - const uint8_t* end = sample + sample_size - 1; - const uint8_t* it = sample; +static LIBYUV_BOOL ScanEOI(const uint8_t* src_mjpg, size_t src_size_mjpg) { + if (src_size_mjpg >= 2) { + const uint8_t* end = src_mjpg + src_size_mjpg - 1; + const uint8_t* it = src_mjpg; while (it < end) { // TODO(fbarchard): scan for 0xd9 instead. it = (const uint8_t*)(memchr(it, 0xff, end - it)); @@ -34,34 +34,35 @@ static LIBYUV_BOOL ScanEOI(const uint8_t* sample, size_t sample_size) { ++it; // Skip over current 0xff. } } - // ERROR: Invalid jpeg end code not found. Size sample_size + // ERROR: Invalid jpeg end code not found. Size src_size_mjpg return LIBYUV_FALSE; } // Helper function to validate the jpeg appears intact. -LIBYUV_BOOL ValidateJpeg(const uint8_t* sample, size_t sample_size) { +LIBYUV_BOOL ValidateJpeg(const uint8_t* src_mjpg, size_t src_size_mjpg) { // Maximum size that ValidateJpeg will consider valid. const size_t kMaxJpegSize = 0x7fffffffull; const size_t kBackSearchSize = 1024; - if (sample_size < 64 || sample_size > kMaxJpegSize || !sample) { - // ERROR: Invalid jpeg size: sample_size + if (src_size_mjpg < 64 || src_size_mjpg > kMaxJpegSize || !src_mjpg) { + // ERROR: Invalid jpeg size: src_size_mjpg return LIBYUV_FALSE; } - if (sample[0] != 0xff || sample[1] != 0xd8) { // SOI marker + // SOI marker + if (src_mjpg[0] != 0xff || src_mjpg[1] != 0xd8 || src_mjpg[2] != 0xff) { // ERROR: Invalid jpeg initial start code return LIBYUV_FALSE; } // Look for the End Of Image (EOI) marker near the end of the buffer. - if (sample_size > kBackSearchSize) { - if (ScanEOI(sample + sample_size - kBackSearchSize, kBackSearchSize)) { + if (src_size_mjpg > kBackSearchSize) { + if (ScanEOI(src_mjpg + src_size_mjpg - kBackSearchSize, kBackSearchSize)) { return LIBYUV_TRUE; // Success: Valid jpeg. } // Reduce search size for forward search. - sample_size = sample_size - kBackSearchSize + 1; + src_size_mjpg = src_size_mjpg - kBackSearchSize + 1; } // Step over SOI marker and scan for EOI. - return ScanEOI(sample + 2, sample_size - 2); + return ScanEOI(src_mjpg + 2, src_size_mjpg - 2); } #ifdef __cplusplus |