Memory overflow detection when packing/unpacking BLOB added

This commit is contained in:
Simon Rozman 2016-07-20 19:29:21 +02:00
parent 2f4425f38c
commit 51428d290f
14 changed files with 168 additions and 119 deletions

View File

@ -53,7 +53,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val Configuration to pack /// \param[in] val Configuration to pack
/// ///
inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config &val); inline void pack(_Inout_ cursor_out &cursor, _In_ const eap::config &val);
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -70,7 +70,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val Configuration to unpack to /// \param[out] val Configuration to unpack to
/// ///
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config &val); inline void unpack(_Inout_ cursor_in &cursor, _Out_ eap::config &val);
} }
#pragma once #pragma once
@ -181,7 +181,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -195,7 +195,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// @} /// @}
@ -288,7 +288,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -302,7 +302,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// @} /// @}
@ -412,7 +412,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -426,7 +426,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// @} /// @}
@ -531,7 +531,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -545,7 +545,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// @} /// @}
@ -557,7 +557,7 @@ namespace eap
namespace eapserial namespace eapserial
{ {
inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::config &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const eap::config &val)
{ {
val.pack(cursor); val.pack(cursor);
} }
@ -569,7 +569,7 @@ namespace eapserial
} }
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::config &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ eap::config &val)
{ {
val.unpack(cursor); val.unpack(cursor);
} }

View File

@ -293,7 +293,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -307,7 +307,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// @} /// @}

View File

