libptpmgmt  1.4
libptpmgmt library that provides the functionality of linuxptp pmc
bin.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-3.0-or-later
2  SPDX-FileCopyrightText: Copyright © 2021 Erez Geva <ErezGeva2@gmail.com> */
3 
12 #ifndef __PTPMGMT_BIN_H
13 #define __PTPMGMT_BIN_H
14 
15 #ifdef __cplusplus
16 #include "name.h"
17 
18 __PTPMGMT_NAMESPACE_BEGIN
19 
20 const size_t EUI48 = 6;
21 const size_t EUI64 = 8;
27 class Binary
28 {
29  private:
30  uint8_t *m_buf = nullptr; /* Used buffer */
31  size_t m_alloc = 0; /* Allocation size */
32  size_t m_size = 0; /* Binary size */
33  bool iResize(size_t newAllocSize);
34  public:
35  Binary();
36  ~Binary();
41  Binary(const Binary &rhs);
47  Binary(const void *buf, const size_t length);
53  Binary(const size_t length, uint8_t set = 0);
58  Binary(const std::string &string);
64  Binary &operator=(const Binary &rhs);
69  size_t length() const { return m_size; }
74  size_t size() const { return m_size; }
79  bool empty() const { return m_size == 0; }
84  const uint8_t *get() const { return m_buf; }
91  std::string getBinString() const { return std::string((char *)m_buf, m_size); }
98  Binary &setBin(const void *buf, const size_t length);
106  Binary &setBin(const Binary &rhs);
114  Binary &setBin(const std::string &string);
122  Binary &setBin(const size_t position, const uint8_t value);
128  const uint8_t getBin(const size_t position) const;
135  uint8_t &operator [](const size_t position);
140  Binary &resize(const size_t length);
149  void copy(uint8_t *target) const;
156  Binary &operator += (const uint8_t octet) { return append(octet); }
162  Binary &append(const uint8_t octet);
169  Binary &operator += (const Binary &rhs) { return append(rhs); }
175  Binary &append(const Binary &rhs);
183  std::string toIp() const;
190  bool fromIp(const std::string &string);
198  bool fromIp(const std::string &string, int domain);
205  std::string toId(const std::string &sep = ":") const {
206  return bufToId(m_buf, m_size, sep);
207  }
216  static std::string bufToId(const uint8_t *id, size_t length);
225  static std::string bufToId(const uint8_t *id, size_t length,
226  const std::string &sep);
233  bool fromId(const std::string &string);
239  bool fromMac(const std::string &string) { return fromId(string) && isMacLen(); }
244  bool isMacLen() const { return m_size == EUI48 || m_size == EUI64; }
249  bool eui48ToEui64();
255  bool fromHex(const std::string &hex);
260  std::string toHex() const { return bufToHex(m_buf, m_size); }
267  static std::string bufToHex(const uint8_t *bin, size_t length);
279  bool fromBase64(const std::string &bin64, bool pad = false);
290  std::string toBase64(bool pad = true, char v62 = '+', char v63 = '/');
296  bool operator==(const Binary &rhs) const { return eq(rhs); }
302  bool eq(const Binary &rhs) const;
308  bool operator<(const Binary &rhs) const { return less(rhs); }
314  bool less(const Binary &rhs) const;
315 };
316 
317 __PTPMGMT_NAMESPACE_END
318 #else /* __cplusplus */
319 #error "this header is for C++ and scripts use only, NOT for C"
320 #endif /* __cplusplus */
321 
322 #endif /* __PTPMGMT_BIN_H */
__PTPMGMT_NAMESPACE_BEGIN const size_t EUI48
Definition: bin.h:20
const size_t EUI64
Definition: bin.h:21
Hold octets.
Definition: bin.h:28
Binary & setBin(const size_t position, const uint8_t value)
std::string toHex() const
Definition: bin.h:260
Binary & operator+=(const uint8_t octet)
Definition: bin.h:156
Binary & operator=(const Binary &rhs)
std::string getBinString() const
Definition: bin.h:91
std::string toId(const std::string &sep=":") const
Definition: bin.h:205
Binary & resize(const size_t length)
Binary & append(const uint8_t octet)
Binary & setBin(const void *buf, const size_t length)
static std::string bufToHex(const uint8_t *bin, size_t length)
Binary & append(const Binary &rhs)
bool fromMac(const std::string &string)
Definition: bin.h:239
bool fromBase64(const std::string &bin64, bool pad=false)
static std::string bufToId(const uint8_t *id, size_t length)
static std::string bufToId(const uint8_t *id, size_t length, const std::string &sep)
Binary(const std::string &string)
bool fromIp(const std::string &string, int domain)
size_t size() const
Definition: bin.h:74
Binary(const size_t length, uint8_t set=0)
bool isMacLen() const
Definition: bin.h:244
std::string toBase64(bool pad=true, char v62='+', char v63='/')
Binary & setBin(const std::string &string)
bool fromHex(const std::string &hex)
bool eq(const Binary &rhs) const
bool fromIp(const std::string &string)
uint8_t & operator[](const size_t position)
Binary & setBin(const Binary &rhs)
Binary(const void *buf, const size_t length)
Binary & clear()
size_t length() const
Definition: bin.h:69
const uint8_t getBin(const size_t position) const
bool fromId(const std::string &string)
Binary(const Binary &rhs)
void copy(uint8_t *target) const
bool empty() const
Definition: bin.h:79
std::string toIp() const
bool operator<(const Binary &rhs) const
Definition: bin.h:308
bool eui48ToEui64()
bool operator==(const Binary &rhs) const
Definition: bin.h:296
bool less(const Binary &rhs) const
const uint8_t * get() const
Definition: bin.h:84
Define name space.