"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/Serializer.cpp" (10 Oct 2018, 7023 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 "Serializer.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 #include "Exception.h"
   14 #include "ForEach.h"
   15 #include "Memory.h"
   16 #include "Serializer.h"
   17 
   18 namespace VeraCrypt
   19 {
   20     template <typename T>
   21     T Serializer::Deserialize ()
   22     {
   23         uint64 size;
   24         DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &size, sizeof (size)));
   25 
   26         if (Endian::Big (size) != sizeof (T))
   27             throw ParameterIncorrect (SRC_POS);
   28 
   29         T data;
   30         DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &data, sizeof (data)));
   31 
   32         return Endian::Big (data);
   33     }
   34 
   35     void Serializer::Deserialize (const string &name, bool &data)
   36     {
   37         ValidateName (name);
   38         data = Deserialize <byte> () == 1;
   39     }
   40 
   41     void Serializer::Deserialize (const string &name, byte &data)
   42     {
   43         ValidateName (name);
   44         data = Deserialize <byte> ();
   45     }
   46 
   47     void Serializer::Deserialize (const string &name, int32 &data)
   48     {
   49         ValidateName (name);
   50         data = (int32) Deserialize <uint32> ();
   51     }
   52 
   53     void Serializer::Deserialize (const string &name, int64 &data)
   54     {
   55         ValidateName (name);
   56         data = (int64) Deserialize <uint64> ();
   57     }
   58 
   59     void Serializer::Deserialize (const string &name, uint32 &data)
   60     {
   61         ValidateName (name);
   62         data = Deserialize <uint32> ();
   63     }
   64 
   65     void Serializer::Deserialize (const string &name, uint64 &data)
   66     {
   67         ValidateName (name);
   68         data = Deserialize <uint64> ();
   69     }
   70 
   71     void Serializer::Deserialize (const string &name, string &data)
   72     {
   73         ValidateName (name);
   74         data = DeserializeString ();
   75     }
   76 
   77     void Serializer::Deserialize (const string &name, wstring &data)
   78     {
   79         ValidateName (name);
   80         data = DeserializeWString ();
   81     }
   82 
   83     void Serializer::Deserialize (const string &name, const BufferPtr &data)
   84     {
   85         ValidateName (name);
   86 
   87         uint64 size = Deserialize <uint64> ();
   88         if (data.Size() != size)
   89             throw ParameterIncorrect (SRC_POS);
   90 
   91         DataStream->ReadCompleteBuffer (data);
   92     }
   93 
   94     bool Serializer::DeserializeBool (const string &name)
   95     {
   96         bool data;
   97         Deserialize (name, data);
   98         return data;
   99     }
  100 
  101     int32 Serializer::DeserializeInt32 (const string &name)
  102     {
  103         ValidateName (name);
  104         return Deserialize <uint32> ();
  105     }
  106 
  107     int64 Serializer::DeserializeInt64 (const string &name)
  108     {
  109         ValidateName (name);
  110         return Deserialize <uint64> ();
  111     }
  112 
  113     uint32 Serializer::DeserializeUInt32 (const string &name)
  114     {
  115         ValidateName (name);
  116         return Deserialize <uint32> ();
  117     }
  118 
  119     uint64 Serializer::DeserializeUInt64 (const string &name)
  120     {
  121         ValidateName (name);
  122         return Deserialize <uint64> ();
  123     }
  124 
  125     string Serializer::DeserializeString ()
  126     {
  127         uint64 size = Deserialize <uint64> ();
  128 
  129         vector <char> data ((size_t) size);
  130         DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &data[0], (size_t) size));
  131 
  132         return string (&data[0]);
  133     }
  134 
  135     string Serializer::DeserializeString (const string &name)
  136     {
  137         ValidateName (name);
  138         return DeserializeString ();
  139     }
  140 
  141     list <string> Serializer::DeserializeStringList (const string &name)
  142     {
  143         ValidateName (name);
  144         list <string> deserializedList;
  145         uint64 listSize = Deserialize <uint64> ();
  146 
  147         for (size_t i = 0; i < listSize; i++)
  148             deserializedList.push_back (DeserializeString ());
  149 
  150         return deserializedList;
  151     }
  152 
  153     wstring Serializer::DeserializeWString ()
  154     {
  155         uint64 size = Deserialize <uint64> ();
  156 
  157         vector <wchar_t> data ((size_t) size / sizeof (wchar_t));
  158         DataStream->ReadCompleteBuffer (BufferPtr ((byte *) &data[0], (size_t) size));
  159 
  160         return wstring (&data[0]);
  161     }
  162 
  163     list <wstring> Serializer::DeserializeWStringList (const string &name)
  164     {
  165         ValidateName (name);
  166         list <wstring> deserializedList;
  167         uint64 listSize = Deserialize <uint64> ();
  168 
  169         for (size_t i = 0; i < listSize; i++)
  170             deserializedList.push_back (DeserializeWString ());
  171 
  172         return deserializedList;
  173     }
  174 
  175     wstring Serializer::DeserializeWString (const string &name)
  176     {
  177         ValidateName (name);
  178         return DeserializeWString ();
  179     }
  180 
  181     template <typename T>
  182     void Serializer::Serialize (T data)
  183     {
  184         uint64 size = Endian::Big (uint64 (sizeof (data)));
  185         DataStream->Write (ConstBufferPtr ((byte *) &size, sizeof (size)));
  186 
  187         data = Endian::Big (data);
  188         DataStream->Write (ConstBufferPtr ((byte *) &data, sizeof (data)));
  189     }
  190 
  191     void Serializer::Serialize (const string &name, bool data)
  192     {
  193         SerializeString (name);
  194         byte d = data ? 1 : 0;
  195         Serialize (d);
  196     }
  197 
  198     void Serializer::Serialize (const string &name, byte data)
  199     {
  200         SerializeString (name);
  201         Serialize (data);
  202     }
  203 
  204     void Serializer::Serialize (const string &name, const char *data)
  205     {
  206         Serialize (name, string (data));
  207     }
  208 
  209     void Serializer::Serialize (const string &name, int32 data)
  210     {
  211         SerializeString (name);
  212         Serialize ((uint32) data);
  213     }
  214 
  215     void Serializer::Serialize (const string &name, int64 data)
  216     {
  217         SerializeString (name);
  218         Serialize ((uint64) data);
  219     }
  220 
  221     void Serializer::Serialize (const string &name, uint32 data)
  222     {
  223         SerializeString (name);
  224         Serialize (data);
  225     }
  226 
  227     void Serializer::Serialize (const string &name, uint64 data)
  228     {
  229         SerializeString (name);
  230         Serialize (data);
  231     }
  232 
  233     void Serializer::Serialize (const string &name, const string &data)
  234     {
  235         SerializeString (name);
  236         SerializeString (data);
  237     }
  238 
  239     void Serializer::Serialize (const string &name, const wchar_t *data)
  240     {
  241         Serialize (name, wstring (data));
  242     }
  243 
  244     void Serializer::Serialize (const string &name, const wstring &data)
  245     {
  246         SerializeString (name);
  247         SerializeWString (data);
  248     }
  249 
  250     void Serializer::Serialize (const string &name, const list <string> &stringList)
  251     {
  252         SerializeString (name);
  253 
  254         uint64 listSize = stringList.size();
  255         Serialize (listSize);
  256 
  257         foreach (const string &item, stringList)
  258             SerializeString (item);
  259     }
  260 
  261     void Serializer::Serialize (const string &name, const list <wstring> &stringList)
  262     {
  263         SerializeString (name);
  264 
  265         uint64 listSize = stringList.size();
  266         Serialize (listSize);
  267 
  268         foreach (const wstring &item, stringList)
  269             SerializeWString (item);
  270     }
  271 
  272     void Serializer::Serialize (const string &name, const ConstBufferPtr &data)
  273     {
  274         SerializeString (name);
  275 
  276         uint64 size = data.Size();
  277         Serialize (size);
  278 
  279         DataStream->Write (data);
  280     }
  281 
  282     void Serializer::SerializeString (const string &data)
  283     {
  284         Serialize ((uint64) data.size() + 1);
  285         DataStream->Write (ConstBufferPtr ((byte *) (data.data() ? data.data() : data.c_str()), data.size() + 1));
  286     }
  287 
  288     void Serializer::SerializeWString (const wstring &data)
  289     {
  290         uint64 size = (data.size() + 1) * sizeof (wchar_t);
  291         Serialize (size);
  292         DataStream->Write (ConstBufferPtr ((byte *) (data.data() ? data.data() : data.c_str()), (size_t) size));
  293     }
  294 
  295     void Serializer::ValidateName (const string &name)
  296     {
  297         string dName = DeserializeString();
  298         if (dName != name)
  299         {
  300             throw ParameterIncorrect (SRC_POS);
  301         }
  302     }
  303 }