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 " );
165 aOut.
Print(
"(file " );
168 const char* type =
nullptr;
176 default: type =
"other";
break;
179 aOut.
Print(
"(type %s)", type );
183 aOut.
Print(
"(data" );
194 aOut.
Print(
"\n%1s%.*s%s\n", first ?
"" :
"|", length, view.data(),
195 remaining == length ?
"|" :
"" );
213 std::vector<char> compressedData;
214 size_t estCompressedSize = ZSTD_compressBound( aFile.
decompressedData.size() );
215 compressedData.resize( estCompressedSize );
216 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
220 if( ZSTD_isError( compressedSize ) )
222 compressedData.clear();
226 const size_t dstLen = wxBase64EncodedSize( compressedSize );
229 compressedData.data(), compressedSize );
231 if( retval != dstLen )
248 std::vector<char> compressedData;
251 if( compressedSize == 0 )
253 wxLogTrace( wxT(
"KICAD_EMBED" ),
254 wxT(
"%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
255 __FILE__, __FUNCTION__, __LINE__, aFile.
name,
260 compressedData.resize( compressedSize );
261 void* compressed = compressedData.data();
267 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
269 if( estDecompressedSize > 1e9 )
272 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
273 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
281 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
282 compressed, compressedSize );
284 if( ZSTD_isError( decompressedSize ) )
286 wxLogTrace( wxT(
"KICAD_EMBED" ),
287 wxT(
"%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
288 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
294 std::string test_hash;
295 std::string new_hash;
304 test_hash = new_hash;
308 wxLogTrace( wxT(
"KICAD_EMBED" ),
309 wxT(
"%s:%s:%d\n * Checksum error in embedded file '%s'" ),
310 __FILE__, __FUNCTION__, __LINE__, aFile.
name );
326 CurLineNumber(), CurOffset() );
328 using namespace EMBEDDED_FILES_T;
330 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file(
nullptr );
332 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
334 if( token != T_LEFT )
339 if( token != T_file )
344 if( !file->compressedEncodedData.empty() )
348 if( !file->Validate() )
350 CurLine(), CurLineNumber(), CurOffset() );
353 aFiles->
AddFile( file.release() );
356 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>(
nullptr );
358 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
360 if( token != T_LEFT )
369 NeedSYMBOLorNUMBER();
371 if( !IsSymbol( token ) )
372 Expecting(
"checksum data" );
374 file->data_hash = CurStr();
382 file->compressedEncodedData.reserve( 1 << 17 );
384 while( token != T_BAR )
386 if( !IsSymbol( token ) )
387 Expecting(
"base64 file data" );
389 file->compressedEncodedData += CurStr();
393 file->compressedEncodedData.shrink_to_fit();
402 wxLogTrace( wxT(
"KICAD_EMBED" ),
403 wxT(
"Duplicate 'name' tag in embedded file %s" ), file->name );
406 NeedSYMBOLorNUMBER();
408 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
409 file->name = CurStr();
436 Expecting(
"datasheet, font, model, worksheet or other" );
443 Expecting(
"checksum, data or name" );
451 if( !file->compressedEncodedData.empty() )
455 CurLine(), CurLineNumber(), CurOffset() );
458 aFiles->
AddFile( file.release() );
465 wxFileName cacheFile;
467 auto it =
m_files.find( aName );
473 cacheFile.AppendDir( wxT(
"embed" ) );
477 wxLogTrace( wxT(
"KICAD_EMBED" ),
478 wxT(
"%s:%s:%d\n * failed to create embed cache directory '%s'" ),
479 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
481 cacheFile.SetPath( wxFileName::GetTempDir() );
484 wxFileName inputName( aName );
488 cacheFile.SetName(
"kicad_embedded_" + it->second->data_hash );
489 cacheFile.SetExt( inputName.GetExt() );
491 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
494 wxFFileOutputStream out( cacheFile.GetFullPath() );
502 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
529 m_files( std::move( other.m_files ) ),
530 m_fontFiles( std::move( other.m_fontFiles ) ),
531 m_fileAddedCallback( std::move( other.m_fileAddedCallback ) ),
532 m_embedFonts( other.m_embedFonts )
534 other.m_embedFonts =
false;
542 ClearEmbeddedFiles();
543 m_files = std::move( other.m_files );
544 m_fontFiles = std::move( other.m_fontFiles );
545 m_fileAddedCallback = std::move( other.m_fileAddedCallback );
546 m_embedFonts = other.m_embedFonts;
547 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