2020-04-03 23:22:24 +00:00
|
|
|
|
/*
|
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2019-2020 Thomas Pointhuber <thomas.pointhuber@gmx.at>
|
2023-07-22 03:55:49 +00:00
|
|
|
|
* Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-04-03 23:22:24 +00:00
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
|
*/
|
|
|
|
|
|
2024-02-04 17:14:19 +00:00
|
|
|
|
#include "altium_binary_parser.h"
|
2021-07-01 18:41:46 +00:00
|
|
|
|
#include "altium_parser_utils.h"
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
|
|
|
|
#include <compoundfilereader.h>
|
2024-06-10 22:01:30 +00:00
|
|
|
|
#include <charconv>
|
2020-04-03 23:22:24 +00:00
|
|
|
|
#include <ki_exception.h>
|
2021-06-02 04:50:46 +00:00
|
|
|
|
#include <math/util.h>
|
2022-02-19 15:45:55 +00:00
|
|
|
|
#include <numeric>
|
2020-04-03 23:22:24 +00:00
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <utf.h>
|
2021-06-07 22:38:35 +00:00
|
|
|
|
#include <wx/log.h>
|
2021-06-26 19:21:30 +00:00
|
|
|
|
#include <wx/translation.h>
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
2022-01-06 15:27:09 +00:00
|
|
|
|
|
2022-02-19 15:45:55 +00:00
|
|
|
|
// Helper for debug logging
|
|
|
|
|
std::string FormatPath( const std::vector<std::string>& aVectorPath )
|
|
|
|
|
{
|
|
|
|
|
return std::accumulate( aVectorPath.cbegin(), aVectorPath.cend(), std::string(),
|
|
|
|
|
[]( const std::string& ss, const std::string& s )
|
|
|
|
|
{
|
|
|
|
|
return ss.empty() ? s : ss + '\\' + s;
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 15:27:09 +00:00
|
|
|
|
ALTIUM_COMPOUND_FILE::ALTIUM_COMPOUND_FILE( const wxString& aFilePath )
|
|
|
|
|
{
|
|
|
|
|
// Open file
|
|
|
|
|
FILE* fp = wxFopen( aFilePath, "rb" );
|
|
|
|
|
|
|
|
|
|
if( fp == nullptr )
|
|
|
|
|
{
|
|
|
|
|
THROW_IO_ERROR( wxString::Format( _( "Cannot open file '%s'." ), aFilePath ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fseek( fp, 0, SEEK_END );
|
|
|
|
|
long len = ftell( fp );
|
|
|
|
|
|
|
|
|
|
if( len < 0 )
|
|
|
|
|
{
|
|
|
|
|
fclose( fp );
|
|
|
|
|
THROW_IO_ERROR( _( "Error reading file: cannot determine length." ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read into buffer (TODO: add support for memory-mapped files to avoid this copy!)
|
|
|
|
|
m_buffer.resize( len );
|
|
|
|
|
|
|
|
|
|
fseek( fp, 0, SEEK_SET );
|
|
|
|
|
|
|
|
|
|
size_t bytesRead = fread( m_buffer.data(), sizeof( unsigned char ), len, fp );
|
|
|
|
|
fclose( fp );
|
|
|
|
|
|
|
|
|
|
if( static_cast<size_t>( len ) != bytesRead )
|
|
|
|
|
{
|
|
|
|
|
THROW_IO_ERROR( _( "Error reading file." ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
m_reader = std::make_unique<CFB::CompoundFileReader>( m_buffer.data(), m_buffer.size() );
|
|
|
|
|
}
|
|
|
|
|
catch( CFB::CFBException& exception )
|
|
|
|
|
{
|
|
|
|
|
THROW_IO_ERROR( exception.what() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
|
ALTIUM_COMPOUND_FILE::ALTIUM_COMPOUND_FILE( const void* aBuffer, size_t aLen )
|
|
|
|
|
{
|
|
|
|
|
m_buffer.resize( aLen );
|
|
|
|
|
memcpy( m_buffer.data(), aBuffer, aLen );
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
m_reader = std::make_unique<CFB::CompoundFileReader>( m_buffer.data(), m_buffer.size() );
|
|
|
|
|
}
|
|
|
|
|
catch( CFB::CFBException& exception )
|
|
|
|
|
{
|
|
|
|
|
THROW_IO_ERROR( exception.what() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<ALTIUM_COMPOUND_FILE>
|
|
|
|
|
ALTIUM_COMPOUND_FILE::DecodeIntLibStream( const CFB::COMPOUND_FILE_ENTRY& cfe )
|
|
|
|
|
{
|
|
|
|
|
wxCHECK( cfe.size >= 1, nullptr );
|
|
|
|
|
|
|
|
|
|
size_t streamSize = cfe.size;
|
|
|
|
|
wxMemoryBuffer buffer( streamSize );
|
|
|
|
|
buffer.SetDataLen( streamSize );
|
|
|
|
|
|
|
|
|
|
// read file into buffer
|
|
|
|
|
GetCompoundFileReader().ReadFile( &cfe, 0, reinterpret_cast<char*>( buffer.GetData() ),
|
|
|
|
|
streamSize );
|
|
|
|
|
|
|
|
|
|
// 0x02: compressed stream, 0x00: uncompressed
|
|
|
|
|
if( buffer[0] == 0x02 )
|
|
|
|
|
{
|
|
|
|
|
wxMemoryInputStream memoryInputStream( buffer.GetData(), streamSize );
|
|
|
|
|
memoryInputStream.SeekI( 1, wxFromStart );
|
|
|
|
|
|
|
|
|
|
wxZlibInputStream zlibInputStream( memoryInputStream );
|
|
|
|
|
wxMemoryOutputStream decodedPcbLibStream;
|
|
|
|
|
decodedPcbLibStream << zlibInputStream;
|
|
|
|
|
|
|
|
|
|
wxStreamBuffer* outStream = decodedPcbLibStream.GetOutputStreamBuffer();
|
|
|
|
|
|
|
|
|
|
return std::make_unique<ALTIUM_COMPOUND_FILE>( outStream->GetBufferStart(),
|
|
|
|
|
outStream->GetIntPosition() );
|
|
|
|
|
}
|
|
|
|
|
else if( buffer[0] == 0x00 )
|
|
|
|
|
{
|
|
|
|
|
return std::make_unique<ALTIUM_COMPOUND_FILE>(
|
|
|
|
|
reinterpret_cast<uint8_t*>( buffer.GetData() ) + 1, streamSize - 1 );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
wxFAIL_MSG( wxString::Format( "Altium IntLib unknown header: %02x %02x %02x %02x %02x",
|
|
|
|
|
buffer[0], buffer[1], buffer[2], buffer[3], buffer[4] ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-31 16:16:35 +00:00
|
|
|
|
std::map<wxString, wxString> ALTIUM_COMPOUND_FILE::ListLibFootprints()
|
2020-04-03 23:22:24 +00:00
|
|
|
|
{
|
2023-08-31 16:16:35 +00:00
|
|
|
|
if( m_libFootprintDirNameCache.empty() )
|
|
|
|
|
cacheLibFootprintNames();
|
2023-07-22 03:55:49 +00:00
|
|
|
|
|
2023-08-31 16:16:35 +00:00
|
|
|
|
return m_libFootprintDirNameCache;
|
2023-07-22 03:55:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-18 20:42:39 +00:00
|
|
|
|
std::tuple<wxString, const CFB::COMPOUND_FILE_ENTRY*>
|
2023-08-31 16:16:35 +00:00
|
|
|
|
ALTIUM_COMPOUND_FILE::FindLibFootprintDirName( const wxString& aFpUnicodeName )
|
2023-07-22 03:55:49 +00:00
|
|
|
|
{
|
2023-08-31 16:16:35 +00:00
|
|
|
|
if( m_libFootprintNameCache.empty() )
|
|
|
|
|
cacheLibFootprintNames();
|
2023-07-22 03:55:49 +00:00
|
|
|
|
|
2023-08-31 16:16:35 +00:00
|
|
|
|
auto it = m_libFootprintNameCache.find( aFpUnicodeName );
|
2023-07-22 03:55:49 +00:00
|
|
|
|
|
2023-08-31 16:16:35 +00:00
|
|
|
|
if( it == m_libFootprintNameCache.end() )
|
|
|
|
|
return { wxEmptyString, nullptr };
|
2023-07-22 03:55:49 +00:00
|
|
|
|
|
2023-08-31 16:16:35 +00:00
|
|
|
|
return { it->first, it->second };
|
2023-07-22 03:55:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY*
|
|
|
|
|
ALTIUM_COMPOUND_FILE::FindStreamSingleLevel( const CFB::COMPOUND_FILE_ENTRY* aEntry,
|
|
|
|
|
const std::string aName, const bool aIsStream ) const
|
|
|
|
|
{
|
|
|
|
|
if( !m_reader || !aEntry )
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* ret = nullptr;
|
2022-01-05 22:35:42 +00:00
|
|
|
|
|
2023-07-22 03:55:49 +00:00
|
|
|
|
m_reader->EnumFiles( aEntry, 1,
|
|
|
|
|
[&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string& dir,
|
2023-08-18 20:42:39 +00:00
|
|
|
|
int level ) -> int
|
2023-07-22 03:55:49 +00:00
|
|
|
|
{
|
2023-08-18 20:42:39 +00:00
|
|
|
|
if( ret != nullptr )
|
|
|
|
|
return 1;
|
|
|
|
|
|
2023-07-22 03:55:49 +00:00
|
|
|
|
if( m_reader->IsStream( entry ) == aIsStream )
|
|
|
|
|
{
|
|
|
|
|
std::string name = UTF16ToUTF8( entry->name );
|
|
|
|
|
if( name == aName.c_str() )
|
|
|
|
|
{
|
|
|
|
|
ret = entry;
|
2023-08-18 20:42:39 +00:00
|
|
|
|
return 1;
|
2023-07-22 03:55:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-18 20:42:39 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
2023-07-22 03:55:49 +00:00
|
|
|
|
} );
|
2020-04-03 23:22:24 +00:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 15:27:09 +00:00
|
|
|
|
|
2024-06-10 22:01:30 +00:00
|
|
|
|
std::map<wxString, ALTIUM_SYMBOL_DATA>
|
2023-08-28 21:50:56 +00:00
|
|
|
|
ALTIUM_COMPOUND_FILE::GetLibSymbols( const CFB::COMPOUND_FILE_ENTRY* aStart ) const
|
|
|
|
|
{
|
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* root = aStart ? aStart : m_reader->GetRootEntry();
|
|
|
|
|
|
|
|
|
|
if( !root )
|
|
|
|
|
return {};
|
|
|
|
|
|
2024-06-10 22:01:30 +00:00
|
|
|
|
std::map<wxString, ALTIUM_SYMBOL_DATA> folders;
|
2023-08-28 21:50:56 +00:00
|
|
|
|
|
|
|
|
|
m_reader->EnumFiles( root, 1, [&]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string&, int ) -> int
|
|
|
|
|
{
|
|
|
|
|
wxString dirName = UTF16ToWstring( tentry->name, tentry->nameLen );
|
|
|
|
|
|
|
|
|
|
if( m_reader->IsStream( tentry ) )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
m_reader->EnumFiles( tentry, 1,
|
|
|
|
|
[&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&, int ) -> int
|
|
|
|
|
{
|
|
|
|
|
std::wstring fileName = UTF16ToWstring( entry->name, entry->nameLen );
|
|
|
|
|
|
|
|
|
|
if( m_reader->IsStream( entry ) && fileName == L"Data" )
|
2024-06-10 22:01:30 +00:00
|
|
|
|
folders[dirName].m_symbol = entry;
|
|
|
|
|
|
|
|
|
|
if( m_reader->IsStream( entry ) && fileName == L"PinFrac" )
|
|
|
|
|
folders[dirName].m_pinsFrac = entry;
|
|
|
|
|
|
|
|
|
|
if( m_reader->IsStream( entry ) && fileName == L"PinWideText" )
|
|
|
|
|
folders[dirName].m_pinsWideText = entry;
|
|
|
|
|
|
|
|
|
|
if( m_reader->IsStream( entry ) && fileName == L"PinTextData" )
|
|
|
|
|
folders[dirName].m_pinsTextData = entry;
|
2023-08-28 21:50:56 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return folders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-11-17 04:20:15 +00:00
|
|
|
|
std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*>
|
|
|
|
|
ALTIUM_COMPOUND_FILE::EnumDir( const std::wstring& aDir ) const
|
|
|
|
|
{
|
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* root = m_reader->GetRootEntry();
|
|
|
|
|
|
|
|
|
|
if( !root )
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
std::map<wxString, const CFB::COMPOUND_FILE_ENTRY*> files;
|
|
|
|
|
|
|
|
|
|
m_reader->EnumFiles(
|
|
|
|
|
root, 1,
|
|
|
|
|
[&]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string& dir,
|
|
|
|
|
int level ) -> int
|
|
|
|
|
{
|
|
|
|
|
if( m_reader->IsStream( tentry ) )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
std::wstring dirName = UTF16ToWstring( tentry->name, tentry->nameLen );
|
|
|
|
|
|
|
|
|
|
if( dirName != aDir )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
m_reader->EnumFiles(
|
|
|
|
|
tentry, 1,
|
|
|
|
|
[&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&,
|
|
|
|
|
int ) -> int
|
|
|
|
|
{
|
|
|
|
|
if( m_reader->IsStream( entry ) )
|
|
|
|
|
{
|
|
|
|
|
std::wstring fileName =
|
|
|
|
|
UTF16ToWstring( entry->name, entry->nameLen );
|
|
|
|
|
|
|
|
|
|
files[fileName] = entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
} );
|
|
|
|
|
return 0;
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-06 15:27:09 +00:00
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY*
|
2023-08-18 20:42:39 +00:00
|
|
|
|
ALTIUM_COMPOUND_FILE::FindStream( const CFB::COMPOUND_FILE_ENTRY* aStart,
|
|
|
|
|
const std::vector<std::string>& aStreamPath ) const
|
2022-01-05 22:35:42 +00:00
|
|
|
|
{
|
2022-01-06 15:27:09 +00:00
|
|
|
|
if( !m_reader )
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
2023-08-18 20:42:39 +00:00
|
|
|
|
if( !aStart )
|
|
|
|
|
aStart = m_reader->GetRootEntry();
|
2022-01-05 22:35:42 +00:00
|
|
|
|
|
2022-02-19 15:45:55 +00:00
|
|
|
|
auto it = aStreamPath.cbegin();
|
2023-08-18 20:42:39 +00:00
|
|
|
|
|
|
|
|
|
while( aStart != nullptr )
|
2022-01-05 22:35:42 +00:00
|
|
|
|
{
|
2022-02-19 15:45:55 +00:00
|
|
|
|
const std::string& name = *it;
|
|
|
|
|
|
|
|
|
|
if( ++it == aStreamPath.cend() )
|
2022-01-05 22:35:42 +00:00
|
|
|
|
{
|
2023-08-18 20:42:39 +00:00
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* ret = FindStreamSingleLevel( aStart, name, true );
|
|
|
|
|
return ret;
|
2022-02-19 15:45:55 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-18 20:42:39 +00:00
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* ret = FindStreamSingleLevel( aStart, name, false );
|
|
|
|
|
aStart = ret;
|
2022-01-05 22:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 15:45:55 +00:00
|
|
|
|
return nullptr;
|
2022-01-05 22:35:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
2023-08-18 20:42:39 +00:00
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY*
|
|
|
|
|
ALTIUM_COMPOUND_FILE::FindStream( const std::vector<std::string>& aStreamPath ) const
|
|
|
|
|
{
|
|
|
|
|
return FindStream( nullptr, aStreamPath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-31 16:16:35 +00:00
|
|
|
|
void ALTIUM_COMPOUND_FILE::cacheLibFootprintNames()
|
|
|
|
|
{
|
|
|
|
|
m_libFootprintDirNameCache.clear();
|
|
|
|
|
m_libFootprintNameCache.clear();
|
|
|
|
|
|
|
|
|
|
if( !m_reader )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* root = m_reader->GetRootEntry();
|
|
|
|
|
|
|
|
|
|
if( !root )
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_reader->EnumFiles( root, 1,
|
|
|
|
|
[this]( const CFB::COMPOUND_FILE_ENTRY* tentry, const CFB::utf16string& dir,
|
|
|
|
|
int level ) -> int
|
|
|
|
|
{
|
|
|
|
|
if( m_reader->IsStream( tentry ) )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
m_reader->EnumFiles( tentry, 1,
|
|
|
|
|
[&]( const CFB::COMPOUND_FILE_ENTRY* entry, const CFB::utf16string&, int ) -> int
|
|
|
|
|
{
|
|
|
|
|
std::wstring fileName = UTF16ToWstring( entry->name, entry->nameLen );
|
|
|
|
|
|
|
|
|
|
if( m_reader->IsStream( entry ) && fileName == L"Parameters" )
|
|
|
|
|
{
|
2024-02-04 17:14:19 +00:00
|
|
|
|
ALTIUM_BINARY_PARSER parametersReader( *this, entry );
|
2023-08-31 16:16:35 +00:00
|
|
|
|
std::map<wxString, wxString> parameterProperties =
|
|
|
|
|
parametersReader.ReadProperties();
|
|
|
|
|
|
2024-02-04 17:14:19 +00:00
|
|
|
|
wxString key = ALTIUM_PROPS_UTILS::ReadString(
|
2023-08-31 16:16:35 +00:00
|
|
|
|
parameterProperties, wxT( "PATTERN" ), wxT( "" ) );
|
2024-02-04 17:14:19 +00:00
|
|
|
|
wxString fpName = ALTIUM_PROPS_UTILS::ReadUnicodeString(
|
2023-08-31 16:16:35 +00:00
|
|
|
|
parameterProperties, wxT( "PATTERN" ), wxT( "" ) );
|
|
|
|
|
|
|
|
|
|
m_libFootprintDirNameCache[key] = fpName;
|
|
|
|
|
m_libFootprintNameCache[fpName] = tentry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
} );
|
|
|
|
|
return 0;
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-04 17:14:19 +00:00
|
|
|
|
ALTIUM_BINARY_PARSER::ALTIUM_BINARY_PARSER( const ALTIUM_COMPOUND_FILE& aFile,
|
2021-07-01 18:41:46 +00:00
|
|
|
|
const CFB::COMPOUND_FILE_ENTRY* aEntry )
|
2020-04-03 23:22:24 +00:00
|
|
|
|
{
|
|
|
|
|
m_subrecord_end = nullptr;
|
2020-08-23 19:01:08 +00:00
|
|
|
|
m_size = static_cast<size_t>( aEntry->size );
|
|
|
|
|
m_error = false;
|
2020-04-03 23:22:24 +00:00
|
|
|
|
m_content.reset( new char[m_size] );
|
|
|
|
|
m_pos = m_content.get();
|
|
|
|
|
|
|
|
|
|
// read file into buffer
|
2022-01-06 15:27:09 +00:00
|
|
|
|
aFile.GetCompoundFileReader().ReadFile( aEntry, 0, m_content.get(), m_size );
|
2020-04-03 23:22:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-04 17:14:19 +00:00
|
|
|
|
ALTIUM_BINARY_PARSER::ALTIUM_BINARY_PARSER( std::unique_ptr<char[]>& aContent, size_t aSize )
|
2021-03-08 22:02:05 +00:00
|
|
|
|
{
|
|
|
|
|
m_subrecord_end = nullptr;
|
|
|
|
|
m_size = aSize;
|
|
|
|
|
m_error = false;
|
|
|
|
|
m_content = std::move( aContent );
|
|
|
|
|
m_pos = m_content.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-02-04 17:14:19 +00:00
|
|
|
|
std::map<wxString, wxString> ALTIUM_BINARY_PARSER::ReadProperties(
|
2023-08-28 21:50:56 +00:00
|
|
|
|
std::function<std::map<wxString, wxString>( const std::string& )> handleBinaryData )
|
2020-04-03 23:22:24 +00:00
|
|
|
|
{
|
2023-08-28 21:50:56 +00:00
|
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
|
std::map<wxString, wxString> kv;
|
|
|
|
|
|
|
|
|
|
uint32_t length = Read<uint32_t>();
|
2023-08-28 21:50:56 +00:00
|
|
|
|
bool isBinary = ( length & 0xff000000 ) != 0;
|
|
|
|
|
|
|
|
|
|
length &= 0x00ffffff;
|
2021-07-01 18:41:46 +00:00
|
|
|
|
|
2021-02-07 15:51:05 +00:00
|
|
|
|
if( length > GetRemainingBytes() )
|
2020-04-03 23:22:24 +00:00
|
|
|
|
{
|
|
|
|
|
m_error = true;
|
|
|
|
|
return kv;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
|
if( length == 0 )
|
|
|
|
|
{
|
|
|
|
|
return kv;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-07 15:51:05 +00:00
|
|
|
|
// There is one case by kliment where Board6 ends with "|NEARDISTANCE=1000mi".
|
|
|
|
|
// Both the 'l' and the null-byte are missing, which looks like Altium swallowed two bytes.
|
2021-03-08 22:02:05 +00:00
|
|
|
|
bool hasNullByte = m_pos[length - 1] == '\0';
|
2021-07-01 18:41:46 +00:00
|
|
|
|
|
2024-06-10 22:01:30 +00:00
|
|
|
|
if( !hasNullByte && !isBinary )
|
2021-02-07 15:51:05 +00:00
|
|
|
|
{
|
2021-07-10 15:37:19 +00:00
|
|
|
|
wxLogError( _( "Missing null byte at end of property list. Imported data might be "
|
|
|
|
|
"malformed or missing." ) );
|
2021-02-07 15:51:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
|
// we use std::string because std::string can handle NULL-bytes
|
|
|
|
|
// wxString would end the string at the first NULL-byte
|
|
|
|
|
std::string str = std::string( m_pos, length - ( hasNullByte ? 1 : 0 ) );
|
2020-04-03 23:22:24 +00:00
|
|
|
|
m_pos += length;
|
|
|
|
|
|
2023-08-28 21:50:56 +00:00
|
|
|
|
if( isBinary )
|
|
|
|
|
{
|
|
|
|
|
return handleBinaryData( str );
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
|
std::size_t token_end = 0;
|
2021-07-01 18:41:46 +00:00
|
|
|
|
|
2020-04-03 23:22:24 +00:00
|
|
|
|
while( token_end < str.size() && token_end != std::string::npos )
|
|
|
|
|
{
|
|
|
|
|
std::size_t token_start = str.find( '|', token_end );
|
2024-01-14 10:12:47 +00:00
|
|
|
|
std::size_t token_equal = str.find( '=', token_end );
|
|
|
|
|
std::size_t key_start;
|
|
|
|
|
|
|
|
|
|
if( token_start <= token_equal )
|
|
|
|
|
{
|
|
|
|
|
key_start = token_start + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Leading "|" before "RECORD=28" may be missing in older schematic versions.
|
|
|
|
|
key_start = token_end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token_end = str.find( '|', key_start );
|
2021-03-08 22:02:05 +00:00
|
|
|
|
|
|
|
|
|
if( token_equal >= token_end )
|
|
|
|
|
{
|
|
|
|
|
continue; // this looks like an error: skip the entry. Also matches on std::string::npos
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( token_end == std::string::npos )
|
|
|
|
|
{
|
|
|
|
|
token_end = str.size() + 1; // this is the correct offset
|
|
|
|
|
}
|
2020-04-03 23:22:24 +00:00
|
|
|
|
|
2024-01-14 10:12:47 +00:00
|
|
|
|
std::string keyS = str.substr( key_start, token_equal - key_start );
|
2020-04-03 23:22:24 +00:00
|
|
|
|
std::string valueS = str.substr( token_equal + 1, token_end - token_equal - 1 );
|
|
|
|
|
|
2021-03-08 22:02:05 +00:00
|
|
|
|
// convert the strings to wxStrings, since we use them everywhere
|
|
|
|
|
// value can have non-ASCII characters, so we convert them from LATIN1/ISO8859-1
|
2020-04-03 23:22:24 +00:00
|
|
|
|
wxString key( keyS.c_str(), wxConvISO8859_1 );
|
2020-10-18 13:23:07 +00:00
|
|
|
|
// Altium stores keys either in Upper, or in CamelCase. Lets unify it.
|
2021-06-26 11:58:56 +00:00
|
|
|
|
wxString canonicalKey = key.Trim( false ).Trim( true ).MakeUpper();
|
|
|
|
|
// If the key starts with '%UTF8%' we have to parse the value using UTF8
|
|
|
|
|
wxString value;
|
2021-07-01 18:41:46 +00:00
|
|
|
|
|
2021-06-26 11:58:56 +00:00
|
|
|
|
if( canonicalKey.StartsWith( "%UTF8%" ) )
|
|
|
|
|
value = wxString( valueS.c_str(), wxConvUTF8 );
|
|
|
|
|
else
|
|
|
|
|
value = wxString( valueS.c_str(), wxConvISO8859_1 );
|
|
|
|
|
|
2023-07-22 03:55:49 +00:00
|
|
|
|
if( canonicalKey != wxS( "PATTERN" ) && canonicalKey != wxS( "SOURCEFOOTPRINTLIBRARY" ) )
|
|
|
|
|
{
|
|
|
|
|
// Breathless hack because I haven't a clue what the story is here (but this character
|
|
|
|
|
// appears in a lot of radial dimensions and is rendered by Altium as a space).
|
|
|
|
|
value.Replace( wxT( "ÿ" ), wxT( " " ) );
|
|
|
|
|
}
|
2021-07-24 13:23:55 +00:00
|
|
|
|
|
2021-07-08 14:52:09 +00:00
|
|
|
|
if( canonicalKey == wxT( "DESIGNATOR" )
|
|
|
|
|
|| canonicalKey == wxT( "NAME" )
|
|
|
|
|
|| canonicalKey == wxT( "TEXT" ) )
|
|
|
|
|
{
|
2021-07-06 10:33:41 +00:00
|
|
|
|
value = AltiumPropertyToKiCadString( value.Trim() );
|
2021-07-08 14:52:09 +00:00
|
|
|
|
}
|
2021-07-01 18:41:46 +00:00
|
|
|
|
|
2021-06-26 11:58:56 +00:00
|
|
|
|
kv.insert( { canonicalKey, value.Trim() } );
|
2020-04-03 23:22:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return kv;
|
2024-06-10 22:01:30 +00:00
|
|
|
|
}
|