KiCad PCB EDA Suite
Loading...
Searching...
No Matches
diptrace_binary_reader.h
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#ifndef DIPTRACE_BINARY_READER_H
25#define DIPTRACE_BINARY_READER_H
26
27#include <cstddef>
28#include <cstdint>
29#include <vector>
30
31#include <wx/string.h>
32
33
34namespace DIPTRACE
35{
36
43
46constexpr int INT3_BIAS = 1000000;
47
50constexpr int INT4_BIAS = 1000000000;
51
55constexpr double DIPTRACE_COORD_TO_MM = 1.0 / 30000.0;
56
58constexpr double DIPTRACE_ANGLE_TO_DEG = 0.00001;
59
68constexpr int LEGACY_STRING_VERSION = 37;
69
72constexpr int MAX_STRING_CHARS = 10000;
73
74
86{
87public:
94 BINARY_READER( const wxString& aFileName );
95
97
98 // -- Position management -------------------------------------------------
99
101 size_t GetOffset() const { return m_offset; }
102
109 void SetOffset( size_t aOffset );
110
117 void Skip( size_t aBytes );
118
120 size_t GetFileSize() const { return m_data.size(); }
121
123 bool IsEOF() const { return m_offset >= m_data.size(); }
124
126 const uint8_t* GetData() const { return m_data.data(); }
127
128 // -- Version management --------------------------------------------------
129
139 void SetVersion( int aVersion ) { m_version = aVersion; }
140
142 int GetVersion() const { return m_version; }
143
148 void SetStringEncoding( STRING_ENCODING aEncoding ) { m_stringEncoding = aEncoding; }
149
162 void DetectStringEncoding( size_t aProbeOffset );
163
164 // -- Primitive readers ---------------------------------------------------
165
171 uint8_t ReadByte();
172
182 int ReadInt3();
183
194 int ReadInt4();
195
205 wxString ReadString();
206
215 void ReadColor( uint8_t& r, uint8_t& g, uint8_t& b );
216
224 void ReadBytes( uint8_t* aDst, size_t aCount );
225
226 // -- Peek methods (do not advance position) ------------------------------
227
233 int PeekInt3() const;
234
240 int PeekInt4() const;
241
247 uint8_t PeekByte() const;
248
249 // -- Coordinate conversion helpers ---------------------------------------
250
257 static int DipTraceToKiCadNm( int aDipTraceCoord );
258
265 static double DipTraceToMM( int aDipTraceCoord );
266
267 // -- Search helpers ------------------------------------------------------
268
280 size_t FindPattern( const uint8_t* aPattern, size_t aPatternLen,
281 size_t aStart, size_t aEnd ) const;
282
294 size_t FindString( const wxString& aStr, size_t aStart, size_t aEnd ) const;
295
296 // -- Try-read methods (return false instead of throwing) ------------------
297
311 bool TryReadString( wxString& aResult );
312
313private:
317 wxString ReadStringUTF16();
318
322 wxString ReadStringASCII();
323
328 bool TryReadStringUTF16( wxString& aResult );
329
334 bool TryReadStringASCII( wxString& aResult );
335
340 static bool IsPrintableString( const wxString& aStr );
341
347 void ThrowEOFError( size_t aBytesNeeded ) const;
348
349 std::vector<uint8_t> m_data;
350 size_t m_offset;
353};
354
355} // namespace DIPTRACE
356
357#endif // DIPTRACE_BINARY_READER_H
bool TryReadStringUTF16(wxString &aResult)
Attempt to read a UTF-16-BE string with validation.
const uint8_t * GetData() const
Return a pointer to the raw file data buffer.
int GetVersion() const
Return the currently configured format version.
size_t FindString(const wxString &aStr, size_t aStart, size_t aEnd) const
Search for a UTF-16-BE encoded string in the file data, including its two-byte length prefix.
int PeekInt3() const
Peek at the next 3-byte biased integer without advancing the position.
wxString ReadStringASCII()
Read a v37 legacy ASCII string: int3(byte_count) + raw ASCII bytes.
bool TryReadStringASCII(wxString &aResult)
Attempt to read a legacy ASCII string with validation.
void ReadBytes(uint8_t *aDst, size_t aCount)
Read a block of raw bytes into the caller's buffer.
int m_version
DipTrace format version.
void Skip(size_t aBytes)
Advance the read position by the given number of bytes.
uint8_t ReadByte()
Read a single unsigned byte and advance the position by 1.
void SetVersion(int aVersion)
Set the DipTrace format version.
size_t m_offset
Current read position (byte offset).
uint8_t PeekByte() const
Peek at the next byte without advancing the position.
void ThrowEOFError(size_t aBytesNeeded) const
Throw IO_ERROR with a message indicating a read past end of file.
void DetectStringEncoding(size_t aProbeOffset)
Detect the string encoding from the bytes at aProbeOffset, which must sit at the start of a non-empty...
STRING_ENCODING m_stringEncoding
Explicit string encoding override.
static bool IsPrintableString(const wxString &aStr)
Verify that all characters in aStr are printable or common whitespace (space, tab,...
void ReadColor(uint8_t &r, uint8_t &g, uint8_t &b)
Read a 3-byte RGB color value.
int ReadInt4()
Read a 4-byte big-endian biased integer (bias 1,000,000,000) and advance the position by 4.
int ReadInt3()
Read a 3-byte big-endian biased integer (bias 1,000,000) and advance the position by 3.
size_t GetFileSize() const
Return the total size of the loaded file in bytes.
static int DipTraceToKiCadNm(int aDipTraceCoord)
Convert a DipTrace coordinate value (10 nm units) to KiCad nanometers.
size_t GetOffset() const
Return the current byte offset within the file.
bool IsEOF() const
Return true if the read position has reached or passed the end of file.
size_t FindPattern(const uint8_t *aPattern, size_t aPatternLen, size_t aStart, size_t aEnd) const
Search for a byte pattern in the file data.
void SetStringEncoding(STRING_ENCODING aEncoding)
Override DipTrace string decoding when a file family has a deterministic string-encoding cutover that...
void SetOffset(size_t aOffset)
Set the read position to an absolute byte offset.
bool TryReadString(wxString &aResult)
Attempt to read a string at the current position.
wxString ReadStringUTF16()
Read a v39+ UTF-16-BE string: uint16-BE char count + UTF-16-BE data.
BINARY_READER(const wxString &aFileName)
Construct a reader by loading the given file into memory.
int PeekInt4() const
Peek at the next 4-byte biased integer without advancing the position.
static double DipTraceToMM(int aDipTraceCoord)
Convert a DipTrace coordinate value (10 nm units) to millimeters.
wxString ReadString()
Read a string using the configured encoding.
std::vector< uint8_t > m_data
Entire file contents loaded into memory.
constexpr double DIPTRACE_COORD_TO_MM
DipTrace uses 762 units per mil (30 000 units per mm).
constexpr int MAX_STRING_CHARS
Maximum sane string length (in characters) accepted by the reader.
constexpr int INT4_BIAS
Bias value added to stored 4-byte unsigned integers.
constexpr int LEGACY_STRING_VERSION
Format version at or below which strings use the legacy ASCII encoding (int3 byte-count + raw ASCII b...
constexpr int INT3_BIAS
Bias value added to stored 3-byte unsigned integers.
constexpr double DIPTRACE_ANGLE_TO_DEG
DipTrace stores angles with 100 000 units per degree.