void mpack_write_tag(mpack_writer_t* writer, mpack_tag_t value) { switch (value.type) { case mpack_type_nil: mpack_write_nil (writer); break; case mpack_type_bool: mpack_write_bool (writer, value.v.b); break; case mpack_type_float: mpack_write_float (writer, value.v.f); break; case mpack_type_double: mpack_write_double(writer, value.v.d); break; case mpack_type_int: mpack_write_int (writer, value.v.i); break; case mpack_type_uint: mpack_write_uint (writer, value.v.u); break; case mpack_type_str: mpack_start_str(writer, value.v.l); break; case mpack_type_bin: mpack_start_bin(writer, value.v.l); break; case mpack_type_ext: mpack_start_ext(writer, value.exttype, value.v.l); break; case mpack_type_array: mpack_start_array(writer, value.v.n); break; case mpack_type_map: mpack_start_map(writer, value.v.n); break; default: mpack_assert(0, "unrecognized type %i", (int)value.type); break; } }
void mpack_writer_init_growable(mpack_writer_t* writer, char** target_data, size_t* target_size) { mpack_assert(target_data != NULL, "cannot initialize writer without a destination for the data"); mpack_assert(target_size != NULL, "cannot initialize writer without a destination for the size"); *target_data = NULL; *target_size = 0; MPACK_STATIC_ASSERT(sizeof(mpack_growable_writer_t) <= sizeof(writer->reserved), "not enough reserved space for growable writer!"); mpack_growable_writer_t* growable_writer = (mpack_growable_writer_t*)mpack_writer_get_reserved(writer); growable_writer->target_data = target_data; growable_writer->target_size = target_size; size_t capacity = MPACK_BUFFER_SIZE; char* buffer = (char*)MPACK_MALLOC(capacity); if (buffer == NULL) { mpack_writer_init_error(writer, mpack_error_memory); return; } mpack_writer_init(writer, buffer, capacity); mpack_writer_set_context(writer, growable_writer); mpack_writer_set_flush(writer, mpack_growable_writer_flush); mpack_writer_set_teardown(writer, mpack_growable_writer_teardown); }
bool mpack_reader_ensure_straddle(mpack_reader_t* reader, size_t count) { mpack_assert(count != 0, "cannot ensure zero bytes!"); mpack_assert(reader->error == mpack_ok, "reader cannot be in an error state!"); if (count <= reader->left) { mpack_assert(0, "big ensure requested for %i bytes, but there are %i bytes " "left in buffer. call mpack_reader_ensure() instead", (int)count, (int)reader->left); mpack_reader_flag_error(reader, mpack_error_bug); return false; } // we'll need a fill function to get more data. if there's no // fill function, the buffer should contain an entire MessagePack // object, so we raise mpack_error_invalid instead of mpack_error_io // on truncated data. if (reader->fill == NULL) { mpack_reader_flag_error(reader, mpack_error_invalid); return false; } mpack_assert(count <= reader->size, "cannot ensure byte count %i larger than buffer size %i", (int)count, (int)reader->size); // re-fill as much as possible mpack_partial_fill(reader); if (reader->left < count) { mpack_reader_flag_error(reader, mpack_error_io); return false; } return true; }
// internal inplace reader for when it straddles the end of the buffer static const char* mpack_read_bytes_inplace_big(mpack_reader_t* reader, size_t count) { // we should only arrive here from inplace straddle; this should already be checked mpack_assert(mpack_reader_error(reader) == mpack_ok, "already in error state? %s", mpack_error_to_string(mpack_reader_error(reader))); mpack_assert(reader->left < count, "already enough bytes in buffer: %i left, %i count", (int)reader->left, (int)count); // we'll need a fill function to get more data. if there's no // fill function, the buffer should contain an entire MessagePack // object, so we raise mpack_error_invalid instead of mpack_error_io // on truncated data. if (reader->fill == NULL) { mpack_reader_flag_error(reader, mpack_error_invalid); return NULL; } // make sure the buffer is big enough to actually fit the data if (count > reader->size) { mpack_reader_flag_error(reader, mpack_error_too_big); return NULL; } // re-fill as much as possible mpack_partial_fill(reader); if (reader->left < count) { mpack_reader_flag_error(reader, mpack_error_io); return NULL; } reader->pos += count; reader->left -= count; return reader->buffer; }
// Ensures there are at least count bytes free in the buffer. This // will flag an error if the flush function fails to make enough // room in the buffer. static bool mpack_writer_ensure(mpack_writer_t* writer, size_t count) { mpack_assert(count != 0, "cannot ensure zero bytes!"); mpack_assert(count <= MPACK_WRITER_MINIMUM_BUFFER_SIZE, "cannot ensure %i bytes, this is more than the minimum buffer size %i!", (int)count, (int)MPACK_WRITER_MINIMUM_BUFFER_SIZE); mpack_assert(count > mpack_writer_buffer_left(writer), "request to ensure %i bytes but there are already %i left in the buffer!", (int)count, (int)mpack_writer_buffer_left(writer)); mpack_log("ensuring %i bytes, %i left\n", (int)count, (int)mpack_writer_buffer_left(writer)); if (mpack_writer_error(writer) != mpack_ok) return false; if (writer->flush == NULL) { mpack_writer_flag_error(writer, mpack_error_too_big); return false; } mpack_writer_flush_unchecked(writer); if (mpack_writer_error(writer) != mpack_ok) return false; if (mpack_writer_buffer_left(writer) >= count) return true; mpack_writer_flag_error(writer, mpack_error_io); return false; }
static void mpack_reader_skip_using_fill(mpack_reader_t* reader, size_t count) { mpack_assert(reader->fill != NULL, "missing fill function!"); mpack_assert(reader->left == 0, "there are bytes left in the buffer!"); mpack_assert(reader->error == mpack_ok, "should not have called this in an error state (%i)", reader->error); mpack_log("skip using fill for %i bytes\n", (int)count); // fill and discard multiples of the buffer size while (count > reader->size) { mpack_log("filling and discarding buffer of %i bytes\n", (int)reader->size); if (mpack_fill(reader, reader->buffer, reader->size) < reader->size) { mpack_reader_flag_error(reader, mpack_error_io); return; } count -= reader->size; } // fill the buffer as much as possible reader->pos = 0; reader->left = mpack_fill(reader, reader->buffer, reader->size); if (reader->left < count) { mpack_reader_flag_error(reader, mpack_error_io); return; } mpack_log("filled %i bytes into buffer; discarding %i bytes\n", (int)reader->left, (int)count); reader->pos += count; reader->left -= count; }
mpack_error_t mpack_track_grow(mpack_track_t* track) { mpack_assert(track->elements, "null track elements!"); mpack_assert(track->count == track->capacity, "incorrect growing?"); size_t new_capacity = track->capacity * 2; mpack_track_element_t* new_elements = (mpack_track_element_t*)mpack_realloc(track->elements, sizeof(mpack_track_element_t) * track->count, sizeof(mpack_track_element_t) * new_capacity); if (new_elements == NULL) return mpack_error_memory; track->elements = new_elements; track->capacity = new_capacity; return mpack_ok; }
void mpack_reader_init_file(mpack_reader_t* reader, const char* filename) { mpack_assert(filename != NULL, "filename is NULL"); size_t capacity = MPACK_BUFFER_SIZE; char* buffer = (char*)MPACK_MALLOC(capacity); if (buffer == NULL) { mpack_reader_init_error(reader, mpack_error_memory); return; } FILE* file = fopen(filename, "rb"); if (file == NULL) { MPACK_FREE(buffer); mpack_reader_init_error(reader, mpack_error_io); return; } mpack_reader_init(reader, buffer, capacity, 0); mpack_reader_set_context(reader, file); mpack_reader_set_fill(reader, mpack_file_reader_fill); #if !MPACK_OPTIMIZE_FOR_SIZE mpack_reader_set_skip(reader, mpack_file_reader_skip); #endif mpack_reader_set_teardown(reader, mpack_file_reader_teardown); }
// Reads native bytes with error callback disabled. This allows MPack reader functions // to hold an allocated buffer and read native data into it without leaking it in // case of a non-local jump (longjmp, throw) out of an error handler. static void mpack_read_native_noerrorfn(mpack_reader_t* reader, char* p, size_t count) { mpack_assert(reader->error == mpack_ok, "cannot call if an error is already flagged!"); mpack_reader_error_t error_fn = reader->error_fn; reader->error_fn = NULL; mpack_read_native(reader, p, count); reader->error_fn = error_fn; }
void mpack_write_utf8_cstr(mpack_writer_t* writer, const char* cstr) { mpack_assert(cstr != NULL, "cstr pointer is NULL"); size_t length = mpack_strlen(cstr); if (length > UINT32_MAX) mpack_writer_flag_error(writer, mpack_error_invalid); mpack_write_utf8(writer, cstr, (uint32_t)length); }
MPACK_STATIC_INLINE void mpack_encode_str8(char* p, uint8_t count) { // TODO: str8 had no counterpart in MessagePack 1.0; there was only // fixraw, raw16 and raw32. This should not be used in compatibility mode. mpack_assert(count > 31); mpack_store_u8(p, 0xd9); mpack_store_u8(p + 1, count); }
mpack_error_t mpack_track_pop(mpack_track_t* track, mpack_type_t type) { mpack_assert(track->elements, "null track elements!"); mpack_log("track popping %s\n", mpack_type_to_string(type)); if (track->count == 0) { mpack_break("attempting to close a %s but nothing was opened!", mpack_type_to_string(type)); return mpack_error_bug; } mpack_track_element_t* element = &track->elements[track->count - 1]; if (element->type != type) { mpack_break("attempting to close a %s but the open element is a %s!", mpack_type_to_string(type), mpack_type_to_string(element->type)); return mpack_error_bug; } if (element->left != 0) { mpack_break("attempting to close a %s but there are %" PRIu64 " %s left", mpack_type_to_string(type), element->left, (type == mpack_type_map || type == mpack_type_array) ? "elements" : "bytes"); return mpack_error_bug; } --track->count; return mpack_ok; }
mpack_error_t mpack_track_bytes(mpack_track_t* track, bool read, uint64_t count) { MPACK_UNUSED(read); mpack_assert(track->elements, "null track elements!"); if (track->count == 0) { mpack_break("bytes cannot be %s with no open bin, str or ext", read ? "read" : "written"); return mpack_error_bug; } mpack_track_element_t* element = &track->elements[track->count - 1]; if (element->type == mpack_type_map || element->type == mpack_type_array) { mpack_break("bytes cannot be %s within an %s", read ? "read" : "written", mpack_type_to_string(element->type)); return mpack_error_bug; } if (element->left < count) { mpack_break("too many bytes %s for %s", read ? "read" : "written", mpack_type_to_string(element->type)); return mpack_error_bug; } element->left -= count; return mpack_ok; }
const char* mpack_type_to_string(mpack_type_t type) { #if MPACK_STRINGS switch (type) { #define MPACK_TYPE_STRING_CASE(e) case e: return #e MPACK_TYPE_STRING_CASE(mpack_type_nil); MPACK_TYPE_STRING_CASE(mpack_type_bool); MPACK_TYPE_STRING_CASE(mpack_type_float); MPACK_TYPE_STRING_CASE(mpack_type_double); MPACK_TYPE_STRING_CASE(mpack_type_int); MPACK_TYPE_STRING_CASE(mpack_type_uint); MPACK_TYPE_STRING_CASE(mpack_type_str); MPACK_TYPE_STRING_CASE(mpack_type_bin); MPACK_TYPE_STRING_CASE(mpack_type_ext); MPACK_TYPE_STRING_CASE(mpack_type_array); MPACK_TYPE_STRING_CASE(mpack_type_map); #undef MPACK_TYPE_STRING_CASE default: break; } mpack_assert(0, "unrecognized type %i", (int)type); return "(unknown mpack_type_t)"; #else MPACK_UNUSED(type); return ""; #endif }
void mpack_read_utf8(mpack_reader_t* reader, char* p, size_t byte_count) { mpack_assert(p != NULL, "destination for read of %i bytes is NULL", (int)byte_count); mpack_reader_track_str_bytes_all(reader, byte_count); mpack_read_native(reader, p, byte_count); if (mpack_reader_error(reader) == mpack_ok && !mpack_utf8_check(p, byte_count)) mpack_reader_flag_error(reader, mpack_error_type); }
void mpack_write_utf8(mpack_writer_t* writer, const char* str, uint32_t length) { mpack_assert(str != NULL, "data for string of length %i is NULL", (int)length); if (!mpack_utf8_check(str, length)) { mpack_writer_flag_error(writer, mpack_error_invalid); return; } mpack_write_str(writer, str, length); }
static void mpack_growable_writer_flush(mpack_writer_t* writer, const char* data, size_t count) { // This is an intrusive flush function which modifies the writer's buffer // in response to a flush instead of emptying it in order to add more // capacity for data. This removes the need to copy data from a fixed buffer // into a growable one, improving performance. // // There are three ways flush can be called: // - flushing the buffer during writing (used is zero, count is all data, data is buffer) // - flushing extra data during writing (used is all flushed data, count is extra data, data is not buffer) // - flushing during teardown (used and count are both all flushed data, data is buffer) // // In the first two cases, we grow the buffer by at least double, enough // to ensure that new data will fit. We ignore the teardown flush. if (data == writer->buffer) { // teardown, do nothing if (writer->used == count) return; // otherwise leave the data in the buffer and just grow writer->used = count; count = 0; } mpack_log("flush size %i used %i data %p buffer %p\n", (int)count, (int)writer->used, data, writer->buffer); mpack_assert(data == writer->buffer || writer->used + count > writer->size, "extra flush for %i but there is %i space left in the buffer! (%i/%i)", (int)count, (int)writer->size - (int)writer->used, (int)writer->used, (int)writer->size); // grow to fit the data // TODO: this really needs to correctly test for overflow size_t new_size = writer->size * 2; while (new_size < writer->used + count) new_size *= 2; mpack_log("flush growing buffer size from %i to %i\n", (int)writer->size, (int)new_size); // grow the buffer char* new_buffer = (char*)mpack_realloc(writer->buffer, writer->used, new_size); if (new_buffer == NULL) { mpack_writer_flag_error(writer, mpack_error_memory); return; } writer->buffer = new_buffer; writer->size = new_size; // append the extra data if (count > 0) { mpack_memcpy(writer->buffer + writer->used, data, count); writer->used += count; } mpack_log("new buffer %p, used %i\n", new_buffer, (int)writer->used); }
void mpack_reader_set_skip(mpack_reader_t* reader, mpack_reader_skip_t skip) { mpack_assert(reader->size != 0, "cannot use skip function without a writeable buffer!"); #if MPACK_OPTIMIZE_FOR_SIZE MPACK_UNUSED(reader); MPACK_UNUSED(skip); #else reader->skip = skip; #endif }
// A helper to call the reader fill function. This makes sure it's // implemented and guards against overflow in case it returns -1. static size_t mpack_fill(mpack_reader_t* reader, char* p, size_t count) { mpack_assert(reader->fill != NULL, "mpack_fill() called with no fill function?"); size_t ret = reader->fill(reader, p, count); if (ret == ((size_t)(-1))) return 0; return ret; }
// Writes encoded bytes to the buffer, flushing if necessary. MPACK_STATIC_INLINE void mpack_write_native(mpack_writer_t* writer, const char* p, size_t count) { mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count); if (writer->size - writer->used < count) { mpack_write_native_straddle(writer, p, count); } else { mpack_memcpy(writer->buffer + writer->used, p, count); writer->used += count; } }
static void mpack_read_cstr_unchecked(mpack_reader_t* reader, char* buf, size_t buffer_size, size_t byte_count) { mpack_assert(buf != NULL, "destination for read of %i bytes is NULL", (int)byte_count); mpack_assert(buffer_size >= 1, "buffer size is zero; you must have room for at least a null-terminator"); if (mpack_reader_error(reader)) { buf[0] = 0; return; } if (byte_count > buffer_size - 1) { mpack_reader_flag_error(reader, mpack_error_too_big); buf[0] = 0; return; } mpack_reader_track_str_bytes_all(reader, byte_count); mpack_read_native(reader, buf, byte_count); buf[byte_count] = 0; }
void mpack_print_file(const char* data, size_t len, FILE* file) { mpack_assert(data != NULL, "data is NULL"); mpack_assert(file != NULL, "file is NULL"); mpack_reader_t reader; mpack_reader_init_data(&reader, data, len); int depth = 2; for (int i = 0; i < depth; ++i) fprintf(file, " "); mpack_print_element(&reader, depth, file); putc('\n', file); size_t remaining = mpack_reader_remaining(&reader, NULL); if (mpack_reader_destroy(&reader) != mpack_ok) fprintf(file, "<mpack parsing error %s>\n", mpack_error_to_string(mpack_reader_error(&reader))); else if (remaining > 0) fprintf(file, "<%i extra bytes at end of mpack>\n", (int)remaining); }
void mpack_writer_init(mpack_writer_t* writer, char* buffer, size_t size) { mpack_assert(buffer != NULL, "cannot initialize writer with empty buffer"); mpack_writer_clear(writer); writer->buffer = buffer; writer->size = size; #if MPACK_WRITE_TRACKING mpack_writer_flag_if_error(writer, mpack_track_init(&writer->track)); #endif mpack_log("===========================\n"); mpack_log("initializing writer with buffer size %i\n", (int)size); }
// Writes encoded bytes to the buffer when we already know the data // does not fit in the buffer (i.e. it straddles the edge of the // buffer.) If there is a flush function, it is guaranteed to be // called; otherwise mpack_error_too_big is raised. static void mpack_write_native_straddle(mpack_writer_t* writer, const char* p, size_t count) { mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count); if (mpack_writer_error(writer) != mpack_ok) return; mpack_log("big write for %i bytes from %p, %i space left in buffer\n", (int)count, p, (int)(writer->size - writer->used)); mpack_assert(count > writer->size - writer->used, "big write requested for %i bytes, but there is %i available " "space in buffer. should have called mpack_write_native() instead", (int)count, (int)(writer->size - writer->used)); // we'll need a flush function if (!writer->flush) { mpack_writer_flag_error(writer, mpack_error_too_big); return; } // flush the buffer mpack_writer_flush_unchecked(writer); if (mpack_writer_error(writer) != mpack_ok) return; // note that an intrusive flush function (such as mpack_growable_writer_flush()) // may have changed size and/or reset used to a non-zero value. we treat both as // though they may have changed, and there may still be data in the buffer. // flush the extra data directly if it doesn't fit in the buffer if (count > writer->size - writer->used) { writer->flush(writer, p, count); if (mpack_writer_error(writer) != mpack_ok) return; } else { mpack_memcpy(writer->buffer + writer->used, p, count); writer->used += count; } }
void mpack_reader_init(mpack_reader_t* reader, char* buffer, size_t size, size_t count) { mpack_assert(buffer != NULL, "buffer is NULL"); mpack_memset(reader, 0, sizeof(*reader)); reader->buffer = buffer; reader->size = size; reader->left = count; #if MPACK_READ_TRACKING mpack_reader_flag_if_error(reader, mpack_track_init(&reader->track)); #endif mpack_log("===========================\n"); mpack_log("initializing reader with buffer size %i\n", (int)size); }
static size_t mpack_expect_cstr_unchecked(mpack_reader_t* reader, char* buf, size_t bufsize) { // make sure buffer makes sense mpack_assert(bufsize >= 1, "buffer size is zero; you must have room for at least a null-terminator"); // expect a str size_t length = mpack_expect_str_buf(reader, buf, bufsize - 1); if (mpack_reader_error(reader)) { buf[0] = 0; return 0; } buf[length] = 0; return length; }
mpack_error_t mpack_track_push(mpack_track_t* track, mpack_type_t type, uint64_t count) { mpack_assert(track->elements, "null track elements!"); mpack_log("track pushing %s count %i\n", mpack_type_to_string(type), (int)count); // maps have twice the number of elements (key/value pairs) if (type == mpack_type_map) count *= 2; // grow if needed if (track->count == track->capacity) { mpack_error_t error = mpack_track_grow(track); if (error != mpack_ok) return error; } // insert new track track->elements[track->count].type = type; track->elements[track->count].left = count; ++track->count; return mpack_ok; }
const char* mpack_error_to_string(mpack_error_t error) { #if MPACK_STRINGS switch (error) { #define MPACK_ERROR_STRING_CASE(e) case e: return #e MPACK_ERROR_STRING_CASE(mpack_ok); MPACK_ERROR_STRING_CASE(mpack_error_io); MPACK_ERROR_STRING_CASE(mpack_error_invalid); MPACK_ERROR_STRING_CASE(mpack_error_type); MPACK_ERROR_STRING_CASE(mpack_error_too_big); MPACK_ERROR_STRING_CASE(mpack_error_memory); MPACK_ERROR_STRING_CASE(mpack_error_bug); MPACK_ERROR_STRING_CASE(mpack_error_data); #undef MPACK_ERROR_STRING_CASE default: break; } mpack_assert(0, "unrecognized error %i", (int)error); return "(unknown mpack_error_t)"; #else MPACK_UNUSED(error); return ""; #endif }
void mpack_writer_init_file(mpack_writer_t* writer, const char* filename) { mpack_assert(filename != NULL, "filename is NULL"); size_t capacity = MPACK_BUFFER_SIZE; char* buffer = (char*)MPACK_MALLOC(capacity); if (buffer == NULL) { mpack_writer_init_error(writer, mpack_error_memory); return; } FILE* file = fopen(filename, "wb"); if (file == NULL) { MPACK_FREE(buffer); mpack_writer_init_error(writer, mpack_error_io); return; } mpack_writer_init(writer, buffer, capacity); mpack_writer_set_context(writer, file); mpack_writer_set_flush(writer, mpack_file_writer_flush); mpack_writer_set_teardown(writer, mpack_file_writer_teardown); }
void mpack_reader_init_data(mpack_reader_t* reader, const char* data, size_t count) { mpack_assert(data != NULL, "data is NULL"); mpack_memset(reader, 0, sizeof(*reader)); reader->left = count; // unfortunately we have to cast away the const to store the buffer, // but we won't be modifying it because there's no fill function. // the buffer size is left at 0 to ensure no fill function can be // set or used (see mpack_reader_set_fill().) #ifdef __cplusplus reader->buffer = const_cast<char*>(data); #else reader->buffer = (char*)data; #endif #if MPACK_READ_TRACKING mpack_reader_flag_if_error(reader, mpack_track_init(&reader->track)); #endif mpack_log("===========================\n"); mpack_log("initializing reader with data size %i\n", (int)count); }