Coverage Report

Created: 2025-12-31 07:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vlc/modules/misc/xml/libxml.c
Line
Count
Source
1
/*****************************************************************************
2
 * libxml.c: XML parser using libxml2
3
 *****************************************************************************
4
 * Copyright (C) 2004 VLC authors and VideoLAN
5
 *
6
 * Authors: Gildas Bazin <gbazin@videolan.org>
7
 *
8
 * This program is free software; you can redistribute it and/or modify it
9
 * under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program; if not, write to the Free Software Foundation,
20
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21
 *****************************************************************************/
22
23
#ifdef HAVE_CONFIG_H
24
# include "config.h"
25
#endif
26
27
#include <vlc_common.h>
28
#include <vlc_plugin.h>
29
30
#include <vlc_block.h>
31
#include <vlc_stream.h>
32
#include <vlc_xml.h>
33
34
#include <libxml/xmlreader.h>
35
#include <libxml/catalog.h>
36
37
/*****************************************************************************
38
 * Catalogue functions
39
 *****************************************************************************/
40
static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
41
0
{
42
0
    VLC_UNUSED(p_xml);
43
0
    if( !psz_filename ) xmlInitializeCatalog();
44
0
    else xmlLoadCatalog( psz_filename );
45
0
}
46
47
static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
48
                          const char *psz_arg2, const char *psz_filename )
49
0
{
50
0
    VLC_UNUSED(p_xml);
51
0
    xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
52
0
        (unsigned char*)psz_filename );
53
0
}
54
55
static vlc_mutex_t lock = VLC_STATIC_MUTEX;
56
57
/*****************************************************************************
58
 * Module initialization
59
 *****************************************************************************/
60
static int Open( vlc_object_t *p_this )
61
1.05k
{
62
1.05k
    xml_t *p_xml = (xml_t *)p_this;
63
64
1.05k
    if( !xmlHasFeature( XML_WITH_THREAD ) )
65
0
        return VLC_EGENERIC;
66
67
1.05k
    vlc_mutex_lock( &lock );
68
1.05k
    xmlInitParser();
69
1.05k
    vlc_mutex_unlock( &lock );
70
71
1.05k
    p_xml->pf_catalog_load = CatalogLoad;
72
1.05k
    p_xml->pf_catalog_add  = CatalogAdd;
73
74
1.05k
    return VLC_SUCCESS;
75
1.05k
}
76
77
/*****************************************************************************
78
 * Module deinitialization
79
 *****************************************************************************/
80
static void Close( vlc_object_t *p_this )
81
1.05k
{
82
    /* /!\
83
     * In theory, xmlCleanupParser() should be called here.
84
     * Unfortunately that function is not thread-safe,
85
     * operating on global state. So even if we would be
86
     * able to know when this module is unloaded, we could
87
     * still not call it then, as other libraries or the apps
88
     * using libVLC could still use libxml themselves.
89
     *
90
     * Citing the libxml docs for xmlCleanupParser:
91
     *
92
     * > If your application is multithreaded or has plugin support
93
     * > calling this may crash the application if another thread or
94
     * > a plugin is still using libxml2. It's sometimes very hard to
95
     * > guess if libxml2 is in use in the application, some libraries
96
     * > or plugins may use it without notice. In case of doubt abstain
97
     * > from calling this function or do it just before calling exit()
98
     * > to avoid leak reports from valgrind!
99
     */
100
1.05k
    VLC_UNUSED(p_this);
101
1.05k
}
102
103
/*****************************************************************************
104
 * Reader functions
105
 *****************************************************************************/
106
static void ReaderErrorHandler( void *p_arg, const char *p_msg,
107
                                xmlParserSeverities severity,
108
                                xmlTextReaderLocatorPtr locator)
