1/*
2 *  Copyright 2012 The LibYuv Project Authors. All rights reserved.
3 *
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
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS. All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "libyuv/mjpeg_decoder.h"
12
13#ifdef __cplusplus
14namespace libyuv {
15extern "C" {
16#endif
17
18// Helper function to validate the jpeg appears intact.
19// TODO(fbarchard): Optimize case where SOI is found but EOI is not.
20LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size) {
21  size_t i;
22  if (sample_size < 64) {
23    // ERROR: Invalid jpeg size: sample_size
24    return LIBYUV_FALSE;
25  }
26  if (sample[0] != 0xff || sample[1] != 0xd8) {  // Start Of Image
27    // ERROR: Invalid jpeg initial start code
28    return LIBYUV_FALSE;
29  }
30  for (i = sample_size - 2; i > 1;) {
31    if (sample[i] != 0xd9) {
32      if (sample[i] == 0xff && sample[i + 1] == 0xd9) {  // End Of Image
33        return LIBYUV_TRUE;  // Success: Valid jpeg.
34      }
35      --i;
36    }
37    --i;
38  }
39  // ERROR: Invalid jpeg end code not found. Size sample_size
40  return LIBYUV_FALSE;
41}
42
43#ifdef __cplusplus
44}  // extern "C"
45}  // namespace libyuv
46#endif
47
48