Resolve code analysis reported warnings

This commit is contained in:
Simon Rozman 2018-12-18 09:05:00 +01:00
parent 51a82c242f
commit aa7cd261f2
12 changed files with 8580 additions and 8556 deletions

View File

@ -1,280 +1,287 @@
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \defgroup WinStdBase64 Base64 conversion
/// Provides Base64 conversion for WinStd classes
///
#include "Common.h"
#include <string>
#include <vector>
namespace winstd
{
class WINSTD_API base64_enc;
class WINSTD_API base64_dec;
}
#pragma once
namespace winstd
{
/// \addtogroup WinStdBase64
/// @{
///
/// Base64 encoding session
///
class WINSTD_API base64_enc
{
public:
///
/// Constructs blank encoding session
///
inline base64_enc() : num(0)
{
}
///
/// Encodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output
/// \param[in ] data Data to encode
/// \param[in ] size Length of `data` in bytes
/// \param[in ] is_last Is this the last block of data?
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true)
{
assert(data || !size);
// Preallocate output
out.reserve(out.size() + enc_size(size));
// Convert data character by character.
for (size_t i = 0;; i++) {
if (num >= 3) {
encode(out);
num = 0;
}
if (i >= size)
break;
buf[num++] = reinterpret_cast<const unsigned char*>(data)[i];
}
// If this is the last block, flush the buffer.
if (is_last && num) {
encode(out, num);
num = 0;
}
}
///
/// Resets encoding session
///
inline void clear()
{
num = 0;
}
///
/// Returns maximum encoded size
///
/// \param size Number of bytes to encode
///
/// \returns Maximum number of bytes for the encoded data of `size` length
///
inline size_t enc_size(size_t size) const
{
return ((num + size + 2)/3)*4;
}
protected:
///
/// Encodes one complete internal buffer of data
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out)
{
out += lookup[ buf[0] >> 2 ];
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += lookup[ buf[2] & 0x3f];
}
///
/// Encodes partial internal buffer of data
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size)
{
if (size > 0) {
out += lookup[buf[0] >> 2];
if (size > 1) {
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
if (size > 2) {
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += lookup[buf[2] & 0x3f];
} else {
out += lookup[(buf[1] << 2) & 0x3f];
out += '=';
}
} else {
out += lookup[(buf[0] << 4) & 0x3f];
out += '=';
out += '=';
}
} else {
out += '=';
out += '=';
out += '=';
out += '=';
}
}
protected:
unsigned char buf[3]; ///< Internal buffer
size_t num; ///< Number of bytes used in `buf`
/// \cond internal
static const char lookup[64];
/// \endcond
};
///
/// Base64 decoding session
///
class WINSTD_API base64_dec
{
public:
///
/// Constructs blank decoding session
///
inline base64_dec() : num(0)
{
}
///
/// Decodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output
/// \param[in ] is_last Was this the last block of data?
/// \param[in ] data Data to decode
/// \param[in ] size Length of `data` in bytes
///
template<class _Ty, class _Ax, class _Tchr>
inline void decode(_Out_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{
is_last = false;
// Trim data size to first terminator.
for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; }
// Preallocate output
out.reserve(out.size() + dec_size(size));
for (size_t i = 0;; i++) {
if (num >= 4) {
// Buffer full; decode it.
size_t nibbles = decode(out);
num = 0;
if (nibbles < 3) {
is_last = true;
break;
}
}
if (i >= size)
break;
int x = data[i];
if ((buf[num] = x < _countof(lookup) ? lookup[x] : 255) != 255)
num++;
}
}
///
/// Resets decoding session
///
inline void clear()
{
num = 0;
}
///
/// Returns maximum decoded size
///
/// \param size Number of bytes to decode
///
/// \returns Maximum number of bytes for the decoded data of `size` length
///
inline size_t dec_size(size_t size) const
{
return ((num + size + 3)/4)*3;
}
protected:
///
/// Decodes one complete internal buffer of data
///
template<class _Ty, class _Ax>
inline size_t decode(_Inout_ std::vector<_Ty, _Ax> &out)
{
out.push_back((_Ty)(((buf[0] << 2) | (buf[1] >> 4)) & 0xff));
if (buf[2] < 64) {
out.push_back((_Ty)(((buf[1] << 4) | (buf[2] >> 2)) & 0xff));
if (buf[3] < 64) {
out.push_back((_Ty)(((buf[2] << 6) | buf[3]) & 0xff));
return 3;
} else
return 2;
} else
return 1;
}
protected:
unsigned char buf[4]; ///< Internal buffer
size_t num; ///< Number of bytes used in `buf`
/// \cond internal
static const unsigned char lookup[256];
/// \endcond
};
/// @}
}
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \defgroup WinStdBase64 Base64 conversion
/// Provides Base64 conversion for WinStd classes
///
#include "Common.h"
#include <string>
#include <vector>
namespace winstd
{
class WINSTD_API base64_enc;
class WINSTD_API base64_dec;
}
#pragma once
namespace winstd
{
/// \addtogroup WinStdBase64
/// @{
///
/// Base64 encoding session
///
class WINSTD_API base64_enc
{
public:
///
/// Constructs blank encoding session
///
inline base64_enc() : num(0)
{
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
}
///
/// Encodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output
/// \param[in ] data Data to encode
/// \param[in ] size Length of `data` in bytes
/// \param[in ] is_last Is this the last block of data?
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size, _In_opt_ bool is_last = true)
{
assert(data || !size);
// Preallocate output
out.reserve(out.size() + enc_size(size));
// Convert data character by character.
for (size_t i = 0;; i++) {
if (num >= 3) {
encode(out);
num = 0;
}
if (i >= size)
break;
buf[num++] = reinterpret_cast<const unsigned char*>(data)[i];
}
// If this is the last block, flush the buffer.
if (is_last && num) {
encode(out, num);
num = 0;
}
}
///
/// Resets encoding session
///
inline void clear()
{
num = 0;
}
///
/// Returns maximum encoded size
///
/// \param size Number of bytes to encode
///
/// \returns Maximum number of bytes for the encoded data of `size` length
///
inline size_t enc_size(size_t size) const
{
return ((num + size + 2)/3)*4;
}
protected:
///
/// Encodes one complete internal buffer of data
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out)
{
out += lookup[ buf[0] >> 2 ];
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += lookup[ buf[2] & 0x3f];
}
///
/// Encodes partial internal buffer of data
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_ size_t size)
{
if (size > 0) {
out += lookup[buf[0] >> 2];
if (size > 1) {
out += lookup[((buf[0] << 4) | (buf[1] >> 4)) & 0x3f];
if (size > 2) {
out += lookup[((buf[1] << 2) | (buf[2] >> 6)) & 0x3f];
out += lookup[buf[2] & 0x3f];
} else {
out += lookup[(buf[1] << 2) & 0x3f];
out += '=';
}
} else {
out += lookup[(buf[0] << 4) & 0x3f];
out += '=';
out += '=';
}
} else {
out += '=';
out += '=';
out += '=';
out += '=';
}
}
protected:
unsigned char buf[3]; ///< Internal buffer
size_t num; ///< Number of bytes used in `buf`
/// \cond internal
static const char lookup[64];
/// \endcond
};
///
/// Base64 decoding session
///
class WINSTD_API base64_dec
{
public:
///
/// Constructs blank decoding session
///
inline base64_dec() : num(0)
{
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
}
///
/// Decodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output
/// \param[in ] is_last Was this the last block of data?
/// \param[in ] data Data to decode
/// \param[in ] size Length of `data` in bytes
///
template<class _Ty, class _Ax, class _Tchr>
inline void decode(_Out_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{
is_last = false;
// Trim data size to first terminator.
for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; }
// Preallocate output
out.reserve(out.size() + dec_size(size));
for (size_t i = 0;; i++) {
if (num >= 4) {
// Buffer full; decode it.
size_t nibbles = decode(out);
num = 0;
if (nibbles < 3) {
is_last = true;
break;
}
}
if (i >= size)
break;
int x = data[i];
if ((buf[num] = x < _countof(lookup) ? lookup[x] : 255) != 255)
num++;
}
}
///
/// Resets decoding session
///
inline void clear()
{
num = 0;
}
///
/// Returns maximum decoded size
///
/// \param size Number of bytes to decode
///
/// \returns Maximum number of bytes for the decoded data of `size` length
///
inline size_t dec_size(size_t size) const
{
return ((num + size + 3)/4)*3;
}
protected:
///
/// Decodes one complete internal buffer of data
///
template<class _Ty, class _Ax>
inline size_t decode(_Inout_ std::vector<_Ty, _Ax> &out)
{
out.push_back((_Ty)(((buf[0] << 2) | (buf[1] >> 4)) & 0xff));
if (buf[2] < 64) {
out.push_back((_Ty)(((buf[1] << 4) | (buf[2] >> 2)) & 0xff));
if (buf[3] < 64) {
out.push_back((_Ty)(((buf[2] << 6) | buf[3]) & 0xff));
return 3;
} else
return 2;
} else
return 1;
}
protected:
unsigned char buf[4]; ///< Internal buffer
size_t num; ///< Number of bytes used in `buf`
/// \cond internal
static const unsigned char lookup[256];
/// \endcond
};
/// @}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,251 +1,248 @@
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \defgroup WinStdCryptoAPI Cryptography API
/// Integrates WinStd classes with Microsoft Cryptography API
///
#include "Common.h"
#include <wincred.h>
#include <memory>
namespace winstd
{
template <class _Ty> struct CredFree_delete;
template <class _Ty> struct CredFree_delete<_Ty[]>;
}
/// \addtogroup WinStdCryptoAPI
/// @{
///
/// Enumerates the credentials from the user's credential set. The credential set used is the one associated with the logon session of the current token. The token must not have the user's SID disabled.
///
/// \sa [CredEnumerate function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374794.aspx)
///
inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _In_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials);
/// @copydoc CredProtectW()
template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType);
///
/// Encrypts the specified credentials so that only the current security context can decrypt them.
///
/// \sa [CredProtect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374803.aspx)
///
template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType);
/// @copydoc CredUnprotectW()
template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials);
///
/// Decrypts credentials that were previously encrypted by using the CredProtect function.
///
/// \sa [CredUnprotect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa375186.aspx)
///
template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials);
/// @}
#pragma once
namespace winstd
{
/// \addtogroup WinStdCryptoAPI
/// @{
///
/// Deleter for unique_ptr using CredFree
///
template <class _Ty> struct CredFree_delete
{
typedef CredFree_delete<_Ty> _Myt; ///< This type
///
/// Default construct
///
CredFree_delete() {}
///
/// Construct from another CredFree_delete
///
template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {}
///
/// Delete a pointer
///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
///
void operator()(_Ty *_Ptr) const
{
CredFree(_Ptr);
}
};
///
/// Deleter for unique_ptr to array of unknown size using CredFree
///
template <class _Ty> struct CredFree_delete<_Ty[]>
{
typedef CredFree_delete<_Ty> _Myt; ///< This type
///
/// Default construct
///
CredFree_delete() {}
///
/// Delete a pointer
///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
///
void operator()(_Ty *_Ptr) const
{
CredFree(_Ptr);
}
///
/// Delete a pointer of another type
///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
///
template<class _Other>
void operator()(_Other *) const
{
CredFree(_Ptr);
}
};
/// @}
}
inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _In_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials)
{
PCREDENTIAL *pCredentials;
if (CredEnumerate(Filter, Flags, Count, &pCredentials)) {
std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > cred(pCredentials);
cCredentials.swap(cred);
return TRUE;
} else
return FALSE;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
sProtectedCredentials.assign(buf, dwSize - 1);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
sProtectedCredentials.assign(buf.get(), dwSize - 1);
return TRUE;
}
}
return FALSE;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
sProtectedCredentials.assign(buf, dwSize - 1);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
sProtectedCredentials.assign(buf.get(), dwSize - 1);
return TRUE;
}
}
return FALSE;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
sCredentials.assign(buf, dwSize);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
sCredentials.assign(buf.get(), dwSize);
return TRUE;
}
}
return FALSE;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
sCredentials.assign(buf, dwSize);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
sCredentials.assign(buf.get(), dwSize);
return TRUE;
}
}
return FALSE;
}
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \defgroup WinStdCryptoAPI Cryptography API
/// Integrates WinStd classes with Microsoft Cryptography API
///
#include "Common.h"
#include <wincred.h>
#include <memory>
namespace winstd
{
template <class _Ty> struct CredFree_delete;
template <class _Ty> struct CredFree_delete<_Ty[]>;
}
/// \addtogroup WinStdCryptoAPI
/// @{
///
/// Enumerates the credentials from the user's credential set. The credential set used is the one associated with the logon session of the current token. The token must not have the user's SID disabled.
///
/// \sa [CredEnumerate function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374794.aspx)
///
inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials);
/// @copydoc CredProtectW()
template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType);
///
/// Encrypts the specified credentials so that only the current security context can decrypt them.
///
/// \sa [CredProtect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374803.aspx)
///
template<class _Elem, class _Traits, class _Ax> inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType);
/// @copydoc CredUnprotectW()
template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials);
///
/// Decrypts credentials that were previously encrypted by using the CredProtect function.
///
/// \sa [CredUnprotect function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa375186.aspx)
///
template<class _Elem, class _Traits, class _Ax> inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials);
/// @}
#pragma once
namespace winstd
{
/// \addtogroup WinStdCryptoAPI
/// @{
///
/// Deleter for unique_ptr using CredFree
///
template <class _Ty> struct CredFree_delete
{
typedef CredFree_delete<_Ty> _Myt; ///< This type
///
/// Default construct
///
CredFree_delete() {}
///
/// Construct from another CredFree_delete
///
template <class _Ty2> CredFree_delete(const CredFree_delete<_Ty2>&) {}
///
/// Delete a pointer
///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
///
void operator()(_Ty *_Ptr) const
{
CredFree(_Ptr);
}
};
///
/// Deleter for unique_ptr to array of unknown size using CredFree
///
template <class _Ty> struct CredFree_delete<_Ty[]>
{
typedef CredFree_delete<_Ty> _Myt; ///< This type
///
/// Default construct
///
CredFree_delete() {}
///
/// Delete a pointer
///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
///
void operator()(_Ty *_Ptr) const
{
CredFree(_Ptr);
}
///
/// Delete a pointer of another type
///
/// \sa [CredFree function](https://msdn.microsoft.com/en-us/library/windows/desktop/aa374796.aspx)
///
template<class _Other>
void operator()(_Other *) const
{
CredFree(_Ptr);
}
};
/// @}
}
inline BOOL CredEnumerate(_In_ LPCTSTR Filter, _Reserved_ DWORD Flags, _Out_ DWORD *Count, _Out_ std::unique_ptr<PCREDENTIAL[], winstd::CredFree_delete<PCREDENTIAL[]> > &cCredentials)
{
PCREDENTIAL *pCredentials = NULL;
BOOL bResult = CredEnumerate(Filter, Flags, Count, &pCredentials);
cCredentials.reset(pCredentials);
return bResult;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredProtectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
sProtectedCredentials.assign(buf, dwSize - 1);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredProtectA(fAsSelf, const_cast<LPSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
sProtectedCredentials.assign(buf.get(), dwSize - 1);
return TRUE;
}
}
return FALSE;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredProtectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sProtectedCredentials, _Out_ CRED_PROTECTION_TYPE *ProtectionType)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf, &dwSize, ProtectionType)) {
// Copy from stack.
sProtectedCredentials.assign(buf, dwSize - 1);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredProtectW(fAsSelf, const_cast<LPWSTR>(pszCredentials), cchCredentials, buf.get(), &dwSize, ProtectionType)) {
sProtectedCredentials.assign(buf.get(), dwSize - 1);
return TRUE;
}
}
return FALSE;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredUnprotectA(_In_ BOOL fAsSelf, _In_ LPCSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
sCredentials.assign(buf, dwSize);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredUnprotectA(fAsSelf, const_cast<LPSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
sCredentials.assign(buf.get(), dwSize);
return TRUE;
}
}
return FALSE;
}
template<class _Elem, class _Traits, class _Ax>
inline BOOL CredUnprotectW(_In_ BOOL fAsSelf, _In_ LPCWSTR pszProtectedCredentials, _In_ DWORD cchCredentials, _Out_ std::basic_string<_Elem, _Traits, _Ax> &sCredentials)
{
_Elem buf[WINSTD_STACK_BUFFER_BYTES/sizeof(_Elem)];
DWORD dwSize = _countof(buf);
// Try with the stack buffer first.
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf, &dwSize)) {
// Copy from stack.
sCredentials.assign(buf, dwSize);
return TRUE;
} else if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
// Allocate on heap and retry.
std::unique_ptr<_Elem[]> buf(new _Elem[dwSize]);
if (CredUnprotectW(fAsSelf, const_cast<LPWSTR>(pszProtectedCredentials), cchCredentials, buf.get(), &dwSize)) {
sCredentials.assign(buf.get(), dwSize);
return TRUE;
}
}
return FALSE;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,190 +1,192 @@
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \defgroup WinStdHexadecimal Hexadecimal conversion
/// Provides Hexadecimal conversion for WinStd classes
///
#include "Common.h"
#include <string>
#include <vector>
namespace winstd
{
class WINSTD_API hex_enc;
class WINSTD_API hex_dec;
}
#pragma once
namespace winstd
{
/// \addtogroup WinStdHexadecimal
/// @{
///
/// Hexadecimal encoding session
///
class WINSTD_API hex_enc
{
public:
///
/// Constructs blank encoding session
///
inline hex_enc()
{
}
///
/// Encodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output
/// \param[in ] data Data to encode
/// \param[in ] size Length of `data` in bytes
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
{
assert(data || !size);
// Preallocate output
out.reserve(out.size() + enc_size(size));
// Convert data character by character.
for (size_t i = 0; i < size; i++) {
unsigned char
x = reinterpret_cast<const unsigned char*>(data)[i],
x_h = ((x & 0xf0) >> 4),
x_l = ((x & 0x0f) );
out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
}
}
///
/// Returns maximum encoded size
///
/// \param size Number of bytes to encode
///
/// \returns Maximum number of bytes for the encoded data of `size` length
///
inline size_t enc_size(size_t size) const
{
return size*2;
}
};
///
/// Hexadecimal decoding session
///
class WINSTD_API hex_dec
{
public:
///
/// Constructs blank decoding session
///
inline hex_dec() : num(0)
{
}
///
/// Decodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output
/// \param[out] is_last Was this the last block of data? Actually, is this block of data complete?
/// \param[in ] data Data to decode
/// \param[in ] size Length of `data` in bytes
///
template<class _Ty, class _Ax, class _Tchr>
inline void decode(_Out_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{
is_last = false;
// Trim data size to first terminator.
for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; }
// Preallocate output
out.reserve(out.size() + dec_size(size));
for (size_t i = 0;; i++) {
if (num >= 2) {
// Buffer full.
out.push_back(buf);
num = 0;
is_last = true;
} else
is_last = false;
if (i >= size)
break;
int x = data[i];
if ('0' <= x && x <= '9') {
buf = ((buf & 0xf) << 4) | (unsigned char)(x - '0');
num++;
} else if ('A' <= x && x <= 'F') {
buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('A' - 10));
num++;
} else if ('a' <= x && x <= 'f') {
buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('a' - 10));
num++;
}
}
}
///
/// Resets decoding session
///
inline void clear()
{
num = 0;
}
///
/// Returns maximum decoded size
///
/// \param size Number of bytes to decode
///
/// \returns Maximum number of bytes for the decoded data of `size` length
///
inline size_t dec_size(size_t size) const
{
return (size + 1)/2;
}
protected:
unsigned char buf; ///< Internal buffer
size_t num; ///< Number of nibbles used in `buf`
};
/// @}
}
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
///
/// \defgroup WinStdHexadecimal Hexadecimal conversion
/// Provides Hexadecimal conversion for WinStd classes
///
#include "Common.h"
#include <string>
#include <vector>
namespace winstd
{
class WINSTD_API hex_enc;
class WINSTD_API hex_dec;
}
#pragma once
namespace winstd
{
/// \addtogroup WinStdHexadecimal
/// @{
///
/// Hexadecimal encoding session
///
class WINSTD_API hex_enc
{
public:
///
/// Constructs blank encoding session
///
inline hex_enc()
{
}
///
/// Encodes one block of information, and _appends_ it to the output
///
/// \param[out] out Output
/// \param[in ] data Data to encode
/// \param[in ] size Length of `data` in bytes
///
template<class _Elem, class _Traits, class _Ax>
inline void encode(_Out_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
{
assert(data || !size);
// Preallocate output
out.reserve(out.size() + enc_size(size));
// Convert data character by character.
for (size_t i = 0; i < size; i++) {
unsigned char
x = reinterpret_cast<const unsigned char*>(data)[i],
x_h = ((x & 0xf0) >> 4),
x_l = ((x & 0x0f) );
out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
}
}
///
/// Returns maximum encoded size
///
/// \param size Number of bytes to encode
///
/// \returns Maximum number of bytes for the encoded data of `size` length
///
inline size_t enc_size(size_t size) const
{
return size*2;
}
};
///
/// Hexadecimal decoding session
///
class WINSTD_API hex_dec
{
public:
///
/// Constructs blank decoding session
///
inline hex_dec() :
buf(0),
num(0)
{
}
///
/// Decodes one block of information, and _appends_ it to the output
///
/// \param[inout] out Output
/// \param[out ] is_last Was this the last block of data? Actually, is this block of data complete?
/// \param[in ] data Data to decode
/// \param[in ] size Length of `data` in bytes
///
template<class _Ty, class _Ax, class _Tchr>
inline void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{
is_last = false;
// Trim data size to first terminator.
for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; }
// Preallocate output
out.reserve(out.size() + dec_size(size));
for (size_t i = 0;; i++) {
if (num >= 2) {
// Buffer full.
out.push_back(buf);
num = 0;
is_last = true;
} else
is_last = false;
if (i >= size)
break;
int x = data[i];
if ('0' <= x && x <= '9') {
buf = ((buf & 0xf) << 4) | (unsigned char)(x - '0');
num++;
} else if ('A' <= x && x <= 'F') {
buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('A' - 10));
num++;
} else if ('a' <= x && x <= 'f') {
buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('a' - 10));
num++;
}
}
}
///
/// Resets decoding session
///
inline void clear()
{
num = 0;
}
///
/// Returns maximum decoded size
///
/// \param size Number of bytes to decode
///
/// \returns Maximum number of bytes for the decoded data of `size` length
///
inline size_t dec_size(size_t size) const
{
return (size + 1)/2;
}
protected:
unsigned char buf; ///< Internal buffer
size_t num; ///< Number of nibbles used in `buf`
};
/// @}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,137 +1,140 @@
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
#pragma comment(lib, "Eappcfg.lib")
//////////////////////////////////////////////////////////////////////
// winstd::eap_attr
//////////////////////////////////////////////////////////////////////
winstd::eap_attr::~eap_attr()
{
if (pValue)
delete []pValue;
}
void winstd::eap_attr::create_ms_mppe_key(_In_ BYTE bVendorType, _In_count_(nKeySize) LPCBYTE pbKey, _In_ BYTE nKeySize)
{
BYTE nPaddingLength = (BYTE)((16 - (1 + (DWORD)nKeySize)) % 16);
DWORD dwLengthNew =
4 + // Vendor-Id
1 + // Vendor type
1 + // Vendor length
2 + // Salt
1 + // Key-Length
nKeySize + // Key
nPaddingLength; // Padding
LPBYTE p = new BYTE[dwLengthNew];
p[0] = 0x00; // Vendor-Id (0x137 = 311 = Microsoft)
p[1] = 0x00; // --|
p[2] = 0x01; // --|
p[3] = 0x37; // --^
p[4] = bVendorType; // Vendor type
p[5] = (BYTE)(dwLengthNew - 4); // Vendor length
p[6] = 0x00; // Salt
p[7] = 0x00; // --^
p[8] = nKeySize; // Key-Length
memcpy(p + 9, pbKey, nKeySize); // Key
memset(p + 9 + nKeySize, 0, nPaddingLength); // Padding
if (pValue)
delete [] pValue;
eaType = eatVendorSpecific;
dwLength = dwLengthNew;
pValue = p;
}
const EAP_ATTRIBUTE winstd::eap_attr::blank = {};
//////////////////////////////////////////////////////////////////////
// winstd::eap_packet
//////////////////////////////////////////////////////////////////////
winstd::eap_packet::~eap_packet()
{
if (m_h)
HeapFree(GetProcessHeap(), 0, m_h);
}
void winstd::eap_packet::free_internal()
{
HeapFree(GetProcessHeap(), 0, m_h);
}
winstd::eap_packet::handle_type winstd::eap_packet::duplicate_internal(_In_ handle_type h) const
{
WORD n = ntohs(*(WORD*)h->Length);
handle_type h2 = (handle_type)HeapAlloc(GetProcessHeap(), 0, n);
if (!h2) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
memcpy(h2, h, n);
return h2;
}
//////////////////////////////////////////////////////////////////////
// winstd::eap_method_info_array
//////////////////////////////////////////////////////////////////////
winstd::eap_method_info_array::~eap_method_info_array()
{
if (pEapMethods)
free_internal();
}
/// \cond internal
void winstd::eap_method_info_array::free_internal()
{
for (DWORD i = 0; i < dwNumberOfMethods; i++)
free_internal(pEapMethods + i);
EapHostPeerFreeMemory((BYTE*)pEapMethods);
}
void winstd::eap_method_info_array::free_internal(_In_ EAP_METHOD_INFO *pMethodInfo)
{
if (pMethodInfo->pInnerMethodInfo)
free_internal(pMethodInfo->pInnerMethodInfo);
EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszAuthorName);
EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszFriendlyName);
}
/// \endcond
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
#pragma comment(lib, "Eappcfg.lib")
//////////////////////////////////////////////////////////////////////
// winstd::eap_attr
//////////////////////////////////////////////////////////////////////
winstd::eap_attr::~eap_attr()
{
if (pValue)
delete []pValue;
}
void winstd::eap_attr::create_ms_mppe_key(_In_ BYTE bVendorType, _In_count_(nKeySize) LPCBYTE pbKey, _In_ BYTE nKeySize)
{
BYTE nPaddingLength = (BYTE)((16 - (1 + (DWORD)nKeySize)) % 16);
DWORD dwLengthNew =
4 + // Vendor-Id
1 + // Vendor type
1 + // Vendor length
2 + // Salt
1 + // Key-Length
nKeySize + // Key
nPaddingLength; // Padding
#pragma warning(push)
#pragma warning(disable: 6386)
LPBYTE p = new BYTE[dwLengthNew];
p[0] = 0x00; // Vendor-Id (0x137 = 311 = Microsoft)
p[1] = 0x00; // --|
p[2] = 0x01; // --|
p[3] = 0x37; // --^
p[4] = bVendorType; // Vendor type
p[5] = (BYTE)(dwLengthNew - 4); // Vendor length
p[6] = 0x00; // Salt
p[7] = 0x00; // --^
p[8] = nKeySize; // Key-Length
#pragma warning(pop)
memcpy(p + 9, pbKey, nKeySize); // Key
memset(p + 9 + nKeySize, 0, nPaddingLength); // Padding
if (pValue)
delete [] pValue;
eaType = eatVendorSpecific;
dwLength = dwLengthNew;
pValue = p;
}
const EAP_ATTRIBUTE winstd::eap_attr::blank = {};
//////////////////////////////////////////////////////////////////////
// winstd::eap_packet
//////////////////////////////////////////////////////////////////////
winstd::eap_packet::~eap_packet()
{
if (m_h)
HeapFree(GetProcessHeap(), 0, m_h);
}
void winstd::eap_packet::free_internal()
{
HeapFree(GetProcessHeap(), 0, m_h);
}
winstd::eap_packet::handle_type winstd::eap_packet::duplicate_internal(_In_ handle_type h) const
{
WORD n = ntohs(*(WORD*)h->Length);
handle_type h2 = (handle_type)HeapAlloc(GetProcessHeap(), 0, n);
if (!h2) {
SetLastError(ERROR_OUTOFMEMORY);
return NULL;
}
memcpy(h2, h, n);
return h2;
}
//////////////////////////////////////////////////////////////////////
// winstd::eap_method_info_array
//////////////////////////////////////////////////////////////////////
winstd::eap_method_info_array::~eap_method_info_array()
{
if (pEapMethods)
free_internal();
}
/// \cond internal
void winstd::eap_method_info_array::free_internal()
{
for (DWORD i = 0; i < dwNumberOfMethods; i++)
free_internal(pEapMethods + i);
EapHostPeerFreeMemory((BYTE*)pEapMethods);
}
void winstd::eap_method_info_array::free_internal(_In_ EAP_METHOD_INFO *pMethodInfo)
{
if (pMethodInfo->pInnerMethodInfo)
free_internal(pMethodInfo->pInnerMethodInfo);
EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszAuthorName);
EapHostPeerFreeMemory((BYTE*)pMethodInfo->pwszFriendlyName);
}
/// \endcond

