digestutils.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // Copyright (C) 2019 James E. King III
  3. //
  4. // Permission to copy, use, modify, sell and
  5. // distribute this software is granted provided this copyright notice appears
  6. // in all copies. This software is provided "as is" without express or implied
  7. // warranty, and with no claim as to its suitability for any purpose.
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // https://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. #ifndef BOOST_UUID_TEST_ENDIAN_2019
  14. #define BOOST_UUID_TEST_ENDIAN_2019
  15. #include <boost/predef/other/endian.h>
  16. namespace boost {
  17. namespace uuids {
  18. namespace test {
  19. //
  20. // A utility to copy a raw digest out from one of the detail hash functions
  21. // to a byte string for comparisons in testing to known values.
  22. //
  23. static void copy_raw_digest(unsigned char* out, const unsigned int *in, size_t inlen)
  24. {
  25. for (size_t chunk = 0; chunk < inlen; ++chunk)
  26. {
  27. const unsigned char * cin = reinterpret_cast<const unsigned char *>(&in[chunk]);
  28. for (size_t byte = 0; byte < 4; ++byte)
  29. {
  30. #if BOOST_ENDIAN_LITTLE_BYTE
  31. *out++ = *(cin + (3-byte));
  32. #else
  33. *out++ = *(cin + byte);
  34. #endif
  35. }
  36. }
  37. }
  38. //
  39. // A utility to compare and report two raw hashes
  40. //
  41. static void test_digest_equal_array(char const * file, int line, char const * function,
  42. const unsigned char *lhs,
  43. const unsigned char *rhs,
  44. size_t len)
  45. {
  46. for (size_t i=0; i<len; i++) {
  47. if ( lhs[i] != rhs[i]) {
  48. std::cerr << file << "(" << line << "): digest [";
  49. for (size_t l=0; l<len; l++) {
  50. std::cerr << std::hex << (int)lhs[l];
  51. }
  52. std::cerr << "] not equal [";
  53. for (size_t r=0; r<len; r++) {
  54. std::cerr << std::hex << (int)rhs[r];
  55. }
  56. std::cerr << "] in function '" << function << "'" << std::endl;
  57. ++boost::detail::test_errors();
  58. return;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. #endif