altium: verify that file contains "Compound File Binary Format" magic bytes, as we do not support the ASCII format

This commit is contained in:
Thomas Pointhuber 2023-08-18 14:46:59 +02:00
parent 85240d590b
commit c2a91caacf
19 changed files with 1226 additions and 1 deletions

View File

@ -679,6 +679,7 @@ public:
protected:
static bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix,
bool aIgnoreWhitespace );
static bool fileStartsWithBinaryHeader( const wxString& aFilePath, const std::vector<uint8_t>& aHeader );
};
#endif // IO_MGR_H_

View File

@ -318,3 +318,22 @@ bool PLUGIN::fileStartsWithPrefix( const wxString& aFilePath, const wxString& aP
return false;
}
bool PLUGIN::fileStartsWithBinaryHeader( const wxString& aFilePath, const std::vector<uint8_t>& aHeader )
{
wxFileInputStream input( aFilePath );
if( input.IsOk() && !input.Eof() )
{
if (input.GetLength() < aHeader.size())
return false;
std::vector<uint8_t> parsedHeader(aHeader.size());
if (!input.ReadAll(parsedHeader.data(), parsedHeader.size()))
return false;
return parsedHeader == aHeader;
}
return false;
}

View File

@ -30,6 +30,7 @@
#include <wx/string.h>
#include <altium_circuit_maker_plugin.h>
#include <altium_designer_plugin.h>
#include <altium_pcb.h>
#include "plugins/altium/altium_parser.h"
@ -62,6 +63,15 @@ PLUGIN_FILE_DESC ALTIUM_CIRCUIT_MAKER_PLUGIN::GetBoardFileDesc() const
}
bool ALTIUM_CIRCUIT_MAKER_PLUGIN::CanReadBoard( const wxString& aFileName ) const
{
if( !PLUGIN::CanReadBoard( aFileName ) )
return false;
return ALTIUM_DESIGNER_PLUGIN::checkFileHeader( aFileName );
}
BOARD* ALTIUM_CIRCUIT_MAKER_PLUGIN::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties,
PROJECT* aProject,

View File

@ -35,6 +35,8 @@ public:
PLUGIN_FILE_DESC GetBoardFileDesc() const override;
bool CanReadBoard( const wxString& aFileName ) const override;
BOARD* LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties, PROJECT* aProject = nullptr,
PROGRESS_REPORTER* aProgressReporter = nullptr ) override;

View File

@ -30,6 +30,7 @@
#include <wx/string.h>
#include <altium_circuit_studio_plugin.h>
#include <altium_designer_plugin.h>
#include <altium_pcb.h>
#include "plugins/altium/altium_parser.h"
@ -62,6 +63,15 @@ PLUGIN_FILE_DESC ALTIUM_CIRCUIT_STUDIO_PLUGIN::GetBoardFileDesc() const
}
bool ALTIUM_CIRCUIT_STUDIO_PLUGIN::CanReadBoard( const wxString& aFileName ) const
{
if( !PLUGIN::CanReadBoard( aFileName ) )
return false;
return ALTIUM_DESIGNER_PLUGIN::checkFileHeader( aFileName );
}
BOARD* ALTIUM_CIRCUIT_STUDIO_PLUGIN::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties,
PROJECT* aProject,

View File

@ -35,6 +35,8 @@ public:
PLUGIN_FILE_DESC GetBoardFileDesc() const override;
bool CanReadBoard( const wxString& aFileName ) const override;
BOARD* LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
PROGRESS_REPORTER* aProgressReporter = nullptr ) override;

View File

@ -50,6 +50,31 @@ ALTIUM_DESIGNER_PLUGIN::~ALTIUM_DESIGNER_PLUGIN()
}
bool ALTIUM_DESIGNER_PLUGIN::checkFileHeader( const wxString& aFileName )
{
// Compound File Binary Format header
return fileStartsWithBinaryHeader( aFileName, { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1} );
}
bool ALTIUM_DESIGNER_PLUGIN::CanReadBoard( const wxString& aFileName ) const
{
if( !PLUGIN::CanReadBoard( aFileName ) )
return false;
return checkFileHeader( aFileName );
}
bool ALTIUM_DESIGNER_PLUGIN::CanReadFootprintLib( const wxString& aFileName ) const
{
if( !PLUGIN::CanReadFootprintLib( aFileName ) )
return false;
return checkFileHeader( aFileName );
}
BOARD* ALTIUM_DESIGNER_PLUGIN::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
PROGRESS_REPORTER* aProgressReporter )