View File

@ -1,40 +1,42 @@
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../include/WinStd/Base64.h"
#include "../include/WinStd/COM.h"
#include "../include/WinStd/Cred.h"
#include "../include/WinStd/Crypt.h"
#include "../include/WinStd/EAP.h"
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
#include "../include/WinStd/ETW.h"
#endif
#include "../include/WinStd/Common.h"
#include "../include/WinStd/MSI.h"
#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL) || defined(SECURITY_MAC)
#include "../include/WinStd/Sec.h"
#endif
#include "../include/WinStd/Shell.h"
#include "../include/WinStd/Win.h"
#include "../include/WinStd/WLAN.h"
#include <tchar.h>
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "../include/WinStd/Base64.h"
#include "../include/WinStd/COM.h"
#include "../include/WinStd/Cred.h"
#include "../include/WinStd/Crypt.h"
#include "../include/WinStd/EAP.h"
#if _WIN32_WINNT >= _WIN32_WINNT_VISTA
#include "../include/WinStd/ETW.h"
#endif
#include "../include/WinStd/Hex.h"
#include "../include/WinStd/MSI.h"
#if defined(SECURITY_WIN32) || defined(SECURITY_KERNEL) || defined(SECURITY_MAC)
#include "../include/WinStd/Sec.h"
#endif
#include "../include/WinStd/Shell.h"
#include "../include/WinStd/Win.h"
#include "../include/WinStd/WinTrust.h"
#include "../include/WinStd/WLAN.h"
#include "../include/WinStd/Common.h"
#include <tchar.h>

