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 The 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#include <math/vector3.h>
42
43namespace CFB
44{
45class CompoundFileReader;
46struct COMPOUND_FILE_ENTRY;
47} // namespace CFB
48
54std::string FormatPath( const std::vector<std::string>& aVectorPath );
55
56
58{
59public:
60 const CFB::COMPOUND_FILE_ENTRY* m_symbol;
61 const CFB::COMPOUND_FILE_ENTRY* m_pinsFrac;
62 const CFB::COMPOUND_FILE_ENTRY* m_pinsWideText;
63 const CFB::COMPOUND_FILE_ENTRY* m_pinsTextData;
64 const CFB::COMPOUND_FILE_ENTRY* m_pinsSymbolLineWidth;
65};
66
67
69{
71
72public:
78 ALTIUM_COMPOUND_FILE( const wxString& aFilePath );
79
87 ALTIUM_COMPOUND_FILE( const void* aBuffer, size_t aLen );
88
89 ALTIUM_COMPOUND_FILE( const ALTIUM_COMPOUND_FILE& temp_obj ) = delete;
92
93 const CFB::CompoundFileReader& GetCompoundFileReader() const { return *m_reader; }
94
95 std::unique_ptr<ALTIUM_COMPOUND_FILE> DecodeIntLibStream( const CFB::COMPOUND_FILE_ENTRY& cfe );
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,
100 const std::vector<std::string>& aStreamPath ) const;
101
102 const CFB::COMPOUND_FILE_ENTRY* FindStreamSingleLevel( const CFB::COMPOUND_FILE_ENTRY* aEntry,
103 const std::string aName,
104 const bool aIsStream ) const;
105
106 std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> EnumDir( const std::wstring& aDir ) const;
107
108 std::map<wxString, ALTIUM_SYMBOL_DATA> GetLibSymbols( const CFB::COMPOUND_FILE_ENTRY* aStart ) const;
109
110private:
111
112 std::unique_ptr<CFB::CompoundFileReader> m_reader;
113 std::vector<char> m_buffer;
114};
115
116
118{
119public:
121 const CFB::COMPOUND_FILE_ENTRY* aEntry );
122 ALTIUM_BINARY_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize );
124
125 template <typename Type>
126 Type Read()
127 {
128 const size_t remainingBytes = GetRemainingBytes();
129
130 if( remainingBytes >= sizeof( Type ) )
131 {
132 Type val = *(Type*) ( m_pos );
133 m_pos += sizeof( Type );
134 return val;
135 }
136 else
137 {
138 m_pos += remainingBytes; // Ensure remaining bytes are zero
139 m_error = true;
140 return 0;
141 }
142 }
143
144 template <typename Type>
145 Type Peek()
146 {
147 char* const oldPos = m_pos;
148 const bool oldError = m_error;
149 Type result = Read<Type>();
150 m_pos = oldPos;
151 m_error = oldError;
152 return result;
153 }
154
155 wxScopedCharBuffer ReadCharBuffer()
156 {
157 uint8_t len = Read<uint8_t>();
158
159 if( GetRemainingBytes() >= len )
160 {
161 char* buf = static_cast<char*>( malloc( len ) );
162 memcpy( buf, m_pos, len );
163 m_pos += len;
164
165 return wxScopedCharBuffer::CreateOwned( buf, len );
166 }
167 else
168 {
169 m_error = true;
170 return wxScopedCharBuffer();
171 }
172 }
173
174 wxString ReadWxString()
175 {
176 // TODO: Identify where the actual code page is stored. For now, this default code page
177 // has limited impact, because recent Altium files come with a UTF16 string table
178 return wxString( ReadCharBuffer(), wxConvISO8859_1 );
179 }
180
181 std::map<uint32_t, wxString> ReadWideStringTable()
182 {
183 std::map<uint32_t, wxString> table;
184 size_t remaining = GetRemainingBytes();
185
186 while( remaining >= 8 )
187 {
188 uint32_t index = Read<uint32_t>();
189 uint32_t length = Read<uint32_t>();
190 wxString str;
191 remaining -= 8;
192
193 if( length <= 2 )
194 {
195 length = 0; // for empty strings, not even the null bytes are present
196 }
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
414 if( byte != 0xD0 )
415 throw std::runtime_error( "ALTIUM_COMPRESSED_READER: invalid compressed string" );
416
417 std::string str = ReadShortPascalString();
418 std::from_chars( str.data(), str.data() + str.size(), id );
419
420 std::string data = ReadFullPascalString();
421 result = decompressData( data );
422
423 return std::make_pair( id, result );
424 }
425
426private:
427 std::string decompressedData;
428
429 std::string* decompressData( std::string& aData )
430 {
431 // Create a memory input stream with the buffer
432 wxMemoryInputStream memStream( (void*) aData.data(), aData.length() );
433
434 // Create a zlib input stream with the memory input stream
435 wxZlibInputStream zStream( memStream );
436
437 // Read decompressed data from the zlib input stream
438 while( !zStream.Eof() )
439 {
440 char buffer[1024];
441 zStream.Read( buffer, sizeof( buffer ) );
442 size_t bytesRead = zStream.LastRead();
443 decompressedData.append( buffer, bytesRead );
444 }
445
446 return &decompressedData;
447 }
448};
449
450#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)
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, ALTIUM_SYMBOL_DATA > GetLibSymbols(const CFB::COMPOUND_FILE_ENTRY *aStart) const
const CFB::COMPOUND_FILE_ENTRY * FindStream(const std::vector< std::string > &aStreamPath) const
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