KiCad PCB EDA Suite
Loading...
Searching...
No Matches
embedded_files.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14 * Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "embedded_files.h"
22
23#include <wx/base64.h>
24#include <wx/debug.h>
25#include <wx/filename.h>
26#include <wx/log.h>
27#include <wx/mstream.h>
28#include <wx/wfstream.h>
29
30#include <map>
31#include <memory>
32#include <sstream>
33
34#include <zstd.h>
35
36#include <kiid.h>
37#include <mmh3_hash.h>
38#include <paths.h>
39
40
41
42EMBEDDED_FILES::EMBEDDED_FILE* EMBEDDED_FILES::AddFile( const wxFileName& aName, bool aOverwrite )
43{
44 if( HasFile( aName.GetFullName() ) )
45 {
46 if( !aOverwrite )
47 return m_files[aName.GetFullName()];
48
49 m_files.erase( aName.GetFullName() );
50 }
51
52 wxFFileInputStream file( aName.GetFullPath() );
53 wxMemoryBuffer buffer;
54
55 if( !file.IsOk() )
56 return nullptr;
57
58 wxFileOffset length = file.GetLength();
59
60 std::unique_ptr<EMBEDDED_FILE> efile = std::make_unique<EMBEDDED_FILE>();
61 efile->name = aName.GetFullName();
62 efile->decompressedData.resize( length );
63
64 wxString ext = aName.GetExt().Upper();
65
66 // Handle some common file extensions
67 if( ext == "STP" || ext == "STPZ" || ext == "STEP" || ext == "WRL" || ext == "WRZ" )
68 {
70 }
71 else if( ext == "WOFF" || ext == "WOFF2" || ext == "TTF" || ext == "OTF" )
72 {
74 }
75 else if( ext == "PDF" )
76 {
78 }
79 else if( ext == "KICAD_WKS" )
80 {
82 }
83
84 if( !efile->decompressedData.data() )
85 return nullptr;
86
87 char* data = efile->decompressedData.data();
88 wxFileOffset total_read = 0;
89
90 while( !file.Eof() && total_read < length )
91 {
92 file.Read( data, length - total_read );
93
94 size_t read = file.LastRead();
95 data += read;
96 total_read += read;
97 }
98
99 if( CompressAndEncode( *efile ) != RETURN_CODE::OK )
100 return nullptr;
101
102 efile->is_valid = true;
103
104 EMBEDDED_FILE* result = efile.release();
105 m_files[aName.GetFullName()] = result;
106
108 m_fileAddedCallback( result );
109
110 return m_files[aName.GetFullName()];
111}
112
113
115{
116 m_files.insert( { aFile->name, aFile } );
117
119 m_fileAddedCallback( aFile );
120}
121
122
123// Remove a file from the collection
124void EMBEDDED_FILES::RemoveFile( const wxString& name, bool aErase )
125{
126 auto it = m_files.find( name );
127
128 if( it != m_files.end() )
129 {
130 if( aErase )
131 delete it->second;
132
133 m_files.erase( it );
134 }
135}
136
137
139{
140 for( auto it = m_files.begin(); it != m_files.end(); )
141 {
142 if( it->second->type == EMBEDDED_FILE::FILE_TYPE::FONT )
143 {
144 delete it->second;
145 it = m_files.erase( it );
146 }
147 else
148 {
149 ++it;
150 }
151 }
152}
153
154
155// Write the collection of files to a disk file in the specified format
156void EMBEDDED_FILES::WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const
157{
158 ssize_t MIME_BASE64_LENGTH = 76;
159 aOut.Print( "(embedded_files " );
160
161 for( const auto& [name, entry] : m_files )
162 {
163 const EMBEDDED_FILE& file = *entry;
164
165 // Skip empty files
166 if( file.compressedEncodedData.empty() )
167 {
168 wxLogDebug( wxT( "Error: Embedded file '%s' is empty" ), file.name );
169 continue;
170 }
171
172 aOut.Print( "(file " );
173 aOut.Print( "(name %s)", aOut.Quotew( file.name ).c_str() );
174
175 const char* type = nullptr;
176
177 switch( file.type )
178 {
179 case EMBEDDED_FILE::FILE_TYPE::DATASHEET: type = "datasheet"; break;
180 case EMBEDDED_FILE::FILE_TYPE::FONT: type = "font"; break;
181 case EMBEDDED_FILE::FILE_TYPE::MODEL: type = "model"; break;
182 case EMBEDDED_FILE::FILE_TYPE::WORKSHEET: type = "worksheet"; break;
183 default: type = "other"; break;
184 }
185
186 aOut.Print( "(type %s)", type );
187
188 if( aWriteData )
189 {
190 aOut.Print( "(data" );
191
192 size_t first = 0;
193
194 while( first < file.compressedEncodedData.length() )
195 {
196 ssize_t remaining = file.compressedEncodedData.length() - first;
197 int length = std::min( remaining, MIME_BASE64_LENGTH );
198
199 std::string_view view( file.compressedEncodedData.data() + first, length );
200
201 aOut.Print( "\n%1s%.*s%s\n", first ? "" : "|", length, view.data(),
202 remaining == length ? "|" : "" );
203 first += MIME_BASE64_LENGTH;
204 }
205
206 aOut.Print( ")" ); // Close data
207 }
208
209 aOut.Print( "(checksum %s)", aOut.Quotew( file.data_hash ).c_str() );
210 aOut.Print( ")" ); // Close file
211 }
212
213 aOut.Print( ")" ); // Close embedded_files
214}
215
216
217// Compress and Base64 encode data
219{
220 std::vector<char> compressedData;
221 size_t estCompressedSize = ZSTD_compressBound( aFile.decompressedData.size() );
222 compressedData.resize( estCompressedSize );
223 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
224 aFile.decompressedData.data(),
225 aFile.decompressedData.size(), 15 );
226
227 if( ZSTD_isError( compressedSize ) )
228 {
229 compressedData.clear();
231 }
232
233 const size_t dstLen = wxBase64EncodedSize( compressedSize );
234 aFile.compressedEncodedData.resize( dstLen );
235 size_t retval = wxBase64Encode( aFile.compressedEncodedData.data(), dstLen,
236 compressedData.data(), compressedSize );
237
238 if( retval != dstLen )
239 {
240 aFile.compressedEncodedData.clear();
242 }
243
245 hash.add( aFile.decompressedData );
246 aFile.data_hash = hash.digest().ToString();
247
248 return RETURN_CODE::OK;
249}
250
251
252// Decompress and Base64 decode data
254{
255 std::vector<char> compressedData;
256 size_t compressedSize = wxBase64DecodedSize( aFile.compressedEncodedData.size() );
257
258 if( compressedSize == 0 )
259 {
260 wxLogTrace( wxT( "KICAD_EMBED" ),
261 wxT( "%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
262 __FILE__, __FUNCTION__, __LINE__, aFile.name,
263 aFile.compressedEncodedData.size() );
265 }
266
267 compressedData.resize( compressedSize );
268 void* compressed = compressedData.data();
269
270 // The return value from wxBase64Decode is the actual size of the decoded data avoiding
271 // the modulo 4 padding of the base64 encoding
272 compressedSize = wxBase64Decode( compressed, compressedSize, aFile.compressedEncodedData );
273
274 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
275
276 if( estDecompressedSize > 1e9 ) // Limit to 1GB
278
279 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
280 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
281 {
283 }
284
285 aFile.decompressedData.resize( estDecompressedSize );
286 void* decompressed = aFile.decompressedData.data();
287
288 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
289 compressed, compressedSize );
290
291 if( ZSTD_isError( decompressedSize ) )
292 {
293 wxLogTrace( wxT( "KICAD_EMBED" ),
294 wxT( "%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
295 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
296 aFile.decompressedData.clear();
298 }
299
300 aFile.decompressedData.resize( decompressedSize );
301 std::string test_hash;
302 std::string new_hash;
303
305 hash.add( aFile.decompressedData );
306 new_hash = hash.digest().ToString();
307
308 if( aFile.data_hash.length() == 64 )
309 picosha2::hash256_hex_string( aFile.decompressedData, test_hash );
310 else
311 test_hash = new_hash;
312
313 if( test_hash != aFile.data_hash )
314 {
315 wxLogTrace( wxT( "KICAD_EMBED" ),
316 wxT( "%s:%s:%d\n * Checksum error in embedded file '%s'" ),
317 __FILE__, __FUNCTION__, __LINE__, aFile.name );
318 aFile.decompressedData.clear();
320 }
321
322 aFile.data_hash = new_hash;
323
324 return RETURN_CODE::OK;
325}
326
327
328// Parsing method
330{
331 // embedded files are version 20240706 and uses also Bars as separator
332 SetKnowsBar( true );
333
334 if( !aFiles )
335 THROW_PARSE_ERROR( "No embedded files object provided", CurSource(), CurLine(),
336 CurLineNumber(), CurOffset() );
337
338 using namespace EMBEDDED_FILES_T;
339
340 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file( nullptr );
341
342 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
343 {
344 if( token != T_LEFT )
345 Expecting( T_LEFT );
346
347 token = NextTok();
348
349 if( token != T_file )
350 Expecting( "file" );
351
352 if( file )
353 {
354 if( !file->compressedEncodedData.empty() )
355 {
357
358 if( !file->Validate() )
359 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
360 CurLine(), CurLineNumber(), CurOffset() );
361 }
362
363 aFiles->AddFile( file.release() );
364 }
365
366 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>( nullptr );
367
368 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
369 {
370 if( token != T_LEFT )
371 Expecting( T_LEFT );
372
373 token = NextTok();
374
375 switch( token )
376 {
377 case T_checksum:
378 if( !file )
379 Expecting( T_name );
380
381 NeedSYMBOLorNUMBER();
382
383 if( !IsSymbol( token ) )
384 Expecting( "checksum data" );
385
386 file->data_hash = CurStr();
387 NeedRIGHT();
388 break;
389
390 case T_data:
391 if( !file )
392 Expecting( T_name);
393
394 try
395 {
396 NeedBAR();
397 }
398 catch( const PARSE_ERROR& e )
399 {
400 // No data in the file -- due to bug in writer for 9.0.0
401 if( curTok == T_RIGHT )
402 break;
403 else
404 throw e;
405 }
406 catch( ... )
407 {
408 throw;
409 }
410
411 token = NextTok();
412
413 file->compressedEncodedData.reserve( 1 << 17 );
414
415 while( token != T_BAR )
416 {
417 if( !IsSymbol( token ) )
418 Expecting( "base64 file data" );
419
420 file->compressedEncodedData += CurStr();
421 token = NextTok();
422 }
423
424 file->compressedEncodedData.shrink_to_fit();
425
426 NeedRIGHT();
427 break;
428
429 case T_name:
430 if( file )
431 {
432 wxLogTrace( wxT( "KICAD_EMBED" ),
433 wxT( "Duplicate 'name' tag in embedded file %s" ), file->name );
434 }
435
436 NeedSYMBOLorNUMBER();
437
438 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
439 file->name = CurStr();
440 NeedRIGHT();
441
442 break;
443
444 case T_type:
445 if( !file )
446 Expecting( T_name );
447
448 token = NextTok();
449
450 switch( token )
451 {
452 case T_datasheet: file->type = EMBEDDED_FILES::EMBEDDED_FILE::FILE_TYPE::DATASHEET; break;
453 case T_font: file->type = EMBEDDED_FILES::EMBEDDED_FILE::FILE_TYPE::FONT; break;
454 case T_model: file->type = EMBEDDED_FILES::EMBEDDED_FILE::FILE_TYPE::MODEL; break;
455 case T_worksheet: file->type = EMBEDDED_FILES::EMBEDDED_FILE::FILE_TYPE::WORKSHEET; break;
456 case T_other: file->type = EMBEDDED_FILES::EMBEDDED_FILE::FILE_TYPE::OTHER; break;
457 default: Expecting( "datasheet, font, model, worksheet or other" ); break;
458 }
459
460 NeedRIGHT();
461 break;
462
463 default:
464 Expecting( "checksum, data or name" );
465 }
466 }
467 }
468
469 // Add the last file in the collection
470 if( file )
471 {
472 if( !file->compressedEncodedData.empty() )
473 {
475 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
476 CurLine(), CurLineNumber(), CurOffset() );
477 }
478
479 aFiles->AddFile( file.release() );
480 }
481}
482
483
484wxFileName EMBEDDED_FILES::GetTemporaryFileName( const wxString& aName ) const
485{
486 wxFileName cacheFile;
487
488 auto it = m_files.find( aName );
489
490 if( it == m_files.end() )
491 return cacheFile;
492
493 cacheFile.AssignDir( PATHS::GetUserCachePath() );
494 cacheFile.AppendDir( wxT( "embed" ) );
495
496 if( !PATHS::EnsurePathExists( cacheFile.GetFullPath() ) )
497 {
498 wxLogTrace( wxT( "KICAD_EMBED" ),
499 wxT( "%s:%s:%d\n * failed to create embed cache directory '%s'" ),
500 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
501
502 cacheFile.SetPath( wxFileName::GetTempDir() );
503 }
504
505 wxFileName inputName( aName );
506
507 // Store the cache file name using the data hash to allow for shared data between
508 // multiple projects using the same files as well as deconflicting files with the same name
509 cacheFile.SetName( "kicad_embedded_" + it->second->data_hash );
510 cacheFile.SetExt( inputName.GetExt() );
511
512 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
513 return cacheFile;
514
515 wxFFileOutputStream out( cacheFile.GetFullPath() );
516
517 if( !out.IsOk() )
518 {
519 cacheFile.Clear();
520 return cacheFile;
521 }
522
523 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
524
525 return cacheFile;
526}
527
528
529const std::vector<wxString>* EMBEDDED_FILES::GetFontFiles() const
530{
531 return &m_fontFiles;
532}
533
534
535const std::vector<wxString>* EMBEDDED_FILES::UpdateFontFiles()
536{
537 m_fontFiles.clear();
538
539 for( const auto& [name, entry] : m_files )
540 {
541 if( entry->type == EMBEDDED_FILE::FILE_TYPE::FONT )
542 m_fontFiles.push_back( GetTemporaryFileName( name ).GetFullPath() );
543 }
544
545 return &m_fontFiles;
546}
547
548// Move constructor
550 m_files( std::move( other.m_files ) ),
551 m_fontFiles( std::move( other.m_fontFiles ) ),
552 m_fileAddedCallback( std::move( other.m_fileAddedCallback ) ),
553 m_embedFonts( other.m_embedFonts )
554{
555 other.m_embedFonts = false;
556}
557
558// Move assignment operator
560{
561 if (this != &other)
562 {
563 ClearEmbeddedFiles();
564 m_files = std::move( other.m_files );
565 m_fontFiles = std::move( other.m_fontFiles );
566 m_fileAddedCallback = std::move( other.m_fileAddedCallback );
567 m_embedFonts = other.m_embedFonts;
568 other.m_embedFonts = false;
569 }
570 return *this;
571}
572
573// Copy constructor
574EMBEDDED_FILES::EMBEDDED_FILES( const EMBEDDED_FILES& other ) : m_embedFonts( other.m_embedFonts )
575{
576 for( const auto& [name, file] : other.m_files )
577 {
578 m_files[name] = new EMBEDDED_FILE( *file );
579 }
580 m_fontFiles = other.m_fontFiles;
582}
583
584// Copy assignment operator
586{
587 if( this != &other )
588 {
590 for( const auto& [name, file] : other.m_files )
591 {
592 m_files[name] = new EMBEDDED_FILE( *file );
593 }
594 m_fontFiles = other.m_fontFiles;
597 }
598 return *this;
599}
const char * name
Definition: DXF_plotter.cpp:62
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.
FileAddedCallback m_fileAddedCallback
@ 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 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.
static uint32_t Seed()
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...
EMBEDDED_FILES()=default
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.
Definition: mmh3_hash.h:60
FORCE_INLINE void add(const std::string &input)
Definition: mmh3_hash.h:95
FORCE_INLINE HASH_128 digest()
Definition: mmh3_hash.h:114
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:322
std::string Quotew(const wxString &aWrapee) const
Definition: richio.cpp:548
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:463
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:482
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:424
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:165
#define MIME_BASE64_LENGTH
std::vector< char > decompressedData
std::string ToString() const
Definition: hash_128.h:47
A filename or source description, a problem input line, a line number, a byte offset,...
Definition: ki_exception.h:120