KiCad PCB EDA Suite
Loading...
Searching...
No Matches
altium_binary_parser.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) 2019-2020 Thomas Pointhuber <[email protected]>
5 * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
26#include "altium_parser_utils.h"
27
28#include <compoundfilereader.h>
29#include <charconv>
30#include <ki_exception.h>
31#include <math/util.h>
32#include <numeric>
33#include <sstream>
34#include <utf.h>
35#include <wx/log.h>
36#include <wx/translation.h>
37
38
39// Helper for debug logging
40std::string FormatPath( const std::vector<std::string>& aVectorPath )
41{
42 return std::accumulate( aVectorPath.cbegin(), aVectorPath.cend(), std::string(),
43 []( const std::string& ss, const std::string& s )
44 {
45 return ss.empty() ? s : ss + '\\' + s;
46 } );
47}
48
49
51{
52 // Open file
53 FILE* fp = wxFopen( aFilePath, "rb" );
54
55 if( fp == nullptr )
56 {
57 THROW_IO_ERROR( wxString::Format( _( "Cannot open file '%s'." ), aFilePath ) );
58 }
59
60 fseek( fp, 0, SEEK_END );
61 long len = ftell( fp );
62
63 if( len < 0 )
64 {
65 fclose( fp );
66 THROW_IO_ERROR( _( "Error reading file: cannot determine length." ) );
67 }
68
69 // Read into buffer (TODO: add support for memory-mapped files to avoid this copy!)
70 m_buffer.resize( len );
71
72 fseek( fp, 0, SEEK_SET );
73
74 size_t bytesRead = fread( m_buffer.data(), sizeof( unsigned char ), len, fp );
75 fclose( fp );
76
77 if( static_cast<size_t>( len ) != bytesRead )
78 {
79 THROW_IO_ERROR( _( "Error reading file." ) );
80 }
81
82 try
83 {
84 m_reader = std::make_unique<CFB::CompoundFileReader>( m_buffer.data(), m_buffer.size() );
85 }
86 catch( CFB::CFBException& exception )
87 {
88 THROW_IO_ERROR( exception.what() );
89 }
90}
91
92
93ALTIUM_COMPOUND_FILE::ALTIUM_COMPOUND_FILE( const void* aBuffer, size_t aLen )
94{
95 m_buffer.resize( aLen );
96 memcpy( m_buffer.data(), aBuffer, aLen );
97
98 try
99 {
100 m_reader = std::make_unique<CFB::CompoundFileReader>( m_buffer.data(), m_buffer.size() );
101 }
102 catch( CFB::CFBException& exception )
103 {
104 THROW_IO_ERROR( exception.what() );
105 }
106}
107
108
109std::unique_ptr<ALTIUM_COMPOUND_FILE>
110ALTIUM_COMPOUND_FILE::DecodeIntLibStream( const CFB::COMPOUND_FILE_ENTRY& cfe )
111{
112 wxCHECK( cfe.size >= 1, nullptr );
113
114 size_t streamSize = cfe.size;
115 wxMemoryBuffer buffer( streamSize );
116 buffer.SetDataLen( streamSize );
117
118 // read file into buffer
119 GetCompoundFileReader().ReadFile( &cfe, 0, reinterpret_cast<char*>( buffer.GetData() ),
120 streamSize );
121
122 // 0x02: compressed stream, 0x00: uncompressed
123 if( buffer[0] == 0x02 )
124 {
125 wxMemoryInputStream memoryInputStream( buffer.GetData(), streamSize );
126 memoryInputStream.SeekI( 1, wxFromStart );
127
128 wxZlibInputStream zlibInputStream( memoryInputStream );
129 wxMemoryOutputStream decodedPcbLibStream;
130 decodedPcbLibStream << zlibInputStream;
131
132 wxStreamBuffer* outStream = decodedPcbLibStream.GetOutputStreamBuffer();
133
134 return std::make_unique<ALTIUM_COMPOUND_FILE>( outStream->GetBufferStart(),
135 outStream->GetIntPosition() );
136 }
137 else if( buffer[0] == 0x00 )
138 {
139 return std::make_unique<ALTIUM_COMPOUND_FILE>(
140 reinterpret_cast<uint8_t*>( buffer.GetData() ) + 1, streamSize - 1 );
141 }
142 else
143 {
144 wxFAIL_MSG( wxString::Format( "Altium IntLib unknown header: %02x %02x %02x %02x %02x",
145 buffer[0], buffer[1], buffer[2], buffer[3], buffer[4] ) );
146 }
147
148 return nullptr;
149}
150
151
152std::map<wxString, wxString> ALTIUM_COMPOUND_FILE::ListLibFootprints()
153{
154 if( m_libFootprintDirNameCache.empty() )
156
158}
159
160
161std::tuple<wxString, const CFB::COMPOUND_FILE_ENTRY*>
162ALTIUM_COMPOUND_FILE::FindLibFootprintDirName( const wxString& aFpUnicodeName )
163{
164 if( m_libFootprintNameCache.empty() )
166
167 auto it = m_libFootprintNameCache.find( aFpUnicodeName );
168
169 if( it == m_libFootprintNameCache.end() )
170 return { wxEmptyString, nullptr };
171
172 return { it->first, it->second };
173}
174
175
176const CFB::COMPOUND_FILE_ENTRY*
177ALTIUM_COMPOUND_FILE::FindStreamSingleLevel( const CFB::COMPOUND_FILE_ENTRY* aEntry,
178 const std::string aName, const bool aIsStream ) const
179{
180 if( !m_reader || !aEntry )
181 return nullptr;
182
183 const CFB::COMPOUND_FILE_ENTRY* ret = nullptr;
184
185 m_reader->EnumFiles( aEntry, 1,
186 [&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string& dir,
187 int level ) -> int
188 {
189 if( ret != nullptr )
190 return 1;
191
192 if( m_reader->IsStream( entry ) == aIsStream )
193 {
194 std::string name = UTF16ToUTF8( entry->name );
195 if( name == aName.c_str() )
196 {
197 ret = entry;
198 return 1;
199 }
200 }
201
202 return 0;
203 } );
204 return ret;
205}
206
207
208std::map<wxString, ALTIUM_SYMBOL_DATA>
209ALTIUM_COMPOUND_FILE::GetLibSymbols( const CFB::COMPOUND_FILE_ENTRY* aStart ) const
210{
211 const CFB::COMPOUND_FILE_ENTRY* root = aStart ? aStart : m_reader->GetRootEntry();
212
213 if( !root )
214 return {};
215
216 std::map<wxString, ALTIUM_SYMBOL_DATA> folders;
217
218 m_reader->EnumFiles( root, 1, [&]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string&, int ) -> int
219 {
220 wxString dirName = UTF16ToWstring( tentry->name, tentry->nameLen );
221
222 if( m_reader->IsStream( tentry ) )
223 return 0;
224
225 m_reader->EnumFiles( tentry, 1,
226 [&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&, int ) -> int
227 {
228 std::wstring fileName = UTF16ToWstring( entry->name, entry->nameLen );
229
230 if( m_reader->IsStream( entry ) && fileName == L"Data" )
231 folders[dirName].m_symbol = entry;
232
233 if( m_reader->IsStream( entry ) && fileName == L"PinFrac" )
234 folders[dirName].m_pinsFrac = entry;
235
236 if( m_reader->IsStream( entry ) && fileName == L"PinWideText" )
237 folders[dirName].m_pinsWideText = entry;
238
239 if( m_reader->IsStream( entry ) && fileName == L"PinTextData" )
240 folders[dirName].m_pinsTextData = entry;
241
242 return 0;
243 } );
244
245 return 0;
246 } );
247
248 return folders;
249}
250
251
252std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*>
253ALTIUM_COMPOUND_FILE::EnumDir( const std::wstring& aDir ) const
254{
255 const CFB::COMPOUND_FILE_ENTRY* root = m_reader->GetRootEntry();
256
257 if( !root )
258 return {};
259
260 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> files;
261
262 m_reader->EnumFiles(
263 root, 1,
264 [&]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string& dir,
265 int level ) -> int
266 {
267 if( m_reader->IsStream( tentry ) )
268 return 0;
269
270 std::wstring dirName = UTF16ToWstring( tentry->name, tentry->nameLen );
271
272 if( dirName != aDir )
273 return 0;
274
275 m_reader->EnumFiles(
276 tentry, 1,
277 [&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&,
278 int ) -> int
279 {
280 if( m_reader->IsStream( entry ) )
281 {
282 std::wstring fileName =
283 UTF16ToWstring( entry->name, entry->nameLen );
284
285 files[fileName] = entry;
286 }
287
288 return 0;
289 } );
290 return 0;
291 } );
292
293 return files;
294}
295
296
297const CFB::COMPOUND_FILE_ENTRY*
298ALTIUM_COMPOUND_FILE::FindStream( const CFB::COMPOUND_FILE_ENTRY* aStart,
299 const std::vector<std::string>& aStreamPath ) const
300{
301 if( !m_reader )
302 return nullptr;
303
304 if( !aStart )
305 aStart = m_reader->GetRootEntry();
306
307 auto it = aStreamPath.cbegin();
308
309 while( aStart != nullptr )
310 {
311 const std::string& name = *it;
312
313 if( ++it == aStreamPath.cend() )
314 {
315 const CFB::COMPOUND_FILE_ENTRY* ret = FindStreamSingleLevel( aStart, name, true );
316 return ret;
317 }
318 else
319 {
320 const CFB::COMPOUND_FILE_ENTRY* ret = FindStreamSingleLevel( aStart, name, false );
321 aStart = ret;
322 }
323 }
324
325 return nullptr;
326}
327
328
329const CFB::COMPOUND_FILE_ENTRY*
330ALTIUM_COMPOUND_FILE::FindStream( const std::vector<std::string>& aStreamPath ) const
331{
332 return FindStream( nullptr, aStreamPath );
333}
334
335
337{
340
341 if( !m_reader )
342 return;
343
344 const CFB::COMPOUND_FILE_ENTRY* root = m_reader->GetRootEntry();
345
346 if( !root )
347 return;
348
349 m_reader->EnumFiles( root, 1,
350 [this]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string& dir,
351 int level ) -> int
352 {
353 if( m_reader->IsStream( tentry ) )
354 return 0;
355
356 m_reader->EnumFiles( tentry, 1,
357 [&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&, int ) -> int
358 {
359 std::wstring fileName = UTF16ToWstring( entry->name, entry->nameLen );
360
361 if( m_reader->IsStream( entry ) && fileName == L"Parameters" )
362 {
363 ALTIUM_BINARY_PARSER parametersReader( *this, entry );
364 std::map<wxString, wxString> parameterProperties =
365 parametersReader.ReadProperties();
366
367 wxString key = ALTIUM_PROPS_UTILS::ReadString(
368 parameterProperties, wxT( "PATTERN" ), wxT( "" ) );
369 wxString fpName = ALTIUM_PROPS_UTILS::ReadUnicodeString(
370 parameterProperties, wxT( "PATTERN" ), wxT( "" ) );
371
372 m_libFootprintDirNameCache[key] = fpName;
373 m_libFootprintNameCache[fpName] = tentry;
374 }
375
376 return 0;
377 } );
378 return 0;
379 } );
380}
381
382
384 const CFB::COMPOUND_FILE_ENTRY* aEntry )
385{
386 m_subrecord_end = nullptr;
387 m_size = static_cast<size_t>( aEntry->size );
388 m_error = false;
389 m_content.reset( new char[m_size] );
390 m_pos = m_content.get();
391
392 // read file into buffer
393 aFile.GetCompoundFileReader().ReadFile( aEntry, 0, m_content.get(), m_size );
394}
395
396
397ALTIUM_BINARY_PARSER::ALTIUM_BINARY_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize )
398{
399 m_subrecord_end = nullptr;
400 m_size = aSize;
401 m_error = false;
402 m_content = std::move( aContent );
403 m_pos = m_content.get();
404}
405
406
407std::map<wxString, wxString> ALTIUM_BINARY_PARSER::ReadProperties(
408 std::function<std::map<wxString, wxString>( const std::string& )> handleBinaryData )
409{
410
411 std::map<wxString, wxString> kv;
412
413 uint32_t length = Read<uint32_t>();
414 bool isBinary = ( length & 0xff000000 ) != 0;
415
416 length &= 0x00ffffff;
417
418 if( length > GetRemainingBytes() )
419 {
420 m_error = true;
421 return kv;
422 }
423
424 if( length == 0 )
425 {
426 return kv;
427 }
428
429 // There is one case by kliment where Board6 ends with "|NEARDISTANCE=1000mi".
430 // Both the 'l' and the null-byte are missing, which looks like Altium swallowed two bytes.
431 bool hasNullByte = m_pos[length - 1] == '\0';
432
433 if( !hasNullByte && !isBinary )
434 {
435 wxLogError( _( "Missing null byte at end of property list. Imported data might be "
436 "malformed or missing." ) );
437 }
438
439 // we use std::string because std::string can handle NULL-bytes
440 // wxString would end the string at the first NULL-byte
441 std::string str = std::string( m_pos, length - ( hasNullByte ? 1 : 0 ) );
442 m_pos += length;
443
444 if( isBinary )
445 {
446 return handleBinaryData( str );
447 }
448
449 std::size_t token_end = 0;
450
451 while( token_end < str.size() && token_end != std::string::npos )
452 {
453 std::size_t token_start = str.find( '|', token_end );
454 std::size_t token_equal = str.find( '=', token_end );
455 std::size_t key_start;
456
457 if( token_start <= token_equal )
458 {
459 key_start = token_start + 1;
460 }
461 else
462 {
463 // Leading "|" before "RECORD=28" may be missing in older schematic versions.
464 key_start = token_end;
465 }
466
467 token_end = str.find( '|', key_start );
468
469 if( token_equal >= token_end )
470 {
471 continue; // this looks like an error: skip the entry. Also matches on std::string::npos
472 }
473
474 if( token_end == std::string::npos )
475 {
476 token_end = str.size() + 1; // this is the correct offset
477 }
478
479 std::string keyS = str.substr( key_start, token_equal - key_start );
480 std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
481
482 // convert the strings to wxStrings, since we use them everywhere
483 // value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
484 wxString key( keyS.c_str(), wxConvISO8859_1 );
485 // Altium stores keys either in Upper, or in CamelCase. Lets unify it.
486 wxString canonicalKey = key.Trim( false ).Trim( true ).MakeUpper();
487 // If the key starts with '%UTF8%' we have to parse the value using UTF8
488 wxString value;
489
490 if( canonicalKey.StartsWith( "%UTF8%" ) )
491 value = wxString( valueS.c_str(), wxConvUTF8 );
492 else
493 value = wxString( valueS.c_str(), wxConvISO8859_1 );
494
495 if( canonicalKey != wxS( "PATTERN" ) && canonicalKey != wxS( "SOURCEFOOTPRINTLIBRARY" ) )
496 {
497 // Breathless hack because I haven't a clue what the story is here (but this character
498 // appears in a lot of radial dimensions and is rendered by Altium as a space).
499 value.Replace( wxT( "ÿ" ), wxT( " " ) );
500 }
501
502 if( canonicalKey == wxT( "DESIGNATOR" )
503 || canonicalKey == wxT( "NAME" )
504 || canonicalKey == wxT( "TEXT" ) )
505 {
506 value = AltiumPropertyToKiCadString( value.Trim() );
507 }
508
509 kv.insert( { canonicalKey, value.Trim() } );
510 }
511
512 return kv;
513}
const char * name
Definition: DXF_plotter.cpp:57
std::string FormatPath(const std::vector< std::string > &aVectorPath)
Helper for debug logging (vector -> string)
wxString AltiumPropertyToKiCadString(const wxString &aString)
size_t GetRemainingBytes() const
std::unique_ptr< char[]> m_content
ALTIUM_BINARY_PARSER(const ALTIUM_COMPOUND_FILE &aFile, const CFB::COMPOUND_FILE_ENTRY *aEntry)
std::map< wxString, wxString > ReadProperties(std::function< std::map< wxString, wxString >(const std::string &)> handleBinaryData=[](const std::string &) { return std::map< wxString, wxString >();})
std::map< wxString, wxString > ListLibFootprints()
const CFB::CompoundFileReader & GetCompoundFileReader() const
std::unique_ptr< ALTIUM_COMPOUND_FILE > DecodeIntLibStream(const CFB::COMPOUND_FILE_ENTRY &cfe)
const CFB::COMPOUND_FILE_ENTRY * FindStreamSingleLevel(const CFB::COMPOUND_FILE_ENTRY *aEntry, const std::string aName, const bool aIsStream) const
std::map< wxString, const CFB::COMPOUND_FILE_ENTRY * > EnumDir(const std::wstring &aDir) const
std::vector< char > m_buffer
std::unique_ptr< CFB::CompoundFileReader > m_reader
ALTIUM_COMPOUND_FILE(const wxString &aFilePath)
Open a CFB file.
std::map< wxString, const CFB::COMPOUND_FILE_ENTRY * > m_libFootprintNameCache
std::tuple< wxString, const CFB::COMPOUND_FILE_ENTRY * > FindLibFootprintDirName(const wxString &aFpUnicodeName)
std::map< wxString, ALTIUM_SYMBOL_DATA > GetLibSymbols(const CFB::COMPOUND_FILE_ENTRY *aStart) const
const CFB::COMPOUND_FILE_ENTRY * FindStream(const std::vector< std::string > &aStreamPath) const
std::map< wxString, wxString > m_libFootprintDirNameCache
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
#define kv