26#include <wx/filename.h>
28#include <wx/mstream.h>
29#include <wx/wfstream.h>
45 if(
HasFile( aName.GetFullName() ) )
48 return m_files[aName.GetFullName()];
50 m_files.erase( aName.GetFullName() );
53 wxFFileInputStream file( aName.GetFullPath() );
54 wxMemoryBuffer buffer;
59 wxFileOffset length = file.GetLength();
61 std::unique_ptr<EMBEDDED_FILE> efile = std::make_unique<EMBEDDED_FILE>();
62 efile->name = aName.GetFullName();
63 efile->decompressedData.resize( length );
65 wxString ext = aName.GetExt().Upper();
68 if( ext ==
"STP" || ext ==
"STPZ" || ext ==
"STEP" || ext ==
"WRL" || ext ==
"WRZ" )
72 else if( ext ==
"WOFF" || ext ==
"WOFF2" || ext ==
"TTF" || ext ==
"OTF" )
76 else if( ext ==
"PDF" )
80 else if( ext ==
"KICAD_WKS" )
85 if( !efile->decompressedData.data() )
88 char* data = efile->decompressedData.data();
89 wxFileOffset total_read = 0;
91 while( !file.Eof() && total_read < length )
93 file.Read( data, length - total_read );
95 size_t read = file.LastRead();
103 efile->is_valid =
true;
105 m_files[aName.GetFullName()] = efile.release();
107 return m_files[aName.GetFullName()];
150 bool aWriteData )
const
153 aOut.
Print( aNestLevel,
"(embedded_files\n" );
159 aOut.
Print( aNestLevel + 1,
"(file\n" );
160 aOut.
Print( aNestLevel + 2,
"(name \"%s\")\n", file.
name.c_str().AsChar() );
162 const char* type =
nullptr;
183 aOut.
Print( aNestLevel + 2,
"(type %s)\n", type );
187 aOut.
Print( 2,
"(data\n" );
198 aOut.
Print( aNestLevel + 3,
"%1s%.*s%s\n", first ?
"" :
"|", length, view.data(),
199 remaining == length ?
"|" :
"" );
202 aOut.
Print( aNestLevel + 2,
")\n" );
205 aOut.
Print( aNestLevel + 2,
"(checksum \"%s\")\n", file.
data_hash.c_str() );
206 aOut.
Print( aNestLevel + 1,
")\n" );
209 aOut.
Print( aNestLevel,
")\n" );
215 std::vector<char> compressedData;
216 size_t estCompressedSize = ZSTD_compressBound( aFile.
decompressedData.size() );
217 compressedData.resize( estCompressedSize );
218 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
222 if( ZSTD_isError( compressedSize ) )
224 compressedData.clear();
228 const size_t dstLen = wxBase64EncodedSize( compressedSize );
231 compressedData.data(), compressedSize );
232 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" ),
259 compressedData.resize( compressedSize );
260 void* compressed = compressedData.data();
266 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
268 if( estDecompressedSize > 1e9 )
271 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
272 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
280 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
281 compressed, compressedSize );
283 if( ZSTD_isError( decompressedSize ) )
285 wxLogTrace( wxT(
"KICAD_EMBED" ),
286 wxT(
"%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
287 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
293 std::string test_hash;
294 std::string new_hash;
303 test_hash = new_hash;
307 wxLogTrace( wxT(
"KICAD_EMBED" ),
308 wxT(
"%s:%s:%d\n * Checksum error in embedded file '%s'" ),
309 __FILE__, __FUNCTION__, __LINE__, aFile.
name );
324 CurLineNumber(), CurOffset() );
326 using namespace EMBEDDED_FILES_T;
328 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file(
nullptr );
330 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
332 if( token != T_LEFT )
337 if( token != T_file )
342 if( !file->compressedEncodedData.empty() )
346 if( !file->Validate() )
348 CurLine(), CurLineNumber(), CurOffset() );
351 aFiles->
AddFile( file.release() );
354 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>(
nullptr );
357 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
359 if( token != T_LEFT )
368 NeedSYMBOLorNUMBER();
370 if( !IsSymbol( token ) )
371 Expecting(
"checksum data" );
373 file->data_hash = CurStr();
381 file->compressedEncodedData.reserve( 1 << 17 );
383 while( token != T_BAR )
385 if( !IsSymbol( token ) )
386 Expecting(
"base64 file data" );
388 file->compressedEncodedData += CurStr();
392 file->compressedEncodedData.shrink_to_fit();
401 wxLogTrace( wxT(
"KICAD_EMBED" ),
402 wxT(
"Duplicate 'name' tag in embedded file %s" ), file->name );
405 NeedSYMBOLorNUMBER();
407 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
408 file->name = CurStr();
435 Expecting(
"datasheet, font, model, worksheet or other" );
442 Expecting(
"checksum, data or name" );
450 if( !file->compressedEncodedData.empty() )
454 CurLine(), CurLineNumber(), CurOffset() );
457 aFiles->
AddFile( file.release() );
464 wxFileName cacheFile;
466 auto it =
m_files.find( aName );
472 cacheFile.AppendDir( wxT(
"embed" ) );
476 wxLogTrace( wxT(
"KICAD_EMBED" ),
477 wxT(
"%s:%s:%d\n * failed to create embed cache directory '%s'" ),
478 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
480 cacheFile.SetPath( wxFileName::GetTempDir() );
483 wxFileName inputName( aName );
487 cacheFile.SetName(
"kicad_embedded_" + it->second->data_hash );
488 cacheFile.SetExt( inputName.GetExt() );
490 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
493 wxFFileOutputStream out( cacheFile.GetFullPath() );
501 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)
Removes a file from the collection and frees the memory.
wxFileName GetTemporaryFileName(const wxString &aName) const
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 WriteEmbeddedFiles(OUTPUTFORMATTER &aOut, int aNestLevel, bool aWriteData) const
Output formatter for the embedded files.
void ClearEmbeddedFonts()
Removes all embedded fonts from the collection.
EMBEDDED_FILE * AddFile(const wxFileName &aName, bool aOverwrite)
Loads 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)
Takes data from the #decompressedData buffer and compresses it using ZSTD into the #compressedEncoded...
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)
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