KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 if( !aFiles )
332 THROW_PARSE_ERROR( "No embedded files object provided", CurSource(), CurLine(),
333 CurLineNumber(), CurOffset() );
334
335 using namespace EMBEDDED_FILES_T;
336
337 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file( nullptr );
338
339 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
340 {
341 if( token != T_LEFT )
342 Expecting( T_LEFT );
343
344 token = NextTok();
345
346 if( token != T_file )
347 Expecting( "file" );
348
349 if( file )
350 {
351 if( !file->compressedEncodedData.empty() )
352 {
354
355 if( !file->Validate() )
356 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
357 CurLine(), CurLineNumber(), CurOffset() );
358 }
359
360 aFiles->AddFile( file.release() );
361 }
362
363 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>( nullptr );
364
365 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
366 {
367 if( token != T_LEFT )
368 Expecting( T_LEFT );
369
370 token = NextTok();
371
372 switch( token )
373 {
374
375 case T_checksum:
376 NeedSYMBOLorNUMBER();
377
378 if( !IsSymbol( token ) )
379 Expecting( "checksum data" );
380
381 file->data_hash = CurStr();
382 NeedRIGHT();
383 break;
384
385 case T_data:
386 try
387 {
388 NeedBAR();
389 }
390 catch( const PARSE_ERROR& e )
391 {
392 // No data in the file -- due to bug in writer for 9.0.0
393 if( curTok == T_RIGHT )
394 break;
395 else
396 throw e;
397 }
398 catch( ... )
399 {
400 throw;
401 }
402
403 token = NextTok();
404
405 file->compressedEncodedData.reserve( 1 << 17 );
406
407 while( token != T_BAR )
408 {
409 if( !IsSymbol( token ) )
410 Expecting( "base64 file data" );
411
412 file->compressedEncodedData += CurStr();
413 token = NextTok();
414 }
415
416 file->compressedEncodedData.shrink_to_fit();
417
418 NeedRIGHT();
419 break;
420
421 case T_name:
422
423 if( file )
424 {
425 wxLogTrace( wxT( "KICAD_EMBED" ),
426 wxT( "Duplicate 'name' tag in embedded file %s" ), file->name );
427 }
428
429 NeedSYMBOLorNUMBER();
430
431 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
432 file->name = CurStr();
433 NeedRIGHT();
434
435 break;
436
437 case T_type:
438
439 token = NextTok();
440
441 switch( token )
442 {
443 case T_datasheet:
445 break;
446 case T_font:
448 break;
449 case T_model:
451 break;
452 case T_worksheet:
454 break;
455 case T_other:
457 break;
458 default:
459 Expecting( "datasheet, font, model, worksheet or other" );
460 break;
461 }
462 NeedRIGHT();
463 break;
464
465 default:
466 Expecting( "checksum, data or name" );
467 }
468 }
469 }
470
471 // Add the last file in the collection
472 if( file )
473 {
474 if( !file->compressedEncodedData.empty() )
475 {
477 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
478 CurLine(), CurLineNumber(), CurOffset() );
479 }
480
481 aFiles->AddFile( file.release() );
482 }
483}
484
485
486wxFileName EMBEDDED_FILES::GetTemporaryFileName( const wxString& aName ) const
487{
488 wxFileName cacheFile;
489
490 auto it = m_files.find( aName );
491
492 if( it == m_files.end() )
493 return cacheFile;
494
495 cacheFile.AssignDir( PATHS::GetUserCachePath() );
496 cacheFile.AppendDir( wxT( "embed" ) );
497
498 if( !PATHS::EnsurePathExists( cacheFile.GetFullPath() ) )
499 {
500 wxLogTrace( wxT( "KICAD_EMBED" ),
501 wxT( "%s:%s:%d\n * failed to create embed cache directory '%s'" ),
502 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
503
504 cacheFile.SetPath( wxFileName::GetTempDir() );
505 }
506
507 wxFileName inputName( aName );
508
509 // Store the cache file name using the data hash to allow for shared data between
510 // multiple projects using the same files as well as deconflicting files with the same name
511 cacheFile.SetName( "kicad_embedded_" + it->second->data_hash );
512 cacheFile.SetExt( inputName.GetExt() );
513
514 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
515 return cacheFile;
516
517 wxFFileOutputStream out( cacheFile.GetFullPath() );
518
519 if( !out.IsOk() )
520 {
521 cacheFile.Clear();
522 return cacheFile;
523 }
524
525 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
526
527 return cacheFile;
528}
529
530
531const std::vector<wxString>* EMBEDDED_FILES::GetFontFiles() const
532{
533 return &m_fontFiles;
534}
535
536
537const std::vector<wxString>* EMBEDDED_FILES::UpdateFontFiles()
538{
539 m_fontFiles.clear();
540
541 for( const auto& [name, entry] : m_files )
542 {
543 if( entry->type == EMBEDDED_FILE::FILE_TYPE::FONT )
544 m_fontFiles.push_back( GetTemporaryFileName( name ).GetFullPath() );
545 }
546
547 return &m_fontFiles;
548}
549
550// Move constructor
552 m_files( std::move( other.m_files ) ),
553 m_fontFiles( std::move( other.m_fontFiles ) ),
554 m_fileAddedCallback( std::move( other.m_fileAddedCallback ) ),
555 m_embedFonts( other.m_embedFonts )
556{
557 other.m_embedFonts = false;
558}
559
560// Move assignment operator
562{
563 if (this != &other)
564 {
565 ClearEmbeddedFiles();
566 m_files = std::move( other.m_files );
567 m_fontFiles = std::move( other.m_fontFiles );
568 m_fileAddedCallback = std::move( other.m_fileAddedCallback );
569 m_embedFonts = other.m_embedFonts;
570 other.m_embedFonts = false;
571 }
572 return *this;
573}
574
575// Copy constructor
576EMBEDDED_FILES::EMBEDDED_FILES( const EMBEDDED_FILES& other ) : m_embedFonts( other.m_embedFonts )
577{
578 for( const auto& [name, file] : other.m_files )
579 {
580 m_files[name] = new EMBEDDED_FILE( *file );
581 }
582 m_fontFiles = other.m_fontFiles;
584}
585
586// Copy assignment operator
588{
589 if( this != &other )
590 {
592 for( const auto& [name, file] : other.m_files )
593 {
594 m_files[name] = new EMBEDDED_FILE( *file );
595 }
596 m_fontFiles = other.m_fontFiles;
599 }
600 return *this;
601}
const char * name
Definition: DXF_plotter.cpp:59
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:545
int PRINTF_FUNC_N Print(int nestLevel, const char *fmt,...)
Format and write text to the output stream.
Definition: richio.cpp:460
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