Kaydet (Commit) 94cdcaa4 authored tarafından Tor Lillqvist's avatar Tor Lillqvist

Add a globally usable WindowsErrorString function

Is comphelper the right place for this? Is having it as "inline" the right
way?

Change-Id: I973dbde108f89b6cab17e5d88db2390d6f18a672
üst 54f10a96
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef INCLUDED_COMPHELPER_WINDOWSERRORSTRING_HXX
#define INCLUDED_COMPHELPER_WINDOWSERRORSTRING_HXX
#include <prewin.h>
#include <postwin.h>
#include <rtl/ustring.hxx>
namespace {
inline OUString WindowsErrorString(DWORD nErrorCode)
{
LPWSTR pMsgBuf;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&pMsgBuf,
0,
NULL) == 0)
return OUString::number(nErrorCode, 16);
if (pMsgBuf[wcslen(pMsgBuf)-1] == '\n')
pMsgBuf[wcslen(pMsgBuf)-1] = '\0';
OUString result(pMsgBuf);
LocalFree(pMsgBuf);
return result;
}
} // anonymous namespace
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
...@@ -92,6 +92,7 @@ ...@@ -92,6 +92,7 @@
#include <prewin.h> #include <prewin.h>
#include <wincrypt.h> #include <wincrypt.h>
#include <postwin.h> #include <postwin.h>
#include <comphelper/windowserrorstring.hxx>
#endif #endif
#include <config_eot.h> #include <config_eot.h>
...@@ -6761,33 +6762,6 @@ typedef BOOL (WINAPI *PointerTo_CryptRetrieveTimeStamp)(LPCWSTR wszUrl, ...@@ -6761,33 +6762,6 @@ typedef BOOL (WINAPI *PointerTo_CryptRetrieveTimeStamp)(LPCWSTR wszUrl,
PCCERT_CONTEXT *ppTsSigner, PCCERT_CONTEXT *ppTsSigner,
HCERTSTORE phStore); HCERTSTORE phStore);
namespace {
OUString WindowsError(DWORD nErrorCode)
{
LPWSTR pMsgBuf;
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
nErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&pMsgBuf,
0,
NULL) == 0)
return OUString::number(nErrorCode, 16);
if (pMsgBuf[wcslen(pMsgBuf)-1] == '\n')
pMsgBuf[wcslen(pMsgBuf)-1] = '\0';
OUString result(pMsgBuf);
LocalFree(pMsgBuf);
return result;
}
}
#endif #endif
bool PDFWriterImpl::finalizeSignature() bool PDFWriterImpl::finalizeSignature()
...@@ -7286,7 +7260,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7286,7 +7260,7 @@ bool PDFWriterImpl::finalizeSignature()
PCCERT_CONTEXT pCertContext = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, reinterpret_cast<const BYTE*>(n_derArray), n_derLength); PCCERT_CONTEXT pCertContext = CertCreateCertificateContext(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, reinterpret_cast<const BYTE*>(n_derArray), n_derLength);
if (pCertContext == NULL) if (pCertContext == NULL)
{ {
SAL_WARN("vcl.pdfwriter", "CertCreateCertificateContext failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CertCreateCertificateContext failed: " << WindowsErrorString(GetLastError()));
return false; return false;
} }
...@@ -7312,7 +7286,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7312,7 +7286,7 @@ bool PDFWriterImpl::finalizeSignature()
&nKeySpec, &nKeySpec,
&bFreeNeeded)) &bFreeNeeded))
{ {
SAL_WARN("vcl.pdfwriter", "CryptAcquireCertificatePrivateKey failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptAcquireCertificatePrivateKey failed: " << WindowsErrorString(GetLastError()));
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
return false; return false;
} }
...@@ -7350,7 +7324,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7350,7 +7324,7 @@ bool PDFWriterImpl::finalizeSignature()
NULL, NULL,
NULL))) NULL)))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgOpenToEncode failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgOpenToEncode failed: " << WindowsErrorString(GetLastError()));
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
return false; return false;
} }
...@@ -7358,7 +7332,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7358,7 +7332,7 @@ bool PDFWriterImpl::finalizeSignature()
if (!CryptMsgUpdate(hMsg, (const BYTE *)buffer1.get(), bytesRead1, FALSE) || if (!CryptMsgUpdate(hMsg, (const BYTE *)buffer1.get(), bytesRead1, FALSE) ||
!CryptMsgUpdate(hMsg, (const BYTE *)buffer2.get(), bytesRead2, TRUE)) !CryptMsgUpdate(hMsg, (const BYTE *)buffer2.get(), bytesRead2, TRUE))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgUpdate failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgUpdate failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
return false; return false;
...@@ -7371,7 +7345,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7371,7 +7345,7 @@ bool PDFWriterImpl::finalizeSignature()
PointerTo_CryptRetrieveTimeStamp crts = (PointerTo_CryptRetrieveTimeStamp) GetProcAddress(LoadLibrary("crypt32.dll"), "CryptRetrieveTimeStamp"); PointerTo_CryptRetrieveTimeStamp crts = (PointerTo_CryptRetrieveTimeStamp) GetProcAddress(LoadLibrary("crypt32.dll"), "CryptRetrieveTimeStamp");
if (!crts) if (!crts)
{ {
SAL_WARN("vcl.pdfwriter", "Could not find the CryptRetrieveTimeStamp function in crypt32.dll: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "Could not find the CryptRetrieveTimeStamp function in crypt32.dll: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
return false; return false;
...@@ -7385,7 +7359,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7385,7 +7359,7 @@ bool PDFWriterImpl::finalizeSignature()
NULL, NULL,
NULL))) NULL)))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgOpenToDecode failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgOpenToDecode failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
return false; return false;
...@@ -7395,7 +7369,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7395,7 +7369,7 @@ bool PDFWriterImpl::finalizeSignature()
if (!CryptMsgGetParam(hMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL, &nTsSigLen)) if (!CryptMsgGetParam(hMsg, CMSG_BARE_CONTENT_PARAM, 0, NULL, &nTsSigLen))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_BARE_CONTENT_PARAM) failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_BARE_CONTENT_PARAM) failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hDecodedMsg); CryptMsgClose(hDecodedMsg);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
...@@ -7408,7 +7382,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7408,7 +7382,7 @@ bool PDFWriterImpl::finalizeSignature()
if (!CryptMsgGetParam(hMsg, CMSG_BARE_CONTENT_PARAM, 0, pTsSig.get(), &nTsSigLen)) if (!CryptMsgGetParam(hMsg, CMSG_BARE_CONTENT_PARAM, 0, pTsSig.get(), &nTsSigLen))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_BARE_CONTENT_PARAM) failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_BARE_CONTENT_PARAM) failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hDecodedMsg); CryptMsgClose(hDecodedMsg);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
...@@ -7417,7 +7391,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7417,7 +7391,7 @@ bool PDFWriterImpl::finalizeSignature()
if (!CryptMsgUpdate(hDecodedMsg, pTsSig.get(), nTsSigLen, TRUE)) if (!CryptMsgUpdate(hDecodedMsg, pTsSig.get(), nTsSigLen, TRUE))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgUpdate failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgUpdate failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hDecodedMsg); CryptMsgClose(hDecodedMsg);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
...@@ -7427,7 +7401,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7427,7 +7401,7 @@ bool PDFWriterImpl::finalizeSignature()
DWORD nDecodedSignerInfoLen = 0; DWORD nDecodedSignerInfoLen = 0;
if (!CryptMsgGetParam(hDecodedMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &nDecodedSignerInfoLen)) if (!CryptMsgGetParam(hDecodedMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &nDecodedSignerInfoLen))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_SIGNER_INFO_PARAM) failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_SIGNER_INFO_PARAM) failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hDecodedMsg); CryptMsgClose(hDecodedMsg);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
...@@ -7438,7 +7412,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7438,7 +7412,7 @@ bool PDFWriterImpl::finalizeSignature()
if (!CryptMsgGetParam(hDecodedMsg, CMSG_SIGNER_INFO_PARAM, 0, pDecodedSignerInfoBuf.get(), &nDecodedSignerInfoLen)) if (!CryptMsgGetParam(hDecodedMsg, CMSG_SIGNER_INFO_PARAM, 0, pDecodedSignerInfoBuf.get(), &nDecodedSignerInfoLen))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_SIGNER_INFO_PARAM) failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_SIGNER_INFO_PARAM) failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hDecodedMsg); CryptMsgClose(hDecodedMsg);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
...@@ -7468,7 +7442,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7468,7 +7442,7 @@ bool PDFWriterImpl::finalizeSignature()
NULL, NULL,
NULL)) NULL))
{ {
SAL_WARN("vcl.pdfwriter", "CryptRetrieveTimeStamp failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptRetrieveTimeStamp failed: " << WindowsErrorString(GetLastError()));
CryptMsgClose(hDecodedMsg); CryptMsgClose(hDecodedMsg);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
CertFreeCertificateContext(pCertContext); CertFreeCertificateContext(pCertContext);
...@@ -7514,7 +7488,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7514,7 +7488,7 @@ bool PDFWriterImpl::finalizeSignature()
!CryptMsgUpdate(hMsg, (const BYTE *)buffer1.get(), bytesRead1, FALSE) || !CryptMsgUpdate(hMsg, (const BYTE *)buffer1.get(), bytesRead1, FALSE) ||
!CryptMsgUpdate(hMsg, (const BYTE *)buffer2.get(), bytesRead2, TRUE)) !CryptMsgUpdate(hMsg, (const BYTE *)buffer2.get(), bytesRead2, TRUE))
{ {
SAL_WARN("vcl.pdfwriter", "Re-creating the message failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "Re-creating the message failed: " << WindowsErrorString(GetLastError()));
CryptMemFree(pTsContext); CryptMemFree(pTsContext);
CryptMsgClose(hDecodedMsg); CryptMsgClose(hDecodedMsg);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
...@@ -7529,7 +7503,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7529,7 +7503,7 @@ bool PDFWriterImpl::finalizeSignature()
if (!CryptMsgGetParam(hMsg, CMSG_CONTENT_PARAM, 0, NULL, &nSigLen)) if (!CryptMsgGetParam(hMsg, CMSG_CONTENT_PARAM, 0, NULL, &nSigLen))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_CONTENT_PARAM) failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_CONTENT_PARAM) failed: " << WindowsErrorString(GetLastError()));
if (pTsContext) if (pTsContext)
CryptMemFree(pTsContext); CryptMemFree(pTsContext);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
...@@ -7552,7 +7526,7 @@ bool PDFWriterImpl::finalizeSignature() ...@@ -7552,7 +7526,7 @@ bool PDFWriterImpl::finalizeSignature()
if (!CryptMsgGetParam(hMsg, CMSG_CONTENT_PARAM, 0, pSig.get(), &nSigLen)) if (!CryptMsgGetParam(hMsg, CMSG_CONTENT_PARAM, 0, pSig.get(), &nSigLen))
{ {
SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_CONTENT_PARAM) failed: " << WindowsError(GetLastError())); SAL_WARN("vcl.pdfwriter", "CryptMsgGetParam(CMSG_CONTENT_PARAM) failed: " << WindowsErrorString(GetLastError()));
if (pTsContext) if (pTsContext)
CryptMemFree(pTsContext); CryptMemFree(pTsContext);
CryptMsgClose(hMsg); CryptMsgClose(hMsg);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment