diff --git a/Makefile b/Makefile index eb0b26e..c29bdeb 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ NOFPUTESTCFLAGS ?= $(TESTCFLAGS) -DCMP_NO_FLOAT ADDRCFLAGS ?= -fsanitize=address MEMCFLAGS ?= -fsanitize=memory -fno-omit-frame-pointer \ -fno-optimize-sibling-calls -UBCFLAGS ?= -fsanitize=undefined +UBCFLAGS ?= -fsanitize=undefined,nullability,local-bounds,float-divide-by-zero,integer .PHONY: all clean test coverage diff --git a/cmp.c b/cmp.c index 3f35eb6..b7a059e 100644 --- a/cmp.c +++ b/cmp.c @@ -1,27 +1,6 @@ -/* -The MIT License (MIT) - -Copyright (c) 2020 Charles Gunyon - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - +/* SPDX-License-Identifier: MIT + * Copyright © 2020-2024 Charles Gunyon. + */ #include "cmp.h" static const uint32_t cmp_version_ = 20; @@ -72,64 +51,70 @@ enum { FIXSTR_SIZE = 0x1F }; -enum { - ERROR_NONE, - STR_DATA_LENGTH_TOO_LONG_ERROR, - BIN_DATA_LENGTH_TOO_LONG_ERROR, - ARRAY_LENGTH_TOO_LONG_ERROR, - MAP_LENGTH_TOO_LONG_ERROR, - INPUT_VALUE_TOO_LARGE_ERROR, - FIXED_VALUE_WRITING_ERROR, - TYPE_MARKER_READING_ERROR, - TYPE_MARKER_WRITING_ERROR, - DATA_READING_ERROR, - DATA_WRITING_ERROR, - EXT_TYPE_READING_ERROR, - EXT_TYPE_WRITING_ERROR, - INVALID_TYPE_ERROR, - LENGTH_READING_ERROR, - LENGTH_WRITING_ERROR, - SKIP_DEPTH_LIMIT_EXCEEDED_ERROR, - INTERNAL_ERROR, - DISABLED_FLOATING_POINT_ERROR, - ERROR_MAX -}; - -static const char * const cmp_error_messages[ERROR_MAX + 1] = { - "No Error", - "Specified string data length is too long (> 0xFFFFFFFF)", - "Specified binary data length is too long (> 0xFFFFFFFF)", - "Specified array length is too long (> 0xFFFFFFFF)", - "Specified map length is too long (> 0xFFFFFFFF)", - "Input value is too large", - "Error writing fixed value", - "Error reading type marker", - "Error writing type marker", - "Error reading packed data", - "Error writing packed data", - "Error reading ext type", - "Error writing ext type", - "Invalid type", - "Error reading size", - "Error writing size", - "Depth limit exceeded while skipping", - "Internal error", - "Floating point operations disabled", - "Max Error" -}; +typedef enum cmp_error_t { + CMP_ERROR_NONE, + CMP_ERROR_STR_DATA_LENGTH_TOO_LONG, + CMP_ERROR_BIN_DATA_LENGTH_TOO_LONG, + CMP_ERROR_ARRAY_LENGTH_TOO_LONG, + CMP_ERROR_MAP_LENGTH_TOO_LONG, + CMP_ERROR_INPUT_VALUE_TOO_LARGE, + CMP_ERROR_FIXED_VALUE_WRITING, + CMP_ERROR_TYPE_MARKER_READING, + CMP_ERROR_TYPE_MARKER_WRITING, + CMP_ERROR_DATA_READING, + CMP_ERROR_DATA_WRITING, + CMP_ERROR_EXT_TYPE_READING, + CMP_ERROR_EXT_TYPE_WRITING, + CMP_ERROR_INVALID_TYPE, + CMP_ERROR_LENGTH_READING, + CMP_ERROR_LENGTH_WRITING, + CMP_ERROR_SKIP_DEPTH_LIMIT_EXCEEDED, + CMP_ERROR_INTERNAL, + CMP_ERROR_DISABLED_FLOATING_POINT, + CMP_ERROR_MAX +} cmp_error_t; + +static const char *cmp_error_message(cmp_error_t error) { + switch (error) { + case CMP_ERROR_NONE: return ""; + case CMP_ERROR_STR_DATA_LENGTH_TOO_LONG: return "Specified string data length is too long (> 0xFFFFFFFF)"; + case CMP_ERROR_BIN_DATA_LENGTH_TOO_LONG: return "Specified binary data length is too long (> 0xFFFFFFFF)"; + case CMP_ERROR_ARRAY_LENGTH_TOO_LONG: return "Specified array length is too long (> 0xFFFFFFFF)"; + case CMP_ERROR_MAP_LENGTH_TOO_LONG: return "Specified map length is too long (> 0xFFFFFFFF)"; + case CMP_ERROR_INPUT_VALUE_TOO_LARGE: return "Input value is too large"; + case CMP_ERROR_FIXED_VALUE_WRITING: return "Error writing fixed value"; + case CMP_ERROR_TYPE_MARKER_READING: return "Error reading type marker"; + case CMP_ERROR_TYPE_MARKER_WRITING: return "Error writing type marker"; + case CMP_ERROR_DATA_READING: return "Error reading packed data"; + case CMP_ERROR_DATA_WRITING: return "Error writing packed data"; + case CMP_ERROR_EXT_TYPE_READING: return "Error reading ext type"; + case CMP_ERROR_EXT_TYPE_WRITING: return "Error writing ext type"; + case CMP_ERROR_INVALID_TYPE: return "Invalid type"; + case CMP_ERROR_LENGTH_READING: return "Error reading size"; + case CMP_ERROR_LENGTH_WRITING: return "Error writing size"; + case CMP_ERROR_SKIP_DEPTH_LIMIT_EXCEEDED: return "Depth limit exceeded while skipping"; + case CMP_ERROR_INTERNAL: return "Internal error"; + case CMP_ERROR_DISABLED_FLOATING_POINT: return "Floating point operations disabled"; + case CMP_ERROR_MAX: return "Max Error"; + } + return ""; +} +static bool is_bigendian(void) { #ifdef WORDS_BIGENDIAN -#define is_bigendian() (WORDS_BIGENDIAN) + return WORDS_BIGENDIAN; #else -static const int32_t i_ = 1; -#define is_bigendian() ((*(const char *)&i_) == 0) -#endif + const int32_t i_ = 1; + const char *i_bytes = (const char *)&i_; + return *i_bytes == 0; +#endif /* WORDS_BIGENDIAN */ +} static uint16_t be16(uint16_t x) { char *b = (char *)&x; if (!is_bigendian()) { - char swap = b[0]; + const char swap = b[0]; b[0] = b[1]; b[1] = swap; } @@ -193,7 +178,7 @@ static int64_t sbe64(int64_t x) { #ifndef CMP_NO_FLOAT static float decode_befloat(const char *b) { - float f = 0.; + float f = 0.0; char *fb = (char *)&f; if (!is_bigendian()) { @@ -213,7 +198,7 @@ static float decode_befloat(const char *b) { } static double decode_bedouble(const char *b) { - double d = 0.; + double d = 0.0; char *db = (char *)&d; if (!is_bigendian()) { @@ -246,18 +231,17 @@ static bool read_byte(cmp_ctx_t *ctx, uint8_t *x) { } static bool write_byte(cmp_ctx_t *ctx, uint8_t x) { - return (ctx->write(ctx, &x, sizeof(uint8_t)) == (sizeof(uint8_t))); + return ctx->write(ctx, &x, sizeof(uint8_t)) == sizeof(uint8_t); } static bool skip_bytes(cmp_ctx_t *ctx, size_t count) { - if (ctx->skip) { + if (ctx->skip != NULL) { return ctx->skip(ctx, count); } else { - uint8_t floor; size_t i; - - for (i = 0; i < count; i++) { + for (i = 0; i < count; ++i) { + uint8_t floor; if (!ctx->read(ctx, &floor, sizeof(uint8_t))) { return false; } @@ -272,7 +256,7 @@ static bool read_type_marker(cmp_ctx_t *ctx, uint8_t *marker) { return true; } - ctx->error = TYPE_MARKER_READING_ERROR; + ctx->error = CMP_ERROR_TYPE_MARKER_READING; return false; } @@ -280,7 +264,7 @@ static bool write_type_marker(cmp_ctx_t *ctx, uint8_t marker) { if (write_byte(ctx, marker)) return true; - ctx->error = TYPE_MARKER_WRITING_ERROR; + ctx->error = CMP_ERROR_TYPE_MARKER_WRITING; return false; } @@ -288,7 +272,7 @@ static bool write_fixed_value(cmp_ctx_t *ctx, uint8_t value) { if (write_byte(ctx, value)) return true; - ctx->error = FIXED_VALUE_WRITING_ERROR; + ctx->error = CMP_ERROR_FIXED_VALUE_WRITING; return false; } @@ -319,101 +303,133 @@ static bool type_marker_to_cmp_type(uint8_t type_marker, uint8_t *cmp_type) { } switch (type_marker) { - case NIL_MARKER: + case NIL_MARKER: { *cmp_type = CMP_TYPE_NIL; return true; - case FALSE_MARKER: + } + case FALSE_MARKER: { *cmp_type = CMP_TYPE_BOOLEAN; return true; - case TRUE_MARKER: + } + case TRUE_MARKER: { *cmp_type = CMP_TYPE_BOOLEAN; return true; - case BIN8_MARKER: + } + case BIN8_MARKER: { *cmp_type = CMP_TYPE_BIN8; return true; - case BIN16_MARKER: + } + case BIN16_MARKER: { *cmp_type = CMP_TYPE_BIN16; return true; - case BIN32_MARKER: + } + case BIN32_MARKER: { *cmp_type = CMP_TYPE_BIN32; return true; - case EXT8_MARKER: + } + case EXT8_MARKER: { *cmp_type = CMP_TYPE_EXT8; return true; - case EXT16_MARKER: + } + case EXT16_MARKER: { *cmp_type = CMP_TYPE_EXT16; return true; - case EXT32_MARKER: + } + case EXT32_MARKER: { *cmp_type = CMP_TYPE_EXT32; return true; - case FLOAT_MARKER: + } + case FLOAT_MARKER: { *cmp_type = CMP_TYPE_FLOAT; return true; - case DOUBLE_MARKER: + } + case DOUBLE_MARKER: { *cmp_type = CMP_TYPE_DOUBLE; return true; - case U8_MARKER: + } + case U8_MARKER: { *cmp_type = CMP_TYPE_UINT8; return true; - case U16_MARKER: + } + case U16_MARKER: { *cmp_type = CMP_TYPE_UINT16; return true; - case U32_MARKER: + } + case U32_MARKER: { *cmp_type = CMP_TYPE_UINT32; return true; - case U64_MARKER: + } + case U64_MARKER: { *cmp_type = CMP_TYPE_UINT64; return true; - case S8_MARKER: + } + case S8_MARKER: { *cmp_type = CMP_TYPE_SINT8; return true; - case S16_MARKER: + } + case S16_MARKER: { *cmp_type = CMP_TYPE_SINT16; return true; - case S32_MARKER: + } + case S32_MARKER: { *cmp_type = CMP_TYPE_SINT32; return true; - case S64_MARKER: + } + case S64_MARKER: { *cmp_type = CMP_TYPE_SINT64; return true; - case FIXEXT1_MARKER: + } + case FIXEXT1_MARKER: { *cmp_type = CMP_TYPE_FIXEXT1; return true; - case FIXEXT2_MARKER: + } + case FIXEXT2_MARKER: { *cmp_type = CMP_TYPE_FIXEXT2; return true; - case FIXEXT4_MARKER: + } + case FIXEXT4_MARKER: { *cmp_type = CMP_TYPE_FIXEXT4; return true; - case FIXEXT8_MARKER: + } + case FIXEXT8_MARKER: { *cmp_type = CMP_TYPE_FIXEXT8; return true; - case FIXEXT16_MARKER: + } + case FIXEXT16_MARKER: { *cmp_type = CMP_TYPE_FIXEXT16; return true; - case STR8_MARKER: + } + case STR8_MARKER: { *cmp_type = CMP_TYPE_STR8; return true; - case STR16_MARKER: + } + case STR16_MARKER: { *cmp_type = CMP_TYPE_STR16; return true; - case STR32_MARKER: + } + case STR32_MARKER: { *cmp_type = CMP_TYPE_STR32; return true; - case ARRAY16_MARKER: + } + case ARRAY16_MARKER: { *cmp_type = CMP_TYPE_ARRAY16; return true; - case ARRAY32_MARKER: + } + case ARRAY32_MARKER: { *cmp_type = CMP_TYPE_ARRAY32; return true; - case MAP16_MARKER: + } + case MAP16_MARKER: { *cmp_type = CMP_TYPE_MAP16; return true; - case MAP32_MARKER: + } + case MAP32_MARKER: { *cmp_type = CMP_TYPE_MAP32; return true; - default: + } + default: { return false; + } } } @@ -425,277 +441,325 @@ static bool read_type_size(cmp_ctx_t *ctx, uint8_t type_marker, uint32_t u32temp = 0; switch (cmp_type) { - case CMP_TYPE_POSITIVE_FIXNUM: + case CMP_TYPE_POSITIVE_FIXNUM: { *size = 0; return true; - case CMP_TYPE_FIXMAP: - *size = (type_marker & FIXMAP_SIZE); + } + case CMP_TYPE_FIXMAP: { + *size = type_marker & FIXMAP_SIZE; return true; - case CMP_TYPE_FIXARRAY: - *size = (type_marker & FIXARRAY_SIZE); + } + case CMP_TYPE_FIXARRAY: { + *size = type_marker & FIXARRAY_SIZE; return true; - case CMP_TYPE_FIXSTR: - *size = (type_marker & FIXSTR_SIZE); + } + case CMP_TYPE_FIXSTR: { + *size = type_marker & FIXSTR_SIZE; return true; - case CMP_TYPE_NIL: + } + case CMP_TYPE_NIL: { *size = 0; return true; - case CMP_TYPE_BOOLEAN: + } + case CMP_TYPE_BOOLEAN: { *size = 0; return true; - case CMP_TYPE_BIN8: + } + case CMP_TYPE_BIN8: { if (!ctx->read(ctx, &u8temp, sizeof(uint8_t))) { - ctx->error = LENGTH_READING_ERROR; + ctx->error = CMP_ERROR_LENGTH_READING; return false; } *size = u8temp; return true; - case CMP_TYPE_BIN16: + } + case CMP_TYPE_BIN16: { if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) { - ctx->error = LENGTH_READING_ERROR; + ctx->error = CMP_ERROR_LENGTH_READING; return false; } *size = be16(u16temp); return true; - case CMP_TYPE_BIN32: + } + case CMP_TYPE_BIN32: { if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) { - ctx->error = LENGTH_READING_ERROR; + ctx->error = CMP_ERROR_LENGTH_READING; return false; } *size = be32(u32temp); return true; - case CMP_TYPE_EXT8: + } + case CMP_TYPE_EXT8: { if (!ctx->read(ctx, &u8temp, sizeof(uint8_t))) { - ctx->error = LENGTH_READING_ERROR; + ctx->error = CMP_ERROR_LENGTH_READING; return false; } *size = u8temp; return true; - case CMP_TYPE_EXT16: + } + case CMP_TYPE_EXT16: { if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) { - ctx->error = LENGTH_READING_ERROR; + ctx->error = CMP_ERROR_LENGTH_READING; return false; } *size = be16(u16temp); return true; - case CMP_TYPE_EXT32: + } + case CMP_TYPE_EXT32: { if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) { - ctx->error = LENGTH_READING_ERROR; + ctx->error = CMP_ERROR_LENGTH_READING; return false; } *size = be32(u32temp); return true; - case CMP_TYPE_FLOAT: + } + case CMP_TYPE_FLOAT: { *size = 4; return true; - case CMP_TYPE_DOUBLE: + } + case CMP_TYPE_DOUBLE: { *size = 8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { *size = 1; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *size = 2; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { *size = 4; return true; - case CMP_TYPE_UINT64: + } + case CMP_TYPE_UINT64: { *size = 8; return true; - case CMP_TYPE_SINT8: + } + case CMP_TYPE_SINT8: { *size = 1; return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { *size = 2; return true; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { *size = 4; return true; - case CMP_TYPE_SINT64: + } + case CMP_TYPE_SINT64: { *size = 8; return true; - case CMP_TYPE_FIXEXT1: + } + case CMP_TYPE_FIXEXT1: { *size = 1; return true; - case CMP_TYPE_FIXEXT2: + } + case CMP_TYPE_FIXEXT2: { *size = 2; return true; - case CMP_TYPE_FIXEXT4: + } + case CMP_TYPE_FIXEXT4: { *size = 4; return true; - case CMP_TYPE_FIXEXT8: + } + case CMP_TYPE_FIXEXT8: { *size = 8; return true; - case CMP_TYPE_FIXEXT16: + } + case CMP_TYPE_FIXEXT16: { *size = 16; return true; - case CMP_TYPE_STR8: + } + case CMP_TYPE_STR8: { if (!ctx->read(ctx, &u8temp, sizeof(uint8_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } *size = u8temp; return true; - case CMP_TYPE_STR16: + } + case CMP_TYPE_STR16: { if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } *size = be16(u16temp); return true; - case CMP_TYPE_STR32: + } + case CMP_TYPE_STR32: { if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } *size = be32(u32temp); return true; - case CMP_TYPE_ARRAY16: + } + case CMP_TYPE_ARRAY16: { if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } *size = be16(u16temp); return true; - case CMP_TYPE_ARRAY32: + } + case CMP_TYPE_ARRAY32: { if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } *size = be32(u32temp); return true; - case CMP_TYPE_MAP16: + } + case CMP_TYPE_MAP16: { if (!ctx->read(ctx, &u16temp, sizeof(uint16_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } *size = be16(u16temp); return true; - case CMP_TYPE_MAP32: + } + case CMP_TYPE_MAP32: { if (!ctx->read(ctx, &u32temp, sizeof(uint32_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } *size = be32(u32temp); return true; - case CMP_TYPE_NEGATIVE_FIXNUM: + } + case CMP_TYPE_NEGATIVE_FIXNUM: { *size = 0; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker, cmp_object_t *obj) { switch (obj->type) { - case CMP_TYPE_POSITIVE_FIXNUM: + case CMP_TYPE_POSITIVE_FIXNUM: { obj->as.u8 = type_marker; return true; - case CMP_TYPE_NEGATIVE_FIXNUM: + } + case CMP_TYPE_NEGATIVE_FIXNUM: { obj->as.s8 = (int8_t)type_marker; return true; - case CMP_TYPE_NIL: + } + case CMP_TYPE_NIL: { obj->as.u8 = 0; return true; - case CMP_TYPE_BOOLEAN: + } + case CMP_TYPE_BOOLEAN: { switch (type_marker) { - case TRUE_MARKER: + case TRUE_MARKER: { obj->as.boolean = true; return true; - case FALSE_MARKER: + } + case FALSE_MARKER: { obj->as.boolean = false; return true; + } default: break; } - ctx->error = INTERNAL_ERROR; + ctx->error = CMP_ERROR_INTERNAL; return false; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { if (!ctx->read(ctx, &obj->as.u8, sizeof(uint8_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { if (!ctx->read(ctx, &obj->as.u16, sizeof(uint16_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.u16 = be16(obj->as.u16); return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { if (!ctx->read(ctx, &obj->as.u32, sizeof(uint32_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.u32 = be32(obj->as.u32); return true; - case CMP_TYPE_UINT64: + } + case CMP_TYPE_UINT64: { if (!ctx->read(ctx, &obj->as.u64, sizeof(uint64_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.u64 = be64(obj->as.u64); return true; - case CMP_TYPE_SINT8: + } + case CMP_TYPE_SINT8: { if (!ctx->read(ctx, &obj->as.s8, sizeof(int8_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { if (!ctx->read(ctx, &obj->as.s16, sizeof(int16_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.s16 = sbe16(obj->as.s16); return true; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { if (!ctx->read(ctx, &obj->as.s32, sizeof(int32_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.s32 = sbe32(obj->as.s32); return true; - case CMP_TYPE_SINT64: + } + case CMP_TYPE_SINT64: { if (!ctx->read(ctx, &obj->as.s64, sizeof(int64_t))) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.s64 = sbe64(obj->as.s64); return true; - case CMP_TYPE_FLOAT: - { + } + case CMP_TYPE_FLOAT: { #ifndef CMP_NO_FLOAT char bytes[4]; if (!ctx->read(ctx, bytes, 4)) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.flt = decode_befloat(bytes); return true; #else /* CMP_NO_FLOAT */ - ctx->error = DISABLED_FLOATING_POINT_ERROR; + ctx->error = CMP_ERROR_DISABLED_FLOATING_POINT; return false; #endif /* CMP_NO_FLOAT */ } - case CMP_TYPE_DOUBLE: - { + case CMP_TYPE_DOUBLE: { #ifndef CMP_NO_FLOAT char bytes[8]; if (!ctx->read(ctx, bytes, 8)) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } obj->as.dbl = decode_bedouble(bytes); return true; #else /* CMP_NO_FLOAT */ - ctx->error = DISABLED_FLOATING_POINT_ERROR; + ctx->error = CMP_ERROR_DISABLED_FLOATING_POINT; return false; #endif /* CMP_NO_FLOAT */ } @@ -716,80 +780,88 @@ static bool read_obj_data(cmp_ctx_t *ctx, uint8_t type_marker, case CMP_TYPE_MAP16: case CMP_TYPE_MAP32: return read_type_size(ctx, type_marker, obj->type, &obj->as.map_size); - case CMP_TYPE_FIXEXT1: + case CMP_TYPE_FIXEXT1: { if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } obj->as.ext.size = 1; return true; - case CMP_TYPE_FIXEXT2: + } + case CMP_TYPE_FIXEXT2: { if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } obj->as.ext.size = 2; return true; - case CMP_TYPE_FIXEXT4: + } + case CMP_TYPE_FIXEXT4: { if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } obj->as.ext.size = 4; return true; - case CMP_TYPE_FIXEXT8: + } + case CMP_TYPE_FIXEXT8: { if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } obj->as.ext.size = 8; return true; - case CMP_TYPE_FIXEXT16: + } + case CMP_TYPE_FIXEXT16: { if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } obj->as.ext.size = 16; return true; - case CMP_TYPE_EXT8: + } + case CMP_TYPE_EXT8: { if (!read_type_size(ctx, type_marker, obj->type, &obj->as.ext.size)) { return false; } if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } return true; - case CMP_TYPE_EXT16: + } + case CMP_TYPE_EXT16: { if (!read_type_size(ctx, type_marker, obj->type, &obj->as.ext.size)) { return false; } if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } return true; - case CMP_TYPE_EXT32: + } + case CMP_TYPE_EXT32: { if (!read_type_size(ctx, type_marker, obj->type, &obj->as.ext.size)) { return false; } if (!ctx->read(ctx, &obj->as.ext.type, sizeof(int8_t))) { - ctx->error = EXT_TYPE_READING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_READING; return false; } return true; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } -void cmp_init(cmp_ctx_t *ctx, void *buf, cmp_reader read, - cmp_skipper skip, - cmp_writer write) { - ctx->error = ERROR_NONE; +void cmp_init(cmp_ctx_t *ctx, void *buf, cmp_reader *read, + cmp_skipper *skip, + cmp_writer *write) { + ctx->error = CMP_ERROR_NONE; ctx->buf = buf; ctx->read = read; ctx->skip = skip; @@ -805,17 +877,14 @@ uint32_t cmp_mp_version(void) { } const char* cmp_strerror(cmp_ctx_t *ctx) { - if (ctx->error > ERROR_NONE && ctx->error < ERROR_MAX) - return cmp_error_messages[ctx->error]; - - return ""; + return cmp_error_message(ctx->error); } bool cmp_write_pfix(cmp_ctx_t *ctx, uint8_t c) { if (c <= 0x7F) return write_fixed_value(ctx, c); - ctx->error = INPUT_VALUE_TOO_LARGE_ERROR; + ctx->error = CMP_ERROR_INPUT_VALUE_TOO_LARGE; return false; } @@ -823,7 +892,7 @@ bool cmp_write_nfix(cmp_ctx_t *ctx, int8_t c) { if (c >= -32 && c <= -1) return write_fixed_value(ctx, (uint8_t)c); - ctx->error = INPUT_VALUE_TOO_LARGE_ERROR; + ctx->error = CMP_ERROR_INPUT_VALUE_TOO_LARGE; return false; } @@ -833,7 +902,7 @@ bool cmp_write_sfix(cmp_ctx_t *ctx, int8_t c) { if (c >= -32 && c <= -1) return cmp_write_nfix(ctx, c); - ctx->error = INPUT_VALUE_TOO_LARGE_ERROR; + ctx->error = CMP_ERROR_INPUT_VALUE_TOO_LARGE; return false; } @@ -950,10 +1019,11 @@ bool cmp_write_float(cmp_ctx_t *ctx, float f) { if (!is_bigendian()) { char swapped[sizeof(float)]; char *fbuf = (char *)&f; - size_t i; - for (i = 0; i < sizeof(float); i++) + size_t i; + for (i = 0; i < sizeof(float); ++i) { swapped[i] = fbuf[sizeof(float) - i - 1]; + } return ctx->write(ctx, swapped, sizeof(float)); } @@ -969,10 +1039,11 @@ bool cmp_write_double(cmp_ctx_t *ctx, double d) { if (!is_bigendian()) { char swapped[sizeof(double)]; char *dbuf = (char *)&d; - size_t i; - for (i = 0; i < sizeof(double); i++) + size_t i; + for (i = 0; i < sizeof(double); ++i) { swapped[i] = dbuf[sizeof(double) - i - 1]; + } return ctx->write(ctx, swapped, sizeof(double)); } @@ -981,8 +1052,8 @@ bool cmp_write_double(cmp_ctx_t *ctx, double d) { } bool cmp_write_decimal(cmp_ctx_t *ctx, double d) { - float f = (float)d; - double df = (double)f; + const float f = (float)d; + const double df = (double)f; if (df == d) return cmp_write_float(ctx, f); @@ -1011,17 +1082,14 @@ bool cmp_write_bool(cmp_ctx_t *ctx, bool b) { } bool cmp_write_u8_as_bool(cmp_ctx_t *ctx, uint8_t b) { - if (b) - return cmp_write_true(ctx); - - return cmp_write_false(ctx); + return cmp_write_bool(ctx, b != 0); } bool cmp_write_fixstr_marker(cmp_ctx_t *ctx, uint8_t size) { if (size <= FIXSTR_SIZE) return write_fixed_value(ctx, FIXSTR_MARKER | size); - ctx->error = INPUT_VALUE_TOO_LARGE_ERROR; + ctx->error = CMP_ERROR_INPUT_VALUE_TOO_LARGE; return false; } @@ -1035,7 +1103,7 @@ bool cmp_write_fixstr(cmp_ctx_t *ctx, const char *data, uint8_t size) { if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1046,7 +1114,7 @@ bool cmp_write_str8_marker(cmp_ctx_t *ctx, uint8_t size) { if (ctx->write(ctx, &size, sizeof(uint8_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1060,7 +1128,7 @@ bool cmp_write_str8(cmp_ctx_t *ctx, const char *data, uint8_t size) { if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1073,7 +1141,7 @@ bool cmp_write_str16_marker(cmp_ctx_t *ctx, uint16_t size) { if (ctx->write(ctx, &size, sizeof(uint16_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1087,7 +1155,7 @@ bool cmp_write_str16(cmp_ctx_t *ctx, const char *data, uint16_t size) { if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1100,7 +1168,7 @@ bool cmp_write_str32_marker(cmp_ctx_t *ctx, uint32_t size) { if (ctx->write(ctx, &size, sizeof(uint32_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1114,7 +1182,7 @@ bool cmp_write_str32(cmp_ctx_t *ctx, const char *data, uint32_t size) { if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1165,7 +1233,7 @@ bool cmp_write_bin8_marker(cmp_ctx_t *ctx, uint8_t size) { if (ctx->write(ctx, &size, sizeof(uint8_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1179,7 +1247,7 @@ bool cmp_write_bin8(cmp_ctx_t *ctx, const void *data, uint8_t size) { if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1192,7 +1260,7 @@ bool cmp_write_bin16_marker(cmp_ctx_t *ctx, uint16_t size) { if (ctx->write(ctx, &size, sizeof(uint16_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1206,7 +1274,7 @@ bool cmp_write_bin16(cmp_ctx_t *ctx, const void *data, uint16_t size) { if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1219,7 +1287,7 @@ bool cmp_write_bin32_marker(cmp_ctx_t *ctx, uint32_t size) { if (ctx->write(ctx, &size, sizeof(uint32_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1233,7 +1301,7 @@ bool cmp_write_bin32(cmp_ctx_t *ctx, const void *data, uint32_t size) { if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1259,7 +1327,7 @@ bool cmp_write_fixarray(cmp_ctx_t *ctx, uint8_t size) { if (size <= FIXARRAY_SIZE) return write_fixed_value(ctx, FIXARRAY_MARKER | size); - ctx->error = INPUT_VALUE_TOO_LARGE_ERROR; + ctx->error = CMP_ERROR_INPUT_VALUE_TOO_LARGE; return false; } @@ -1272,7 +1340,7 @@ bool cmp_write_array16(cmp_ctx_t *ctx, uint16_t size) { if (ctx->write(ctx, &size, sizeof(uint16_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1285,7 +1353,7 @@ bool cmp_write_array32(cmp_ctx_t *ctx, uint32_t size) { if (ctx->write(ctx, &size, sizeof(uint32_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1302,7 +1370,7 @@ bool cmp_write_fixmap(cmp_ctx_t *ctx, uint8_t size) { if (size <= FIXMAP_SIZE) return write_fixed_value(ctx, FIXMAP_MARKER | size); - ctx->error = INPUT_VALUE_TOO_LARGE_ERROR; + ctx->error = CMP_ERROR_INPUT_VALUE_TOO_LARGE; return false; } @@ -1315,7 +1383,7 @@ bool cmp_write_map16(cmp_ctx_t *ctx, uint16_t size) { if (ctx->write(ctx, &size, sizeof(uint16_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1328,7 +1396,7 @@ bool cmp_write_map32(cmp_ctx_t *ctx, uint32_t size) { if (ctx->write(ctx, &size, sizeof(uint32_t))) return true; - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } @@ -1348,7 +1416,7 @@ bool cmp_write_fixext1_marker(cmp_ctx_t *ctx, int8_t type) { if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1359,7 +1427,7 @@ bool cmp_write_fixext1(cmp_ctx_t *ctx, int8_t type, const void *data) { if (ctx->write(ctx, data, 1)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1370,7 +1438,7 @@ bool cmp_write_fixext2_marker(cmp_ctx_t *ctx, int8_t type) { if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1381,7 +1449,7 @@ bool cmp_write_fixext2(cmp_ctx_t *ctx, int8_t type, const void *data) { if (ctx->write(ctx, data, 2)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1392,7 +1460,7 @@ bool cmp_write_fixext4_marker(cmp_ctx_t *ctx, int8_t type) { if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1403,7 +1471,7 @@ bool cmp_write_fixext4(cmp_ctx_t *ctx, int8_t type, const void *data) { if (ctx->write(ctx, data, 4)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1414,7 +1482,7 @@ bool cmp_write_fixext8_marker(cmp_ctx_t *ctx, int8_t type) { if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1425,7 +1493,7 @@ bool cmp_write_fixext8(cmp_ctx_t *ctx, int8_t type, const void *data) { if (ctx->write(ctx, data, 8)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1436,7 +1504,7 @@ bool cmp_write_fixext16_marker(cmp_ctx_t *ctx, int8_t type) { if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1447,7 +1515,7 @@ bool cmp_write_fixext16(cmp_ctx_t *ctx, int8_t type, const void *data) { if (ctx->write(ctx, data, 16)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1456,14 +1524,14 @@ bool cmp_write_ext8_marker(cmp_ctx_t *ctx, int8_t type, uint8_t size) { return false; if (!ctx->write(ctx, &size, sizeof(uint8_t))) { - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1474,7 +1542,7 @@ bool cmp_write_ext8(cmp_ctx_t *ctx, int8_t type, uint8_t size, const void *data) if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1485,14 +1553,14 @@ bool cmp_write_ext16_marker(cmp_ctx_t *ctx, int8_t type, uint16_t size) { size = be16(size); if (!ctx->write(ctx, &size, sizeof(uint16_t))) { - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1503,7 +1571,7 @@ bool cmp_write_ext16(cmp_ctx_t *ctx, int8_t type, uint16_t size, const void *dat if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1514,14 +1582,14 @@ bool cmp_write_ext32_marker(cmp_ctx_t *ctx, int8_t type, uint32_t size) { size = be32(size); if (!ctx->write(ctx, &size, sizeof(uint32_t))) { - ctx->error = LENGTH_WRITING_ERROR; + ctx->error = CMP_ERROR_LENGTH_WRITING; return false; } if (ctx->write(ctx, &type, sizeof(int8_t))) return true; - ctx->error = EXT_TYPE_WRITING_ERROR; + ctx->error = CMP_ERROR_EXT_TYPE_WRITING; return false; } @@ -1532,7 +1600,7 @@ bool cmp_write_ext32(cmp_ctx_t *ctx, int8_t type, uint32_t size, const void *dat if (ctx->write(ctx, data, size)) return true; - ctx->error = DATA_WRITING_ERROR; + ctx->error = CMP_ERROR_DATA_WRITING; return false; } @@ -1586,10 +1654,11 @@ bool cmp_write_object(cmp_ctx_t *ctx, const cmp_object_t *obj) { return cmp_write_fixstr_marker(ctx, (uint8_t)obj->as.str_size); case CMP_TYPE_NIL: return cmp_write_nil(ctx); - case CMP_TYPE_BOOLEAN: + case CMP_TYPE_BOOLEAN: { if (obj->as.boolean) return cmp_write_true(ctx); return cmp_write_false(ctx); + } case CMP_TYPE_BIN8: return cmp_write_bin8_marker(ctx, (uint8_t)obj->as.bin_size); case CMP_TYPE_BIN16: @@ -1606,20 +1675,22 @@ bool cmp_write_object(cmp_ctx_t *ctx, const cmp_object_t *obj) { ); case CMP_TYPE_EXT32: return cmp_write_ext32_marker(ctx, obj->as.ext.type, obj->as.ext.size); - case CMP_TYPE_FLOAT: + case CMP_TYPE_FLOAT: { #ifndef CMP_NO_FLOAT return cmp_write_float(ctx, obj->as.flt); #else /* CMP_NO_FLOAT */ - ctx->error = DISABLED_FLOATING_POINT_ERROR; + ctx->error = CMP_ERROR_DISABLED_FLOATING_POINT; return false; #endif /* CMP_NO_FLOAT */ - case CMP_TYPE_DOUBLE: + } + case CMP_TYPE_DOUBLE: { #ifndef CMP_NO_FLOAT return cmp_write_double(ctx, obj->as.dbl); #else /* CMP_NO_FLOAT */ - ctx->error = DISABLED_FLOATING_POINT_ERROR; + ctx->error = CMP_ERROR_DISABLED_FLOATING_POINT; return false; -#endif +#endif /* CMP_NO_FLOAT */ + } case CMP_TYPE_UINT8: return cmp_write_u8(ctx, obj->as.u8); case CMP_TYPE_UINT16: @@ -1662,9 +1733,10 @@ bool cmp_write_object(cmp_ctx_t *ctx, const cmp_object_t *obj) { return cmp_write_map32(ctx, obj->as.map_size); case CMP_TYPE_NEGATIVE_FIXNUM: return cmp_write_nfix(ctx, obj->as.s8); - default: - ctx->error = INVALID_TYPE_ERROR; + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -1680,10 +1752,11 @@ bool cmp_write_object_v4(cmp_ctx_t *ctx, const cmp_object_t *obj) { return cmp_write_fixstr_marker(ctx, (uint8_t)obj->as.str_size); case CMP_TYPE_NIL: return cmp_write_nil(ctx); - case CMP_TYPE_BOOLEAN: + case CMP_TYPE_BOOLEAN: { if (obj->as.boolean) return cmp_write_true(ctx); return cmp_write_false(ctx); + } case CMP_TYPE_EXT8: return cmp_write_ext8_marker(ctx, obj->as.ext.type, (uint8_t)obj->as.ext.size); case CMP_TYPE_EXT16: @@ -1692,20 +1765,22 @@ bool cmp_write_object_v4(cmp_ctx_t *ctx, const cmp_object_t *obj) { ); case CMP_TYPE_EXT32: return cmp_write_ext32_marker(ctx, obj->as.ext.type, obj->as.ext.size); - case CMP_TYPE_FLOAT: + case CMP_TYPE_FLOAT: { #ifndef CMP_NO_FLOAT return cmp_write_float(ctx, obj->as.flt); #else /* CMP_NO_FLOAT */ - ctx->error = DISABLED_FLOATING_POINT_ERROR; + ctx->error = CMP_ERROR_DISABLED_FLOATING_POINT; return false; -#endif - case CMP_TYPE_DOUBLE: +#endif /* CMP_NO_FLOAT */ + } + case CMP_TYPE_DOUBLE: { #ifndef CMP_NO_FLOAT return cmp_write_double(ctx, obj->as.dbl); #else - ctx->error = DISABLED_FLOATING_POINT_ERROR; + ctx->error = CMP_ERROR_DISABLED_FLOATING_POINT; return false; -#endif +#endif /* CMP_NO_FLOAT */ + } case CMP_TYPE_UINT8: return cmp_write_u8(ctx, obj->as.u8); case CMP_TYPE_UINT16: @@ -1746,9 +1821,10 @@ bool cmp_write_object_v4(cmp_ctx_t *ctx, const cmp_object_t *obj) { return cmp_write_map32(ctx, obj->as.map_size); case CMP_TYPE_NEGATIVE_FIXNUM: return cmp_write_nfix(ctx, obj->as.s8); - default: - ctx->error = INVALID_TYPE_ERROR; + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -1759,7 +1835,7 @@ bool cmp_read_pfix(cmp_ctx_t *ctx, uint8_t *c) { return false; if (obj.type != CMP_TYPE_POSITIVE_FIXNUM) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1774,7 +1850,7 @@ bool cmp_read_nfix(cmp_ctx_t *ctx, int8_t *c) { return false; if (obj.type != CMP_TYPE_NEGATIVE_FIXNUM) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1790,12 +1866,14 @@ bool cmp_read_sfix(cmp_ctx_t *ctx, int8_t *c) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_NEGATIVE_FIXNUM: + case CMP_TYPE_NEGATIVE_FIXNUM: { *c = obj.as.s8; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -1806,7 +1884,7 @@ bool cmp_read_s8(cmp_ctx_t *ctx, int8_t *c) { return false; if (obj.type != CMP_TYPE_SINT8) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1821,7 +1899,7 @@ bool cmp_read_s16(cmp_ctx_t *ctx, int16_t *s) { return false; if (obj.type != CMP_TYPE_SINT16) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1836,7 +1914,7 @@ bool cmp_read_s32(cmp_ctx_t *ctx, int32_t *i) { return false; if (obj.type != CMP_TYPE_SINT32) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1851,7 +1929,7 @@ bool cmp_read_s64(cmp_ctx_t *ctx, int64_t *l) { return false; if (obj.type != CMP_TYPE_SINT64) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1868,20 +1946,22 @@ bool cmp_read_char(cmp_ctx_t *ctx, int8_t *c) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *c = obj.as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { if (obj.as.u8 <= 127) { *c = (int8_t)obj.as.u8; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1894,26 +1974,30 @@ bool cmp_read_short(cmp_ctx_t *ctx, int16_t *s) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *s = obj.as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { *s = obj.as.u8; return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { *s = obj.as.s16; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { if (obj.as.u16 <= 32767) { *s = (int16_t)obj.as.u16; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1926,32 +2010,38 @@ bool cmp_read_int(cmp_ctx_t *ctx, int32_t *i) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *i = obj.as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { *i = obj.as.u8; return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { *i = obj.as.s16; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *i = obj.as.u16; return true; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { *i = obj.as.s32; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { if (obj.as.u32 <= 2147483647) { *i = (int32_t)obj.as.u32; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -1964,38 +2054,46 @@ bool cmp_read_long(cmp_ctx_t *ctx, int64_t *d) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *d = obj.as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { *d = obj.as.u8; return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { *d = obj.as.s16; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *d = obj.as.u16; return true; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { *d = obj.as.s32; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { *d = obj.as.u32; return true; - case CMP_TYPE_SINT64: + } + case CMP_TYPE_SINT64: { *d = obj.as.s64; return true; - case CMP_TYPE_UINT64: + } + case CMP_TYPE_UINT64: { if (obj.as.u64 <= 9223372036854775807) { *d = (int64_t)obj.as.u64; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2014,7 +2112,7 @@ bool cmp_read_u8(cmp_ctx_t *ctx, uint8_t *c) { return false; if (obj.type != CMP_TYPE_UINT8) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2029,7 +2127,7 @@ bool cmp_read_u16(cmp_ctx_t *ctx, uint16_t *s) { return false; if (obj.type != CMP_TYPE_UINT16) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2044,7 +2142,7 @@ bool cmp_read_u32(cmp_ctx_t *ctx, uint32_t *i) { return false; if (obj.type != CMP_TYPE_UINT32) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2059,7 +2157,7 @@ bool cmp_read_u64(cmp_ctx_t *ctx, uint64_t *l) { return false; if (obj.type != CMP_TYPE_UINT64) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2075,21 +2173,23 @@ bool cmp_read_uchar(cmp_ctx_t *ctx, uint8_t *c) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *c = obj.as.u8; return true; + } case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { if (obj.as.s8 >= 0) { *c = (uint8_t)obj.as.s8; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2101,30 +2201,34 @@ bool cmp_read_ushort(cmp_ctx_t *ctx, uint16_t *s) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *s = obj.as.u8; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *s = obj.as.u16; return true; + } case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { if (obj.as.s8 >= 0) { *s = (uint8_t)obj.as.s8; return true; } break; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { if (obj.as.s16 >= 0) { *s = (uint16_t)obj.as.s16; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2136,39 +2240,45 @@ bool cmp_read_uint(cmp_ctx_t *ctx, uint32_t *i) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *i = obj.as.u8; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *i = obj.as.u16; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { *i = obj.as.u32; return true; + } case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { if (obj.as.s8 >= 0) { *i = (uint8_t)obj.as.s8; return true; } break; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { if (obj.as.s16 >= 0) { *i = (uint16_t)obj.as.s16; return true; } break; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { if (obj.as.s32 >= 0) { *i = (uint32_t)obj.as.s32; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2180,48 +2290,56 @@ bool cmp_read_ulong(cmp_ctx_t *ctx, uint64_t *u) { switch (obj.type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *u = obj.as.u8; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *u = obj.as.u16; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { *u = obj.as.u32; return true; - case CMP_TYPE_UINT64: + } + case CMP_TYPE_UINT64: { *u = obj.as.u64; return true; + } case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { if (obj.as.s8 >= 0) { *u = (uint8_t)obj.as.s8; return true; } break; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { if (obj.as.s16 >= 0) { *u = (uint16_t)obj.as.s16; return true; } break; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { if (obj.as.s32 >= 0) { *u = (uint32_t)obj.as.s32; return true; } break; - case CMP_TYPE_SINT64: + } + case CMP_TYPE_SINT64: { if (obj.as.s64 >= 0) { *u = (uint64_t)obj.as.s64; return true; } break; + } default: break; } - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2237,7 +2355,7 @@ bool cmp_read_float(cmp_ctx_t *ctx, float *f) { return false; if (obj.type != CMP_TYPE_FLOAT) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2253,7 +2371,7 @@ bool cmp_read_double(cmp_ctx_t *ctx, double *d) { return false; if (obj.type != CMP_TYPE_DOUBLE) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2269,15 +2387,18 @@ bool cmp_read_decimal(cmp_ctx_t *ctx, double *d) { return false; switch (obj.type) { - case CMP_TYPE_FLOAT: + case CMP_TYPE_FLOAT: { *d = (double)obj.as.flt; return true; - case CMP_TYPE_DOUBLE: + } + case CMP_TYPE_DOUBLE: { *d = obj.as.dbl; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } #endif /* CMP_NO_FLOAT */ @@ -2291,7 +2412,7 @@ bool cmp_read_nil(cmp_ctx_t *ctx) { if (obj.type == CMP_TYPE_NIL) return true; - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2302,14 +2423,15 @@ bool cmp_read_bool(cmp_ctx_t *ctx, bool *b) { return false; if (obj.type != CMP_TYPE_BOOLEAN) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } - if (obj.as.boolean) + if (obj.as.boolean) { *b = true; - else + } else { *b = false; + } return true; } @@ -2321,14 +2443,15 @@ bool cmp_read_bool_as_u8(cmp_ctx_t *ctx, uint8_t *b) { return false; if (obj.type != CMP_TYPE_BOOLEAN) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } - if (obj.as.boolean) + if (obj.as.boolean) { *b = 1; - else + } else { *b = 0; + } return true; } @@ -2343,12 +2466,14 @@ bool cmp_read_str_size(cmp_ctx_t *ctx, uint32_t *size) { case CMP_TYPE_FIXSTR: case CMP_TYPE_STR8: case CMP_TYPE_STR16: - case CMP_TYPE_STR32: + case CMP_TYPE_STR32: { *size = obj.as.str_size; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -2360,12 +2485,12 @@ bool cmp_read_str(cmp_ctx_t *ctx, char *data, uint32_t *size) { if (str_size >= *size) { *size = str_size; - ctx->error = STR_DATA_LENGTH_TOO_LONG_ERROR; + ctx->error = CMP_ERROR_STR_DATA_LENGTH_TOO_LONG; return false; } if (!ctx->read(ctx, data, str_size)) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2384,12 +2509,14 @@ bool cmp_read_bin_size(cmp_ctx_t *ctx, uint32_t *size) { switch (obj.type) { case CMP_TYPE_BIN8: case CMP_TYPE_BIN16: - case CMP_TYPE_BIN32: + case CMP_TYPE_BIN32: { *size = obj.as.bin_size; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -2400,12 +2527,12 @@ bool cmp_read_bin(cmp_ctx_t *ctx, void *data, uint32_t *size) { return false; if (bin_size > *size) { - ctx->error = BIN_DATA_LENGTH_TOO_LONG_ERROR; + ctx->error = CMP_ERROR_BIN_DATA_LENGTH_TOO_LONG; return false; } if (!ctx->read(ctx, data, bin_size)) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2422,12 +2549,14 @@ bool cmp_read_array(cmp_ctx_t *ctx, uint32_t *size) { switch (obj.type) { case CMP_TYPE_FIXARRAY: case CMP_TYPE_ARRAY16: - case CMP_TYPE_ARRAY32: + case CMP_TYPE_ARRAY32: { *size = obj.as.array_size; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -2440,12 +2569,14 @@ bool cmp_read_map(cmp_ctx_t *ctx, uint32_t *size) { switch (obj.type) { case CMP_TYPE_FIXMAP: case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: + case CMP_TYPE_MAP32: { *size = obj.as.map_size; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -2456,7 +2587,7 @@ bool cmp_read_fixext1_marker(cmp_ctx_t *ctx, int8_t *type) { return false; if (obj.type != CMP_TYPE_FIXEXT1) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2471,7 +2602,7 @@ bool cmp_read_fixext1(cmp_ctx_t *ctx, int8_t *type, void *data) { if (ctx->read(ctx, data, 1)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2482,7 +2613,7 @@ bool cmp_read_fixext2_marker(cmp_ctx_t *ctx, int8_t *type) { return false; if (obj.type != CMP_TYPE_FIXEXT2) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2497,7 +2628,7 @@ bool cmp_read_fixext2(cmp_ctx_t *ctx, int8_t *type, void *data) { if (ctx->read(ctx, data, 2)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2508,7 +2639,7 @@ bool cmp_read_fixext4_marker(cmp_ctx_t *ctx, int8_t *type) { return false; if (obj.type != CMP_TYPE_FIXEXT4) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2523,7 +2654,7 @@ bool cmp_read_fixext4(cmp_ctx_t *ctx, int8_t *type, void *data) { if (ctx->read(ctx, data, 4)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2534,7 +2665,7 @@ bool cmp_read_fixext8_marker(cmp_ctx_t *ctx, int8_t *type) { return false; if (obj.type != CMP_TYPE_FIXEXT8) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2549,7 +2680,7 @@ bool cmp_read_fixext8(cmp_ctx_t *ctx, int8_t *type, void *data) { if (ctx->read(ctx, data, 8)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2560,7 +2691,7 @@ bool cmp_read_fixext16_marker(cmp_ctx_t *ctx, int8_t *type) { return false; if (obj.type != CMP_TYPE_FIXEXT16) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2575,7 +2706,7 @@ bool cmp_read_fixext16(cmp_ctx_t *ctx, int8_t *type, void *data) { if (ctx->read(ctx, data, 16)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2586,7 +2717,7 @@ bool cmp_read_ext8_marker(cmp_ctx_t *ctx, int8_t *type, uint8_t *size) { return false; if (obj.type != CMP_TYPE_EXT8) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2603,7 +2734,7 @@ bool cmp_read_ext8(cmp_ctx_t *ctx, int8_t *type, uint8_t *size, void *data) { if (ctx->read(ctx, data, *size)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2614,7 +2745,7 @@ bool cmp_read_ext16_marker(cmp_ctx_t *ctx, int8_t *type, uint16_t *size) { return false; if (obj.type != CMP_TYPE_EXT16) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2631,7 +2762,7 @@ bool cmp_read_ext16(cmp_ctx_t *ctx, int8_t *type, uint16_t *size, void *data) { if (ctx->read(ctx, data, *size)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2642,7 +2773,7 @@ bool cmp_read_ext32_marker(cmp_ctx_t *ctx, int8_t *type, uint32_t *size) { return false; if (obj.type != CMP_TYPE_EXT32) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2659,7 +2790,7 @@ bool cmp_read_ext32(cmp_ctx_t *ctx, int8_t *type, uint32_t *size, void *data) { if (ctx->read(ctx, data, *size)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2677,13 +2808,15 @@ bool cmp_read_ext_marker(cmp_ctx_t *ctx, int8_t *type, uint32_t *size) { case CMP_TYPE_FIXEXT16: case CMP_TYPE_EXT8: case CMP_TYPE_EXT16: - case CMP_TYPE_EXT32: + case CMP_TYPE_EXT32: { *type = obj.as.ext.type; *size = obj.as.ext.size; return true; - default: - ctx->error = INVALID_TYPE_ERROR; + } + default: { + ctx->error = CMP_ERROR_INVALID_TYPE; return false; + } } } @@ -2694,7 +2827,7 @@ bool cmp_read_ext(cmp_ctx_t *ctx, int8_t *type, uint32_t *size, void *data) { if (ctx->read(ctx, data, *size)) return true; - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } @@ -2705,7 +2838,7 @@ bool cmp_read_object(cmp_ctx_t *ctx, cmp_object_t *obj) { return false; if (!type_marker_to_cmp_type(type_marker, &obj->type)) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2722,7 +2855,7 @@ bool cmp_skip_object(cmp_ctx_t *ctx, cmp_object_t *obj) { } if (!type_marker_to_cmp_type(type_marker, &cmp_type)) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2732,22 +2865,23 @@ bool cmp_skip_object(cmp_ctx_t *ctx, cmp_object_t *obj) { case CMP_TYPE_ARRAY32: case CMP_TYPE_FIXMAP: case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: + case CMP_TYPE_MAP32: { obj->type = cmp_type; if (!read_obj_data(ctx, type_marker, obj)) { return false; } - ctx->error = SKIP_DEPTH_LIMIT_EXCEEDED_ERROR; + ctx->error = CMP_ERROR_SKIP_DEPTH_LIMIT_EXCEEDED; return false; - default: + } + default: { if (!read_type_size(ctx, type_marker, cmp_type, &size)) { return false; } - if (size) { + if (size != 0) { switch (cmp_type) { case CMP_TYPE_FIXEXT1: case CMP_TYPE_FIXEXT2: @@ -2756,15 +2890,17 @@ bool cmp_skip_object(cmp_ctx_t *ctx, cmp_object_t *obj) { case CMP_TYPE_FIXEXT16: case CMP_TYPE_EXT8: case CMP_TYPE_EXT16: - case CMP_TYPE_EXT32: - size++; + case CMP_TYPE_EXT32: { + ++size; break; + } default: break; } skip_bytes(ctx, size); } + } } return true; @@ -2774,7 +2910,7 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj) { size_t element_count = 1; bool in_container = false; - while (element_count) { + while (element_count != 0) { uint8_t type_marker = 0; uint8_t cmp_type; uint32_t size = 0; @@ -2784,7 +2920,7 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj) { } if (!type_marker_to_cmp_type(type_marker, &cmp_type)) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2794,7 +2930,7 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj) { case CMP_TYPE_ARRAY32: case CMP_TYPE_FIXMAP: case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: + case CMP_TYPE_MAP32: { if (in_container) { obj->type = cmp_type; @@ -2802,19 +2938,20 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj) { return false; } - ctx->error = SKIP_DEPTH_LIMIT_EXCEEDED_ERROR; + ctx->error = CMP_ERROR_SKIP_DEPTH_LIMIT_EXCEEDED; return false; } in_container = true; break; - default: + } + default: { if (!read_type_size(ctx, type_marker, cmp_type, &size)) { return false; } - if (size) { + if (size != 0) { switch (cmp_type) { case CMP_TYPE_FIXEXT1: case CMP_TYPE_FIXEXT2: @@ -2823,36 +2960,40 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj) { case CMP_TYPE_FIXEXT16: case CMP_TYPE_EXT8: case CMP_TYPE_EXT16: - case CMP_TYPE_EXT32: - size++; + case CMP_TYPE_EXT32: { + ++size; break; + } default: break; } skip_bytes(ctx, size); } + } } - element_count--; + --element_count; switch (cmp_type) { case CMP_TYPE_FIXARRAY: case CMP_TYPE_ARRAY16: - case CMP_TYPE_ARRAY32: + case CMP_TYPE_ARRAY32: { if (!read_type_size(ctx, type_marker, cmp_type, &size)) { return false; } element_count += size; break; + } case CMP_TYPE_FIXMAP: case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: + case CMP_TYPE_MAP32: { if (!read_type_size(ctx, type_marker, cmp_type, &size)) { return false; } element_count += ((size_t)size) * 2; break; + } default: break; } @@ -2864,7 +3005,7 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj) { bool cmp_skip_object_no_limit(cmp_ctx_t *ctx) { size_t element_count = 1; - while (element_count) { + while (element_count != 0) { uint8_t type_marker = 0; uint8_t cmp_type = 0; uint32_t size = 0; @@ -2874,85 +3015,7 @@ bool cmp_skip_object_no_limit(cmp_ctx_t *ctx) { } if (!type_marker_to_cmp_type(type_marker, &cmp_type)) { - ctx->error = INVALID_TYPE_ERROR; - return false; - } - - switch (cmp_type) { - case CMP_TYPE_FIXARRAY: - case CMP_TYPE_ARRAY16: - case CMP_TYPE_ARRAY32: - case CMP_TYPE_FIXMAP: - case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: - break; - default: - if (!read_type_size(ctx, type_marker, cmp_type, &size)) { - return false; - } - - if (size) { - switch (cmp_type) { - case CMP_TYPE_FIXEXT1: - case CMP_TYPE_FIXEXT2: - case CMP_TYPE_FIXEXT4: - case CMP_TYPE_FIXEXT8: - case CMP_TYPE_FIXEXT16: - case CMP_TYPE_EXT8: - case CMP_TYPE_EXT16: - case CMP_TYPE_EXT32: - size++; - break; - default: - break; - } - - skip_bytes(ctx, size); - } - } - - element_count--; - - switch (cmp_type) { - case CMP_TYPE_FIXARRAY: - case CMP_TYPE_ARRAY16: - case CMP_TYPE_ARRAY32: - if (!read_type_size(ctx, type_marker, cmp_type, &size)) { - return false; - } - element_count += size; - break; - case CMP_TYPE_FIXMAP: - case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: - if (!read_type_size(ctx, type_marker, cmp_type, &size)) { - return false; - } - element_count += ((size_t)size) * 2; - break; - default: - break; - } - } - - return true; -} - -bool cmp_skip_object_limit(cmp_ctx_t *ctx, cmp_object_t *obj, uint32_t limit) { - size_t element_count = 1; - uint32_t depth = 0; - - while (element_count) { - uint8_t type_marker = 0; - uint8_t cmp_type; - uint32_t size = 0; - - if (!read_type_marker(ctx, &type_marker)) { - return false; - } - - if (!type_marker_to_cmp_type(type_marker, &cmp_type)) { - ctx->error = INVALID_TYPE_ERROR; + ctx->error = CMP_ERROR_INVALID_TYPE; return false; } @@ -2963,27 +3026,13 @@ bool cmp_skip_object_limit(cmp_ctx_t *ctx, cmp_object_t *obj, uint32_t limit) { case CMP_TYPE_FIXMAP: case CMP_TYPE_MAP16: case CMP_TYPE_MAP32: - depth++; - - if (depth > limit) { - obj->type = cmp_type; - - if (!read_obj_data(ctx, type_marker, obj)) { - return false; - } - - ctx->error = SKIP_DEPTH_LIMIT_EXCEEDED_ERROR; - - return false; - } - break; - default: + default: { if (!read_type_size(ctx, type_marker, cmp_type, &size)) { return false; } - if (size) { + if (size != 0) { switch (cmp_type) { case CMP_TYPE_FIXEXT1: case CMP_TYPE_FIXEXT2: @@ -2992,36 +3041,40 @@ bool cmp_skip_object_limit(cmp_ctx_t *ctx, cmp_object_t *obj, uint32_t limit) { case CMP_TYPE_FIXEXT16: case CMP_TYPE_EXT8: case CMP_TYPE_EXT16: - case CMP_TYPE_EXT32: - size++; + case CMP_TYPE_EXT32: { + ++size; break; + } default: break; } skip_bytes(ctx, size); } + } } - element_count--; + --element_count; switch (cmp_type) { case CMP_TYPE_FIXARRAY: case CMP_TYPE_ARRAY16: - case CMP_TYPE_ARRAY32: + case CMP_TYPE_ARRAY32: { if (!read_type_size(ctx, type_marker, cmp_type, &size)) { return false; } element_count += size; break; + } case CMP_TYPE_FIXMAP: case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: + case CMP_TYPE_MAP32: { if (!read_type_size(ctx, type_marker, cmp_type, &size)) { return false; } element_count += ((size_t)size) * 2; break; + } default: break; } @@ -3224,10 +3277,11 @@ bool cmp_object_as_char(const cmp_object_t *obj, int8_t *c) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *c = obj->as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { if (obj->as.u8 <= 127) { *c = obj->as.s8; return true; @@ -3235,8 +3289,9 @@ bool cmp_object_as_char(const cmp_object_t *obj, int8_t *c) { else { return false; } + } default: - return false; + return false; } } @@ -3244,16 +3299,19 @@ bool cmp_object_as_short(const cmp_object_t *obj, int16_t *s) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *s = obj->as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { *s = obj->as.u8; return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { *s = obj->as.s16; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { if (obj->as.u16 <= 32767) { *s = (int16_t)obj->as.u16; return true; @@ -3261,8 +3319,9 @@ bool cmp_object_as_short(const cmp_object_t *obj, int16_t *s) { else { return false; } + } default: - return false; + return false; } } @@ -3270,22 +3329,27 @@ bool cmp_object_as_int(const cmp_object_t *obj, int32_t *i) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *i = obj->as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { *i = obj->as.u8; return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { *i = obj->as.s16; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *i = obj->as.u16; return true; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { *i = obj->as.s32; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { if (obj->as.u32 <= 2147483647) { *i = (int32_t)obj->as.u32; return true; @@ -3293,8 +3357,9 @@ bool cmp_object_as_int(const cmp_object_t *obj, int32_t *i) { else { return false; } + } default: - return false; + return false; } } @@ -3302,28 +3367,35 @@ bool cmp_object_as_long(const cmp_object_t *obj, int64_t *d) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: case CMP_TYPE_NEGATIVE_FIXNUM: - case CMP_TYPE_SINT8: + case CMP_TYPE_SINT8: { *d = obj->as.s8; return true; - case CMP_TYPE_UINT8: + } + case CMP_TYPE_UINT8: { *d = obj->as.u8; return true; - case CMP_TYPE_SINT16: + } + case CMP_TYPE_SINT16: { *d = obj->as.s16; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *d = obj->as.u16; return true; - case CMP_TYPE_SINT32: + } + case CMP_TYPE_SINT32: { *d = obj->as.s32; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { *d = obj->as.u32; return true; - case CMP_TYPE_SINT64: + } + case CMP_TYPE_SINT64: { *d = obj->as.s64; return true; - case CMP_TYPE_UINT64: + } + case CMP_TYPE_UINT64: { if (obj->as.u64 <= 9223372036854775807) { *d = (int64_t)obj->as.u64; return true; @@ -3331,8 +3403,9 @@ bool cmp_object_as_long(const cmp_object_t *obj, int64_t *d) { else { return false; } + } default: - return false; + return false; } } @@ -3343,62 +3416,72 @@ bool cmp_object_as_sinteger(const cmp_object_t *obj, int64_t *d) { bool cmp_object_as_uchar(const cmp_object_t *obj, uint8_t *c) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *c = obj->as.u8; return true; + } default: - return false; + return false; } } bool cmp_object_as_ushort(const cmp_object_t *obj, uint16_t *s) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *s = obj->as.u8; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *s = obj->as.u16; return true; + } default: - return false; + return false; } } bool cmp_object_as_uint(const cmp_object_t *obj, uint32_t *i) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *i = obj->as.u8; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *i = obj->as.u16; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { *i = obj->as.u32; return true; + } default: - return false; + return false; } } bool cmp_object_as_ulong(const cmp_object_t *obj, uint64_t *u) { switch (obj->type) { case CMP_TYPE_POSITIVE_FIXNUM: - case CMP_TYPE_UINT8: + case CMP_TYPE_UINT8: { *u = obj->as.u8; return true; - case CMP_TYPE_UINT16: + } + case CMP_TYPE_UINT16: { *u = obj->as.u16; return true; - case CMP_TYPE_UINT32: + } + case CMP_TYPE_UINT32: { *u = obj->as.u32; return true; - case CMP_TYPE_UINT64: + } + case CMP_TYPE_UINT64: { *u = obj->as.u64; return true; + } default: - return false; + return false; } } @@ -3428,10 +3511,11 @@ bool cmp_object_as_double(const cmp_object_t *obj, double *d) { bool cmp_object_as_bool(const cmp_object_t *obj, bool *b) { if (obj->type == CMP_TYPE_BOOLEAN) { - if (obj->as.boolean) + if (obj->as.boolean) { *b = true; - else + } else { *b = false; + } return true; } @@ -3444,11 +3528,12 @@ bool cmp_object_as_str(const cmp_object_t *obj, uint32_t *size) { case CMP_TYPE_FIXSTR: case CMP_TYPE_STR8: case CMP_TYPE_STR16: - case CMP_TYPE_STR32: + case CMP_TYPE_STR32: { *size = obj->as.str_size; return true; + } default: - return false; + return false; } } @@ -3456,11 +3541,12 @@ bool cmp_object_as_bin(const cmp_object_t *obj, uint32_t *size) { switch (obj->type) { case CMP_TYPE_BIN8: case CMP_TYPE_BIN16: - case CMP_TYPE_BIN32: + case CMP_TYPE_BIN32: { *size = obj->as.bin_size; return true; + } default: - return false; + return false; } } @@ -3468,11 +3554,12 @@ bool cmp_object_as_array(const cmp_object_t *obj, uint32_t *size) { switch (obj->type) { case CMP_TYPE_FIXARRAY: case CMP_TYPE_ARRAY16: - case CMP_TYPE_ARRAY32: + case CMP_TYPE_ARRAY32: { *size = obj->as.array_size; return true; + } default: - return false; + return false; } } @@ -3480,11 +3567,12 @@ bool cmp_object_as_map(const cmp_object_t *obj, uint32_t *size) { switch (obj->type) { case CMP_TYPE_FIXMAP: case CMP_TYPE_MAP16: - case CMP_TYPE_MAP32: + case CMP_TYPE_MAP32: { *size = obj->as.map_size; return true; + } default: - return false; + return false; } } @@ -3497,37 +3585,37 @@ bool cmp_object_as_ext(const cmp_object_t *obj, int8_t *type, uint32_t *size) { case CMP_TYPE_FIXEXT16: case CMP_TYPE_EXT8: case CMP_TYPE_EXT16: - case CMP_TYPE_EXT32: + case CMP_TYPE_EXT32: { *type = obj->as.ext.type; *size = obj->as.ext.size; return true; + } default: - return false; + return false; } } bool cmp_object_to_str(cmp_ctx_t *ctx, const cmp_object_t *obj, char *data, uint32_t buf_size) { - uint32_t str_size = 0; - switch (obj->type) { case CMP_TYPE_FIXSTR: case CMP_TYPE_STR8: case CMP_TYPE_STR16: - case CMP_TYPE_STR32: - str_size = obj->as.str_size; + case CMP_TYPE_STR32: { + const uint32_t str_size = obj->as.str_size; if (str_size >= buf_size) { - ctx->error = STR_DATA_LENGTH_TOO_LONG_ERROR; + ctx->error = CMP_ERROR_STR_DATA_LENGTH_TOO_LONG; return false; } if (!ctx->read(ctx, data, str_size)) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } data[str_size] = 0; return true; + } default: return false; } @@ -3535,27 +3623,25 @@ bool cmp_object_to_str(cmp_ctx_t *ctx, const cmp_object_t *obj, char *data, bool cmp_object_to_bin(cmp_ctx_t *ctx, const cmp_object_t *obj, void *data, uint32_t buf_size) { - uint32_t bin_size = 0; - switch (obj->type) { case CMP_TYPE_BIN8: case CMP_TYPE_BIN16: - case CMP_TYPE_BIN32: - bin_size = obj->as.bin_size; + case CMP_TYPE_BIN32: { + const uint32_t bin_size = obj->as.bin_size; if (bin_size > buf_size) { - ctx->error = BIN_DATA_LENGTH_TOO_LONG_ERROR; + ctx->error = CMP_ERROR_BIN_DATA_LENGTH_TOO_LONG; return false; } if (!ctx->read(ctx, data, bin_size)) { - ctx->error = DATA_READING_ERROR; + ctx->error = CMP_ERROR_DATA_READING; return false; } return true; + } default: return false; } } /* vi: set et ts=2 sw=2: */ - diff --git a/cmp.h b/cmp.h index 26dc0d0..a509098 100644 --- a/cmp.h +++ b/cmp.h @@ -1,27 +1,6 @@ -/* -The MIT License (MIT) - -Copyright (c) 2020 Charles Gunyon - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - +/* SPDX-License-Identifier: MIT + * Copyright © 2020-2024 Charles Gunyon. + */ #ifndef CMP_H_INCLUDED #define CMP_H_INCLUDED @@ -29,12 +8,11 @@ THE SOFTWARE. #include #include -struct cmp_ctx_s; +typedef struct cmp_ctx_s cmp_ctx_t; -typedef bool (*cmp_reader)(struct cmp_ctx_s *ctx, void *data, size_t limit); -typedef bool (*cmp_skipper)(struct cmp_ctx_s *ctx, size_t count); -typedef size_t (*cmp_writer)(struct cmp_ctx_s *ctx, const void *data, - size_t count); +typedef bool cmp_reader(cmp_ctx_t *ctx, void *data, size_t limit); +typedef bool cmp_skipper(cmp_ctx_t *ctx, size_t count); +typedef size_t cmp_writer(cmp_ctx_t *ctx, const void *data, size_t count); enum { CMP_TYPE_POSITIVE_FIXNUM, /* 0 */ @@ -79,7 +57,7 @@ typedef struct cmp_ext_s { uint32_t size; } cmp_ext_t; -union cmp_object_data_u { +typedef union cmp_object_data_u { bool boolean; uint8_t u8; uint16_t u16; @@ -98,19 +76,19 @@ union cmp_object_data_u { uint32_t str_size; uint32_t bin_size; cmp_ext_t ext; -}; +} cmp_object_data_t; -typedef struct cmp_ctx_s { +struct cmp_ctx_s { uint8_t error; void *buf; - cmp_reader read; - cmp_skipper skip; - cmp_writer write; -} cmp_ctx_t; + cmp_reader *read; + cmp_skipper *skip; + cmp_writer *write; +}; typedef struct cmp_object_s { uint8_t type; - union cmp_object_data_u as; + cmp_object_data_t as; } cmp_object_t; #ifdef __cplusplus @@ -134,9 +112,9 @@ extern "C" { * If you don't intend to write, `write` may be NULL, but calling `*write*` * functions will crash; there is no check. */ -void cmp_init(cmp_ctx_t *ctx, void *buf, cmp_reader read, - cmp_skipper skip, - cmp_writer write); +void cmp_init(cmp_ctx_t *ctx, void *buf, cmp_reader *read, + cmp_skipper *skip, + cmp_writer *write); /* Returns CMP's version */ uint32_t cmp_version(void); @@ -362,37 +340,6 @@ bool cmp_skip_object_flat(cmp_ctx_t *ctx, cmp_object_t *obj); */ bool cmp_skip_object_no_limit(cmp_ctx_t *ctx); -/* - * WARNING: THIS FUNCTION IS DEPRECATED AND WILL BE REMOVED IN A FUTURE RELEASE - * - * There is no way to track depths across elements without allocation. For - * example, an array constructed as: `[ [] [] [] [] [] [] [] [] [] [] ]` - * should be able to be skipped with `cmp_skip_object_limit(&cmp, &obj, 2)`. - * However, because we cannot track depth across the elements, there's no way - * to reset it after descending down into each element. - * - * This is similar to `cmp_skip_object`, except it tolerates up to `limit` - * levels of nesting. For example, in order to skip an array that contains a - * map, call `cmp_skip_object_limit(ctx, &obj, 2)`. Or in other words, - * `cmp_skip_object(ctx, &obj)` acts similarly to `cmp_skip_object_limit(ctx, - * &obj, 0)` - * - * Specifically, `limit` refers to depth, not breadth. So in order to skip an - * array that contains two arrays that each contain 3 strings, you would call - * `cmp_skip_object_limit(ctx, &obj, 2). In order to skip an array that - * contains 4 arrays that each contain 1 string, you would still call - * `cmp_skip_object_limit(ctx, &obj, 2). - */ -bool cmp_skip_object_limit(cmp_ctx_t *ctx, cmp_object_t *obj, uint32_t limit) -#ifdef __GNUC__ - __attribute__((deprecated)) -#endif -; - -#ifdef _MSC_VER -#pragma deprecated(cmp_skip_object_limit) -#endif - /* * ============================================================================ * === Specific API @@ -538,8 +485,10 @@ bool cmp_object_as_ushort(const cmp_object_t *obj, uint16_t *s); bool cmp_object_as_uint(const cmp_object_t *obj, uint32_t *i); bool cmp_object_as_ulong(const cmp_object_t *obj, uint64_t *u); bool cmp_object_as_uinteger(const cmp_object_t *obj, uint64_t *u); +#ifndef CMP_NO_FLOAT bool cmp_object_as_float(const cmp_object_t *obj, float *f); bool cmp_object_as_double(const cmp_object_t *obj, double *d); +#endif bool cmp_object_as_bool(const cmp_object_t *obj, bool *b); bool cmp_object_as_str(const cmp_object_t *obj, uint32_t *size); bool cmp_object_as_bin(const cmp_object_t *obj, uint32_t *size); @@ -554,19 +503,6 @@ bool cmp_object_to_bin(cmp_ctx_t *ctx, const cmp_object_t *obj, void *data, uint } /* extern "C" */ #endif -/* - * ============================================================================ - * === Backwards compatibility defines - * ============================================================================ - */ - -#define cmp_write_int cmp_write_integer -#define cmp_write_sint cmp_write_integer -#define cmp_write_sinteger cmp_write_integer -#define cmp_write_uint cmp_write_uinteger -#define cmp_read_sinteger cmp_read_integer - #endif /* CMP_H_INCLUDED */ /* vi: set et ts=2 sw=2: */ - diff --git a/examples/example2.c b/examples/example2.c index 8859326..3ea7513 100644 --- a/examples/example2.c +++ b/examples/example2.c @@ -87,11 +87,11 @@ int main(void) { error_and_exit(cmp_strerror(&cmp)); } - if (!cmp_write_sint(&cmp, -14)) { + if (!cmp_write_integer(&cmp, -14)) { error_and_exit(cmp_strerror(&cmp)); } - if (!cmp_write_uint(&cmp, 38)) { + if (!cmp_write_uinteger(&cmp, 38)) { error_and_exit(cmp_strerror(&cmp)); } @@ -209,7 +209,7 @@ int main(void) { error_and_exit("Array size was not 9"); } - if (!cmp_read_sinteger(&cmp, &sint)) { + if (!cmp_read_integer(&cmp, &sint)) { error_and_exit(cmp_strerror(&cmp)); } diff --git a/test/tests.c b/test/tests.c index 9e1046f..a71f9d7 100644 --- a/test/tests.c +++ b/test/tests.c @@ -438,13 +438,13 @@ void test_fixedint(void **state) { cmp_write_sfix, cmp_read_uinteger, u8, uint64_t, 0, "\x00", 1 ); test_int_format( - cmp_write_sfix, cmp_read_sinteger, s8, int64_t, -0, "\x00", 1 + cmp_write_sfix, cmp_read_integer, s8, int64_t, -0, "\x00", 1 ); test_int_format( cmp_write_sfix, cmp_read_uinteger, u8, uint64_t, 127, "\x7f", 1 ); test_int_format( - cmp_write_sfix, cmp_read_sinteger, s8, int64_t, -32, "\xe0", 1 + cmp_write_sfix, cmp_read_integer, s8, int64_t, -32, "\xe0", 1 ); test_int_format( cmp_write_pfix, cmp_read_uinteger, u8, uint64_t, 0, "\x00", 1 @@ -456,10 +456,10 @@ void test_fixedint(void **state) { cmp_write_pfix, cmp_read_uinteger, u8, uint64_t, 127, "\x7f", 1 ); test_int_format( - cmp_write_nfix, cmp_read_sinteger, s8, int64_t, -1, "\xff", 1 + cmp_write_nfix, cmp_read_integer, s8, int64_t, -1, "\xff", 1 ); test_int_format( - cmp_write_nfix, cmp_read_sinteger, s8, int64_t, -32, "\xe0", 1 + cmp_write_nfix, cmp_read_integer, s8, int64_t, -32, "\xe0", 1 ); teardown_cmp_and_buf(&cmp, &buf); @@ -488,55 +488,55 @@ void test_numbers(void **state) { setup_cmp_and_buf(&cmp, &buf); test_int_format( - cmp_write_s8, cmp_read_sinteger, s8, int64_t, 0, "\xd0\x00", 2 + cmp_write_s8, cmp_read_integer, s8, int64_t, 0, "\xd0\x00", 2 ); test_int_format( - cmp_write_s8, cmp_read_sinteger, s8, int64_t, 1, "\xd0\x01", 2 + cmp_write_s8, cmp_read_integer, s8, int64_t, 1, "\xd0\x01", 2 ); test_int_format( - cmp_write_s8, cmp_read_sinteger, s8, int64_t, -1, "\xd0\xff", 2 + cmp_write_s8, cmp_read_integer, s8, int64_t, -1, "\xd0\xff", 2 ); test_int_format( - cmp_write_s8, cmp_read_sinteger, s8, int64_t, 127, "\xd0\x7f", 2 + cmp_write_s8, cmp_read_integer, s8, int64_t, 127, "\xd0\x7f", 2 ); test_int_format( - cmp_write_s8, cmp_read_sinteger, s8, int64_t, -128, "\xd0\x80", 2 + cmp_write_s8, cmp_read_integer, s8, int64_t, -128, "\xd0\x80", 2 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, 0, "\xd1\x00\x00", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, 0, "\xd1\x00\x00", 3 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, 1, "\xd1\x00\x01", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, 1, "\xd1\x00\x01", 3 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, -1, "\xd1\xff\xff", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, -1, "\xd1\xff\xff", 3 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, 127, "\xd1\x00\x7f", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, 127, "\xd1\x00\x7f", 3 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, -128, "\xd1\xff\x80", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, -128, "\xd1\xff\x80", 3 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, 256, "\xd1\x01\x00", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, 256, "\xd1\x01\x00", 3 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, 32767, "\xd1\x7f\xff", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, 32767, "\xd1\x7f\xff", 3 ); test_int_format( - cmp_write_s16, cmp_read_sinteger, s16, int64_t, -32768, "\xd1\x80\x00", 3 + cmp_write_s16, cmp_read_integer, s16, int64_t, -32768, "\xd1\x80\x00", 3 ); test_int_format( - cmp_write_s32, cmp_read_sinteger, s32, int64_t, 0, "\xd2\x00\x00\x00\x00", 5 + cmp_write_s32, cmp_read_integer, s32, int64_t, 0, "\xd2\x00\x00\x00\x00", 5 ); test_int_format( - cmp_write_s32, cmp_read_sinteger, s32, int64_t, 1, "\xd2\x00\x00\x00\x01", 5 + cmp_write_s32, cmp_read_integer, s32, int64_t, 1, "\xd2\x00\x00\x00\x01", 5 ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, -1, @@ -545,7 +545,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, 127, @@ -554,7 +554,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, -128, @@ -563,7 +563,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, 256, @@ -572,7 +572,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, 32767, @@ -581,7 +581,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, -32768, @@ -590,7 +590,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, 65535, @@ -599,7 +599,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, -65536, @@ -608,7 +608,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, 8388607, @@ -617,7 +617,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, -8388608, @@ -626,7 +626,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, 16777215, @@ -635,7 +635,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, -16777216, @@ -644,7 +644,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, 2147483647, @@ -653,7 +653,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s32, - cmp_read_sinteger, + cmp_read_integer, s32, int64_t, -2147483648, @@ -663,7 +663,7 @@ void test_numbers(void **state) { test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 0, @@ -672,7 +672,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 1, @@ -681,7 +681,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -1, @@ -690,7 +690,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 127, @@ -699,7 +699,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -128, @@ -708,7 +708,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 256, @@ -717,7 +717,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 32767, @@ -726,7 +726,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -32768, @@ -735,7 +735,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 65535, @@ -744,7 +744,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -65536, @@ -753,7 +753,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 8388607, @@ -762,7 +762,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -8388608, @@ -771,7 +771,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 16777215, @@ -780,7 +780,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -16777216, @@ -789,7 +789,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 2147483647, @@ -798,7 +798,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -2147483648, @@ -807,7 +807,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, 4294967295, @@ -816,7 +816,7 @@ void test_numbers(void **state) { ); test_int_format( cmp_write_s64, - cmp_read_sinteger, + cmp_read_integer, s64, int64_t, -4294967296, @@ -825,34 +825,34 @@ void test_numbers(void **state) { ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u8, uint64_t, 0, "\x00", 1 + cmp_write_integer, cmp_read_uinteger, u8, uint64_t, 0, "\x00", 1 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u8, uint64_t, 1, "\x01", 1 + cmp_write_integer, cmp_read_uinteger, u8, uint64_t, 1, "\x01", 1 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u8, uint64_t, 127, "\x7f", 1 + cmp_write_integer, cmp_read_uinteger, u8, uint64_t, 127, "\x7f", 1 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u8, uint64_t, 128, "\xcc\x80", 2 + cmp_write_integer, cmp_read_uinteger, u8, uint64_t, 128, "\xcc\x80", 2 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u8, uint64_t, 255, "\xcc\xff", 2 + cmp_write_integer, cmp_read_uinteger, u8, uint64_t, 255, "\xcc\xff", 2 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u16, uint64_t, 256, "\xcd\x01\x00", 3 + cmp_write_integer, cmp_read_uinteger, u16, uint64_t, 256, "\xcd\x01\x00", 3 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u16, uint64_t, 32767, "\xcd\x7f\xff", 3 + cmp_write_integer, cmp_read_uinteger, u16, uint64_t, 32767, "\xcd\x7f\xff", 3 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u16, uint64_t, 32768, "\xcd\x80\x00", 3 + cmp_write_integer, cmp_read_uinteger, u16, uint64_t, 32768, "\xcd\x80\x00", 3 ); test_int_format( - cmp_write_sint, cmp_read_uinteger, u16, uint64_t, 65535, "\xcd\xff\xff", 3 + cmp_write_integer, cmp_read_uinteger, u16, uint64_t, 65535, "\xcd\xff\xff", 3 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u32, uint64_t, @@ -861,7 +861,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u32, uint64_t, @@ -870,7 +870,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u32, uint64_t, @@ -879,7 +879,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u32, uint64_t, @@ -888,7 +888,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u32, uint64_t, @@ -897,7 +897,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u32, uint64_t, @@ -906,7 +906,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -915,7 +915,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -924,7 +924,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -933,7 +933,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -942,7 +942,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -951,7 +951,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -960,7 +960,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -969,7 +969,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -978,7 +978,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -987,7 +987,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -996,7 +996,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -1005,7 +1005,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -1014,7 +1014,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -1023,7 +1023,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, + cmp_write_integer, cmp_read_uinteger, u64, uint64_t, @@ -1033,32 +1033,32 @@ void test_numbers(void **state) { ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s8, int64_t, -1, "\xff", 1 + cmp_write_integer, cmp_read_integer, s8, int64_t, -1, "\xff", 1 ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s8, int64_t, -32, "\xe0", 1 + cmp_write_integer, cmp_read_integer, s8, int64_t, -32, "\xe0", 1 ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s8, int64_t, -127, "\xd0\x81", 2 + cmp_write_integer, cmp_read_integer, s8, int64_t, -127, "\xd0\x81", 2 ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s8, int64_t, -128, "\xd0\x80", 2 + cmp_write_integer, cmp_read_integer, s8, int64_t, -128, "\xd0\x80", 2 ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s16, int64_t, -255, "\xd1\xff\x01", 3 + cmp_write_integer, cmp_read_integer, s16, int64_t, -255, "\xd1\xff\x01", 3 ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s16, int64_t, -256, "\xd1\xff\x00", 3 + cmp_write_integer, cmp_read_integer, s16, int64_t, -256, "\xd1\xff\x00", 3 ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s16, int64_t, -32767, "\xd1\x80\x01", 3 + cmp_write_integer, cmp_read_integer, s16, int64_t, -32767, "\xd1\x80\x01", 3 ); test_int_format( - cmp_write_sint, cmp_read_sinteger, s16, int64_t, -32768, "\xd1\x80\x00", 3 + cmp_write_integer, cmp_read_integer, s16, int64_t, -32768, "\xd1\x80\x00", 3 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -65535, @@ -1066,8 +1066,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -65536, @@ -1075,8 +1075,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -8388607, @@ -1084,8 +1084,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -8388608, @@ -1093,8 +1093,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -16777215, @@ -1102,8 +1102,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -16777216, @@ -1111,8 +1111,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -2147483647, @@ -1120,8 +1120,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s32, int64_t, -2147483648, @@ -1129,8 +1129,8 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -4294967295, @@ -1138,8 +1138,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -4294967296, @@ -1147,8 +1147,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -549755813887, @@ -1156,8 +1156,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -549755813888, @@ -1165,8 +1165,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -1099511627775, @@ -1174,8 +1174,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -1099511627776, @@ -1183,8 +1183,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -140737488355327, @@ -1192,8 +1192,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -140737488355328, @@ -1201,8 +1201,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -281474976710655, @@ -1210,8 +1210,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -281474976710656, @@ -1219,8 +1219,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -36028797018963967, @@ -1228,8 +1228,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -36028797018963968, @@ -1237,8 +1237,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -72057594037927935, @@ -1246,8 +1246,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -72057594037927936, @@ -1255,8 +1255,8 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_sint, - cmp_read_sinteger, + cmp_write_integer, + cmp_read_integer, s64, int64_t, -9223372036854775807, @@ -1517,34 +1517,34 @@ void test_numbers(void **state) { ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u8, uint64_t, 0, "\x00", 1 + cmp_write_uinteger, cmp_read_uinteger, u8, uint64_t, 0, "\x00", 1 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u8, uint64_t, 1, "\x01", 1 + cmp_write_uinteger, cmp_read_uinteger, u8, uint64_t, 1, "\x01", 1 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u8, uint64_t, 127, "\x7f", 1 + cmp_write_uinteger, cmp_read_uinteger, u8, uint64_t, 127, "\x7f", 1 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u8, uint64_t, 128, "\xcc\x80", 2 + cmp_write_uinteger, cmp_read_uinteger, u8, uint64_t, 128, "\xcc\x80", 2 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u8, uint64_t, 255, "\xcc\xff", 2 + cmp_write_uinteger, cmp_read_uinteger, u8, uint64_t, 255, "\xcc\xff", 2 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u16, uint64_t, 256, "\xcd\x01\x00", 3 + cmp_write_uinteger, cmp_read_uinteger, u16, uint64_t, 256, "\xcd\x01\x00", 3 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u16, uint64_t, 32767, "\xcd\x7f\xff", 3 + cmp_write_uinteger, cmp_read_uinteger, u16, uint64_t, 32767, "\xcd\x7f\xff", 3 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u16, uint64_t, 32768, "\xcd\x80\x00", 3 + cmp_write_uinteger, cmp_read_uinteger, u16, uint64_t, 32768, "\xcd\x80\x00", 3 ); test_int_format( - cmp_write_uint, cmp_read_uinteger, u16, uint64_t, 65535, "\xcd\xff\xff", 3 + cmp_write_uinteger, cmp_read_uinteger, u16, uint64_t, 65535, "\xcd\xff\xff", 3 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1553,7 +1553,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1562,7 +1562,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1571,7 +1571,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1580,7 +1580,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1589,7 +1589,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1598,7 +1598,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1607,7 +1607,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u32, uint64_t, @@ -1616,7 +1616,7 @@ void test_numbers(void **state) { 5 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1625,7 +1625,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1634,7 +1634,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1643,7 +1643,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1652,7 +1652,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1661,7 +1661,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1670,7 +1670,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1679,7 +1679,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1688,7 +1688,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1697,7 +1697,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1706,7 +1706,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1715,7 +1715,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1724,7 +1724,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1733,7 +1733,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -1742,7 +1742,7 @@ void test_numbers(void **state) { 9 ); test_int_format( - cmp_write_uint, + cmp_write_uinteger, cmp_read_uinteger, u64, uint64_t, @@ -3448,7 +3448,7 @@ void test_obj(void **state) { setup_cmp_and_buf(&cmp, &buf); - obj_write(cmp_write_sint, -1); + obj_write(cmp_write_integer, -1); obj_test(cmp_object_is_char, cmp_object_as_char, "char", int8_t, -1); obj_test(cmp_object_is_short, cmp_object_as_short, "short", int16_t, -1); obj_test(cmp_object_is_int, cmp_object_as_int, "int", int32_t, -1); @@ -3473,7 +3473,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_sint, -129); + obj_write(cmp_write_integer, -129); obj_test(cmp_object_is_short, cmp_object_as_short, "short", int16_t, -129); obj_test(cmp_object_is_int, cmp_object_as_int, "int", int32_t, -129); obj_test(cmp_object_is_long, cmp_object_as_long, "long", int64_t, -129); @@ -3498,7 +3498,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_sint, -32769); + obj_write(cmp_write_integer, -32769); obj_test(cmp_object_is_int, cmp_object_as_int, "int", int32_t, -32769); obj_test(cmp_object_is_long, cmp_object_as_long, "long", int64_t, -32769); obj_test( @@ -3523,7 +3523,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_sint, -2147483649); + obj_write(cmp_write_integer, -2147483649); obj_test( cmp_object_is_long, cmp_object_as_long, "long", int64_t, -2147483649 ); @@ -3553,7 +3553,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_uint, 1); + obj_write(cmp_write_uinteger, 1); obj_test(cmp_object_is_uchar, cmp_object_as_uchar, "uchar", uint8_t, 1); obj_test(cmp_object_is_ushort, cmp_object_as_ushort, "ushort", uint16_t, 1); obj_test(cmp_object_is_uint, cmp_object_as_uint, "uint", uint32_t, 1); @@ -3578,7 +3578,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_uint, 255); + obj_write(cmp_write_uinteger, 255); obj_test(cmp_object_is_uchar, cmp_object_as_uchar, "uchar", uint8_t, 255); obj_test( cmp_object_is_ushort, cmp_object_as_ushort, "ushort", uint16_t, 255 @@ -3605,7 +3605,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_uint, 256); + obj_write(cmp_write_uinteger, 256); obj_test( cmp_object_is_ushort, cmp_object_as_ushort, "ushort", uint16_t, 256 ); @@ -3632,7 +3632,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_uint, 65535); + obj_write(cmp_write_uinteger, 65535); obj_test( cmp_object_is_ushort, cmp_object_as_ushort, "ushort", uint16_t, 65535 ); @@ -3662,7 +3662,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_uint, 65536); + obj_write(cmp_write_uinteger, 65536); obj_test(cmp_object_is_uint, cmp_object_as_uint, "uint", uint32_t, 65536); obj_test(cmp_object_is_ulong, cmp_object_as_ulong, "ulong", uint64_t, 65536); obj_test( @@ -3691,7 +3691,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_uint, 4294967295); + obj_write(cmp_write_uinteger, 4294967295); obj_test( cmp_object_is_uint, cmp_object_as_uint, "uint", uint32_t, 4294967295 ); @@ -3724,7 +3724,7 @@ void test_obj(void **state) { obj_test_not(cmp_object_is_map, "map"); obj_test_not(cmp_object_is_ext, "ext"); - obj_write(cmp_write_uint, 4294967296); + obj_write(cmp_write_uinteger, 4294967296); obj_test( cmp_object_is_ulong, cmp_object_as_ulong, "ulong", uint64_t, 4294967296 ); @@ -4010,8 +4010,8 @@ void test_obj(void **state) { M_BufferSeek(&buf, 0); cmp_write_array(&cmp, 2); - cmp_write_uint(&cmp, 1); - cmp_write_uint(&cmp, 2); + cmp_write_uinteger(&cmp, 1); + cmp_write_uinteger(&cmp, 2); M_BufferSeek(&buf, 0); cmp_read_object(&cmp, &obj); obj_array_test(1, 2); @@ -4038,8 +4038,8 @@ void test_obj(void **state) { M_BufferSeek(&buf, 0); cmp_write_map(&cmp, 1); - cmp_write_uint(&cmp, 1); - cmp_write_uint(&cmp, 2); + cmp_write_uinteger(&cmp, 1); + cmp_write_uinteger(&cmp, 2); M_BufferSeek(&buf, 0); cmp_read_object(&cmp, &obj); obj_map_test(1, 2); @@ -4305,7 +4305,7 @@ void test_skipping(void **state) { buf_t buf; cmp_ctx_t cmp; cmp_object_t obj; - cmp_skipper skip; + cmp_skipper *skip; (void)state; @@ -4700,7 +4700,7 @@ void test_deprecated_limited_skipping(void **state) { buf_t buf; cmp_ctx_t cmp; cmp_object_t obj; - cmp_skipper skip; + cmp_skipper *skip; (void)state; @@ -4779,22 +4779,16 @@ void test_deprecated_limited_skipping(void **state) { assert_true(cmp_skip_object_no_limit(&cmp)); assert_true(cmp_skip_object_no_limit(&cmp)); assert_true(cmp_skip_object_no_limit(&cmp)); - assert_true(cmp_skip_object_limit(&cmp, &obj, 11)); - assert_false(cmp_skip_object_limit(&cmp, &obj, 1)); M_BufferSeek(&buf, 0); assert_true(cmp_skip_object_no_limit(&cmp)); assert_true(cmp_skip_object_no_limit(&cmp)); assert_true(cmp_skip_object_no_limit(&cmp)); - assert_true(cmp_skip_object_limit(&cmp, &obj, 11)); - assert_false(cmp_skip_object_limit(&cmp, &obj, 2)); M_BufferSeek(&buf, 0); assert_true(cmp_skip_object_no_limit(&cmp)); assert_true(cmp_skip_object_no_limit(&cmp)); assert_true(cmp_skip_object_no_limit(&cmp)); - assert_true(cmp_skip_object_limit(&cmp, &obj, 11)); - assert_true(cmp_skip_object_limit(&cmp, &obj, 3)); M_BufferSeek(&buf, 0); assert_true(cmp_read_object(&cmp, &obj));