139 lines
4.4 KiB
C++
139 lines
4.4 KiB
C++
/*
|
|
uuid.h
|
|
|
|
Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <string>
|
|
|
|
// RFC4122 A Universally Unique IDentifier (UUID) URN Namespace
|
|
namespace UUID {
|
|
|
|
class uuid {
|
|
public:
|
|
enum Type {
|
|
NCS, // Reserved, NCS backward compatibility.
|
|
RFC4122, // The variant specified in this document.
|
|
MS, // Reserved, Microsoft Corporation backward compatibility
|
|
RESVERED, // Reserved for future definition.
|
|
NIL
|
|
};
|
|
|
|
enum RFC4122Version {
|
|
VOID = 0b0000,
|
|
TIME = 0b0001, // The time-based version specified in this document.
|
|
DCE = 0b0010, // DCE Security version, with embedded POSIX UIDs.
|
|
MD5 = 0b0011, // The name-based version that uses MD5 hashing.
|
|
RAND = 0b0100, // The randomly or pseudo-randomly generated version.
|
|
SHA1 = 0b0101 // The name-based version that uses SHA-1 hashing.
|
|
};
|
|
|
|
enum Namespace {
|
|
DNS, // the name string is a fully-qualified domain name.
|
|
URL, // the name string is a URL.
|
|
OID, // the name string is an ISO OID.
|
|
X500 // the name string is an X.500 DN in DER or a text output format.
|
|
};
|
|
|
|
struct NCSFields {
|
|
uint64_t time : 48;
|
|
uint16_t res;
|
|
uint8_t family;
|
|
uint64_t node : 56;
|
|
}__attribute__((packed));
|
|
|
|
struct RFC4122Fields {
|
|
uint32_t time_low;
|
|
uint16_t time_mid;
|
|
uint16_t time_hi_version;
|
|
uint8_t clock_seq_hi_variant;
|
|
uint8_t clock_seq_low;
|
|
uint16_t node_low;
|
|
uint32_t node_high;
|
|
}__attribute__((packed));
|
|
|
|
// constructor
|
|
uuid();
|
|
uuid(const uint8_t * raw) : uuid() { fromArray(raw); };
|
|
uuid(const char * cstr) : uuid() { fromCstring(cstr); };
|
|
uuid(std::string str) : uuid() { fromString(str); };
|
|
|
|
// accessors
|
|
const Type type() const { return type_; };
|
|
const uint16_t version() const { return version_; };
|
|
const uint64_t time() const { return timestamp_; };
|
|
const uint16_t sequence() const { return clock_seq_; };
|
|
const uint64_t node() const { return node_; };
|
|
const uint8_t * bytes() const { return raw_; };
|
|
|
|
// comparitor overload
|
|
bool operator== (const uuid &) const;
|
|
bool operator== (const uint8_t *) const;
|
|
bool operator== (const char *) const;
|
|
bool operator== (const std::string &) const;
|
|
|
|
// typecast overload
|
|
operator const uint8_t * () const { return raw_; };
|
|
operator const std::string () const { return string(); };
|
|
|
|
// output
|
|
std::string hex() const; // '12345678123456781234567812345678'
|
|
std::string string() const;// '{12345678-1234-5678-1234-567812345678}'
|
|
std::string urn() const; // 'urn:uuid:12345678-1234-5678-1234-567812345678'
|
|
|
|
// creators
|
|
virtual void uuid1(uint64_t node, uint16_t clock_seq) {};
|
|
virtual void uuid3(Namespace, std::string) {};
|
|
virtual void uuid4(); // very low quality of random
|
|
virtual void uuid5(Namespace, std::string) {};
|
|
|
|
private:
|
|
uint8_t raw_[16];
|
|
Type type_;
|
|
uint16_t version_;
|
|
uint64_t timestamp_;
|
|
uint16_t clock_seq_;
|
|
uint64_t node_;
|
|
|
|
void fromArray(const uint8_t *);
|
|
void fromCstring(const char *);
|
|
void fromString(std::string);
|
|
|
|
void setType();
|
|
void setBytes();
|
|
void setNCSBytes();
|
|
void setRFC4122Bytes();
|
|
|
|
void setNCSFields();
|
|
void setRFC4122Fields();
|
|
}; // uuid
|
|
|
|
|
|
// extern const std::string NAMESPACE_DNS;
|
|
// extern const std::string NAMESPACE_URL;
|
|
// extern const std::string NAMESPACE_OID;
|
|
// extern const std::string NAMESPACE_X500;
|
|
|
|
}; // UUID
|