OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 The LibYuv Project Authors. All rights reserved. | 2 * Copyright 2012 The LibYuv Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 } | 35 } |
36 } | 36 } |
37 // ERROR: Invalid jpeg end code not found. Size sample_size | 37 // ERROR: Invalid jpeg end code not found. Size sample_size |
38 return LIBYUV_FALSE; | 38 return LIBYUV_FALSE; |
39 } | 39 } |
40 | 40 |
41 // Helper function to validate the jpeg appears intact. | 41 // Helper function to validate the jpeg appears intact. |
42 LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) { | 42 LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) { |
43 // Maximum size that ValidateJpeg will consider valid. | 43 // Maximum size that ValidateJpeg will consider valid. |
44 const size_t kMaxJpegSize = 0x7fffffffull; | 44 const size_t kMaxJpegSize = 0x7fffffffull; |
| 45 const size_t kBackSearchSize = 1024; |
45 if (sample_size < 64 || sample_size > kMaxJpegSize || !sample) { | 46 if (sample_size < 64 || sample_size > kMaxJpegSize || !sample) { |
46 // ERROR: Invalid jpeg size: sample_size | 47 // ERROR: Invalid jpeg size: sample_size |
47 return LIBYUV_FALSE; | 48 return LIBYUV_FALSE; |
48 } | 49 } |
49 if (sample[0] != 0xff || sample[1] != 0xd8) { // SOI marker | 50 if (sample[0] != 0xff || sample[1] != 0xd8) { // SOI marker |
50 // ERROR: Invalid jpeg initial start code | 51 // ERROR: Invalid jpeg initial start code |
51 return LIBYUV_FALSE; | 52 return LIBYUV_FALSE; |
52 } | 53 } |
| 54 |
| 55 // Look for the End Of Image (EOI) marker near the end of the buffer. |
| 56 if (sample_size > kBackSearchSize) { |
| 57 if (ScanEOI(sample + sample_size - kBackSearchSize, kBackSearchSize)) { |
| 58 return LIBYUV_TRUE; // Success: Valid jpeg. |
| 59 } |
| 60 // Reduce search size for forward search. |
| 61 sample_size = sample_size - kBackSearchSize + 1; |
| 62 } |
53 // Step over SOI marker and scan for EOI. | 63 // Step over SOI marker and scan for EOI. |
54 return ScanEOI(sample + 2, sample_size - 2); | 64 return ScanEOI(sample + 2, sample_size - 2); |
55 } | 65 } |
56 | 66 |
57 #ifdef __cplusplus | 67 #ifdef __cplusplus |
58 } // extern "C" | 68 } // extern "C" |
59 } // namespace libyuv | 69 } // namespace libyuv |
60 #endif | 70 #endif |
61 | 71 |
OLD | NEW |