X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=windows%2Fwinmisc.c;h=f2e4f223595b4b73d642df1648223f282583d85a;hb=8fdeb3a95cc3d7dce5629fc22e309eb3c996f44d;hp=e3321b5366f41f25d1abc11733d1541823c1e314;hpb=3b27c3e32b8fe560c5b2bb35781038715abf29c2;p=PuTTY.git diff --git a/windows/winmisc.c b/windows/winmisc.c index e3321b53..f2e4f223 100644 --- a/windows/winmisc.c +++ b/windows/winmisc.c @@ -5,6 +5,9 @@ #include #include #include "putty.h" +#ifndef SECURITY_WIN32 +#define SECURITY_WIN32 +#endif #include OSVERSIONINFO osVersion; @@ -68,7 +71,14 @@ Filename *filename_deserialise(void *vdata, int maxsize, int *used) return filename_from_str(data); } -#ifndef __WINE__ /* No SecureZeroMemory in Winelib 1.4 */ +char filename_char_sanitise(char c) +{ + if (strchr("<>:\"/\\|?*", c)) + return '.'; + return c; +} + +#ifndef NO_SECUREZEROMEMORY /* * Windows implementation of smemclr (see misc.c) using SecureZeroMemory. */ @@ -173,12 +183,72 @@ HMODULE load_system32_dll(const char *libname) return ret; } +/* + * A tree234 containing mappings from system error codes to strings. + */ + +struct errstring { + int error; + char *text; +}; + +static int errstring_find(void *av, void *bv) +{ + int *a = (int *)av; + struct errstring *b = (struct errstring *)bv; + if (*a < b->error) + return -1; + if (*a > b->error) + return +1; + return 0; +} +static int errstring_compare(void *av, void *bv) +{ + struct errstring *a = (struct errstring *)av; + return errstring_find(&a->error, bv); +} + +static tree234 *errstrings = NULL; + +const char *win_strerror(int error) +{ + struct errstring *es; + + if (!errstrings) + errstrings = newtree234(errstring_compare); + + es = find234(errstrings, &error, errstring_find); + + if (!es) { + char msgtext[65536]; /* maximum size for FormatMessage is 64K */ + + es = snew(struct errstring); + es->error = error; + if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + msgtext, lenof(msgtext)-1, NULL)) { + sprintf(msgtext, + "(unable to format: FormatMessage returned %u)", + (unsigned int)GetLastError()); + } else { + int len = strlen(msgtext); + if (len > 0 && msgtext[len-1] == '\n') + msgtext[len-1] = '\0'; + } + es->text = dupprintf("Error %d: %s", error, msgtext); + add234(errstrings, es); + } + + return es->text; +} + #ifdef DEBUG static FILE *debug_fp = NULL; static HANDLE debug_hdl = INVALID_HANDLE_VALUE; static int debug_got_console = 0; -void dputs(char *buf) +void dputs(const char *buf) { DWORD dw;