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
14 * General 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 m_files[aName.GetFullName()] = efile.release();
105
106 return m_files[aName.GetFullName()];
107}
108
109
111{
112 m_files.insert( { aFile->name, aFile } );
113}
114
115
116// Remove a file from the collection
117void EMBEDDED_FILES::RemoveFile( const wxString& name, bool aErase )
118{
119 auto it = m_files.find( name );
120
121 if( it != m_files.end() )
122 {
123 m_files.erase( it );
124
125 if( aErase )
126 delete it->second;
127 }
128}
129
130
132{
133 for( auto it = m_files.begin(); it != m_files.end(); )
134 {
135 if( it->second->type == EMBEDDED_FILE::FILE_TYPE::FONT )
136 {
137 delete it->second;
138 it = m_files.erase( it );
139 }
140 else
141 {
142 ++it;
143 }
144 }
145}
146
147
148// Write the collection of files to a disk file in the specified format
149void EMBEDDED_FILES::WriteEmbeddedFiles( OUTPUTFORMATTER& aOut, bool aWriteData ) const
150{
151 ssize_t MIME_BASE64_LENGTH = 76;
152 aOut.Print( "(embedded_files " );
153
154 for( const auto& [name, entry] : m_files )
155 {
156 const EMBEDDED_FILE& file = *entry;
157
158 aOut.Print( "(file " );
159 aOut.Print( "(name %s)", aOut.Quotew( file.name ).c_str() );
160
161 const char* type = nullptr;
162
163 switch( file.type )
164 {
165 case EMBEDDED_FILE::FILE_TYPE::DATASHEET: type = "datasheet"; break;
166 case EMBEDDED_FILE::FILE_TYPE::FONT: type = "font"; break;
167 case EMBEDDED_FILE::FILE_TYPE::MODEL: type = "model"; break;
168 case EMBEDDED_FILE::FILE_TYPE::WORKSHEET: type = "worksheet"; break;
169 default: type = "other"; break;
170 }
171
172 aOut.Print( "(type %s)", type );
173
174 if( aWriteData )
175 {
176 aOut.Print( "(data" );
177
178 size_t first = 0;
179
180 while( first < file.compressedEncodedData.length() )
181 {
182 ssize_t remaining = file.compressedEncodedData.length() - first;
183 int length = std::min( remaining, MIME_BASE64_LENGTH );
184
185 std::string_view view( file.compressedEncodedData.data() + first, length );
186
187 aOut.Print( "\n%1s%.*s%s\n", first ? "" : "|", length, view.data(),
188 remaining == length ? "|" : "" );
189 first += MIME_BASE64_LENGTH;
190 }
191
192 aOut.Print( ")" ); // Close data
193 }
194
195 aOut.Print( "(checksum %s)", aOut.Quotew( file.data_hash ).c_str() );
196 aOut.Print( ")" ); // Close file
197 }
198
199 aOut.Print( ")" ); // Close embedded_files
200}
201
202
203// Compress and Base64 encode data
205{
206 std::vector<char> compressedData;
207 size_t estCompressedSize = ZSTD_compressBound( aFile.decompressedData.size() );
208 compressedData.resize( estCompressedSize );
209 size_t compressedSize = ZSTD_compress( compressedData.data(), estCompressedSize,
210 aFile.decompressedData.data(),
211 aFile.decompressedData.size(), 15 );
212
213 if( ZSTD_isError( compressedSize ) )
214 {
215 compressedData.clear();
217 }
218
219 const size_t dstLen = wxBase64EncodedSize( compressedSize );
220 aFile.compressedEncodedData.resize( dstLen );
221 size_t retval = wxBase64Encode( aFile.compressedEncodedData.data(), dstLen,
222 compressedData.data(), compressedSize );
223
224 if( retval != dstLen )
225 {
226 aFile.compressedEncodedData.clear();
228 }
229
231 hash.add( aFile.decompressedData );
232 aFile.data_hash = hash.digest().ToString();
233
234 return RETURN_CODE::OK;
235}
236
237
238// Decompress and Base64 decode data
240{
241 std::vector<char> compressedData;
242 size_t compressedSize = wxBase64DecodedSize( aFile.compressedEncodedData.size() );
243
244 if( compressedSize == 0 )
245 {
246 wxLogTrace( wxT( "KICAD_EMBED" ),
247 wxT( "%s:%s:%d\n * Base64DecodedSize failed for file '%s' with size %zu" ),
248 __FILE__, __FUNCTION__, __LINE__, aFile.name,
249 aFile.compressedEncodedData.size() );
251 }
252
253 compressedData.resize( compressedSize );
254 void* compressed = compressedData.data();
255
256 // The return value from wxBase64Decode is the actual size of the decoded data avoiding
257 // the modulo 4 padding of the base64 encoding
258 compressedSize = wxBase64Decode( compressed, compressedSize, aFile.compressedEncodedData );
259
260 unsigned long long estDecompressedSize = ZSTD_getFrameContentSize( compressed, compressedSize );
261
262 if( estDecompressedSize > 1e9 ) // Limit to 1GB
264
265 if( estDecompressedSize == ZSTD_CONTENTSIZE_ERROR
266 || estDecompressedSize == ZSTD_CONTENTSIZE_UNKNOWN )
267 {
269 }
270
271 aFile.decompressedData.resize( estDecompressedSize );
272 void* decompressed = aFile.decompressedData.data();
273
274 size_t decompressedSize = ZSTD_decompress( decompressed, estDecompressedSize,
275 compressed, compressedSize );
276
277 if( ZSTD_isError( decompressedSize ) )
278 {
279 wxLogTrace( wxT( "KICAD_EMBED" ),
280 wxT( "%s:%s:%d\n * ZSTD_decompress failed with error '%s'" ),
281 __FILE__, __FUNCTION__, __LINE__, ZSTD_getErrorName( decompressedSize ) );
282 aFile.decompressedData.clear();
284 }
285
286 aFile.decompressedData.resize( decompressedSize );
287 std::string test_hash;
288 std::string new_hash;
289
291 hash.add( aFile.decompressedData );
292 new_hash = hash.digest().ToString();
293
294 if( aFile.data_hash.length() == 64 )
295 picosha2::hash256_hex_string( aFile.decompressedData, test_hash );
296 else
297 test_hash = new_hash;
298
299 if( test_hash != aFile.data_hash )
300 {
301 wxLogTrace( wxT( "KICAD_EMBED" ),
302 wxT( "%s:%s:%d\n * Checksum error in embedded file '%s'" ),
303 __FILE__, __FUNCTION__, __LINE__, aFile.name );
304 aFile.decompressedData.clear();
306 }
307
308 aFile.data_hash = new_hash;
309
310 return RETURN_CODE::OK;
311}
312
313
314// Parsing method
316{
317 if( !aFiles )
318 THROW_PARSE_ERROR( "No embedded files object provided", CurSource(), CurLine(),
319 CurLineNumber(), CurOffset() );
320
321 using namespace EMBEDDED_FILES_T;
322
323 std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE> file( nullptr );
324
325 for( T token = NextTok(); token != T_RIGHT; token = NextTok() )
326 {
327 if( token != T_LEFT )
328 Expecting( T_LEFT );
329
330 token = NextTok();
331
332 if( token != T_file )
333 Expecting( "file" );
334
335 if( file )
336 {
337 if( !file->compressedEncodedData.empty() )
338 {
340
341 if( !file->Validate() )
342 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
343 CurLine(), CurLineNumber(), CurOffset() );
344 }
345
346 aFiles->AddFile( file.release() );
347 }
348
349 file = std::unique_ptr<EMBEDDED_FILES::EMBEDDED_FILE>( nullptr );
350
351 for( token = NextTok(); token != T_RIGHT; token = NextTok() )
352 {
353 if( token != T_LEFT )
354 Expecting( T_LEFT );
355
356 token = NextTok();
357
358 switch( token )
359 {
360
361 case T_checksum:
362 NeedSYMBOLorNUMBER();
363
364 if( !IsSymbol( token ) )
365 Expecting( "checksum data" );
366
367 file->data_hash = CurStr();
368 NeedRIGHT();
369 break;
370
371 case T_data:
372 NeedBAR();
373 token = NextTok();
374
375 file->compressedEncodedData.reserve( 1 << 17 );
376
377 while( token != T_BAR )
378 {
379 if( !IsSymbol( token ) )
380 Expecting( "base64 file data" );
381
382 file->compressedEncodedData += CurStr();
383 token = NextTok();
384 }
385
386 file->compressedEncodedData.shrink_to_fit();
387
388 NeedRIGHT();
389 break;
390
391 case T_name:
392
393 if( file )
394 {
395 wxLogTrace( wxT( "KICAD_EMBED" ),
396 wxT( "Duplicate 'name' tag in embedded file %s" ), file->name );
397 }
398
399 NeedSYMBOLorNUMBER();
400
401 file = std::make_unique<EMBEDDED_FILES::EMBEDDED_FILE>();
402 file->name = CurStr();
403 NeedRIGHT();
404
405 break;
406
407 case T_type:
408
409 token = NextTok();
410
411 switch( token )
412 {
413 case T_datasheet:
415 break;
416 case T_font:
418 break;
419 case T_model:
421 break;
422 case T_worksheet:
424 break;
425 case T_other:
427 break;
428 default:
429 Expecting( "datasheet, font, model, worksheet or other" );
430 break;
431 }
432 NeedRIGHT();
433 break;
434
435 default:
436 Expecting( "checksum, data or name" );
437 }
438 }
439 }
440
441 // Add the last file in the collection
442 if( file )
443 {
444 if( !file->compressedEncodedData.empty() )
445 {
447 THROW_PARSE_ERROR( "Checksum error in embedded file " + file->name, CurSource(),
448 CurLine(), CurLineNumber(), CurOffset() );
449 }
450
451 aFiles->AddFile( file.release() );
452 }
453}
454
455
456wxFileName EMBEDDED_FILES::GetTemporaryFileName( const wxString& aName ) const
457{
458 wxFileName cacheFile;
459
460 auto it = m_files.find( aName );
461
462 if( it == m_files.end() )
463 return cacheFile;
464
465 cacheFile.AssignDir( PATHS::GetUserCachePath() );
466 cacheFile.AppendDir( wxT( "embed" ) );
467
468 if( !PATHS::EnsurePathExists( cacheFile.GetFullPath() ) )
469 {
470 wxLogTrace( wxT( "KICAD_EMBED" ),
471 wxT( "%s:%s:%d\n * failed to create embed cache directory '%s'" ),
472 __FILE__, __FUNCTION__, __LINE__, cacheFile.GetPath() );
473
474 cacheFile.SetPath( wxFileName::GetTempDir() );
475 }
476
477 wxFileName inputName( aName );
478
479 // Store the cache file name using the data hash to allow for shared data between
480 // multiple projects using the same files as well as deconflicting files with the same name
481 cacheFile.SetName( "kicad_embedded_" + it->second->data_hash );
482 cacheFile.SetExt( inputName.GetExt() );
483
484 if( cacheFile.FileExists() && cacheFile.IsFileReadable() )
485 return cacheFile;
486
487 wxFFileOutputStream out( cacheFile.GetFullPath() );
488
489 if( !out.IsOk() )
490 {
491 cacheFile.Clear();
492 return cacheFile;
493 }
494
495 out.Write( it->second->decompressedData.data(), it->second->decompressedData.size() );
496
497 return cacheFile;
498}
499
500
501const std::vector<wxString>* EMBEDDED_FILES::GetFontFiles() const
502{
503 return &m_fontFiles;
504}
505
506
507const std::vector<wxString>* EMBEDDED_FILES::UpdateFontFiles()
508{
509 m_fontFiles.clear();
510
511 for( const auto& [name, entry] : m_files )
512 {
513 if( entry->type == EMBEDDED_FILE::FILE_TYPE::FONT )
514 m_fontFiles.push_back( GetTemporaryFileName( name ).GetFullPath() );
515 }
516
517 return &m_fontFiles;
518}
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.
@ 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 ClearEmbeddedFonts()
Remove all embedded fonts from the collection.
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...
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
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 MIME_BASE64_LENGTH
#define THROW_PARSE_ERROR(aProblem, aSource, aInputLine, aLineNumber, aByteIndex)
Definition: ki_exception.h:165
std::vector< char > decompressedData
std::string ToString() const
Definition: hash_128.h:47