-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsbuf_stream.cpp
192 lines (182 loc) · 4.36 KB
/
sbuf_stream.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include "sbuf_stream.h"
/*
* Stream interfaces
*/
sbuf_stream::sbuf_stream(const sbuf_t& sbuf_) : sbuf(sbuf_) { }
sbuf_stream::~sbuf_stream() {}
void sbuf_stream::seek(size_t offset_) { offset = offset_; }
size_t sbuf_stream::tell() { return offset; }
/*
* unsigned integers, default little endian
*/
uint8_t sbuf_stream::get8u() {
uint8_t value = sbuf.get8u(offset);
offset++;
return value;
}
uint16_t sbuf_stream::get16u() {
uint16_t value = sbuf.get16u(offset);
offset += 2;
return value;
}
uint32_t sbuf_stream::get32u() {
uint32_t value = sbuf.get32u(offset);
offset += 4;
return value;
}
uint64_t sbuf_stream::get64u() {
uint64_t value = sbuf.get64u(offset);
offset += 8;
return value;
}
/*
* unsigned integers, big endian
*/
uint8_t sbuf_stream::get8uBE() {
uint8_t value = sbuf.get8uBE(offset);
offset++;
return value;
}
uint16_t sbuf_stream::get16uBE() {
uint16_t value = sbuf.get16uBE(offset);
offset += 2;
return value;
}
uint32_t sbuf_stream::get32uBE() {
uint32_t value = sbuf.get32uBE(offset);
offset += 4;
return value;
}
uint64_t sbuf_stream::get64uBE() {
uint64_t value = sbuf.get64uBE(offset);
offset += 8;
return value;
}
/*
* unsigned integers, byte order specified
*/
uint8_t sbuf_stream::get8u(sbuf_t::byte_order_t bo) {
uint8_t value = sbuf.get8u(offset, bo);
offset++;
return value;
}
uint16_t sbuf_stream::get16u(sbuf_t::byte_order_t bo) {
uint16_t value = sbuf.get16u(offset, bo);
offset += 2;
return value;
}
uint32_t sbuf_stream::get32u(sbuf_t::byte_order_t bo) {
uint32_t value = sbuf.get32u(offset, bo);
offset += 4;
return value;
}
uint64_t sbuf_stream::get64u(sbuf_t::byte_order_t bo) {
uint64_t value = sbuf.get64u(offset, bo);
offset += 8;
return value;
}
/*
* signed integers, default little endian
*/
int8_t sbuf_stream::get8i() {
int8_t value = sbuf.get8i(offset);
offset++;
return value;
}
int16_t sbuf_stream::get16i() {
int16_t value = sbuf.get16i(offset);
offset += 2;
return value;
}
int32_t sbuf_stream::get32i() {
int32_t value = sbuf.get32i(offset);
offset += 4;
return value;
}
int64_t sbuf_stream::get64i() {
int64_t value = sbuf.get64i(offset);
offset += 8;
return value;
}
/*
* signed integers, big endian
*/
int8_t sbuf_stream::get8iBE() {
int8_t value = sbuf.get8iBE(offset);
offset++;
return value;
}
int16_t sbuf_stream::get16iBE() {
int16_t value = sbuf.get16iBE(offset);
offset += 2;
return value;
}
int32_t sbuf_stream::get32iBE() {
int32_t value = sbuf.get32iBE(offset);
offset += 4;
return value;
}
int64_t sbuf_stream::get64iBE() {
int64_t value = sbuf.get64iBE(offset);
offset += 8;
return value;
}
/*
* signed integers, byte order specified
*/
int8_t sbuf_stream::get8i(sbuf_t::byte_order_t bo) {
int8_t value = sbuf.get8i(offset, bo);
offset++;
return value;
}
int16_t sbuf_stream::get16i(sbuf_t::byte_order_t bo) {
int16_t value = sbuf.get16i(offset, bo);
offset += 2;
return value;
}
int32_t sbuf_stream::get32i(sbuf_t::byte_order_t bo) {
int32_t value = sbuf.get32i(offset, bo);
offset += 4;
return value;
}
int64_t sbuf_stream::get64i(sbuf_t::byte_order_t bo) {
int64_t value = sbuf.get64i(offset, bo);
offset += 8;
return value;
}
/*
* string readers
*/
std::string sbuf_stream::getUTF8(size_t num_octets_requested)
{
std::string utf8_string = sbuf.getUTF8(num_octets_requested);
offset += utf8_string.length();
return utf8_string;
}
std::string sbuf_stream::getUTF8() {
std::string ret = sbuf.getUTF8(offset);
size_t num_bytes = ret.length();
// if anything was read then also skip \0
if (num_bytes > 0) {
num_bytes++;
}
offset += num_bytes;
return ret;
}
std::wstring sbuf_stream::getUTF16(size_t code_units_requested) {
std::wstring ret = sbuf.getUTF16(offset, code_units_requested);
offset += ret.length() * 2;
return ret;
}
std::wstring sbuf_stream::getUTF16() {
std::wstring utf16_string = sbuf.getUTF16(offset);
size_t num_bytes = utf16_string.length() * 2;
if (num_bytes > 0) {
// if anything was read then also skip \U0000
num_bytes += 2;
}
offset += num_bytes;
return utf16_string;
}