error.fmt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Error message information
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * 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, WITHOUT
  15. * 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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_ERROR_C) || defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  27. #include "mbedtls/error.h"
  28. #include <string.h>
  29. #endif
  30. #if defined(MBEDTLS_PLATFORM_C)
  31. #include "mbedtls/platform.h"
  32. #else
  33. #define mbedtls_snprintf snprintf
  34. #define mbedtls_time_t time_t
  35. #endif
  36. #if defined(MBEDTLS_ERROR_C)
  37. #include <stdio.h>
  38. HEADER_INCLUDED
  39. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  40. {
  41. size_t len;
  42. int use_ret;
  43. if( buflen == 0 )
  44. return;
  45. memset( buf, 0x00, buflen );
  46. if( ret < 0 )
  47. ret = -ret;
  48. if( ret & 0xFF80 )
  49. {
  50. use_ret = ret & 0xFF80;
  51. // High level error codes
  52. //
  53. // BEGIN generated code
  54. HIGH_LEVEL_CODE_CHECKS
  55. // END generated code
  56. if( strlen( buf ) == 0 )
  57. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  58. }
  59. use_ret = ret & ~0xFF80;
  60. if( use_ret == 0 )
  61. return;
  62. // If high level code is present, make a concatenation between both
  63. // error strings.
  64. //
  65. len = strlen( buf );
  66. if( len > 0 )
  67. {
  68. if( buflen - len < 5 )
  69. return;
  70. mbedtls_snprintf( buf + len, buflen - len, " : " );
  71. buf += len + 3;
  72. buflen -= len + 3;
  73. }
  74. // Low level error codes
  75. //
  76. // BEGIN generated code
  77. LOW_LEVEL_CODE_CHECKS
  78. // END generated code
  79. if( strlen( buf ) != 0 )
  80. return;
  81. mbedtls_snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
  82. }
  83. #else /* MBEDTLS_ERROR_C */
  84. #if defined(MBEDTLS_ERROR_STRERROR_DUMMY)
  85. /*
  86. * Provide an non-function in case MBEDTLS_ERROR_C is not defined
  87. */
  88. void mbedtls_strerror( int ret, char *buf, size_t buflen )
  89. {
  90. ((void) ret);
  91. if( buflen > 0 )
  92. buf[0] = '\0';
  93. }
  94. #endif /* MBEDTLS_ERROR_STRERROR_DUMMY */
  95. #endif /* MBEDTLS_ERROR_C */