"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/StringConverter.cpp" (10 Oct 2018, 8446 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "StringConverter.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifdef __GNUC__
   14 #   include <cxxabi.h>
   15 #endif
   16 #include <locale>
   17 #include <typeinfo>
   18 #include "Buffer.h"
   19 #include "Exception.h"
   20 #include "ForEach.h"
   21 #include "StringConverter.h"
   22 #include "SystemException.h"
   23 
   24 namespace VeraCrypt
   25 {
   26     void StringConverter::Erase (string &str)
   27     {
   28         for (size_t i = 0; i < str.size(); ++i)
   29         {
   30             str[i] = ' ';
   31         }
   32     }
   33 
   34     void StringConverter::Erase (wstring &str)
   35     {
   36         for (size_t i = 0; i < str.size(); ++i)
   37         {
   38             str[i] = ' ';
   39         }
   40     }
   41 
   42     wstring StringConverter::FromNumber (double number)
   43     {
   44         wstringstream s;
   45         s << number;
   46         return s.str();
   47     }
   48 
   49     wstring StringConverter::FromNumber (int32 number)
   50     {
   51         wstringstream s;
   52         s << number;
   53         return s.str();
   54     }
   55 
   56     wstring StringConverter::FromNumber (uint32 number)
   57     {
   58         wstringstream s;
   59         s << number;
   60         return s.str();
   61     }
   62 
   63     wstring StringConverter::FromNumber (int64 number)
   64     {
   65         wstringstream s;
   66         s << number;
   67         return s.str();
   68     }
   69 
   70     wstring StringConverter::FromNumber (uint64 number)
   71     {
   72         wstringstream s;
   73         s << number;
   74         return s.str();
   75     }
   76 
   77     string StringConverter::GetTrailingNumber (const string &str)
   78     {
   79         size_t start = str.find_last_not_of ("0123456789");
   80         if (start == string::npos)
   81             return str;
   82 
   83         string s = str.substr (start + 1);
   84         if (s.empty ())
   85             throw ParameterIncorrect (SRC_POS);
   86 
   87         return s;
   88     }
   89 
   90     string StringConverter::GetTypeName (const type_info &typeInfo)
   91     {
   92         try
   93         {
   94 #ifdef _MSC_VER
   95             // type_info::name() leaks memory as of MS VC++ 8.0
   96             string rawName (typeInfo.raw_name());
   97 
   98             size_t cut1 = (rawName.find (".?A") != string::npos) ? 4 : string::npos;
   99             size_t cut2 = rawName.find ("@");
  100             size_t cut3 = rawName.find ("@@");
  101 
  102             if (cut1 == string::npos || cut2 == string::npos || cut3 == string::npos)
  103                 return typeInfo.name();
  104 
  105             return rawName.substr (cut2 + 1, cut3 - cut2 - 1) + "::" + rawName.substr (cut1, cut2 - cut1);
  106 
  107 #elif defined (__GNUC__)
  108             int status;
  109             char *name = abi::__cxa_demangle (typeInfo.name(), nullptr, nullptr, &status);
  110 
  111             if (name)
  112             {
  113                 string s (name);
  114                 free (name);
  115                 return s;
  116             }
  117 #endif
  118         }
  119         catch (...) { }
  120 
  121         return typeInfo.name();
  122     }
  123 
  124     wstring StringConverter::QuoteSpaces (const wstring &str)
  125     {
  126         if (str.find (L' ') == string::npos)
  127             return str;
  128 
  129         wstring escaped (L"'");
  130         foreach (wchar_t c, str)
  131         {
  132             if (c == L'\'')
  133                 escaped += L'\'';
  134             escaped += c;
  135         }
  136         return escaped + L'\'';
  137     }
  138 
  139     vector <string> StringConverter::Split (const string &str, const string &separators, bool returnEmptyFields)
  140     {
  141         vector <string> elements;
  142 
  143         if (!returnEmptyFields)
  144         {
  145             size_t p = 0;
  146             while ((p = str.find_first_not_of (separators, p)) != string::npos)
  147             {
  148                 size_t end = str.find_first_of (separators, p);
  149                 if (end == string::npos)
  150                 {
  151                     elements.push_back (str.substr (p));
  152                     break;
  153                 }
  154 
  155                 elements.push_back (str.substr (p, end - p));
  156                 p = end;
  157             }
  158         }
  159         else
  160         {
  161             string element;
  162             elements.push_back (element);
  163             foreach (char c, str)
  164             {
  165                 if (separators.find (c) != string::npos)
  166                 {
  167                     element.erase();
  168                     elements.push_back (element);
  169                 }
  170                 else
  171                 {
  172                     elements.back() += c;
  173                 }
  174             }
  175         }
  176 
  177         return elements;
  178     }
  179 
  180     string StringConverter::StripTrailingNumber (const string &str)
  181     {
  182         size_t start = str.find_last_not_of ("0123456789");
  183         if (start == string::npos)
  184             return "";
  185 
  186         return str.substr (0, start + 1);
  187     }
  188 
  189     wstring StringConverter::ToExceptionString (const exception &ex)
  190     {
  191         const SystemException *sysEx = dynamic_cast <const SystemException *> (&ex);
  192         if (sysEx)
  193             return ToWide (sysEx->what()) + L": " + sysEx->SystemText() + L": " + sysEx->GetSubject();
  194 
  195         if (ex.what() && !string (ex.what()).empty())
  196             return ToWide (GetTypeName (typeid (ex)) + ": " + ex.what());
  197 
  198         return ToWide (GetTypeName (typeid (ex)));
  199     }
  200 
  201     string StringConverter::ToLower (const string &str)
  202     {
  203         string s;
  204         foreach (char c, str)
  205             s += tolower (c, locale());
  206         return s;
  207     }
  208 
  209     string StringConverter::ToSingle (const wstring &wstr, bool noThrow)
  210     {
  211         string str;
  212         ToSingle (wstr, str, noThrow);
  213         return str;
  214     }
  215 
  216     void StringConverter::ToSingle (const wstring &wstr, string &str, bool noThrow)
  217     {
  218         try
  219         {
  220             mbstate_t mbState;
  221             Memory::Zero (&mbState, sizeof (mbState));
  222             const wchar_t *src = wstr.c_str();
  223 
  224             size_t size = wcsrtombs (nullptr, &src, 0, &mbState);
  225             if (size == (size_t) -1)
  226                 throw StringConversionFailed (SRC_POS, wstr);
  227 
  228             vector <char> buf (size + 1);
  229             Memory::Zero (&mbState, sizeof (mbState));
  230 
  231             if ((size = wcsrtombs (&buf[0], &src, buf.size(), &mbState)) == (size_t) -1)
  232                 throw StringConversionFailed (SRC_POS, wstr);
  233 
  234             str.clear();
  235             str.insert (0, &buf.front(), size);
  236             Memory::Erase (&buf.front(), buf.size());
  237         }
  238         catch (...)
  239         {
  240             if (!noThrow)
  241                 throw;
  242         }
  243     }
  244 
  245     uint32 StringConverter::ToUInt32 (const string &str)
  246     {
  247         uint32 n;
  248         stringstream ss (str);
  249 
  250         ss >> n;
  251         if (ss.fail() || n == 0xffffFFFFU)
  252             throw ParameterIncorrect (SRC_POS);
  253 
  254         return n;
  255     }
  256 
  257     uint32 StringConverter::ToUInt32 (const wstring &str)
  258     {
  259         uint32 n;
  260         wstringstream ss (str);
  261 
  262         ss >> n;
  263         if (ss.fail() || n == 0xffffFFFFU)
  264             throw ParameterIncorrect (SRC_POS);
  265 
  266         return n;
  267     }
  268 
  269     int32 StringConverter::ToInt32 (const string &str)
  270     {
  271         int32 n;
  272         stringstream ss (str);
  273 
  274         ss >> n;
  275         if (ss.fail() || n == 0x7fffFFFF || n == -0x7fffFFFF)
  276             throw ParameterIncorrect (SRC_POS);
  277 
  278         return n;
  279     }
  280 
  281     int32 StringConverter::ToInt32 (const wstring &str)
  282     {
  283         int32 n;
  284         wstringstream ss (str);
  285 
  286         ss >> n;
  287         if (ss.fail() || n == 0x7fffFFFF || n == -0x7fffFFFF)
  288             throw ParameterIncorrect (SRC_POS);
  289 
  290         return n;
  291     }
  292 
  293     uint64 StringConverter::ToUInt64 (const string &str)
  294     {
  295         uint64 n;
  296         stringstream ss (str);
  297 
  298         ss >> n;
  299         if (ss.fail() || n == 0xffffFFFFffffFFFFULL)
  300             throw ParameterIncorrect (SRC_POS);
  301 
  302         return n;
  303     }
  304 
  305     uint64 StringConverter::ToUInt64 (const wstring &str)
  306     {
  307         uint64 n;
  308         wstringstream ss (str);
  309 
  310         ss >> n;
  311         if (ss.fail() || n == 0xffffFFFFffffFFFFULL)
  312             throw ParameterIncorrect (SRC_POS);
  313 
  314         return n;
  315     }
  316 
  317     int64 StringConverter::ToInt64 (const string &str)
  318     {
  319         int64 n;
  320         stringstream ss (str);
  321 
  322         ss >> n;
  323         if (ss.fail() || n == 0x7fffFFFFffffFFFFLL || n == -0x7fffFFFFffffFFFFLL)
  324             throw ParameterIncorrect (SRC_POS);
  325 
  326         return n;
  327     }
  328 
  329     int64 StringConverter::ToInt64 (const wstring &str)
  330     {
  331         int64 n;
  332         wstringstream ss (str);
  333 
  334         ss >> n;
  335         if (ss.fail() || n == 0x7fffFFFFffffFFFFLL || n == -0x7fffFFFFffffFFFFLL)
  336             throw ParameterIncorrect (SRC_POS);
  337 
  338         return n;
  339     }
  340 
  341     string StringConverter::ToUpper (const string &str)
  342     {
  343         string s;
  344         foreach (char c, str)
  345             s += toupper (c, locale());
  346         return s;
  347     }
  348 
  349     wstring StringConverter::ToWide (const string &str, bool noThrow)
  350     {
  351         try
  352         {
  353             mbstate_t mbState;
  354             Memory::Zero (&mbState, sizeof (mbState));
  355             const char *src = str.c_str();
  356 
  357             size_t size = mbsrtowcs (nullptr, &src, 0, &mbState);
  358             if (size == (size_t) -1)
  359                 throw StringConversionFailed (SRC_POS);
  360 
  361             vector <wchar_t> buf (size + 1);
  362             Memory::Zero (&mbState, sizeof (mbState));
  363 
  364             if ((size = mbsrtowcs (&buf[0], &src, buf.size(), &mbState)) == (size_t) -1)
  365                 throw StringConversionFailed (SRC_POS);
  366 
  367             wstring s;
  368             s.insert (s.begin(), buf.begin(), buf.begin() + size);
  369             return s;
  370         }
  371         catch (...)
  372         {
  373             if (noThrow)
  374                 return L"";
  375             throw;
  376         }
  377     }
  378 
  379     void StringConverter::ToWideBuffer (const wstring &str, wchar_t *buffer, size_t bufferSize)
  380     {
  381         if (str.length() < 1)
  382         {
  383             buffer[0] = 0;
  384             return;
  385         }
  386 
  387         BufferPtr (
  388             (byte *) buffer,
  389             bufferSize).CopyFrom (
  390                 ConstBufferPtr ((byte *) (wstring (str).c_str()),
  391                 (str.length() + 1) * sizeof (wchar_t)
  392             )
  393         );
  394     }
  395 
  396     string StringConverter::Trim (const string &str)
  397     {
  398         size_t start = 0;
  399         size_t end = str.size();
  400         if (end < 1)
  401             return str;
  402 
  403         foreach (char c, str)
  404         {
  405             if (c > ' ')
  406                 break;
  407             ++start;
  408         }
  409 
  410         foreach_reverse (char c, str)
  411         {
  412             if (c > ' ')
  413                 break;
  414             --end;
  415         }
  416 
  417         return str.substr (start, end - start);
  418     }
  419 }