winapi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "../curl_setup.h"
  25. /*
  26. * curlx_winapi_strerror:
  27. * Variant of curlx_strerror if the error code is definitely Windows API.
  28. */
  29. #ifdef _WIN32
  30. #include "winapi.h"
  31. #ifndef WITHOUT_LIBCURL
  32. #include <curl/mprintf.h>
  33. #define SNPRINTF curl_msnprintf
  34. #else
  35. /* when built for the test servers */
  36. /* adjust for old MSVC */
  37. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  38. #define SNPRINTF _snprintf
  39. #else
  40. #define SNPRINTF snprintf
  41. #endif
  42. #endif /* !WITHOUT_LIBCURL */
  43. /* This is a helper function for curlx_strerror that converts Windows API error
  44. * codes (GetLastError) to error messages.
  45. * Returns NULL if no error message was found for error code.
  46. */
  47. const char *curlx_get_winapi_error(DWORD err, char *buf, size_t buflen)
  48. {
  49. char *p;
  50. wchar_t wbuf[256];
  51. if(!buflen)
  52. return NULL;
  53. *buf = '\0';
  54. *wbuf = L'\0';
  55. /* We return the local codepage version of the error string because if it is
  56. output to the user's terminal it will likely be with functions which
  57. expect the local codepage (eg fprintf, failf, infof).
  58. FormatMessageW -> wcstombs is used for Windows CE compatibility. */
  59. if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
  60. FORMAT_MESSAGE_IGNORE_INSERTS), NULL, err,
  61. LANG_NEUTRAL, wbuf, CURL_ARRAYSIZE(wbuf), NULL)) {
  62. size_t written = wcstombs(buf, wbuf, buflen - 1);
  63. if(written != (size_t)-1)
  64. buf[written] = '\0';
  65. else
  66. *buf = '\0';
  67. }
  68. /* Truncate multiple lines */
  69. p = strchr(buf, '\n');
  70. if(p) {
  71. if(p > buf && *(p-1) == '\r')
  72. *(p-1) = '\0';
  73. else
  74. *p = '\0';
  75. }
  76. return *buf ? buf : NULL;
  77. }
  78. const char *curlx_winapi_strerror(DWORD err, char *buf, size_t buflen)
  79. {
  80. DWORD old_win_err = GetLastError();
  81. int old_errno = errno;
  82. if(!buflen)
  83. return NULL;
  84. *buf = '\0';
  85. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  86. if(!curlx_get_winapi_error(err, buf, buflen)) {
  87. #if defined(__GNUC__) && __GNUC__ >= 7
  88. #pragma GCC diagnostic push
  89. #pragma GCC diagnostic warning "-Wformat-truncation=1"
  90. #endif
  91. /* some GCC compilers cause false positive warnings if we allow this
  92. warning */
  93. SNPRINTF(buf, buflen, "Unknown error %lu (0x%08lx)", err, err);
  94. #if defined(__GNUC__) && __GNUC__ >= 7
  95. #pragma GCC diagnostic pop
  96. #endif
  97. }
  98. #else
  99. {
  100. const char *txt = (err == ERROR_SUCCESS) ? "No error" : "Error";
  101. if(strlen(txt) < buflen)
  102. strcpy(buf, txt);
  103. }
  104. #endif
  105. if(errno != old_errno)
  106. CURL_SETERRNO(old_errno);
  107. if(old_win_err != GetLastError())
  108. SetLastError(old_win_err);
  109. return buf;
  110. }
  111. #endif /* _WIN32 */