quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

winapi.c (3695B)


      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 /*
     27  * curlx_winapi_strerror:
     28  * Variant of Curl_strerror if the error code is definitely Windows API.
     29  */
     30 #ifdef _WIN32
     31 #include "winapi.h"
     32 
     33 #ifdef BUILDING_LIBCURL
     34 #include <curl/mprintf.h>
     35 #define SNPRINTF curl_msnprintf
     36 #else
     37 /* when built for the test servers */
     38 
     39 /* adjust for old MSVC */
     40 #if defined(_MSC_VER) && (_MSC_VER < 1900)
     41 # define SNPRINTF _snprintf
     42 #else
     43 #define SNPRINTF snprintf
     44 #endif
     45 
     46 #endif /* !BUILDING_LIBCURL */
     47 
     48 #ifdef _WIN32
     49 /* This is a helper function for Curl_strerror that converts Windows API error
     50  * codes (GetLastError) to error messages.
     51  * Returns NULL if no error message was found for error code.
     52  */
     53 const char *curlx_get_winapi_error(int err, char *buf, size_t buflen)
     54 {
     55   char *p;
     56   wchar_t wbuf[256];
     57 
     58   if(!buflen)
     59     return NULL;
     60 
     61   *buf = '\0';
     62   *wbuf = L'\0';
     63 
     64   /* We return the local codepage version of the error string because if it is
     65      output to the user's terminal it will likely be with functions which
     66      expect the local codepage (eg fprintf, failf, infof).
     67      FormatMessageW -> wcstombs is used for Windows CE compatibility. */
     68   if(FormatMessageW((FORMAT_MESSAGE_FROM_SYSTEM |
     69                      FORMAT_MESSAGE_IGNORE_INSERTS), NULL, (DWORD)err,
     70                     LANG_NEUTRAL, wbuf, CURL_ARRAYSIZE(wbuf), NULL)) {
     71     size_t written = wcstombs(buf, wbuf, buflen - 1);
     72     if(written != (size_t)-1)
     73       buf[written] = '\0';
     74     else
     75       *buf = '\0';
     76   }
     77 
     78   /* Truncate multiple lines */
     79   p = strchr(buf, '\n');
     80   if(p) {
     81     if(p > buf && *(p-1) == '\r')
     82       *(p-1) = '\0';
     83     else
     84       *p = '\0';
     85   }
     86 
     87   return *buf ? buf : NULL;
     88 }
     89 #endif /* _WIN32 */
     90 
     91 const char *curlx_winapi_strerror(DWORD err, char *buf, size_t buflen)
     92 {
     93 #ifdef _WIN32
     94   DWORD old_win_err = GetLastError();
     95 #endif
     96   int old_errno = errno;
     97 
     98   if(!buflen)
     99     return NULL;
    100 
    101   *buf = '\0';
    102 
    103 #ifndef CURL_DISABLE_VERBOSE_STRINGS
    104   if(!curlx_get_winapi_error((int)err, buf, buflen)) {
    105 #if defined(__GNUC__) && __GNUC__ >= 7
    106 #pragma GCC diagnostic push
    107 #pragma GCC diagnostic warning "-Wformat-truncation=1"
    108 #endif
    109     /* some GCC compilers cause false positive warnings if we allow this
    110        warning */
    111     SNPRINTF(buf, buflen, "Unknown error %lu (0x%08lX)", err, err);
    112 #if defined(__GNUC__) && __GNUC__ >= 7
    113 #pragma GCC diagnostic pop
    114 #endif
    115 
    116   }
    117 #else
    118   {
    119     const char *txt = (err == ERROR_SUCCESS) ? "No error" : "Error";
    120     if(strlen(txt) < buflen)
    121       strcpy(buf, txt);
    122   }
    123 #endif
    124 
    125   if(errno != old_errno)
    126     CURL_SETERRNO(old_errno);
    127 
    128 #ifdef _WIN32
    129   if(old_win_err != GetLastError())
    130     SetLastError(old_win_err);
    131 #endif
    132 
    133   return buf;
    134 }
    135 #endif /* _WIN32 */