From 2fe34b25634c4739cf38ee13e28d69e773f55681 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sun, 29 Apr 2007 00:21:46 +0000 Subject: More fixes for cryptoki-log. --- module/cryptoki-log.c | 91 +++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/module/cryptoki-log.c b/module/cryptoki-log.c index 84e4b7a..7084ae8 100644 --- a/module/cryptoki-log.c +++ b/module/cryptoki-log.c @@ -92,17 +92,45 @@ static pthread_mutex_t global_mutex = PTHREAD_MUTEX_INITIALIZER; static CK_FUNCTION_LIST_PTR module_list = NULL; static FILE* output_file = NULL; +#define LINE_BUF_LEN 4096 +#define PREFIX "PKCS11: " +#define PREFIX_LEN (sizeof(PREFIX) - 1) + +static char win32_line_buf_full[LINE_BUF_LEN + PREFIX_LEN] = { 0, }; +static char* win32_line_buf = win32_line_buf_full + PREFIX_LEN; + +static void +cklog_line(const char* line) +{ + if (output_file && output_file != stderr) { + fputs (line, output_file); + } else { #ifdef _WIN32 + OutputDebugStringA(line); +#else + fputs (line, stderr); +#endif + } -static char win32_line_buf[4096] = { 0, }; + if (output_file) + fflush (output_file); +} static void -cklog_win32(const char* msg, va_list va) +cklog(const char* msg, ...) { + va_list va; size_t l; char* p; char ch; + va_start (va, msg); + + /* Fill in the prefix if necessary */ + if(!win32_line_buf_full[0]) + memcpy(win32_line_buf_full, PREFIX, PREFIX_LEN); + + /* Length of data already present */ l = strlen(win32_line_buf); /* Line is just too long? */ @@ -110,46 +138,21 @@ cklog_win32(const char* msg, va_list va) l = 0; /* Print into the buffer */ - _vsnprintf(win32_line_buf + l, sizeof(win32_line_buf) - l, msg, va); - win32_line_buf[sizeof(win32_line_buf) - 1] = 0; + _vsnprintf(win32_line_buf + l, LINE_BUF_LEN - l, msg, va); + win32_line_buf[LINE_BUF_LEN - 1] = 0; + + va_end (va); /* Now send out all lines from the buffer */ while((p = strchr(win32_line_buf, '\n')) != NULL) { ch = p[1]; p[1] = 0; - OutputDebugStringA(win32_line_buf); + cklog_line(win32_line_buf_full); p[1] = ch; - memmove(win32_line_buf, p + 1, sizeof(win32_line_buf) - ((p + 1) - win32_line_buf)); + memmove(win32_line_buf, p + 1, LINE_BUF_LEN - ((p + 1) - win32_line_buf)); } } -#endif - -static void -cklog (const char* msg, ...) -{ - va_list va; - va_start (va, msg); - - if(output_file && output_file != stderr) - { - vfprintf (output_file, msg, va); - fflush (output_file); - } - else - { -#ifdef _WIN32 - cklog_win32 (msg, va); -#else - vfprintf (stderr, msg, va); - fflush (stderr); -#endif - } - - va_end (va); -} - - #define LOG_FLAG(flags, had, flag) \ if ((flags & flag) == flag) { \ cklog ("%s%s", (had) ? "|" : " = ", #flag); \ @@ -190,7 +193,7 @@ log_some_bytes (CK_BYTE_PTR arr, CK_ULONG num) } else if (ch == '\r') { p[0] = '\\'; p[1] = 'r'; ++p; - } else if (ch >= 32 && ch <= 127) { + } else if (ch >= 32 && ch < 127) { *p = ch; } else { p[0] = '\\'; @@ -247,6 +250,16 @@ log_attribute_array (const char *pref, const char *name, CK_ATTRIBUTE_PTR arr, cklog ("(-1) INVALID"); } else if (arr[i].pValue == NULL) { cklog ("(%d) NULL", arr[i].ulValueLen); + + /* A pretty good guess that 4 == CK_ULONG */ + } else if (arr[i].ulValueLen == 4) { + cklog ("(%d) 0x%08X", arr[i].ulValueLen, *((CK_ULONG_PTR)(arr[i].pValue))); + + /* A pretty good guess that 1 = CK_BBOOL */ + } else if (arr[i].ulValueLen == 1) { + cklog ("(%d) 0x%02X", arr[i].ulValueLen, (CK_ULONG)(*((CK_BBOOL*)(arr[i].pValue)))); + + /* Any other length */ } else { cklog ("(%d) ", arr[i].ulValueLen); log_some_bytes (arr[i].pValue, arr[i].ulValueLen); @@ -775,10 +788,10 @@ CL_C_Initialize (CK_VOID_PTR pInitArgs) } cklog ("INITIALIZE cryptoki-log debugging (%s / %s)\n", - module, output ? output : "stderr"); + module, output ? output : "ods"); #else cklog ("INITIALIZE cryptoki-log debugging (%s / %s)\n", - module, output ? output : "ods"); + module, output ? output : "stderr"); #endif @@ -1647,12 +1660,6 @@ static struct CK_FUNCTION_LIST functionList = { CL_C_WaitForSlotEvent }; -/* #ifdef MSVC -__declspec(dllexport) CK_RV -#if 0 -CK_RV -#endif -*/ __declspec(dllexport) CK_RV C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) { -- cgit v1.2.3