"Fossies" - the Fresh Open Source Software Archive

Member "src/Main/Xml.cpp" (10 Oct 2018, 4710 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 "Xml.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 "System.h"
   14 #include <wx/tokenzr.h>
   15 #include "Platform/FileStream.h"
   16 #include "Xml.h"
   17 
   18 namespace VeraCrypt
   19 {
   20     XmlParser::XmlParser (const FilePath &fileName)
   21     {
   22         make_shared_auto (File, file);
   23         file->Open (fileName);
   24         FileStream stream (file);
   25 
   26         XmlText = wxString::FromUTF8 (stream.ReadToEnd().c_str());
   27     }
   28 
   29     wxString XmlParser::ConvertEscapedChars (wxString xmlString) const
   30     {
   31         xmlString.Replace (L"&lt;", L"<");
   32         xmlString.Replace (L"&gt;", L">");
   33         xmlString.Replace (L"&amp;", L"&");
   34         xmlString.Replace (L"&quot;", L"\"");
   35         return xmlString;
   36     }
   37 
   38     XmlNodeList XmlParser::GetNodes (const wxString &nodeName) const
   39     {
   40         XmlNodeList nodeList;
   41 
   42         size_t nodePos = 0;
   43         while ((nodePos = XmlText.find (L"<" + nodeName, nodePos)) != string::npos)
   44         {
   45             XmlNode xmlNode;
   46             xmlNode.Name = nodeName;
   47 
   48             size_t nodeEnd = XmlText.find (L">", nodePos);
   49             if (nodeEnd == string::npos)
   50                 throw ParameterIncorrect (SRC_POS);
   51 
   52             wxString nodeTagText = XmlText.substr (nodePos + 1, nodeEnd - nodePos - 1);
   53             nodePos = nodeEnd;
   54 
   55             if (nodeTagText.size() > nodeName.size() && nodeTagText[nodeName.size()] != L' ' && nodeTagText[nodeName.size()] != L'/')
   56                 continue;
   57 
   58             nodeTagText = nodeTagText.substr (nodeName.size());
   59 
   60 
   61             // Attributes
   62             wxStringTokenizer tokenizer (nodeTagText, L"\"", wxTOKEN_RET_EMPTY);
   63             while (tokenizer.HasMoreTokens())
   64             {
   65                 wxString attributeName = tokenizer.GetNextToken();
   66                 attributeName.Replace (L" ", L"", true);
   67                 attributeName.Replace (L"=", L"");
   68 
   69                 if (!attributeName.empty() && tokenizer.HasMoreTokens())
   70                 {
   71                     wxString attributeText = tokenizer.GetNextToken();
   72                     xmlNode.Attributes[attributeName] = ConvertEscapedChars (attributeText);
   73                 }
   74             }
   75 
   76             // Inner text
   77             if (!nodeTagText.EndsWith (L"/"))
   78             {
   79                 size_t innerTextPos = nodeEnd + 1;
   80                 size_t innerTextEnd = XmlText.find (L"</" + nodeName + L">", innerTextPos);
   81                 if (innerTextEnd == string::npos)
   82                     throw ParameterIncorrect (SRC_POS);
   83 
   84                 xmlNode.InnerText = ConvertEscapedChars (XmlText.substr (innerTextPos, innerTextEnd - innerTextPos));
   85                 nodePos = innerTextEnd;
   86             }
   87 
   88             nodeList.push_back (xmlNode);
   89         }
   90 
   91         return nodeList;
   92     }
   93 
   94     XmlWriter::XmlWriter (const FilePath &fileName)
   95     {
   96         MemOutStream.reset (new wxMemoryOutputStream);
   97         TextOutStream.reset (new wxTextOutputStream (*MemOutStream));
   98         OutFile.Open (fileName, File::CreateWrite);
   99 
  100         *TextOutStream << L"<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl << L"<VeraCrypt>" << endl;
  101         CurrentIndentLevel = 0;
  102     }
  103 
  104     void XmlWriter::Close()
  105     {
  106         if (MemOutStream.get())
  107         {
  108             *TextOutStream << L"</VeraCrypt>" << endl;
  109 
  110             wxStreamBuffer *buf = MemOutStream->GetOutputStreamBuffer();
  111             OutFile.Write (ConstBufferPtr (reinterpret_cast <byte *> (buf->GetBufferStart()), buf->GetBufferSize()));
  112             OutFile.Close();
  113 
  114             TextOutStream.reset();
  115             MemOutStream.reset();
  116         }
  117     }
  118 
  119     wxString XmlWriter::EscapeChars (wxString rawString) const
  120     {
  121         rawString.Replace (L"<", L"&lt;");
  122         rawString.Replace (L">", L"&gt;");
  123         rawString.Replace (L"&", L"&amp;");
  124         rawString.Replace (L"\"", L"&quot;");
  125         return rawString;
  126     }
  127 
  128     void XmlWriter::WriteNode (const XmlNode &xmlNode)
  129     {
  130         XmlNodeList nodes;
  131         nodes.push_back (xmlNode);
  132         WriteNodes (nodes);
  133     }
  134 
  135     void XmlWriter::WriteNodes (const XmlNodeList &xmlNodes)
  136     {
  137         CurrentIndentLevel++;
  138         wxString indent;
  139         for (int i = 0; i < CurrentIndentLevel; ++i)
  140             indent += L"\t";
  141 
  142         foreach (const XmlNode &node, xmlNodes)
  143         {
  144             *TextOutStream << indent << L"<" << node.Name;
  145 
  146             typedef pair <wxString, wxString> AttribPair;
  147             foreach (AttribPair attrib, node.Attributes)
  148             {
  149                 *TextOutStream << L" " << attrib.first << L"=\"" << EscapeChars (attrib.second) << L"\"";
  150             }
  151 
  152             if (!node.InnerNodes.empty())
  153             {
  154                 *TextOutStream << L">" << endl;
  155                 WriteNodes (node.InnerNodes);
  156                 *TextOutStream << indent;
  157             }
  158             else if (!node.InnerText.empty())
  159             {
  160                 *TextOutStream << L">" << EscapeChars (node.InnerText);
  161             }
  162             else
  163             {
  164                 *TextOutStream << L"/>" << endl;
  165                 continue;
  166             }
  167 
  168             *TextOutStream << L"</" << node.Name << L">" << endl;
  169         }
  170 
  171         CurrentIndentLevel--;
  172     }
  173 
  174     XmlWriter::~XmlWriter ()
  175     {
  176         try
  177         {
  178             Close();
  179         }
  180         catch (...) { }
  181     }
  182 }