@ -48,13 +48,23 @@ namespace eap
namespace eapserial namespace eapserial
{ {
///
/// Output BLOB cursor
///
struct cursor_out;
///
/// Input BLOB cursor
///
struct cursor_in;
/// ///
/// Packs a boolean /// Packs a boolean
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val Variable with data to pack /// \param[in] val Variable with data to pack
/// ///
inline void pack(_Inout_ unsigned char *&cursor, _In_ const bool &val); inline void pack(_Inout_ cursor_out &cursor, _In_ const bool &val);
/// ///
/// Returns packed size of a boolean /// Returns packed size of a boolean
@ -71,7 +81,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val Variable to receive unpacked value /// \param[out] val Variable to receive unpacked value
/// ///
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ bool &val); inline void unpack(_Inout_ cursor_in &cursor, _Out_ bool &val);
/// ///
/// Packs a byte /// Packs a byte
@ -79,7 +89,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val Variable with data to pack /// \param[in] val Variable with data to pack
/// ///
inline void pack(_Inout_ unsigned char *&cursor, _In_ const unsigned char &val); inline void pack(_Inout_ cursor_out &cursor, _In_ const unsigned char &val);
/// ///
/// Returns packed size of a byte /// Returns packed size of a byte
@ -96,7 +106,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val Variable to receive unpacked value /// \param[out] val Variable to receive unpacked value
/// ///
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ unsigned char &val); inline void unpack(_Inout_ cursor_in &cursor, _Out_ unsigned char &val);
/// ///
/// Packs an unsigned int /// Packs an unsigned int
@ -104,7 +114,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val Variable with data to pack /// \param[in] val Variable with data to pack
/// ///
inline void pack(_Inout_ unsigned char *&cursor, _In_ const unsigned int &val); inline void pack(_Inout_ cursor_out &cursor, _In_ const unsigned int &val);
/// ///
/// Returns packed size of an unsigned int /// Returns packed size of an unsigned int
@ -121,7 +131,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val Variable to receive unpacked value /// \param[out] val Variable to receive unpacked value
/// ///
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ unsigned int &val); inline void unpack(_Inout_ cursor_in &cursor, _Out_ unsigned int &val);
#ifdef _WIN64 #ifdef _WIN64
/// ///
@ -130,7 +140,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val Variable with data to pack /// \param[in] val Variable with data to pack
/// ///
inline void pack(_Inout_ unsigned char *&cursor, _In_ const size_t &val); inline void pack(_Inout_ cursor_out &cursor, _In_ const size_t &val);
/// ///
/// Returns packed size of a size_t /// Returns packed size of a size_t
@ -147,7 +157,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val Variable to receive unpacked value /// \param[out] val Variable to receive unpacked value
/// ///
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ size_t &val); inline void unpack(_Inout_ cursor_in &cursor, _Out_ size_t &val);
#endif #endif
/// ///
@ -156,7 +166,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val String to pack /// \param[in] val String to pack
/// ///
template<class _Elem, class _Traits, class _Ax> inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::basic_string<_Elem, _Traits, _Ax> &val); template<class _Elem, class _Traits, class _Ax> inline void pack(_Inout_ cursor_out &cursor, _In_ const std::basic_string<_Elem, _Traits, _Ax> &val);
/// ///
/// Returns packed size of a string /// Returns packed size of a string
@ -173,7 +183,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val String to unpack to /// \param[out] val String to unpack to
/// ///
template<class _Elem, class _Traits, class _Ax> inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::basic_string<_Elem, _Traits, _Ax> &val); template<class _Elem, class _Traits, class _Ax> inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::basic_string<_Elem, _Traits, _Ax> &val);
/// ///
/// Packs a wide string /// Packs a wide string
@ -181,7 +191,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val String to pack /// \param[in] val String to pack
/// ///
template<class _Traits, class _Ax> inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::basic_string<wchar_t, _Traits, _Ax> &val); template<class _Traits, class _Ax> inline void pack(_Inout_ cursor_out &cursor, _In_ const std::basic_string<wchar_t, _Traits, _Ax> &val);
/// ///
/// Returns packed size of a wide string /// Returns packed size of a wide string
@ -198,7 +208,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val String to unpack to /// \param[out] val String to unpack to
/// ///
template<class _Traits, class _Ax> inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &val); template<class _Traits, class _Ax> inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &val);
/// ///
/// Packs a vector /// Packs a vector
@ -206,7 +216,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val Vector to pack /// \param[in] val Vector to pack
/// ///
template<class _Ty, class _Ax> inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::vector<_Ty, _Ax> &val); template<class _Ty, class _Ax> inline void pack(_Inout_ cursor_out &cursor, _In_ const std::vector<_Ty, _Ax> &val);
/// ///
/// Returns packed size of a vector /// Returns packed size of a vector
@ -223,7 +233,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val Vector to unpack to /// \param[out] val Vector to unpack to
/// ///
template<class _Ty, class _Ax> inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::vector<_Ty, _Ax> &val); template<class _Ty, class _Ax> inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::vector<_Ty, _Ax> &val);
/// ///
/// Packs a list /// Packs a list
@ -231,7 +241,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val List to pack /// \param[in] val List to pack
/// ///
template<class _Ty, class _Ax> inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::list<_Ty, _Ax> &val); template<class _Ty, class _Ax> inline void pack(_Inout_ cursor_out &cursor, _In_ const std::list<_Ty, _Ax> &val);
/// ///
/// Returns packed size of a list /// Returns packed size of a list
@ -248,7 +258,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val List to unpack to /// \param[out] val List to unpack to
/// ///
template<class _Ty, class _Ax> inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::list<_Ty, _Ax> &val); template<class _Ty, class _Ax> inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::list<_Ty, _Ax> &val);
/// ///
/// Packs a std::unique_ptr /// Packs a std::unique_ptr
@ -256,7 +266,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val std::unique_ptr to pack /// \param[in] val std::unique_ptr to pack
/// ///
template<class _Ty, class _Dx> inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::unique_ptr<_Ty, _Dx> &val); template<class _Ty, class _Dx> inline void pack(_Inout_ cursor_out &cursor, _In_ const std::unique_ptr<_Ty, _Dx> &val);
/// ///
/// Returns packed size of a std::unique_ptr /// Returns packed size of a std::unique_ptr
@ -275,7 +285,7 @@ namespace eapserial
///// \param[inout] cursor Memory cursor ///// \param[inout] cursor Memory cursor
///// \param[out] val std::unique_ptr to unpack to ///// \param[out] val std::unique_ptr to unpack to
///// /////
//template<class _Ty, class _Dx> inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::unique_ptr<_Ty, _Dx> &val); //template<class _Ty, class _Dx> inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::unique_ptr<_Ty, _Dx> &val);
/// ///
/// Packs a certificate context /// Packs a certificate context
@ -283,7 +293,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val Certificate context to pack /// \param[in] val Certificate context to pack
/// ///
inline void pack(_Inout_ unsigned char *&cursor, _In_ const winstd::cert_context &val); inline void pack(_Inout_ cursor_out &cursor, _In_ const winstd::cert_context &val);
/// ///
/// Returns packed size of a certificate context /// Returns packed size of a certificate context
@ -300,7 +310,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val Certificate context to unpack to /// \param[out] val Certificate context to unpack to
/// ///
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ winstd::cert_context &val); inline void unpack(_Inout_ cursor_in &cursor, _Out_ winstd::cert_context &val);
/// ///
/// Packs an EAP method type /// Packs an EAP method type
@ -308,7 +318,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[in] val EAP method type to pack /// \param[in] val EAP method type to pack
/// ///
inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::type_t &val); inline void pack(_Inout_ cursor_out &cursor, _In_ const eap::type_t &val);
/// ///
/// Returns packed size of an EAP method type /// Returns packed size of an EAP method type
@ -325,7 +335,7 @@ namespace eapserial
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// \param[out] val EAP method type to unpack to /// \param[out] val EAP method type to unpack to
/// ///
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::type_t &val); inline void unpack(_Inout_ cursor_in &cursor, _Out_ eap::type_t &val);
} }
#pragma once #pragma once
@ -346,10 +356,29 @@ namespace eap
namespace eapserial namespace eapserial
{ {
inline void pack(_Inout_ unsigned char *&cursor, _In_ const bool &val) struct cursor_out
{ {
*cursor = val ? 1 : 0; typedef unsigned char *ptr_type;
cursor++;
ptr_type ptr; ///< Pointer to first data unwritten
ptr_type ptr_end; ///< Pointer to the end of available memory
};
struct cursor_in
{
typedef const unsigned char *ptr_type;
ptr_type ptr; ///< Pointer to first data unread
ptr_type ptr_end; ///< Pointer to the end of BLOB
};
inline void pack(_Inout_ cursor_out &cursor, _In_ const bool &val)
{
cursor_out::ptr_type ptr_end = cursor.ptr + 1;
assert(ptr_end <= cursor.ptr_end);
*cursor.ptr = val ? 1 : 0;
cursor.ptr = ptr_end;
} }
@ -360,17 +389,21 @@ namespace eapserial
} }
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ bool &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ bool &val)
{ {
val = *cursor ? true : false; cursor_in::ptr_type ptr_end = cursor.ptr + 1;
cursor++; assert(ptr_end <= cursor.ptr_end);
val = *cursor.ptr ? true : false;
cursor.ptr = ptr_end;
} }
inline void pack(_Inout_ unsigned char *&cursor, _In_ const unsigned char &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const unsigned char &val)
{ {
*cursor = val; cursor_out::ptr_type ptr_end = cursor.ptr + 1;
cursor++; assert(ptr_end <= cursor.ptr_end);
*cursor.ptr = val;
cursor.ptr = ptr_end;
} }
@ -381,17 +414,21 @@ namespace eapserial
} }
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ unsigned char &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ unsigned char &val)
{ {
val = *cursor; cursor_in::ptr_type ptr_end = cursor.ptr + 1;
cursor++; assert(ptr_end <= cursor.ptr_end);
val = *cursor.ptr;
cursor.ptr = ptr_end;
} }
inline void pack(_Inout_ unsigned char *&cursor, _In_ const unsigned int &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const unsigned int &val)
{ {
*(unsigned int*)cursor = val; cursor_out::ptr_type ptr_end = cursor.ptr + sizeof(unsigned int);
cursor += sizeof(unsigned int); assert(ptr_end <= cursor.ptr_end);
*(unsigned int*)cursor.ptr = val;
cursor.ptr = ptr_end;
} }
@ -402,18 +439,22 @@ namespace eapserial
} }
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ unsigned int &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ unsigned int &val)
{ {
val = *(unsigned int*)cursor; cursor_in::ptr_type ptr_end = cursor.ptr + sizeof(unsigned int);
cursor += sizeof(unsigned int); assert(ptr_end <= cursor.ptr_end);
val = *(unsigned int*)cursor.ptr;
cursor.ptr = ptr_end;
} }
#ifdef _WIN64 #ifdef _WIN64
inline void pack(_Inout_ unsigned char *&cursor, _In_ const size_t &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const size_t &val)
{ {
*(size_t*)cursor = val; cursor_out::ptr_type ptr_end = cursor.ptr + sizeof(size_t);
cursor += sizeof(size_t); assert(ptr_end <= cursor.ptr_end);
*(size_t*)cursor.ptr = val;
cursor.ptr = ptr_end;
} }
@ -424,22 +465,26 @@ namespace eapserial
} }
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ size_t &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ size_t &val)
{ {
val = *(size_t*)cursor; cursor_in::ptr_type ptr_end = cursor.ptr + sizeof(size_t);
cursor += sizeof(size_t); assert(ptr_end <= cursor.ptr_end);
val = *(size_t*)cursor.ptr;
cursor.ptr = ptr_end;
} }
#endif #endif
template<class _Elem, class _Traits, class _Ax> template<class _Elem, class _Traits, class _Ax>
inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::basic_string<_Elem, _Traits, _Ax> &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const std::basic_string<_Elem, _Traits, _Ax> &val)
{ {
std::basic_string<_Elem, _Traits, _Ax>::size_type count = val.length(); std::basic_string<_Elem, _Traits, _Ax>::size_type count = val.length();
assert(strlen(val.c_str()) == count); // String should not contain null characters assert(strlen(val.c_str()) == count); // String should not contain zero terminators.
size_t nSize = sizeof(_Elem)*(count + 1); size_t size = sizeof(_Elem)*(count + 1);
memcpy(cursor, (const _Elem*)val.c_str(), nSize); cursor_out::ptr_type ptr_end = cursor.ptr + size;
cursor += nSize; assert(ptr_end <= cursor.ptr_end);
memcpy(cursor.ptr, (const _Elem*)val.c_str(), size);
cursor.ptr = ptr_end;
} }
@ -451,16 +496,18 @@ namespace eapserial
template<class _Elem, class _Traits, class _Ax> template<class _Elem, class _Traits, class _Ax>
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::basic_string<_Elem, _Traits, _Ax> &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::basic_string<_Elem, _Traits, _Ax> &val)
{ {
std::basic_string<_Elem, _Traits, _Ax>::size_type count = strlen((const _Elem*&)cursor); size_t count_max = cursor.ptr_end - cursor.ptr;
val.assign((const _Elem*&)cursor, count); std::basic_string<_Elem, _Traits, _Ax>::size_type count = strnlen((const _Elem*&)cursor.ptr, count_max);
cursor += sizeof(_Elem)*(count + 1); assert(count < count_max); // String should be zero terminated.
val.assign((const _Elem*&)cursor.ptr, count);
cursor.ptr += sizeof(_Elem)*(count + 1);
} }
template<class _Traits, class _Ax> template<class _Traits, class _Ax>
inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::basic_string<wchar_t, _Traits, _Ax> &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const std::basic_string<wchar_t, _Traits, _Ax> &val)
{ {
std::string val_utf8; std::string val_utf8;
WideCharToMultiByte(CP_UTF8, 0, val.c_str(), (int)val.length(), val_utf8, NULL, NULL); WideCharToMultiByte(CP_UTF8, 0, val.c_str(), (int)val.length(), val_utf8, NULL, NULL);
@ -471,12 +518,12 @@ namespace eapserial
template<class _Traits, class _Ax> template<class _Traits, class _Ax>
inline size_t get_pk_size(const std::basic_string<wchar_t, _Traits, _Ax> &val) inline size_t get_pk_size(const std::basic_string<wchar_t, _Traits, _Ax> &val)
{ {
return sizeof(std::string::size_type) + WideCharToMultiByte(CP_UTF8, 0, val.c_str(), (int)val.length(), NULL, 0, NULL, NULL); return sizeof(char)*(WideCharToMultiByte(CP_UTF8, 0, val.c_str(), (int)val.length(), NULL, 0, NULL, NULL) + 1);
} }
template<class _Traits, class _Ax> template<class _Traits, class _Ax>
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &val)
{ {
std::string val_utf8; std::string val_utf8;
unpack(cursor, val_utf8); unpack(cursor, val_utf8);
@ -485,7 +532,7 @@ namespace eapserial
template<class _Ty, class _Ax> template<class _Ty, class _Ax>
inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::vector<_Ty, _Ax> &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const std::vector<_Ty, _Ax> &val)
{ {
std::vector<_Ty, _Ax>::size_type count = val.size(); std::vector<_Ty, _Ax>::size_type count = val.size();
pack(cursor, count); pack(cursor, count);
@ -511,7 +558,7 @@ namespace eapserial
template<class _Ty, class _Ax> template<class _Ty, class _Ax>
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::vector<_Ty, _Ax> &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::vector<_Ty, _Ax> &val)
{ {
std::vector<_Ty, _Ax>::size_type count; std::vector<_Ty, _Ax>::size_type count;
unpack(cursor, count); unpack(cursor, count);
@ -529,7 +576,7 @@ namespace eapserial
template<class _Ty, class _Ax> template<class _Ty, class _Ax>
inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::list<_Ty, _Ax> &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const std::list<_Ty, _Ax> &val)
{ {
std::list<_Ty, _Ax>::size_type count = val.size(); std::list<_Ty, _Ax>::size_type count = val.size();
pack(cursor, count); pack(cursor, count);
@ -555,7 +602,7 @@ namespace eapserial
template<class _Ty, class _Ax> template<class _Ty, class _Ax>
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::list<_Ty, _Ax> &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ std::list<_Ty, _Ax> &val)
{ {
std::list<_Ty, _Ax>::size_type count; std::list<_Ty, _Ax>::size_type count;
unpack(cursor, count); unpack(cursor, count);
@ -572,7 +619,7 @@ namespace eapserial
template<class _Ty, class _Dx> template<class _Ty, class _Dx>
inline void pack(_Inout_ unsigned char *&cursor, _In_ const std::unique_ptr<_Ty, _Dx> &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const std::unique_ptr<_Ty, _Dx> &val)
{ {
if (val) { if (val) {
pack(cursor, true); pack(cursor, true);
@ -593,13 +640,15 @@ namespace eapserial
} }
inline void pack(_Inout_ unsigned char *&cursor, _In_ const winstd::cert_context &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const winstd::cert_context &val)
{ {
if (val) { if (val) {
pack(cursor, (unsigned int)val->dwCertEncodingType); pack(cursor, (unsigned int)val->dwCertEncodingType);
pack(cursor, (unsigned int)val->cbCertEncoded ); pack(cursor, (unsigned int)val->cbCertEncoded );
memcpy(cursor, val->pbCertEncoded, val->cbCertEncoded); cursor_out::ptr_type ptr_end = cursor.ptr + val->cbCertEncoded;
cursor += val->cbCertEncoded; assert(ptr_end <= cursor.ptr_end);
memcpy(cursor.ptr, val->pbCertEncoded, val->cbCertEncoded);
cursor.ptr = ptr_end;
} else { } else {
pack(cursor, (unsigned int)0); pack(cursor, (unsigned int)0);
pack(cursor, (unsigned int)0); pack(cursor, (unsigned int)0);
@ -619,7 +668,7 @@ namespace eapserial
} }
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ winstd::cert_context &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ winstd::cert_context &val)
{ {
DWORD dwCertEncodingType; DWORD dwCertEncodingType;
unpack(cursor, (unsigned int&)dwCertEncodingType); unpack(cursor, (unsigned int&)dwCertEncodingType);
@ -628,14 +677,16 @@ namespace eapserial
unpack(cursor, (unsigned int&)dwCertEncodedSize); unpack(cursor, (unsigned int&)dwCertEncodedSize);
if (dwCertEncodedSize) { if (dwCertEncodedSize) {
val.create(dwCertEncodingType, (BYTE*)cursor, dwCertEncodedSize); cursor_in::ptr_type ptr_end = cursor.ptr + dwCertEncodedSize;
cursor += dwCertEncodedSize; assert(ptr_end <= cursor.ptr_end);
val.create(dwCertEncodingType, (BYTE*)cursor.ptr, dwCertEncodedSize);
cursor.ptr = ptr_end;
} else } else
val.free(); val.free();
} }
inline void pack(_Inout_ unsigned char *&cursor, _In_ const eap::type_t &val) inline void pack(_Inout_ cursor_out &cursor, _In_ const eap::type_t &val)
{ {
pack(cursor, (unsigned char)val); pack(cursor, (unsigned char)val);
} }
@ -647,7 +698,7 @@ namespace eapserial
} }
inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ eap::type_t &val) inline void unpack(_Inout_ cursor_in &cursor, _Out_ eap::type_t &val)
{ {
unsigned char t; unsigned char t;
unpack(cursor, t); unpack(cursor, t);

View File

@ -561,15 +561,13 @@ namespace eap
if (!decrypt_md5(cp, pDataIn, dwDataInSize, data, ppEapError)) if (!decrypt_md5(cp, pDataIn, dwDataInSize, data, ppEapError))
return false; return false;
const unsigned char *cursor = data.data(); eapserial::cursor_in cursor = { data.data(), data.data() + data.size() };
eapserial::unpack(cursor, record); eapserial::unpack(cursor, record);
assert(cursor - data.data() <= (ptrdiff_t)data.size());
#else #else
UNREFERENCED_PARAMETER(ppEapError); UNREFERENCED_PARAMETER(ppEapError);
const unsigned char *cursor = pDataIn; eapserial::cursor_in cursor = { pDataIn, pDataIn + dwDataInSize };
eapserial::unpack(cursor, record); eapserial::unpack(cursor, record);
assert(cursor - pDataIn <= (ptrdiff_t)dwDataInSize);
#endif #endif
return true; return true;
@ -601,9 +599,9 @@ namespace eap
data.resize(eapserial::get_pk_size(record)); data.resize(eapserial::get_pk_size(record));
// Pack to BLOB. // Pack to BLOB.
unsigned char *cursor = data.data(); eapserial::cursor_out cursor = { data.data(), data.data() + data.size() };
eapserial::pack(cursor, record); eapserial::pack(cursor, record);
data.resize(cursor - data.data()); data.resize(cursor.ptr - &data.front());
// Prepare cryptographics provider. // Prepare cryptographics provider.
winstd::crypt_prov cp; winstd::crypt_prov cp;
@ -639,9 +637,9 @@ namespace eap
} }
// Pack to BLOB. // Pack to BLOB.
unsigned char *cursor = *ppDataOut; eapserial::cursor_out cursor = { *ppDataOut, *ppDataOut + *pdwDataOutSize };
eapserial::pack(cursor, record); eapserial::pack(cursor, record);
assert(cursor - *ppDataOut <= (ptrdiff_t)*pdwDataOutSize); *pdwDataOutSize = cursor.ptr - *ppDataOut;
#endif #endif
return true; return true;

View File

@ -81,7 +81,7 @@ bool eap::config::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR **ppEapErr
} }
void eap::config::pack(_Inout_ unsigned char *&cursor) const void eap::config::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
UNREFERENCED_PARAMETER(cursor); UNREFERENCED_PARAMETER(cursor);
} }
@ -93,7 +93,7 @@ size_t eap::config::get_pk_size() const
} }
void eap::config::unpack(_Inout_ const unsigned char *&cursor) void eap::config::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
UNREFERENCED_PARAMETER(cursor); UNREFERENCED_PARAMETER(cursor);
} }
@ -236,7 +236,7 @@ bool eap::config_method::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR **p
} }
void eap::config_method::pack(_Inout_ unsigned char *&cursor) const void eap::config_method::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
config::pack(cursor); config::pack(cursor);
eapserial::pack(cursor, m_allow_save ); eapserial::pack(cursor, m_allow_save );
@ -262,7 +262,7 @@ size_t eap::config_method::get_pk_size() const
} }
void eap::config_method::unpack(_Inout_ const unsigned char *&cursor) void eap::config_method::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
config::unpack(cursor); config::unpack(cursor);
eapserial::unpack(cursor, m_allow_save ); eapserial::unpack(cursor, m_allow_save );
@ -590,7 +590,7 @@ bool eap::config_provider::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR *
} }
void eap::config_provider::pack(_Inout_ unsigned char *&cursor) const void eap::config_provider::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
config::pack(cursor); config::pack(cursor);
eapserial::pack(cursor, m_read_only ); eapserial::pack(cursor, m_read_only );
@ -623,7 +623,7 @@ size_t eap::config_provider::get_pk_size() const
} }
void eap::config_provider::unpack(_Inout_ const unsigned char *&cursor) void eap::config_provider::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
config::unpack(cursor); config::unpack(cursor);
eapserial::unpack(cursor, m_read_only ); eapserial::unpack(cursor, m_read_only );
@ -776,7 +776,7 @@ bool eap::config_providers::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR
} }
void eap::config_providers::pack(_Inout_ unsigned char *&cursor) const void eap::config_providers::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
config::pack(cursor); config::pack(cursor);
eapserial::pack(cursor, m_providers); eapserial::pack(cursor, m_providers);
@ -791,7 +791,7 @@ size_t eap::config_providers::get_pk_size() const
} }
void eap::config_providers::unpack(_Inout_ const unsigned char *&cursor) void eap::config_providers::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
config::unpack(cursor); config::unpack(cursor);