View File

@ -45,6 +45,9 @@ public:
return PLUGIN_FILE_DESC( _HKI( "Altium PCB footprint library files" ), { "PcbLib" } );
}
bool CanReadBoard( const wxString& aFileName ) const override;
bool CanReadFootprintLib( const wxString& aFileName ) const override;
BOARD* LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties, PROJECT* aProject = nullptr,
PROGRESS_REPORTER* aProgressReporter = nullptr ) override;
@ -67,6 +70,8 @@ public:
ALTIUM_DESIGNER_PLUGIN();
~ALTIUM_DESIGNER_PLUGIN();
static bool checkFileHeader( const wxString& aFileName );
private:
const STRING_UTF8_MAP* m_props;
BOARD* m_board;

View File

@ -20,6 +20,7 @@
#include <wx/string.h>
#include <solidworks_pcb_plugin.h>
#include <altium_designer_plugin.h>
#include <altium_pcb.h>
#include "plugins/altium/altium_parser.h"
@ -52,6 +53,15 @@ PLUGIN_FILE_DESC SOLIDWORKS_PCB_PLUGIN::GetBoardFileDesc() const
}
bool SOLIDWORKS_PCB_PLUGIN::CanReadBoard( const wxString& aFileName ) const
{
if( !PLUGIN::CanReadBoard( aFileName ) )
return false;
return ALTIUM_DESIGNER_PLUGIN::checkFileHeader( aFileName );
}
BOARD* SOLIDWORKS_PCB_PLUGIN::LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties, PROJECT* aProject,
PROGRESS_REPORTER* aProgressReporter )

View File

@ -30,6 +30,8 @@ public:
PLUGIN_FILE_DESC GetBoardFileDesc() const override;
bool CanReadBoard( const wxString& aFileName ) const override;
BOARD* LoadBoard( const wxString& aFileName, BOARD* aAppendToMe,
const STRING_UTF8_MAP* aProperties, PROJECT* aProject = nullptr,
PROGRESS_REPORTER* aProgressReporter = nullptr ) override;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
A fake Board file for testing

View File

@ -0,0 +1 @@
A fake Board file for testing

View File

@ -0,0 +1 @@
A fake Board file for testing

View File

@ -0,0 +1 @@
A fake Board file for testing

View File

@ -63,6 +63,27 @@ static const std::vector<BOARD_PLUGIN_CASE> BoardPluginCases = {
"plugins/fakeboard.cpa",
IO_MGR::FILE_TYPE_NONE
},
{
"Fake Board file (Altium Circuit Studio file ext)",
"plugins/fakeboard.CSPcbDoc",
IO_MGR::FILE_TYPE_NONE
},
{
"Fake Board file (Altium Circuit Maker file ext)",
"plugins/fakeboard.CMPcbDoc",
IO_MGR::FILE_TYPE_NONE
},
{
"Fake Board file (Altium Designer file ext)",
"plugins/fakeboard.PcbDoc",
IO_MGR::FILE_TYPE_NONE
},
{
"Fake Board file (Solid Works PCB file ext)",
"plugins/fakeboard.SWPcbDoc",
IO_MGR::FILE_TYPE_NONE
},
//
// REAL Boards:
@ -92,8 +113,13 @@ static const std::vector<BOARD_PLUGIN_CASE> BoardPluginCases = {
"Basic CADSTAR board file",
"plugins/cadstar/route_offset/minimal_route_offset_curved_track.cpa",
IO_MGR::CADSTAR_PCB_ARCHIVE
},
{
"Basic Altium Designer board file",
"plugins/altium/eDP_adapter_dvt1_source/eDP_adapter_dvt1.PcbDoc",
IO_MGR::ALTIUM_DESIGNER
}
// Todo: Add Altium (+derivatives) and Fabmaster tests
// Todo: Add Altium derivatives and Fabmaster tests
};