diff options
Diffstat (limited to 'ckcapi.c')
-rw-r--r-- | ckcapi.c | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -160,6 +160,55 @@ ckcapi_return_data(CK_VOID_PTR dst, CK_ULONG_PTR dlen, return CKR_OK; } +CK_RV +ckcapi_return_string(CK_VOID_PTR dst, CK_ULONG_PTR dlen, + WCHAR* string) +{ + DWORD error; + int result; + + SetLastError(0); + + /* + * Sadly WideCharToMultiByte doesn't handle zero + * length strings properly. So we have to special + * case this part. + */ + if(!string[0]) + return ckcapi_return_data(dst, dlen, "\0", 1); + + result = WideCharToMultiByte(CP_UTF8, 0, string, -1, + dst, dst ? *dlen : 0, NULL, NULL); + + + /* An error result somehow */ + if(!result) + { + error = GetLastError(); + switch(error) + { + /* If buffer was too short, calculate good buffer length */ + case ERROR_INSUFFICIENT_BUFFER: + result = WideCharToMultiByte(CP_UTF8, 0, string, -1, + NULL, 0, NULL, NULL); + if(!result) + return CKR_GENERAL_ERROR; + *dlen = result; + return CKR_BUFFER_TOO_SMALL; + + /* A strange zero length success */ + case ERROR_SUCCESS: + break; + + /* All other errors are not handleable */ + default: + return CKR_GENERAL_ERROR; + } + } + + *dlen = result; + return CKR_OK; +} /* ---------------------------------------------------------------- */ |