25#include <wx/filename.h>
27#include <wx/mstream.h>
28#include <wx/wfstream.h>
71 if(
HasFile( aName.GetFullName() ) )
74 return m_files[aName.GetFullName()];
76 m_files.erase( aName.GetFullName() );
79 wxFFileInputStream file( aName.GetFullPath() );
80 wxMemoryBuffer buffer;
85 wxFileOffset length = file.GetLength();
87 std::unique_ptr<EMBEDDED_FILE> efile = std::make_unique<EMBEDDED_FILE>();
88 efile->name = aName.GetFullName();
89 efile->decompressedData.resize( length );
91 wxString ext = aName.GetExt().Upper();
94 if( ext ==
"STP" || ext ==
"STPZ" || ext ==
"STEP" || ext ==
"WRL" || ext ==
"WRZ" )
98 else if( ext ==
"WOFF" || ext ==
"WOFF2" || ext ==
"TTF" || ext ==
"OTF" )
102 else if( ext ==
"PDF" )
106 else if( ext ==
"KICAD_WKS" )
111 if( !efile->decompressedData.data() )
114 char* data = efile->decompressedData.data();
115 wxFileOffset total_read = 0;
117 while( !file.Eof() && total_read < length )
119 file.Read( data, length - total_read );
121 size_t read = file.LastRead();
129 efile->is_valid =
true;
137 return m_files[aName.GetFullName()];
186 aOut.
Print(
"(embedded_files " );
198 aOut.
Print(
"(file " );
201 const char* type =
nullptr;
209 default: type =
"other";
break;
212 aOut.
Print(
"(type %s)", type );
216 aOut.
Print(
"(data" );
227 aOut.
Print(
"\n%1s%.*s%s\n", first ?
"" :
"|", length, view.data(),
228 remaining == length ?
"|" :
"" );
246 std::vector<char> compressedData;
247 size_t estCompressedSize = ZSTD_compressBound( aFile.
decompressedData.size() );
248 compressedData.resize( estCompressedSize );
249 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
253 if( ZSTD_isError( compressedSize ) )
255 compressedData.clear();
259 const size_t dstLen = wxBase64EncodedSize( compressedSize );
262 compressedData.data(), compressedSize );
264 if( retval != dstLen )
281 std::vector<char> compressedData;
284 if( compressedSize == 0 )
286 wxLogTrace( wxT(
"KICAD_EMBED" ),
287 wxT(
"%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
288 __FILE__, __FUNCTION__, __LINE__, aFile.
name,
293 compressedData.resize( compressedSize );
294 void* compressed = compressedData.data();
300 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
302 if( estDecompressedSize > 1e9 )
305 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
306 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
314 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
315 compressed, compressedSize );
317 if( ZSTD_isError( decompressedSize ) )
319 wxLogTrace( wxT(
"KICAD_EMBED" ),
320 wxT(
"%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
321 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
327 std::string test_hash;
328 std::string new_hash;
337 test_hash = new_hash;
341 wxLogTrace( wxT(
"KICAD_EMBED" ),
342 wxT(
"%s:%s:%d\n * Checksum error in embedded file '%s'" ),
343 __FILE__, __FUNCTION__, __LINE__, aFile.
name );
357 wxFFileInputStream file( aFileName.GetFullPath() );
362 wxFileOffset length = file.GetLength();
363 std::vector<char> data( length );
368 char* dataPtr = data.data();
369 wxFileOffset totalRead = 0;
371 while( !file.Eof() && totalRead < length )
373 file.Read( dataPtr, length - totalRead );
374 size_t bytesRead = file.LastRead();
375 dataPtr += bytesRead;
376 totalRead += bytesRead;
395 CurLineNumber(), CurOffset() );
397 using namespace EMBEDDED_FILES_T;
399 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file(
nullptr );
401 for(
T token = NextTok(); token != T_RIGHT; token = NextTok() )
403 if( token != T_LEFT )
408 if( token != T_file )
413 if( !file->compressedEncodedData.empty() )
417 if( !file->Validate() )
419 CurLine(), CurLineNumber(), CurOffset() );
422 aFiles->
AddFile( file.release() );
425 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>(
nullptr );
427 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
429 if( token != T_LEFT )
440 NeedSYMBOLorNUMBER();
442 if( !IsSymbol( token ) )
443 Expecting(
"checksum data" );
445 file->data_hash = CurStr();
460 if( curTok == T_RIGHT )
472 file->compressedEncodedData.reserve( 1 << 17 );
474 while( token != T_BAR )
476 if( !IsSymbol( token ) )
477 Expecting(
"base64 file data" );
479 file->compressedEncodedData += CurStr();
483 file->compressedEncodedData.shrink_to_fit();
491 wxLogTrace( wxT(
"KICAD_EMBED" ),
492 wxT(
"Duplicate 'name' tag in embedded file %s" ), file->name );
495 NeedSYMBOLorNUMBER();
497 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
498 file->name = CurStr();
516 default: Expecting(
"datasheet, font, model, worksheet or other" );
break;
523 Expecting(
"checksum, data or name" );
531 if( !file->compressedEncodedData.empty() )
535 CurLine(), CurLineNumber(), CurOffset() );
538 aFiles->
AddFile( file.release() );
545 wxFileName cacheFile;
547 auto it =
m_files.find( aName );
553 cacheFile.AppendDir( wxT(
"embed" ) );
557 wxLogTrace( wxT(
"KICAD_EMBED" ),
558 wxT(
"%s:%s:%d\n * failed to create embed cache directory '%s'" ),
559 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
561 cacheFile.SetPath( wxFileName::GetTempDir() );
564 wxFileName inputName( aName );
568 cacheFile.SetName(
"kicad_embedded_" + it->second->data_hash );
569 cacheFile.SetExt( inputName.GetExt() );
571 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
574 wxFFileOutputStream out( cacheFile.GetFullPath() );
582 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
610 m_files( std::move( other.m_files ) ),
615 other.m_embedFonts =
false;
625 m_files = std::move( other.m_files );
629 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.
@ OUT_OF_MEMORY
Could not allocate memory.
@ FILE_NOT_FOUND
File not found on disk.
@ 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.
FILE_ADDED_CALLBACK m_fileAddedCallback
static RETURN_CODE DecompressAndDecode(EMBEDDED_FILE &aFile)
Takes data from the #compressedEncodedData buffer and Base64 decodes it.
static RETURN_CODE ComputeFileHash(const wxFileName &aFileName, std::string &aHash)
Compute the hash of a file on disk without fully embedding 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.
static const std::string KiCadUriPrefix
#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,...
wxString result
Test unit parsing edge cases and error handling.
Definition of file extensions used in Kicad.