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
152const CFB::COMPOUND_FILE_ENTRY*
153ALTIUM_COMPOUND_FILE::FindStreamSingleLevel( const CFB::COMPOUND_FILE_ENTRY* aEntry,
154 const std::string aName, const bool aIsStream ) const
155{
156 if( !m_reader || !aEntry )
157 return nullptr;
158
159 const CFB::COMPOUND_FILE_ENTRY* ret = nullptr;
160
161 m_reader->EnumFiles( aEntry, 1,
162 [&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string& dir,
163 int level ) -> int
164 {
165 if( ret != nullptr )
166 return 1;
167
168 if( m_reader->IsStream( entry ) == aIsStream )
169 {
170 std::string name = UTF16ToUTF8( entry->name );
171 if( name == aName.c_str() )
172 {
173 ret = entry;
174 return 1;
175 }
176 }
177
178 return 0;
179 } );
180 return ret;
181}
182
183
184std::map<wxString, ALTIUM_SYMBOL_DATA>
185ALTIUM_COMPOUND_FILE::GetLibSymbols( const CFB::COMPOUND_FILE_ENTRY* aStart ) const
186{
187 const CFB::COMPOUND_FILE_ENTRY* root = aStart ? aStart : m_reader->GetRootEntry();
188
189 if( !root )
190 return {};
191
192 std::map<wxString, ALTIUM_SYMBOL_DATA> folders;
193
194 m_reader->EnumFiles( root, 1, [&]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string&, int ) -> int
195 {
196 wxString dirName = UTF16ToWstring( tentry->name, tentry->nameLen );
197
198 if( m_reader->IsStream( tentry ) )
199 return 0;
200
201 m_reader->EnumFiles( tentry, 1,
202 [&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&, int ) -> int
203 {
204 std::wstring fileName = UTF16ToWstring( entry->name, entry->nameLen );
205
206 if( m_reader->IsStream( entry ) && fileName == L"Data" )
207 folders[dirName].m_symbol = entry;
208
209 if( m_reader->IsStream( entry ) && fileName == L"PinFrac" )
210 folders[dirName].m_pinsFrac = entry;
211
212 if( m_reader->IsStream( entry ) && fileName == L"PinWideText" )
213 folders[dirName].m_pinsWideText = entry;
214
215 if( m_reader->IsStream( entry ) && fileName == L"PinTextData" )
216 folders[dirName].m_pinsTextData = entry;
217
218 return 0;
219 } );
220
221 return 0;
222 } );
223
224 return folders;
225}
226
227
228std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*>
229ALTIUM_COMPOUND_FILE::EnumDir( const std::wstring& aDir ) const
230{
231 const CFB::COMPOUND_FILE_ENTRY* root = m_reader->GetRootEntry();
232
233 if( !root )
234 return {};
235
236 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> files;
237
238 m_reader->EnumFiles(
239 root, 1,
240 [&]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string& dir,
241 int level ) -> int
242 {
243 if( m_reader->IsStream( tentry ) )
244 return 0;
245
246 std::wstring dirName = UTF16ToWstring( tentry->name, tentry->nameLen );
247
248 if( dirName != aDir )
249 return 0;
250
251 m_reader->EnumFiles(
252 tentry, 1,
253 [&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&,
254 int ) -> int
255 {
256 if( m_reader->IsStream( entry ) )
257 {
258 std::wstring fileName =
259 UTF16ToWstring( entry->name, entry->nameLen );
260
261 files[fileName] = entry;
262 }
263
264 return 0;
265 } );
266 return 0;
267 } );
268
269 return files;
270}
271
272
273const CFB::COMPOUND_FILE_ENTRY*
274ALTIUM_COMPOUND_FILE::FindStream( const CFB::COMPOUND_FILE_ENTRY* aStart,
275 const std::vector<std::string>& aStreamPath ) const
276{
277 if( !m_reader )
278 return nullptr;
279
280 if( !aStart )
281 aStart = m_reader->GetRootEntry();
282
283 auto it = aStreamPath.cbegin();
284
285 while( aStart != nullptr )
286 {
287 const std::string& name = *it;
288
289 if( ++it == aStreamPath.cend() )
290 {
291 const CFB::COMPOUND_FILE_ENTRY* ret = FindStreamSingleLevel( aStart, name, true );
292 return ret;
293 }
294 else
295 {
296 const CFB::COMPOUND_FILE_ENTRY* ret = FindStreamSingleLevel( aStart, name, false );
297 aStart = ret;
298 }
299 }
300
301 return nullptr;
302}
303
304
305const CFB::COMPOUND_FILE_ENTRY*
306ALTIUM_COMPOUND_FILE::FindStream( const std::vector<std::string>& aStreamPath ) const
307{
308 return FindStream( nullptr, aStreamPath );
309}
310
311
313 const CFB::COMPOUND_FILE_ENTRY* aEntry )
314{
315 m_subrecord_end = nullptr;
316 m_size = static_cast<size_t>( aEntry->size );
317 m_error = false;
318 m_content.reset( new char[m_size] );
319 m_pos = m_content.get();
320
321 // read file into buffer
322 aFile.GetCompoundFileReader().ReadFile( aEntry, 0, m_content.get(), m_size );
323}
324
325
326ALTIUM_BINARY_PARSER::ALTIUM_BINARY_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize )
327{
328 m_subrecord_end = nullptr;
329 m_size = aSize;
330 m_error = false;
331 m_content = std::move( aContent );
332 m_pos = m_content.get();
333}
334
335
336std::map<wxString, wxString> ALTIUM_BINARY_PARSER::ReadProperties(
337 std::function<std::map<wxString, wxString>( const std::string& )> handleBinaryData )
338{
339
340 std::map<wxString, wxString> kv;
341
342 uint32_t length = Read<uint32_t>();
343 bool isBinary = ( length & 0xff000000 ) != 0;
344
345 length &= 0x00ffffff;
346
347 if( length > GetRemainingBytes() )
348 {
349 m_error = true;
350 return kv;
351 }
352
353 if( length == 0 )
354 {
355 return kv;
356 }
357
358 // There is one case by kliment where Board6 ends with "|NEARDISTANCE=1000mi".
359 // Both the 'l' and the null-byte are missing, which looks like Altium swallowed two bytes.
360 bool hasNullByte = m_pos[length - 1] == '\0';
361
362 if( !hasNullByte && !isBinary )
363 {
364 wxLogTrace( "ALTIUM", wxT( "Missing null byte at end of property list. Imported data might be "
365 "malformed or missing." ) );
366 }
367
368 // we use std::string because std::string can handle NULL-bytes
369 // wxString would end the string at the first NULL-byte
370 std::string str = std::string( m_pos, length - ( hasNullByte ? 1 : 0 ) );
371 m_pos += length;
372
373 if( isBinary )
374 {
375 return handleBinaryData( str );
376 }
377
378 std::size_t token_end = 0;
379
380 while( token_end < str.size() && token_end != std::string::npos )
381 {
382 std::size_t token_start = str.find( '|', token_end );
383 std::size_t token_equal = str.find( '=', token_end );
384 std::size_t key_start;
385
386 if( token_start <= token_equal )
387 {
388 key_start = token_start + 1;
389 }
390 else
391 {
392 // Leading "|" before "RECORD=28" may be missing in older schematic versions.
393 key_start = token_end;
394 }
395
396 token_end = str.find( '|', key_start );
397
398 if( token_equal >= token_end )
399 {
400 continue; // this looks like an error: skip the entry. Also matches on std::string::npos
401 }
402
403 if( token_end == std::string::npos )
404 {
405 token_end = str.size() + 1; // this is the correct offset
406 }
407
408 std::string keyS = str.substr( key_start, token_equal - key_start );
409 std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
410
411 // convert the strings to wxStrings, since we use them everywhere
412 // value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
413 wxString key( keyS.c_str(), wxConvISO8859_1 );
414 // Altium stores keys either in Upper, or in CamelCase. Lets unify it.
415 wxString canonicalKey = key.Trim( false ).Trim( true ).MakeUpper();
416 // If the key starts with '%UTF8%' we have to parse the value using UTF8
417 wxString value;
418
419 if( canonicalKey.StartsWith( "%UTF8%" ) )
420 value = wxString( valueS.c_str(), wxConvUTF8 );
421 else
422 value = wxString( valueS.c_str(), wxConvISO8859_1 );
423
424 if( canonicalKey != wxS( "PATTERN" ) && canonicalKey != wxS( "SOURCEFOOTPRINTLIBRARY" ) )
425 {
426 // Breathless hack because I haven't a clue what the story is here (but this character
427 // appears in a lot of radial dimensions and is rendered by Altium as a space).
428 value.Replace( wxT( "ÿ" ), wxT( " " ) );
429 }
430
431 if( canonicalKey == wxT( "DESIGNATOR" )
432 || canonicalKey == wxT( "NAME" )
433 || canonicalKey == wxT( "TEXT" ) )
434 {
435 value = AltiumPropertyToKiCadString( value.Trim() );
436 }
437
438 kv.insert( { canonicalKey, value.Trim() } );
439 }
440
441 return kv;
442}
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 >();})
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, ALTIUM_SYMBOL_DATA > GetLibSymbols(const CFB::COMPOUND_FILE_ENTRY *aStart) const
const CFB::COMPOUND_FILE_ENTRY * FindStream(const std::vector< std::string > &aStreamPath) const
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
#define kv