Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
uid.C
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31# include <random>
32# include <cstring>
33# include <cctype>
34# include <cstdint>
35# include <uid.H>
36# include <ah-errors.H>
37
38
39typedef unsigned char Byte;
40
41static char * hexadecimalize(const Byte & byte, char *& str)
42{
43 const Byte l = byte & 0x0F;
44 const Byte h = (byte & 0xF0) >> 4;
45
46 *str++ = Aleph::nibble_to_char(h);
47 *str++ = Aleph::nibble_to_char(l);
48
49 return str;
50}
51
52
53static char unhexadecimalize(char *& str)
54{
55 const Byte h = Aleph::char_to_nibble(*str++) << 4;
56 const Byte l = Aleph::char_to_nibble(*str++);
57
58 return h | l;
59}
60
61
62char * Uid::stringficate(char *buffer,
63 const size_t & src_size) const
64{
65 ah_range_error_if(src_size < 2*sizeof(Uid) + 1)
66 << "Buffer size is not enough";
67
68 auto this_str = (char *) this;
69 char *ret_val = buffer;
70
71 // copiar nibbles convertidas a ascii a buffer
72 for (int i = 0; i < sizeof(Uid); ++i)
73 hexadecimalize(*this_str++, buffer);
74
75 assert(this_str - (char*) this == sizeof(Uid));
76
77 *buffer = '\0';
78
79 return ret_val;
80}
81
82void Uid::destringficate(char *str)
83{
84 auto this_str = reinterpret_cast<char *>(this);
85
86 // Convert the string in ASCII to the rendering in Nibbles
87 for (int i = 0; i < sizeof(Uid); ++i)
88 *this_str++ = unhexadecimalize(str);
89}
90
91
93 const uint64_t & _counter,
94 const uint32_t & _port_number)
95 : ipAddr(_ipAddr), port_number(_port_number), counter(_counter)
96{
97 // Generate the random component directly from std::random_device,
98 // which is typically backed by the operating system CSPRNG.
99 // We intentionally avoid std::mt19937 and any time-based fallback to
100 // keep the UID suitable for security-relevant use cases.
101 std::random_device rd;
102
103 // Combine two 32-bit random values to fill the 64-bit random_number
104 // This ensures the full width is utilized even if random_device returns 32-bit values
105 const auto high = static_cast<decltype(random_number)>(rd());
106 const auto low = static_cast<decltype(random_number)>(rd());
107
108 random_number = (high << 32) | low;
109}
110
111
112Uid::Uid(char *str)
113{
114 ah_invalid_argument_if(str == nullptr) << "Null string passed to Uid constructor";
115
116 const size_t len = std::strlen(str);
117 ah_invalid_argument_if(len < static_cast<size_t>(stringSize - 1))
118 << "String too short for Uid (expected " << (stringSize - 1) << " chars)";
119
120 // Validate hex characters
121 for (size_t i = 0; i < static_cast<size_t>(stringSize - 1); ++i)
122 ah_invalid_argument_if(not std::isxdigit(static_cast<unsigned char>(str[i])))
123 << "Invalid hex character in Uid string at index " << i;
124
125 destringficate(str);
126}
127
128bool Uid::operator ==(const Uid & uid) const noexcept
129{
130 return (ipAddr == uid.ipAddr and
131 port_number == uid.port_number and
132 counter == uid.counter and
133 random_number == uid.random_number);
134}
135
136char * Uid::getStringUid(char *str, const size_t & size) const
137{
139 << "Buffer size too small for UID string representation";
140
141 return stringficate(str, size);
142}
Exception handling system with formatted messages for Aleph-w.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:207
long double h
Definition btreepic.C:154
Unique identifier for distributed systems.
Definition uid.H:95
static constexpr int stringSize
Required buffer size for string representation.
Definition uid.H:109
char * stringficate(char *buffer, const size_t &src_size) const
Convert UID components to hexadecimal string.
Definition uid.C:62
char * getStringUid(char *str, const size_t &size) const
Convert UID to hexadecimal string representation.
Definition uid.C:136
uint64_t random_number
Random number component.
Definition uid.H:101
void destringficate(char *str)
Parse hexadecimal string and populate UID components.
Definition uid.C:82
Uid()=default
Default constructor.
bool operator==(const Uid &uid) const noexcept
Compare two UIDs for equality.
Definition uid.C:128
uint32_t IPv4_Address
Type alias for IPv4 addresses stored as 32-bit integers.
Definition ahDefs.H:89
char nibble_to_char(const int i)
Convert a 4-bit nibble stored in an int to its hex character.
Definition ahUtils.H:122
int char_to_nibble(const char c)
Convert a hex character in 0..9A..F to its 4-bit nibble value.
Definition ahUtils.H:137
DynList< int > l
static char * hexadecimalize(const Byte &byte, char *&str)
Definition uid.C:41
static char unhexadecimalize(char *&str)
Definition uid.C:53
unsigned char Byte
Definition uid.C:39
Unique identifier generation for distributed systems.