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, you may find one here:
18* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19* or you may search the http://www.gnu.org website for the version 2 license,
20* or you may write to the Free Software Foundation, Inc.,
21* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22*/
23
24#include "io_utils.h"
25
26#include <wx/wfstream.h>
27#include <wx/txtstrm.h>
28
29#include <mmh3_hash.h>
30
31namespace IO_UTILS
32{
33 const std::vector<uint8_t> COMPOUND_FILE_HEADER = { 0xD0, 0xCF, 0x11, 0xE0,
34 0xA1, 0xB1, 0x1A, 0xE1 };
35
36bool fileStartsWithPrefix( const wxString& aFilePath, const wxString& aPrefix,
37 bool aIgnoreWhitespace )
38{
39 wxFFileInputStream input( aFilePath );
40
41 if( input.IsOk() && !input.Eof() )
42 {
43 // Find first non-empty line
44 wxTextInputStream text( input );
45 wxString line = text.ReadLine();
46
47 if( aIgnoreWhitespace )
48 {
49 while( !input.Eof() && line.IsEmpty() )
50 line = text.ReadLine().Trim( false /*trim from left*/ );
51 }
52
53 if( line.StartsWith( aPrefix ) )
54 return true;
55 }
56
57 return false;
58}
59
60
61bool fileHasBinaryHeader( const wxString& aFilePath, const std::vector<uint8_t>& aHeader,
62 size_t aOffset )
63{
64 wxFFileInputStream input( aFilePath );
65
66 if( input.IsOk() && !input.Eof() )
67 {
68 if( static_cast<size_t>( input.GetLength() ) < aOffset + aHeader.size() )
69 return false;
70
71 // Move the stream to the offset
72 if( aOffset )
73 {
74 if( !input.SeekI( aOffset, wxFromStart ) )
75 return false;
76 }
77
78 std::vector<uint8_t> parsedHeader( aHeader.size() );
79
80 if( !input.ReadAll( parsedHeader.data(), parsedHeader.size() ) )
81 return false;
82
83 return parsedHeader == aHeader;
84 }
85
86 return false;
87}
88
89
90std::optional<wxString> fileHashMMH3( const wxString& aFilePath )
91{
92 constexpr size_t c_BufSize = 1024;
93 wxFFileInputStream input( aFilePath );
94 MMH3_HASH hash( 0x68AF835D ); // Arbitrary seed
95
96 if( input.IsOk() )
97 {
98 uint8_t buf[ c_BufSize ];
99 while ( !input.Eof() )
100 {
101 input.Read( buf, c_BufSize);
102 size_t byteCount = input.LastRead();
103 if( byteCount > 0 )
104 {
105 hash.addData( buf, byteCount );
106 }
107 else
108 {
109 break;
110 }
111 }
112
113 return hash.digest().ToString();
114 }
115
116 return std::optional<wxString>();
117}
118
119
120}
A streaming C++ equivalent for MurmurHash3_x64_128.
Definition mmh3_hash.h:60
FORCE_INLINE void addData(const uint8_t *data, size_t length)
Definition mmh3_hash.h:73
FORCE_INLINE HASH_128 digest()
Definition mmh3_hash.h:140
std::optional< wxString > fileHashMMH3(const wxString &aFilePath)
Calculates an MMH3 hash of a given file.
Definition io_utils.cpp:90
bool fileStartsWithPrefix(const wxString &aFilePath, const wxString &aPrefix, bool aIgnoreWhitespace)
Check if a file starts with a defined string.
Definition io_utils.cpp:36
const std::vector< uint8_t > COMPOUND_FILE_HEADER
Definition io_utils.cpp:33
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:61
std::string ToString() const
Definition hash_128.h:47