This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
/
writer.c
481 lines (370 loc) · 10.1 KB
/
writer.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <string.h>
#include <php.h>
#include "protobuf.h"
#include "writer.h"
#define WRITER_DATA_SIZE_INCREMENT 1024
#define WRITER_VARINT_SPACE 10
#define WRITER_32BIT_SPACE 4
#define WRITER_64BIT_SPACE 8
static inline int writer_ensure_space(writer_t *writer, size_t len);
static inline void writer_write_varint(writer_t *writer, int64_t value);
static inline void write_fixed32(writer_t *writer, fixed32_t value);
static inline void write_fixed64(writer_t *writer, fixed64_t value);
static inline void zigzag_encode(int64_t *value);
void writer_free_pack(writer_t *writer)
{
if (writer->data != NULL) {
efree(writer->data);
writer->data = NULL;
writer->size = 0;
writer->pos = 0;
}
}
void writer_init(writer_t *writer)
{
writer_init_ex(writer, WRITER_DATA_SIZE_INCREMENT);
}
void writer_init_ex(writer_t *writer, size_t size)
{
if ((writer->data = (uint8_t *) emalloc(size)) != NULL)
writer->size = size;
else
writer->size = 0;
writer->pos = 0;
}
char *writer_get_pack(writer_t *writer, size_t *size)
{
*size = writer->pos;
return (char *) writer->data;
}
int writer_write_double(writer_t *writer, uint64_t field_number, double value)
{
int64_t key;
fixed64_t val;
if (writer_ensure_space(writer, WRITER_VARINT_SPACE + WRITER_64BIT_SPACE) != 0)
return -1;
key = (field_number << 3 | WIRE_TYPE_64BIT);
writer_write_varint(writer, key);
val.d_val = value;
write_fixed64(writer, val);
return 0;
}
int writer_write_fixed32(writer_t *writer, uint64_t field_number, int32_t value)
{
int64_t key;
fixed32_t val;
if (writer_ensure_space(writer, WRITER_VARINT_SPACE + WRITER_32BIT_SPACE) != 0)
return -1;
key = (field_number << 3 | WIRE_TYPE_32BIT);
writer_write_varint(writer, key);
val.i_val = value;
write_fixed32(writer, val);
return 0;
}
int writer_write_fixed64(writer_t *writer, uint64_t field_number, int64_t value)
{
int64_t key;
fixed64_t val;
if (writer_ensure_space(writer, WRITER_VARINT_SPACE + WRITER_64BIT_SPACE) != 0)
return -1;
key = (field_number << 3 | WIRE_TYPE_64BIT);
writer_write_varint(writer, key);
val.i_val = value;
write_fixed64(writer, val);
return 0;
}
int writer_write_float(writer_t *writer, uint64_t field_number, double value)
{
int64_t key;
fixed32_t val;
if (writer_ensure_space(writer, WRITER_VARINT_SPACE + WRITER_32BIT_SPACE) != 0)
return -1;
key = (field_number << 3 | WIRE_TYPE_32BIT);
writer_write_varint(writer, key);
val.f_val = value;
write_fixed32(writer, val);
return 0;
}
int writer_write_int(writer_t *writer, uint64_t field_number, int64_t value)
{
int64_t key;
if (writer_ensure_space(writer, 2 * WRITER_VARINT_SPACE) != 0)
return -1;
key = (field_number << 3 | WIRE_TYPE_VARINT);
writer_write_varint(writer, key);
writer_write_varint(writer, value);
return 0;
}
int writer_write_signed_int(writer_t *writer, uint64_t field_number, int64_t value)
{
int64_t key;
if (writer_ensure_space(writer, 2 * WRITER_VARINT_SPACE) != 0)
return -1;
key = (field_number << 3 | WIRE_TYPE_VARINT);
writer_write_varint(writer, key);
zigzag_encode(&value);
writer_write_varint(writer, value);
return 0;
}
int writer_write_string(writer_t *writer, uint64_t field_number, const char *str, size_t len)
{
int64_t key;
if (writer_ensure_space(writer, 2 * WRITER_VARINT_SPACE + len) != 0)
return -1;
key = (field_number << 3 | WIRE_TYPE_LENGTH_DELIMITED);
writer_write_varint(writer, key);
writer_write_varint(writer, len);
if (len > 0) {
memcpy(writer->data + writer->pos, str, len);
writer->pos += len;
}
return 0;
}
int writer_write_packed_double(writer_t *writer, uint64_t field_number, zval *values)
{
fixed64_t val;
HashPosition i;
zval *value;
writer_t packed_writer;
int ret, num;
size_t pack_size;
char *pack;
num = zend_hash_num_elements(Z_ARRVAL_P(values));
if (num == 0) {
return 0;
}
writer_init_ex(&packed_writer, WRITER_64BIT_SPACE * num);
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
val.d_val = Z_DVAL_P(value);
write_fixed64(&packed_writer, val);
}
pack = writer_get_pack(&packed_writer, &pack_size);
ret = writer_write_string(writer, field_number, pack, pack_size);
writer_free_pack(&packed_writer);
return ret;
}
int writer_write_packed_fixed32(writer_t *writer, uint64_t field_number, zval *values)
{
fixed32_t val;
HashPosition i;
zval *value;
writer_t packed_writer;
int ret, num;
size_t pack_size;
char *pack;
num = zend_hash_num_elements(Z_ARRVAL_P(values));
if (num == 0) {
return 0;
}
writer_init_ex(&packed_writer, WRITER_32BIT_SPACE * num);
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
val.i_val = (int32_t)Z_LVAL_P(value);
write_fixed32(&packed_writer, val);
}
pack = writer_get_pack(&packed_writer, &pack_size);
ret = writer_write_string(writer, field_number, pack, pack_size);
writer_free_pack(&packed_writer);
return ret;
}
int writer_write_packed_fixed64(writer_t *writer, uint64_t field_number, zval *values)
{
fixed64_t val;
HashPosition i;
zval *value;
writer_t packed_writer;
int ret, num;
size_t pack_size;
char *pack;
num = zend_hash_num_elements(Z_ARRVAL_P(values));
if (num == 0) {
return 0;
}
writer_init_ex(&packed_writer, WRITER_64BIT_SPACE * num);
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
val.i_val = Z_LVAL_P(value);
write_fixed64(&packed_writer, val);
}
pack = writer_get_pack(&packed_writer, &pack_size);
ret = writer_write_string(writer, field_number, pack, pack_size);
writer_free_pack(&packed_writer);
return ret;
}
int writer_write_packed_float(writer_t *writer, uint64_t field_number, zval *values)
{
fixed32_t val;
HashPosition i;
zval *value;
writer_t packed_writer;
int ret, num;
size_t pack_size;
char *pack;
num = zend_hash_num_elements(Z_ARRVAL_P(values));
if (num == 0) {
return 0;
}
writer_init_ex(&packed_writer, WRITER_32BIT_SPACE * num);
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
val.f_val = Z_DVAL_P(value);
write_fixed32(&packed_writer, val);
}
pack = writer_get_pack(&packed_writer, &pack_size);
ret = writer_write_string(writer, field_number, pack, pack_size);
writer_free_pack(&packed_writer);
return ret;
}
int writer_write_packed_int(writer_t *writer, uint64_t field_number, zval *values)
{
int64_t val;
HashPosition i;
zval *value;
writer_t packed_writer;
int ret, num;
size_t pack_size;
char *pack;
num = zend_hash_num_elements(Z_ARRVAL_P(values));
if (num == 0) {
return 0;
}
writer_init_ex(&packed_writer, WRITER_VARINT_SPACE * num);
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
val = Z_LVAL_P(value);
writer_write_varint(&packed_writer, val);
}
pack = writer_get_pack(&packed_writer, &pack_size);
ret = writer_write_string(writer, field_number, pack, pack_size);
writer_free_pack(&packed_writer);
return ret;
}
int writer_write_packed_bool(writer_t *writer, uint64_t field_number, zval *values)
{
int64_t val;
HashPosition i;
zval *value;
writer_t packed_writer;
int ret, num;
size_t pack_size;
char *pack;
num = zend_hash_num_elements(Z_ARRVAL_P(values));
if (num == 0) {
return 0;
}
writer_init_ex(&packed_writer, WRITER_VARINT_SPACE * num);
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
if (Z_TYPE_P(value) == IS_TRUE) {
val = 1;
} else {
val = 0;
}
writer_write_varint(&packed_writer, val);
}
pack = writer_get_pack(&packed_writer, &pack_size);
ret = writer_write_string(writer, field_number, pack, pack_size);
writer_free_pack(&packed_writer);
return ret;
}
int writer_write_packed_signed_int(writer_t *writer, uint64_t field_number, zval *values)
{
int64_t val;
HashPosition i;
zval *value;
writer_t packed_writer;
int ret, num;
size_t pack_size;
char *pack;
num = zend_hash_num_elements(Z_ARRVAL_P(values));
if (num == 0) {
return 0;
}
writer_init_ex(&packed_writer, WRITER_VARINT_SPACE * num);
PB_FOREACH(&i, Z_ARRVAL_P(values)) {
value = zend_hash_get_current_data_ex(Z_ARRVAL_P(values), &i);
val = Z_LVAL_P(value);
zigzag_encode(&val);
writer_write_varint(&packed_writer, val);
}
pack = writer_get_pack(&packed_writer, &pack_size);
ret = writer_write_string(writer, field_number, pack, pack_size);
writer_free_pack(&packed_writer);
return ret;
}
static inline int writer_ensure_space(writer_t *writer, size_t space)
{
uint8_t *data;
size_t left_space;
left_space = writer->size - writer->pos;
if (left_space < space) {
writer->size += (space - left_space);
data = (uint8_t *) erealloc(writer->data, writer->size);
if (data == NULL)
return -1;
writer->data = data;
}
return 0;
}
static inline void writer_write_varint(writer_t *writer, int64_t value)
{
int i;
uint8_t byte;
if (value == 0) {
writer->data[writer->pos++] = 0;
} else if (value < 0) {
for (i = 0; i < WRITER_VARINT_SPACE - 1; i++) {
writer->data[writer->pos++] = value | 0x80;
*(uint64_t *) &value >>= 7;
}
writer->data[writer->pos++] = value;
} else {
do {
byte = value;
value >>= 7;
if (value != 0)
byte |= 0x80;
else
byte &= 0x7F;
writer->data[writer->pos++] = byte;
} while (value != 0);
}
}
static inline void write_fixed32(writer_t *writer, fixed32_t value)
{
uint8_t *out;
out = writer->data + writer->pos;
#ifndef WORDS_BIGENDIAN
memcpy(out, &value.u_val, 4);
#else
out[0] = value.u_val;
out[1] = value.u_val >> 8;
out[2] = value.u_val >> 16;
out[3] = value.u_val >> 24;
#endif
writer->pos += WRITER_32BIT_SPACE;
}
static inline void write_fixed64(writer_t *writer, fixed64_t value)
{
#ifndef WORDS_BIGENDIAN
uint8_t *out;
out = writer->data + writer->pos;
memcpy(out, &value.u_val, 8);
writer->pos += WRITER_64BIT_SPACE;
#else
fixed32_t lo;
lo.u_val = value.u_val;
fixed32_t hi;
hi.u_val = value.u_val >> 32;
write_fixed32(writer, lo);
write_fixed32(writer, hi);
#endif
}
static inline void zigzag_encode(int64_t *value)
{
if (*value < 0)
*(uint64_t *) value = ((uint64_t) (-(*value)) * 2) - 1;
else
*(uint64_t *) value = 2 * (*value);
}