error.fmt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Error message information
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  52. #include "mbedtls/error.h"
  53. #if defined(MBEDTLS_ERROR_C)
  54. #if defined(MBEDTLS_PLATFORM_C)
  55. #include "mbedtls/platform.h"
  56. #else
  57. #define mbedtls_snprintf snprintf
  58. #endif
  59. #include <stdio.h>
  60. #include <string.h>
  61. HEADER_INCLUDED
  62. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  63. {
  64. size_t len;
  65. int use_ret;
  66. if( buflen == 0 )
  67. return;
  68. memset( buf, 0x00, buflen );
  69. if( ret < 0 )
  70. ret = -ret;
  71. if( ret & 0xFF80 )
  72. {
  73. use_ret = ret & 0xFF80;
  74. // High level error codes
  75. //
  76. // BEGIN generated code
  77. HIGH_LEVEL_CODE_CHECKS
  78. // END generated code
  79. if( strlen( buf ) == 0 )
  80. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  81. }
  82. use_ret = ret & ~0xFF80;
  83. if( use_ret == 0 )
  84. return;
  85. // If high level code is present, make a concatenation between both
  86. // error strings.
  87. //
  88. len = strlen( buf );
  89. if( len > 0 )
  90. {
  91. if( buflen - len < 5 )
  92. return;
  93. mbedtls_snprintf( buf + len, buflen - len, " : " );
  94. buf += len + 3;
  95. buflen -= len + 3;
  96. }
  97. // Low level error codes
  98. //
  99. // BEGIN generated code
  100. LOW_LEVEL_CODE_CHECKS
  101. // END generated code
  102. if( strlen( buf ) != 0 )
  103. return;
  104. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  105. }
  106. #else /* MBEDTLS_ERROR_C */
  107. /*
  108. * Provide an non-function in case MBEDTLS_ERROR_C is not defined
  109. */
  110. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  111. {
  112. ((void) ret);
  113. if( buflen > 0 )
  114. buf[0] = '\0';
  115. }
  116. #endif /* MBEDTLS_ERROR_C */
  117. #endif /* MBEDTLS_ERROR_C || MBEDTLS_ERROR_STRERROR_DUMMY */