From 23b06ec0945cfc9c09e27584f9dfb4f6f6483f01 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 30 Mar 2012 04:24:52 +0000 Subject: [PATCH] changed: in Crypto::Crypt() MSVC compiler in debug mode reports accessing to vector[0] element (assertion) when the vector was empty (we took only a pointer without dereferencing) now we are using a dummy char object in such a case git-svn-id: svn://ttmath.org/publicrep/tito/trunk@398 e52654a7-88a9-db11-a3e9-0013d4bc506e --- src/crypto.cpp | 38 +++++++++++++++++++++++++++++++++----- src/crypto.h | 4 ++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/crypto.cpp b/src/crypto.cpp index 6e8634b..2666c2c 100755 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -86,6 +86,14 @@ void Crypto::AssignString(const std::wstring & src, std::string & dst, bool clea + +Crypto::Crypto() +{ + dummy_char = 0; +} + + + bool Crypto::SetAESKey(unsigned char aes_key[32]) { if( !aes.Key(aes_key, 32) ) @@ -102,7 +110,11 @@ return true; void Crypto::Crypt(const std::wstring & in, std::wstring & out) { PT::WideToUTF8(in, utf8_str); - aes.Encode((const unsigned char*)utf8_str.c_str(), utf8_str.size(), aes_name, uint8_tab); + + if( utf8_str.empty() ) + aes.Encode(&dummy_char, 0, aes_name, uint8_tab); + else + aes.Encode((const unsigned char*)utf8_str.c_str(), utf8_str.size(), aes_name, uint8_tab); base64_str.clear(); @@ -116,16 +128,32 @@ void Crypto::Crypt(const std::wstring & in, std::wstring & out) void Crypto::Crypt(const std::string & in, std::string & out) { - aes.Encode((const unsigned char*)&in[0], in.size(), aes_name, uint8_tab); - base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), out); + if( in.empty() ) + aes.Encode(&dummy_char, 0, aes_name, uint8_tab); + else + aes.Encode((const unsigned char*)&in[0], in.size(), aes_name, uint8_tab); + + out.clear(); + + if( !uint8_tab.empty() ) + base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), out); + Clear(); } void Crypto::Crypt(const std::vector & in, std::string & out) { - aes.Encode((const unsigned char*)&in[0], in.size(), aes_name, uint8_tab); - base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), out); + if( in.empty() ) + aes.Encode(&dummy_char, 0, aes_name, uint8_tab); + else + aes.Encode((const unsigned char*)&in[0], in.size(), aes_name, uint8_tab); + + out.clear(); + + if( !uint8_tab.empty() ) + base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), out); + Clear(); } diff --git a/src/crypto.h b/src/crypto.h index cadfb7d..d6288b3 100755 --- a/src/crypto.h +++ b/src/crypto.h @@ -51,6 +51,7 @@ class Crypto { public: + Crypto(); /* setting AES256 key @@ -106,6 +107,9 @@ private: std::string base64_str; std::string aes_str; + // a dummy character used when an input string is empty + unsigned char dummy_char; + void Clear();