hex.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include <algorithm>
  5. #include <cstdint>
  6. #include <cstring>
  7. #include "opentelemetry/nostd/string_view.h"
  8. #include "opentelemetry/version.h"
  9. OPENTELEMETRY_BEGIN_NAMESPACE
  10. namespace trace
  11. {
  12. namespace propagation
  13. {
  14. // NOTE - code within `detail` namespace implements internal details, and not part
  15. // of the public interface.
  16. namespace detail
  17. {
  18. constexpr int8_t kHexDigits[256] = {
  19. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  20. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  21. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1,
  22. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  23. -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  24. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  25. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  26. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  27. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  28. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  29. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  30. };
  31. inline int8_t HexToInt(char c)
  32. {
  33. return kHexDigits[uint8_t(c)];
  34. }
  35. inline bool IsValidHex(nostd::string_view s)
  36. {
  37. return std::all_of(s.begin(), s.end(), [](char c) { return HexToInt(c) != -1; });
  38. }
  39. /**
  40. * Converts a hexadecimal to binary format if the hex string will fit the buffer.
  41. * Smaller hex strings are left padded with zeroes.
  42. */
  43. inline bool HexToBinary(nostd::string_view hex, uint8_t *buffer, size_t buffer_size)
  44. {
  45. std::memset(buffer, 0, buffer_size);
  46. if (hex.size() > buffer_size * 2)
  47. {
  48. return false;
  49. }
  50. int64_t hex_size = int64_t(hex.size());
  51. int64_t buffer_pos = int64_t(buffer_size) - (hex_size + 1) / 2;
  52. int64_t last_hex_pos = hex_size - 1;
  53. bool is_hex_size_odd = (hex_size % 2) == 1;
  54. int64_t i = 0;
  55. if (is_hex_size_odd)
  56. {
  57. buffer[buffer_pos++] = HexToInt(hex[i++]);
  58. }
  59. for (; i < last_hex_pos; i += 2)
  60. {
  61. buffer[buffer_pos++] = (HexToInt(hex[i]) << 4) | HexToInt(hex[i + 1]);
  62. }
  63. return true;
  64. }
  65. } // namespace detail
  66. } // namespace propagation
  67. } // namespace trace
  68. OPENTELEMETRY_END_NAMESPACE