GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/xml/libxml.cpp Lines: 111 136 81.6 %
Date: 2021-03-17 Branches: 51 134 38.1 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifdef ENABLE_LIBXML
25
26
#include "utils/xml/libxml.h"
27
28
#include "fs/virtfs/fs.h"
29
30
#include "utils/cast.h"
31
#include "utils/checkutils.h"
32
#include "utils/fuzzer.h"
33
#include "utils/stringutils.h"
34
35
#include "utils/translation/podict.h"
36
37
#include <fstream>
38
39
#include "debug.h"
40
41
namespace
42
{
43
    bool valid = false;
44
}  // namespace
45
46
static void xmlErrorLogger(void *ctx A_UNUSED, const char *msg A_UNUSED, ...)
47
#ifdef __GNUC__
48
#ifdef __OpenBSD__
49
    __attribute__((__format__(printf, 2, 3)))
50
#else  // __OpenBSD__
51
    __attribute__((__format__(gnu_printf, 2, 3)))
52
#endif  // __OpenBSD__
53
#endif  // __GNUC__
54
;
55
56
6
static void xmlErrorLogger(void *ctx A_UNUSED, const char *msg, ...)
57
{
58
6
    if (msg == nullptr)
59
        return;
60
61
6
    size_t size = 1024;
62
6
    const size_t msgSize = strlen(msg);
63
6
    if (msgSize * 3 > size)
64
        size = msgSize * 3;
65
66
6
    char* buf = new char[size + 1];
67
    va_list ap;
68
69
    // Use a temporary buffer to fill in the variables
70
6
    va_start(ap, msg);
71
6
    vsnprintf(buf, size, msg, ap);
72
6
    buf[size] = 0;
73
6
    va_end(ap);
74
75
6
    if (logger != nullptr)
76
6
        logger->log_r("%s", buf);
77
    else
78
        puts(buf);
79
80
    // Delete temporary buffer
81
6
    delete [] buf;
82
6
    valid = false;
83
}
84
85
namespace XML
86
{
87
2820
    Document::Document(const std::string &filename,
88
                       const UseVirtFs useResman,
89
2820
                       const SkipError skipError) :
90
        Resource(),
91
        mDoc(nullptr),
92
5640
        mIsValid(false)
93
    {
94
#ifdef USE_FUZZER
95
        if (Fuzzer::conditionTerminate(filename.c_str()))
96
            return;
97
#endif  // USE_FUZZER
98
99
        BLOCK_START("XML::Document::Document")
100
2820
        int size = 0;
101
2820
        char *data = nullptr;
102
2820
        valid = true;
103
2820
        if (useResman == UseVirtFs_true)
104
        {
105
4894
            data = const_cast<char*>(VirtFs::loadFile(
106
                filename,
107
2447
                size));
108
        }
109
        else
110
        {
111
746
            std::ifstream file;
112
373
            file.open(filename.c_str(), std::ios::in);
113
114
373
            if (file.is_open())
115
            {
116
                // Get length of file
117
373
                file.seekg(0, std::ios::end);
118
373
                size = CAST_S32(file.tellg());
119
373
                if (size < 0)
120
                {
121
                    reportAlways("Error loading XML file %s",
122
                        filename.c_str())
123
                }
124
                else
125
                {
126
373
                    file.seekg(0, std::ios::beg);
127
373
                    data = new char[size];
128
373
                    file.read(data, size);
129
                }
130
373
                file.close();
131
            }
132
            else if (skipError == SkipError_false)
133
            {
134
                reportAlways("Error loading XML file %s",
135
                    filename.c_str())
136
            }
137
        }
138
139
2820
        if (data != nullptr)
140
        {
141
2739
            mDoc = xmlParseMemory(data, size);
142
2739
            delete [] data;
143
144
2739
            if (mDoc == nullptr)
145
            {
146
                reportAlways("Error parsing XML file %s", filename.c_str())
147
            }
148
        }
149
81
        else if (skipError == SkipError_false)
150
        {
151
            reportAlways("Error loading XML file %s", filename.c_str())
152
        }
153
2820
        mIsValid = valid;
154
        BLOCK_END("XML::Document::Document")
155
2820
    }
156
157
5
    Document::Document(const char *const data, const int size) :
158

5
        mDoc(data != nullptr ? xmlParseMemory(data, size) : nullptr),
159
15
        mIsValid(true)
160
    {
161
5
    }
162
163
7908
    Document::~Document()
164
    {
165
2825
        if (mDoc != nullptr)
166
2744
            xmlFreeDoc(mDoc);
167
5083
    }
168
169
3259
    XmlNodePtr Document::rootNode()
170
    {
171
3259
        return mDoc != nullptr ? xmlDocGetRootElement(mDoc) : nullptr;
172
    }
173
174
46123
    int getProperty(XmlNodeConstPtr node,
175
                    const char *const name,
176
                    int def)
177
    {
178
46123
        int &ret = def;
179
180
46123
        xmlChar *const prop = XmlGetProp(node, name);
181
46123
        if (prop != nullptr)
182
        {
183
32760
            ret = atoi(reinterpret_cast<const char*>(prop));
184
32760
            xmlFree(prop);
185
        }
186
187
46123
        return ret;
188
    }
189
190
8801
    int getIntProperty(XmlNodeConstPtr node,
191
                       const char *const name,
192
                       int def,
193
                       const int min,
194
                       const int max)
195
    {
196
8801
        int &ret = def;
197
198
8801
        xmlChar *const prop = XmlGetProp(node, name);
199
8801
        if (prop != nullptr)
200
        {
201
1256
            ret = atoi(reinterpret_cast<char*>(prop));
202
1256
            xmlFree(prop);
203
        }
204
8801
        if (ret < min)
205
            ret = min;
206
8801
        else if (ret > max)
207
            ret = max;
208
8801
        return ret;
209
    }
210
211
110
    float getFloatProperty(XmlNodeConstPtr node,
212
                           const char *const name,
213
                           float def)
214
    {
215
110
        float &ret = def;
216
217
110
        xmlChar *const prop = XmlGetProp(node, name);
218
110
        if (prop != nullptr)
219
        {
220
2
            ret = static_cast<float>(atof(reinterpret_cast<char*>(prop)));
221
2
            xmlFree(prop);
222
        }
223
224
110
        return ret;
225
    }
226
227
    double getDoubleProperty(XmlNodeConstPtr node,
228
                             const char *const name,
229
                             double def)
230
    {
231
        double &ret = def;
232
233
        xmlChar *const prop = XmlGetProp(node, name);
234
        if (prop != nullptr)
235
        {
236
            ret = atof(reinterpret_cast<char*>(prop));
237
            xmlFree(prop);
238
        }
239
240
        return ret;
241
    }
242
243
429104
    std::string getProperty(XmlNodeConstPtr node,
244
                            const char *const name,
245
                            const std::string &def)
246
    {
247
429104
        xmlChar *const prop = XmlGetProp(node, name);
248
429104
        if (prop != nullptr)
249
        {
250
1310580
            std::string val = reinterpret_cast<char*>(prop);
251
327645
            xmlFree(prop);
252
327645
            return val;
253
        }
254
255
        return def;
256
    }
257
258
189
    std::string langProperty(XmlNodeConstPtr node,
259
                             const char *const name,
260
                             const std::string &def)
261
    {
262
378
        std::string str = getProperty(node, name, def);
263
189
        if (translator == nullptr)
264
            return str;
265
266
186
        return translator->getStr(str);
267
    }
268
269
45
    bool getBoolProperty(XmlNodeConstPtr node,
270
                         const char *const name,
271
                         const bool def)
272
    {
273
45
        xmlChar *const prop = XmlGetProp(node, name);
274
275
45
        if (XmlStrEqual(prop, "true"))
276
        {
277
2
            XmlFree(prop);
278
2
            return true;
279
        }
280
43
        if (XmlStrEqual(prop, "false"))
281
        {
282
2
            XmlFree(prop);
283
2
            return false;
284
        }
285
41
        XmlFree(prop);
286
41
        return def;
287
    }
288
289
25
    XmlNodePtr findFirstChildByName(XmlNodeConstPtrConst parent,
290
                                    const char *const name)
291
    {
292
25
        if (parent == nullptr)
293
            return nullptr;
294
229
        for_each_xml_child_node(child, parent)
295
        {
296
229
            if (xmlNameEqual(child, name))
297
                return child;
298
        }
299
300
        return nullptr;
301
    }
302
303
    // Initialize libxml2 and check for potential ABI mismatches between
304
    // compiled version and the shared library actually used.
305
184
    void initXML()
306
    {
307
184
        xmlInitParser();
308
184
        LIBXML_TEST_VERSION
309
310
        // Suppress libxml2 error messages
311
184
        xmlSetGenericErrorFunc(nullptr, &xmlErrorLogger);
312
184
    }
313
314
    // Shutdown libxml
315
204
    void cleanupXML()
316
    {
317
204
        xmlCleanupParser();
318
204
    }
319
320
2
    bool Document::validateXml(const std::string &fileName)
321
    {
322
2
        const xmlDocPtr doc = xmlReadFile(fileName.c_str(),
323
2
            nullptr, XML_PARSE_PEDANTIC);
324
2
        const bool valid1(doc != nullptr);
325
2
        xmlFreeDoc(doc);
326
2
        if (!valid1)
327
            return false;
328
329
        std::ifstream file;
330
        file.open(fileName.c_str(), std::ios::in);
331
        if (!file.is_open())
332
        {
333
            file.close();
334
            return false;
335
        }
336
        char line[101];
337
        if (!file.getline(line, 100))
338
            return false;
339
        file.close();
340
341
        const std::string str = line;
342
        if (!strStartWith(str, "<?xml "))
343
            return false;
344
345
        return true;
346
    }
347
}  // namespace XML
348
349
#endif  // ENABLE_LIBXML