1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Intel Corporation
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
14 **
15 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 ** THE SOFTWARE.
22 **
23 ****************************************************************************/
24
25 #include "tinycbor/cbor.h"
26
27 #ifndef _
28 # define _(msg) msg
29 #endif
30
31 /**
32 * \enum CborError
33 * \ingroup CborGlobals
34 * The CborError enum contains the possible error values used by the CBOR encoder and decoder.
35 *
36 * TinyCBOR functions report success by returning CborNoError, or one error
37 * condition by returning one of the values below. One exception is the
38 * out-of-memory condition (CborErrorOutOfMemory), which the functions for \ref
39 * CborEncoding may report in bit-wise OR with other conditions.
40 *
41 * This technique allows code to determine whether the only error condition was
42 * a lack of buffer space, which may not be a fatal condition if the buffer can
43 * be resized. Additionally, the functions for \ref CborEncoding may continue
44 * to be used even after CborErrorOutOfMemory is returned, and instead they
45 * will simply calculate the extra space needed.
46 *
47 * \value CborNoError No error occurred
48 * \omitvalue CborUnknownError
49 * \value CborErrorUnknownLength Request for the length of an array, map or string whose length is not provided in the CBOR stream
50 * \value CborErrorAdvancePastEOF Not enough data in the stream to decode item (decoding would advance past end of stream)
51 * \value CborErrorIO An I/O error occurred, probably due to an out-of-memory situation
52 * \value CborErrorGarbageAtEnd Bytes exist past the end of the CBOR stream
53 * \value CborErrorUnexpectedEOF End of stream reached unexpectedly
54 * \value CborErrorUnexpectedBreak A CBOR break byte was found where not expected
55 * \value CborErrorUnknownType An unknown type (future extension to CBOR) was found in the stream
56 * \value CborErrorIllegalType An invalid type was found while parsing a chunked CBOR string
57 * \value CborErrorIllegalNumber An illegal initial byte (encoding unspecified additional information) was found
58 * \value CborErrorIllegalSimpleType An illegal encoding of a CBOR Simple Type of value less than 32 was found
59 * \omitvalue CborErrorUnknownSimpleType
60 * \omitvalue CborErrorUnknownTag
61 * \omitvalue CborErrorInappropriateTagForType
62 * \omitvalue CborErrorDuplicateObjectKeys
63 * \value CborErrorInvalidUtf8TextString Illegal UTF-8 encoding found while parsing CBOR Text String
64 * \value CborErrorTooManyItems Too many items were added to CBOR map or array of pre-determined length
65 * \value CborErrorTooFewItems Too few items were added to CBOR map or array of pre-determeined length
66 * \value CborErrorDataTooLarge Data item size exceeds TinyCBOR's implementation limits
67 * \value CborErrorNestingTooDeep Data item nesting exceeds TinyCBOR's implementation limits
68 * \omitvalue CborErrorUnsupportedType
69 * \value CborErrorJsonObjectKeyIsAggregate Conversion to JSON failed because the key in a map is a CBOR map or array
70 * \value CborErrorJsonObjectKeyNotString Conversion to JSON failed because the key in a map is not a text string
71 * \value CborErrorOutOfMemory During CBOR encoding, the buffer provided is insufficient for encoding the data item;
72 * in other situations, TinyCBOR failed to allocate memory
73 * \value CborErrorInternalError An internal error occurred in TinyCBOR
74 */
75
76 /**
77 * \ingroup CborGlobals
78 * Returns the error string corresponding to the CBOR error condition \a error.
79 */
cbor_error_string(CborError error)80 const char *cbor_error_string(CborError error)
81 {
82 switch (error) {
83 case CborNoError:
84 return "";
85
86 case CborUnknownError:
87 return _("unknown error");
88
89 case CborErrorOutOfMemory:
90 return _("out of memory/need more memory");
91
92 case CborErrorUnknownLength:
93 return _("unknown length (attempted to get the length of a map/array/string of indeterminate length");
94
95 case CborErrorAdvancePastEOF:
96 return _("attempted to advance past EOF");
97
98 case CborErrorIO:
99 return _("I/O error");
100
101 case CborErrorGarbageAtEnd:
102 return _("garbage after the end of the content");
103
104 case CborErrorUnexpectedEOF:
105 return _("unexpected end of data");
106
107 case CborErrorUnexpectedBreak:
108 return _("unexpected 'break' byte");
109
110 case CborErrorUnknownType:
111 return _("illegal byte (encodes future extension type)");
112
113 case CborErrorIllegalType:
114 return _("mismatched string type in chunked string");
115
116 case CborErrorIllegalNumber:
117 return _("illegal initial byte (encodes unspecified additional information)");
118
119 case CborErrorIllegalSimpleType:
120 return _("illegal encoding of simple type smaller than 32");
121
122 case CborErrorUnknownSimpleType:
123 return _("unknown simple type");
124
125 case CborErrorUnknownTag:
126 return _("unknown tag");
127
128 case CborErrorInappropriateTagForType:
129 return _("inappropriate tag for type");
130
131 case CborErrorDuplicateObjectKeys:
132 return _("duplicate keys in object");
133
134 case CborErrorInvalidUtf8TextString:
135 return _("invalid UTF-8 content in string");
136
137 case CborErrorTooManyItems:
138 return _("too many items added to encoder");
139
140 case CborErrorTooFewItems:
141 return _("too few items added to encoder");
142
143 case CborErrorDataTooLarge:
144 return _("internal error: data too large");
145
146 case CborErrorNestingTooDeep:
147 return _("internal error: too many nested containers found in recursive function");
148
149 case CborErrorUnsupportedType:
150 return _("unsupported type");
151
152 case CborErrorJsonObjectKeyIsAggregate:
153 return _("conversion to JSON failed: key in object is an array or map");
154
155 case CborErrorJsonObjectKeyNotString:
156 return _("conversion to JSON failed: key in object is not a string");
157
158 case CborErrorJsonNotImplemented:
159 return _("conversion to JSON failed: open_memstream unavailable");
160
161 case CborErrorInternalError:
162 return _("internal error");
163 }
164 return cbor_error_string(CborUnknownError);
165 }
166