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