2014-09-13 23:40:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 The LibYuv Project Authors. All rights reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libyuv/mjpeg_decoder.h"
|
|
|
|
|
2016-01-11 17:19:48 +00:00
|
|
|
#include <string.h> // For memchr.
|
|
|
|
|
2014-09-13 23:40:36 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
namespace libyuv {
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-01-11 17:19:48 +00:00
|
|
|
// Helper function to scan for EOI marker (0xff 0xd9).
|
|
|
|
static LIBYUV_BOOL ScanEOI(const uint8* sample, size_t sample_size) {
|
|
|
|
if (sample_size >= 2) {
|
|
|
|
const uint8* end = sample + sample_size - 1;
|
|
|
|
const uint8* it = sample;
|
|
|
|
while (it < end) {
|
|
|
|
// TODO(fbarchard): scan for 0xd9 instead.
|
|
|
|
it = static_cast<const uint8 *>(memchr(it, 0xff, end - it));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-13 23:40:36 +00:00
|
|
|
// Helper function to validate the jpeg appears intact.
|
|
|
|
LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) {
|
2016-01-11 17:19:48 +00:00
|
|
|
// 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) {
|
2014-09-13 23:40:36 +00:00
|
|
|
// ERROR: Invalid jpeg size: sample_size
|
|
|
|
return LIBYUV_FALSE;
|
|
|
|
}
|
2016-01-11 17:19:48 +00:00
|
|
|
if (sample[0] != 0xff || sample[1] != 0xd8) { // SOI marker
|
2014-09-13 23:40:36 +00:00
|
|
|
// ERROR: Invalid jpeg initial start code
|
|
|
|
return LIBYUV_FALSE;
|
|
|
|
}
|
2016-01-11 17:19:48 +00:00
|
|
|
|
|
|
|
// 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.
|
2014-09-13 23:40:36 +00:00
|
|
|
}
|
2016-01-11 17:19:48 +00:00
|
|
|
// Reduce search size for forward search.
|
|
|
|
sample_size = sample_size - kBackSearchSize + 1;
|
2014-09-13 23:40:36 +00:00
|
|
|
}
|
2016-01-11 17:19:48 +00:00
|
|
|
// Step over SOI marker and scan for EOI.
|
|
|
|
return ScanEOI(sample + 2, sample_size - 2);
|
2014-09-13 23:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
} // extern "C"
|
|
|
|
} // namespace libyuv
|
|
|
|
#endif
|
|
|
|
|