make style

This commit is contained in:
Philippe Teuwen 2019-03-10 00:00:59 +01:00
commit 0373696662
483 changed files with 56514 additions and 52451 deletions

View file

@ -195,15 +195,14 @@ typedef enum CborError {
CborErrorJsonObjectKeyNotString,
CborErrorJsonNotImplemented,
CborErrorOutOfMemory = (int) (~0U / 2 + 1),
CborErrorInternalError = (int) (~0U / 2) /* INT_MAX on two's complement machines */
CborErrorOutOfMemory = (int)(~0U / 2 + 1),
CborErrorInternalError = (int)(~0U / 2) /* INT_MAX on two's complement machines */
} CborError;
CBOR_API const char *cbor_error_string(CborError error);
/* Encoder API */
struct CborEncoder
{
struct CborEncoder {
union {
uint8_t *ptr;
ptrdiff_t bytes_needed;
@ -264,8 +263,7 @@ CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *en
/* Parser API */
enum CborParserIteratorFlags
{
enum CborParserIteratorFlags {
CborIteratorFlag_IntegerValueTooLarge = 0x01,
CborIteratorFlag_NegativeInteger = 0x02,
CborIteratorFlag_IteratingStringChunks = 0x02,
@ -273,15 +271,13 @@ enum CborParserIteratorFlags
CborIteratorFlag_ContainerIsMap = 0x20
};
struct CborParser
{
struct CborParser {
const uint8_t *end;
uint32_t flags;
};
typedef struct CborParser CborParser;
struct CborValue
{
struct CborValue {
const CborParser *parser;
const uint8_t *ptr;
uint32_t remaining;
@ -310,7 +306,7 @@ CBOR_PRIVATE_API uint64_t _cbor_value_decode_int64_internal(const CborValue *val
CBOR_INLINE_API uint64_t _cbor_value_extract_int64_helper(const CborValue *value)
{
return value->flags & CborIteratorFlag_IntegerValueTooLarge ?
_cbor_value_decode_int64_internal(value) : value->extra;
_cbor_value_decode_int64_internal(value) : value->extra;
}
CBOR_INLINE_API bool cbor_value_is_valid(const CborValue *value)
@ -579,9 +575,9 @@ enum CborPrettyFlags {
CborPrettyDefaultFlags = CborPrettyIndicateIndeterminateLength
};
typedef CborError (*CborStreamFunction)(void *token, const char *fmt, ...)
typedef CborError(*CborStreamFunction)(void *token, const char *fmt, ...)
#ifdef __GNUC__
__attribute__((__format__(printf, 2, 3)))
__attribute__((__format__(printf, 2, 3)))
#endif
;

View file

@ -393,11 +393,11 @@ CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, cons
size = 2U << (fpType - CborHalfFloatType);
if (size == 8)
put64(buf + 1, *(const uint64_t*)value);
put64(buf + 1, *(const uint64_t *)value);
else if (size == 4)
put32(buf + 1, *(const uint32_t*)value);
put32(buf + 1, *(const uint32_t *)value);
else
put16(buf + 1, *(const uint16_t*)value);
put16(buf + 1, *(const uint16_t *)value);
saturated_decrement(encoder);
return append_to_buffer(encoder, buf, size + 1);
}

View file

@ -80,103 +80,103 @@
const char *cbor_error_string(CborError error)
{
switch (error) {
case CborNoError:
return "";
case CborNoError:
return "";
case CborUnknownError:
return _("unknown error");
case CborUnknownError:
return _("unknown error");
case CborErrorOutOfMemory:
return _("out of memory/need more memory");
case CborErrorOutOfMemory:
return _("out of memory/need more memory");
case CborErrorUnknownLength:
return _("unknown length (attempted to get the length of a map/array/string of indeterminate length");
case CborErrorUnknownLength:
return _("unknown length (attempted to get the length of a map/array/string of indeterminate length");
case CborErrorAdvancePastEOF:
return _("attempted to advance past EOF");
case CborErrorAdvancePastEOF:
return _("attempted to advance past EOF");
case CborErrorIO:
return _("I/O error");
case CborErrorIO:
return _("I/O error");
case CborErrorGarbageAtEnd:
return _("garbage after the end of the content");
case CborErrorGarbageAtEnd:
return _("garbage after the end of the content");
case CborErrorUnexpectedEOF:
return _("unexpected end of data");
case CborErrorUnexpectedEOF:
return _("unexpected end of data");
case CborErrorUnexpectedBreak:
return _("unexpected 'break' byte");
case CborErrorUnexpectedBreak:
return _("unexpected 'break' byte");
case CborErrorUnknownType:
return _("illegal byte (encodes future extension type)");
case CborErrorUnknownType:
return _("illegal byte (encodes future extension type)");
case CborErrorIllegalType:
return _("mismatched string type in chunked string");
case CborErrorIllegalType:
return _("mismatched string type in chunked string");
case CborErrorIllegalNumber:
return _("illegal initial byte (encodes unspecified additional information)");
case CborErrorIllegalNumber:
return _("illegal initial byte (encodes unspecified additional information)");
case CborErrorIllegalSimpleType:
return _("illegal encoding of simple type smaller than 32");
case CborErrorIllegalSimpleType:
return _("illegal encoding of simple type smaller than 32");
case CborErrorUnknownSimpleType:
return _("unknown simple type");
case CborErrorUnknownSimpleType:
return _("unknown simple type");
case CborErrorUnknownTag:
return _("unknown tag");
case CborErrorUnknownTag:
return _("unknown tag");
case CborErrorInappropriateTagForType:
return _("inappropriate tag for type");
case CborErrorInappropriateTagForType:
return _("inappropriate tag for type");
case CborErrorDuplicateObjectKeys:
return _("duplicate keys in object");
case CborErrorDuplicateObjectKeys:
return _("duplicate keys in object");
case CborErrorInvalidUtf8TextString:
return _("invalid UTF-8 content in string");
case CborErrorInvalidUtf8TextString:
return _("invalid UTF-8 content in string");
case CborErrorExcludedType:
return _("excluded type found");
case CborErrorExcludedType:
return _("excluded type found");
case CborErrorExcludedValue:
return _("excluded value found");
case CborErrorExcludedValue:
return _("excluded value found");
case CborErrorImproperValue:
case CborErrorOverlongEncoding:
return _("value encoded in non-canonical form");
case CborErrorImproperValue:
case CborErrorOverlongEncoding:
return _("value encoded in non-canonical form");
case CborErrorMapKeyNotString:
case CborErrorJsonObjectKeyNotString:
return _("key in map is not a string");
case CborErrorMapKeyNotString:
case CborErrorJsonObjectKeyNotString:
return _("key in map is not a string");
case CborErrorMapNotSorted:
return _("map is not sorted");
case CborErrorMapNotSorted:
return _("map is not sorted");
case CborErrorMapKeysNotUnique:
return _("map keys are not unique");
case CborErrorMapKeysNotUnique:
return _("map keys are not unique");
case CborErrorTooManyItems:
return _("too many items added to encoder");
case CborErrorTooManyItems:
return _("too many items added to encoder");
case CborErrorTooFewItems:
return _("too few items added to encoder");
case CborErrorTooFewItems:
return _("too few items added to encoder");
case CborErrorDataTooLarge:
return _("internal error: data too large");
case CborErrorDataTooLarge:
return _("internal error: data too large");
case CborErrorNestingTooDeep:
return _("internal error: too many nested containers found in recursive function");
case CborErrorNestingTooDeep:
return _("internal error: too many nested containers found in recursive function");
case CborErrorUnsupportedType:
return _("unsupported type");
case CborErrorUnsupportedType:
return _("unsupported type");
case CborErrorJsonObjectKeyIsAggregate:
return _("conversion to JSON failed: key in object is an array or map");
case CborErrorJsonObjectKeyIsAggregate:
return _("conversion to JSON failed: key in object is an array or map");
case CborErrorJsonNotImplemented:
return _("conversion to JSON failed: open_memstream unavailable");
case CborErrorJsonNotImplemented:
return _("conversion to JSON failed: open_memstream unavailable");
case CborErrorInternalError:
return _("internal error");
case CborErrorInternalError:
return _("internal error");
}
return cbor_error_string(CborUnknownError);
}

View file

@ -56,7 +56,7 @@ static inline unsigned short encode_half(double val)
memcpy(&v, &val, sizeof(v));
sign = v >> 63 << 15;
exp = (v >> 52) & 0x7ff;
mant = v << 12 >> 12 >> (53-11); /* keep only the 11 most significant bits of the mantissa */
mant = v << 12 >> 12 >> (53 - 11); /* keep only the 11 most significant bits of the mantissa */
exp -= 1023;
if (exp == 1024) {
/* infinity or NaN */
@ -147,7 +147,7 @@ enum {
IndefiniteLength = 31U,
MajorTypeShift = SmallValueBitLength,
MajorTypeMask = (int) (~0U << MajorTypeShift),
MajorTypeMask = (int)(~0U << MajorTypeShift),
BreakByte = (unsigned)Break | (SimpleTypesType << MajorTypeShift)
};
@ -155,7 +155,7 @@ enum {
CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len);
CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC _cbor_value_prepare_string_iteration(CborValue *it);
CBOR_INTERNAL_API CborError CBOR_INTERNAL_API_CC _cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
size_t *len, CborValue *next);
size_t *len, CborValue *next);
#endif /* CBORINTERNAL_P_H */

View file

@ -32,8 +32,7 @@ extern "C" {
#endif
/* Conversion to JSON */
enum CborToJsonFlags
{
enum CborToJsonFlags {
CborConvertAddMetadata = 1,
CborConvertTagsToObjects = 2,
CborConvertIgnoreTags = 0,

View file

@ -249,38 +249,38 @@ static CborError preparse_value(CborValue *it)
it->type = CborIntegerType;
} else if (majortype == SimpleTypesType) {
switch (descriptor) {
case FalseValue:
it->extra = false;
it->type = CborBooleanType;
break;
case FalseValue:
it->extra = false;
it->type = CborBooleanType;
break;
case SinglePrecisionFloat:
case DoublePrecisionFloat:
it->flags |= CborIteratorFlag_IntegerValueTooLarge;
case SinglePrecisionFloat:
case DoublePrecisionFloat:
it->flags |= CborIteratorFlag_IntegerValueTooLarge;
/* fall through */
case TrueValue:
case NullValue:
case UndefinedValue:
case HalfPrecisionFloat:
it->type = *it->ptr;
break;
case TrueValue:
case NullValue:
case UndefinedValue:
case HalfPrecisionFloat:
it->type = *it->ptr;
break;
case SimpleTypeInNextByte:
it->extra = (uint8_t)it->ptr[1];
case SimpleTypeInNextByte:
it->extra = (uint8_t)it->ptr[1];
#ifndef CBOR_PARSER_NO_STRICT_CHECKS
if (unlikely(it->extra < 32)) {
it->type = CborInvalidType;
return CborErrorIllegalSimpleType;
}
if (unlikely(it->extra < 32)) {
it->type = CborInvalidType;
return CborErrorIllegalSimpleType;
}
#endif
break;
break;
case 28:
case 29:
case 30:
case Break:
cbor_assert(false); /* these conditions can't be reached */
return CborErrorUnexpectedBreak;
case 28:
case 29:
case 30:
case Break:
cbor_assert(false); /* these conditions can't be reached */
return CborErrorUnexpectedBreak;
}
return CborNoError;
}
@ -872,7 +872,7 @@ CborError cbor_value_get_int_checked(const CborValue *value, int *result)
*/
if (value->flags & CborIteratorFlag_NegativeInteger) {
if (unlikely(v > (unsigned) -(INT_MIN + 1)))
if (unlikely(v > (unsigned) - (INT_MIN + 1)))
return CborErrorDataTooLarge;
*result = (int)v;
@ -1189,13 +1189,13 @@ static CborError iterate_string_chunks(const CborValue *value, char *buffer, siz
*/
CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
size_t *buflen, CborValue *next)
size_t *buflen, CborValue *next)
{
bool copied_all;
CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
CborError err = iterate_string_chunks(value, (char *)buffer, buflen, &copied_all, next,
buffer ? iterate_memcpy : iterate_noop);
return err ? err :
copied_all ? CborNoError : CborErrorOutOfMemory;
copied_all ? CborNoError : CborErrorOutOfMemory;
}
/**

View file

@ -197,7 +197,7 @@ static CborError hexDump(CborStreamFunction stream, void *out, const void *ptr,
static CborError utf8EscapedDump(CborStreamFunction stream, void *out, const void *ptr, size_t n)
{
const uint8_t *buffer = (const uint8_t *)ptr;
const uint8_t * const end = buffer + n;
const uint8_t *const end = buffer + n;
CborError err = CborNoError;
while (buffer < end && !err) {
@ -215,26 +215,26 @@ static CborError utf8EscapedDump(CborStreamFunction stream, void *out, const voi
/* print as an escape sequence */
switch (uc) {
case '"':
case '\\':
break;
case '\b':
escaped = 'b';
break;
case '\f':
escaped = 'f';
break;
case '\n':
escaped = 'n';
break;
case '\r':
escaped = 'r';
break;
case '\t':
escaped = 't';
break;
default:
goto print_utf16;
case '"':
case '\\':
break;
case '\b':
escaped = 'b';
break;
case '\f':
escaped = 'f';
break;
case '\n':
escaped = 'n';
break;
case '\r':
escaped = 'r';
break;
case '\t':
escaped = 't';
break;
default:
goto print_utf16;
}
err = stream(out, "\\%c", escaped);
continue;
@ -277,7 +277,7 @@ static const char *resolve_indicator(const uint8_t *ptr, const uint8_t *end, int
/* determine whether to show anything */
if ((flags & CborPrettyIndicateIndeterminateLength) &&
additional_information == IndefiniteLength)
additional_information == IndefiniteLength)
return indicators[IndefiniteLength - Value8Bit];
if ((flags & CborPrettyIndicateOverlongNumbers) == 0)
return no_indicator;
@ -296,8 +296,8 @@ static const char *resolve_indicator(const uint8_t *ptr, const uint8_t *end, int
if (value > 0xffffffffU)
++expected_information;
return expected_information == additional_information ?
no_indicator :
indicators[additional_information - Value8Bit];
no_indicator :
indicators[additional_information - Value8Bit];
}
static const char *get_indicator(const CborValue *it, int flags)
@ -341,208 +341,208 @@ static CborError value_to_pretty(CborStreamFunction stream, void *out, CborValue
CborError err = CborNoError;
CborType type = cbor_value_get_type(it);
switch (type) {
case CborArrayType:
case CborMapType: {
/* recursive type */
CborValue recursed;
const char *indicator = get_indicator(it, flags);
const char *space = *indicator ? " " : indicator;
case CborArrayType:
case CborMapType: {
/* recursive type */
CborValue recursed;
const char *indicator = get_indicator(it, flags);
const char *space = *indicator ? " " : indicator;
err = stream(out, "%c%s%s", type == CborArrayType ? '[' : '{', indicator, space);
if (err)
return err;
err = stream(out, "%c%s%s", type == CborArrayType ? '[' : '{', indicator, space);
if (err)
return err;
err = cbor_value_enter_container(it, &recursed);
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
err = cbor_value_enter_container(it, &recursed);
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
}
err = container_to_pretty(stream, out, &recursed, type, flags, recursionsLeft - 1);
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
}
err = cbor_value_leave_container(it, &recursed);
if (err)
return err; /* parse error */
return stream(out, type == CborArrayType ? "]" : "}");
}
err = container_to_pretty(stream, out, &recursed, type, flags, recursionsLeft - 1);
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
}
err = cbor_value_leave_container(it, &recursed);
if (err)
return err; /* parse error */
return stream(out, type == CborArrayType ? "]" : "}");
}
case CborIntegerType: {
uint64_t val;
cbor_value_get_raw_integer(it, &val); /* can't fail */
case CborIntegerType: {
uint64_t val;
cbor_value_get_raw_integer(it, &val); /* can't fail */
if (cbor_value_is_unsigned_integer(it)) {
err = stream(out, "%" PRIu64, val);
} else {
/* CBOR stores the negative number X as -1 - X
* (that is, -1 is stored as 0, -2 as 1 and so forth) */
if (++val) { /* unsigned overflow may happen */
err = stream(out, "-%" PRIu64, val);
if (cbor_value_is_unsigned_integer(it)) {
err = stream(out, "%" PRIu64, val);
} else {
/* overflown
* 0xffff`ffff`ffff`ffff + 1 =
* 0x1`0000`0000`0000`0000 = 18446744073709551616 (2^64) */
err = stream(out, "-18446744073709551616");
/* CBOR stores the negative number X as -1 - X
* (that is, -1 is stored as 0, -2 as 1 and so forth) */
if (++val) { /* unsigned overflow may happen */
err = stream(out, "-%" PRIu64, val);
} else {
/* overflown
* 0xffff`ffff`ffff`ffff + 1 =
* 0x1`0000`0000`0000`0000 = 18446744073709551616 (2^64) */
err = stream(out, "-18446744073709551616");
}
}
}
if (!err)
err = stream(out, "%s", get_indicator(it, flags));
break;
}
case CborByteStringType:
case CborTextStringType: {
size_t n = 0;
const void *ptr;
bool showingFragments = (flags & CborPrettyShowStringFragments) && !cbor_value_is_length_known(it);
const char *separator = "";
char close = '\'';
char open[3] = "h'";
const char *indicator = NULL;
if (type == CborTextStringType) {
close = open[0] = '"';
open[1] = '\0';
}
if (showingFragments) {
err = stream(out, "(_ ");
if (!err)
err = _cbor_value_prepare_string_iteration(it);
} else {
err = stream(out, "%s", open);
err = stream(out, "%s", get_indicator(it, flags));
break;
}
while (!err) {
if (showingFragments || indicator == NULL) {
/* any iteration, except the second for a non-chunked string */
indicator = resolve_indicator(it->ptr, it->parser->end, flags);
case CborByteStringType:
case CborTextStringType: {
size_t n = 0;
const void *ptr;
bool showingFragments = (flags & CborPrettyShowStringFragments) && !cbor_value_is_length_known(it);
const char *separator = "";
char close = '\'';
char open[3] = "h'";
const char *indicator = NULL;
if (type == CborTextStringType) {
close = open[0] = '"';
open[1] = '\0';
}
err = _cbor_value_get_string_chunk(it, &ptr, &n, it);
if (!ptr)
break;
if (showingFragments) {
err = stream(out, "(_ ");
if (!err)
err = _cbor_value_prepare_string_iteration(it);
} else {
err = stream(out, "%s", open);
}
if (!err && showingFragments)
err = stream(out, "%s%s", separator, open);
if (!err)
err = (type == CborByteStringType ?
while (!err) {
if (showingFragments || indicator == NULL) {
/* any iteration, except the second for a non-chunked string */
indicator = resolve_indicator(it->ptr, it->parser->end, flags);
}
err = _cbor_value_get_string_chunk(it, &ptr, &n, it);
if (!ptr)
break;
if (!err && showingFragments)
err = stream(out, "%s%s", separator, open);
if (!err)
err = (type == CborByteStringType ?
hexDump(stream, out, ptr, n) :
utf8EscapedDump(stream, out, ptr, n));
if (!err && showingFragments) {
err = stream(out, "%c%s", close, indicator);
separator = ", ";
if (!err && showingFragments) {
err = stream(out, "%c%s", close, indicator);
separator = ", ";
}
}
if (!err) {
if (showingFragments)
err = stream(out, ")");
else
err = stream(out, "%c%s", close, indicator);
}
return err;
}
if (!err) {
if (showingFragments)
case CborTagType: {
CborTag tag;
cbor_value_get_tag(it, &tag); /* can't fail */
err = stream(out, "%" PRIu64 "%s(", tag, get_indicator(it, flags));
if (!err)
err = cbor_value_advance_fixed(it);
if (!err && recursionsLeft)
err = value_to_pretty(stream, out, it, flags, recursionsLeft - 1);
else if (!err)
printRecursionLimit(stream, out);
if (!err)
err = stream(out, ")");
else
err = stream(out, "%c%s", close, indicator);
return err;
}
return err;
}
case CborTagType: {
CborTag tag;
cbor_value_get_tag(it, &tag); /* can't fail */
err = stream(out, "%" PRIu64 "%s(", tag, get_indicator(it, flags));
if (!err)
err = cbor_value_advance_fixed(it);
if (!err && recursionsLeft)
err = value_to_pretty(stream, out, it, flags, recursionsLeft - 1);
else if (!err)
printRecursionLimit(stream, out);
if (!err)
err = stream(out, ")");
return err;
}
case CborSimpleType: {
/* simple types can't fail and can't have overlong encoding */
uint8_t simple_type;
cbor_value_get_simple_type(it, &simple_type);
err = stream(out, "simple(%" PRIu8 ")", simple_type);
break;
}
case CborSimpleType: {
/* simple types can't fail and can't have overlong encoding */
uint8_t simple_type;
cbor_value_get_simple_type(it, &simple_type);
err = stream(out, "simple(%" PRIu8 ")", simple_type);
break;
}
case CborNullType:
err = stream(out, "null");
break;
case CborNullType:
err = stream(out, "null");
break;
case CborUndefinedType:
err = stream(out, "undefined");
break;
case CborUndefinedType:
err = stream(out, "undefined");
break;
case CborBooleanType: {
bool val;
cbor_value_get_boolean(it, &val); /* can't fail */
err = stream(out, val ? "true" : "false");
break;
}
case CborBooleanType: {
bool val;
cbor_value_get_boolean(it, &val); /* can't fail */
err = stream(out, val ? "true" : "false");
break;
}
#ifndef CBOR_NO_FLOATING_POINT
case CborDoubleType: {
const char *suffix;
double val;
int r;
uint64_t ival;
case CborDoubleType: {
const char *suffix;
double val;
int r;
uint64_t ival;
if (false) {
float f;
case CborFloatType:
cbor_value_get_float(it, &f);
val = f;
suffix = flags & CborPrettyNumericEncodingIndicators ? "_2" : "f";
} else if (false) {
uint16_t f16;
case CborHalfFloatType:
if (false) {
float f;
case CborFloatType:
cbor_value_get_float(it, &f);
val = f;
suffix = flags & CborPrettyNumericEncodingIndicators ? "_2" : "f";
} else if (false) {
uint16_t f16;
case CborHalfFloatType:
#ifndef CBOR_NO_HALF_FLOAT_TYPE
cbor_value_get_half_float(it, &f16);
val = decode_half(f16);
suffix = flags & CborPrettyNumericEncodingIndicators ? "_1" : "f16";
cbor_value_get_half_float(it, &f16);
val = decode_half(f16);
suffix = flags & CborPrettyNumericEncodingIndicators ? "_1" : "f16";
#else
(void)f16;
(void)f16;
err = CborErrorUnsupportedType;
break;
#endif
} else {
cbor_value_get_double(it, &val);
suffix = "";
}
if ((flags & CborPrettyNumericEncodingIndicators) == 0) {
r = fpclassify(val);
if (r == FP_NAN || r == FP_INFINITE)
suffix = "";
}
if (convertToUint64(val, &ival)) {
/* this double value fits in a 64-bit integer, so show it as such
* (followed by a floating point suffix, to disambiguate) */
err = stream(out, "%s%" PRIu64 ".%s", val < 0 ? "-" : "", ival, suffix);
} else {
/* this number is definitely not a 64-bit integer */
err = stream(out, "%." DBL_DECIMAL_DIG_STR "g%s", val, suffix);
}
break;
}
#else
case CborDoubleType:
case CborFloatType:
case CborHalfFloatType:
err = CborErrorUnsupportedType;
break;
#endif
} else {
cbor_value_get_double(it, &val);
suffix = "";
}
if ((flags & CborPrettyNumericEncodingIndicators) == 0) {
r = fpclassify(val);
if (r == FP_NAN || r == FP_INFINITE)
suffix = "";
}
if (convertToUint64(val, &ival)) {
/* this double value fits in a 64-bit integer, so show it as such
* (followed by a floating point suffix, to disambiguate) */
err = stream(out, "%s%" PRIu64 ".%s", val < 0 ? "-" : "", ival, suffix);
} else {
/* this number is definitely not a 64-bit integer */
err = stream(out, "%." DBL_DECIMAL_DIG_STR "g%s", val, suffix);
}
break;
}
#else
case CborDoubleType:
case CborFloatType:
case CborHalfFloatType:
err = CborErrorUnsupportedType;
break;
#endif /* !CBOR_NO_FLOATING_POINT */
case CborInvalidType:
err = stream(out, "invalid");
if (err)
return err;
return CborErrorUnknownType;
case CborInvalidType:
err = stream(out, "invalid");
if (err)
return err;
return CborErrorUnknownType;
}
if (!err)

View file

@ -188,8 +188,8 @@ static CborError dump_bytestring_base16(char **result, CborValue *it)
for (i = 0; i < n; ++i) {
uint8_t byte = buffer[n + i];
buffer[2*i] = characters[byte >> 4];
buffer[2*i + 1] = characters[byte & 0xf];
buffer[2 * i] = characters[byte >> 4];
buffer[2 * i + 1] = characters[byte & 0xf];
}
return CborNoError;
}
@ -352,8 +352,8 @@ static CborError tagged_value_to_json(FILE *out, CborValue *it, int flags, Conve
return err;
if (flags & CborConvertAddMetadata && status->flags) {
if (fprintf(out, ",\"tag%" PRIu64 "$cbor\":{", tag) < 0 ||
add_value_metadata(out, type, status) != CborNoError ||
fputc('}', out) < 0)
add_value_metadata(out, type, status) != CborNoError ||
fputc('}', out) < 0)
return CborErrorIO;
}
if (fputc('}', out) < 0)
@ -370,7 +370,7 @@ static CborError tagged_value_to_json(FILE *out, CborValue *it, int flags, Conve
/* special handling of byte strings? */
if (type == CborByteStringType && (flags & CborConvertByteStringsToBase64Url) == 0 &&
(tag == CborNegativeBignumTag || tag == CborExpectedBase16Tag || tag == CborExpectedBase64Tag)) {
(tag == CborNegativeBignumTag || tag == CborExpectedBase16Tag || tag == CborExpectedBase64Tag)) {
char *str;
char *pre = "";
@ -473,8 +473,8 @@ static CborError map_to_json(FILE *out, CborValue *it, int flags, ConversionStat
}
if (!err && status->flags) {
if (fprintf(out, ",\"%s$cbor\":{", key) < 0 ||
add_value_metadata(out, valueType, status) != CborNoError ||
fputc('}', out) < 0)
add_value_metadata(out, valueType, status) != CborNoError ||
fputc('}', out) < 0)
err = CborErrorIO;
}
}
@ -492,164 +492,164 @@ static CborError value_to_json(FILE *out, CborValue *it, int flags, CborType typ
status->flags = 0;
switch (type) {
case CborArrayType:
case CborMapType: {
/* recursive type */
CborValue recursed;
err = cbor_value_enter_container(it, &recursed);
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
}
if (fputc(type == CborArrayType ? '[' : '{', out) < 0)
return CborErrorIO;
case CborArrayType:
case CborMapType: {
/* recursive type */
CborValue recursed;
err = cbor_value_enter_container(it, &recursed);
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
}
if (fputc(type == CborArrayType ? '[' : '{', out) < 0)
return CborErrorIO;
err = (type == CborArrayType) ?
err = (type == CborArrayType) ?
array_to_json(out, &recursed, flags, status) :
map_to_json(out, &recursed, flags, status);
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
}
if (fputc(type == CborArrayType ? ']' : '}', out) < 0)
return CborErrorIO;
err = cbor_value_leave_container(it, &recursed);
if (err)
return err; /* parse error */
status->flags = 0; /* reset, there are never conversion errors for us */
return CborNoError;
}
case CborIntegerType: {
double num; /* JS numbers are IEEE double precision */
uint64_t val;
cbor_value_get_raw_integer(it, &val); /* can't fail */
num = (double)val;
if (cbor_value_is_negative_integer(it)) {
num = -num - 1; /* convert to negative */
if ((uint64_t)(-num - 1) != val) {
status->flags = NumberPrecisionWasLost | NumberWasNegative;
status->originalNumber = val;
if (err) {
it->ptr = recursed.ptr;
return err; /* parse error */
}
} else {
if ((uint64_t)num != val) {
status->flags = NumberPrecisionWasLost;
status->originalNumber = val;
if (fputc(type == CborArrayType ? ']' : '}', out) < 0)
return CborErrorIO;
err = cbor_value_leave_container(it, &recursed);
if (err)
return err; /* parse error */
status->flags = 0; /* reset, there are never conversion errors for us */
return CborNoError;
}
case CborIntegerType: {
double num; /* JS numbers are IEEE double precision */
uint64_t val;
cbor_value_get_raw_integer(it, &val); /* can't fail */
num = (double)val;
if (cbor_value_is_negative_integer(it)) {
num = -num - 1; /* convert to negative */
if ((uint64_t)(-num - 1) != val) {
status->flags = NumberPrecisionWasLost | NumberWasNegative;
status->originalNumber = val;
}
} else {
if ((uint64_t)num != val) {
status->flags = NumberPrecisionWasLost;
status->originalNumber = val;
}
}
}
if (fprintf(out, "%.0f", num) < 0) /* this number has no fraction, so no decimal points please */
return CborErrorIO;
break;
}
case CborByteStringType:
case CborTextStringType: {
char *str;
if (type == CborByteStringType) {
err = dump_bytestring_base64url(&str, it);
status->flags = TypeWasNotNative;
} else {
size_t n = 0;
err = cbor_value_dup_text_string(it, &str, &n, it);
}
if (err)
return err;
err = (fprintf(out, "\"%s\"", str) < 0) ? CborErrorIO : CborNoError;
free(str);
return err;
}
case CborTagType:
return tagged_value_to_json(out, it, flags, status);
case CborSimpleType: {
uint8_t simple_type;
cbor_value_get_simple_type(it, &simple_type); /* can't fail */
status->flags = TypeWasNotNative;
status->originalNumber = simple_type;
if (fprintf(out, "\"simple(%" PRIu8 ")\"", simple_type) < 0)
return CborErrorIO;
break;
}
case CborNullType:
if (fprintf(out, "null") < 0)
return CborErrorIO;
break;
case CborUndefinedType:
status->flags = TypeWasNotNative;
if (fprintf(out, "\"undefined\"") < 0)
return CborErrorIO;
break;
case CborBooleanType: {
bool val;
cbor_value_get_boolean(it, &val); /* can't fail */
if (fprintf(out, val ? "true" : "false") < 0)
return CborErrorIO;
break;
}
#ifndef CBOR_NO_FLOATING_POINT
case CborDoubleType: {
double val;
if (false) {
float f;
case CborFloatType:
status->flags = TypeWasNotNative;
cbor_value_get_float(it, &f);
val = f;
} else if (false) {
uint16_t f16;
case CborHalfFloatType:
# ifndef CBOR_NO_HALF_FLOAT_TYPE
status->flags = TypeWasNotNative;
cbor_value_get_half_float(it, &f16);
val = decode_half(f16);
# else
(void)f16;
err = CborErrorUnsupportedType;
if (fprintf(out, "%.0f", num) < 0) /* this number has no fraction, so no decimal points please */
return CborErrorIO;
break;
# endif
} else {
cbor_value_get_double(it, &val);
}
int r = fpclassify(val);
if (r == FP_NAN || r == FP_INFINITE) {
case CborByteStringType:
case CborTextStringType: {
char *str;
if (type == CborByteStringType) {
err = dump_bytestring_base64url(&str, it);
status->flags = TypeWasNotNative;
} else {
size_t n = 0;
err = cbor_value_dup_text_string(it, &str, &n, it);
}
if (err)
return err;
err = (fprintf(out, "\"%s\"", str) < 0) ? CborErrorIO : CborNoError;
free(str);
return err;
}
case CborTagType:
return tagged_value_to_json(out, it, flags, status);
case CborSimpleType: {
uint8_t simple_type;
cbor_value_get_simple_type(it, &simple_type); /* can't fail */
status->flags = TypeWasNotNative;
status->originalNumber = simple_type;
if (fprintf(out, "\"simple(%" PRIu8 ")\"", simple_type) < 0)
return CborErrorIO;
break;
}
case CborNullType:
if (fprintf(out, "null") < 0)
return CborErrorIO;
status->flags |= r == FP_NAN ? NumberWasNaN :
NumberWasInfinite | (val < 0 ? NumberWasNegative : 0);
} else {
uint64_t ival = (uint64_t)fabs(val);
if ((double)ival == fabs(val)) {
/* print as integer so we get the full precision */
r = fprintf(out, "%s%" PRIu64, val < 0 ? "-" : "", ival);
status->flags |= TypeWasNotNative; /* mark this integer number as a double */
} else {
/* this number is definitely not a 64-bit integer */
r = fprintf(out, "%." DBL_DECIMAL_DIG_STR "g", val);
}
if (r < 0)
break;
case CborUndefinedType:
status->flags = TypeWasNotNative;
if (fprintf(out, "\"undefined\"") < 0)
return CborErrorIO;
break;
case CborBooleanType: {
bool val;
cbor_value_get_boolean(it, &val); /* can't fail */
if (fprintf(out, val ? "true" : "false") < 0)
return CborErrorIO;
break;
}
break;
}
#ifndef CBOR_NO_FLOATING_POINT
case CborDoubleType: {
double val;
if (false) {
float f;
case CborFloatType:
status->flags = TypeWasNotNative;
cbor_value_get_float(it, &f);
val = f;
} else if (false) {
uint16_t f16;
case CborHalfFloatType:
# ifndef CBOR_NO_HALF_FLOAT_TYPE
status->flags = TypeWasNotNative;
cbor_value_get_half_float(it, &f16);
val = decode_half(f16);
# else
(void)f16;
err = CborErrorUnsupportedType;
break;
# endif
} else {
cbor_value_get_double(it, &val);
}
int r = fpclassify(val);
if (r == FP_NAN || r == FP_INFINITE) {
if (fprintf(out, "null") < 0)
return CborErrorIO;
status->flags |= r == FP_NAN ? NumberWasNaN :
NumberWasInfinite | (val < 0 ? NumberWasNegative : 0);
} else {
uint64_t ival = (uint64_t)fabs(val);
if ((double)ival == fabs(val)) {
/* print as integer so we get the full precision */
r = fprintf(out, "%s%" PRIu64, val < 0 ? "-" : "", ival);
status->flags |= TypeWasNotNative; /* mark this integer number as a double */
} else {
/* this number is definitely not a 64-bit integer */
r = fprintf(out, "%." DBL_DECIMAL_DIG_STR "g", val);
}
if (r < 0)
return CborErrorIO;
}
break;
}
#else
case CborDoubleType:
case CborFloatType:
case CborHalfFloatType:
err = CborErrorUnsupportedType;
break;
case CborDoubleType:
case CborFloatType:
case CborHalfFloatType:
err = CborErrorUnsupportedType;
break;
#endif /* !CBOR_NO_FLOATING_POINT */
case CborInvalidType:
return CborErrorUnknownType;
case CborInvalidType:
return CborErrorUnknownType;
}
return cbor_value_advance_fixed(it);

View file

@ -242,7 +242,7 @@
struct KnownTagData { uint32_t tag; uint32_t types; };
static const struct KnownTagData knownTagData[] = {
{ 0, (uint32_t)CborTextStringType },
{ 1, (uint32_t)(CborIntegerType+1) },
{ 1, (uint32_t)(CborIntegerType + 1) },
{ 2, (uint32_t)CborByteStringType },
{ 3, (uint32_t)CborByteStringType },
{ 4, (uint32_t)CborArrayType },
@ -270,7 +270,7 @@ static CborError validate_value(CborValue *it, uint32_t flags, int recursionLeft
static inline CborError validate_utf8_string(const void *ptr, size_t n)
{
const uint8_t *buffer = (const uint8_t *)ptr;
const uint8_t * const end = buffer + n;
const uint8_t *const end = buffer + n;
while (buffer < end) {
uint32_t uc = get_utf8(&buffer, end);
if (uc == ~0U)
@ -287,7 +287,7 @@ static inline CborError validate_simple_type(uint8_t simple_type, uint32_t flags
if (simple_type < 32)
return (flags & CborValidateNoUnknownSimpleTypesSA) ? CborErrorUnknownSimpleType : CborNoError;
return (flags & CborValidateNoUnknownSimpleTypes) == CborValidateNoUnknownSimpleTypes ?
CborErrorUnknownSimpleType : CborNoError;
CborErrorUnknownSimpleType : CborNoError;
}
static inline CborError validate_number(const CborValue *it, CborType type, uint32_t flags)
@ -326,7 +326,7 @@ static inline CborError validate_tag(CborValue *it, CborTag tag, uint32_t flags,
CborType type = cbor_value_get_type(it);
const size_t knownTagCount = sizeof(knownTagData) / sizeof(knownTagData[0]);
const struct KnownTagData *tagData = knownTagData;
const struct KnownTagData * const knownTagDataEnd = knownTagData + knownTagCount;
const struct KnownTagData *const knownTagDataEnd = knownTagData + knownTagCount;
if (!recursionLeft)
return CborErrorNestingTooDeep;
@ -334,7 +334,7 @@ static inline CborError validate_tag(CborValue *it, CborTag tag, uint32_t flags,
return CborErrorExcludedType;
/* find the tag data, if any */
for ( ; tagData != knownTagDataEnd; ++tagData) {
for (; tagData != knownTagDataEnd; ++tagData) {
if (tagData->tag < tag)
continue;
if (tagData->tag > tag)
@ -526,110 +526,110 @@ static CborError validate_value(CborValue *it, uint32_t flags, int recursionLeft
}
switch (type) {
case CborArrayType:
case CborMapType: {
/* recursive type */
CborValue recursed;
err = cbor_value_enter_container(it, &recursed);
if (!err)
err = validate_container(&recursed, type, flags, recursionLeft - 1);
if (err) {
it->ptr = recursed.ptr;
return err;
case CborArrayType:
case CborMapType: {
/* recursive type */
CborValue recursed;
err = cbor_value_enter_container(it, &recursed);
if (!err)
err = validate_container(&recursed, type, flags, recursionLeft - 1);
if (err) {
it->ptr = recursed.ptr;
return err;
}
err = cbor_value_leave_container(it, &recursed);
if (err)
return err;
return CborNoError;
}
err = cbor_value_leave_container(it, &recursed);
if (err)
return err;
return CborNoError;
}
case CborIntegerType: {
uint64_t val;
err = cbor_value_get_raw_integer(it, &val);
cbor_assert(err == CborNoError); /* can't fail */
case CborIntegerType: {
uint64_t val;
err = cbor_value_get_raw_integer(it, &val);
cbor_assert(err == CborNoError); /* can't fail */
break;
}
break;
}
case CborByteStringType:
case CborTextStringType: {
size_t n = 0;
const void *ptr;
case CborByteStringType:
case CborTextStringType: {
size_t n = 0;
const void *ptr;
err = _cbor_value_prepare_string_iteration(it);
if (err)
return err;
while (1) {
err = validate_number(it, type, flags);
err = _cbor_value_prepare_string_iteration(it);
if (err)
return err;
err = _cbor_value_get_string_chunk(it, &ptr, &n, it);
if (err)
return err;
if (!ptr)
break;
if (type == CborTextStringType && flags & CborValidateUtf8) {
err = validate_utf8_string(ptr, n);
while (1) {
err = validate_number(it, type, flags);
if (err)
return err;
err = _cbor_value_get_string_chunk(it, &ptr, &n, it);
if (err)
return err;
if (!ptr)
break;
if (type == CborTextStringType && flags & CborValidateUtf8) {
err = validate_utf8_string(ptr, n);
if (err)
return err;
}
}
return CborNoError;
}
return CborNoError;
}
case CborTagType: {
CborTag tag;
err = cbor_value_get_tag(it, &tag);
cbor_assert(err == CborNoError); /* can't fail */
case CborTagType: {
CborTag tag;
err = cbor_value_get_tag(it, &tag);
cbor_assert(err == CborNoError); /* can't fail */
err = cbor_value_advance_fixed(it);
if (err)
return err;
err = validate_tag(it, tag, flags, recursionLeft - 1);
if (err)
return err;
err = cbor_value_advance_fixed(it);
if (err)
return err;
err = validate_tag(it, tag, flags, recursionLeft - 1);
if (err)
return err;
return CborNoError;
}
return CborNoError;
}
case CborSimpleType: {
uint8_t simple_type;
err = cbor_value_get_simple_type(it, &simple_type);
cbor_assert(err == CborNoError); /* can't fail */
err = validate_simple_type(simple_type, flags);
if (err)
return err;
break;
}
case CborSimpleType: {
uint8_t simple_type;
err = cbor_value_get_simple_type(it, &simple_type);
cbor_assert(err == CborNoError); /* can't fail */
err = validate_simple_type(simple_type, flags);
if (err)
return err;
break;
}
case CborNullType:
case CborBooleanType:
break;
case CborNullType:
case CborBooleanType:
break;
case CborUndefinedType:
if (flags & CborValidateNoUndefined)
return CborErrorExcludedType;
break;
case CborUndefinedType:
if (flags & CborValidateNoUndefined)
return CborErrorExcludedType;
break;
case CborHalfFloatType:
case CborFloatType:
case CborDoubleType: {
case CborHalfFloatType:
case CborFloatType:
case CborDoubleType: {
#ifdef CBOR_NO_FLOATING_POINT
return CborErrorUnsupportedType;
return CborErrorUnsupportedType;
#else
err = validate_floating_point(it, type, flags);
if (err)
return err;
break;
err = validate_floating_point(it, type, flags);
if (err)
return err;
break;
#endif /* !CBOR_NO_FLOATING_POINT */
}
}
case CborInvalidType:
return CborErrorUnknownType;
case CborInvalidType:
return CborErrorUnknownType;
}
err = cbor_value_advance_fixed(it);

View file

@ -47,8 +47,7 @@ typedef size_t LenType;
#include "compilersupport_p.h"
struct Buffer
{
struct Buffer {
char **ptr;
size_t *len;
size_t alloc;