1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/*
*
* Copyright (C) 2011-2022, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Uli Schlachter
*
* Purpose: Definitions for generating UUIDs, as defined by ITU-T X.667
*
*/
#include "dcmtk/config/osconfig.h"
#include "dcmtk/ofstd/ofuuid.h"
#include "dcmtk/ofstd/ofdefine.h"
#include "dcmtk/ofstd/ofthread.h"
#include "dcmtk/ofstd/ofstd.h"
BEGIN_EXTERN_C
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
END_EXTERN_C
#ifdef HAVE_WINDOWS_H
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
static OFMutex UUIDMutex;
static Uint32 last_time[2];
static int uuids_this_tick;
static Uint16 last_clock_sequence;
static Uint8 last_node[6];
static OFBool initialized = OFFalse;
static void get_random(OFRandom &rnd, void *dest, size_t num)
{
Uint8* ptr = OFreinterpret_cast(Uint8*, dest);
while (num > 0) {
*ptr++ = OFstatic_cast(Uint8, rnd.getRND16());
num--;
}
}
static void get_node(OFRandom &rnd)
{
/* FIXME: This is supposed to be a MAC address and we are supposed to
* re-check the MAC address each time we generate a UUID and do some stuff
* if the MAC changes.
*/
get_random(rnd, &last_node[0], sizeof(last_node));
}
#ifdef _WIN32
static void get_system_time(Uint32 *out)
{
ULARGE_INTEGER tm;
GetSystemTimeAsFileTime(OFreinterpret_cast(FILETIME *, &tm));
/* tm is number of 100ns ticks since Jan 01 1601, but we want the number of
* ticks since Oct 15 1582 (since a new calendar system has been introduced
* at that time).
*/
tm.QuadPart += OFstatic_cast(unsigned __int64, 1000 * 1000 * 10) // seconds
* OFstatic_cast(unsigned __int64, 60 * 60 * 24) // days
* OFstatic_cast(unsigned __int64, 17 + 30 + 31 + 365 * 18 + 5); // number of days
out[0] = tm.LowPart;
out[1] = tm.HighPart;
}
#else
static void get_system_time(Uint32 *out)
{
struct timeval tp;
Uint32 sec_factor = 1000 * 1000 * 10;
Uint32 add;
Uint32 ah, al, bh, bl;
gettimeofday(&tp, NULL);
/* tp is offset from Jan 1 1970, but we want Oct 15 1582 */
out[1] = 0x01B21DD2;
out[0] = 0x13814000;
add = OFstatic_cast(Uint32, tp.tv_usec * 10);
if (OFStandard::check32BitAddOverflow(out[0], add))
out[1]++;
out[0] += add;
// We have to add tp.tv_sec * sec_factor, but that doesn't fit into 32 bits.
ah = OFstatic_cast(Uint32, tp.tv_sec >> 16);
al = tp.tv_sec & 0xffff;
bh = sec_factor >> 16;
bl = sec_factor & 0xffff;
// tv_sec * sec_factor = (ah * 2^16 + al) * (bh * 2^16 + bl)
// = ah * bh * 2^32 + (ah * bl + al * bh) * 2^16 + (al * bl)
add = al*bl;
if (OFStandard::check32BitAddOverflow(out[0], add))
out[1]++;
out[0] += add;
out[1] += ah * bh;
// We have to add (ah * bl + al * bh) * 2^16. First we add the lower
// 16 bits of that summand to out[0], then we add the higher 16 bits
// directly to out[1].
add = (((ah * bl) + (al * bh)) & 0xffff) << 16;
if (OFStandard::check32BitAddOverflow(out[0], add))
out[1]++;
out[0] += add;
add = (((ah * bl) + (al * bh)) & 0xffff0000) >> 16;
out[1] += add;
}
#endif
static void get_time(Uint32 *out)
{
get_system_time(out);
if (out[0] == last_time[0] && out[1] == last_time[1]) {
uuids_this_tick++;
/* FIXME: If this gets to high, we are supposed to busy loop. For now
* let's assume that we aren't called that often. (See A.3.3)
* FIXME: Do we have to handle the integer overflow? I think no since
* the lowest bits should always be zero and if we fix the above FIXME,
* this one should be gone, too.
*/
out[0] += uuids_this_tick;
} else
uuids_this_tick = 0;
}
void OFUUID::generate()
{
Uint32 system_time[2];
Uint16 clock_sequence;
/* See ITU-T X.667, Annex A */
UUIDMutex.lock();
if (!initialized) {
get_node(rnd);
get_random(rnd, &last_clock_sequence, sizeof(last_clock_sequence));
initialized = OFTrue;
}
get_time(&system_time[0]);
/* If time went backwards, increment clock sequence */
if (system_time[0] < last_time[0] || (system_time[0] == last_time[0] && system_time[1] < last_time[1]))
last_clock_sequence++;
clock_sequence = last_clock_sequence;
last_time[0] = system_time[0];
last_time[1] = system_time[1];
UUIDMutex.unlock();
/* Bits 0-31 of time (32 bits) */
time_low = system_time[0];
/* Bits 32-47 of time (16 bits) */
time_mid = OFstatic_cast(Uint16, system_time[1] & 0xffff);
/* Bits 48-59 of time (11 bits) */
version_and_time_high = OFstatic_cast(Uint16, (system_time[1] >> 16) & 0xeff);
/* Version number, bits 15 to 12 of version_and_time_high */
version_and_time_high |= 0x100;
/* Sequence, lowest 8 bits of clock_sequence */
clock_seq_low = OFstatic_cast(Uint8, clock_sequence & 0xff);
/* Bits 8-13 of clock_sequence */
variant_and_clock_seq_high = OFstatic_cast(Uint8, (clock_sequence >> 8) & 0xcf);
/* Version */
variant_and_clock_seq_high |= 0x80;
/* And the node value */
memcpy(&node, &last_node, sizeof(node));
}
OFUUID::OFUUID()
: time_low(0),
time_mid(0),
version_and_time_high(0),
variant_and_clock_seq_high(0),
clock_seq_low(0),
node(),
rnd()
{
generate();
}
OFUUID::OFUUID(const struct BinaryRepresentation& rep)
: time_low(0),
time_mid(0),
version_and_time_high(0),
variant_and_clock_seq_high(0),
clock_seq_low(0),
node()
{
time_low = rep.value[0];
time_low = time_low << 8 | rep.value[1];
time_low = time_low << 8 | rep.value[2];
time_low = time_low << 8 | rep.value[3];
time_mid = rep.value[4];
time_mid = OFstatic_cast(Uint16, time_mid << 8 | rep.value[5]);
version_and_time_high = rep.value[6];
version_and_time_high = OFstatic_cast(Uint16, version_and_time_high << 8 | rep.value[7]);
variant_and_clock_seq_high = rep.value[8];
clock_seq_low = rep.value[9];
memcpy(&node[0], &rep.value[10], sizeof(node));
}
void OFUUID::getBinaryRepresentation(struct BinaryRepresentation& rep) const
{
rep.value[0] = OFstatic_cast(Uint8, (time_low >> 24) & 0xff);
rep.value[1] = OFstatic_cast(Uint8, (time_low >> 16) & 0xff);
rep.value[2] = OFstatic_cast(Uint8, (time_low >> 8) & 0xff);
rep.value[3] = OFstatic_cast(Uint8, (time_low >> 0) & 0xff);
rep.value[4] = OFstatic_cast(Uint8, (time_mid >> 8) & 0xff);
rep.value[5] = OFstatic_cast(Uint8, (time_mid >> 0) & 0xff);
rep.value[6] = OFstatic_cast(Uint8, (version_and_time_high >> 8) & 0xff);
rep.value[7] = OFstatic_cast(Uint8, (version_and_time_high >> 0) & 0xff);
rep.value[8] = variant_and_clock_seq_high;
rep.value[9] = clock_seq_low;
memcpy(&rep.value[10], &node[0], sizeof(node));
}
OFString& OFUUID::toString(OFString& result, E_Representation representation) const
{
OFOStringStream stream;
print(stream, representation);
OFSTRINGSTREAM_GETSTR(stream, result_ptr)
result = result_ptr;
OFSTRINGSTREAM_FREESTR(result_ptr);
return result;
}
STD_NAMESPACE ostream& OFUUID::print(STD_NAMESPACE ostream& stream, E_Representation representation) const
{
switch (representation) {
case ER_RepresentationOID:
stream << "2.25.";
/* Fall through */
case ER_RepresentationInteger:
printInteger(stream);
break;
case ER_RepresentationURN:
stream << "urn:uuid:";
/* Fall through */
case ER_RepresentationHex:
printHex(stream);
break;
}
return stream;
}
void OFUUID::printHex(STD_NAMESPACE ostream& stream) const
{
STD_NAMESPACE ios_base::fmtflags flags = stream.flags(STD_NAMESPACE ios_base::hex);
char fill_char = stream.fill('0');
stream << STD_NAMESPACE setw(8) << time_low << "-";
stream << STD_NAMESPACE setw(4) << time_mid << "-";
stream << STD_NAMESPACE setw(4) << version_and_time_high << "-";
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, variant_and_clock_seq_high);
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, clock_seq_low) << "-";
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, node[0]);
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, node[1]);
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, node[2]);
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, node[3]);
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, node[4]);
stream << STD_NAMESPACE setw(2) << OFstatic_cast(int, node[5]);
stream.flags(flags);
stream.fill(fill_char);
}
static void divide_by_10(Uint32 n, Uint32& res, Uint32& rem)
{
// This calculates res = m / d and rem = m % d where m = n + rem * 2^32
// First we do the higher half of the division
Uint32 next = (rem << 16) | (n >> 16);
Uint32 tmp = next / 10;
rem = next % 10;
// Then we do the same with the lower half
next = (rem << 16) | (n & 0xffff);
res = (next / 10) + (tmp << 16);
rem = next % 10;
}
void OFUUID::printInteger(STD_NAMESPACE ostream& stream) const
{
// Oh sh.., converting a 128-bit integer to its base 10 representation. Not funny.
// A 128-bit int has at most 39 characters in base 10
char buffer[40];
// Our current 128-bit state
Uint32 data[4];
struct BinaryRepresentation representation;
/* Current buffer index (we are generating the last character of the output
* first, so we have write it into buffer in reverse order) */
int idx = OFstatic_cast(int, sizeof(buffer) - 1);
// First we convert the 128-bit integer into four 32-bit integers
getBinaryRepresentation(representation);
for (int i = 0; i < 4; i++) {
data[i] = representation.value[0 + 4*i] << 24;
data[i] |= representation.value[1 + 4*i] << 16;
data[i] |= representation.value[2 + 4*i] << 8;
data[i] |= representation.value[3 + 4*i] << 0;
}
if (data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 0) {
// This should never happen, but we can still handle it! :-)
stream << "0";
return;
}
// As long as the result isn't 0, divide by 10 and print the remainder
while (data[0] != 0 || data[1] != 0 || data[2] != 0 || data[3] != 0) {
Uint32 rem = 0;
divide_by_10(data[0], data[0], rem);
divide_by_10(data[1], data[1], rem);
divide_by_10(data[2], data[2], rem);
divide_by_10(data[3], data[3], rem);
assert(rem <= 9);
buffer[--idx] = OFstatic_cast(char, rem + '0');
}
assert(idx >= 0);
buffer[sizeof(buffer) - 1] = '\0';
stream << &buffer[idx];
}
OFBool OFUUID::operator==(const OFUUID& o) const
{
struct BinaryRepresentation own;
struct BinaryRepresentation other;
getBinaryRepresentation(own);
o.getBinaryRepresentation(other);
return memcmp(&own, &other, sizeof(struct BinaryRepresentation)) == 0;
}
|