KiCad PCB EDA Suite
Loading...
Searching...
No Matches
io_utils.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
7* modify it under the terms of the GNU General Public License
8* as published by the Free Software Foundation; either version 2
9* of the License, or (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20#include "io_utils.h"
21
22#include <wx/log.h>
23#include <wx/wfstream.h>
24#include <wx/txtstrm.h>
25
26#include <mmh3_hash.h>
27
28namespace IO_UTILS
29{
30 const std::vector<uint8_t> COMPOUND_FILE_HEADER = { 0xD0, 0xCF, 0x11, 0xE0,
31 0xA1, 0xB1, 0x1A, 0xE1 };
32
33bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix,
34 bool aIgnoreWhitespace )
35{
36 // A probe answers false for a missing or unreadable file rather than logging an error
37 wxLogNull noLog;
38 wxFFileInputStream input( aFilePath );
39
40 if( input.IsOk() && !input.Eof() )
41 {
42 // Find first non-empty line
43 wxTextInputStream text( input );
44 wxString line = text.ReadLine();
45
46 if( aIgnoreWhitespace )
47 {
48 while( !input.Eof() && line.IsEmpty() )
49 line = text.ReadLine().Trim( false /*trim from left*/ );
50 }
51
52 if( line.StartsWith( aPrefix ) )
53 return true;
54 }
55
56 return false;
57}
58
59
60bool fileHasBinaryHeader( const wxString& aFilePath, const std::vector<uint8_t>& aHeader,
61 size_t aOffset )
62{
63 // A probe answers false for a missing or unreadable file rather than logging an error
64 wxLogNull noLog;
65 wxFFileInputStream input( aFilePath );
66
67 if( input.IsOk() && !input.Eof() )
68 {
69 if( static_cast<size_t>( input.GetLength() ) < aOffset + aHeader.size() )
70 return false;
71
72 // Move the stream to the offset
73 if( aOffset )
74 {
75 if( !input.SeekI( aOffset, wxFromStart ) )
76 return false;
77 }
78
79 std::vector<uint8_t> parsedHeader( aHeader.size() );
80
81 if( !input.ReadAll( parsedHeader.data(), parsedHeader.size() ) )
82 return false;
83
84 return parsedHeader == aHeader;
85 }
86
87 return false;
88}
89
90
91std::optional<wxString> fileHashMMH3( const wxString& aFilePath )
92{
93 constexpr size_t c_BufSize = 1024;
94 wxFFileInputStream input( aFilePath );
95 MMH3_HASH hash( 0x68AF835D ); // Arbitrary seed
96
97 if( input.IsOk() )
98 {
99 uint8_t buf[ c_BufSize ];
100 while ( !input.Eof() )
101 {
102 input.Read( buf, c_BufSize);
103 size_t byteCount = input.LastRead();
104 if( byteCount > 0 )
105 {
106 hash.addData( buf, byteCount );
107 }
108 else
109 {
110 break;
111 }
112 }
113
114 return hash.digest().ToString();
115 }
116
117 return std::optional<wxString>();
118}
119
120
121}
A streaming C++ equivalent for MurmurHash3_x64_128.
Definition mmh3_hash.h:56
FORCE_INLINE void addData(const uint8_t *data, size_t length)
Definition mmh3_hash.h:69
FORCE_INLINE HASH_128 digest()
Definition mmh3_hash.h:136
std::optional< wxString > fileHashMMH3(const wxString &aFilePath)
Calculates an MMH3 hash of a given file.
Definition io_utils.cpp:91
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition io_utils.cpp:33
const std::vector< uint8_t > COMPOUND_FILE_HEADER
Definition io_utils.cpp:30
bool fileHasBinaryHeader(const wxString &aFilePath, const std::vector< uint8_t > &aHeader, size_t aOffset)
Check if a file starts with a defined binary header.
Definition io_utils.cpp:60
std::string ToString() const
Definition hash_128.h:43