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

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

unix.superglobalmegacorp.com

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