|
KiCad PCB EDA Suite
|
Low-level binary reader for DipTrace file formats. More...
#include <diptrace_binary_reader.h>
Public Member Functions | |
| BINARY_READER (const wxString &aFileName) | |
| Construct a reader by loading the given file into memory. | |
| ~BINARY_READER () | |
| size_t | GetOffset () const |
| Return the current byte offset within the file. | |
| void | SetOffset (size_t aOffset) |
| Set the read position to an absolute byte offset. | |
| void | Skip (size_t aBytes) |
| Advance the read position by the given number of bytes. | |
| size_t | GetFileSize () const |
| Return the total size of the loaded file in bytes. | |
| bool | IsEOF () const |
| Return true if the read position has reached or passed the end of file. | |
| const uint8_t * | GetData () const |
| Return a pointer to the raw file data buffer. | |
| void | SetVersion (int aVersion) |
| Set the DipTrace format version. | |
| int | GetVersion () const |
| Return the currently configured format version. | |
| void | SetStringEncoding (STRING_ENCODING aEncoding) |
| Override DipTrace string decoding when a file family has a deterministic string-encoding cutover that differs from the shared version threshold. | |
| void | DetectStringEncoding (size_t aProbeOffset) |
Detect the string encoding from the bytes at aProbeOffset, which must sit at the start of a non-empty DipTrace string, and pin the encoding override accordingly. | |
| uint8_t | ReadByte () |
| Read a single unsigned byte and advance the position by 1. | |
| int | ReadInt3 () |
| Read a 3-byte big-endian biased integer (bias 1,000,000) and advance the position by 3. | |
| int | ReadInt4 () |
| Read a 4-byte big-endian biased integer (bias 1,000,000,000) and advance the position by 4. | |
| wxString | ReadString () |
| Read a string using the configured encoding. | |
| void | ReadColor (uint8_t &r, uint8_t &g, uint8_t &b) |
| Read a 3-byte RGB color value. | |
| void | ReadBytes (uint8_t *aDst, size_t aCount) |
| Read a block of raw bytes into the caller's buffer. | |
| int | PeekInt3 () const |
| Peek at the next 3-byte biased integer without advancing the position. | |
| int | PeekInt4 () const |
| Peek at the next 4-byte biased integer without advancing the position. | |
| uint8_t | PeekByte () const |
| Peek at the next byte without advancing the position. | |
| 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. | |
| 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. | |
| bool | TryReadString (wxString &aResult) |
| Attempt to read a string at the current position. | |
Static Public Member Functions | |
| static int | DipTraceToKiCadNm (int aDipTraceCoord) |
| Convert a DipTrace coordinate value (10 nm units) to KiCad nanometers. | |
| static double | DipTraceToMM (int aDipTraceCoord) |
| Convert a DipTrace coordinate value (10 nm units) to millimeters. | |
Private Member Functions | |
| wxString | ReadStringUTF16 () |
| Read a v39+ UTF-16-BE string: uint16-BE char count + UTF-16-BE data. | |
| wxString | ReadStringASCII () |
| Read a v37 legacy ASCII string: int3(byte_count) + raw ASCII bytes. | |
| bool | TryReadStringUTF16 (wxString &aResult) |
| Attempt to read a UTF-16-BE string with validation. | |
| bool | TryReadStringASCII (wxString &aResult) |
| Attempt to read a legacy ASCII string with validation. | |
| void | ThrowEOFError (size_t aBytesNeeded) const |
| Throw IO_ERROR with a message indicating a read past end of file. | |
Static Private Member Functions | |
| static bool | IsPrintableString (const wxString &aStr) |
| Verify that all characters in aStr are printable or common whitespace (space, tab, CR, LF). | |
Private Attributes | |
| std::vector< uint8_t > | m_data |
| Entire file contents loaded into memory. | |
| size_t | m_offset |
| Current read position (byte offset). | |
| int | m_version |
| DipTrace format version. | |
| STRING_ENCODING | m_stringEncoding |
| Explicit string encoding override. | |
Low-level binary reader for DipTrace file formats.
DipTrace files (.dip for PCB, .dch for schematic) use a proprietary big-endian binary encoding with biased integers. This reader loads the entire file into memory and provides methods to decode each primitive type.
Shared by both the PCB and schematic importers.
Definition at line 85 of file diptrace_binary_reader.h.
| BINARY_READER::BINARY_READER | ( | const wxString & | aFileName | ) |
Construct a reader by loading the given file into memory.
| aFileName | path to the DipTrace binary file. |
| IO_ERROR | if the file cannot be opened or read. |
Definition at line 38 of file diptrace_binary_reader.cpp.
References _, DIPTRACE::BY_VERSION, m_data, m_offset, m_stringEncoding, m_version, and THROW_IO_ERROR.
| BINARY_READER::~BINARY_READER | ( | ) |
Definition at line 74 of file diptrace_binary_reader.cpp.
| void BINARY_READER::DetectStringEncoding | ( | size_t | aProbeOffset | ) |
Detect the string encoding from the bytes at aProbeOffset, which must sit at the start of a non-empty DipTrace string, and pin the encoding override accordingly.
DipTrace ships files of the same format version in both encodings (e.g. v37 schematics exist as legacy ASCII and as UTF-16-BE), so the version threshold alone is not reliable. The two framings are mutually exclusive on real string bytes – the ASCII length is the int3 bias 0x0F42xx while a small UTF-16 char count starts 0x00xx – so exactly one parses as a printable string. The encoding is left unchanged when the probe is inconclusive.
| aProbeOffset | file offset of a known non-empty string field. |
Definition at line 323 of file diptrace_binary_reader.cpp.
References DIPTRACE::LEGACY_ASCII, m_offset, m_stringEncoding, TryReadStringASCII(), TryReadStringUTF16(), and DIPTRACE::UTF16_BE.
|
static |
Convert a DipTrace coordinate value (10 nm units) to KiCad nanometers.
| aDipTraceCoord | coordinate in DipTrace units. |
Definition at line 234 of file diptrace_binary_reader.cpp.
|
static |
Convert a DipTrace coordinate value (10 nm units) to millimeters.
| aDipTraceCoord | coordinate in DipTrace units. |
Definition at line 240 of file diptrace_binary_reader.cpp.
References DIPTRACE::DIPTRACE_COORD_TO_MM.
| size_t BINARY_READER::FindPattern | ( | const uint8_t * | aPattern, |
| size_t | aPatternLen, | ||
| size_t | aStart, | ||
| size_t | aEnd ) const |
Search for a byte pattern in the file data.
| aPattern | pointer to the pattern bytes. |
| aPatternLen | length of the pattern in bytes. |
| aStart | byte offset to begin searching (inclusive). |
| aEnd | byte offset to stop searching (exclusive). Use 0 to search to end of file. |
std::string::npos if not found. Definition at line 249 of file diptrace_binary_reader.cpp.
References m_data.
Referenced by FindString().
| size_t BINARY_READER::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.
| aStr | the string to search for. |
| aStart | byte offset to begin searching. |
| aEnd | byte offset to stop searching. Use 0 to search to end of file. |
std::string::npos if not found. Definition at line 270 of file diptrace_binary_reader.cpp.
References FindPattern().
|
inline |
Return a pointer to the raw file data buffer.
Definition at line 126 of file diptrace_binary_reader.h.
References m_data.
|
inline |
Return the total size of the loaded file in bytes.
Definition at line 120 of file diptrace_binary_reader.h.
References m_data.
|
inline |
Return the current byte offset within the file.
Definition at line 101 of file diptrace_binary_reader.h.
References m_offset.
|
inline |
Return the currently configured format version.
Definition at line 142 of file diptrace_binary_reader.h.
References m_version.
|
inline |
Return true if the read position has reached or passed the end of file.
Definition at line 123 of file diptrace_binary_reader.h.
|
staticprivate |
Verify that all characters in aStr are printable or common whitespace (space, tab, CR, LF).
Definition at line 510 of file diptrace_binary_reader.cpp.
Referenced by TryReadStringASCII(), and TryReadStringUTF16().
| uint8_t BINARY_READER::PeekByte | ( | ) | const |
Peek at the next byte without advancing the position.
| IO_ERROR | if at end of file. |
Definition at line 219 of file diptrace_binary_reader.cpp.
References _, m_data, m_offset, and THROW_IO_ERROR.
| int BINARY_READER::PeekInt3 | ( | ) | const |
Peek at the next 3-byte biased integer without advancing the position.
| IO_ERROR | if there are fewer than 3 bytes remaining. |
Definition at line 182 of file diptrace_binary_reader.cpp.
References _, DIPTRACE::INT3_BIAS, m_data, m_offset, and THROW_IO_ERROR.
| int BINARY_READER::PeekInt4 | ( | ) | const |
Peek at the next 4-byte biased integer without advancing the position.
| IO_ERROR | if there are fewer than 4 bytes remaining. |
Definition at line 200 of file diptrace_binary_reader.cpp.
References _, DIPTRACE::INT4_BIAS, m_data, m_offset, and THROW_IO_ERROR.
| uint8_t BINARY_READER::ReadByte | ( | ) |
Read a single unsigned byte and advance the position by 1.
| IO_ERROR | on read past end of file. |
Definition at line 106 of file diptrace_binary_reader.cpp.
References m_data, m_offset, and ThrowEOFError().
Referenced by ReadColor().
| void BINARY_READER::ReadBytes | ( | uint8_t * | aDst, |
| size_t | aCount ) |
Read a block of raw bytes into the caller's buffer.
| aDst | destination buffer (must hold at least aCount bytes). |
| aCount | number of bytes to read. |
| IO_ERROR | on read past end of file. |
Definition at line 169 of file diptrace_binary_reader.cpp.
References m_data, m_offset, and ThrowEOFError().
| void BINARY_READER::ReadColor | ( | uint8_t & | r, |
| uint8_t & | g, | ||
| uint8_t & | b ) |
Read a 3-byte RGB color value.
| [out] | r | red component (0-255). |
| [out] | g | green component (0-255). |
| [out] | b | blue component (0-255). |
| IO_ERROR | on read past end of file. |
Definition at line 161 of file diptrace_binary_reader.cpp.
References ReadByte().
Referenced by ReadColorPacked().
| int BINARY_READER::ReadInt3 | ( | ) |
Read a 3-byte big-endian biased integer (bias 1,000,000) and advance the position by 3.
Decoding: value = (byte[0]<<16 | byte[1]<<8 | byte[2]) - 1,000,000
| IO_ERROR | on read past end of file. |
Definition at line 117 of file diptrace_binary_reader.cpp.
References DIPTRACE::INT3_BIAS, m_data, m_offset, and ThrowEOFError().
Referenced by ReadStringASCII().
| int BINARY_READER::ReadInt4 | ( | ) |
Read a 4-byte big-endian biased integer (bias 1,000,000,000) and advance the position by 4.
Decoding: value = (byte[0]<<24 | byte[1]<<16 | byte[2]<<8 | byte[3])
| IO_ERROR | on read past end of file. |
Definition at line 131 of file diptrace_binary_reader.cpp.
References DIPTRACE::INT4_BIAS, m_data, m_offset, and ThrowEOFError().
| wxString BINARY_READER::ReadString | ( | ) |
Read a string using the configured encoding.
| IO_ERROR | on read past end of file or corrupt string data. |
Definition at line 146 of file diptrace_binary_reader.cpp.
References DIPTRACE::LEGACY_ASCII, DIPTRACE::LEGACY_STRING_VERSION, m_stringEncoding, m_version, ReadStringASCII(), ReadStringUTF16(), and DIPTRACE::UTF16_BE.
|
private |
Read a v37 legacy ASCII string: int3(byte_count) + raw ASCII bytes.
Definition at line 388 of file diptrace_binary_reader.cpp.
References _, m_data, m_offset, DIPTRACE::MAX_STRING_CHARS, ReadInt3(), result, THROW_IO_ERROR, and ThrowEOFError().
Referenced by ReadString().
|
private |
Read a v39+ UTF-16-BE string: uint16-BE char count + UTF-16-BE data.
Definition at line 354 of file diptrace_binary_reader.cpp.
References _, m_data, m_offset, DIPTRACE::MAX_STRING_CHARS, result, THROW_IO_ERROR, and ThrowEOFError().
Referenced by ReadString().
| void BINARY_READER::SetOffset | ( | size_t | aOffset | ) |
Set the read position to an absolute byte offset.
| aOffset | byte offset from the start of the file. |
| IO_ERROR | if aOffset is beyond the end of the file. |
Definition at line 82 of file diptrace_binary_reader.cpp.
References _, m_data, m_offset, and THROW_IO_ERROR.
|
inline |
Override DipTrace string decoding when a file family has a deterministic string-encoding cutover that differs from the shared version threshold.
Definition at line 148 of file diptrace_binary_reader.h.
References m_stringEncoding.
|
inline |
Set the DipTrace format version.
The version affects default string decoding: versions <= 37 use the legacy int3+ASCII encoding while versions >= 39 use uint16+UTF-16-BE. File-family parsers can override this with SetStringEncoding().
| aVersion | DipTrace format version number. |
Definition at line 139 of file diptrace_binary_reader.h.
References m_version.
| void BINARY_READER::Skip | ( | size_t | aBytes | ) |
Advance the read position by the given number of bytes.
| aBytes | number of bytes to skip. |
| IO_ERROR | if skipping would read past the end of the file. |
Definition at line 94 of file diptrace_binary_reader.cpp.
References m_data, m_offset, and ThrowEOFError().
|
private |
Throw IO_ERROR with a message indicating a read past end of file.
| aBytesNeeded | number of bytes the caller tried to read. |
Definition at line 527 of file diptrace_binary_reader.cpp.
References _, m_data, m_offset, and THROW_IO_ERROR.
Referenced by ReadByte(), ReadBytes(), ReadInt3(), ReadInt4(), ReadStringASCII(), ReadStringUTF16(), and Skip().
| bool BINARY_READER::TryReadString | ( | wxString & | aResult | ) |
Attempt to read a string at the current position.
On success, the position is advanced past the string and aResult is populated. On failure the position is restored and false is returned.
Uses the version-appropriate encoding, with additional sanity checks: character count must be <= 500, and all decoded characters must be printable or whitespace.
| [out] | aResult | receives the decoded string on success. |
Definition at line 308 of file diptrace_binary_reader.cpp.
References DIPTRACE::LEGACY_ASCII, DIPTRACE::LEGACY_STRING_VERSION, m_stringEncoding, m_version, TryReadStringASCII(), TryReadStringUTF16(), and DIPTRACE::UTF16_BE.
|
private |
Attempt to read a legacy ASCII string with validation.
Returns false and restores the position on failure.
Definition at line 461 of file diptrace_binary_reader.cpp.
References DIPTRACE::INT3_BIAS, IsPrintableString(), m_data, and m_offset.
Referenced by DetectStringEncoding(), and TryReadString().
|
private |
Attempt to read a UTF-16-BE string with validation.
Returns false and restores the position on failure.
Definition at line 414 of file diptrace_binary_reader.cpp.
References IsPrintableString(), m_data, and m_offset.
Referenced by DetectStringEncoding(), and TryReadString().
|
private |
Entire file contents loaded into memory.
Definition at line 349 of file diptrace_binary_reader.h.
Referenced by BINARY_READER(), FindPattern(), GetData(), GetFileSize(), IsEOF(), PeekByte(), PeekInt3(), PeekInt4(), ReadByte(), ReadBytes(), ReadInt3(), ReadInt4(), ReadStringASCII(), ReadStringUTF16(), SetOffset(), Skip(), ThrowEOFError(), TryReadStringASCII(), and TryReadStringUTF16().
|
private |
Current read position (byte offset).
Definition at line 350 of file diptrace_binary_reader.h.
Referenced by BINARY_READER(), DetectStringEncoding(), GetOffset(), IsEOF(), PeekByte(), PeekInt3(), PeekInt4(), ReadByte(), ReadBytes(), ReadInt3(), ReadInt4(), ReadStringASCII(), ReadStringUTF16(), SetOffset(), Skip(), ThrowEOFError(), TryReadStringASCII(), and TryReadStringUTF16().
|
private |
Explicit string encoding override.
Definition at line 352 of file diptrace_binary_reader.h.
Referenced by BINARY_READER(), DetectStringEncoding(), ReadString(), SetStringEncoding(), and TryReadString().
|
private |
DipTrace format version.
Definition at line 351 of file diptrace_binary_reader.h.
Referenced by BINARY_READER(), GetVersion(), ReadString(), SetVersion(), and TryReadString().