-
Notifications
You must be signed in to change notification settings - Fork 3
/
stream_util.cpp
212 lines (170 loc) · 3.26 KB
/
stream_util.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Created by hijiang on 2019/8/4.
//
#include <assert.h>
#include "stream_util.h"
using namespace std;
StreamUtil::StreamUtil()
{
p = bytes = NULL;
nb_bytes = 0;
// TODO: support both little and big endian.
// assert(srs_is_little_endian());
}
StreamUtil::~StreamUtil()
{
}
int StreamUtil::initialize(char* b, int nb)
{
int ret = 0;
if (!b) {
ret = -1;
return ret;
}
if (nb <= 0) {
ret = -2;
return ret;
}
nb_bytes = nb;
p = bytes = b;
return ret;
}
char* StreamUtil::data()
{
return bytes;
}
int StreamUtil::size()
{
return nb_bytes;
}
int StreamUtil::pos()
{
return (int)(p - bytes);
}
int StreamUtil::left()
{
return nb_bytes - (int)(p-bytes);
}
bool StreamUtil::empty()
{
return !bytes || (p >= bytes + nb_bytes);
}
bool StreamUtil::require(int required_size)
{
return required_size <= nb_bytes - (p - bytes);
}
void StreamUtil::skip(int size)
{
p += size;
}
int8_t StreamUtil::read_1bytes()
{
return (int8_t)*p++;
}
int16_t StreamUtil::read_2bytes()
{
int16_t value;
char* pp = (char*)&value;
pp[1] = *p++;
pp[0] = *p++;
return value;
}
int32_t StreamUtil::read_3bytes()
{
int32_t value = 0x00;
char* pp = (char*)&value;
pp[2] = *p++;
pp[1] = *p++;
pp[0] = *p++;
return value;
}
int32_t StreamUtil::read_4bytes()
{
int32_t value;
char* pp = (char*)&value;
pp[3] = *p++;
pp[2] = *p++;
pp[1] = *p++;
pp[0] = *p++;
return value;
}
int64_t StreamUtil::read_8bytes()
{
int64_t value;
char* pp = (char*)&value;
pp[7] = *p++;
pp[6] = *p++;
pp[5] = *p++;
pp[4] = *p++;
pp[3] = *p++;
pp[2] = *p++;
pp[1] = *p++;
pp[0] = *p++;
return value;
}
string StreamUtil::read_string(int len)
{
std::string value;
value.append(p, len);
p += len;
return value;
}
void StreamUtil::read_bytes(char* data, int size)
{
memcpy(data, p, size);
p += size;
}
void StreamUtil::write_1bytes(int8_t value)
{
assert(require(1));
*p++ = value;
}
void StreamUtil::write_2bytes(int16_t value)
{
assert(require(2));
char* pp = (char*)&value;
*p++ = pp[1];
*p++ = pp[0];
}
void StreamUtil::write_4bytes(int32_t value)
{
assert(require(4));
char* pp = (char*)&value;
*p++ = pp[3];
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
}
void StreamUtil::write_3bytes(int32_t value)
{
assert(require(3));
char* pp = (char*)&value;
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
}
void StreamUtil::write_8bytes(int64_t value)
{
assert(require(8));
char* pp = (char*)&value;
*p++ = pp[7];
*p++ = pp[6];
*p++ = pp[5];
*p++ = pp[4];
*p++ = pp[3];
*p++ = pp[2];
*p++ = pp[1];
*p++ = pp[0];
}
void StreamUtil::write_string(string value)
{
assert(require((int)value.length()));
memcpy(p, value.data(), value.length());
p += value.length();
}
void StreamUtil::write_bytes(char* data, int size)
{
assert(require(size));
memcpy(p, data, size);
p += size;
}