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()];
151 aOut.
Print(
"(embedded_files " );
157 aOut.
Print(
"(file " );
160 const char* type =
nullptr;
168 default: type =
"other";
break;
171 aOut.
Print(
"(type %s)", type );
175 aOut.
Print(
"(data" );
186 aOut.
Print(
"\n%1s%.*s%s\n", first ?
"" :
"|", length, view.data(),
187 remaining == length ?
"|" :
"" );
203 std::vector<char> compressedData;
204 size_t estCompressedSize = ZSTD_compressBound( aFile.
decompressedData.size() );
205 compressedData.resize( estCompressedSize );
206 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
210 if( ZSTD_isError( compressedSize ) )
212 compressedData.clear();
216 const size_t dstLen = wxBase64EncodedSize( compressedSize );
219 compressedData.data(), compressedSize );
220 if( retval != dstLen )
236 std::vector<char> compressedData;
239 if( compressedSize == 0 )
241 wxLogTrace( wxT(
"KICAD_EMBED" ),
242 wxT(
"%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
247 compressedData.resize( compressedSize );
248 void* compressed = compressedData.data();
254 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
256 if( estDecompressedSize > 1e9 )
259 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
260 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
268 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
269 compressed, compressedSize );
271 if( ZSTD_isError( decompressedSize ) )
273 wxLogTrace( wxT(
"KICAD_EMBED" ),
274 wxT(
"%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
275 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
281 std::string test_hash;
282 std::string new_hash;
291 test_hash = new_hash;
295 wxLogTrace( wxT(
"KICAD_EMBED" ),
296 wxT(
"%s:%s:%d\n * Checksum error in embedded file '%s'" ),
297 __FILE__, __FUNCTION__, __LINE__, aFile.
name );
312 CurLineNumber(), CurOffset() );
314 using namespace EMBEDDED_FILES_T;
316 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file(
nullptr );
318 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
320 if( token != T_LEFT )
325 if( token != T_file )
330 if( !file->compressedEncodedData.empty() )
334 if( !file->Validate() )
336 CurLine(), CurLineNumber(), CurOffset() );
339 aFiles->
AddFile( file.release() );
342 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>(
nullptr );
345 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
347 if( token != T_LEFT )
356 NeedSYMBOLorNUMBER();
358 if( !IsSymbol( token ) )
359 Expecting(
"checksum data" );
361 file->data_hash = CurStr();
369 file->compressedEncodedData.reserve( 1 << 17 );
371 while( token != T_BAR )
373 if( !IsSymbol( token ) )
374 Expecting(
"base64 file data" );
376 file->compressedEncodedData += CurStr();
380 file->compressedEncodedData.shrink_to_fit();
389 wxLogTrace( wxT(
"KICAD_EMBED" ),
390 wxT(
"Duplicate 'name' tag in embedded file %s" ), file->name );
393 NeedSYMBOLorNUMBER();
395 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
396 file->name = CurStr();
423 Expecting(
"datasheet, font, model, worksheet or other" );
430 Expecting(
"checksum, data or name" );
438 if( !file->compressedEncodedData.empty() )
442 CurLine(), CurLineNumber(), CurOffset() );
445 aFiles->
AddFile( file.release() );
452 wxFileName cacheFile;
454 auto it =
m_files.find( aName );
460 cacheFile.AppendDir( wxT(
"embed" ) );
464 wxLogTrace( wxT(
"KICAD_EMBED" ),
465 wxT(
"%s:%s:%d\n * failed to create embed cache directory '%s'" ),
466 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
468 cacheFile.SetPath( wxFileName::GetTempDir() );
471 wxFileName inputName( aName );
475 cacheFile.SetName(
"kicad_embedded_" + it->second->data_hash );
476 cacheFile.SetExt( inputName.GetExt() );
478 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
481 wxFFileOutputStream out( cacheFile.GetFullPath() );
489 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
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()
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