This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
DataFile.h
143 lines (117 loc) · 3.79 KB
/
DataFile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
DataFile.h ... Memory-mapped file class
Copyright (C) 2009 KennyTM~
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DATAFILE_H
#define DATAFILE_H
#include <cstdio>
#include <exception>
#include <new>
#include <string>
class TRException : public std::exception {
private:
char* m_error;
public:
TRException(const char* format, ...);
~TRException() throw();
inline const char* what() const throw() { return m_error; }
};
class DataFile {
protected:
unsigned char* m_data;
off_t m_filesize;
int m_fd;
off_t m_location;
public:
DataFile(const char* path);
inline const unsigned char* data() const throw() { return m_data; }
inline off_t filesize() const throw() { return m_filesize; }
inline void seek(off_t new_location) throw() { m_location = new_location; }
inline off_t tell() const throw() { return m_location; }
inline void advance(off_t delta) throw() { m_location += delta; }
inline void retreat(off_t neg_delta) throw() { m_location -= neg_delta; }
inline void rewind() throw() { m_location = 0; }
inline bool is_eof() const throw() { return m_location == m_filesize; }
unsigned read_integer() throw();
char read_char() throw() { return static_cast<char>(m_data[m_location++]); }
const char* read_string(std::size_t* p_string_length = NULL) throw();
const char* read_ASCII_string(std::size_t* p_string_length = NULL) throw();
const unsigned char* read_raw_data(std::size_t data_size) throw();
const char* peek_ASCII_Cstring_at(off_t offset, std::size_t* p_string_length = NULL) const throw();
inline const char* peek_ASCII_Cstring(std::size_t* p_string_length = NULL) const throw() {
return this->peek_ASCII_Cstring_at(m_location, p_string_length);
}
template<typename T>
const T* read_data() throw() {
if (m_location + static_cast<off_t>(sizeof(T)) <= m_filesize) {
const T* retval = reinterpret_cast<const T*>(m_data + m_location);
m_location += sizeof(T);
return retval;
} else {
return NULL;
}
}
template<typename T>
T copy_data() throw() { return *(this->read_data<T>()); }
template<typename T>
inline const T* peek_data(unsigned items_after = 0) throw() {
if (m_location+static_cast<off_t>((1+items_after)*sizeof(T)) <= m_filesize) {
return reinterpret_cast<const T*>(m_data + m_location) + items_after;
} else
return NULL;
}
template<typename T>
inline const T* peek_data_at(off_t offset) const throw() {
if (offset+static_cast<off_t>(sizeof(T)) <= m_filesize) {
return reinterpret_cast<const T*>(m_data + offset);
} else
return NULL;
}
template<typename T>
T read_uleb128() throw() {
T res = 0;
int bit = 0;
unsigned char c;
do {
c = static_cast<unsigned char>(read_char());
T s = c & 0x7F;
res |= s << bit;
bit += 7;
} while (c & 0x80);
return res;
}
template<typename T>
T read_sleb128() throw() {
T res = 0;
int bit = 0;
signed char c;
do {
c = read_char();
T s = c & 0x7F;
res |= s << bit;
bit += 7;
} while (c & 0x80);
if (c & 0x40) {
T n1 = -1;
res |= n1 << bit;
}
return res;
}
bool search_forward(const unsigned char* data, size_t length) throw();
~DataFile() throw();
};
template<>
inline const void* DataFile::peek_data_at<void>(off_t offset) const throw() {
return m_data + offset;
}
#endif