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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 Ian McInerney <ian.s.mcinerney@ieee.org>
* Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <kiid.h>
#include <boost/random/mersenne_twister.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#if BOOST_VERSION >= 106700
#include <boost/uuid/entropy_error.hpp>
#endif
#include <json_common.h>
#include <cctype>
#include <mutex>
#include <utility>
#include <stdlib.h>
#include <wx/log.h>
// boost:mt19937 is not thread-safe
static std::mutex rng_mutex;
// Static rng and generators are used because the overhead of constant seeding is expensive
// We rely on the default non-arg constructor of basic_random_generator to provide a random seed.
// We use a separate rng object for cases where we want to control the basic_random_generator
// initial seed by calling SeedGenerator from unit tests and other special cases.
static boost::mt19937 rng;
static boost::uuids::basic_random_generator<boost::mt19937> randomGenerator;
// These don't have the same performance penalty, but we might as well be consistent
static boost::uuids::string_generator stringGenerator;
static boost::uuids::nil_generator nilGenerator;
// Global nil reference
KIID niluuid( 0 );
// When true, always create nil uuids for performance, when valid ones aren't needed
static bool g_createNilUuids = false;
// For static initialization
KIID& NilUuid()
{
static KIID nil( 0 );
return nil;
}
KIID::KIID()
{
#if BOOST_VERSION >= 106700
try
{
#endif
if( g_createNilUuids )
{
m_uuid = nilGenerator();
}
else
{
std::lock_guard<std::mutex> lock( rng_mutex );
m_uuid = randomGenerator();
}
#if BOOST_VERSION >= 106700
}
catch( const boost::uuids::entropy_error& )
{
wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
__FILE__, __FUNCTION__ );
}
#endif
}
KIID::KIID( int null ) :
m_uuid( nilGenerator() )
{
wxASSERT( null == 0 );
}
KIID::KIID( const std::string& aString ) :
m_uuid()
{
if( !aString.empty() && aString.length() <= 8
&& std::all_of( aString.begin(), aString.end(),
[]( unsigned char c )
{
return std::isxdigit( c );
} ) )
{
// A legacy-timestamp-based UUID has only the last 4 octets filled in.
// Convert them individually to avoid stepping in the little-endian/big-endian
// doo-doo.
for( int i = 0; i < 4; i++ )
{
int start = static_cast<int>( aString.length() ) - 8 + i * 2;
int end = start + 2;
start = std::max( 0, start );
int len = std::max( 0, end - start );
std::string octet = aString.substr( start, len );
m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
}
}
else
{
try
{
m_uuid = stringGenerator( aString );
}
catch( ... )
{
// Failed to parse string representation; best we can do is assign a new
// random one.
#if BOOST_VERSION >= 106700
try
{
#endif
m_uuid = randomGenerator();
#if BOOST_VERSION >= 106700
}
catch( const boost::uuids::entropy_error& )
{
wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
__FILE__, __FUNCTION__ );
}
#endif
}
}
}
KIID::KIID( const char* aString ) :
KIID( std::string( aString ) )
{
}
KIID::KIID( const wxString& aString ) :
KIID( std::string( aString.ToUTF8() ) )
{
}
bool KIID::SniffTest( const wxString& aCandidate )
{
static wxString niluuidStr = niluuid.AsString();
if( aCandidate.Length() != niluuidStr.Length() )
return false;
for( wxChar c : aCandidate )
{
if( c >= '0' && c <= '9' )
continue;
if( c >= 'a' && c <= 'f' )
continue;
if( c >= 'A' && c <= 'F' )
continue;
if( c == '-' )
continue;
return false;
}
return true;
}
KIID::KIID( timestamp_t aTimestamp )
{
m_uuid.data[12] = static_cast<uint8_t>( aTimestamp >> 24 );
m_uuid.data[13] = static_cast<uint8_t>( aTimestamp >> 16 );
m_uuid.data[14] = static_cast<uint8_t>( aTimestamp >> 8 );
m_uuid.data[15] = static_cast<uint8_t>( aTimestamp );
}
bool KIID::IsLegacyTimestamp() const
{
return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11];
}
timestamp_t KIID::AsLegacyTimestamp() const
{
timestamp_t ret = 0;
ret |= m_uuid.data[12] << 24;
ret |= m_uuid.data[13] << 16;
ret |= m_uuid.data[14] << 8;
ret |= m_uuid.data[15];
return ret;
}
size_t KIID::Hash() const
{
return boost::uuids::hash_value( m_uuid );
}
void KIID::Clone( const KIID& aUUID )
{
m_uuid = aUUID.m_uuid;
}
wxString KIID::AsString() const
{
return boost::uuids::to_string( m_uuid );
}
std::string KIID::AsStdString() const
{
return boost::uuids::to_string( m_uuid );
}
wxString KIID::AsLegacyTimestampString() const
{
return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() );
}
void KIID::ConvertTimestampToUuid()
{
if( !IsLegacyTimestamp() )
return;
m_uuid = randomGenerator();
}
void KIID::Increment()
{
// This obviously destroys uniform distribution, but it can be useful when a
// deterministic replacement for a duplicate ID is required.
for( int i = 15; i >= 0; --i )
{
m_uuid.data[i]++;
if( m_uuid.data[i] != 0 )
break;
}
}
void KIID::CreateNilUuids( bool aNil )
{
g_createNilUuids = aNil;
}
void KIID::SeedGenerator( unsigned int aSeed )
{
rng.seed( aSeed );
randomGenerator = boost::uuids::basic_random_generator<boost::mt19937>( rng );
}
KIID_PATH::KIID_PATH( const wxString& aString )
{
for( const wxString& pathStep : wxSplit( aString, '/' ) )
{
if( !pathStep.empty() )
emplace_back( KIID( pathStep ) );
}
}
bool KIID_PATH::MakeRelativeTo( const KIID_PATH& aPath )
{
KIID_PATH copy = *this;
clear();
if( aPath.size() > copy.size() )
return false; // this path is not contained within aPath
for( size_t i = 0; i < aPath.size(); ++i )
{
if( copy.at( i ) != aPath.at( i ) )
{
*this = copy;
return false; // this path is not contained within aPath
}
}
for( size_t i = aPath.size(); i < copy.size(); ++i )
push_back( copy.at( i ) );
return true;
}
bool KIID_PATH::EndsWith( const KIID_PATH& aPath ) const
{
if( aPath.size() > size() )
return false; // this path can not end aPath
KIID_PATH copyThis = *this;
KIID_PATH copyThat = aPath;
while( !copyThat.empty() )
{
if( *std::prev( copyThis.end() ) != *std::prev( copyThat.end() ) )
return false;
copyThis.pop_back();
copyThat.pop_back();
}
return true;
}
wxString KIID_PATH::AsString() const
{
wxString path;
for( const KIID& pathStep : *this )
path += '/' + pathStep.AsString();
return path;
}
void to_json( nlohmann::json& aJson, const KIID& aKIID )
{
aJson = aKIID.AsString().ToUTF8();
}
void from_json( const nlohmann::json& aJson, KIID& aKIID )
{
aKIID = KIID( aJson.get<std::string>() );
}
|