mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2026-05-12 11:18:11 -07:00
added tinycbor
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
|
||||
LIB_A = tinycbor.a
|
||||
tinycbor_SOURCES = \
|
||||
cborencoder.c \
|
||||
cborencoder_close_container_checked.c \
|
||||
cborerrorstrings.c \
|
||||
cborparser.c \
|
||||
cborparser_dup_string.c \
|
||||
cborpretty.c \
|
||||
cbortojson.c \
|
||||
cborvalidation.c \
|
||||
|
||||
CFILES = $(filter %.c, $(tinycbor_SOURCES))
|
||||
CMDOBJS = $(CFILES:%.c=%.o)
|
||||
CLEAN = $(CMDOBJS)
|
||||
|
||||
CC= gcc
|
||||
CFLAGS= -O2 -Wall -Wno-unused-variable -Wno-unused-function
|
||||
LIBS= -lm $(SYSLIBS) $(MYLIBS)
|
||||
DEFAULT_INCLUDES = -I. -I..
|
||||
DEFS = -DHAVE_STDINT_H
|
||||
|
||||
AR= ar rcs
|
||||
RANLIB= ranlib
|
||||
RM= rm -f
|
||||
TST= echo
|
||||
|
||||
SYSLDFLAGS=
|
||||
SYSLIBS=
|
||||
|
||||
MYLIBS=
|
||||
MYOBJS=
|
||||
|
||||
all: $(CMDOBJS)
|
||||
$(AR) $(LIB_A) $(CMDOBJS)
|
||||
$(RANLIB) $(LIB_A)
|
||||
|
||||
clean:
|
||||
$(RM) $(CLEAN)
|
||||
$(RM) $(LIB_A)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(CFLAGS) -c -o $@ $< $(LIBS)
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,57 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2015 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#define _BSD_SOURCE 1
|
||||
#define _DEFAULT_SOURCE 1
|
||||
#ifndef __STDC_LIMIT_MACROS
|
||||
# define __STDC_LIMIT_MACROS 1
|
||||
#endif
|
||||
|
||||
#include "cbor.h"
|
||||
|
||||
/**
|
||||
* \addtogroup CborEncoding
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* Closes the CBOR container (array or map) provided by \a containerEncoder and
|
||||
* updates the CBOR stream provided by \a encoder. Both parameters must be the
|
||||
* same as were passed to cbor_encoder_create_array() or
|
||||
* cbor_encoder_create_map().
|
||||
*
|
||||
* Prior to version 0.5, cbor_encoder_close_container() did not check the
|
||||
* number of items added. Since that version, it does and now
|
||||
* cbor_encoder_close_container_checked() is no longer needed.
|
||||
*
|
||||
* \sa cbor_encoder_create_array(), cbor_encoder_create_map()
|
||||
*/
|
||||
CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
|
||||
{
|
||||
return cbor_encoder_close_container(encoder, containerEncoder);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
@@ -0,0 +1,182 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "cbor.h"
|
||||
|
||||
#ifndef _
|
||||
# define _(msg) msg
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \enum CborError
|
||||
* \ingroup CborGlobals
|
||||
* The CborError enum contains the possible error values used by the CBOR encoder and decoder.
|
||||
*
|
||||
* TinyCBOR functions report success by returning CborNoError, or one error
|
||||
* condition by returning one of the values below. One exception is the
|
||||
* out-of-memory condition (CborErrorOutOfMemory), which the functions for \ref
|
||||
* CborEncoding may report in bit-wise OR with other conditions.
|
||||
*
|
||||
* This technique allows code to determine whether the only error condition was
|
||||
* a lack of buffer space, which may not be a fatal condition if the buffer can
|
||||
* be resized. Additionally, the functions for \ref CborEncoding may continue
|
||||
* to be used even after CborErrorOutOfMemory is returned, and instead they
|
||||
* will simply calculate the extra space needed.
|
||||
*
|
||||
* \value CborNoError No error occurred
|
||||
* \omitvalue CborUnknownError
|
||||
* \value CborErrorUnknownLength Request for the length of an array, map or string whose length is not provided in the CBOR stream
|
||||
* \value CborErrorAdvancePastEOF Not enough data in the stream to decode item (decoding would advance past end of stream)
|
||||
* \value CborErrorIO An I/O error occurred, probably due to an out-of-memory situation
|
||||
* \value CborErrorGarbageAtEnd Bytes exist past the end of the CBOR stream
|
||||
* \value CborErrorUnexpectedEOF End of stream reached unexpectedly
|
||||
* \value CborErrorUnexpectedBreak A CBOR break byte was found where not expected
|
||||
* \value CborErrorUnknownType An unknown type (future extension to CBOR) was found in the stream
|
||||
* \value CborErrorIllegalType An invalid type was found while parsing a chunked CBOR string
|
||||
* \value CborErrorIllegalNumber An illegal initial byte (encoding unspecified additional information) was found
|
||||
* \value CborErrorIllegalSimpleType An illegal encoding of a CBOR Simple Type of value less than 32 was found
|
||||
* \omitvalue CborErrorUnknownSimpleType
|
||||
* \omitvalue CborErrorUnknownTag
|
||||
* \omitvalue CborErrorInappropriateTagForType
|
||||
* \omitvalue CborErrorDuplicateObjectKeys
|
||||
* \value CborErrorInvalidUtf8TextString Illegal UTF-8 encoding found while parsing CBOR Text String
|
||||
* \value CborErrorTooManyItems Too many items were added to CBOR map or array of pre-determined length
|
||||
* \value CborErrorTooFewItems Too few items were added to CBOR map or array of pre-determined length
|
||||
* \value CborErrorDataTooLarge Data item size exceeds TinyCBOR's implementation limits
|
||||
* \value CborErrorNestingTooDeep Data item nesting exceeds TinyCBOR's implementation limits
|
||||
* \omitvalue CborErrorUnsupportedType
|
||||
* \value CborErrorJsonObjectKeyIsAggregate Conversion to JSON failed because the key in a map is a CBOR map or array
|
||||
* \value CborErrorJsonObjectKeyNotString Conversion to JSON failed because the key in a map is not a text string
|
||||
* \value CborErrorOutOfMemory During CBOR encoding, the buffer provided is insufficient for encoding the data item;
|
||||
* in other situations, TinyCBOR failed to allocate memory
|
||||
* \value CborErrorInternalError An internal error occurred in TinyCBOR
|
||||
*/
|
||||
|
||||
/**
|
||||
* \ingroup CborGlobals
|
||||
* Returns the error string corresponding to the CBOR error condition \a error.
|
||||
*/
|
||||
const char *cbor_error_string(CborError error)
|
||||
{
|
||||
switch (error) {
|
||||
case CborNoError:
|
||||
return "";
|
||||
|
||||
case CborUnknownError:
|
||||
return _("unknown error");
|
||||
|
||||
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 CborErrorAdvancePastEOF:
|
||||
return _("attempted to advance past EOF");
|
||||
|
||||
case CborErrorIO:
|
||||
return _("I/O error");
|
||||
|
||||
case CborErrorGarbageAtEnd:
|
||||
return _("garbage after the end of the content");
|
||||
|
||||
case CborErrorUnexpectedEOF:
|
||||
return _("unexpected end of data");
|
||||
|
||||
case CborErrorUnexpectedBreak:
|
||||
return _("unexpected 'break' byte");
|
||||
|
||||
case CborErrorUnknownType:
|
||||
return _("illegal byte (encodes future extension type)");
|
||||
|
||||
case CborErrorIllegalType:
|
||||
return _("mismatched string type in chunked string");
|
||||
|
||||
case CborErrorIllegalNumber:
|
||||
return _("illegal initial byte (encodes unspecified additional information)");
|
||||
|
||||
case CborErrorIllegalSimpleType:
|
||||
return _("illegal encoding of simple type smaller than 32");
|
||||
|
||||
case CborErrorUnknownSimpleType:
|
||||
return _("unknown simple type");
|
||||
|
||||
case CborErrorUnknownTag:
|
||||
return _("unknown tag");
|
||||
|
||||
case CborErrorInappropriateTagForType:
|
||||
return _("inappropriate tag for type");
|
||||
|
||||
case CborErrorDuplicateObjectKeys:
|
||||
return _("duplicate keys in object");
|
||||
|
||||
case CborErrorInvalidUtf8TextString:
|
||||
return _("invalid UTF-8 content in string");
|
||||
|
||||
case CborErrorExcludedType:
|
||||
return _("excluded type found");
|
||||
|
||||
case CborErrorExcludedValue:
|
||||
return _("excluded value found");
|
||||
|
||||
case CborErrorImproperValue:
|
||||
case CborErrorOverlongEncoding:
|
||||
return _("value encoded in non-canonical form");
|
||||
|
||||
case CborErrorMapKeyNotString:
|
||||
case CborErrorJsonObjectKeyNotString:
|
||||
return _("key in map is not a string");
|
||||
|
||||
case CborErrorMapNotSorted:
|
||||
return _("map is not sorted");
|
||||
|
||||
case CborErrorMapKeysNotUnique:
|
||||
return _("map keys are not unique");
|
||||
|
||||
case CborErrorTooManyItems:
|
||||
return _("too many items added to encoder");
|
||||
|
||||
case CborErrorTooFewItems:
|
||||
return _("too few items added to encoder");
|
||||
|
||||
case CborErrorDataTooLarge:
|
||||
return _("internal error: data too large");
|
||||
|
||||
case CborErrorNestingTooDeep:
|
||||
return _("internal error: too many nested containers found in recursive function");
|
||||
|
||||
case CborErrorUnsupportedType:
|
||||
return _("unsupported type");
|
||||
|
||||
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 CborErrorInternalError:
|
||||
return _("internal error");
|
||||
}
|
||||
return cbor_error_string(CborUnknownError);
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2017 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CBORINTERNAL_P_H
|
||||
#define CBORINTERNAL_P_H
|
||||
|
||||
#include "compilersupport_p.h"
|
||||
|
||||
#ifndef CBOR_NO_FLOATING_POINT
|
||||
# include <float.h>
|
||||
# include <math.h>
|
||||
#else
|
||||
# ifndef CBOR_NO_HALF_FLOAT_TYPE
|
||||
# define CBOR_NO_HALF_FLOAT_TYPE 1
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#ifndef CBOR_NO_HALF_FLOAT_TYPE
|
||||
# ifdef __F16C__
|
||||
# include <immintrin.h>
|
||||
static inline unsigned short encode_half(double val)
|
||||
{
|
||||
return _cvtss_sh((float)val, 3);
|
||||
}
|
||||
static inline double decode_half(unsigned short half)
|
||||
{
|
||||
return _cvtsh_ss(half);
|
||||
}
|
||||
# else
|
||||
/* software implementation of float-to-fp16 conversions */
|
||||
static inline unsigned short encode_half(double val)
|
||||
{
|
||||
uint64_t v;
|
||||
int sign, exp, mant;
|
||||
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 */
|
||||
exp -= 1023;
|
||||
if (exp == 1024) {
|
||||
/* infinity or NaN */
|
||||
exp = 16;
|
||||
mant >>= 1;
|
||||
} else if (exp >= 16) {
|
||||
/* overflow, as largest number */
|
||||
exp = 15;
|
||||
mant = 1023;
|
||||
} else if (exp >= -14) {
|
||||
/* regular normal */
|
||||
} else if (exp >= -24) {
|
||||
/* subnormal */
|
||||
mant |= 1024;
|
||||
mant >>= -(exp + 14);
|
||||
exp = -15;
|
||||
} else {
|
||||
/* underflow, make zero */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* safe cast here as bit operations above guarantee not to overflow */
|
||||
return (unsigned short)(sign | ((exp + 15) << 10) | mant);
|
||||
}
|
||||
|
||||
/* this function was copied & adapted from RFC 7049 Appendix D */
|
||||
static inline double decode_half(unsigned short half)
|
||||
{
|
||||
int exp = (half >> 10) & 0x1f;
|
||||
int mant = half & 0x3ff;
|
||||
double val;
|
||||
if (exp == 0) val = ldexp(mant, -24);
|
||||
else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
|
||||
else val = mant == 0 ? INFINITY : NAN;
|
||||
return half & 0x8000 ? -val : val;
|
||||
}
|
||||
# endif
|
||||
#endif /* CBOR_NO_HALF_FLOAT_TYPE */
|
||||
|
||||
#ifndef CBOR_INTERNAL_API
|
||||
# define CBOR_INTERNAL_API
|
||||
#endif
|
||||
|
||||
#ifndef CBOR_PARSER_MAX_RECURSIONS
|
||||
# define CBOR_PARSER_MAX_RECURSIONS 1024
|
||||
#endif
|
||||
|
||||
/*
|
||||
* CBOR Major types
|
||||
* Encoded in the high 3 bits of the descriptor byte
|
||||
* See http://tools.ietf.org/html/rfc7049#section-2.1
|
||||
*/
|
||||
typedef enum CborMajorTypes {
|
||||
UnsignedIntegerType = 0U,
|
||||
NegativeIntegerType = 1U,
|
||||
ByteStringType = 2U,
|
||||
TextStringType = 3U,
|
||||
ArrayType = 4U,
|
||||
MapType = 5U, /* a.k.a. object */
|
||||
TagType = 6U,
|
||||
SimpleTypesType = 7U
|
||||
} CborMajorTypes;
|
||||
|
||||
/*
|
||||
* CBOR simple and floating point types
|
||||
* Encoded in the low 8 bits of the descriptor byte when the
|
||||
* Major Type is 7.
|
||||
*/
|
||||
typedef enum CborSimpleTypes {
|
||||
FalseValue = 20,
|
||||
TrueValue = 21,
|
||||
NullValue = 22,
|
||||
UndefinedValue = 23,
|
||||
SimpleTypeInNextByte = 24, /* not really a simple type */
|
||||
HalfPrecisionFloat = 25, /* ditto */
|
||||
SinglePrecisionFloat = 26, /* ditto */
|
||||
DoublePrecisionFloat = 27, /* ditto */
|
||||
Break = 31
|
||||
} CborSimpleTypes;
|
||||
|
||||
enum {
|
||||
SmallValueBitLength = 5U,
|
||||
SmallValueMask = (1U << SmallValueBitLength) - 1, /* 31 */
|
||||
Value8Bit = 24U,
|
||||
Value16Bit = 25U,
|
||||
Value32Bit = 26U,
|
||||
Value64Bit = 27U,
|
||||
IndefiniteLength = 31U,
|
||||
|
||||
MajorTypeShift = SmallValueBitLength,
|
||||
MajorTypeMask = (int) (~0U << MajorTypeShift),
|
||||
|
||||
BreakByte = (unsigned)Break | (SimpleTypesType << MajorTypeShift)
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
|
||||
#endif /* CBORINTERNAL_P_H */
|
||||
@@ -0,0 +1,62 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2015 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CBORJSON_H
|
||||
#define CBORJSON_H
|
||||
|
||||
#include "cbor.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Conversion to JSON */
|
||||
enum CborToJsonFlags
|
||||
{
|
||||
CborConvertAddMetadata = 1,
|
||||
CborConvertTagsToObjects = 2,
|
||||
CborConvertIgnoreTags = 0,
|
||||
|
||||
CborConvertObeyByteStringTags = 0,
|
||||
CborConvertByteStringsToBase64Url = 4,
|
||||
|
||||
CborConvertRequireMapStringKeys = 0,
|
||||
CborConvertStringifyMapKeys = 8,
|
||||
|
||||
CborConvertDefaultFlags = 0
|
||||
};
|
||||
|
||||
CBOR_API CborError cbor_value_to_json_advance(FILE *out, CborValue *value, int flags);
|
||||
CBOR_INLINE_API CborError cbor_value_to_json(FILE *out, const CborValue *value, int flags)
|
||||
{
|
||||
CborValue copy = *value;
|
||||
return cbor_value_to_json_advance(out, ©, flags);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CBORJSON_H */
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,119 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2016 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef _BSD_SOURCE
|
||||
#define _BSD_SOURCE 1
|
||||
#endif
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
#define _DEFAULT_SOURCE 1
|
||||
#endif
|
||||
#ifndef __STDC_LIMIT_MACROS
|
||||
# define __STDC_LIMIT_MACROS 1
|
||||
#endif
|
||||
|
||||
#include "cbor.h"
|
||||
#include "compilersupport_p.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
|
||||
*
|
||||
* Allocates memory for the string pointed by \a value and copies it into this
|
||||
* buffer. The pointer to the buffer is stored in \a buffer and the number of
|
||||
* bytes copied is stored in \a buflen (those variables must not be NULL).
|
||||
*
|
||||
* If the iterator \a value does not point to a text string, the behaviour is
|
||||
* undefined, so checking with \ref cbor_value_get_type or \ref
|
||||
* cbor_value_is_text_string is recommended.
|
||||
*
|
||||
* If \c malloc returns a NULL pointer, this function will return error
|
||||
* condition \ref CborErrorOutOfMemory.
|
||||
*
|
||||
* On success, \c{*buffer} will contain a valid pointer that must be freed by
|
||||
* calling \c{free()}. This is the case even for zero-length strings.
|
||||
*
|
||||
* The \a next pointer, if not null, will be updated to point to the next item
|
||||
* after this string. If \a value points to the last item, then \a next will be
|
||||
* invalid.
|
||||
*
|
||||
* This function may not run in constant time (it will run in O(n) time on the
|
||||
* number of chunks). It requires constant memory (O(1)) in addition to the
|
||||
* malloc'ed block.
|
||||
*
|
||||
* \note This function does not perform UTF-8 validation on the incoming text
|
||||
* string.
|
||||
*
|
||||
* \sa cbor_value_get_text_string_chunk(), cbor_value_copy_text_string(), cbor_value_dup_byte_string()
|
||||
*/
|
||||
|
||||
/**
|
||||
* \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
|
||||
*
|
||||
* Allocates memory for the string pointed by \a value and copies it into this
|
||||
* buffer. The pointer to the buffer is stored in \a buffer and the number of
|
||||
* bytes copied is stored in \a buflen (those variables must not be NULL).
|
||||
*
|
||||
* If the iterator \a value does not point to a byte string, the behaviour is
|
||||
* undefined, so checking with \ref cbor_value_get_type or \ref
|
||||
* cbor_value_is_byte_string is recommended.
|
||||
*
|
||||
* If \c malloc returns a NULL pointer, this function will return error
|
||||
* condition \ref CborErrorOutOfMemory.
|
||||
*
|
||||
* On success, \c{*buffer} will contain a valid pointer that must be freed by
|
||||
* calling \c{free()}. This is the case even for zero-length strings.
|
||||
*
|
||||
* The \a next pointer, if not null, will be updated to point to the next item
|
||||
* after this string. If \a value points to the last item, then \a next will be
|
||||
* invalid.
|
||||
*
|
||||
* This function may not run in constant time (it will run in O(n) time on the
|
||||
* number of chunks). It requires constant memory (O(1)) in addition to the
|
||||
* malloc'ed block.
|
||||
*
|
||||
* \sa cbor_value_get_text_string_chunk(), cbor_value_copy_byte_string(), cbor_value_dup_text_string()
|
||||
*/
|
||||
CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
|
||||
{
|
||||
CborError err;
|
||||
cbor_assert(buffer);
|
||||
cbor_assert(buflen);
|
||||
*buflen = SIZE_MAX;
|
||||
err = _cbor_value_copy_string(value, NULL, buflen, NULL);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
++*buflen;
|
||||
*buffer = malloc(*buflen);
|
||||
if (!*buffer) {
|
||||
/* out of memory */
|
||||
return CborErrorOutOfMemory;
|
||||
}
|
||||
err = _cbor_value_copy_string(value, *buffer, buflen, next);
|
||||
if (err) {
|
||||
free(*buffer);
|
||||
return err;
|
||||
}
|
||||
return CborNoError;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,87 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2017 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "cbor.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static CborError cbor_fprintf(void *out, const char *fmt, ...)
|
||||
{
|
||||
int n;
|
||||
|
||||
va_list list;
|
||||
va_start(list, fmt);
|
||||
n = vfprintf((FILE *)out, fmt, list);
|
||||
va_end(list);
|
||||
|
||||
return n < 0 ? CborErrorIO : CborNoError;
|
||||
}
|
||||
|
||||
/**
|
||||
* \fn CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
|
||||
*
|
||||
* Converts the current CBOR type pointed to by \a value to its textual
|
||||
* representation and writes it to the \a out stream. If an error occurs, this
|
||||
* function returns an error code similar to CborParsing.
|
||||
*
|
||||
* \sa cbor_value_to_pretty_advance(), cbor_value_to_json_advance()
|
||||
*/
|
||||
|
||||
/**
|
||||
* Converts the current CBOR type pointed to by \a value to its textual
|
||||
* representation and writes it to the \a out stream. If an error occurs, this
|
||||
* function returns an error code similar to CborParsing.
|
||||
*
|
||||
* If no error ocurred, this function advances \a value to the next element.
|
||||
* Often, concatenating the text representation of multiple elements can be
|
||||
* done by appending a comma to the output stream in between calls to this
|
||||
* function.
|
||||
*
|
||||
* \sa cbor_value_to_pretty(), cbor_value_to_pretty_stream(), cbor_value_to_json_advance()
|
||||
*/
|
||||
CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
|
||||
{
|
||||
return cbor_value_to_pretty_stream(cbor_fprintf, out, value, CborPrettyDefaultFlags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the current CBOR type pointed to by \a value to its textual
|
||||
* representation and writes it to the \a out stream. If an error occurs, this
|
||||
* function returns an error code similar to CborParsing.
|
||||
*
|
||||
* The textual representation can be controlled by the \a flags parameter (see
|
||||
* CborPrettyFlags for more information).
|
||||
*
|
||||
* If no error ocurred, this function advances \a value to the next element.
|
||||
* Often, concatenating the text representation of multiple elements can be
|
||||
* done by appending a comma to the output stream in between calls to this
|
||||
* function.
|
||||
*
|
||||
* \sa cbor_value_to_pretty_stream(), cbor_value_to_pretty(), cbor_value_to_json_advance()
|
||||
*/
|
||||
CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags)
|
||||
{
|
||||
return cbor_value_to_pretty_stream(cbor_fprintf, out, value, flags);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,205 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2017 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef COMPILERSUPPORT_H
|
||||
#define COMPILERSUPPORT_H
|
||||
|
||||
#include "cbor.h"
|
||||
|
||||
#ifndef _BSD_SOURCE
|
||||
# define _BSD_SOURCE
|
||||
#endif
|
||||
#ifndef _DEFAULT_SOURCE
|
||||
# define _DEFAULT_SOURCE
|
||||
#endif
|
||||
#ifndef assert
|
||||
# include <assert.h>
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef __cplusplus
|
||||
# include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L || __cpp_static_assert >= 200410
|
||||
# define cbor_static_assert(x) static_assert(x, #x)
|
||||
#elif !defined(__cplusplus) && defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 406) && (__STDC_VERSION__ > 199901L)
|
||||
# define cbor_static_assert(x) _Static_assert(x, #x)
|
||||
#else
|
||||
# define cbor_static_assert(x) ((void)sizeof(char[2*!!(x) - 1]))
|
||||
#endif
|
||||
#if __STDC_VERSION__ >= 199901L || defined(__cplusplus)
|
||||
/* inline is a keyword */
|
||||
#else
|
||||
/* use the definition from cbor.h */
|
||||
# define inline CBOR_INLINE
|
||||
#endif
|
||||
|
||||
#ifdef NDEBUG
|
||||
# define cbor_assert(cond) do { if (!(cond)) unreachable(); } while (0)
|
||||
#else
|
||||
# define cbor_assert(cond) assert(cond)
|
||||
#endif
|
||||
|
||||
#ifndef STRINGIFY
|
||||
#define STRINGIFY(x) STRINGIFY2(x)
|
||||
#endif
|
||||
#define STRINGIFY2(x) #x
|
||||
|
||||
#if !defined(UINT32_MAX) || !defined(INT64_MAX)
|
||||
/* C89? We can define UINT32_MAX portably, but not INT64_MAX */
|
||||
# error "Your system has stdint.h but that doesn't define UINT32_MAX or INT64_MAX"
|
||||
#endif
|
||||
|
||||
#ifndef DBL_DECIMAL_DIG
|
||||
/* DBL_DECIMAL_DIG is C11 */
|
||||
# define DBL_DECIMAL_DIG 17
|
||||
#endif
|
||||
#define DBL_DECIMAL_DIG_STR STRINGIFY(DBL_DECIMAL_DIG)
|
||||
|
||||
#if defined(__GNUC__) && defined(__i386__) && !defined(__iamcu__)
|
||||
# define CBOR_INTERNAL_API_CC __attribute__((regparm(3)))
|
||||
#elif defined(_MSC_VER) && defined(_M_IX86)
|
||||
# define CBOR_INTERNAL_API_CC __fastcall
|
||||
#else
|
||||
# define CBOR_INTERNAL_API_CC
|
||||
#endif
|
||||
|
||||
#ifndef __has_builtin
|
||||
# define __has_builtin(x) 0
|
||||
#endif
|
||||
|
||||
#if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) || \
|
||||
(__has_builtin(__builtin_bswap64) && __has_builtin(__builtin_bswap32))
|
||||
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||
# define cbor_ntohll __builtin_bswap64
|
||||
# define cbor_htonll __builtin_bswap64
|
||||
# define cbor_ntohl __builtin_bswap32
|
||||
# define cbor_htonl __builtin_bswap32
|
||||
# ifdef __INTEL_COMPILER
|
||||
# define cbor_ntohs _bswap16
|
||||
# define cbor_htons _bswap16
|
||||
# elif (__GNUC__ * 100 + __GNUC_MINOR__ >= 608) || __has_builtin(__builtin_bswap16)
|
||||
# define cbor_ntohs __builtin_bswap16
|
||||
# define cbor_htons __builtin_bswap16
|
||||
# else
|
||||
# define cbor_ntohs(x) (((uint16_t)(x) >> 8) | ((uint16_t)(x) << 8))
|
||||
# define cbor_htons cbor_ntohs
|
||||
# endif
|
||||
# else
|
||||
# define cbor_ntohll
|
||||
# define cbor_htonll
|
||||
# define cbor_ntohl
|
||||
# define cbor_htonl
|
||||
# define cbor_ntohs
|
||||
# define cbor_htons
|
||||
# endif
|
||||
#elif defined(__sun)
|
||||
# include <sys/byteorder.h>
|
||||
#elif defined(_MSC_VER)
|
||||
/* MSVC, which implies Windows, which implies little-endian and sizeof(long) == 4 */
|
||||
# include <stdlib.h>
|
||||
# define cbor_ntohll _byteswap_uint64
|
||||
# define cbor_htonll _byteswap_uint64
|
||||
# define cbor_ntohl _byteswap_ulong
|
||||
# define cbor_htonl _byteswap_ulong
|
||||
# define cbor_ntohs _byteswap_ushort
|
||||
# define cbor_htons _byteswap_ushort
|
||||
#endif
|
||||
#ifndef cbor_ntohs
|
||||
# include <arpa/inet.h>
|
||||
# define cbor_ntohs ntohs
|
||||
# define cbor_htons htons
|
||||
#endif
|
||||
#ifndef cbor_ntohl
|
||||
# include <arpa/inet.h>
|
||||
# define cbor_ntohl ntohl
|
||||
# define cbor_htonl htonl
|
||||
#endif
|
||||
#ifndef cbor_ntohll
|
||||
# define cbor_ntohll ntohll
|
||||
# define cbor_htonll htonll
|
||||
/* ntohll isn't usually defined */
|
||||
# ifndef ntohll
|
||||
# if (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
|
||||
(defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || \
|
||||
(defined(BYTE_ORDER) && defined(BIG_ENDIAN) && BYTE_ORDER == BIG_ENDIAN) || \
|
||||
(defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)) || (defined(__BIG_ENDIAN__) && !defined(__LITTLE_ENDIAN__)) || \
|
||||
defined(__ARMEB__) || defined(__MIPSEB__) || defined(__s390__) || defined(__sparc__)
|
||||
# define ntohll
|
||||
# define htonll
|
||||
# elif (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
|
||||
(defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
|
||||
(defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && BYTE_ORDER == LITTLE_ENDIAN) || \
|
||||
defined(_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN__) || defined(__ARMEL__) || defined(__MIPSEL__) || \
|
||||
defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__) || defined(__amd64)
|
||||
# define ntohll(x) ((ntohl((uint32_t)(x)) * UINT64_C(0x100000000)) + (ntohl((x) >> 32)))
|
||||
# define htonll ntohll
|
||||
# else
|
||||
# error "Unable to determine byte order!"
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
# define CONST_CAST(t, v) const_cast<t>(v)
|
||||
#else
|
||||
/* C-style const_cast without triggering a warning with -Wcast-qual */
|
||||
# define CONST_CAST(t, v) (t)(uintptr_t)(v)
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
#ifndef likely
|
||||
# define likely(x) __builtin_expect(!!(x), 1)
|
||||
#endif
|
||||
#ifndef unlikely
|
||||
# define unlikely(x) __builtin_expect(!!(x), 0)
|
||||
#endif
|
||||
# define unreachable() __builtin_unreachable()
|
||||
#elif defined(_MSC_VER)
|
||||
# define likely(x) (x)
|
||||
# define unlikely(x) (x)
|
||||
# define unreachable() __assume(0)
|
||||
#else
|
||||
# define likely(x) (x)
|
||||
# define unlikely(x) (x)
|
||||
# define unreachable() do {} while (0)
|
||||
#endif
|
||||
|
||||
static inline bool add_check_overflow(size_t v1, size_t v2, size_t *r)
|
||||
{
|
||||
#if ((defined(__GNUC__) && (__GNUC__ >= 5)) && !defined(__INTEL_COMPILER)) || __has_builtin(__builtin_add_overflow)
|
||||
return __builtin_add_overflow(v1, v2, r);
|
||||
#else
|
||||
/* unsigned additions are well-defined */
|
||||
*r = v1 + v2;
|
||||
return v1 > v1 + v2;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* COMPILERSUPPORT_H */
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2015 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#define _BSD_SOURCE 1
|
||||
#define _DEFAULT_SOURCE 1
|
||||
#define _GNU_SOURCE 1
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
#ifdef __APPLE__
|
||||
typedef int RetType;
|
||||
typedef int LenType;
|
||||
#elif __GLIBC__
|
||||
typedef ssize_t RetType;
|
||||
typedef size_t LenType;
|
||||
#else
|
||||
# error "Cannot implement open_memstream!"
|
||||
#endif
|
||||
|
||||
#include "compilersupport_p.h"
|
||||
|
||||
struct Buffer
|
||||
{
|
||||
char **ptr;
|
||||
size_t *len;
|
||||
size_t alloc;
|
||||
};
|
||||
|
||||
static RetType write_to_buffer(void *cookie, const char *data, LenType len)
|
||||
{
|
||||
struct Buffer *b = (struct Buffer *)cookie;
|
||||
char *ptr = *b->ptr;
|
||||
size_t newsize;
|
||||
|
||||
errno = EFBIG;
|
||||
if (unlikely(add_check_overflow(*b->len, len, &newsize)))
|
||||
return -1;
|
||||
|
||||
if (newsize >= b->alloc) { // NB! one extra byte is needed to avoid buffer overflow at close_buffer
|
||||
// make room
|
||||
size_t newalloc = newsize + newsize / 2 + 1; // give 50% more room
|
||||
ptr = realloc(ptr, newalloc);
|
||||
if (ptr == NULL)
|
||||
return -1;
|
||||
b->alloc = newalloc;
|
||||
*b->ptr = ptr;
|
||||
}
|
||||
|
||||
memcpy(ptr + *b->len, data, len);
|
||||
*b->len = newsize;
|
||||
return len;
|
||||
}
|
||||
|
||||
static int close_buffer(void *cookie)
|
||||
{
|
||||
struct Buffer *b = (struct Buffer *)cookie;
|
||||
if (*b->ptr)
|
||||
(*b->ptr)[*b->len] = '\0';
|
||||
free(b);
|
||||
return 0;
|
||||
}
|
||||
|
||||
FILE *open_memstream(char **bufptr, size_t *lenptr)
|
||||
{
|
||||
struct Buffer *b = (struct Buffer *)malloc(sizeof(struct Buffer));
|
||||
if (b == NULL)
|
||||
return NULL;
|
||||
b->alloc = 0;
|
||||
b->len = lenptr;
|
||||
b->ptr = bufptr;
|
||||
*bufptr = NULL;
|
||||
*lenptr = 0;
|
||||
|
||||
#ifdef __APPLE__
|
||||
return funopen(b, NULL, write_to_buffer, NULL, close_buffer);
|
||||
#elif __GLIBC__
|
||||
static const cookie_io_functions_t vtable = {
|
||||
NULL,
|
||||
write_to_buffer,
|
||||
NULL,
|
||||
close_buffer
|
||||
};
|
||||
return fopencookie(b, "w", vtable);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
#define TINYCBOR_VERSION_MAJOR 0
|
||||
#define TINYCBOR_VERSION_MINOR 5
|
||||
#define TINYCBOR_VERSION_PATCH 3
|
||||
@@ -0,0 +1,104 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2017 Intel Corporation
|
||||
**
|
||||
** 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.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CBOR_UTF8_H
|
||||
#define CBOR_UTF8_H
|
||||
|
||||
#include "compilersupport_p.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static inline uint32_t get_utf8(const uint8_t **buffer, const uint8_t *end)
|
||||
{
|
||||
int charsNeeded;
|
||||
uint32_t uc, min_uc;
|
||||
uint8_t b;
|
||||
ptrdiff_t n = end - *buffer;
|
||||
if (n == 0)
|
||||
return ~0U;
|
||||
|
||||
uc = *(*buffer)++;
|
||||
if (uc < 0x80) {
|
||||
/* single-byte UTF-8 */
|
||||
return uc;
|
||||
}
|
||||
|
||||
/* multi-byte UTF-8, decode it */
|
||||
if (unlikely(uc <= 0xC1))
|
||||
return ~0U;
|
||||
if (uc < 0xE0) {
|
||||
/* two-byte UTF-8 */
|
||||
charsNeeded = 2;
|
||||
min_uc = 0x80;
|
||||
uc &= 0x1f;
|
||||
} else if (uc < 0xF0) {
|
||||
/* three-byte UTF-8 */
|
||||
charsNeeded = 3;
|
||||
min_uc = 0x800;
|
||||
uc &= 0x0f;
|
||||
} else if (uc < 0xF5) {
|
||||
/* four-byte UTF-8 */
|
||||
charsNeeded = 4;
|
||||
min_uc = 0x10000;
|
||||
uc &= 0x07;
|
||||
} else {
|
||||
return ~0U;
|
||||
}
|
||||
|
||||
if (n < charsNeeded)
|
||||
return ~0U;
|
||||
|
||||
/* first continuation character */
|
||||
b = *(*buffer)++;
|
||||
if ((b & 0xc0) != 0x80)
|
||||
return ~0U;
|
||||
uc <<= 6;
|
||||
uc |= b & 0x3f;
|
||||
|
||||
if (charsNeeded > 2) {
|
||||
/* second continuation character */
|
||||
b = *(*buffer)++;
|
||||
if ((b & 0xc0) != 0x80)
|
||||
return ~0U;
|
||||
uc <<= 6;
|
||||
uc |= b & 0x3f;
|
||||
|
||||
if (charsNeeded > 3) {
|
||||
/* third continuation character */
|
||||
b = *(*buffer)++;
|
||||
if ((b & 0xc0) != 0x80)
|
||||
return ~0U;
|
||||
uc <<= 6;
|
||||
uc |= b & 0x3f;
|
||||
}
|
||||
}
|
||||
|
||||
/* overlong sequence? surrogate pair? out or range? */
|
||||
if (uc < min_uc || uc - 0xd800U < 2048U || uc > 0x10ffff)
|
||||
return ~0U;
|
||||
|
||||
return uc;
|
||||
}
|
||||
|
||||
#endif /* CBOR_UTF8_H */
|
||||
Reference in New Issue
Block a user