summaryrefslogtreecommitdiff
path: root/src/simplexml.c
blob: 0639199589342186ab1c1ca69abcee62d73148e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/** **************************************************************************
 * simplexml.c
 * 
 * Copyright 2008 Bryan Ischo <bryan@ischo.com>
 * 
 * This file is part of libs3.
 * 
 * libs3 is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, version 3 of the License.
 *
 * libs3 is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License version 3
 * along with libs3, in a file named COPYING.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 ************************************************************************** **/

#include <libxml/parser.h>
#include <string.h>
#include "simplexml.h"

// Use libxml2 for parsing XML.  XML is severely overused in modern
// computing.  It is useful for only a very small subset of tasks, but
// software developers who don't know better and are afraid to go against the
// grain use it for everything, and in most cases, it is completely
// inappropriate.  Usually, the document structure is severely under-specified
// as well, as is the case with S3.  We do our best by just caring about the
// most important aspects of the S3 "XML document" responses: the elements and
// their values.  The SAX API (just about the lamest API ever devised and
// proof that XML sucks - well, the real proof is how crappy all of the XML
// parsing libraries are, including libxml2 - but I digress) is used here
// because we don't need much from the parser and SAX is fast and low memory.
//
// Note that for simplicity we assume all ASCII here.  No attempts are made to
// detect non-ASCII sequences in utf-8 and convert them into ASCII in any way.
// S3 appears to only use ASCII anyway.


static xmlEntityPtr saxGetEntity(void *user_data, const xmlChar *name)
{
    return xmlGetPredefinedEntity(name);
}


static void saxStartElement(void *user_data, const xmlChar *nameUtf8,
                            const xmlChar **attr)
{
    SimpleXml *simpleXml = (SimpleXml *) user_data;

    if (simpleXml->status != S3StatusOK) {
        return;
    }
    
    // Assume that name has no non-ASCII in it
    char *name = (char *) nameUtf8;

    // Append the element to the element path
    int len = strlen(name);

    if ((simpleXml->elementPathLen + len + 1) >= 
        sizeof(simpleXml->elementPath)) {
        // Cannot handle this element, stop!
        simpleXml->status = S3StatusXmlParseFailure;
        return;
    }

    if (simpleXml->elementPathLen) {
        simpleXml->elementPath[simpleXml->elementPathLen++] = '/';
    }
    strcpy(&(simpleXml->elementPath[simpleXml->elementPathLen]), name);
    simpleXml->elementPathLen += len;
}


static void saxEndElement(void *user_data, const xmlChar *name)
{
    SimpleXml *simpleXml = (SimpleXml *) user_data;

    if (simpleXml->status != S3StatusOK) {
        return;
    }

    // Call back with 0 data
    simpleXml->status = (*(simpleXml->callback))
        (simpleXml->elementPath, 0, 0, simpleXml->callbackData);

    while ((simpleXml->elementPathLen > 0) &&
           (simpleXml->elementPath[simpleXml->elementPathLen] != '/')) {
        simpleXml->elementPathLen--;
    }

    simpleXml->elementPath[simpleXml->elementPathLen] = 0;
}


static void saxCharacters(void *user_data, const xmlChar *ch, int len)
{
    SimpleXml *simpleXml = (SimpleXml *) user_data;

    if (simpleXml->status != S3StatusOK) {
        return;
    }

    simpleXml->status = (*(simpleXml->callback))
        (simpleXml->elementPath, (char *) ch, len, simpleXml->callbackData);
}


static void saxError(void *user_data, const char *msg, ...)
{
    SimpleXml *simpleXml = (SimpleXml *) user_data;

    if (simpleXml->status != S3StatusOK) {
        return;
    }

    simpleXml->status = S3StatusXmlParseFailure;
}


static struct _xmlSAXHandler saxHandlerG =
{
    0, // internalSubsetSAXFunc
    0, // isStandaloneSAXFunc
    0, // hasInternalSubsetSAXFunc
    0, // hasExternalSubsetSAXFunc
    0, // resolveEntitySAXFunc
    &saxGetEntity, // getEntitySAXFunc
    0, // entityDeclSAXFunc
    0, // notationDeclSAXFunc
    0, // attributeDeclSAXFunc
    0, // elementDeclSAXFunc
    0, // unparsedEntityDeclSAXFunc
    0, // setDocumentLocatorSAXFunc
    0, // startDocumentSAXFunc
    0, // endDocumentSAXFunc
    &saxStartElement, // startElementSAXFunc
    &saxEndElement, // endElementSAXFunc
    0, // referenceSAXFunc
    &saxCharacters, // charactersSAXFunc
    0, // ignorableWhitespaceSAXFunc
    0, // processingInstructionSAXFunc
    0, // commentSAXFunc
    0, // warningSAXFunc
    &saxError, // errorSAXFunc
    &saxError, // fatalErrorSAXFunc
    0, // getParameterEntitySAXFunc
    &saxCharacters, // cdataBlockSAXFunc
    0, // externalSubsetSAXFunc
    0, // initialized
    0, // _private
    0, // startElementNsSAX2Func
    0, // endElementNsSAX2Func
    0 // xmlStructuredErrorFunc serror;
};

void simplexml_initialize(SimpleXml *simpleXml, 
                          SimpleXmlCallback *callback, void *callbackData)
{
    simpleXml->callback = callback;
    simpleXml->callbackData = callbackData;
    simpleXml->elementPathLen = 0;
    simpleXml->status = S3StatusOK;
    simpleXml->xmlParser = 0;
}


void simplexml_deinitialize(SimpleXml *simpleXml)
{
    if (simpleXml->xmlParser) {
        xmlFreeParserCtxt(simpleXml->xmlParser);
    }
}


S3Status simplexml_add(SimpleXml *simpleXml, const char *data, int dataLen)
{
    if (!simpleXml->xmlParser &&
        (!(simpleXml->xmlParser = xmlCreatePushParserCtxt
           (&saxHandlerG, simpleXml, 0, 0, 0)))) {
        return S3StatusInternalError;
    }

    if (xmlParseChunk((xmlParserCtxtPtr) simpleXml->xmlParser, 
                      data, dataLen, 0)) {
        return S3StatusXmlParseFailure;
    }

    return simpleXml->status;
}