109
17.6k
{
110
17.6k
    VLC_UNUSED(severity);
111
17.6k
    xml_reader_t *p_reader = (xml_reader_t *)p_arg;
112
17.6k
    int line = xmlTextReaderLocatorLineNumber( locator );
113
17.6k
    msg_Err( p_reader, "XML parser error (line %d) : %s", line, p_msg );
114
17.6k
}
115
116
typedef struct
117
{
118
    xmlTextReaderPtr xml;
119
    char *node;
120
    char *namespace;
121
} xml_reader_sys_t;
122
123
static int ReaderUseDTD ( xml_reader_t *p_reader )
124
0
{
125
    /* Activate DTD validation */
126
0
    xml_reader_sys_t *p_sys = p_reader->p_sys;
127
0
    xmlTextReaderSetParserProp( p_sys->xml,
128
0
                                XML_PARSER_DEFAULTATTRS, true );
129
0
    xmlTextReaderSetParserProp( p_sys->xml,
130
0
                                XML_PARSER_VALIDATE, true );
131
132
0
    return VLC_SUCCESS;
133
0
}
134
135
static int ReaderNextNode( xml_reader_t *p_reader, const char **pval,
136
                             const char **pnamespace )
137
906k
{
138
906k
    xml_reader_sys_t *p_sys = p_reader->p_sys;
139
906k
    const xmlChar *node, *namespace = NULL;
140
906k
    int ret;
141
142
928k
    skip:
143
928k
    free( p_sys->node );
144
928k
    free( p_sys->namespace );
145
928k
    p_sys->node = NULL;
146
928k
    p_sys->namespace = NULL;
147
148
928k
    switch( xmlTextReaderRead( p_sys->xml ) )
149
928k
    {
150
20.4k
        case 0: /* EOF */
151
20.4k
            return XML_READER_NONE;
152
9.85k
        case -1: /* error */
153
9.85k
            return XML_READER_ERROR;
154
928k
    }
155
156
898k
    switch( xmlTextReaderNodeType( p_sys->xml ) )
157
898k
    {
158
461k
        case XML_READER_TYPE_ELEMENT:
159
461k
            node = xmlTextReaderConstName( p_sys->xml );
160
461k
            namespace = xmlTextReaderConstNamespaceUri( p_sys->xml );
161
461k
            ret = XML_READER_STARTELEM;
162
461k
            break;
163
164
370k
        case XML_READER_TYPE_END_ELEMENT:
165
370k
            node = xmlTextReaderConstName( p_sys->xml );
166
370k
            namespace = xmlTextReaderConstNamespaceUri( p_sys->xml );
167
370k
            ret = XML_READER_ENDELEM;
168
370k
            break;
169
170
0
        case XML_READER_TYPE_CDATA:
171
44.0k
        case XML_READER_TYPE_TEXT:
172
44.0k
            node = xmlTextReaderConstValue( p_sys->xml );
173
44.0k
            ret = XML_READER_TEXT;
174
44.0k
            break;
175
176
0
        case -1:
177
0
            return XML_READER_ERROR;
178
179
21.7k
        default:
180
21.7k
            goto skip;
181
898k
    }
182
183
876k
    if( unlikely(node == NULL) )
184
0
        return XML_READER_ERROR;
185
186
876k
    p_sys->node = strdup( (const char *) node );
187
876k
    if( namespace )
188
797k
        p_sys->namespace = strdup( (const char *) namespace );
189
876k
    if( pval != NULL )
190
876k
        *pval = p_sys->node;
191
876k
    if( pnamespace != NULL )
192
876k
        *pnamespace = p_sys->namespace;
193
876k
    return likely(p_sys->node != NULL) ? ret : XML_READER_ERROR;
194
876k
}
195
196
static const char *ReaderNextAttr( xml_reader_t *p_reader, const char **pval,
197
                                     const char **pnamespace )
