LibreOffice Module xmlsecurity (master) 1
saxhelper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <sal/config.h>
21
22#include <string_view>
23
24#include <rtl/ustring.hxx>
25
26#include <xmlsec/saxhelper.hxx>
27#include <libxml/parserInternals.h>
28
29#include <com/sun/star/xml/csax/XMLAttribute.hpp>
30#include <com/sun/star/uno/Sequence.hxx>
31
32#ifndef XMLSEC_NO_XSLT
33#include "libxslt/xslt.h"
34#endif
35
40static xmlChar* ous_to_xmlstr( std::u16string_view oustr )
41{
42 OString ostr = OUStringToOString( oustr , RTL_TEXTENCODING_UTF8 ) ;
43 return xmlStrndup( reinterpret_cast<xmlChar const *>(ostr.getStr()), static_cast<int>(ostr.getLength()) ) ;
44}
45
50static xmlChar* ous_to_nxmlstr( std::u16string_view oustr, int& length )
51{
52 OString ostr = OUStringToOString( oustr , RTL_TEXTENCODING_UTF8 ) ;
53 length = ostr.getLength();
54
55 return xmlStrndup( reinterpret_cast<xmlChar const *>(ostr.getStr()), length ) ;
56}
57
62static const xmlChar** attrlist_to_nxmlstr( const css::uno::Sequence< css::xml::csax::XMLAttribute >& aAttributes )
63{
64 xmlChar* attname = nullptr ;
65 xmlChar* attvalue = nullptr ;
66 const xmlChar** attrs = nullptr ;
67
68 sal_Int32 nLength = aAttributes.getLength();
69
70 if( nLength != 0 )
71 {
72 attrs = static_cast<const xmlChar**>(xmlMalloc( ( nLength * 2 + 2 ) * sizeof( xmlChar* ) ));
73 }
74 else
75 {
76 return nullptr ;
77 }
78
79 int i = 0;
80 for( const auto& rAttr : aAttributes )
81 {
82 attname = ous_to_xmlstr( rAttr.sName ) ;
83 attvalue = ous_to_xmlstr( rAttr.sValue ) ;
84
85 if( attname != nullptr && attvalue != nullptr )
86 {
87 attrs[i++] = attname ;
88 attrs[i++] = attvalue ;
89 attrs[i] = nullptr ;
90 attrs[i+1] = nullptr ;
91 }
92 else
93 {
94 if( attname != nullptr )
95 xmlFree( attname ) ;
96 if( attvalue != nullptr )
97 xmlFree( attvalue ) ;
98 }
99 }
100
101 return attrs ;
102}
103
111 : m_pParserCtxt( nullptr ),
112 m_pSaxHandler( nullptr )
113{
114 xmlInitParser() ;
115 LIBXML_TEST_VERSION ;
116
117 /*
118 * compile error:
119 * xmlLoadExtDtdDefaultValue = XML_DETECT_IDS | XML_COMPLETE_ATTRS ;
120 */
121 xmlSubstituteEntitiesDefault(0) ;
122
123#ifndef XMLSEC_NO_XSLT
124 xmlIndentTreeOutput = 1 ;
125#endif /* XMLSEC_NO_XSLT */
126
127 m_pParserCtxt = xmlNewParserCtxt() ;
128
129 if( m_pParserCtxt == nullptr )
130 {
131// see issue i74334, we cannot call xmlCleanupParser when libxml is still used
132// in other parts of the office.
133// xmlCleanupParser() ;
134// and neither can we call xsltCleanupGlobals()
135 throw css::uno::RuntimeException() ;
136 }
137
138 xmlSAXVersion(m_pParserCtxt->sax, 1);
139
140 if (m_pParserCtxt->inputTab != nullptr)
141 {
142 m_pParserCtxt->inputTab[0] = nullptr ;
143 }
144
145 if( m_pParserCtxt->sax == nullptr )
146 {
147 xmlFreeParserCtxt( m_pParserCtxt ) ;
148
149// see issue i74334, we cannot call xmlCleanupParser when libxml is still used
150// in other parts of the office.
151// xmlCleanupParser() ;
152// and neither can we call xsltCleanupGlobals()
153 m_pParserCtxt = nullptr ;
154 throw css::uno::RuntimeException() ;
155 }
156 else
157 {
159
160 //Adjust the context
161 m_pParserCtxt->recovery = 1 ;
162 }
163}
164
174 if( m_pParserCtxt != nullptr )
175 {
176 /*
177 * In the situation that no object refer the Document, this destructor
178 * must deallocate the Document memory
179 */
180 if( m_pSaxHandler == m_pParserCtxt->sax )
181 {
182 m_pSaxHandler = nullptr ;
183 }
184
185 xmlFreeParserCtxt( m_pParserCtxt ) ;
186 m_pParserCtxt = nullptr ;
187 }
188
189 if( m_pSaxHandler != nullptr )
190 {
191 xmlFree( m_pSaxHandler ) ;
192 m_pSaxHandler = nullptr ;
193 }
194// see issue i74334, we cannot call xmlCleanupParser when libxml is still used
195// in other parts of the office.
196// xmlCleanupParser() ;
197}
198
199
200void SAXHelper::setCurrentNode(const xmlNodePtr pNode)
201{
202 /*
203 * This is really a black trick.
204 * When the current node is replaced, the nodeTab
205 * stack's top has to been replaced with the same
206 * node, in order to make compatibility.
207 */
208 m_pParserCtxt->nodeTab[m_pParserCtxt->nodeNr - 1]
209 = m_pParserCtxt->node
210 = pNode;
211}
212
213
218{
219 if( m_pParserCtxt == nullptr)
220 {
221 throw css::uno::RuntimeException() ;
222 }
223 /*
224 * Adjust inputTab
225 */
226 xmlParserInputPtr pInput = xmlNewInputStream( m_pParserCtxt ) ;
227
228 if( m_pParserCtxt->inputTab != nullptr && m_pParserCtxt->inputMax != 0 )
229 {
230 m_pParserCtxt->inputTab[0] = pInput ;
231 m_pParserCtxt->input = pInput ;
232 }
233
234 m_pSaxHandler->startDocument( m_pParserCtxt ) ;
235
236 if( m_pParserCtxt->myDoc == nullptr )
237 {
238 throw css::uno::RuntimeException() ;
239 }
240}
241
246{
247 m_pSaxHandler->endDocument( m_pParserCtxt ) ;
248}
249
254 std::u16string_view aName,
255 const css::uno::Sequence< css::xml::csax::XMLAttribute >& aAttributes )
256{
257 const xmlChar* fullName = nullptr ;
258 const xmlChar** attrs = nullptr ;
259
260 fullName = ous_to_xmlstr( aName ) ;
261 attrs = attrlist_to_nxmlstr( aAttributes ) ;
262
263 if( fullName != nullptr || attrs != nullptr )
264 {
265 m_pSaxHandler->startElement( m_pParserCtxt , fullName , attrs ) ;
266 }
267
268 if( fullName != nullptr )
269 {
270 xmlFree( const_cast<xmlChar*>(fullName) ) ;
271 fullName = nullptr ;
272 }
273
274 if( attrs != nullptr )
275 {
276 for( int i = 0 ; attrs[i] != nullptr ; ++i )
277 {
278 xmlFree( const_cast<xmlChar*>(attrs[i]) ) ;
279 attrs[i] = nullptr ;
280 }
281
282 xmlFree( static_cast<void*>(attrs) ) ;
283 attrs = nullptr ;
284 }
285}
286
290void SAXHelper::endElement( std::u16string_view aName )
291{
292 xmlChar* fullname = ous_to_xmlstr( aName ) ;
293 m_pSaxHandler->endElement( m_pParserCtxt , fullname ) ;
294
295 if( fullname != nullptr )
296 {
297 xmlFree( fullname ) ;
298 fullname = nullptr ;
299 }
300}
301
305void SAXHelper::characters( std::u16string_view aChars )
306{
307 const xmlChar* chars = nullptr ;
308 int length = 0 ;
309
310 chars = ous_to_nxmlstr( aChars, length ) ;
311 m_pSaxHandler->characters( m_pParserCtxt , chars , length ) ;
312
313 if( chars != nullptr )
314 {
315 xmlFree( const_cast<xmlChar*>(chars) ) ;
316 }
317}
318
322void SAXHelper::ignorableWhitespace( std::u16string_view aWhitespaces )
323{
324 const xmlChar* chars = nullptr ;
325 int length = 0 ;
326
327 chars = ous_to_nxmlstr( aWhitespaces, length ) ;
328 m_pSaxHandler->ignorableWhitespace( m_pParserCtxt , chars , length ) ;
329
330 if( chars != nullptr )
331 {
332 xmlFree( const_cast<xmlChar*>(chars) ) ;
333 }
334}
335
340 std::u16string_view aTarget,
341 std::u16string_view aData )
342{
343 xmlChar* target = nullptr ;
344 xmlChar* data = nullptr ;
345
346 target = ous_to_xmlstr( aTarget ) ;
347 data = ous_to_xmlstr( aData ) ;
348
349 m_pSaxHandler->processingInstruction( m_pParserCtxt , target , data ) ;
350
351 if( target != nullptr )
352 {
353 xmlFree( target ) ;
354 target = nullptr ;
355 }
356
357 if( data != nullptr )
358 {
359 xmlFree( data ) ;
360 data = nullptr ;
361 }
362}
363
364/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
xmlSAXHandlerPtr m_pSaxHandler
Definition: saxhelper.hxx:40
~SAXHelper()
Destructor.
Definition: saxhelper.cxx:173
xmlParserCtxtPtr m_pParserCtxt
Definition: saxhelper.hxx:39
void characters(std::u16string_view aChars)
XDocumentHandler – an xml element or cdata characters.
Definition: saxhelper.cxx:305
void endElement(std::u16string_view aName)
XDocumentHandler – end an xml element.
Definition: saxhelper.cxx:290
void setCurrentNode(const xmlNodePtr pNode)
Definition: saxhelper.cxx:200
void startElement(std::u16string_view aName, const css::uno::Sequence< css::xml::csax::XMLAttribute > &aAttributes)
XDocumentHandler – start an xml element.
Definition: saxhelper.cxx:253
void ignorableWhitespace(std::u16string_view aWhitespaces)
XDocumentHandler – ignorable xml white space.
Definition: saxhelper.cxx:322
void startDocument()
XDocumentHandler – start an xml document.
Definition: saxhelper.cxx:217
void endDocument()
XDocumentHandler – end an xml document.
Definition: saxhelper.cxx:245
void processingInstruction(std::u16string_view aTarget, std::u16string_view aData)
XDocumentHandler – preprocessing instruction.
Definition: saxhelper.cxx:339
SAXHelper()
Constructor.
Definition: saxhelper.cxx:110
OUString aName
constexpr OUStringLiteral aData
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
static xmlChar * ous_to_xmlstr(std::u16string_view oustr)
The return value is NULL terminated.
Definition: saxhelper.cxx:40
static const xmlChar ** attrlist_to_nxmlstr(const css::uno::Sequence< css::xml::csax::XMLAttribute > &aAttributes)
The return value and the referenced value must be NULL terminated.
Definition: saxhelper.cxx:62
static xmlChar * ous_to_nxmlstr(std::u16string_view oustr, int &length)
The return value is NULL terminated.
Definition: saxhelper.cxx:50
sal_Int32 nLength