View File

@ -1,328 +1,328 @@
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
//////////////////////////////////////////////////////////////////////
// StringToGuidA
//////////////////////////////////////////////////////////////////////
BOOL WINSTD_API StringToGuidA(_In_z_ LPCSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCSTR *lpszGuidEnd)
{
GUID g;
LPSTR lpszEnd;
unsigned long ulTmp;
unsigned long long ullTmp;
if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE;
lpszGuid++;
g.Data1 = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE) return FALSE;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data2 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data3 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff);
g.Data4[1] = (unsigned char)( ulTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ullTmp = _strtoui64(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE;
g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff);
g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff);
g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff);
g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff);
g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff);
g.Data4[7] = (unsigned char)( ullTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '}') return FALSE;
lpszGuid++;
if (lpszGuidEnd)
*lpszGuidEnd = lpszGuid;
*lpGuid = g;
return TRUE;
}
BOOL WINSTD_API StringToGuidW(_In_z_ LPCWSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCWSTR *lpszGuidEnd)
{
GUID g;
LPWSTR lpszEnd;
unsigned long ulTmp;
unsigned long long ullTmp;
if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE;
lpszGuid++;
g.Data1 = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE) return FALSE;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data2 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data3 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff);
g.Data4[1] = (unsigned char)( ulTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ullTmp = _wcstoui64(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE;
g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff);
g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff);
g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff);
g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff);
g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff);
g.Data4[7] = (unsigned char)( ullTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '}') return FALSE;
lpszGuid++;
if (lpszGuidEnd)
*lpszGuidEnd = lpszGuid;
*lpGuid = g;
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// winstd::win_handle
//////////////////////////////////////////////////////////////////////
winstd::win_handle::~win_handle()
{
if (m_h)
CloseHandle(m_h);
}
void winstd::win_handle::free_internal()
{
CloseHandle(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::library
//////////////////////////////////////////////////////////////////////
winstd::library::~library()
{
if (m_h)
FreeLibrary(m_h);
}
void winstd::library::free_internal()
{
FreeLibrary(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::heap
//////////////////////////////////////////////////////////////////////
winstd::heap::~heap()
{
if (m_h) {
enumerate();
HeapDestroy(m_h);
}
}
bool winstd::heap::enumerate()
{
bool found = false;
// Lock the heap for exclusive access.
HeapLock(m_h);
PROCESS_HEAP_ENTRY e;
e.lpData = NULL;
while (HeapWalk(m_h, &e) != FALSE) {
if ((e.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
OutputDebugStr(
_T("Allocated block%s%s\n")
_T(" Data portion begins at: %#p\n Size: %d bytes\n")
_T(" Overhead: %d bytes\n Region index: %d\n"),
(e.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE) != 0 ? tstring_printf(_T(", movable with HANDLE %#p"), e.Block.hMem).c_str() : _T(""),
(e.wFlags & PROCESS_HEAP_ENTRY_DDESHARE) != 0 ? _T(", DDESHARE") : _T(""),
e.lpData,
e.cbData,
e.cbOverhead,
e.iRegionIndex);
found = true;
}
}
DWORD dwResult = GetLastError();
if (dwResult != ERROR_NO_MORE_ITEMS)
OutputDebugStr(_T("HeapWalk failed (error %u).\n"), dwResult);
// Unlock the heap.
HeapUnlock(m_h);
return found;
}
void winstd::heap::free_internal()
{
enumerate();
HeapDestroy(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::actctx_activator
//////////////////////////////////////////////////////////////////////
winstd::actctx_activator::actctx_activator(_In_ HANDLE hActCtx)
{
if (!ActivateActCtx(hActCtx, &m_cookie))
m_cookie = 0;
}
winstd::actctx_activator::~actctx_activator()
{
if (m_cookie)
DeactivateActCtx(0, m_cookie);
}
//////////////////////////////////////////////////////////////////////
// winstd::user_impersonator
//////////////////////////////////////////////////////////////////////
winstd::user_impersonator::user_impersonator(_In_ HANDLE hToken)
{
m_cookie = ImpersonateLoggedOnUser(hToken);
}
winstd::user_impersonator::~user_impersonator()
{
if (m_cookie)
RevertToSelf();
}
//////////////////////////////////////////////////////////////////////
// winstd::vmemory
//////////////////////////////////////////////////////////////////////
winstd::vmemory::~vmemory()
{
if (m_h)
VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE);
}
void winstd::vmemory::free_internal()
{
VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE);
}
//////////////////////////////////////////////////////////////////////
// winstd::reg_key
//////////////////////////////////////////////////////////////////////
winstd::reg_key::~reg_key()
{
if (m_h)
RegCloseKey(m_h);
}
void winstd::reg_key::free_internal()
{
RegCloseKey(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::security_id
//////////////////////////////////////////////////////////////////////
winstd::security_id::~security_id()
{
if (m_h)
FreeSid(m_h);
}
void winstd::security_id::free_internal()
{
FreeSid(m_h);
}
/*
Copyright 1991-2018 Amebis
Copyright 2016 GÉANT
This file is part of WinStd.
Setup is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Setup is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Setup. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
//////////////////////////////////////////////////////////////////////
// StringToGuidA
//////////////////////////////////////////////////////////////////////
_Success_(return) BOOL WINSTD_API StringToGuidA(_In_z_ LPCSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCSTR *lpszGuidEnd)
{
GUID g;
LPSTR lpszEnd;
unsigned long ulTmp;
unsigned long long ullTmp;
if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE;
lpszGuid++;
g.Data1 = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE) return FALSE;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data2 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data3 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = strtoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff);
g.Data4[1] = (unsigned char)( ulTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ullTmp = _strtoui64(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE;
g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff);
g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff);
g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff);
g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff);
g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff);
g.Data4[7] = (unsigned char)( ullTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '}') return FALSE;
lpszGuid++;
if (lpszGuidEnd)
*lpszGuidEnd = lpszGuid;
*lpGuid = g;
return TRUE;
}
_Success_(return) BOOL WINSTD_API StringToGuidW(_In_z_ LPCWSTR lpszGuid, _Out_ LPGUID lpGuid, _Out_opt_ LPCWSTR *lpszGuidEnd)
{
GUID g;
LPWSTR lpszEnd;
unsigned long ulTmp;
unsigned long long ullTmp;
if (!lpszGuid || !lpGuid || *lpszGuid != '{') return FALSE;
lpszGuid++;
g.Data1 = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE) return FALSE;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data2 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data3 = (unsigned short)ulTmp;
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ulTmp = wcstoul(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ulTmp > 0xFFFF) return FALSE;
g.Data4[0] = (unsigned char)((ulTmp >> 8) & 0xff);
g.Data4[1] = (unsigned char)( ulTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '-') return FALSE;
lpszGuid++;
ullTmp = _wcstoui64(lpszGuid, &lpszEnd, 16);
if (errno == ERANGE || ullTmp > 0xFFFFFFFFFFFF) return FALSE;
g.Data4[2] = (unsigned char)((ullTmp >> 40) & 0xff);
g.Data4[3] = (unsigned char)((ullTmp >> 32) & 0xff);
g.Data4[4] = (unsigned char)((ullTmp >> 24) & 0xff);
g.Data4[5] = (unsigned char)((ullTmp >> 16) & 0xff);
g.Data4[6] = (unsigned char)((ullTmp >> 8) & 0xff);
g.Data4[7] = (unsigned char)( ullTmp & 0xff);
lpszGuid = lpszEnd;
if (*lpszGuid != '}') return FALSE;
lpszGuid++;
if (lpszGuidEnd)
*lpszGuidEnd = lpszGuid;
*lpGuid = g;
return TRUE;
}
//////////////////////////////////////////////////////////////////////
// winstd::win_handle
//////////////////////////////////////////////////////////////////////
winstd::win_handle::~win_handle()
{
if (m_h)
CloseHandle(m_h);
}
void winstd::win_handle::free_internal()
{
CloseHandle(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::library
//////////////////////////////////////////////////////////////////////
winstd::library::~library()
{
if (m_h)
FreeLibrary(m_h);
}
void winstd::library::free_internal()
{
FreeLibrary(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::heap
//////////////////////////////////////////////////////////////////////
winstd::heap::~heap()
{
if (m_h) {
enumerate();
HeapDestroy(m_h);
}
}
bool winstd::heap::enumerate()
{
bool found = false;
// Lock the heap for exclusive access.
HeapLock(m_h);
PROCESS_HEAP_ENTRY e;
e.lpData = NULL;
while (HeapWalk(m_h, &e) != FALSE) {
if ((e.wFlags & PROCESS_HEAP_ENTRY_BUSY) != 0) {
OutputDebugStr(
_T("Allocated block%s%s\n")
_T(" Data portion begins at: %#p\n Size: %d bytes\n")
_T(" Overhead: %d bytes\n Region index: %d\n"),
(e.wFlags & PROCESS_HEAP_ENTRY_MOVEABLE) != 0 ? tstring_printf(_T(", movable with HANDLE %#p"), e.Block.hMem).c_str() : _T(""),
(e.wFlags & PROCESS_HEAP_ENTRY_DDESHARE) != 0 ? _T(", DDESHARE") : _T(""),
e.lpData,
e.cbData,
e.cbOverhead,
e.iRegionIndex);
found = true;
}
}
DWORD dwResult = GetLastError();
if (dwResult != ERROR_NO_MORE_ITEMS)
OutputDebugStr(_T("HeapWalk failed (error %u).\n"), dwResult);
// Unlock the heap.
HeapUnlock(m_h);
return found;
}
void winstd::heap::free_internal()
{
enumerate();
HeapDestroy(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::actctx_activator
//////////////////////////////////////////////////////////////////////
winstd::actctx_activator::actctx_activator(_In_ HANDLE hActCtx)
{
if (!ActivateActCtx(hActCtx, &m_cookie))
m_cookie = 0;
}
winstd::actctx_activator::~actctx_activator()
{
if (m_cookie)
DeactivateActCtx(0, m_cookie);
}
//////////////////////////////////////////////////////////////////////
// winstd::user_impersonator
//////////////////////////////////////////////////////////////////////
winstd::user_impersonator::user_impersonator(_In_ HANDLE hToken)
{
m_cookie = ImpersonateLoggedOnUser(hToken);
}
winstd::user_impersonator::~user_impersonator()
{
if (m_cookie)
RevertToSelf();
}
//////////////////////////////////////////////////////////////////////
// winstd::vmemory
//////////////////////////////////////////////////////////////////////
winstd::vmemory::~vmemory()
{
if (m_h)
VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE);
}
void winstd::vmemory::free_internal()
{
VirtualFreeEx(m_proc, m_h, 0, MEM_RELEASE);
}
//////////////////////////////////////////////////////////////////////
// winstd::reg_key
//////////////////////////////////////////////////////////////////////
winstd::reg_key::~reg_key()
{
if (m_h)
RegCloseKey(m_h);
}
void winstd::reg_key::free_internal()
{
RegCloseKey(m_h);
}
//////////////////////////////////////////////////////////////////////
// winstd::security_id
//////////////////////////////////////////////////////////////////////
winstd::security_id::~security_id()
{
if (m_h)
FreeSid(m_h);
}
void winstd::security_id::free_internal()
{
FreeSid(m_h);
}