198
1.20M
{
199
1.20M
    xml_reader_sys_t *p_sys = p_reader->p_sys;
200
1.20M
    xmlTextReaderPtr xml = p_sys->xml;
201
1.20M
    const xmlChar *name, *value;
202
203
1.20M
    if( xmlTextReaderMoveToNextAttribute( xml ) != 1
204
744k
     || (name = xmlTextReaderConstName( xml )) == NULL
205
744k
     || (value = xmlTextReaderConstValue( xml )) == NULL )
206
461k
        return NULL;
207
208
744k
    if( pnamespace )
209
744k
        *pnamespace = (const char *) xmlTextReaderConstNamespaceUri( xml );
210
211
744k
    *pval = (const char *)value;
212
744k
    return (const char *) name;
213
1.20M
}
214
215
static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
216
46.7k
{
217
46.7k
    stream_t *s = (stream_t*)p_context;
218
46.7k
    return vlc_stream_Read( s, p_buffer, i_buffer );
219
46.7k
}
220
221
static int ReaderIsEmptyElement( xml_reader_t *p_reader )
222
877k
{
223
877k
    xml_reader_sys_t *p_sys = p_reader->p_sys;
224
877k
    return xmlTextReaderIsEmptyElement( p_sys->xml );
225
877k
}
226
227
static int ReaderOpen( vlc_object_t *p_this )
228
20.2k
{
229
20.2k
    if( !xmlHasFeature( XML_WITH_THREAD ) )
230
0
        return VLC_EGENERIC;
231
232
20.2k
    xml_reader_t *p_reader = (xml_reader_t *)p_this;
233
20.2k
    xml_reader_sys_t *p_sys = malloc( sizeof( *p_sys ) );
234
20.2k
    xmlTextReaderPtr p_libxml_reader;
235
236
20.2k
    if( unlikely(!p_sys) )
237
0
        return VLC_ENOMEM;
238
239
20.2k
    vlc_mutex_lock( &lock );
240
20.2k
    xmlInitParser();
241
20.2k
    vlc_mutex_unlock( &lock );
242
243
20.2k
    p_libxml_reader = xmlReaderForIO( StreamRead, NULL, p_reader->p_stream,
244
20.2k
                                      NULL, NULL, 0 );
245
20.2k
    if( !p_libxml_reader )
246
0
    {
247
0
        free( p_sys );
248
        /* /!\
249
         * xmlCleanupParser should but can't be called here,
250
         * for the same reason as in Close().
251
         */
252
0
        return VLC_ENOMEM;
253
0
    }
254
255
    /* Set the error handler */
256
20.2k
    xmlTextReaderSetErrorHandler( p_libxml_reader,
257
20.2k
                                  ReaderErrorHandler, p_reader );
258
259
20.2k
    p_sys->xml = p_libxml_reader;
260
20.2k
    p_sys->node = NULL;
261
20.2k
    p_sys->namespace = NULL;
262
20.2k
    p_reader->p_sys = p_sys;
263
20.2k
    p_reader->pf_next_node = ReaderNextNode;
264
20.2k
    p_reader->pf_next_attr = ReaderNextAttr;
265
20.2k
    p_reader->pf_is_empty = ReaderIsEmptyElement;
266
20.2k
    p_reader->pf_use_dtd = ReaderUseDTD;
267
268
20.2k
    return VLC_SUCCESS;
269
20.2k
}
270
271
static void ReaderClose( vlc_object_t *p_this )
272
20.2k
{
273
20.2k
    xml_reader_t *p_reader = (xml_reader_t *)p_this;
274
20.2k
    xml_reader_sys_t *p_sys = p_reader->p_sys;
275
276
20.2k
    xmlFreeTextReader( p_sys->xml );
277
20.2k
    free( p_sys->node );
278
20.2k
    free( p_sys->namespace );
279
20.2k
    free( p_sys );
280
281
    /* /!\
282
     * xmlCleanupParser should but can't be called here,
283
     * same reason as in Close() of the main xml module.
284
     */
285
20.2k
}
286
287
104
vlc_module_begin ()
288
52
    set_description( N_("XML Parser (using libxml2)") )
289
52
    set_capability( "xml", 10 )
290
104
    set_callbacks( Open, Close )
291
292
#ifdef _WIN32
293
    cannot_unload_broken_library()
294
#endif
295
296
52
    add_submodule()
297
52
    set_capability( "xml reader", 10 )
298
104
    set_callbacks( ReaderOpen, ReaderClose )
299
300
52
vlc_module_end ()