3 /******************************************************************************
5 * Copyright (c) 2019 AT&T Intellectual Property.
6 * Copyright (c) 2018-2019 Nokia.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 ******************************************************************************/
22 // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries
25 // Local Includes: Application specific classes, functions, and libraries
26 #include "type_defs.h"
36 ,INCORRECT_LENGTH_BITS
37 ,INCORRECT_LENGTH_BYTES
52 explicit operator bool() const { return SUCCESS == m_error; }
54 void reset() { m_error = SUCCESS; }
55 error_e getError() const { return m_error; }
57 char const* toString() const;
59 void alignmentError(u32 shift);
60 void lengthErrorBytes(u32 bytes_left, u32 requested);
61 void lengthErrorBits(u32 bits_left, u32 requested);
62 void valueError(u32 got, u32 expected, u32 ofs);
63 void tagError(u32 tag);
64 void valueRangeError(size_t value);
65 void sizeRangeError(size_t size, size_t low = 0, size_t high = 0);
66 void allocatorNoMem(size_t bytes_left, size_t bytes_required);
67 void errorUnsupported() {setError(UNSUPPORTED);}
68 void errorUnspecified() {setError(UNSPECIFIED);}
69 void errorNoObject(const char* container_name_);
70 void errorNoMandatory();
71 void errorWrongEndOfContent() {setError(WRONG_END_OF_CONTENT);}
73 //to inline in choice decoder: TODO: think of changing it!
74 void choiceError(char const* name, u32 id)
78 setError(INCORRECT_CHOICE_TAG);
80 //Name of the being processed IE
81 void ie_name(const char* name) { m_ie_name = name; }
82 char const* ie_name() const { return m_ie_name; }
84 void container_name(const char* name) { m_container_name = name; }
85 char const* container_name() const { return m_container_name; }
89 void setError(error_e n) { m_error = n; }
91 enum { MAX_PARAMS = 3 };
94 char const* m_ie_name {""};
95 char const* m_container_name {""};
96 size_t m_param[MAX_PARAMS];
101 void error_context::errorNoObject(const char* c_name)
103 container_name(c_name);
109 void error_context::errorNoMandatory()
112 setError(NO_MANDATORY);
116 void error_context::alignmentError(u32 shift)
124 void error_context::lengthErrorBytes(u32 bytes_left, u32 requested)
127 m_param[0] = bytes_left;
128 m_param[1] = requested;
129 setError(INCORRECT_LENGTH_BYTES);
133 void error_context::lengthErrorBits(u32 bits_left, u32 requested)
136 m_param[0] = bits_left;
137 m_param[1] = requested;
138 setError(INCORRECT_LENGTH_BITS);
142 void error_context::valueError(u32 got, u32 expected, u32 ofs)
146 m_param[1] = expected;
148 setError(INCORRECT_VALUE);
152 void error_context::tagError(u32 tag)
156 setError(INCORRECT_IE_TAG);
160 void error_context::valueRangeError(size_t value)
164 setError(VALUE_OUT_OF_RANGE);
168 void error_context::sizeRangeError(size_t size, size_t low, size_t high)
174 setError(SIZE_OUT_OF_RANGE);
178 void error_context::allocatorNoMem(size_t bytes_left, size_t bytes_required)
181 m_param[0] = bytes_left;
182 m_param[1] = bytes_required;
183 setError(ALLOC_NO_MEMORY);
187 char const* error_context::toString() const
196 case INCORRECT_LENGTH_BITS:
197 snprintf(sz, sizeof(sz)-1, "%zu bits left, '%s::%s' needs %zu", m_param[0], m_container_name, m_name, m_param[1]);
200 case INCORRECT_LENGTH_BYTES:
201 snprintf(sz, sizeof(sz)-1, "%zu bytes left, '%s::%s' needs %zu", m_param[0], m_container_name, m_name, m_param[1]);
204 snprintf(sz, sizeof(sz)-1, "misaligned '%s::%s': shift %zu bits", m_container_name, m_name, m_param[0]);
207 case INCORRECT_VALUE:
208 snprintf(sz, sizeof(sz)-1, "Wrong value of '%s::%s' at %zu: 0x%zX expected=0x%zX", m_container_name, m_name, m_param[2], m_param[0], m_param[1]);
211 case INCORRECT_CHOICE_TAG:
212 snprintf(sz, sizeof(sz)-1, "Wrong choice tag of '%s::%s': %zu (0x%zX)", m_container_name, m_name, m_param[0], m_param[0]);
215 case INCORRECT_IE_TAG:
216 snprintf(sz, sizeof(sz)-1, "Wrong tag of '%s::%s': %zu (0x%zX)", m_container_name, m_name, m_param[0], m_param[0]);
219 case VALUE_OUT_OF_RANGE:
220 snprintf(sz, sizeof(sz) - 1, "Value is out of the range '%s::%s': value: %zu", m_container_name, m_name, m_param[0]);
223 case SIZE_OUT_OF_RANGE:
226 snprintf(sz, sizeof(sz) - 1, "Size=%zu bytes is out of the range [%zu, %zu] in '%s::%s'", m_param[0], m_param[1], m_param[2], m_container_name, m_name);
232 snprintf(sz, sizeof(sz) - 1, "Size=%zu bytes is less than %zu in '%s::%s'", m_param[0], m_param[1], m_container_name, m_name);
236 snprintf(sz, sizeof(sz) - 1, "Size=%zu bytes is out of the range in '%s::%s'", m_param[0], m_container_name, m_name);
241 case ALLOC_NO_MEMORY:
242 snprintf(sz, sizeof(sz) - 1, "Allocator memory has ended '%s::%s': left bytes: %zu; required bytes: %zu", m_container_name, m_name, m_param[0], m_param[1]);
245 snprintf(sz, sizeof(sz) - 1, "Component relation constraint violated '%s::%s'", m_container_name, m_name);
248 snprintf(sz, sizeof(sz) - 1, "no mandatory '%s::%s' is found", m_container_name, m_name);
251 snprintf(sz, sizeof(sz) - 1, "unsupported object '%s::%s' is found", m_container_name, m_name);
252 return "unsupported";
255 snprintf(sz, sizeof(sz) - 1, "unspecified object '%s::%s' is found", m_container_name, m_name);
256 return "unspecified";
258 case WRONG_END_OF_CONTENT:
259 snprintf(sz, sizeof(sz) - 1, "wrong end of content in object '%s::%s'", m_container_name, m_name);
260 return "wrong end of content";
267 } //end: namespace asn