Apply Upstream code (2021-03-15)
[platform/upstream/connectedhomeip.git] / src / lib / asn1 / ASN1Error.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2019 Google LLC.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *      This file contains functions for working with ASN1 errors.
23  */
24
25 #include <stdlib.h>
26
27 #include <asn1/ASN1.h>
28 #include <support/ErrorStr.h>
29
30 namespace chip {
31 namespace ASN1 {
32
33 /**
34  * Given an ASN1 error, returns a human-readable NULL-terminated C string
35  * describing the error.
36  *
37  * @param[in] buf                   Buffer into which the error string will be placed.
38  * @param[in] bufSize               Size of the supplied buffer in bytes.
39  * @param[in] err                   The error to be described.
40  *
41  * @return true                     If a description string was written into the supplied buffer.
42  * @return false                    If the supplied error was not an ASN1 error.
43  *
44  */
45 bool FormatASN1Error(char * buf, uint16_t bufSize, int32_t err)
46 {
47     const char * desc = nullptr;
48
49     if (err < ASN1_ERROR_MIN || err > ASN1_ERROR_MAX)
50     {
51         return false;
52     }
53
54 #if !CHIP_CONFIG_SHORT_ERROR_STR
55     switch (err)
56     {
57     case ASN1_END:
58         desc = "End of input";
59         break;
60     case ASN1_ERROR_UNDERRUN:
61         desc = "Reader underrun";
62         break;
63     case ASN1_ERROR_OVERFLOW:
64         desc = "Writer overflow";
65         break;
66     case ASN1_ERROR_INVALID_STATE:
67         desc = "Invalid state";
68         break;
69     case ASN1_ERROR_MAX_DEPTH_EXCEEDED:
70         desc = "Max depth exceeded";
71         break;
72     case ASN1_ERROR_INVALID_ENCODING:
73         desc = "Invalid encoding";
74         break;
75     case ASN1_ERROR_UNSUPPORTED_ENCODING:
76         desc = "Unsupported encoding";
77         break;
78     case ASN1_ERROR_TAG_OVERFLOW:
79         desc = "Tag overflow";
80         break;
81     case ASN1_ERROR_LENGTH_OVERFLOW:
82         desc = "Length overflow";
83         break;
84     case ASN1_ERROR_VALUE_OVERFLOW:
85         desc = "Value overflow";
86         break;
87     case ASN1_ERROR_UNKNOWN_OBJECT_ID:
88         desc = "Unknown object id";
89         break;
90     }
91 #endif // !CHIP_CONFIG_SHORT_ERROR_STR
92
93     FormatError(buf, bufSize, "ASN1", err, desc);
94
95     return true;
96 }
97
98 } // namespace ASN1
99 } // namespace chip