KiCad PCB EDA Suite
kiid.cpp
Go to the documentation of this file.
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-2021 KiCad Developers, see AUTHORS.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 
26 #include <kiid.h>
27 
28 #include <boost/uuid/uuid_generators.hpp>
29 #include <boost/uuid/uuid_io.hpp>
30 #include <boost/functional/hash.hpp>
31 
32 #if BOOST_VERSION >= 106700
33 #include <boost/uuid/entropy_error.hpp>
34 #endif
35 
36 #include <wx/log.h>
37 
38 
39 // Create only once, as seeding is *very* expensive
40 static boost::uuids::random_generator randomGenerator;
41 
42 // These don't have the same performance penalty, but might as well be consistent
43 static boost::uuids::string_generator stringGenerator;
44 static boost::uuids::nil_generator nilGenerator;
45 
46 // Global nil reference
47 KIID niluuid( 0 );
48 
49 // When true, always create nil uuids for performance, when valid ones aren't needed
50 static bool createNilUuids = false;
51 
52 
53 // For static initialization
55 {
56  static KIID nil( 0 );
57  return nil;
58 }
59 
60 
62 {
64 
65 #if BOOST_VERSION >= 106700
66  try
67  {
68 #endif
69 
70  if( createNilUuids )
71  m_uuid = nilGenerator();
72  else
74 
75 #if BOOST_VERSION >= 106700
76  }
77  catch( const boost::uuids::entropy_error& )
78  {
79  wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
80  __FILE__, __FUNCTION__ );
81  }
82 #endif
83 }
84 
85 
86 KIID::KIID( int null ) : m_uuid( nilGenerator() ), m_cached_timestamp( 0 )
87 {
88  wxASSERT( null == 0 );
89 }
90 
91 
92 KIID::KIID( const wxString& aString ) : m_uuid(), m_cached_timestamp( 0 )
93 {
94  if( aString.length() == 8 )
95  {
96  // A legacy-timestamp-based UUID has only the last 4 octets filled in.
97  // Convert them individually to avoid stepping in the little-endian/big-endian
98  // doo-doo.
99  for( int i = 0; i < 4; ++i )
100  {
101  wxString octet = aString.substr( i * 2, 2 );
102  m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
103  }
104 
105  m_cached_timestamp = strtol( aString.c_str(), nullptr, 16 );
106  }
107  else
108  {
109  try
110  {
111  m_uuid = stringGenerator( aString.wc_str() );
112 
113  if( IsLegacyTimestamp() )
114  m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), nullptr, 16 );
115  }
116  catch( ... )
117  {
118  // Failed to parse string representation; best we can do is assign a new
119  // random one.
120 #if BOOST_VERSION >= 106700
121  try
122  {
123 #endif
124 
126 
127 #if BOOST_VERSION >= 106700
128  }
129  catch( const boost::uuids::entropy_error& )
130  {
131  wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
132  __FILE__, __FUNCTION__ );
133  }
134 #endif
135  }
136  }
137 }
138 
139 
140 bool KIID::SniffTest( const wxString& aCandidate )
141 {
142  static wxString niluuidStr = niluuid.AsString();
143 
144  if( aCandidate.Length() != niluuidStr.Length() )
145  return false;
146 
147  for( wxChar c : aCandidate )
148  {
149  if( c >= '0' && c <= '9' )
150  continue;
151 
152  if( c >= 'a' && c <= 'f' )
153  continue;
154 
155  if( c >= 'A' && c <= 'F' )
156  continue;
157 
158  if( c == '-' )
159  continue;
160 
161  return false;
162  }
163 
164  return true;
165 }
166 
167 
168 KIID::KIID( timestamp_t aTimestamp )
169 {
170  m_cached_timestamp = aTimestamp;
171 
172  // A legacy-timestamp-based UUID has only the last 4 octets filled in.
173  // Convert them individually to avoid stepping in the little-endian/big-endian
174  // doo-doo.
175  wxString str = AsLegacyTimestampString();
176 
177  for( int i = 0; i < 4; ++i )
178  {
179  wxString octet = str.substr( i * 2, 2 );
180  m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
181  }
182 }
183 
184 
186 {
187  return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11];
188 }
189 
190 
192 {
193  return m_cached_timestamp;
194 }
195 
196 
197 size_t KIID::Hash() const
198 {
199  size_t hash = 0;
200 
201  // Note: this is NOT little-endian/big-endian safe, but as long as it's just used
202  // at runtime it won't matter.
203 
204  for( int i = 0; i < 4; ++i )
205  boost::hash_combine( hash, reinterpret_cast<const uint32_t*>( m_uuid.data )[i] );
206 
207  return hash;
208 }
209 
210 
211 void KIID::Clone( const KIID& aUUID )
212 {
213  m_uuid = aUUID.m_uuid;
215 }
216 
217 
218 wxString KIID::AsString() const
219 {
220  return boost::uuids::to_string( m_uuid );
221 }
222 
223 
225 {
226  return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() );
227 }
228 
229 
231 {
232  if( !IsLegacyTimestamp() )
233  return;
234 
235  m_cached_timestamp = 0;
237 }
238 
239 
240 void KIID::CreateNilUuids( bool aNil )
241 {
242  createNilUuids = aNil;
243 }
244 
245 
246 KIID_PATH::KIID_PATH( const wxString& aString )
247 {
248  for( const wxString& pathStep : wxSplit( aString, '/' ) )
249  {
250  if( !pathStep.empty() )
251  emplace_back( KIID( pathStep ) );
252  }
253 }
254 
255 
257 {
258  KIID_PATH copy = *this;
259  clear();
260 
261  if( aPath.size() > copy.size() )
262  return false; // this path is not contained within aPath
263 
264  for( size_t i = 0; i < aPath.size(); ++i )
265  {
266  if( copy.at( i ).AsString() != aPath.at( i ).AsString() )
267  return false; // this path is not contained within aPath
268  }
269 
270  for( size_t i = aPath.size(); i < copy.size(); ++i )
271  push_back( copy.at( i ) );
272 
273  return true;
274 }
275 
276 
277 wxString KIID_PATH::AsString() const
278 {
279  wxString path;
280 
281  for( const KIID& pathStep : *this )
282  path += '/' + pathStep.AsString();
283 
284  return path;
285 }
KIID niluuid(0)
KIID()
Definition: kiid.cpp:61
wxString AsString() const
Definition: kiid.cpp:218
boost::uuids::uuid m_uuid
Definition: kiid.h:94
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash_eda.h:67
bool IsLegacyTimestamp() const
Definition: kiid.cpp:185
timestamp_t m_cached_timestamp
Definition: kiid.h:96
static boost::uuids::nil_generator nilGenerator
Definition: kiid.cpp:44
bool MakeRelativeTo(const KIID_PATH &aPath)
Definition: kiid.cpp:256
Definition: kiid.h:44
size_t Hash() const
Definition: kiid.cpp:197
KIID_PATH()
Definition: kiid.h:110
timestamp_t AsLegacyTimestamp() const
Definition: kiid.cpp:191
static bool SniffTest(const wxString &aCandidate)
Definition: kiid.cpp:140
static boost::uuids::random_generator randomGenerator
Definition: kiid.cpp:40
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition: kiid.cpp:230
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: kiid.h:32
void Clone(const KIID &aUUID)
Definition: kiid.cpp:211
static bool createNilUuids
Definition: kiid.cpp:50
wxString AsLegacyTimestampString() const
Definition: kiid.cpp:224
static void CreateNilUuids(bool aNil=true)
Definition: kiid.cpp:240
wxString AsString() const
Definition: kiid.cpp:277
KIID & NilUuid()
Definition: kiid.cpp:54
static boost::uuids::string_generator stringGenerator
Definition: kiid.cpp:43