From 504ea681a953f203a219f53a358b7af02290ae40 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 20 Jul 2016 09:22:54 +0200 Subject: [PATCH] Strings packed/unpacked as zero-terminated (instead of length prefixed) now --- lib/EAPBase/include/EAPSerial.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/EAPBase/include/EAPSerial.h b/lib/EAPBase/include/EAPSerial.h index f49c190..95d6692 100644 --- a/lib/EAPBase/include/EAPSerial.h +++ b/lib/EAPBase/include/EAPSerial.h @@ -351,10 +351,8 @@ namespace eapserial inline void pack(_Inout_ unsigned char *&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*&)cursor = count; - cursor += sizeof(std::basic_string<_Elem, _Traits, _Ax>::size_type); - - size_t nSize = sizeof(_Elem)*count; + assert(strlen(val.c_str()) == count); // String should not contain null characters + size_t nSize = sizeof(_Elem)*(count + 1); memcpy(cursor, (const _Elem*)val.c_str(), nSize); cursor += nSize; } @@ -363,18 +361,16 @@ namespace eapserial template inline size_t get_pk_size(const std::basic_string<_Elem, _Traits, _Ax> &val) { - return sizeof(std::basic_string<_Elem, _Traits, _Ax>::size_type) + sizeof(_Elem)*val.length(); + return sizeof(_Elem)*(val.length() + 1); } template inline void unpack(_Inout_ const unsigned char *&cursor, _Out_ std::basic_string<_Elem, _Traits, _Ax> &val) { - std::basic_string<_Elem, _Traits, _Ax>::size_type count = *(const std::basic_string<_Elem, _Traits, _Ax>::size_type*&)cursor; - cursor += sizeof(std::basic_string<_Elem, _Traits, _Ax>::size_type); - + std::basic_string<_Elem, _Traits, _Ax>::size_type count = strlen((const _Elem*&)cursor); val.assign((const _Elem*&)cursor, count); - cursor += sizeof(_Elem)*count; + cursor += sizeof(_Elem)*(count + 1); }