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;
110 return m_files[aName.GetFullName()];
159 aOut.
Print(
"(embedded_files " );
171 aOut.
Print(
"(file " );
174 const char* type =
nullptr;
182 default: type =
"other";
break;
185 aOut.
Print(
"(type %s)", type );
189 aOut.
Print(
"(data" );
200 aOut.
Print(
"\n%1s%.*s%s\n", first ?
"" :
"|", length, view.data(),
201 remaining == length ?
"|" :
"" );
219 std::vector<char> compressedData;
220 size_t estCompressedSize = ZSTD_compressBound( aFile.
decompressedData.size() );
221 compressedData.resize( estCompressedSize );
222 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
226 if( ZSTD_isError( compressedSize ) )
228 compressedData.clear();
232 const size_t dstLen = wxBase64EncodedSize( compressedSize );
235 compressedData.data(), compressedSize );
237 if( retval != dstLen )
254 std::vector<char> compressedData;
257 if( compressedSize == 0 )
259 wxLogTrace( wxT(
"KICAD_EMBED" ),
260 wxT(
"%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
261 __FILE__, __FUNCTION__, __LINE__, aFile.
name,
266 compressedData.resize( compressedSize );
267 void* compressed = compressedData.data();
273 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
275 if( estDecompressedSize > 1e9 )
278 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
279 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
287 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
288 compressed, compressedSize );
290 if( ZSTD_isError( decompressedSize ) )
292 wxLogTrace( wxT(
"KICAD_EMBED" ),
293 wxT(
"%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
294 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
300 std::string test_hash;
301 std::string new_hash;
310 test_hash = new_hash;
314 wxLogTrace( wxT(
"KICAD_EMBED" ),
315 wxT(
"%s:%s:%d\n * Checksum error in embedded file '%s'" ),
316 __FILE__, __FUNCTION__, __LINE__, aFile.
name );
330 wxFFileInputStream file( aFileName.GetFullPath() );
335 wxFileOffset length = file.GetLength();
336 std::vector<char> data( length );
341 char* dataPtr = data.data();
342 wxFileOffset totalRead = 0;
344 while( !file.Eof() && totalRead < length )
346 file.Read( dataPtr, length - totalRead );
347 size_t bytesRead = file.LastRead();
348 dataPtr += bytesRead;
349 totalRead += bytesRead;
368 CurLineNumber(), CurOffset() );
370 using namespace EMBEDDED_FILES_T;
372 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file(
nullptr );
374 for(
T token = NextTok(); token != T_RIGHT; token = NextTok() )
376 if( token != T_LEFT )
381 if( token != T_file )
386 if( !file->compressedEncodedData.empty() )
390 if( !file->Validate() )
392 CurLine(), CurLineNumber(), CurOffset() );
395 aFiles->
AddFile( file.release() );
398 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>(
nullptr );
400 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
402 if( token != T_LEFT )
413 NeedSYMBOLorNUMBER();
415 if( !IsSymbol( token ) )
416 Expecting(
"checksum data" );
418 file->data_hash = CurStr();
433 if( curTok == T_RIGHT )
445 file->compressedEncodedData.reserve( 1 << 17 );
447 while( token != T_BAR )
449 if( !IsSymbol( token ) )
450 Expecting(
"base64 file data" );
452 file->compressedEncodedData += CurStr();
456 file->compressedEncodedData.shrink_to_fit();
464 wxLogTrace( wxT(
"KICAD_EMBED" ),
465 wxT(
"Duplicate 'name' tag in embedded file %s" ), file->name );
468 NeedSYMBOLorNUMBER();
470 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
471 file->name = CurStr();
489 default: Expecting(
"datasheet, font, model, worksheet or other" );
break;
496 Expecting(
"checksum, data or name" );
504 if( !file->compressedEncodedData.empty() )
508 CurLine(), CurLineNumber(), CurOffset() );
511 aFiles->
AddFile( file.release() );
518 wxFileName cacheFile;
520 auto it =
m_files.find( aName );
526 cacheFile.AppendDir( wxT(
"embed" ) );
530 wxLogTrace( wxT(
"KICAD_EMBED" ),
531 wxT(
"%s:%s:%d\n * failed to create embed cache directory '%s'" ),
532 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
534 cacheFile.SetPath( wxFileName::GetTempDir() );
537 wxFileName inputName( aName );
541 cacheFile.SetName(
"kicad_embedded_" + it->second->data_hash );
542 cacheFile.SetExt( inputName.GetExt() );
544 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
547 wxFFileOutputStream out( cacheFile.GetFullPath() );
555 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
583 m_files( std::move( other.m_files ) ),
588 other.m_embedFonts =
false;
598 m_files = std::move( other.m_files );
602 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.
#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.