1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libyuv/source/mjpeg_validate.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,47 @@ 1.4 +/* 1.5 + * Copyright 2012 The LibYuv Project Authors. All rights reserved. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license 1.8 + * that can be found in the LICENSE file in the root of the source 1.9 + * tree. An additional intellectual property rights grant can be found 1.10 + * in the file PATENTS. All contributing project authors may 1.11 + * be found in the AUTHORS file in the root of the source tree. 1.12 + */ 1.13 + 1.14 +#include "libyuv/mjpeg_decoder.h" 1.15 + 1.16 +#ifdef __cplusplus 1.17 +namespace libyuv { 1.18 +extern "C" { 1.19 +#endif 1.20 + 1.21 +// Helper function to validate the jpeg appears intact. 1.22 +// TODO(fbarchard): Optimize case where SOI is found but EOI is not. 1.23 +LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) { 1.24 + size_t i; 1.25 + if (sample_size < 64) { 1.26 + // ERROR: Invalid jpeg size: sample_size 1.27 + return LIBYUV_FALSE; 1.28 + } 1.29 + if (sample[0] != 0xff || sample[1] != 0xd8) { // Start Of Image 1.30 + // ERROR: Invalid jpeg initial start code 1.31 + return LIBYUV_FALSE; 1.32 + } 1.33 + for (i = sample_size - 2; i > 1;) { 1.34 + if (sample[i] != 0xd9) { 1.35 + if (sample[i] == 0xff && sample[i + 1] == 0xd9) { // End Of Image 1.36 + return LIBYUV_TRUE; // Success: Valid jpeg. 1.37 + } 1.38 + --i; 1.39 + } 1.40 + --i; 1.41 + } 1.42 + // ERROR: Invalid jpeg end code not found. Size sample_size 1.43 + return LIBYUV_FALSE; 1.44 +} 1.45 + 1.46 +#ifdef __cplusplus 1.47 +} // extern "C" 1.48 +} // namespace libyuv 1.49 +#endif 1.50 +