You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Ian McInerney <ian.s.mcinerney@ieee.org>
  5. * Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  6. * Copyright (C) 1992-2020 KiCad Developers, see CHANGELOG.TXT for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <common.h> // AsLegacyTimestampString, AsString
  26. #include <kiid.h>
  27. #include <boost/uuid/uuid_generators.hpp>
  28. #include <boost/uuid/uuid_io.hpp>
  29. #include <boost/functional/hash.hpp>
  30. // Create only once, as seeding is *very* expensive
  31. static boost::uuids::random_generator randomGenerator;
  32. // These don't have the same performance penalty, but might as well be consistent
  33. static boost::uuids::string_generator stringGenerator;
  34. static boost::uuids::nil_generator nilGenerator;
  35. // Global nil reference
  36. KIID niluuid( 0 );
  37. // For static initialization
  38. KIID& NilUuid()
  39. {
  40. static KIID nil( 0 );
  41. return nil;
  42. }
  43. KIID::KIID() : m_uuid( randomGenerator() ), m_cached_timestamp( 0 )
  44. {
  45. }
  46. KIID::KIID( int null ) : m_uuid( nilGenerator() ), m_cached_timestamp( 0 )
  47. {
  48. wxASSERT( null == 0 );
  49. }
  50. KIID::KIID( const wxString& aString ) : m_uuid(), m_cached_timestamp( 0 )
  51. {
  52. if( aString.length() == 8 )
  53. {
  54. // A legacy-timestamp-based UUID has only the last 4 octets filled in.
  55. // Convert them individually to avoid stepping in the little-endian/big-endian
  56. // doo-doo.
  57. for( int i = 0; i < 4; ++i )
  58. {
  59. wxString octet = aString.substr( i * 2, 2 );
  60. m_uuid.data[i + 12] = strtol( octet.data(), NULL, 16 );
  61. }
  62. m_cached_timestamp = strtol( aString.c_str(), NULL, 16 );
  63. }
  64. else
  65. {
  66. try
  67. {
  68. m_uuid = stringGenerator( aString.wc_str() );
  69. if( IsLegacyTimestamp() )
  70. m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), NULL, 16 );
  71. }
  72. catch( ... )
  73. {
  74. // Failed to parse string representation; best we can do is assign a new
  75. // random one.
  76. m_uuid = randomGenerator();
  77. }
  78. }
  79. }
  80. bool KIID::SniffTest( const wxString& aCandidate )
  81. {
  82. static wxString niluuidStr = niluuid.AsString();
  83. if( aCandidate.Length() != niluuidStr.Length() )
  84. return false;
  85. for( wxChar c : aCandidate )
  86. {
  87. if( c >= '0' && c <= '9' )
  88. continue;
  89. if( c >= 'a' && c <= 'f' )
  90. continue;
  91. if( c >= 'A' && c <= 'F' )
  92. continue;
  93. if( c == '-' )
  94. continue;
  95. return false;
  96. }
  97. return true;
  98. }
  99. KIID::KIID( timestamp_t aTimestamp )
  100. {
  101. m_cached_timestamp = aTimestamp;
  102. // A legacy-timestamp-based UUID has only the last 4 octets filled in.
  103. // Convert them individually to avoid stepping in the little-endian/big-endian
  104. // doo-doo.
  105. wxString str = AsLegacyTimestampString();
  106. for( int i = 0; i < 4; ++i )
  107. {
  108. wxString octet = str.substr( i * 2, 2 );
  109. m_uuid.data[i + 12] = strtol( octet.data(), NULL, 16 );
  110. }
  111. }
  112. bool KIID::IsLegacyTimestamp() const
  113. {
  114. return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11];
  115. }
  116. timestamp_t KIID::AsLegacyTimestamp() const
  117. {
  118. return m_cached_timestamp;
  119. }
  120. size_t KIID::Hash() const
  121. {
  122. size_t hash = 0;
  123. // Note: this is NOT little-endian/big-endian safe, but as long as it's just used
  124. // at runtime it won't matter.
  125. for( int i = 0; i < 4; ++i )
  126. boost::hash_combine( hash, reinterpret_cast<const uint32_t*>( m_uuid.data )[i] );
  127. return hash;
  128. }
  129. void KIID::Clone( const KIID& aUUID )
  130. {
  131. m_uuid = aUUID.m_uuid;
  132. m_cached_timestamp = aUUID.m_cached_timestamp;
  133. }
  134. wxString KIID::AsString() const
  135. {
  136. return boost::uuids::to_string( m_uuid );
  137. }
  138. wxString KIID::AsLegacyTimestampString() const
  139. {
  140. return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() );
  141. }
  142. void KIID::ConvertTimestampToUuid()
  143. {
  144. if( !IsLegacyTimestamp() )
  145. return;
  146. m_cached_timestamp = 0;
  147. m_uuid = randomGenerator();
  148. }
  149. KIID_PATH::KIID_PATH( const wxString& aString )
  150. {
  151. for( const wxString& pathStep : wxSplit( aString, '/' ) )
  152. {
  153. if( !pathStep.empty() )
  154. emplace_back( KIID( pathStep ) );
  155. }
  156. }
  157. wxString KIID_PATH::AsString() const
  158. {
  159. wxString path;
  160. for( const KIID& pathStep : *this )
  161. path += '/' + pathStep.AsString();
  162. return path;
  163. }