| 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 |
|
| 36 |
/** |
| 37 |
* The return value is NULL terminated. The application has the responsibility to |
| 38 |
* deallocate the return value. |
| 39 |
*/ |
| 40 |
static 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 |
|
| 46 |
/** |
| 47 |
* The return value is NULL terminated. The application has the responsibility to |
| 48 |
* deallocate the return value. |
| 49 |
*/ |
| 50 |
static 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 |
|
| 58 |
/** |
| 59 |
* The return value and the referenced value must be NULL terminated. |
| 60 |
* The application has the responsibility to deallocate the return value. |
| 61 |
*/ |
| 62 |
static 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 |
|
| 104 |
/** |
| 105 |
* Constructor |
| 106 |
* |
| 107 |
* In this constructor, a libxml sax parser context is initialized. a libxml |
| 108 |
* default sax handler is initialized with the context. |
| 109 |
*/ |
| 110 |
SAXHelper::SAXHelper( ) |
| 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 |
SAL_WNODEPRECATED_DECLARATIONS_PUSH |
| 122 |
xmlSubstituteEntitiesDefault(0) ; |
| 123 |
#ifndef XMLSEC_NO_XSLT |
| 124 |
xmlIndentTreeOutput = 1 ; |
| 125 |
#endif /* XMLSEC_NO_XSLT */ |
| 126 |
SAL_WNODEPRECATED_DECLARATIONS_POP |
| 127 |
|
| 128 |
m_pParserCtxt = xmlNewParserCtxt() ; |
| 129 |
|
| 130 |
if( m_pParserCtxt == nullptr ) |
| 131 |
{ |
| 132 |
// see issue i74334, we cannot call xmlCleanupParser when libxml is still used |
| 133 |
// in other parts of the office. |
| 134 |
// xmlCleanupParser() ; |
| 135 |
// and neither can we call xsltCleanupGlobals() |
| 136 |
throw css::uno::RuntimeException() ; |
| 137 |
} |
| 138 |
|
| 139 |
xmlSAXVersion(m_pParserCtxt->sax, 1); |
| 140 |
|
| 141 |
if (m_pParserCtxt->inputTab != nullptr) |
| 142 |
{ |
| 143 |
m_pParserCtxt->inputTab[0] = nullptr ; |
| 144 |
} |
| 145 |
|
| 146 |
if( m_pParserCtxt->sax == nullptr ) |
| 147 |
{ |
| 148 |
xmlFreeParserCtxt( m_pParserCtxt ) ; |
| 149 |
|
| 150 |
// see issue i74334, we cannot call xmlCleanupParser when libxml is still used |
| 151 |
// in other parts of the office. |
| 152 |
// xmlCleanupParser() ; |
| 153 |
// and neither can we call xsltCleanupGlobals() |
| 154 |
m_pParserCtxt = nullptr ; |
| 155 |
throw css::uno::RuntimeException() ; |
| 156 |
} |
| 157 |
else |
| 158 |
{ |
| 159 |
m_pSaxHandler = m_pParserCtxt->sax ; |
| 160 |
|
| 161 |
//Adjust the context |
| 162 |
SAL_WNODEPRECATED_DECLARATIONS_PUSH |
| 163 |
m_pParserCtxt->recovery = 1 ; |
| 164 |
SAL_WNODEPRECATED_DECLARATIONS_POP |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Destructor |
| 170 |
* |
| 171 |
* In this destructor, a libxml sax parser context is destructed. The XML tree |
| 172 |
* in the context is not deallocated because the tree is bind with a document |
| 173 |
* model by the setTargetDocument method, which delegate the target document to |
| 174 |
* destruct the xml tree. |
| 175 |
*/ |
| 176 |
SAXHelper::~SAXHelper() { |
| 177 |
if( m_pParserCtxt != nullptr ) |
| 178 |
{ |
| 179 |
/* |
| 180 |
* In the situation that no object refer the Document, this destructor |
| 181 |
* must deallocate the Document memory |
| 182 |
*/ |
| 183 |
if( m_pSaxHandler == m_pParserCtxt->sax ) |
| 184 |
{ |
| 185 |
m_pSaxHandler = nullptr ; |
| 186 |
} |
| 187 |
|
| 188 |
xmlFreeParserCtxt( m_pParserCtxt ) ; |
| 189 |
m_pParserCtxt = nullptr ; |
| 190 |
} |
| 191 |
|
| 192 |
if( m_pSaxHandler != nullptr ) |
| 193 |
{ |
| 194 |
xmlFree( m_pSaxHandler ) ; |
| 195 |
m_pSaxHandler = nullptr ; |
| 196 |
} |
| 197 |
// see issue i74334, we cannot call xmlCleanupParser when libxml is still used |
| 198 |
// in other parts of the office. |
| 199 |
// xmlCleanupParser() ; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
void SAXHelper::setCurrentNode(const xmlNodePtr pNode) |
| 204 |
{ |
| 205 |
/* |
| 206 |
* This is really a black trick. |
| 207 |
* When the current node is replaced, the nodeTab |
| 208 |
* stack's top has to been replaced with the same |
| 209 |
* node, in order to make compatibility. |
| 210 |
*/ |
| 211 |
SAL_WNODEPRECATED_DECLARATIONS_PUSH |
| 212 |
m_pParserCtxt->nodeTab[m_pParserCtxt->nodeNr - 1] |
| 213 |
= m_pParserCtxt->node |
| 214 |
= pNode; |
| 215 |
SAL_WNODEPRECATED_DECLARATIONS_POP |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* XDocumentHandler -- start an xml document |
| 221 |
*/ |
| 222 |
void SAXHelper::startDocument() |
| 223 |
{ |
| 224 |
if( m_pParserCtxt == nullptr) |
| 225 |
{ |
| 226 |
throw css::uno::RuntimeException() ; |
| 227 |
} |
| 228 |
/* |
| 229 |
* Adjust inputTab |
| 230 |
*/ |
| 231 |
xmlParserInputPtr pInput = xmlNewInputStream( m_pParserCtxt ) ; |
| 232 |
|
| 233 |
SAL_WNODEPRECATED_DECLARATIONS_PUSH |
| 234 |
if( m_pParserCtxt->inputTab != nullptr && m_pParserCtxt->inputMax != 0 ) |
| 235 |
SAL_WNODEPRECATED_DECLARATIONS_POP |
| 236 |
{ |
| 237 |
m_pParserCtxt->inputTab[0] = pInput ; |
| 238 |
m_pParserCtxt->input = pInput ; |
| 239 |
} |
| 240 |
|
| 241 |
m_pSaxHandler->startDocument( m_pParserCtxt ) ; |
| 242 |
|
| 243 |
if( m_pParserCtxt->myDoc == nullptr ) |
| 244 |
{ |
| 245 |
throw css::uno::RuntimeException() ; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* XDocumentHandler -- end an xml document |
| 251 |
*/ |
| 252 |
void SAXHelper::endDocument() |
| 253 |
{ |
| 254 |
m_pSaxHandler->endDocument( m_pParserCtxt ) ; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* XDocumentHandler -- start an xml element |
| 259 |
*/ |
| 260 |
void SAXHelper::startElement( |
| 261 |
std::u16string_view aName, |
| 262 |
const css::uno::Sequence< css::xml::csax::XMLAttribute >& aAttributes ) |
| 263 |
{ |
| 264 |
const xmlChar* fullName = nullptr ; |
| 265 |
const xmlChar** attrs = nullptr ; |
| 266 |
|
| 267 |
fullName = ous_to_xmlstr( aName ) ; |
| 268 |
attrs = attrlist_to_nxmlstr( aAttributes ) ; |
| 269 |
|
| 270 |
if( fullName != nullptr || attrs != nullptr ) |
| 271 |
{ |
| 272 |
m_pSaxHandler->startElement( m_pParserCtxt , fullName , attrs ) ; |
| 273 |
} |
| 274 |
|
| 275 |
if( fullName != nullptr ) |
| 276 |
{ |
| 277 |
xmlFree( const_cast<xmlChar*>(fullName) ) ; |
| 278 |
fullName = nullptr ; |
| 279 |
} |
| 280 |
|
| 281 |
if( attrs != nullptr ) |
| 282 |
{ |
| 283 |
for( int i = 0 ; attrs[i] != nullptr ; ++i ) |
| 284 |
{ |
| 285 |
xmlFree( const_cast<xmlChar*>(attrs[i]) ) ; |
| 286 |
attrs[i] = nullptr ; |
| 287 |
} |
| 288 |
|
| 289 |
xmlFree( static_cast<void*>(attrs) ) ; |
| 290 |
attrs = nullptr ; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* XDocumentHandler -- end an xml element |
| 296 |
*/ |
| 297 |
void SAXHelper::endElement( std::u16string_view aName ) |
| 298 |
{ |
| 299 |
xmlChar* fullname = ous_to_xmlstr( aName ) ; |
| 300 |
m_pSaxHandler->endElement( m_pParserCtxt , fullname ) ; |
| 301 |
|
| 302 |
if( fullname != nullptr ) |
| 303 |
{ |
| 304 |
xmlFree( fullname ) ; |
| 305 |
fullname = nullptr ; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* XDocumentHandler -- an xml element or cdata characters |
| 311 |
*/ |
| 312 |
void SAXHelper::characters( std::u16string_view aChars ) |
| 313 |
{ |
| 314 |
const xmlChar* chars = nullptr ; |
| 315 |
int length = 0 ; |
| 316 |
|
| 317 |
chars = ous_to_nxmlstr( aChars, length ) ; |
| 318 |
m_pSaxHandler->characters( m_pParserCtxt , chars , length ) ; |
| 319 |
|
| 320 |
if( chars != nullptr ) |
| 321 |
{ |
| 322 |
xmlFree( const_cast<xmlChar*>(chars) ) ; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* XDocumentHandler -- ignorable xml white space |
| 328 |
*/ |
| 329 |
void SAXHelper::ignorableWhitespace( std::u16string_view aWhitespaces ) |
| 330 |
{ |
| 331 |
const xmlChar* chars = nullptr ; |
| 332 |
int length = 0 ; |
| 333 |
|
| 334 |
chars = ous_to_nxmlstr( aWhitespaces, length ) ; |
| 335 |
m_pSaxHandler->ignorableWhitespace( m_pParserCtxt , chars , length ) ; |
| 336 |
|
| 337 |
if( chars != nullptr ) |
| 338 |
{ |
| 339 |
xmlFree( const_cast<xmlChar*>(chars) ) ; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* XDocumentHandler -- preprocessing instruction |
| 345 |
*/ |
| 346 |
void SAXHelper::processingInstruction( |
| 347 |
std::u16string_view aTarget, |
| 348 |
std::u16string_view aData ) |
| 349 |
{ |
| 350 |
xmlChar* target = nullptr ; |
| 351 |
xmlChar* data = nullptr ; |
| 352 |
|
| 353 |
target = ous_to_xmlstr( aTarget ) ; |
| 354 |
data = ous_to_xmlstr( aData ) ; |
| 355 |
|
| 356 |
m_pSaxHandler->processingInstruction( m_pParserCtxt , target , data ) ; |
| 357 |
|
| 358 |
if( target != nullptr ) |
| 359 |
{ |
| 360 |
xmlFree( target ) ; |
| 361 |
target = nullptr ; |
| 362 |
} |
| 363 |
|
| 364 |
if( data != nullptr ) |
| 365 |
{ |
| 366 |
xmlFree( data ) ; |
| 367 |
data = nullptr ; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |