proxygen
folly::detail::Bytes Struct Reference

#include <IPAddressSource.h>

Static Public Member Functions

template<std::size_t N>
static std::array< uint8_t, N > mask (const std::array< uint8_t, N > &a, const std::array< uint8_t, N > &b)
 
template<std::size_t N>
static std::pair< std::array< uint8_t, N >, uint8_tlongestCommonPrefix (const std::array< uint8_t, N > &one, uint8_t oneMask, const std::array< uint8_t, N > &two, uint8_t twoMask)
 
static in_addr mkAddress4 (const uint8_t *src)
 
static in6_addr mkAddress6 (const uint8_t *src)
 
static std::string toHex (const uint8_t *src, std::size_t len)
 

Private Member Functions

 Bytes ()=delete
 
 ~Bytes ()=delete
 

Detailed Description

Helper for working with unsigned char* or uint8_t* ByteArray values

Definition at line 44 of file IPAddressSource.h.

Constructor & Destructor Documentation

folly::detail::Bytes::Bytes ( )
privatedelete

Referenced by toHex().

folly::detail::Bytes::~Bytes ( )
privatedelete

Referenced by toHex().

Member Function Documentation

template<std::size_t N>
static std::pair<std::array<uint8_t, N>, uint8_t> folly::detail::Bytes::longestCommonPrefix ( const std::array< uint8_t, N > &  one,
uint8_t  oneMask,
const std::array< uint8_t, N > &  two,
uint8_t  twoMask 
)
inlinestatic

Definition at line 60 of file IPAddressSource.h.

References mask(), max, min, folly::sformat(), and uint8_t.

Referenced by folly::IPAddressV4::longestCommonPrefix(), and folly::IPAddressV6::longestCommonPrefix().

64  {
65  static constexpr auto kBitCount = N * 8;
66  static constexpr std::array<uint8_t, 8> kMasks{{
67  0x80, // /1
68  0xc0, // /2
69  0xe0, // /3
70  0xf0, // /4
71  0xf8, // /5
72  0xfc, // /6
73  0xfe, // /7
74  0xff // /8
75  }};
76  if (oneMask > kBitCount || twoMask > kBitCount) {
77  throw std::invalid_argument(sformat(
78  "Invalid mask length: {}. Mask length must be <= {}",
79  std::max(oneMask, twoMask),
80  kBitCount));
81  }
82 
83  auto mask = std::min(oneMask, twoMask);
84  uint8_t byteIndex = 0;
85  std::array<uint8_t, N> ba{{0}};
86  // Compare a byte at a time. Note - I measured compared this with
87  // going multiple bytes at a time (8, 4, 2 and 1). It turns out
88  // to be 20 - 25% slower for 4 and 16 byte arrays.
89  while (byteIndex * 8 < mask && one[byteIndex] == two[byteIndex]) {
90  ba[byteIndex] = one[byteIndex];
91  ++byteIndex;
92  }
93  auto bitIndex = std::min(mask, uint8_t(byteIndex * 8));
94  uint8_t bI = uint8_t(bitIndex / 8);
95  uint8_t bM = uint8_t(bitIndex % 8);
96  // Compute the bit up to which the two byte arrays match in the
97  // unmatched byte.
98  // Here the check is bitIndex < mask since the 0th mask entry in
99  // kMasks array holds the mask for masking the MSb in this byte.
100  // We could instead make it hold so that no 0th entry masks no
101  // bits but thats a useless iteration.
102  while (bitIndex < mask &&
103  ((one[bI] & kMasks[bM]) == (two[bI] & kMasks[bM]))) {
104  ba[bI] = uint8_t(one[bI] & kMasks[bM]);
105  ++bitIndex;
106  bI = uint8_t(bitIndex / 8);
107  bM = uint8_t(bitIndex % 8);
108  }
109  return {ba, bitIndex};
110  }
LogLevel max
Definition: LogLevel.cpp:31
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
static std::array< uint8_t, N > mask(const std::array< uint8_t, N > &a, const std::array< uint8_t, N > &b)
LogLevel min
Definition: LogLevel.cpp:30
template<std::size_t N>
static std::array<uint8_t, N> folly::detail::Bytes::mask ( const std::array< uint8_t, N > &  a,
const std::array< uint8_t, N > &  b 
)
inlinestatic

Definition at line 47 of file IPAddressSource.h.

References i, and uint8_t.

Referenced by folly::IPAddressV4::inSubnetWithMask(), folly::IPAddressV6::inSubnetWithMask(), longestCommonPrefix(), folly::IPAddressV4::mask(), and folly::IPAddressV6::mask().

49  {
50  static_assert(N > 0, "Can't mask an empty ByteArray");
51  std::size_t asize = a.size();
52  std::array<uint8_t, N> ba{{0}};
53  for (std::size_t i = 0; i < asize; i++) {
54  ba[i] = uint8_t(a[i] & b[i]);
55  }
56  return ba;
57  }
static in_addr folly::detail::Bytes::mkAddress4 ( const uint8_t src)
inlinestatic

Definition at line 113 of file IPAddressSource.h.

References addr, and uint8_t.

Referenced by folly::IPAddressV6::createIPv4(), and TEST_P().

113  {
114  union {
115  in_addr addr;
116  uint8_t bytes[4];
117  } addr;
118  std::memset(&addr, 0, 4);
119  std::memcpy(addr.bytes, src, 4);
120  return addr.addr;
121  }
ThreadPoolListHook * addr
static in6_addr folly::detail::Bytes::mkAddress6 ( const uint8_t src)
inlinestatic

Definition at line 124 of file IPAddressSource.h.

References addr.

Referenced by TEST_P().

124  {
125  in6_addr addr;
126  std::memset(&addr, 0, 16);
127  std::memcpy(addr.s6_addr, src, 16);
128  return addr;
129  }
ThreadPoolListHook * addr
static std::string folly::detail::Bytes::toHex ( const uint8_t src,
std::size_t  len 
)
inlinestatic

Definition at line 132 of file IPAddressSource.h.

References Bytes(), c, i, string, type, value, and ~Bytes().

Referenced by folly::IPAddressV6::str(), TEST(), and folly::toAppend().

132  {
133  static const char* const lut = "0123456789abcdef";
134  std::string out(len * 2, 0);
135  for (std::size_t i = 0; i < len; i++) {
136  const unsigned char c = src[i];
137  out[i * 2 + 0] = lut[c >> 4];
138  out[i * 2 + 1] = lut[c & 15];
139  }
140  return out;
141  }
const char * string
Definition: Conv.cpp:212
char c

The documentation for this struct was generated from the following file: