md5_helper.h 2.1 KB

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