CMake/Source/cmUuid.cxx

168 lines
3.9 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 19:01:08 +00:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmUuid.h"
#include <array>
#include <cstring>
#include "cmCryptoHash.h"
static const std::array<int, 5> kUuidGroups = { { 4, 2, 2, 2, 6 } };
std::string cmUuid::FromMd5(std::vector<unsigned char> const& uuidNamespace,
std::string const& name) const
{
std::vector<unsigned char> hashInput;
this->CreateHashInput(uuidNamespace, name, hashInput);
cmCryptoHash md5(cmCryptoHash::AlgoMD5);
md5.Initialize();
md5.Append(&hashInput[0], hashInput.size());
std::vector<unsigned char> digest = md5.Finalize();
return this->FromDigest(&digest[0], 3);
}
std::string cmUuid::FromSha1(std::vector<unsigned char> const& uuidNamespace,
std::string const& name) const
{
std::vector<unsigned char> hashInput;
this->CreateHashInput(uuidNamespace, name, hashInput);
cmCryptoHash sha1(cmCryptoHash::AlgoSHA1);
sha1.Initialize();
sha1.Append(&hashInput[0], hashInput.size());
std::vector<unsigned char> digest = sha1.Finalize();
return this->FromDigest(&digest[0], 5);
}
void cmUuid::CreateHashInput(std::vector<unsigned char> const& uuidNamespace,
std::string const& name,
std::vector<unsigned char>& output) const
{
output = uuidNamespace;
if (!name.empty()) {
output.resize(output.size() + name.size());
memcpy(&output[0] + uuidNamespace.size(), name.c_str(), name.size());
}
}
std::string cmUuid::FromDigest(const unsigned char* digest,
unsigned char version) const
{
2019-08-23 21:25:56 +00:00
using byte_t = unsigned char;
byte_t uuid[16] = { 0 };
memcpy(uuid, digest, 16);
uuid[6] &= 0xF;
uuid[6] |= byte_t(version << 4);
uuid[8] &= 0x3F;
uuid[8] |= 0x80;
return this->BinaryToString(uuid);
}
bool cmUuid::StringToBinary(std::string const& input,
std::vector<unsigned char>& output) const
{
output.clear();
output.reserve(16);
if (input.length() != 36) {
return false;
}
size_t index = 0;
for (size_t i = 0; i < kUuidGroups.size(); ++i) {
if (i != 0 && input[index++] != '-') {
return false;
}
size_t digits = kUuidGroups[i] * 2;
if (!StringToBinaryImpl(input.substr(index, digits), output)) {
return false;
}
index += digits;
}
return true;
}
std::string cmUuid::BinaryToString(const unsigned char* input) const
{
std::string output;
size_t inputIndex = 0;
for (size_t i = 0; i < kUuidGroups.size(); ++i) {
if (i != 0) {
output += '-';
}
size_t bytes = kUuidGroups[i];
for (size_t j = 0; j < bytes; ++j) {
unsigned char byte = input[inputIndex++];
output += this->ByteToHex(byte);
}
}
return output;
}
std::string cmUuid::ByteToHex(unsigned char byte) const
{
std::string result(" ");
for (int i = 0; i < 2; ++i) {
unsigned char rest = byte % 16;
byte /= 16;
char c = (rest < 0xA) ? char('0' + rest) : char('a' + (rest - 0xA));
result.at(1 - i) = c;
}
return result;
}
bool cmUuid::StringToBinaryImpl(std::string const& input,
std::vector<unsigned char>& output) const
{
if (input.size() % 2) {
return false;
}
for (size_t i = 0; i < input.size(); i += 2) {
char c1 = 0;
if (!IntFromHexDigit(input[i], c1)) {
return false;
}
char c2 = 0;
if (!IntFromHexDigit(input[i + 1], c2)) {
return false;
}
output.push_back(char(c1 << 4 | c2));
}
return true;
}
bool cmUuid::IntFromHexDigit(char input, char& output) const
{
if (input >= '0' && input <= '9') {
output = char(input - '0');
return true;
2016-08-18 18:36:29 +00:00
}
if (input >= 'a' && input <= 'f') {
output = char(input - 'a' + 0xA);
return true;
2016-08-18 18:36:29 +00:00
}
if (input >= 'A' && input <= 'F') {
output = char(input - 'A' + 0xA);
return true;
}
2016-08-18 18:36:29 +00:00
return false;
}