NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/trace/propagation/detail/hex.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include <cstdint>
8 #include <cstring>
9 
10 #include "opentelemetry/nostd/string_view.h"
11 #include "opentelemetry/version.h"
12 
14 namespace trace
15 {
16 namespace propagation
17 {
18 // NOTE - code within `detail` namespace implements internal details, and not part
19 // of the public interface.
20 namespace detail
21 {
22 
23 constexpr int8_t kHexDigits[256] = {
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  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -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, 10, 11, 12, 13, 14, 15, -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, -1, -1, -1, -1, -1, -1, -1, -1,
30  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35 };
36 
37 inline int8_t HexToInt(char c)
38 {
39  return kHexDigits[uint8_t(c)];
40 }
41 
43 {
44  return std::all_of(s.begin(), s.end(), [](char c) { return HexToInt(c) != -1; });
45 }
46 
51 inline bool HexToBinary(nostd::string_view hex, uint8_t *buffer, size_t buffer_size)
52 {
53  std::memset(buffer, 0, buffer_size);
54 
55  if (hex.size() > buffer_size * 2)
56  {
57  return false;
58  }
59 
60  int64_t hex_size = int64_t(hex.size());
61  int64_t buffer_pos = int64_t(buffer_size) - (hex_size + 1) / 2;
62  int64_t last_hex_pos = hex_size - 1;
63 
64  bool is_hex_size_odd = (hex_size % 2) == 1;
65  int64_t i = 0;
66 
67  if (is_hex_size_odd)
68  {
69  buffer[buffer_pos++] = HexToInt(hex[i++]);
70  }
71 
72  for (; i < last_hex_pos; i += 2)
73  {
74  buffer[buffer_pos++] = (HexToInt(hex[i]) << 4) | HexToInt(hex[i + 1]);
75  }
76 
77  return true;
78 }
79 
80 } // namespace detail
81 } // namespace propagation
82 } // namespace trace
trace
Definition: includes/opentelemetry/context/context_value.h:18
nostd::string_view::begin
const char * begin() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:64
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
nostd::string_view::size
size_type size() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:62
nostd::string_view::end
const char * end() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:66
trace::propagation::detail::IsValidHex
bool IsValidHex(nostd::string_view s)
Definition: includes/opentelemetry/trace/propagation/detail/hex.h:42
trace::propagation::detail::kHexDigits
constexpr int8_t kHexDigits[256]
Definition: includes/opentelemetry/trace/propagation/detail/hex.h:23
trace::propagation::detail::HexToInt
int8_t HexToInt(char c)
Definition: includes/opentelemetry/trace/propagation/detail/hex.h:37
trace::propagation::detail::HexToBinary
bool HexToBinary(nostd::string_view hex, uint8_t *buffer, size_t buffer_size)
Converts a hexadecimal to binary format if the hex string will fit the buffer.
Definition: includes/opentelemetry/trace/propagation/detail/hex.h:51
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37