Annotation of truecrypt/common/xml.c, revision 1.1.1.9

1.1.1.4   root        1: /*
1.1.1.6   root        2:  Copyright (c) 2005 TrueCrypt Foundation. All rights reserved.
1.1       root        3: 
1.1.1.9 ! root        4:  Governed by the TrueCrypt License 2.7 the full text of which is contained
1.1.1.4   root        5:  in the file License.txt included in TrueCrypt binary and source code
                      6:  distribution packages.
1.1       root        7: */
                      8: 
                      9: #include <windows.h>
                     10: #include <stdio.h>
                     11: #include "Xml.h"
                     12: 
                     13: 
                     14: static BOOL BeginsWith (char *string, char *subString)
                     15: {
                     16:        while (*string++ == *subString++)
                     17:        {
                     18:                if (*subString == 0) return TRUE;
                     19:                if (*string == 0) return FALSE;
                     20:        }
                     21: 
                     22:        return FALSE;
                     23: }
                     24: 
                     25: 
                     26: char *XmlNextNode (char *xmlNode)
                     27: {
                     28:        char *t = xmlNode + 1;
                     29:        while ((t = strchr (t, '<')) != NULL)
                     30:        {
                     31:                if (t[1] != '/')
                     32:                        return t;
                     33: 
                     34:                t++;
                     35:        }
                     36: 
                     37:        return NULL;
                     38: }
                     39: 
                     40: 
                     41: char *XmlFindElement (char *xmlNode, char *nodeName)
                     42: {
                     43:        char *t = xmlNode;
                     44:        size_t nameLen = strlen (nodeName);
                     45: 
                     46:        do
                     47:        {
                     48:                if (BeginsWith (t + 1, nodeName)
                     49:                        && (t[nameLen + 1] == '>'
                     50:                        || t[nameLen + 1] == ' ')) return t;
                     51: 
                     52:        } while (t = XmlNextNode (t));
                     53: 
                     54:        return NULL;
                     55: }
                     56: 
                     57: 
                     58: char *XmlFindElementByAttributeValue (char *xml, char *nodeName, char *attrName, char *attrValue)
                     59: {
                     60:        char attr[2048];
                     61: 
                     62:        while (xml = XmlFindElement (xml, nodeName))
                     63:        {
1.1.1.4   root       64:                XmlGetAttributeText (xml, attrName, attr, sizeof (attr));
1.1       root       65:                if (strcmp (attr, attrValue) == 0)
                     66:                        return xml;
                     67: 
                     68:                xml++;
                     69:        }
                     70: 
                     71:        return NULL;
                     72: }
                     73: 
                     74: 
1.1.1.4   root       75: char *XmlGetAttributeText (char *xmlNode, char *xmlAttrName, char *xmlAttrValue, int xmlAttrValueSize)
1.1       root       76: {
                     77:        char *t = xmlNode;
                     78:        char *e = xmlNode;
                     79:        int l = 0;
                     80: 
                     81:        xmlAttrValue[0] = 0;
                     82:        if (t[0] != '<') return NULL;
                     83: 
                     84:        e = strchr (e, '>');
                     85:        if (e == NULL) return NULL;
                     86: 
                     87:        while ((t = strstr (t, xmlAttrName)) && t < e)
                     88:        {
                     89:                char *o = t + strlen (xmlAttrName);
                     90:                if (t[-1] == ' '
                     91:                        &&
                     92:                        (BeginsWith (o, "=\"")
                     93:                        || BeginsWith (o, "= \"")
                     94:                        || BeginsWith (o, " =\"")
                     95:                        || BeginsWith (o, " = \""))
                     96:                        )
                     97:                        break;
                     98: 
                     99:                t++;
                    100:        }
                    101: 
                    102:        if (t == NULL || t > e) return NULL;
                    103: 
                    104:        t = strchr (t, '"') + 1;
                    105:        e = strchr (t, '"');
                    106:        l = (int)(e - t);
                    107:        if (e == NULL || l > xmlAttrValueSize) return NULL;
                    108: 
                    109:        memcpy (xmlAttrValue, t, l);
                    110:        xmlAttrValue[l] = 0;
                    111: 
                    112:        return xmlAttrValue;
                    113: }
                    114: 
                    115: 
