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
144
145
146
147
148
|
/*
Copyright (C) 2026 Carl Hetherington <cth@carlh.net>
This file is part of libttf.
libttf 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 2 of the License, or
(at your option) any later version.
libttf 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 libttf. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdint>
#include <stdexcept>
#include <string>
#include <vector>
namespace ttf {
class ParseError : public std::runtime_error
{
public:
ParseError() : std::runtime_error("Parse error") {}
};
/** Class which offers data read from a memory buffer */
class Source
{
public:
explicit Source(std::vector<uint8_t> const& data);
Source(uint8_t const* data, int size);
Source(Source& other, uint32_t offset);
uint16_t get_uint16();
uint32_t get_uint32();
std::string get_string(uint32_t offset, uint16_t length);
std::wstring get_wstring(uint32_t offset, uint16_t length);
std::vector<uint8_t> get_block(uint32_t offset, uint32_t length);
uint32_t offset() const {
return _offset;
}
uint8_t const* data() {
return _data;
}
int size() const {
return _size;
}
private:
uint8_t const* _data;
int _size;
uint32_t _offset;
};
/** Class which accepts data to be written into a memory buffer */
class Sink
{
public:
explicit Sink(std::vector<uint8_t>& data, uint32_t offset = 0);
Sink(Sink&) = delete;
Sink& operator=(Sink&) = delete;
Sink(Sink&& other) = delete;
Sink& operator=(Sink&& other) = delete;
void put(uint16_t value);
void put(uint32_t value);
void put(uint32_t value, uint32_t offset);
void put(std::string const& data);
void put(std::wstring const& data);
void put(std::vector<uint8_t> const& data);
std::vector<uint8_t>& data() {
return _data;
}
uint32_t initial_offset() const {
return _initial_offset;
}
uint32_t offset() const {
return _offset;
}
uint32_t offset_from_initial() const {
return _offset - _initial_offset;
}
void set_offset(uint32_t offset) {
_offset = offset;
}
void pad();
uint32_t checksum(uint32_t start) const;
private:
std::vector<uint8_t>& _data;
uint32_t _initial_offset;
uint32_t _offset;
};
/** Sink which starts at a given offset from another Sink's offset,
* and on destruction sets its parent offset to be the same as its
* own.
*/
class SubSink : public Sink
{
public:
explicit SubSink(Sink& parent, uint32_t offset = 0);
SubSink(SubSink const&) = delete;
SubSink& operator=(SubSink const&) = delete;
SubSink(SubSink&& other) = delete;
SubSink& operator=(SubSink&& other) = delete;
~SubSink();
private:
Sink& _parent;
};
uint32_t checksum(std::vector<uint8_t> const& data);
uint32_t checksum(uint8_t const* data, uint32_t size);
}
|