blob: da906c87e6e97bd0c13d49b4eeaf194bf6da361f [file] [edit]
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// Internal utilities for Ethernet address manipulation.
///
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_NETINET_ETHER_ETHER_UTILS_H
#define LLVM_LIBC_SRC_NETINET_ETHER_ETHER_UTILS_H
#include "hdr/stdint_proxy.h"
#include "hdr/types/size_t.h"
#include "hdr/types/struct_ether_addr.h"
#include "src/__support/CPP/span.h"
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/macros/config.h"
namespace LIBC_NAMESPACE_DECL {
namespace net {
/// Maximum length of an Ethernet address string "xx:xx:xx:xx:xx:xx\0".
inline constexpr size_t ETHER_ADDR_STR_LEN = 18;
/// Formats \p addr into \p buf (which must be at least ETHER_ADDR_STR_LEN
/// bytes) as a colon-separated lowercase hexadecimal string and returns
/// `buf.data()`.
LIBC_INLINE char *ether_ntoa_r(const struct ether_addr &addr,
cpp::span<char> buf) {
size_t pos = 0;
for (size_t i = 0; i < 6; ++i) {
uint8_t val = addr.ether_addr_octet[i];
if (val >= 0x10)
buf[pos++] = internal::int_to_b36_char(val >> 4);
buf[pos++] = internal::int_to_b36_char(val & 0xf);
buf[pos++] = i < 5 ? ':' : '\0';
}
return buf.data();
}
} // namespace net
} // namespace LIBC_NAMESPACE_DECL
#endif // LLVM_LIBC_SRC_NETINET_ETHER_ETHER_UTILS_H