forked from allegro/php-protobuf
-
Notifications
You must be signed in to change notification settings - Fork 8
/
reader.c
231 lines (179 loc) · 3.87 KB
/
reader.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <string.h>
#include "protobuf.h"
#include "reader.h"
#define READER_LEFT(reader) (reader)->len - (reader)->pos
static inline fixed32_t reader_read_fixed_uint32(const uint8_t *data);
static inline fixed64_t reader_read_fixed_uint64(const uint8_t *data);
static inline int reader_read_varint(reader_t *reader, uint64_t *value);
void reader_init(reader_t *reader, char *string, size_t len)
{
reader->string = (const uint8_t *) string;
reader->len = len;
reader->pos = 0;
}
int reader_has_more(reader_t *reader)
{
return reader->len != reader->pos;
}
int reader_read_double(reader_t *reader, double *value)
{
fixed64_t val;
if (READER_LEFT(reader) >= 8)
{
val = reader_read_fixed_uint64(reader->string + reader->pos);
*value = val.d_val;
reader->pos += 8;
return 0;
}
return -1;
}
int reader_read_fixed32(reader_t *reader, long *value)
{
fixed32_t val;
if (READER_LEFT(reader) >= 4)
{
val = reader_read_fixed_uint32(reader->string + reader->pos);
*value = val.i_val;
reader->pos += 4;
return 0;
}
return -1;
}
int reader_read_fixed64(reader_t *reader, long *value)
{
fixed64_t val;
if (READER_LEFT(reader) >= 8)
{
val = reader_read_fixed_uint64(reader->string + reader->pos);
*value = val.i_val;
reader->pos += 8;
return 0;
}
return -1;
}
int reader_read_float(reader_t *reader, double *value)
{
fixed32_t val;
if (READER_LEFT(reader) >= 4)
{
val = reader_read_fixed_uint32(reader->string + reader->pos);
*value = val.f_val;
reader->pos += 4;
return 0;
}
return -1;
}
int reader_read_int(reader_t *reader, long *value)
{
uint64_t val;
if (reader_read_varint(reader, &val) != 0)
return -1;
*value = *(int64_t *) &val;
return 0;
}
int reader_read_signed_int(reader_t *reader, long *value)
{
uint64_t val;
if (reader_read_varint(reader, &val) != 0)
return -1;
if (val & 1)
val = -((*(uint64_t *) &val) >> 1) - 1;
else
val = ((*(uint64_t *) &val) >> 1);
*value = val;
return 0;
}
int reader_read_string(reader_t *reader, char **string, int *len)
{
uint64_t l;
if (reader_read_varint(reader, &l) != 0)
return -1;
if (READER_LEFT(reader) < l)
return -1;
if (string != NULL)
{
*string = (char *) (reader->string + reader->pos);
*len = l;
}
reader->pos += l;
return 0;
}
int reader_read_tag(reader_t *reader, uint32_t *field_number, uint8_t *wire_type)
{
uint64_t key;
if (reader_read_varint(reader, &key) != 0)
return -1;
*wire_type = key & 0x07;
*field_number = (key >> 3);
return 0;
}
int reader_skip_32bit(reader_t *reader)
{
if (READER_LEFT(reader) >= 4)
{
reader->pos += 4;
return 0;
}
return -1;
}
int reader_skip_64bit(reader_t *reader)
{
if (READER_LEFT(reader) >= 8)
{
reader->pos += 8;
return 0;
}
return -1;
}
int reader_skip_length_delimited(reader_t *reader)
{
return reader_read_string(reader, NULL, NULL);
}
int reader_skip_varint(reader_t *reader)
{
return reader_read_varint(reader, NULL);
}
static inline int reader_read_varint(reader_t *reader, uint64_t *value)
{
int i = 0;
uint64_t byte;
uint64_t val = 0;
while (reader_has_more(reader) && i <= 10)
{
byte = reader->string[reader->pos];
byte &= 0x7F;
byte <<= (7 * i++);
val |= byte;
if (reader->string[reader->pos++] <= 0x7F)
{
if (value != NULL)
*value = val;
return 0;
}
}
return -1;
}
static inline fixed32_t reader_read_fixed_uint32(const uint8_t *data)
{
fixed32_t val;
#ifndef WORDS_BIGENDIAN
memcpy (&val.u_val, data, 4);
return val;
#else
val.u_val = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
return val;
#endif
}
static inline fixed64_t reader_read_fixed_uint64(const uint8_t *data)
{
fixed64_t val;
#ifndef WORDS_BIGENDIAN
memcpy (&val.u_val, data, 8);
return val;
#else
fixed32_t lo = reader_read_fixed_uint32(data);
fixed32_t hi = reader_read_fixed_uint32(data + 4);
val.u_val = (uint64_t) lo.u_val | ((uint64_t) hi.u_val) << 32;
return val;
#endif
}