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;
104 m_files[aName.GetFullName()] = efile.release();
106 return m_files[aName.GetFullName()];
152 aOut.
Print(
"(embedded_files " );
158 aOut.
Print(
"(file " );
161 const char* type =
nullptr;
169 default: type =
"other";
break;
172 aOut.
Print(
"(type %s)", type );
176 aOut.
Print(
"(data" );
187 aOut.
Print(
"\n%1s%.*s%s\n", first ?
"" :
"|", length, view.data(),
188 remaining == length ?
"|" :
"" );
206 std::vector<char> compressedData;
207 size_t estCompressedSize = ZSTD_compressBound( aFile.
decompressedData.size() );
208 compressedData.resize( estCompressedSize );
209 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
213 if( ZSTD_isError( compressedSize ) )
215 compressedData.clear();
219 const size_t dstLen = wxBase64EncodedSize( compressedSize );
222 compressedData.data(), compressedSize );
224 if( retval != dstLen )
241 std::vector<char> compressedData;
244 if( compressedSize == 0 )
246 wxLogTrace( wxT(
"KICAD_EMBED" ),
247 wxT(
"%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
248 __FILE__, __FUNCTION__, __LINE__, aFile.
name,
253 compressedData.resize( compressedSize );
254 void* compressed = compressedData.data();
260 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
262 if( estDecompressedSize > 1e9 )
265 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
266 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
274 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
275 compressed, compressedSize );
277 if( ZSTD_isError( decompressedSize ) )
279 wxLogTrace( wxT(
"KICAD_EMBED" ),
280 wxT(
"%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
281 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
287 std::string test_hash;
288 std::string new_hash;
297 test_hash = new_hash;
301 wxLogTrace( wxT(
"KICAD_EMBED" ),
302 wxT(
"%s:%s:%d\n * Checksum error in embedded file '%s'" ),
303 __FILE__, __FUNCTION__, __LINE__, aFile.
name );
319 CurLineNumber(), CurOffset() );
321 using namespace EMBEDDED_FILES_T;
323 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file(
nullptr );
325 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
327 if( token != T_LEFT )
332 if( token != T_file )
337 if( !file->compressedEncodedData.empty() )
341 if( !file->Validate() )
343 CurLine(), CurLineNumber(), CurOffset() );
346 aFiles->
AddFile( file.release() );
349 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>(
nullptr );
351 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
353 if( token != T_LEFT )
362 NeedSYMBOLorNUMBER();
364 if( !IsSymbol( token ) )
365 Expecting(
"checksum data" );
367 file->data_hash = CurStr();
375 file->compressedEncodedData.reserve( 1 << 17 );
377 while( token != T_BAR )
379 if( !IsSymbol( token ) )
380 Expecting(
"base64 file data" );
382 file->compressedEncodedData += CurStr();
386 file->compressedEncodedData.shrink_to_fit();
395 wxLogTrace( wxT(
"KICAD_EMBED" ),
396 wxT(
"Duplicate 'name' tag in embedded file %s" ), file->name );
399 NeedSYMBOLorNUMBER();
401 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
402 file->name = CurStr();
429 Expecting(
"datasheet, font, model, worksheet or other" );
436 Expecting(
"checksum, data or name" );
444 if( !file->compressedEncodedData.empty() )
448 CurLine(), CurLineNumber(), CurOffset() );
451 aFiles->
AddFile( file.release() );
458 wxFileName cacheFile;
460 auto it =
m_files.find( aName );
466 cacheFile.AppendDir( wxT(
"embed" ) );
470 wxLogTrace( wxT(
"KICAD_EMBED" ),
471 wxT(
"%s:%s:%d\n * failed to create embed cache directory '%s'" ),
472 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
474 cacheFile.SetPath( wxFileName::GetTempDir() );
477 wxFileName inputName( aName );
481 cacheFile.SetName(
"kicad_embedded_" + it->second->data_hash );
482 cacheFile.SetExt( inputName.GetExt() );
484 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
487 wxFFileOutputStream out( cacheFile.GetFullPath() );
495 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
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.
@ 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 ClearEmbeddedFonts()
Remove all embedded fonts from the collection.
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
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 MIME_BASE64_LENGTH
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
std::vector< char > decompressedData
std::string compressedEncodedData
std::string ToString() const