1 /*
2  *  Copyright (c) 2012 The WebM 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 #ifndef TEST_MD5_HELPER_H_
12 #define TEST_MD5_HELPER_H_
13 
14 #include "./md5_utils.h"
15 #include "vpx/vpx_decoder.h"
16 
17 namespace libvpx_test {
18 class MD5 {
19  public:
MD5()20   MD5() {
21     MD5Init(&md5_);
22   }
23 
Add(const vpx_image_t * img)24   void Add(const vpx_image_t *img) {
25     for (int plane = 0; plane < 3; ++plane) {
26       const uint8_t *buf = img->planes[plane];
27       // Calculate the width and height to do the md5 check. For the chroma
28       // plane, we never want to round down and thus skip a pixel so if
29       // we are shifting by 1 (chroma_shift) we add 1 before doing the shift.
30       // This works only for chroma_shift of 0 and 1.
31       const int bytes_per_sample =
32           (img->fmt & VPX_IMG_FMT_HIGHBITDEPTH) ? 2 : 1;
33       const int h = plane ? (img->d_h + img->y_chroma_shift) >>
34                     img->y_chroma_shift : img->d_h;
35       const int w = (plane ? (img->d_w + img->x_chroma_shift) >>
36                      img->x_chroma_shift : img->d_w) * bytes_per_sample;
37 
38       for (int y = 0; y < h; ++y) {
39         MD5Update(&md5_, buf, w);
40         buf += img->stride[plane];
41       }
42     }
43   }
44 
Add(const uint8_t * data,size_t size)45   void Add(const uint8_t *data, size_t size) {
46     MD5Update(&md5_, data, static_cast<uint32_t>(size));
47   }
48 
Get(void)49   const char *Get(void) {
50     static const char hex[16] = {
51       '0', '1', '2', '3', '4', '5', '6', '7',
52       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
53     };
54     uint8_t tmp[16];
55     MD5Context ctx_tmp = md5_;
56 
57     MD5Final(tmp, &ctx_tmp);
58     for (int i = 0; i < 16; i++) {
59       res_[i * 2 + 0]  = hex[tmp[i] >> 4];
60       res_[i * 2 + 1]  = hex[tmp[i] & 0xf];
61     }
62     res_[32] = 0;
63 
64     return res_;
65   }
66 
67  protected:
68   char res_[33];
69   MD5Context md5_;
70 };
71 
72 }  // namespace libvpx_test
73 
74 #endif  // TEST_MD5_HELPER_H_
75