25#include <wx/filename.h>
27#include <wx/mstream.h>
28#include <wx/wfstream.h>
44 if(
HasFile( aName.GetFullName() ) )
47 return m_files[aName.GetFullName()];
49 m_files.erase( aName.GetFullName() );
52 wxFFileInputStream file( aName.GetFullPath() );
53 wxMemoryBuffer buffer;
58 wxFileOffset length = file.GetLength();
60 std::unique_ptr<EMBEDDED_FILE> efile = std::make_unique<EMBEDDED_FILE>();
61 efile->name = aName.GetFullName();
62 efile->decompressedData.resize( length );
64 wxString ext = aName.GetExt().Upper();
67 if( ext ==
"STP" || ext ==
"STPZ" || ext ==
"STEP" || ext ==
"WRL" || ext ==
"WRZ" )
71 else if( ext ==
"WOFF" || ext ==
"WOFF2" || ext ==
"TTF" || ext ==
"OTF" )
75 else if( ext ==
"PDF" )
79 else if( ext ==
"KICAD_WKS" )
84 if( !efile->decompressedData.data() )
87 char* data = efile->decompressedData.data();
88 wxFileOffset total_read = 0;
90 while( !file.Eof() && total_read < length )
92 file.Read( data, length - total_read );
94 size_t read = file.LastRead();
102 efile->is_valid =
true;
105 m_files[aName.GetFullName()] = result;
110 return m_files[aName.GetFullName()];
159 aOut.
Print(
"(embedded_files " );
168 wxLogDebug( wxT(
"Error: Embedded file '%s' is empty" ), file.
name );
172 aOut.
Print(
"(file " );
175 const char* type =
nullptr;
183 default: type =
"other";
break;
186 aOut.
Print(
"(type %s)", type );
190 aOut.
Print(
"(data" );
201 aOut.
Print(
"\n%1s%.*s%s\n", first ?
"" :
"|", length, view.data(),
202 remaining == length ?
"|" :
"" );
220 std::vector<char> compressedData;
221 size_t estCompressedSize = ZSTD_compressBound( aFile.
decompressedData.size() );
222 compressedData.resize( estCompressedSize );
223 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
227 if( ZSTD_isError( compressedSize ) )
229 compressedData.clear();
233 const size_t dstLen = wxBase64EncodedSize( compressedSize );
236 compressedData.data(), compressedSize );
238 if( retval != dstLen )
255 std::vector<char> compressedData;
258 if( compressedSize == 0 )
260 wxLogTrace( wxT(
"KICAD_EMBED" ),
261 wxT(
"%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
262 __FILE__, __FUNCTION__, __LINE__, aFile.
name,
267 compressedData.resize( compressedSize );
268 void* compressed = compressedData.data();
274 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
276 if( estDecompressedSize > 1e9 )
279 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
280 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
288 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
289 compressed, compressedSize );
291 if( ZSTD_isError( decompressedSize ) )
293 wxLogTrace( wxT(
"KICAD_EMBED" ),
294 wxT(
"%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
295 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
301 std::string test_hash;
302 std::string new_hash;
311 test_hash = new_hash;
315 wxLogTrace( wxT(
"KICAD_EMBED" ),
316 wxT(
"%s:%s:%d\n * Checksum error in embedded file '%s'" ),
317 __FILE__, __FUNCTION__, __LINE__, aFile.
name );
333 CurLineNumber(), CurOffset() );
335 using namespace EMBEDDED_FILES_T;
337 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file(
nullptr );
339 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
341 if( token != T_LEFT )
346 if( token != T_file )
351 if( !file->compressedEncodedData.empty() )
355 if( !file->Validate() )
357 CurLine(), CurLineNumber(), CurOffset() );
360 aFiles->
AddFile( file.release() );
363 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>(
nullptr );
365 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
367 if( token != T_LEFT )
376 NeedSYMBOLorNUMBER();
378 if( !IsSymbol( token ) )
379 Expecting(
"checksum data" );
381 file->data_hash = CurStr();
393 if( curTok == T_RIGHT )
405 file->compressedEncodedData.reserve( 1 << 17 );
407 while( token != T_BAR )
409 if( !IsSymbol( token ) )
410 Expecting(
"base64 file data" );
412 file->compressedEncodedData += CurStr();
416 file->compressedEncodedData.shrink_to_fit();
425 wxLogTrace( wxT(
"KICAD_EMBED" ),
426 wxT(
"Duplicate 'name' tag in embedded file %s" ), file->name );
429 NeedSYMBOLorNUMBER();
431 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
432 file->name = CurStr();
459 Expecting(
"datasheet, font, model, worksheet or other" );
466 Expecting(
"checksum, data or name" );
474 if( !file->compressedEncodedData.empty() )
478 CurLine(), CurLineNumber(), CurOffset() );
481 aFiles->
AddFile( file.release() );
488 wxFileName cacheFile;
490 auto it =
m_files.find( aName );
496 cacheFile.AppendDir( wxT(
"embed" ) );
500 wxLogTrace( wxT(
"KICAD_EMBED" ),
501 wxT(
"%s:%s:%d\n * failed to create embed cache directory '%s'" ),
502 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
504 cacheFile.SetPath( wxFileName::GetTempDir() );
507 wxFileName inputName( aName );
511 cacheFile.SetName(
"kicad_embedded_" + it->second->data_hash );
512 cacheFile.SetExt( inputName.GetExt() );
514 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
517 wxFFileOutputStream out( cacheFile.GetFullPath() );
525 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
552 m_files( std::move( other.m_files ) ),
553 m_fontFiles( std::move( other.m_fontFiles ) ),
554 m_fileAddedCallback( std::move( other.m_fileAddedCallback ) ),
555 m_embedFonts( other.m_embedFonts )
557 other.m_embedFonts =
false;
565 ClearEmbeddedFiles();
566 m_files = std::move( other.m_files );
567 m_fontFiles = std::move( other.m_fontFiles );
568 m_fileAddedCallback = std::move( other.m_fileAddedCallback );
569 m_embedFonts = other.m_embedFonts;
570 other.m_embedFonts =
false;
void ParseEmbedded(EMBEDDED_FILES *aFiles)
std::vector< wxString > m_fontFiles
void RemoveFile(const wxString &name, bool aErase=true)
Remove a file from the collection and frees the memory.
FileAddedCallback m_fileAddedCallback
@ OUT_OF_MEMORY
Could not allocate memory.
@ CHECKSUM_ERROR
Checksum in file does not match data.
wxFileName GetTemporaryFileName(const wxString &aName) const
void WriteEmbeddedFiles(OUTPUTFORMATTER &aOut, bool aWriteData) const
Output formatter for the embedded files.
const std::vector< wxString > * UpdateFontFiles()
Helper function to get a list of fonts for fontconfig to add to the library.
static RETURN_CODE DecompressAndDecode(EMBEDDED_FILE &aFile)
Takes data from the #compressedEncodedData buffer and Base64 decodes it.
bool HasFile(const wxString &name) const
void ClearEmbeddedFiles(bool aDeleteFiles=true)
void ClearEmbeddedFonts()
Remove all embedded fonts from the collection.
EMBEDDED_FILES & operator=(EMBEDDED_FILES &&other) noexcept
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Load a file from disk and adds it to the collection.
const std::vector< wxString > * GetFontFiles() const
If we just need the cached version of the font files, we can use this function which is const and wil...
static RETURN_CODE CompressAndEncode(EMBEDDED_FILE &aFile)
Take data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncodedD...
std::map< wxString, EMBEDDED_FILE * > m_files
bool m_embedFonts
If set, fonts will be embedded in the element on save.
A streaming C++ equivalent for MurmurHash3_x64_128.
FORCE_INLINE void add(const std::string &input)
FORCE_INLINE HASH_128 digest()
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
#define MIME_BASE64_LENGTH
std::vector< char > decompressedData
std::string compressedEncodedData
std::string ToString() const
A filename or source description, a problem input line, a line number, a byte offset,...