Commit 194f740d authored by fbarchard@google.com's avatar fbarchard@google.com

Scan from start of buffer to handle case where an invalid size was passed.

BUG=404
TESTED=libyuvTest.ValidateJpegLarge
R=brucedawson@google.com, harryjin@google.com

Review URL: https://webrtc-codereview.appspot.com/41989004

git-svn-id: http://libyuv.googlecode.com/svn/trunk@1285 16f28f9a-4ce2-e073-06de-1de4eb20be90
parent a965a97d
Name: libyuv Name: libyuv
URL: http://code.google.com/p/libyuv/ URL: http://code.google.com/p/libyuv/
Version: 1284 Version: 1285
License: BSD License: BSD
License File: LICENSE License File: LICENSE
......
...@@ -11,6 +11,6 @@ ...@@ -11,6 +11,6 @@
#ifndef INCLUDE_LIBYUV_VERSION_H_ // NOLINT #ifndef INCLUDE_LIBYUV_VERSION_H_ // NOLINT
#define INCLUDE_LIBYUV_VERSION_H_ #define INCLUDE_LIBYUV_VERSION_H_
#define LIBYUV_VERSION 1284 #define LIBYUV_VERSION 1285
#endif // INCLUDE_LIBYUV_VERSION_H_ NOLINT #endif // INCLUDE_LIBYUV_VERSION_H_ NOLINT
...@@ -10,15 +10,66 @@ ...@@ -10,15 +10,66 @@
#include "libyuv/mjpeg_decoder.h" #include "libyuv/mjpeg_decoder.h"
#include <string.h> // For memchr.
#ifdef __cplusplus #ifdef __cplusplus
namespace libyuv { namespace libyuv {
extern "C" { extern "C" {
#endif #endif
// Enable this to try scasb implementation.
// #define ENABLE_SCASB 1
#ifdef ENABLE_SCASB
// Multiple of 1.
__declspec(naked) __declspec(align(16))
const uint8* ScanRow_ERMS(const uint8* src, uint32 val, int count) {
__asm {
mov edx, edi
mov edi, [esp + 4] // src
mov eax, [esp + 8] // val
mov ecx, [esp + 12] // count
repne scasb
jne sr99
mov eax, edi
sub eax, 1
mov edi, edx
ret
sr99:
mov eax, 0
mov edi, edx
ret
}
}
#endif
// Helper function to scan for EOI marker.
static LIBYUV_BOOL ScanEOI(const uint8* sample, size_t sample_size) {
const uint8* end = sample + sample_size - 1;
const uint8* it = sample;
for (;;) {
#ifdef ENABLE_SCASB
it = ScanRow_ERMS(it, 0xff, end - it);
#else
it = static_cast<const uint8*>(memchr(it, 0xff, end - it));
#endif
if (it == NULL) {
break;
}
if (it[1] == 0xd9) {
return LIBYUV_TRUE; // Success: Valid jpeg.
}
++it; // Skip over current 0xff.
}
// ERROR: Invalid jpeg end code not found. Size sample_size
return LIBYUV_FALSE;
}
// Helper function to validate the jpeg appears intact. // Helper function to validate the jpeg appears intact.
// TODO(fbarchard): Optimize case where SOI is found but EOI is not.
LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) { LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) {
size_t i; const size_t kBackSearchSize = 1024;
if (sample_size < 64) { if (sample_size < 64) {
// ERROR: Invalid jpeg size: sample_size // ERROR: Invalid jpeg size: sample_size
return LIBYUV_FALSE; return LIBYUV_FALSE;
...@@ -27,17 +78,20 @@ LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) { ...@@ -27,17 +78,20 @@ LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) {
// ERROR: Invalid jpeg initial start code // ERROR: Invalid jpeg initial start code
return LIBYUV_FALSE; return LIBYUV_FALSE;
} }
for (i = sample_size - 2; i > 1;) { // Step over SOI marker.
if (sample[i] != 0xd9) { sample += 2;
if (sample[i] == 0xff && sample[i + 1] == 0xd9) { // End Of Image sample_size -= 2;
// Look for the End Of Image (EOI) marker in the end kilobyte of the buffer.
if (sample_size > kBackSearchSize) {
if (ScanEOI(sample + sample_size - kBackSearchSize, kBackSearchSize)) {
return LIBYUV_TRUE; // Success: Valid jpeg. return LIBYUV_TRUE; // Success: Valid jpeg.
} }
--i; // Reduce search size for forward search.
} sample_size = sample_size - kBackSearchSize + 1;
--i;
} }
// ERROR: Invalid jpeg end code not found. Size sample_size return ScanEOI(sample, sample_size);
return LIBYUV_FALSE;
} }
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -1035,7 +1035,8 @@ TEST_F(libyuvTest, ValidateJpegLarge) { ...@@ -1035,7 +1035,8 @@ TEST_F(libyuvTest, ValidateJpegLarge) {
const int kImageSize = benchmark_width_ * benchmark_height_ >= kMinJpeg ? const int kImageSize = benchmark_width_ * benchmark_height_ >= kMinJpeg ?
benchmark_width_ * benchmark_height_ : kMinJpeg; benchmark_width_ * benchmark_height_ : kMinJpeg;
const int kSize = kImageSize + kOff; const int kSize = kImageSize + kOff;
const int kBufSize = kImageSize * 10 + kOff; const int kMultiple = 10;
const int kBufSize = kImageSize * kMultiple + kOff;
align_buffer_64(orig_pixels, kBufSize); align_buffer_64(orig_pixels, kBufSize);
// No SOI or EOI. Expect fail. // No SOI or EOI. Expect fail.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment