Index: source/mjpeg_validate.cc |
diff --git a/source/mjpeg_validate.cc b/source/mjpeg_validate.cc |
index fdb8d6ced0d1fc20b16471b8a127cde4f93378b4..9c4883204517bcfd8a5d322356c0e8a1438a3aba 100644 |
--- a/source/mjpeg_validate.cc |
+++ b/source/mjpeg_validate.cc |
@@ -42,6 +42,7 @@ static LIBYUV_BOOL ScanEOI(const uint8* sample, size_t sample_size) { |
LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) { |
// 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 |
return LIBYUV_FALSE; |
@@ -50,6 +51,15 @@ LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) { |
// 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)) { |
+ return LIBYUV_TRUE; // Success: Valid jpeg. |
+ } |
+ // Reduce search size for forward search. |
+ sample_size = sample_size - kBackSearchSize + 1; |
+ } |
// Step over SOI marker and scan for EOI. |
return ScanEOI(sample + 2, sample_size - 2); |
} |