RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
reader.h
1#ifndef READER_H
2#define READER_H
3
4#include <cstring>
5#include <iostream>
6#include <string>
7#include <vector>
8#include "../common.h"
9
10namespace Linker
11{
13 {
14 };
15
19 class Reader
20 {
21 public:
25 EndianType endiantype;
29 std::istream * in;
30
31 enum OverflowHandlingMode
32 {
33 IgnoreOnOverflow,
34 ReportOnOverflow,
35 TerminateOnOverflow,
36 };
37 OverflowHandlingMode on_overflow = TerminateOnOverflow;
38
39 const offset_t start_offset;
40 const offset_t maximum_size;
41
42 Reader(EndianType endiantype, std::istream * in = nullptr)
43 : endiantype(endiantype), in(in), start_offset(0), maximum_size(offset_t(-1))
44 {
45 }
46
47 Reader(offset_t start_offset, offset_t maximum_size, EndianType endiantype, std::istream * in = nullptr)
48 : endiantype(endiantype), in(in), start_offset(start_offset), maximum_size(maximum_size)
49 {
50 }
51
52 Reader CreateWindow(offset_t new_start_offset, offset_t new_maximum_size = offset_t(-1));
53
57 void ReadData(size_t count, void * data);
58
62 void ReadData(size_t count, std::vector<uint8_t>& data, size_t offset = 0);
63
67 void ReadData(std::vector<uint8_t>& data, size_t offset = 0);
68
72 std::string ReadData(size_t count, bool terminate_at_null = false);
73
77 std::string ReadUTF16Data(size_t count, bool terminate_at_null = false);
78
82 template <class T, std::size_t N>
83 void ReadData(std::array<T, N>& data, size_t offset = 0)
84 {
85 if(offset >= N)
86 return;
87 ReadData(N - offset, reinterpret_cast<char *>(data.data()) + offset);
88 }
89
93 std::string ReadASCII(char terminator, size_t maximum = size_t(-1));
94
98 std::string ReadASCIIZ(size_t maximum = size_t(-1));
99
103 std::string ReadUTF16Data(const char terminator[2], size_t maximum = size_t(-1));
104
108 std::string ReadUTF16Data(char16_t terminator, size_t maximum, EndianType endiantype);
109
113 std::string ReadUTF16Data(char16_t terminator, size_t maximum = size_t(-1));
114
118 std::string ReadUTF16Data(char16_t terminator, EndianType endiantype);
119
123 std::string ReadUTF16ZData(size_t maximum = size_t(-1));
124
128 uint64_t ReadUnsigned(size_t bytes, EndianType endiantype);
129
133 uint64_t ReadUnsigned(size_t bytes);
134
138 uint64_t ReadSigned(size_t bytes, EndianType endiantype);
139
143 uint64_t ReadSigned(size_t bytes);
144
148 void Seek(offset_t offset);
149
153 void Skip(offset_t offset);
154
158 void SeekEnd(relative_offset_t offset = 0);
159
163 offset_t Tell();
164
168 offset_t GetImageEnd();
169
173 offset_t GetRemainingCount();
174 };
175}
176
177#endif /* READER_H */
Definition reader.h:13
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:20
void Skip(offset_t offset)
Jump to a distance in the input stream.
Definition reader.cc:189
std::string ReadASCIIZ(size_t maximum=size_t(-1))
Read a zero terminated ASCII string.
Definition reader.cc:93
std::string ReadASCII(char terminator, size_t maximum=size_t(-1))
Read an ASCII string up to a terminator.
Definition reader.cc:78
void Seek(offset_t offset)
Jump to a specific location in the input stream.
Definition reader.cc:179
offset_t GetImageEnd()
Returns the last location that can be read.
Definition reader.cc:262
std::string ReadUTF16Data(size_t count, bool terminate_at_null=false)
Read in a sequence of 16-bit words.
Definition reader.cc:98
uint64_t ReadSigned(size_t bytes, EndianType endiantype)
Read a signed word.
Definition reader.cc:167
void SeekEnd(relative_offset_t offset=0)
Jump to end of the input stream.
Definition reader.cc:211
EndianType endiantype
The default endianness of the binary format, used for reading multibyte numeric data.
Definition reader.h:25
std::string ReadUTF16ZData(size_t maximum=size_t(-1))
Read a zero terminated string of 16-bit words.
Definition reader.cc:149
std::istream * in
The input stream.
Definition reader.h:29
void ReadData(size_t count, void *data)
Read in a sequence of bytes.
Definition reader.cc:30
offset_t GetRemainingCount()
Returns the byte count until the last location that can be read.
Definition reader.cc:278
void ReadData(std::array< T, N > &data, size_t offset=0)
Read in a sequence of bytes.
Definition reader.h:83
offset_t Tell()
Retrieve the current location.
Definition reader.cc:233
uint64_t ReadUnsigned(size_t bytes, EndianType endiantype)
Read an unsigned word.
Definition reader.cc:155