static int encode_utf8_bytes(const void *src, size_t src_len, void **dest, size_t *dest_len) { check_param(EINVAL, src, "source"); check_param(EINVAL, dest, "dest"); check_param(EINVAL, dest_len, "dest_len"); // First, determine the size of the resulting UTF-8 buffer. // Bytes in the range 0x00..0x7f will take up one byte; bytes in // the range 0x80..0xff will take up two. const uint8_t *src8 = (const uint8_t *) src; size_t utf8_len = src_len + 1; // +1 for NUL terminator size_t i; for (i = 0; i < src_len; i++) { if (src8[i] & 0x80) { utf8_len++; } } // Allocate a new buffer for the UTF-8 string and fill it in. uint8_t *dest8 = (uint8_t *) avro_malloc(utf8_len); if (dest8 == NULL) { avro_set_error("Cannot allocate JSON bytes buffer"); return ENOMEM; } uint8_t *curr = dest8; for (i = 0; i < src_len; i++) { if (src8[i] & 0x80) { *curr++ = (0xc0 | (src8[i] >> 6)); *curr++ = (0x80 | (src8[i] & 0x3f)); } else {
static int encode_snappy(avro_codec_t c, void * data, int64_t len) { uint32_t crc; size_t outlen = snappy_max_compressed_length(len); if (!c->block_data) { c->block_data = avro_malloc(outlen+4); c->block_size = outlen+4; } else if (c->block_size < (int64_t) (outlen+4)) { c->block_data = avro_realloc(c->block_data, c->block_size, (outlen+4)); c->block_size = outlen+4; } if (!c->block_data) { avro_set_error("Cannot allocate memory for snappy"); return 1; } if (snappy_compress(data, len, c->block_data, &outlen) != SNAPPY_OK) { avro_set_error("Error compressing block with Snappy"); return 1; } crc = __bswap_32(crc32(0, data, len)); memcpy(c->block_data+outlen, &crc, 4); c->used_size = outlen+4; return 0; }
void *avro_calloc(size_t count, size_t size) { void *ptr = avro_malloc(count * size); if (ptr != NULL) { memset(ptr, 0, count * size); } return ptr; }
char *avro_str_alloc(size_t str_size) { size_t buf_size = str_size + sizeof(size_t); void *buf = avro_malloc(buf_size); if (buf == NULL) { return NULL; } size_t *size = (size_t *) buf; char *new_str = (char *) (size + 1); *size = buf_size; return new_str; }
static int decode_snappy(avro_codec_t c, void * data, int64_t len) { uint32_t crc; size_t outlen; if (snappy_uncompressed_length(data, len-4, &outlen) != SNAPPY_OK) { avro_set_error("Uncompressed length error in snappy"); return 1; } if (!c->block_data) { c->block_data = avro_malloc(outlen); c->block_size = outlen; } else if ( (size_t)c->block_size < outlen) { c->block_data = avro_realloc(c->block_data, c->block_size, outlen); c->block_size = outlen; } if (!c->block_data) { avro_set_error("Cannot allocate memory for snappy"); return 1; } if (snappy_uncompress(data, len-4, c->block_data, &outlen) != SNAPPY_OK) { avro_set_error("Error uncompressing block with Snappy"); return 1; } crc = __bswap_32(crc32(0, c->block_data, outlen)); if (memcmp(&crc, (char*)data+len-4, 4)) { avro_set_error("CRC32 check failure uncompressing block with Snappy"); return 1; } c->used_size = outlen; return 0; }
char *avro_strdup(const char *str) { if (str == NULL) { return NULL; } size_t str_size = strlen(str)+1; size_t buf_size = str_size + sizeof(size_t); void *buf = avro_malloc(buf_size); if (buf == NULL) { return NULL; } size_t *size = buf; char *new_str = (char *) (size + 1); *size = buf_size; memcpy(new_str, str, str_size); //fprintf(stderr, "--- new %zu %p %s\n", *size, new_str, new_str); return new_str; }
int avro_consume_binary(avro_reader_t reader, avro_consumer_t *consumer, void *ud) { int rval; const avro_encoding_t *enc = &avro_binary_encoding; check_param(EINVAL, reader, "reader"); check_param(EINVAL, consumer, "consumer"); switch (avro_typeof(consumer->schema)) { case AVRO_NULL: check_prefix(rval, enc->read_null(reader), "Cannot read null value: "); check(rval, avro_consumer_call(consumer, null_value, ud)); break; case AVRO_BOOLEAN: { int8_t b; check_prefix(rval, enc->read_boolean(reader, &b), "Cannot read boolean value: "); check(rval, avro_consumer_call(consumer, boolean_value, b, ud)); } break; case AVRO_STRING: { int64_t len; char *s; check_prefix(rval, enc->read_string(reader, &s, &len), "Cannot read string value: "); check(rval, avro_consumer_call(consumer, string_value, s, len, ud)); } break; case AVRO_INT32: { int32_t i; check_prefix(rval, enc->read_int(reader, &i), "Cannot read int value: "); check(rval, avro_consumer_call(consumer, int_value, i, ud)); } break; case AVRO_INT64: { int64_t l; check_prefix(rval, enc->read_long(reader, &l), "Cannot read long value: "); check(rval, avro_consumer_call(consumer, long_value, l, ud)); } break; case AVRO_FLOAT: { float f; check_prefix(rval, enc->read_float(reader, &f), "Cannot read float value: "); check(rval, avro_consumer_call(consumer, float_value, f, ud)); } break; case AVRO_DOUBLE: { double d; check_prefix(rval, enc->read_double(reader, &d), "Cannot read double value: "); check(rval, avro_consumer_call(consumer, double_value, d, ud)); } break; case AVRO_BYTES: { char *bytes; int64_t len; check_prefix(rval, enc->read_bytes(reader, &bytes, &len), "Cannot read bytes value: "); check(rval, avro_consumer_call(consumer, bytes_value, bytes, len, ud)); } break; case AVRO_FIXED: { char *bytes; int64_t size = avro_schema_to_fixed(consumer->schema)->size; bytes = avro_malloc(size); if (!bytes) { avro_prefix_error("Cannot allocate new fixed value"); return ENOMEM; } rval = avro_read(reader, bytes, size); if (rval) { avro_prefix_error("Cannot read fixed value: "); avro_free(bytes, size); return rval; } rval = avro_consumer_call(consumer, fixed_value, bytes, size, ud); if (rval) { avro_free(bytes, size); return rval; } } break; case AVRO_ENUM: check(rval, read_enum(reader, enc, consumer, ud)); break; case AVRO_ARRAY: check(rval, read_array(reader, enc, consumer, ud)); break; case AVRO_MAP: check(rval, read_map(reader, enc, consumer, ud)); break; case AVRO_UNION: check(rval, read_union(reader, enc, consumer, ud)); break; case AVRO_RECORD: check(rval, read_record(reader, enc, consumer, ud)); break; case AVRO_LINK: avro_set_error("Consumer can't consume a link schema directly"); return EINVAL; } return 0; }