"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Xml.c" (10 Oct 2018, 4944 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.c" 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 #if !defined(_UEFI)
   13 #include <windows.h>
   14 #include <stdio.h>
   15 #else
   16 #include "Tcdefs.h"
   17 #pragma warning( disable : 4706 )  //  assignment within conditional expression
   18 #endif
   19 #include "Xml.h"
   20 
   21 
   22 static BOOL BeginsWith (char *string, char *subString)
   23 {
   24     while (*string++ == *subString++)
   25     {
   26         if (*subString == 0) return TRUE;
   27         if (*string == 0) return FALSE;
   28     }
   29 
   30     return FALSE;
   31 }
   32 
   33 
   34 char *XmlNextNode (char *xmlNode)
   35 {
   36     char *t = xmlNode + 1;
   37     while ((t = strchr (t, '<')) != NULL)
   38     {
   39         if (t[1] != '/')
   40             return t;
   41 
   42         t++;
   43     }
   44 
   45     return NULL;
   46 }
   47 
   48 
   49 char *XmlFindElement (char *xmlNode, char *nodeName)
   50 {
   51     char *t = xmlNode;
   52     size_t nameLen = strlen (nodeName);
   53 
   54     do
   55     {
   56         if (BeginsWith (t + 1, nodeName)
   57             && (t[nameLen + 1] == '>'
   58             || t[nameLen + 1] == ' ')) return t;
   59 
   60     } while (t = XmlNextNode (t));
   61 
   62     return NULL;
   63 }
   64 
   65 
   66 char *XmlFindElementByAttributeValue (char *xml, char *nodeName, const char *attrName, const char *attrValue)
   67 {
   68     char attr[2048];
   69 
   70     while (xml = XmlFindElement (xml, nodeName))
   71     {
   72         XmlGetAttributeText (xml, attrName, attr, sizeof (attr));
   73         if (strcmp (attr, attrValue) == 0)
   74             return xml;
   75 
   76         xml++;
   77     }
   78 
   79     return NULL;
   80 }
   81 
   82 
   83 char *XmlGetAttributeText (char *xmlNode, const char *xmlAttrName, char *xmlAttrValue, int xmlAttrValueSize)
   84 {
   85     char *t = xmlNode;
   86     char *e = xmlNode;
   87     int l = 0;
   88 
   89     xmlAttrValue[0] = 0;
   90     if (t[0] != '<') return NULL;
   91 
   92     e = strchr (e, '>');
   93     if (e == NULL) return NULL;
   94 
   95     while ((t = strstr (t, xmlAttrName)) && t < e)
   96     {
   97         char *o = t + strlen (xmlAttrName);
   98         if (t[-1] == ' '
   99             &&
  100             (BeginsWith (o, "=\"")
  101             || BeginsWith (o, "= \"")
  102             || BeginsWith (o, " =\"")
  103             || BeginsWith (o, " = \""))
  104             )
  105             break;
  106 
  107         t++;
  108     }
  109 
  110     if (t == NULL || t > e) return NULL;
  111 
  112     t = ((char*)strchr (t, '"')) + 1;
  113     e = strchr (t, '"');
  114     l = (int)(e - t);
  115     if (e == NULL || l > xmlAttrValueSize) return NULL;
  116 
  117     memcpy (xmlAttrValue, t, l);
  118     xmlAttrValue[l] = 0;
  119 
  120     return xmlAttrValue;
  121 }
  122 
  123 
  124 char *XmlGetNodeText (char *xmlNode, char *xmlText, int xmlTextSize)
  125 {
  126     char *t = xmlNode;
  127     char *e = xmlNode + 1;
  128     int l = 0, i = 0, j = 0;
  129 
  130     xmlText[0] = 0;
  131 
  132     if (t[0] != '<')
  133         return NULL;
  134 
  135     t = (char*) strchr (t, '>');
  136     if (t == NULL) return NULL;
  137 
  138     t++;
  139     e = strchr (e, '<');
  140     if (e == NULL) return NULL;
  141 
  142     l = (int)(e - t);
  143     if (e == NULL || l > xmlTextSize) return NULL;
  144 
  145     while (i < l)
  146     {
  147         if (BeginsWith (&t[i], "&lt;"))
  148         {
  149             xmlText[j++] = '<';
  150             i += 4;
  151             continue;
  152         }
  153         if (BeginsWith (&t[i], "&gt;"))
  154         {
  155             xmlText[j++] = '>';
  156             i += 4;
  157             continue;
  158         }
  159         if (BeginsWith (&t[i], "&amp;"))
  160         {
  161             xmlText[j++] = '&';
  162             i += 5;
  163             continue;
  164         }
  165         xmlText[j++] = t[i++];
  166     }
  167     xmlText[j] = 0;
  168 
  169     return t;
  170 }
  171 
  172 
  173 char *XmlQuoteText (const char *textSrc, char *textDst, int textDstMaxSize)
  174 {
  175     char *textDstLast = textDst + textDstMaxSize - 1;
  176 
  177     if (textDstMaxSize == 0)
  178         return NULL;
  179 
  180     while (*textSrc != 0 && textDst <= textDstLast)
  181     {
  182         char c = *textSrc++;
  183         switch (c)
  184         {
  185         case '&':
  186             if (textDst + 6 > textDstLast)
  187                 return NULL;
  188             strcpy (textDst, "&amp;");
  189             textDst += 5;
  190             continue;
  191 
  192         case '>':
  193             if (textDst + 5 > textDstLast)
  194                 return NULL;
  195             strcpy (textDst, "&gt;");
  196             textDst += 4;
  197             continue;
  198 
  199         case '<':
  200             if (textDst + 5 > textDstLast)
  201                 return NULL;
  202             strcpy (textDst, "&lt;");
  203             textDst += 4;
  204             continue;
  205 
  206         default:
  207             *textDst++ = c;
  208         }
  209     }
  210 
  211     if (textDst > textDstLast)
  212         return NULL;
  213 
  214     *textDst = 0;
  215     return textDst;
  216 }
  217 
  218 wchar_t *XmlQuoteTextW (const wchar_t *textSrc, wchar_t *textDst, int textDstMaxSize)
  219 {
  220     wchar_t *textDstLast = textDst + textDstMaxSize - 1;
  221 
  222     if (textDstMaxSize == 0)
  223         return NULL;
  224 
  225     while (*textSrc != 0 && textDst <= textDstLast)
  226     {
  227         wchar_t c = *textSrc++;
  228         switch (c)
  229         {
  230         case L'&':
  231             if (textDst + 6 > textDstLast)
  232                 return NULL;
  233             wcscpy (textDst, L"&amp;");
  234             textDst += 5;
  235             continue;
  236 
  237         case L'>':
  238             if (textDst + 5 > textDstLast)
  239                 return NULL;
  240             wcscpy (textDst, L"&gt;");
  241             textDst += 4;
  242             continue;
  243 
  244         case L'<':
  245             if (textDst + 5 > textDstLast)
  246                 return NULL;
  247             wcscpy (textDst, L"&lt;");
  248             textDst += 4;
  249             continue;
  250 
  251         default:
  252             *textDst++ = c;
  253         }
  254     }
  255 
  256     if (textDst > textDstLast)
  257         return NULL;
  258 
  259     *textDst = 0;
  260     return textDst;
  261 }
  262 
  263 #if !defined(_UEFI)
  264 #pragma warning( default : 4706 )
  265 int XmlWriteHeader (FILE *file)
  266 {
  267     return fputws (L"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<VeraCrypt>", file);
  268 }
  269 
  270 
  271 int XmlWriteFooter (FILE *file)
  272 {
  273     return fputws (L"\n</VeraCrypt>", file);
  274 }
  275 #endif !defined(_UEFI)