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