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 aOut.Print( "(file " );
166 aOut.Print( "(name %s)", aOut.Quotew( file.name ).c_str() );
167
168 const char* type = nullptr;
169
170 switch( file.type )
171 {
172 case EMBEDDED_FILE::FILE_TYPE::DATASHEET: type = "datasheet"; break;
173 case EMBEDDED_FILE::FILE_TYPE::FONT: type = "font"; break;
174 case EMBEDDED_FILE::FILE_TYPE::MODEL: type = "model"; break;
175 case EMBEDDED_FILE::FILE_TYPE::WORKSHEET: type = "worksheet"; break;
176 default: type = "other"; break;
177 }
178
179 aOut.Print( "(type %s)", type );
180
181 if( aWriteData )
182 {
183 aOut.Print( "(data" );
184
185 size_t first = 0;
186
187 while( first < file.compressedEncodedData.length() )
188 {
189 ssize_t remaining = file.compressedEncodedData.length() - first;
190 int length = std::min( remaining, MIME_BASE64_LENGTH );
191
192 std::string_view view( file.compressedEncodedData.data() + first, length );
193
194 aOut.Print( "\n%1s%.*s%s\n", first ? "" : "|", length, view.data(),
195 remaining == length ? "|" : "" );
196 first += MIME_BASE64_LENGTH;
197 }
198
199 aOut.Print( ")" ); // Close data
200 }
201
202 aOut.Print( "(checksum %s)", aOut.Quotew( file.data_hash ).c_str() );
203 aOut.Print( ")" ); // Close file
204 }
205
206 aOut.Print( ")" ); // Close embedded_files
207}
208
209
210// Compress and Base64 encode data
212{
213 std::vector<char> compressedData;
214 size_t estCompressedSize = ZSTD_compressBound( aFile.decompressedData.size() );
215 compressedData.resize( estCompressedSize );
216 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
217 aFile.decompressedData.data(),
218 aFile.decompressedData.size(), 15 );
219
220 if( ZSTD_isError( compressedSize ) )
221 {
222 compressedData.clear();
224 }
225
226 const size_t dstLen = wxBase64EncodedSize( compressedSize );
227 aFile.compressedEncodedData.resize( dstLen );
228 size_t retval = wxBase64Encode( aFile.compressedEncodedData.data(), dstLen,
229 compressedData.data(), compressedSize );
230
231 if( retval != dstLen )
232 {
233 aFile.compressedEncodedData.clear();
235 }
236
238 hash.add( aFile.decompressedData );
239 aFile.data_hash = hash.digest().ToString();
240
241 return RETURN_CODE::OK;
242}
243
244
245// Decompress and Base64 decode data
247{
248 std::vector<char> compressedData;
249 size_t compressedSize = wxBase64DecodedSize( aFile.compressedEncodedData.size() );
250
251 if( compressedSize == 0 )
252 {
253 wxLogTrace( wxT( "KICAD_EMBED" ),
254 wxT( "%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
255 __FILE__, __FUNCTION__, __LINE__, aFile.name,
256 aFile.compressedEncodedData.size() );
258 }
259
260 compressedData.resize( compressedSize );
261 void* compressed = compressedData.data();
262
263 // The return value from wxBase64Decode is the actual size of the decoded data avoiding
264 // the modulo 4 padding of the base64 encoding
265 compressedSize = wxBase64Decode( compressed, compressedSize, aFile.compressedEncodedData );
266
267 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
268
269 if( estDecompressedSize > 1e9 ) // Limit to 1GB
271
272 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
273 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
274 {
276 }
277
278 aFile.decompressedData.resize( estDecompressedSize );
279 void* decompressed = aFile.decompressedData.data();
280
281 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
282 compressed, compressedSize );
283
284 if( ZSTD_isError( decompressedSize ) )
285 {
286 wxLogTrace( wxT( "KICAD_EMBED" ),
287 wxT( "%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
288 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
289 aFile.decompressedData.clear();
291 }
292
293 aFile.decompressedData.resize( decompressedSize );
294 std::string test_hash;
295 std::string new_hash;
296
298 hash.add( aFile.decompressedData );
299 new_hash = hash.digest().ToString();
300
301 if( aFile.data_hash.length() == 64 )
302 picosha2::hash256_hex_string( aFile.decompressedData, test_hash );
303 else
304 test_hash = new_hash;
305
306 if( test_hash != aFile.data_hash )
307 {
308 wxLogTrace( wxT( "KICAD_EMBED" ),
309 wxT( "%s:%s:%d\n * Checksum error in embedded file '%s'" ),
310 __FILE__, __FUNCTION__, __LINE__, aFile.name );
311 aFile.decompressedData.clear();
313 }
314
315 aFile.data_hash = new_hash;
316
317 return RETURN_CODE::OK;
318}
319
320
321// Parsing method
323{
324 if( !aFiles )
325 THROW_PARSE_ERROR( "No embedded files object provided", CurSource(), CurLine(),
326 CurLineNumber(), CurOffset() );
327
328 using namespace EMBEDDED_FILES_T;
329
330 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file( nullptr );
331
332 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
333 {
334 if( token != T_LEFT )
335 Expecting( T_LEFT );
336
337 token = NextTok();
338
339 if( token != T_file )
340 Expecting( "file" );
341
342 if( file )
343 {
344 if( !file->compressedEncodedData.empty() )
345 {
347
348 if( !file->Validate() )
349 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
350 CurLine(), CurLineNumber(), CurOffset() );
351 }
352
353 aFiles->AddFile( file.release() );
354 }
355
356 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>( nullptr );
357
358 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
359 {
360 if( token != T_LEFT )
361 Expecting( T_LEFT );
362
363 token = NextTok();
364
365 switch( token )
366 {
367
368 case T_checksum:
369 NeedSYMBOLorNUMBER();
370
371 if( !IsSymbol( token ) )
372 Expecting( "checksum data" );
373
374 file->data_hash = CurStr();
375 NeedRIGHT();
376 break;
377
378 case T_data:
379 NeedBAR();
380 token = NextTok();
381
382 file->compressedEncodedData.reserve( 1 << 17 );
383
384 while( token != T_BAR )
385 {
386 if( !IsSymbol( token ) )
387 Expecting( "base64 file data" );
388
389 file->compressedEncodedData += CurStr();
390 token = NextTok();
391 }
392
393 file->compressedEncodedData.shrink_to_fit();
394
395 NeedRIGHT();
396 break;
397
398 case T_name:
399
400 if( file )
401 {
402 wxLogTrace( wxT( "KICAD_EMBED" ),
403 wxT( "Duplicate 'name' tag in embedded file %s" ), file->name );
404 }
405
406 NeedSYMBOLorNUMBER();
407
408 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
409 file->name = CurStr();
410 NeedRIGHT();
411
412 break;
413
414 case T_type:
415
416 token = NextTok();
417
418 switch( token )
419 {
420 case T_datasheet:
422 break;
423 case T_font:
425 break;
426 case T_model:
428 break;
429 case T_worksheet:
431 break;
432 case T_other:
434 break;
435 default:
436 Expecting( "datasheet, font, model, worksheet or other" );
437 break;
438 }
439 NeedRIGHT();
440 break;
441
442 default:
443 Expecting( "checksum, data or name" );
444 }
445 }
446 }
447
448 // Add the last file in the collection
449 if( file )
450 {
451 if( !file->compressedEncodedData.empty() )
452 {
454 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
455 CurLine(), CurLineNumber(), CurOffset() );
456 }
457
458 aFiles->AddFile( file.release() );
459 }
460}
461
462
463wxFileName EMBEDDED_FILES::GetTemporaryFileName( const wxString& aName ) const
464{
465 wxFileName cacheFile;
466
467 auto it = m_files.find( aName );
468
469 if( it == m_files.end() )
470 return cacheFile;
471
472 cacheFile.AssignDir( PATHS::GetUserCachePath() );
473 cacheFile.AppendDir( wxT( "embed" ) );
474
475 if( !PATHS::EnsurePathExists( cacheFile.GetFullPath() ) )
476 {
477 wxLogTrace( wxT( "KICAD_EMBED" ),
478 wxT( "%s:%s:%d\n * failed to create embed cache directory '%s'" ),
479 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
480
481 cacheFile.SetPath( wxFileName::GetTempDir() );
482 }
483
484 wxFileName inputName( aName );
485
486 // Store the cache file name using the data hash to allow for shared data between
487 // multiple projects using the same files as well as deconflicting files with the same name
488 cacheFile.SetName( "kicad_embedded_" + it->second->data_hash );
489 cacheFile.SetExt( inputName.GetExt() );
490
491 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
492 return cacheFile;
493
494 wxFFileOutputStream out( cacheFile.GetFullPath() );
495
496 if( !out.IsOk() )
497 {
498 cacheFile.Clear();
499 return cacheFile;
500 }
501
502 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
503
504 return cacheFile;
505}
506
507
508const std::vector<wxString>* EMBEDDED_FILES::GetFontFiles() const
509{
510 return &m_fontFiles;
511}
512
513
514const std::vector<wxString>* EMBEDDED_FILES::UpdateFontFiles()
515{
516 m_fontFiles.clear();
517
518 for( const auto& [name, entry] : m_files )
519 {
520 if( entry->type == EMBEDDED_FILE::FILE_TYPE::FONT )
521 m_fontFiles.push_back( GetTemporaryFileName( name ).GetFullPath() );
522 }
523
524 return &m_fontFiles;
525}
526
527// Move constructor
529 m_files( std::move( other.m_files ) ),
530 m_fontFiles( std::move( other.m_fontFiles ) ),
531 m_fileAddedCallback( std::move( other.m_fileAddedCallback ) ),
532 m_embedFonts( other.m_embedFonts )
533{
534 other.m_embedFonts = false;
535}
536
537// Move assignment operator
539{
540 if (this != &other)
541 {
542 ClearEmbeddedFiles();
543 m_files = std::move( other.m_files );
544 m_fontFiles = std::move( other.m_fontFiles );
545 m_fileAddedCallback = std::move( other.m_fileAddedCallback );
546 m_embedFonts = other.m_embedFonts;
547 other.m_embedFonts = false;
548 }
549 return *this;
550}
551
552// Copy constructor
553EMBEDDED_FILES::EMBEDDED_FILES( const EMBEDDED_FILES& other ) : m_embedFonts( other.m_embedFonts )
554{
555 for( const auto& [name, file] : other.m_files )
556 {
557 m_files[name] = new EMBEDDED_FILE( *file );
558 }
559 m_fontFiles = other.m_fontFiles;
561}
562
563// Copy assignment operator
565{
566 if( this != &other )
567 {
569 for( const auto& [name, file] : other.m_files )
570 {
571 m_files[name] = new EMBEDDED_FILE( *file );
572 }
573 m_fontFiles = other.m_fontFiles;
576 }
577 return *this;
578}
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:467
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:409
#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