KiCad PCB EDA Suite
Loading...
Searching...
No Matches
altium_binary_parser.h
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) 2020-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
25#ifndef _ALTIUM_BINARY_PARSER_H
26#define _ALTIUM_BINARY_PARSER_H
27
28#include "altium_props_utils.h"
29
30#include <charconv>
31#include <map>
32#include <memory>
33#include <numeric>
34#include <string>
35#include <stdexcept>
36#include <vector>
37
38#include <wx/mstream.h>
39#include <wx/zstream.h>
40#include <math/vector2d.h>
41
42namespace CFB
43{
44class CompoundFileReader;
45struct COMPOUND_FILE_ENTRY;
46} // namespace CFB
47
53std::string FormatPath( const std::vector<std::string>& aVectorPath );
54
55
57{
58public:
59 const CFB::COMPOUND_FILE_ENTRY* m_symbol;
60 const CFB::COMPOUND_FILE_ENTRY* m_pinsFrac;
61 const CFB::COMPOUND_FILE_ENTRY* m_pinsWideText;
62 const CFB::COMPOUND_FILE_ENTRY* m_pinsTextData;
63 const CFB::COMPOUND_FILE_ENTRY* m_pinsSymbolLineWidth;
64};
65
67{
68public:
74 ALTIUM_COMPOUND_FILE( const wxString& aFilePath );
75
83 ALTIUM_COMPOUND_FILE( const void* aBuffer, size_t aLen );
84
85 ALTIUM_COMPOUND_FILE( const ALTIUM_COMPOUND_FILE& temp_obj ) = delete;
88
89 const CFB::CompoundFileReader& GetCompoundFileReader() const { return *m_reader; }
90
91 std::unique_ptr<ALTIUM_COMPOUND_FILE> DecodeIntLibStream( const CFB::COMPOUND_FILE_ENTRY& cfe );
92
93 std::map<wxString, wxString> ListLibFootprints();
94
95 std::tuple<wxString, const CFB::COMPOUND_FILE_ENTRY*> FindLibFootprintDirName( const wxString& aFpUnicodeName );
96
97 const CFB::COMPOUND_FILE_ENTRY* FindStream( const std::vector<std::string>& aStreamPath ) const;
98
99 const CFB::COMPOUND_FILE_ENTRY* FindStream( const CFB::COMPOUND_FILE_ENTRY* aStart, const std::vector<std::string>& aStreamPath ) const;
100
101 const CFB::COMPOUND_FILE_ENTRY* FindStreamSingleLevel( const CFB::COMPOUND_FILE_ENTRY* aEntry,
102 const std::string aName,
103 const bool aIsStream ) const;
104
105 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> EnumDir( const std::wstring& aDir ) const;
106
107 std::map<wxString, ALTIUM_SYMBOL_DATA> GetLibSymbols( const CFB::COMPOUND_FILE_ENTRY* aStart ) const;
108
109private:
110
112
113 std::unique_ptr<CFB::CompoundFileReader> m_reader;
114 std::vector<char> m_buffer;
115
116 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> m_libFootprintNameCache;
117 std::map<wxString, wxString> m_libFootprintDirNameCache;
118};
119
120
122{
123public:
124 ALTIUM_BINARY_PARSER( const ALTIUM_COMPOUND_FILE& aFile, const CFB::COMPOUND_FILE_ENTRY* aEntry );
125 ALTIUM_BINARY_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize );
127
128 template <typename Type>
129 Type Read()
130 {
131 const size_t remainingBytes = GetRemainingBytes();
132 if( remainingBytes >= sizeof( Type ) )
133 {
134 Type val = *(Type*) ( m_pos );
135 m_pos += sizeof( Type );
136 return val;
137 }
138 else
139 {
140 m_pos += remainingBytes; // Ensure remaining bytes are zero
141 m_error = true;
142 return 0;
143 }
144 }
145
146 template <typename Type>
147 Type Peek()
148 {
149 char* const oldPos = m_pos;
150 const bool oldError = m_error;
151 Type result = Read<Type>();
152 m_pos = oldPos;
153 m_error = oldError;
154 return result;
155 }
156
157 wxScopedCharBuffer ReadCharBuffer()
158 {
159 uint8_t len = Read<uint8_t>();
160
161 if( GetRemainingBytes() >= len )
162 {
163 char* buf = static_cast<char*>( malloc( len ) );
164 memcpy( buf, m_pos, len );
165 m_pos += len;
166
167 return wxScopedCharBuffer::CreateOwned( buf, len );
168 }
169 else
170 {
171 m_error = true;
172 return wxScopedCharBuffer();
173 }
174 }
175
176 wxString ReadWxString()
177 {
178 // TODO: Identify where the actual code page is stored. For now, this default code page
179 // has limited impact, because recent Altium files come with a UTF16 string table
180 return wxString( ReadCharBuffer(), wxConvISO8859_1 );
181 }
182
183 std::map<uint32_t, wxString> ReadWideStringTable()
184 {
185 std::map<uint32_t, wxString> table;
186 size_t remaining = GetRemainingBytes();
187
188 while( remaining >= 8 )
189 {
190 uint32_t index = Read<uint32_t>();
191 uint32_t length = Read<uint32_t>();
192 wxString str;
193 remaining -= 8;
194
195 if( length <= 2 )
196 length = 0; // for empty strings, not even the null bytes are present
197 else
198 {
199 if( length > remaining )
200 break;
201
202 str = wxString( m_pos, wxMBConvUTF16LE(), length - 2 );
203 }
204
205 table.emplace( index, str );
206 m_pos += length;
207 remaining -= length;
208 }
209
210 return table;
211 }
212
213 std::vector<char> ReadVector( size_t aSize )
214 {
215 if( aSize > GetRemainingBytes() )
216 {
217 m_error = true;
218 return {};
219 }
220 else
221 {
222 std::vector<char> data( m_pos, m_pos + aSize );
223 m_pos += aSize;
224 return data;
225 }
226 }
227
228 int ReadBytes( char* aOut, size_t aSize )
229 {
230 if( aSize > GetRemainingBytes() )
231 {
232 m_error = true;
233 return 0;
234 }
235 else
236 {
237 memcpy( aOut, m_pos, aSize );
238 m_pos += aSize;
239 return aSize;
240 }
241 }
242
244 {
245 return ALTIUM_PROPS_UTILS::ConvertToKicadUnit( Read<int32_t>() );
246 }
247
249 {
250 return ReadKicadUnit();
251 }
252
254 {
255 return -ReadKicadUnit();
256 }
257
259 {
260 int32_t x = ReadKicadUnitX();
261 int32_t y = ReadKicadUnitY();
262 return { x, y };
263 }
264
266 {
267 int32_t x = ReadKicadUnit();
268 int32_t y = ReadKicadUnit();
269 return { x, y };
270 }
271
273 {
274 uint32_t length = Read<uint32_t>();
275 m_subrecord_end = m_pos + length;
276 return length;
277 }
278
279 std::map<wxString, wxString> ReadProperties(
280 std::function<std::map<wxString, wxString>( const std::string& )> handleBinaryData =
281 []( const std::string& )
282 {
283 return std::map<wxString, wxString>();
284 } );
285
286 void Skip( size_t aLength )
287 {
288 if( GetRemainingBytes() >= aLength )
289 {
290 m_pos += aLength;
291 }
292 else
293 {
294 m_error = true;
295 }
296 }
297
299 {
300 if( m_subrecord_end == nullptr || m_subrecord_end < m_pos )
301 {
302 m_error = true;
303 }
304 else
305 {
307 }
308 };
309
310 size_t GetRemainingBytes() const
311 {
312 return m_pos == nullptr ? 0 : m_size - ( m_pos - m_content.get() );
313 }
314
316 {
317 return m_pos == nullptr || m_subrecord_end == nullptr || m_subrecord_end <= m_pos ?
318 0 :
320 };
321
323 {
324 return m_error;
325 }
326
327private:
328 std::unique_ptr<char[]> m_content;
329 size_t m_size;
330
331 char* m_pos; // current read pointer
332 char* m_subrecord_end; // pointer which points to next subrecord start
334};
335
336
338{
339public:
340 ALTIUM_BINARY_READER( const std::string& binaryData ) : m_data( binaryData ), m_position( 0 ) {}
341
342 int32_t ReadInt32()
343 {
344 if( m_position + sizeof( int32_t ) > m_data.size() )
345 throw std::out_of_range( "ALTIUM_BINARY_READER: out of range" );
346
347 int32_t value = *reinterpret_cast<const int32_t*>( &m_data[m_position] );
348 m_position += sizeof( int32_t );
349 return value;
350 }
351
352 int16_t ReadInt16()
353 {
354 if( m_position + sizeof( int16_t ) > m_data.size() )
355 throw std::out_of_range( "ALTIUM_BINARY_READER: out of range" );
356
357 int16_t value = *reinterpret_cast<const int16_t*>( &m_data[m_position] );
358 m_position += sizeof( int16_t );
359 return value;
360 }
361
362 uint8_t ReadByte()
363 {
364 if( m_position + sizeof( uint8_t ) > m_data.size() )
365 throw std::out_of_range( "ALTIUM_BINARY_READER: out of range" );
366
367 uint8_t value = *reinterpret_cast<const uint8_t*>( &m_data[m_position] );
368 m_position += sizeof( uint8_t );
369 return value;
370 }
371
373 {
374 uint8_t length = ReadByte();
375
376 if( m_position + length > m_data.size() )
377 throw std::out_of_range( "ALTIUM_BINARY_READER: out of range" );
378
379 std::string pascalString( &m_data[m_position], &m_data[m_position + length] );
380 m_position += length;
381 return pascalString;
382 }
383
385 {
386 uint32_t length = ReadInt32();
387
388 if( m_position + length > m_data.size() )
389 throw std::out_of_range( "ALTIUM_BINARY_READER: out of range" );
390
391 std::string pascalString( &m_data[m_position], &m_data[m_position + length] );
392 m_position += length;
393 return pascalString;
394 }
395
396private:
397 const std::string& m_data;
399};
400
402{
403public:
404 ALTIUM_COMPRESSED_READER( const std::string& aData ) : ALTIUM_BINARY_READER( aData )
405 {}
406
407 std::pair<int, std::string*> ReadCompressedString()
408 {
409 std::string* result;
410 int id = -1;
411
412 uint8_t byte = ReadByte();
413 if( byte != 0xD0 )
414 throw std::runtime_error( "ALTIUM_COMPRESSED_READER: invalid compressed string" );
415
416 std::string str = ReadShortPascalString();
417 std::from_chars( str.data(), str.data() + str.size(), id );
418
419 std::string data = ReadFullPascalString();
420 result = decompressData( data );
421
422 return std::make_pair( id, result );
423 }
424
425private:
426 std::string decompressedData;
427
428 std::string* decompressData( std::string& aData )
429 {
430 // Create a memory input stream with the buffer
431 wxMemoryInputStream memStream( (void*) aData.data(), aData.length() );
432
433 // Create a zlib input stream with the memory input stream
434 wxZlibInputStream zStream( memStream );
435
436 // Read decompressed data from the zlib input stream
437 while( !zStream.Eof() )
438 {
439 char buffer[1024];
440 zStream.Read( buffer, sizeof( buffer ) );
441 size_t bytesRead = zStream.LastRead();
442 decompressedData.append( buffer, bytesRead );
443 }
444
445 return &decompressedData;
446 }
447};
448
449#endif //_ALTIUM_BINARY_PARSER_H
std::string FormatPath(const std::vector< std::string > &aVectorPath)
Helper for debug logging (vector -> string)
size_t GetRemainingBytes() const
~ALTIUM_BINARY_PARSER()=default
void Skip(size_t aLength)
std::vector< char > ReadVector(size_t aSize)
std::unique_ptr< char[]> m_content
wxScopedCharBuffer ReadCharBuffer()
std::map< uint32_t, wxString > ReadWideStringTable()
size_t GetRemainingSubrecordBytes() const
std::map< wxString, wxString > ReadProperties(std::function< std::map< wxString, wxString >(const std::string &)> handleBinaryData=[](const std::string &) { return std::map< wxString, wxString >();})
int ReadBytes(char *aOut, size_t aSize)
std::string ReadShortPascalString()
std::string ReadFullPascalString()
const std::string & m_data
ALTIUM_BINARY_READER(const std::string &binaryData)
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
~ALTIUM_COMPOUND_FILE()=default
std::map< wxString, const CFB::COMPOUND_FILE_ENTRY * > EnumDir(const std::wstring &aDir) const
ALTIUM_COMPOUND_FILE & operator=(const ALTIUM_COMPOUND_FILE &temp_obj)=delete
std::vector< char > m_buffer
std::unique_ptr< CFB::CompoundFileReader > m_reader
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
ALTIUM_COMPOUND_FILE(const ALTIUM_COMPOUND_FILE &temp_obj)=delete
std::string * decompressData(std::string &aData)
ALTIUM_COMPRESSED_READER(const std::string &aData)
std::pair< int, std::string * > ReadCompressedString()
static int32_t ConvertToKicadUnit(const double aValue)
const CFB::COMPOUND_FILE_ENTRY * m_pinsSymbolLineWidth
const CFB::COMPOUND_FILE_ENTRY * m_symbol
const CFB::COMPOUND_FILE_ENTRY * m_pinsFrac
const CFB::COMPOUND_FILE_ENTRY * m_pinsWideText
const CFB::COMPOUND_FILE_ENTRY * m_pinsTextData