xref: /optee_os/core/lib/qcbor/src/qcbor_err_to_str.c (revision b586599be35c4311337a5d8db5f4b5e5c81a754d)
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* ==========================================================================
3  * err_to_str.c -- strings names for errors
4  *
5  * Copyright (c) 2020, Patrick Uiterwijk. All rights reserved.
6  * Copyright (c) 2020,2024, Laurence Lundblade.
7  * Copyright (c) 2021, Arm Limited. All rights reserved.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  *
11  * See BSD-3-Clause license in README.md
12  *
13  * Created on 3/21/20
14  * ========================================================================== */
15 
16 #include "qcbor/qcbor_common.h"
17 #include <string.h>
18 
19 #define ERR_TO_STR_CASE(errpart) case errpart: return #errpart;
20 
21 
22 const char *
qcbor_err_to_str(const QCBORError uErr)23 qcbor_err_to_str(const QCBORError uErr) {
24    switch (uErr) {
25    ERR_TO_STR_CASE(QCBOR_SUCCESS)
26    ERR_TO_STR_CASE(QCBOR_ERR_BUFFER_TOO_SMALL)
27    ERR_TO_STR_CASE(QCBOR_ERR_ENCODE_UNSUPPORTED)
28    ERR_TO_STR_CASE(QCBOR_ERR_BUFFER_TOO_LARGE)
29    ERR_TO_STR_CASE(QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
30    ERR_TO_STR_CASE(QCBOR_ERR_CLOSE_MISMATCH)
31    ERR_TO_STR_CASE(QCBOR_ERR_ARRAY_TOO_LONG)
32    ERR_TO_STR_CASE(QCBOR_ERR_TOO_MANY_CLOSES)
33    ERR_TO_STR_CASE(QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN)
34    ERR_TO_STR_CASE(QCBOR_ERR_OPEN_BYTE_STRING)
35    ERR_TO_STR_CASE(QCBOR_ERR_CANNOT_CANCEL)
36    ERR_TO_STR_CASE(QCBOR_ERR_BAD_TYPE_7)
37    ERR_TO_STR_CASE(QCBOR_ERR_EXTRA_BYTES)
38    ERR_TO_STR_CASE(QCBOR_ERR_UNSUPPORTED)
39    ERR_TO_STR_CASE(QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED)
40    ERR_TO_STR_CASE(QCBOR_ERR_BAD_INT)
41    ERR_TO_STR_CASE(QCBOR_ERR_INDEFINITE_STRING_CHUNK)
42    ERR_TO_STR_CASE(QCBOR_ERR_HIT_END)
43    ERR_TO_STR_CASE(QCBOR_ERR_BAD_BREAK)
44    ERR_TO_STR_CASE(QCBOR_ERR_INPUT_TOO_LARGE)
45    ERR_TO_STR_CASE(QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
46    ERR_TO_STR_CASE(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)
47    ERR_TO_STR_CASE(QCBOR_ERR_STRING_TOO_LONG)
48    ERR_TO_STR_CASE(QCBOR_ERR_BAD_EXP_AND_MANTISSA)
49    ERR_TO_STR_CASE(QCBOR_ERR_NO_STRING_ALLOCATOR)
50    ERR_TO_STR_CASE(QCBOR_ERR_STRING_ALLOCATE)
51    ERR_TO_STR_CASE(QCBOR_ERR_MAP_LABEL_TYPE)
52    ERR_TO_STR_CASE(QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT)
53    ERR_TO_STR_CASE(QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED)
54    ERR_TO_STR_CASE(QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED)
55    ERR_TO_STR_CASE(QCBOR_ERR_TAGS_DISABLED)
56    ERR_TO_STR_CASE(QCBOR_ERR_TOO_MANY_TAGS)
57    ERR_TO_STR_CASE(QCBOR_ERR_UNEXPECTED_TYPE)
58    ERR_TO_STR_CASE(QCBOR_ERR_DUPLICATE_LABEL)
59    ERR_TO_STR_CASE(QCBOR_ERR_MEM_POOL_SIZE)
60    ERR_TO_STR_CASE(QCBOR_ERR_INT_OVERFLOW)
61    ERR_TO_STR_CASE(QCBOR_ERR_DATE_OVERFLOW)
62    ERR_TO_STR_CASE(QCBOR_ERR_EXIT_MISMATCH)
63    ERR_TO_STR_CASE(QCBOR_ERR_NO_MORE_ITEMS)
64    ERR_TO_STR_CASE(QCBOR_ERR_LABEL_NOT_FOUND)
65    ERR_TO_STR_CASE(QCBOR_ERR_NUMBER_SIGN_CONVERSION)
66    ERR_TO_STR_CASE(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW)
67    ERR_TO_STR_CASE(QCBOR_ERR_MAP_NOT_ENTERED)
68    ERR_TO_STR_CASE(QCBOR_ERR_CALLBACK_FAIL)
69    ERR_TO_STR_CASE(QCBOR_ERR_FLOAT_DATE_DISABLED)
70    ERR_TO_STR_CASE(QCBOR_ERR_HALF_PRECISION_DISABLED)
71    ERR_TO_STR_CASE(QCBOR_ERR_HW_FLOAT_DISABLED)
72    ERR_TO_STR_CASE(QCBOR_ERR_FLOAT_EXCEPTION)
73    ERR_TO_STR_CASE(QCBOR_ERR_ALL_FLOAT_DISABLED)
74    ERR_TO_STR_CASE(QCBOR_ERR_RECOVERABLE_BAD_TAG_CONTENT)
75    ERR_TO_STR_CASE(QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING)
76 
77    default:
78       if(uErr >= QCBOR_ERR_FIRST_USER_DEFINED && uErr <= QCBOR_ERR_LAST_USER_DEFINED) {
79          /* Static buffer is not thread safe, but this is only a diagnostic */
80          static char buf[20];
81          strcpy(buf, "USER_DEFINED_");
82          size_t uEndOffset = strlen(buf);
83          buf[uEndOffset]   = (char)(uErr/100 + '0');
84          buf[uEndOffset+1] = (char)(((uErr/10) % 10) + '0');
85          buf[uEndOffset+2] = (char)((uErr % 10 )+ '0');
86          buf[uEndOffset+3] = '\0';
87          return buf;
88 
89       } else {
90          return "Unidentified QCBOR error";
91       }
92    }
93 }
94