1.1.1.4   root      116: char *XmlGetNodeText (char *xmlNode, char *xmlText, int xmlTextSize)
1.1       root      117: {
                    118:        char *t = xmlNode;
                    119:        char *e = xmlNode + 1;
                    120:        int l = 0, i = 0, j = 0;
                    121: 
                    122:        xmlText[0] = 0;
                    123: 
                    124:        if (t[0] != '<')
                    125:                return NULL;
                    126: 
                    127:        t = strchr (t, '>') + 1;
1.1.1.2   root      128:        if (t == (char *)1) return NULL;
1.1       root      129: 
                    130:        e = strchr (e, '<');
                    131:        if (e == NULL) return NULL;
                    132: 
                    133:        l = (int)(e - t);
                    134:        if (e == NULL || l > xmlTextSize) return NULL;
                    135: 
                    136:        while (i < l)
                    137:        {
                    138:                if (BeginsWith (&t[i], "&lt;"))
                    139:                {
                    140:                        xmlText[j++] = '<';
                    141:                        i += 4;
                    142:                        continue;
                    143:                }
                    144:                if (BeginsWith (&t[i], "&gt;"))
                    145:                {
                    146:                        xmlText[j++] = '>';
                    147:                        i += 4;
                    148:                        continue;
                    149:                }
                    150:                if (BeginsWith (&t[i], "&amp;"))
                    151:                {
                    152:                        xmlText[j++] = '&';
                    153:                        i += 5;
                    154:                        continue;
                    155:                }
                    156:                xmlText[j++] = t[i++];
                    157:        }
                    158:        xmlText[j] = 0;
                    159: 
                    160:        return t;
                    161: }
                    162: 
                    163: 
1.1.1.4   root      164: char *XmlQuoteText (char *textSrc, char *textDst, int textDstMaxSize)
                    165: {
                    166:        char *textDstLast = textDst + textDstMaxSize - 1;
                    167: 
                    168:        if (textDstMaxSize == 0)
                    169:                return NULL;
                    170: 
                    171:        while (*textSrc != 0 && textDst <= textDstLast) 
                    172:        {
                    173:                char c = *textSrc++;
                    174:                switch (c)
                    175:                {
                    176:                case '&':
                    177:                        if (textDst + 6 > textDstLast)
                    178:                                return NULL;
                    179:                        strcpy (textDst, "&amp;");
                    180:                        textDst += 5;
                    181:                        continue;
                    182: 
                    183:                case '>':
                    184:                        if (textDst + 5 > textDstLast)
                    185:                                return NULL;
                    186:                        strcpy (textDst, "&gt;");
                    187:                        textDst += 4;
                    188:                        continue;
                    189: 
                    190:                case '<':
                    191:                        if (textDst + 5 > textDstLast)
                    192:                                return NULL;
                    193:                        strcpy (textDst, "&lt;");
                    194:                        textDst += 4;
                    195:                        continue;
                    196: 
                    197:                default:
                    198:                        *textDst++ = c;
                    199:                }
                    200:        }
                    201: 
                    202:        if (textDst > textDstLast)
                    203:                return NULL;
                    204: 
                    205:        *textDst = 0;
                    206:        return textDst;
                    207: }
                    208: 
                    209: 
1.1       root      210: int XmlWriteHeader (FILE *file)
                    211: {
1.1.1.5   root      212:        return fputs ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<TrueCrypt>", file);
1.1       root      213: }
                    214: 
                    215: 
                    216: int XmlWriteFooter (FILE *file)
                    217: {
                    218:        return fputs ("\n</TrueCrypt>", file);
                    219: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.