2020-10-24 04:00:49 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2022-12-25 16:04:45 +00:00
|
|
|
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.TXT for contributors.
|
2020-10-24 04:00:49 +00:00
|
|
|
*
|
|
|
|
* 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/uuid/uuid_generators.hpp>
|
|
|
|
#include <boost/uuid/uuid_io.hpp>
|
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
|
2021-01-15 12:56:38 +00:00
|
|
|
#if BOOST_VERSION >= 106700
|
|
|
|
#include <boost/uuid/entropy_error.hpp>
|
|
|
|
#endif
|
|
|
|
|
2022-11-04 02:22:35 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2022-08-13 14:52:55 +00:00
|
|
|
#include <cctype>
|
2021-11-21 18:30:36 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2021-01-15 12:56:38 +00:00
|
|
|
#include <wx/log.h>
|
|
|
|
|
2021-11-21 18:30:36 +00:00
|
|
|
// boost:mt19937 is not thread-safe
|
2021-11-24 11:02:38 +00:00
|
|
|
static std::mutex rng_mutex;
|
2021-01-15 12:56:38 +00:00
|
|
|
|
2022-03-23 22:49:19 +00:00
|
|
|
// Static rng and generators are used because the overhead of constant seeding is expensive
|
2022-08-07 07:22:11 +00:00
|
|
|
// 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;
|
2020-10-24 04:00:49 +00:00
|
|
|
|
2021-11-24 11:02:38 +00:00
|
|
|
// 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;
|
|
|
|
|
2020-10-24 04:00:49 +00:00
|
|
|
|
|
|
|
// Global nil reference
|
|
|
|
KIID niluuid( 0 );
|
|
|
|
|
2021-11-24 11:02:38 +00:00
|
|
|
|
2021-03-14 21:50:49 +00:00
|
|
|
// When true, always create nil uuids for performance, when valid ones aren't needed
|
2021-11-24 11:02:38 +00:00
|
|
|
static bool g_createNilUuids = false;
|
2021-03-14 21:50:49 +00:00
|
|
|
|
2021-01-15 12:56:38 +00:00
|
|
|
|
2020-10-24 04:00:49 +00:00
|
|
|
// For static initialization
|
|
|
|
KIID& NilUuid()
|
|
|
|
{
|
|
|
|
static KIID nil( 0 );
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-15 12:56:38 +00:00
|
|
|
KIID::KIID()
|
2020-10-24 04:00:49 +00:00
|
|
|
{
|
2021-01-15 12:56:38 +00:00
|
|
|
m_cached_timestamp = 0;
|
|
|
|
|
|
|
|
#if BOOST_VERSION >= 106700
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
2021-11-24 11:02:38 +00:00
|
|
|
if( g_createNilUuids )
|
2021-11-21 18:30:36 +00:00
|
|
|
{
|
2021-03-14 21:50:49 +00:00
|
|
|
m_uuid = nilGenerator();
|
2021-11-21 18:30:36 +00:00
|
|
|
}
|
2021-03-14 21:50:49 +00:00
|
|
|
else
|
2021-11-21 18:30:36 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock( rng_mutex );
|
2021-03-14 21:50:49 +00:00
|
|
|
m_uuid = randomGenerator();
|
2021-11-21 18:30:36 +00:00
|
|
|
}
|
2021-01-15 12:56:38 +00:00
|
|
|
|
|
|
|
#if BOOST_VERSION >= 106700
|
|
|
|
}
|
2021-02-20 16:50:10 +00:00
|
|
|
catch( const boost::uuids::entropy_error& )
|
2021-01-15 12:56:38 +00:00
|
|
|
{
|
|
|
|
wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
|
|
|
|
__FILE__, __FUNCTION__ );
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-24 04:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 18:20:04 +00:00
|
|
|
KIID::KIID( int null ) :
|
|
|
|
m_uuid( nilGenerator() ),
|
|
|
|
m_cached_timestamp( 0 )
|
2020-10-24 04:00:49 +00:00
|
|
|
{
|
|
|
|
wxASSERT( null == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 18:20:04 +00:00
|
|
|
KIID::KIID( const std::string& aString ) :
|
|
|
|
m_uuid(),
|
|
|
|
m_cached_timestamp( 0 )
|
2020-10-24 04:00:49 +00:00
|
|
|
{
|
2022-08-13 14:52:55 +00:00
|
|
|
if( aString.length() == 8
|
|
|
|
&& std::all_of( aString.begin(), aString.end(),
|
|
|
|
[]( unsigned char c )
|
|
|
|
{
|
|
|
|
return std::isxdigit( c );
|
|
|
|
} ) )
|
2020-10-24 04:00:49 +00:00
|
|
|
{
|
|
|
|
// 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 )
|
|
|
|
{
|
2022-08-13 14:52:55 +00:00
|
|
|
std::string octet = aString.substr( i * 2, 2 );
|
2021-07-15 19:26:35 +00:00
|
|
|
m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
|
2020-10-24 04:00:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 19:26:35 +00:00
|
|
|
m_cached_timestamp = strtol( aString.c_str(), nullptr, 16 );
|
2020-10-24 04:00:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-08-13 14:52:55 +00:00
|
|
|
m_uuid = stringGenerator( aString );
|
2020-10-24 04:00:49 +00:00
|
|
|
|
|
|
|
if( IsLegacyTimestamp() )
|
2021-07-15 19:26:35 +00:00
|
|
|
m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), nullptr, 16 );
|
2020-10-24 04:00:49 +00:00
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
|
|
|
// Failed to parse string representation; best we can do is assign a new
|
|
|
|
// random one.
|
2021-01-15 12:56:38 +00:00
|
|
|
#if BOOST_VERSION >= 106700
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_uuid = randomGenerator();
|
|
|
|
|
|
|
|
#if BOOST_VERSION >= 106700
|
|
|
|
}
|
2021-02-20 16:50:10 +00:00
|
|
|
catch( const boost::uuids::entropy_error& )
|
2021-01-15 12:56:38 +00:00
|
|
|
{
|
|
|
|
wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
|
|
|
|
__FILE__, __FUNCTION__ );
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-24 04:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 18:20:04 +00:00
|
|
|
KIID::KIID( const char* aString ) :
|
|
|
|
KIID( std::string( aString ) )
|
2022-08-13 14:52:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-21 18:20:04 +00:00
|
|
|
KIID::KIID( const wxString& aString ) :
|
|
|
|
KIID( std::string( aString.ToUTF8() ) )
|
2022-08-13 14:52:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-24 04:00:49 +00:00
|
|
|
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;
|
|
|
|
|
2020-10-25 20:53:24 +00:00
|
|
|
if( c == '-' )
|
|
|
|
continue;
|
|
|
|
|
2020-10-24 04:00:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
KIID::KIID( timestamp_t aTimestamp )
|
|
|
|
{
|
|
|
|
m_cached_timestamp = aTimestamp;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
wxString str = AsLegacyTimestampString();
|
|
|
|
|
|
|
|
for( int i = 0; i < 4; ++i )
|
|
|
|
{
|
|
|
|
wxString octet = str.substr( i * 2, 2 );
|
2021-07-15 19:26:35 +00:00
|
|
|
m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
|
2020-10-24 04:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return m_cached_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t KIID::Hash() const
|
|
|
|
{
|
|
|
|
size_t hash = 0;
|
|
|
|
|
|
|
|
// Note: this is NOT little-endian/big-endian safe, but as long as it's just used
|
|
|
|
// at runtime it won't matter.
|
|
|
|
|
|
|
|
for( int i = 0; i < 4; ++i )
|
|
|
|
boost::hash_combine( hash, reinterpret_cast<const uint32_t*>( m_uuid.data )[i] );
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void KIID::Clone( const KIID& aUUID )
|
|
|
|
{
|
|
|
|
m_uuid = aUUID.m_uuid;
|
|
|
|
m_cached_timestamp = aUUID.m_cached_timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
wxString KIID::AsString() 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_cached_timestamp = 0;
|
|
|
|
m_uuid = randomGenerator();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-24 11:02:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-14 21:50:49 +00:00
|
|
|
void KIID::CreateNilUuids( bool aNil )
|
|
|
|
{
|
2021-11-24 11:02:38 +00:00
|
|
|
g_createNilUuids = aNil;
|
2021-03-14 21:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-20 19:19:29 +00:00
|
|
|
void KIID::SeedGenerator( unsigned int aSeed )
|
|
|
|
{
|
|
|
|
rng.seed( aSeed );
|
2022-08-07 07:22:11 +00:00
|
|
|
randomGenerator = boost::uuids::basic_random_generator<boost::mt19937>( rng );
|
2021-11-20 19:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-24 04:00:49 +00:00
|
|
|
KIID_PATH::KIID_PATH( const wxString& aString )
|
|
|
|
{
|
|
|
|
for( const wxString& pathStep : wxSplit( aString, '/' ) )
|
|
|
|
{
|
|
|
|
if( !pathStep.empty() )
|
|
|
|
emplace_back( KIID( pathStep ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-01 22:56:37 +00:00
|
|
|
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 ).AsString() != aPath.at( i ).AsString() )
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-25 16:04:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-10-24 04:00:49 +00:00
|
|
|
wxString KIID_PATH::AsString() const
|
|
|
|
{
|
|
|
|
wxString path;
|
|
|
|
|
|
|
|
for( const KIID& pathStep : *this )
|
|
|
|
path += '/' + pathStep.AsString();
|
|
|
|
|
|
|
|
return path;
|
2021-01-15 12:56:38 +00:00
|
|
|
}
|
2022-11-04 02:22:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
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>() );
|
|
|
|
}
|