1) Add FORMAT_MESSAGE_IGNORE_INSERTS to flags for security.
2) Optimize removal of CR/LF terminators
3) Switch to use snprintf()
Windows-specific:
1) Don't waste time converting debug messages to unicode
WinCE-specific:
1) Get rid of TCHAR code
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
enum libusb_log_level level, const char * str)
{
#if defined(USE_SYSTEM_LOGGING_FACILITY)
-#if defined(OS_WINDOWS) || defined(OS_WINCE)
+#if defined(OS_WINDOWS)
+ OutputDebugString(str);
+#elif defined(OS_WINCE)
/* Windows CE only supports the Unicode version of OutputDebugString. */
WCHAR wbuf[USBI_MAX_LOG_LEN];
MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, sizeof(wbuf));
* uses retval as errorcode, or, if 0, use GetLastError()
*/
#if defined(ENABLE_LOGGING)
-static const char *windows_error_str(DWORD retval)
+static const char *windows_error_str(DWORD error_code)
{
static TCHAR wErr_string[ERR_BUFFER_SIZE];
static char err_string[ERR_BUFFER_SIZE];
- DWORD error_code, format_error;
DWORD size;
- size_t i;
+ int len;
- error_code = retval ? retval : GetLastError();
+ if (error_code == 0)
+ error_code = GetLastError();
- safe_stprintf(wErr_string, ERR_BUFFER_SIZE, _T("[%u] "), (unsigned int)error_code);
+ len = sprintf(err_string, "[%u] ", (unsigned int)error_code);
- size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &wErr_string[safe_tcslen(wErr_string)],
- ERR_BUFFER_SIZE - (DWORD)safe_tcslen(wErr_string), NULL);
+ size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ wErr_string, ERR_BUFFER_SIZE, NULL);
if (size == 0) {
- format_error = GetLastError();
+ DWORD format_error = GetLastError();
if (format_error)
- safe_stprintf(wErr_string, ERR_BUFFER_SIZE,
- _T("Windows error code %u (FormatMessage error code %u)"),
+ snprintf(err_string, ERR_BUFFER_SIZE,
+ "Windows error code %u (FormatMessage error code %u)",
(unsigned int)error_code, (unsigned int)format_error);
else
- safe_stprintf(wErr_string, ERR_BUFFER_SIZE, _T("Unknown error code %u"), (unsigned int)error_code);
+ snprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", (unsigned int)error_code);
} else {
- // Remove CR/LF terminators
- for (i = safe_tcslen(wErr_string) - 1; ((wErr_string[i] == 0x0A) || (wErr_string[i] == 0x0D)); i--)
- wErr_string[i] = 0;
- }
+ // Remove CR/LF terminators, if present
+ size_t pos = size - 2;
+ if (wErr_string[pos] == 0x0D)
+ wErr_string[pos] = 0;
- if (WideCharToMultiByte(CP_ACP, 0, wErr_string, -1, err_string, ERR_BUFFER_SIZE, NULL, NULL) < 0)
- strcpy(err_string, "Unable to convert error string");
+ if (!WideCharToMultiByte(CP_ACP, 0, wErr_string, -1, &err_string[len], ERR_BUFFER_SIZE - len, NULL, NULL))
+ strcpy(err_string, "Unable to convert error string");
+ }
return err_string;
}
* uses retval as errorcode, or, if 0, use GetLastError()
*/
#if defined(ENABLE_LOGGING)
-const char *windows_error_str(DWORD retval)
+const char *windows_error_str(DWORD error_code)
{
static char err_string[ERR_BUFFER_SIZE];
- DWORD error_code, format_error;
DWORD size;
- ssize_t i;
+ int len;
- error_code = retval ? retval : GetLastError();
+ if (error_code == 0)
+ error_code = GetLastError();
- safe_sprintf(err_string, ERR_BUFFER_SIZE, "[%u] ", (unsigned int)error_code);
+ len = sprintf(err_string, "[%u] ", (unsigned int)error_code);
// Translate codes returned by SetupAPI. The ones we are dealing with are either
// in 0x0000xxxx or 0xE000xxxx and can be distinguished from standard error codes.
break;
}
- size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[safe_strlen(err_string)],
- ERR_BUFFER_SIZE - (DWORD)safe_strlen(err_string), NULL);
+ size = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ &err_string[len], ERR_BUFFER_SIZE - len, NULL);
if (size == 0) {
- format_error = GetLastError();
+ DWORD format_error = GetLastError();
if (format_error)
- safe_sprintf(err_string, ERR_BUFFER_SIZE,
- "Windows error code %u (FormatMessage error code %u)",
- (unsigned int)error_code, (unsigned int)format_error);
+ snprintf(err_string, ERR_BUFFER_SIZE,
+ "Windows error code %u (FormatMessage error code %u)",
+ (unsigned int)error_code, (unsigned int)format_error);
else
- safe_sprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", (unsigned int)error_code);
- }
- else {
- // Remove CR/LF terminators
- for (i = safe_strlen(err_string) - 1; (i >= 0) && ((err_string[i] == 0x0A) || (err_string[i] == 0x0D)); i--)
- err_string[i] = 0;
+ snprintf(err_string, ERR_BUFFER_SIZE, "Unknown error code %u", (unsigned int)error_code);
+ } else {
+ // Remove CRLF from end of message, if present
+ size_t pos = len + size - 2;
+ if (err_string[pos] == '\r')
+ err_string[pos] = '\0';
}
return err_string;
int windows_handle_events(struct libusb_context *ctx, struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
#if defined(ENABLE_LOGGING)
-const char *windows_error_str(DWORD retval);
+const char *windows_error_str(DWORD error_code);
#endif
-#define LIBUSB_NANO 11173
+#define LIBUSB_NANO 11174