summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/avro/src/encoding_binary.c
blob: 1fc5f0c9a7be7bc790814165eb31de72f33e67b6 (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0 
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 * https://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License. 
 */

#include "avro_private.h"
#include "avro/allocation.h"
#include "avro/errors.h"
#include "encoding.h"
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <string.h>

#define MAX_VARINT_BUF_SIZE 10

static int read_long(avro_reader_t reader, int64_t * l)
{
	uint64_t value = 0;
	uint8_t b;
	int offset = 0;
	do {
		if (offset == MAX_VARINT_BUF_SIZE) {
			/*
			 * illegal byte sequence 
			 */
			avro_set_error("Varint too long");
			return EILSEQ;
		}
		AVRO_READ(reader, &b, 1);
		value |= (int64_t) (b & 0x7F) << (7 * offset);
		++offset;
	}
	while (b & 0x80);
	*l = ((value >> 1) ^ -(value & 1));
	return 0;
}

static int skip_long(avro_reader_t reader)
{
	uint8_t b;
	int offset = 0;
	do {
		if (offset == MAX_VARINT_BUF_SIZE) {
			avro_set_error("Varint too long");
			return EILSEQ;
		}
		AVRO_READ(reader, &b, 1);
		++offset;
	}
	while (b & 0x80);
	return 0;
}

static int write_long(avro_writer_t writer, int64_t l)
{
	char buf[MAX_VARINT_BUF_SIZE];
	uint8_t bytes_written = 0;
	uint64_t n = (l << 1) ^ (l >> 63);
	while (n & ~0x7F) {
		buf[bytes_written++] = (char)((((uint8_t) n) & 0x7F) | 0x80);
		n >>= 7;
	}
	buf[bytes_written++] = (char)n;
	AVRO_WRITE(writer, buf, bytes_written);
	return 0;
}

static int64_t size_long(avro_writer_t writer, int64_t l)
{
	AVRO_UNUSED(writer);

	int64_t len = 0;
	uint64_t n = (l << 1) ^ (l >> 63);
	while (n & ~0x7F) {
		len++;
		n >>= 7;
	}
	len++;
	return len;
}

static int read_int(avro_reader_t reader, int32_t * i)
{
	int64_t l;
	int rval;
	check(rval, read_long(reader, &l));
	if (!(INT_MIN <= l && l <= INT_MAX)) {
		avro_set_error("Varint out of range for int type");
		return ERANGE;
	}
	*i = l;
	return 0;
}

static int skip_int(avro_reader_t reader)
{
	return skip_long(reader);
}

static int write_int(avro_writer_t writer, const int32_t i)
{
	int64_t l = i;
	return write_long(writer, l);
}

static int64_t size_int(avro_writer_t writer, const int32_t i)
{
	int64_t l = i;
	return size_long(writer, l);
}

static int read_bytes(avro_reader_t reader, char **bytes, int64_t * len)
{
	int rval;
	check_prefix(rval, read_long(reader, len),
		     "Cannot read bytes length: ");
	*bytes = (char *) avro_malloc(*len + 1);
	if (!*bytes) {
		avro_set_error("Cannot allocate buffer for bytes value");
		return ENOMEM;
	}
	AVRO_READ(reader, *bytes, *len);
	(*bytes)[*len] = '\0';
	return 0;
}

static int skip_bytes(avro_reader_t reader)
{
	int64_t len = 0;
	int rval;
	check_prefix(rval, read_long(reader, &len),
		     "Cannot read bytes length: ");
	AVRO_SKIP(reader, len);
	return 0;
}

static int
write_bytes(avro_writer_t writer, const char *bytes, const int64_t len)
{
	int rval;
	if (len < 0) {
		avro_set_error("Invalid bytes value length");
		return EINVAL;
	}
	check_prefix(rval, write_long(writer, len),
		     "Cannot write bytes length: ");
	AVRO_WRITE(writer, (char *)bytes, len);
	return 0;
}

static int64_t
size_bytes(avro_writer_t writer, const char *bytes, const int64_t len)
{
	AVRO_UNUSED(bytes);

	return size_long(writer, len) + len;
}

static int read_string(avro_reader_t reader, char **s, int64_t *len)
{
	int64_t  str_len = 0;
	int rval;
	check_prefix(rval, read_long(reader, &str_len),
		     "Cannot read string length: ");
	*len = str_len + 1;
	*s = (char *) avro_malloc(*len);
	if (!*s) {
		avro_set_error("Cannot allocate buffer for string value");
		return ENOMEM;
	}
	(*s)[str_len] = '\0';
	AVRO_READ(reader, *s, str_len);
	return 0;
}

static int skip_string(avro_reader_t reader)
{
	return skip_bytes(reader);
}

static int write_string(avro_writer_t writer, const char *s)
{
	int64_t len = strlen(s);
	return write_bytes(writer, s, len);
}

static int64_t size_string(avro_writer_t writer, const char *s)
{
	int64_t len = strlen(s);
	return size_bytes(writer, s, len);
}

static int read_float(avro_reader_t reader, float *f)
{
#if AVRO_PLATFORM_IS_BIG_ENDIAN
	uint8_t buf[4];
#endif
	union {
		float f;
		int32_t i;
	} v;
#if AVRO_PLATFORM_IS_BIG_ENDIAN
	AVRO_READ(reader, buf, 4);
	v.i = ((int32_t) buf[0] << 0)
	    | ((int32_t) buf[1] << 8)
	    | ((int32_t) buf[2] << 16) | ((int32_t) buf[3] << 24);
#else
	AVRO_READ(reader, (void *)&v.i, 4);
#endif
	*f = v.f;
	return 0;
}

static int skip_float(avro_reader_t reader)
{
	AVRO_SKIP(reader, 4);
	return 0;
}

static int write_float(avro_writer_t writer, const float f)
{
#if AVRO_PLATFORM_IS_BIG_ENDIAN
	uint8_t buf[4];
#endif
	union {
		float f;
		int32_t i;
	} v;

	v.f = f;
#if AVRO_PLATFORM_IS_BIG_ENDIAN
	buf[0] = (uint8_t) (v.i >> 0);
	buf[1] = (uint8_t) (v.i >> 8);
	buf[2] = (uint8_t) (v.i >> 16);
	buf[3] = (uint8_t) (v.i >> 24);
	AVRO_WRITE(writer, buf, 4);
#else
	AVRO_WRITE(writer, (void *)&v.i, 4);
#endif
	return 0;
}

static int64_t size_float(avro_writer_t writer, const float f)
{
	AVRO_UNUSED(writer);
	AVRO_UNUSED(f);

	return 4;
}

static int read_double(avro_reader_t reader, double *d)
{
#if AVRO_PLATFORM_IS_BIG_ENDIAN
	uint8_t buf[8];
#endif
	union {
		double d;
		int64_t l;
	} v;

#if AVRO_PLATFORM_IS_BIG_ENDIAN
	AVRO_READ(reader, buf, 8);
	v.l = ((int64_t) buf[0] << 0)
	    | ((int64_t) buf[1] << 8)
	    | ((int64_t) buf[2] << 16)
	    | ((int64_t) buf[3] << 24)
	    | ((int64_t) buf[4] << 32)
	    | ((int64_t) buf[5] << 40)
	    | ((int64_t) buf[6] << 48) | ((int64_t) buf[7] << 56);
#else
	AVRO_READ(reader, (void *)&v.l, 8);
#endif
	*d = v.d;
	return 0;
}

static int skip_double(avro_reader_t reader)
{
	AVRO_SKIP(reader, 8);
	return 0;
}

static int write_double(avro_writer_t writer, const double d)
{
#if AVRO_PLATFORM_IS_BIG_ENDIAN
	uint8_t buf[8];
#endif
	union {
		double d;
		int64_t l;
	} v;

	v.d = d;
#if AVRO_PLATFORM_IS_BIG_ENDIAN
	buf[0] = (uint8_t) (v.l >> 0);
	buf[1] = (uint8_t) (v.l >> 8);
	buf[2] = (uint8_t) (v.l >> 16);
	buf[3] = (uint8_t) (v.l >> 24);
	buf[4] = (uint8_t) (v.l >> 32);
	buf[5] = (uint8_t) (v.l >> 40);
	buf[6] = (uint8_t) (v.l >> 48);
	buf[7] = (uint8_t) (v.l >> 56);
	AVRO_WRITE(writer, buf, 8);
#else
	AVRO_WRITE(writer, (void *)&v.l, 8);
#endif
	return 0;
}

static int64_t size_double(avro_writer_t writer, const double d)
{
	AVRO_UNUSED(writer);
	AVRO_UNUSED(d);

	return 8;
}

static int read_boolean(avro_reader_t reader, int8_t * b)
{
	AVRO_READ(reader, b, 1);
	return 0;
}

static int skip_boolean(avro_reader_t reader)
{
	AVRO_SKIP(reader, 1);
	return 0;
}

static int write_boolean(avro_writer_t writer, const int8_t b)
{
	AVRO_WRITE(writer, (char *)&b, 1);
	return 0;
}

static int64_t size_boolean(avro_writer_t writer, const int8_t b)
{
	AVRO_UNUSED(writer);
	AVRO_UNUSED(b);

	return 1;
}

static int read_skip_null(avro_reader_t reader)
{
	/*
	 * no-op 
	 */
	AVRO_UNUSED(reader);

	return 0;
}

static int write_null(avro_writer_t writer)
{
	/*
	 * no-op 
	 */
	AVRO_UNUSED(writer);

	return 0;
}

static int64_t size_null(avro_writer_t writer)
{
	AVRO_UNUSED(writer);

	return 0;
}

/* Win32 doesn't support the C99 method of initializing named elements
 * in a struct declaration. So hide the named parameters for Win32,
 * and initialize in the order the code was written.
 */
const avro_encoding_t avro_binary_encoding = {
	/* .description = */ "BINARY FORMAT",
	/*
	 * string 
	 */
	/* .read_string = */ read_string,
	/* .skip_string = */ skip_string,
	/* .write_string = */ write_string,
	/* .size_string = */ size_string,
	/*
	 * bytes 
	 */
	/* .read_bytes = */ read_bytes,
	/* .skip_bytes = */ skip_bytes,
	/* .write_bytes = */ write_bytes,
	/* .size_bytes = */ size_bytes,
	/*
	 * int 
	 */
	/* .read_int = */ read_int,
	/* .skip_int = */ skip_int,
	/* .write_int = */ write_int,
	/* .size_int = */ size_int,
	/*
	 * long 
	 */
	/* .read_long = */ read_long,
	/* .skip_long = */ skip_long,
	/* .write_long = */ write_long,
	/* .size_long = */ size_long,
	/*
	 * float 
	 */
	/* .read_float = */ read_float,
	/* .skip_float = */ skip_float,
	/* .write_float = */ write_float,
	/* .size_float = */ size_float,
	/*
	 * double 
	 */
	/* .read_double = */ read_double,
	/* .skip_double = */ skip_double,
	/* .write_double = */ write_double,
	/* .size_double = */ size_double,
	/*
	 * boolean 
	 */
	/* .read_boolean = */ read_boolean,
	/* .skip_boolean = */ skip_boolean,
	/* .write_boolean = */ write_boolean,
	/* .size_boolean = */ size_boolean,
	/*
	 * null 
	 */
	/* .read_null = */ read_skip_null,
	/* .skip_null = */ read_skip_null,
	/* .write_null = */ write_null,
	/* .size_null = */ size_null
};