2014-08-01 14:39:55 -04:00
|
|
|
// Copyright 2014 The Crashpad Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-03-06 18:43:28 -05:00
|
|
|
#if !defined(__STDC_FORMAT_MACROS)
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
#include "util/misc/uuid.h"
|
|
|
|
|
2015-01-02 18:46:10 -05:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdio.h>
|
2014-08-01 14:39:55 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
2015-04-20 14:21:48 -07:00
|
|
|
#include "base/logging.h"
|
2014-08-01 14:39:55 -04:00
|
|
|
#include "base/strings/stringprintf.h"
|
2015-02-11 12:17:05 -08:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2014-08-01 16:10:58 -04:00
|
|
|
#include "base/sys_byteorder.h"
|
2014-08-03 18:50:09 -04:00
|
|
|
#include "util/stdlib/cxx.h"
|
|
|
|
|
2015-04-20 14:21:48 -07:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
#endif // OS_MACOSX
|
|
|
|
|
2014-08-03 18:50:09 -04:00
|
|
|
#if CXX_LIBRARY_VERSION >= 2011
|
|
|
|
#include <type_traits>
|
|
|
|
#endif
|
2014-08-01 14:39:55 -04:00
|
|
|
|
|
|
|
namespace crashpad {
|
|
|
|
|
2014-10-01 12:29:01 -07:00
|
|
|
static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes");
|
2014-08-03 18:50:09 -04:00
|
|
|
|
|
|
|
#if CXX_LIBRARY_VERSION >= 2011
|
2014-10-01 12:29:01 -07:00
|
|
|
static_assert(std::is_standard_layout<UUID>::value,
|
|
|
|
"UUID must be standard layout");
|
2014-08-03 18:50:09 -04:00
|
|
|
#endif
|
|
|
|
|
2014-08-01 16:10:58 -04:00
|
|
|
UUID::UUID() : data_1(0), data_2(0), data_3(0), data_4(), data_5() {
|
2014-08-01 14:39:55 -04:00
|
|
|
}
|
|
|
|
|
2015-04-20 14:21:48 -07:00
|
|
|
UUID::UUID(InitializeWithNewTag) {
|
|
|
|
CHECK(InitializeWithNew());
|
|
|
|
}
|
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
UUID::UUID(const uint8_t* bytes) {
|
2014-08-01 16:10:58 -04:00
|
|
|
InitializeFromBytes(bytes);
|
|
|
|
}
|
|
|
|
|
2014-09-04 11:45:40 -04:00
|
|
|
bool UUID::operator==(const UUID& that) const {
|
|
|
|
return memcmp(this, &that, sizeof(UUID)) == 0;
|
|
|
|
}
|
|
|
|
|
2014-08-01 16:10:58 -04:00
|
|
|
void UUID::InitializeFromBytes(const uint8_t* bytes) {
|
|
|
|
memcpy(this, bytes, sizeof(*this));
|
|
|
|
data_1 = base::NetToHost32(data_1);
|
|
|
|
data_2 = base::NetToHost16(data_2);
|
|
|
|
data_3 = base::NetToHost16(data_3);
|
2014-08-01 14:39:55 -04:00
|
|
|
}
|
|
|
|
|
2015-01-02 18:46:10 -05:00
|
|
|
bool UUID::InitializeFromString(const base::StringPiece& string) {
|
|
|
|
if (string.length() != 36)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UUID temp;
|
|
|
|
const char kScanFormat[] =
|
|
|
|
"%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16
|
|
|
|
"-%02" SCNx8 "%02" SCNx8
|
|
|
|
"-%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8;
|
|
|
|
int rv = sscanf(string.data(),
|
|
|
|
kScanFormat,
|
|
|
|
&temp.data_1,
|
|
|
|
&temp.data_2,
|
|
|
|
&temp.data_3,
|
|
|
|
&temp.data_4[0],
|
|
|
|
&temp.data_4[1],
|
|
|
|
&temp.data_5[0],
|
|
|
|
&temp.data_5[1],
|
|
|
|
&temp.data_5[2],
|
|
|
|
&temp.data_5[3],
|
|
|
|
&temp.data_5[4],
|
|
|
|
&temp.data_5[5]);
|
|
|
|
if (rv != 11)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*this = temp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-20 14:21:48 -07:00
|
|
|
bool UUID::InitializeWithNew() {
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
uuid_t uuid;
|
|
|
|
uuid_generate(uuid);
|
|
|
|
InitializeFromBytes(uuid);
|
|
|
|
return true;
|
|
|
|
#elif defined(OS_WIN)
|
|
|
|
::UUID system_uuid;
|
|
|
|
if (UuidCreate(&system_uuid) != RPC_S_OK) {
|
|
|
|
LOG(ERROR) << "UuidCreate";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
InitializeFromSystemUUID(&system_uuid);
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
#error Port.
|
|
|
|
#endif // OS_MACOSX
|
|
|
|
}
|
|
|
|
|
2015-03-13 13:53:38 -04:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
void UUID::InitializeFromSystemUUID(const ::UUID* system_uuid) {
|
|
|
|
static_assert(sizeof(::UUID) == sizeof(UUID),
|
|
|
|
"unexpected system uuid size");
|
|
|
|
static_assert(offsetof(::UUID, Data1) == offsetof(UUID, data_1),
|
|
|
|
"unexpected system uuid layout");
|
|
|
|
memcpy(this, system_uuid, sizeof(::UUID));
|
|
|
|
}
|
|
|
|
#endif // OS_WIN
|
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
std::string UUID::ToString() const {
|
2014-08-01 16:10:58 -04:00
|
|
|
return base::StringPrintf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
|
|
|
data_1,
|
|
|
|
data_2,
|
|
|
|
data_3,
|
|
|
|
data_4[0],
|
|
|
|
data_4[1],
|
|
|
|
data_5[0],
|
|
|
|
data_5[1],
|
|
|
|
data_5[2],
|
|
|
|
data_5[3],
|
|
|
|
data_5[4],
|
|
|
|
data_5[5]);
|
2014-08-01 14:39:55 -04:00
|
|
|
}
|
|
|
|
|
2015-02-11 12:17:05 -08:00
|
|
|
#if defined(OS_WIN)
|
2015-02-17 12:05:29 -08:00
|
|
|
base::string16 UUID::ToString16() const {
|
2015-02-11 12:17:05 -08:00
|
|
|
return base::UTF8ToUTF16(ToString());
|
|
|
|
}
|
|
|
|
#endif // OS_WIN
|
|
|
|
|
2014-08-01 14:39:55 -04:00
|
|
|
} // namespace crashpad
|