View File

@ -233,7 +233,7 @@ bool eap::credentials_pass::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR
} }
void eap::credentials_pass::pack(_Inout_ unsigned char *&cursor) const void eap::credentials_pass::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
eap::credentials::pack(cursor); eap::credentials::pack(cursor);
eapserial::pack(cursor, m_identity); eapserial::pack(cursor, m_identity);
@ -250,7 +250,7 @@ size_t eap::credentials_pass::get_pk_size() const
} }
void eap::credentials_pass::unpack(_Inout_ const unsigned char *&cursor) void eap::credentials_pass::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
eap::credentials::unpack(cursor); eap::credentials::unpack(cursor);
eapserial::unpack(cursor, m_identity); eapserial::unpack(cursor, m_identity);

View File

@ -141,7 +141,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -155,7 +155,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// @} /// @}

View File

@ -137,7 +137,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -151,7 +151,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// @} /// @}

View File

@ -256,7 +256,7 @@ bool eap::config_method_tls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR
} }
void eap::config_method_tls::pack(_Inout_ unsigned char *&cursor) const void eap::config_method_tls::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
eap::config_method::pack(cursor); eap::config_method::pack(cursor);
eapserial::pack(cursor, m_trusted_root_ca); eapserial::pack(cursor, m_trusted_root_ca);
@ -273,7 +273,7 @@ size_t eap::config_method_tls::get_pk_size() const
} }
void eap::config_method_tls::unpack(_Inout_ const unsigned char *&cursor) void eap::config_method_tls::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
eap::config_method::unpack(cursor); eap::config_method::unpack(cursor);
eapserial::unpack(cursor, m_trusted_root_ca); eapserial::unpack(cursor, m_trusted_root_ca);

View File

@ -167,7 +167,7 @@ bool eap::credentials_tls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR *
} }
void eap::credentials_tls::pack(_Inout_ unsigned char *&cursor) const void eap::credentials_tls::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
eap::credentials::pack(cursor); eap::credentials::pack(cursor);
eapserial::pack(cursor, m_cert); eapserial::pack(cursor, m_cert);
@ -182,7 +182,7 @@ size_t eap::credentials_tls::get_pk_size() const
} }
void eap::credentials_tls::unpack(_Inout_ const unsigned char *&cursor) void eap::credentials_tls::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
eap::credentials::unpack(cursor); eap::credentials::unpack(cursor);
eapserial::unpack(cursor, m_cert); eapserial::unpack(cursor, m_cert);

View File

@ -127,7 +127,7 @@ namespace eap {
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -141,7 +141,7 @@ namespace eap {
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// ///
/// Returns EAP method type of this configuration /// Returns EAP method type of this configuration

View File

@ -133,7 +133,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void pack(_Inout_ unsigned char *&cursor) const; virtual void pack(_Inout_ eapserial::cursor_out &cursor) const;
/// ///
/// Returns packed size of a configuration /// Returns packed size of a configuration
@ -147,7 +147,7 @@ namespace eap
/// ///
/// \param[inout] cursor Memory cursor /// \param[inout] cursor Memory cursor
/// ///
virtual void unpack(_Inout_ const unsigned char *&cursor); virtual void unpack(_Inout_ eapserial::cursor_in &cursor);
/// \name Storage /// \name Storage
/// @{ /// @{

View File

@ -158,7 +158,7 @@ bool eap::config_method_ttls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERRO
} }
void eap::config_method_ttls::pack(_Inout_ unsigned char *&cursor) const void eap::config_method_ttls::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
eap::config_method_tls::pack(cursor); eap::config_method_tls::pack(cursor);
if (m_inner) { if (m_inner) {
@ -195,7 +195,7 @@ size_t eap::config_method_ttls::get_pk_size() const
} }
void eap::config_method_ttls::unpack(_Inout_ const unsigned char *&cursor) void eap::config_method_ttls::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
eap::config_method_tls::unpack(cursor); eap::config_method_tls::unpack(cursor);

View File

@ -149,7 +149,7 @@ bool eap::credentials_ttls::load(_In_ IXMLDOMNode *pConfigRoot, _Out_ EAP_ERROR
} }
void eap::credentials_ttls::pack(_Inout_ unsigned char *&cursor) const void eap::credentials_ttls::pack(_Inout_ eapserial::cursor_out &cursor) const
{ {
eap::credentials_tls::pack(cursor); eap::credentials_tls::pack(cursor);
if (m_inner) { if (m_inner) {
@ -186,7 +186,7 @@ size_t eap::credentials_ttls::get_pk_size() const
} }
void eap::credentials_ttls::unpack(_Inout_ const unsigned char *&cursor) void eap::credentials_ttls::unpack(_Inout_ eapserial::cursor_in &cursor)
{ {
eap::credentials_tls::unpack(cursor); eap::credentials_tls::unpack(cursor);