Kaydet (Commit) fac093e5 authored tarafından Arkadiy Illarionov's avatar Arkadiy Illarionov Kaydeden (comit) Noel Grandin

Simplify Sequence iterations in xmlscript, xmlsecurity

Use range-based loops or replace with comphelper or STL functions

Change-Id: I3d63811caf80c87a9d560087e1f0d933ebcc0d55
Reviewed-on: https://gerrit.libreoffice.org/72040
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 2479ab8b
......@@ -318,11 +318,10 @@ void ElementDescriptor::readComboBoxModel( StyleBag * all_styles )
{
ElementDescriptor * popup = new ElementDescriptor( _xProps, _xPropState, XMLNS_DIALOGS_PREFIX ":menupopup", _xDocument );
OUString const * pItemValues = itemValues.getConstArray();
for ( sal_Int32 nPos = 0; nPos < itemValues.getLength(); ++nPos )
for ( const auto& rItemValue : itemValues )
{
ElementDescriptor * item = new ElementDescriptor( _xProps, _xPropState, XMLNS_DIALOGS_PREFIX ":menuitem", _xDocument );
item->addAttribute( XMLNS_DIALOGS_PREFIX ":value", pItemValues[ nPos ] );
item->addAttribute( XMLNS_DIALOGS_PREFIX ":value", rItemValue );
popup->addSubElement( item );
}
......@@ -366,12 +365,10 @@ void ElementDescriptor::readListBoxModel( StyleBag * all_styles )
{
ElementDescriptor * popup = new ElementDescriptor( _xProps, _xPropState, XMLNS_DIALOGS_PREFIX ":menupopup", _xDocument );
OUString const * pItemValues = itemValues.getConstArray();
sal_Int32 nPos;
for ( nPos = 0; nPos < itemValues.getLength(); ++nPos )
for ( const auto& rItemValue : itemValues )
{
ElementDescriptor * item = new ElementDescriptor(_xProps, _xPropState, XMLNS_DIALOGS_PREFIX ":menuitem", _xDocument );
item->addAttribute( XMLNS_DIALOGS_PREFIX ":value", pItemValues[ nPos ] );
item->addAttribute( XMLNS_DIALOGS_PREFIX ":value", rItemValue );
popup->addSubElement( item );
}
......@@ -379,7 +376,7 @@ void ElementDescriptor::readListBoxModel( StyleBag * all_styles )
if (readProp( "SelectedItems" ) >>= selected)
{
sal_Int16 const * pSelected = selected.getConstArray();
for ( nPos = selected.getLength(); nPos--; )
for ( sal_Int32 nPos = selected.getLength(); nPos--; )
{
ElementDescriptor * item = static_cast< ElementDescriptor * >(
popup->getSubElement( pSelected[ nPos ] ).get() );
......@@ -1090,14 +1087,12 @@ void ElementDescriptor::readBullitinBoard( StyleBag * all_styles )
if ( !xDialogModel.is() )
return; // #TODO throw???
Sequence< OUString > aElements( xDialogModel->getElementNames() );
OUString const * pElements = aElements.getConstArray();
ElementDescriptor * pRadioGroup = nullptr;
sal_Int32 nPos;
for ( nPos = 0; nPos < aElements.getLength(); ++nPos )
for ( const auto& rElement : aElements )
{
Any aControlModel( xDialogModel->getByName( pElements[ nPos ] ) );
Any aControlModel( xDialogModel->getByName( rElement ) );
Reference< beans::XPropertySet > xProps;
OSL_VERIFY( aControlModel >>= xProps );
if (! xProps.is())
......
......@@ -1152,11 +1152,10 @@ void ElementDescriptor::readEvents()
if (xEvents.is())
{
Sequence< OUString > aNames( xEvents->getElementNames() );
OUString const * pNames = aNames.getConstArray();
for ( sal_Int32 nPos = 0; nPos < aNames.getLength(); ++nPos )
for ( const auto& rName : aNames )
{
script::ScriptEventDescriptor descr;
if (xEvents->getByName( pNames[ nPos ] ) >>= descr)
if (xEvents->getByName( rName ) >>= descr)
{
SAL_WARN_IF( descr.ListenerType.isEmpty() ||
descr.EventMethod.isEmpty() ||
......
......@@ -153,17 +153,13 @@ sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /
if ( xLibContainer.is() )
{
Sequence< OUString > aLibNames = xLibContainer->getElementNames();
sal_Int32 nLibCount = aLibNames.getLength();
const OUString* pLibNames = aLibNames.getConstArray();
for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
for ( const OUString& rLibName : aLibNames )
{
OUString aLibName( pLibNames[i] );
if ( xLibContainer->hasByName( aLibName ) )
if ( xLibContainer->hasByName( rLibName ) )
{
OUString aTrueStr( "true" );
if ( xLibContainer->isLibraryLink( aLibName ) )
if ( xLibContainer->isLibraryLink( rLibName ) )
{
// ooo/script:library-linked element
OUString aLibElementName( aPrefix );
......@@ -173,9 +169,9 @@ sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /
xLibAttribs = static_cast< xml::sax::XAttributeList* >( pLibElement );
// ooo/script:name attribute
pLibElement->addAttribute( aPrefix + ":name", aLibName );
pLibElement->addAttribute( aPrefix + ":name", rLibName );
OUString aLinkURL( xLibContainer->getLibraryLinkURL( aLibName ) );
OUString aLinkURL( xLibContainer->getLibraryLinkURL( rLibName ) );
if ( !aLinkURL.isEmpty() )
{
// xlink:href attribute
......@@ -185,7 +181,7 @@ sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /
pLibElement->addAttribute( XMLNS_XLINK_PREFIX ":type", "simple" );
}
if ( xLibContainer->isLibraryReadOnly( aLibName ) )
if ( xLibContainer->isLibraryReadOnly( rLibName ) )
{
// ooo/script:readonly attribute
pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
......@@ -209,9 +205,9 @@ sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /
xLibAttribs = static_cast< xml::sax::XAttributeList* >( pLibElement );
// ooo/script:name attribute
pLibElement->addAttribute( aPrefix + ":name", aLibName );
pLibElement->addAttribute( aPrefix + ":name", rLibName );
if ( xLibContainer->isLibraryReadOnly( aLibName ) )
if ( xLibContainer->isLibraryReadOnly( rLibName ) )
{
// ooo/script:readonly attribute
pLibElement->addAttribute( aPrefix + ":readonly", aTrueStr );
......@@ -219,28 +215,25 @@ sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /
// TODO: password protected libraries
Reference< script::XLibraryContainerPassword > xPasswd( xLibContainer, UNO_QUERY );
if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( aLibName ) )
if ( xPasswd.is() && xPasswd->isLibraryPasswordProtected( rLibName ) )
continue;
// <ooo/script:library-embedded...
m_xHandler->ignorableWhitespace( OUString() );
m_xHandler->startElement( aLibElementName, xLibAttribs );
if ( !xLibContainer->isLibraryLoaded( aLibName ) )
xLibContainer->loadLibrary( aLibName );
if ( !xLibContainer->isLibraryLoaded( rLibName ) )
xLibContainer->loadLibrary( rLibName );
Reference< container::XNameContainer > xLib;
xLibContainer->getByName( aLibName ) >>= xLib;
xLibContainer->getByName( rLibName ) >>= xLib;
if ( xLib.is() )
{
Sequence< OUString > aModNames = xLib->getElementNames();
sal_Int32 nModCount = aModNames.getLength();
const OUString* pModNames = aModNames.getConstArray();
for ( sal_Int32 j = 0 ; j < nModCount ; ++j )
for ( const OUString& rModName : aModNames )
{
OUString aModName( pModNames[j] );
if ( xLib->hasByName( aModName ) )
if ( xLib->hasByName( rModName ) )
{
// ooo/script:module element
OUString aModElementName( aPrefix );
......@@ -250,7 +243,7 @@ sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /
xModAttribs = static_cast< xml::sax::XAttributeList* >( pModElement );
// ooo/script:name attribute
pModElement->addAttribute( aPrefix + ":name", aModName );
pModElement->addAttribute( aPrefix + ":name", rModName );
// <ooo/script:module...
m_xHandler->ignorableWhitespace( OUString() );
......@@ -270,7 +263,7 @@ sal_Bool XMLBasicExporterBase::filter( const Sequence< beans::PropertyValue >& /
// module data
// TODO: write encrypted data for password protected libraries
OUString aSource;
xLib->getByName( aModName ) >>= aSource;
xLib->getByName( rModName ) >>= aSource;
m_xHandler->characters( aSource );
// TODO: <ooo/script:byte-code>
......
......@@ -118,22 +118,17 @@ exportLibrary(
if( rLib.bPreload )
pLibElement->addAttribute( XMLNS_LIBRARY_PREFIX ":preload", sTrueStr );
sal_Int32 nElementCount = rLib.aElementNames.getLength();
if( nElementCount )
{
const OUString* pElementNames = rLib.aElementNames.getConstArray();
for( sal_Int32 i = 0 ; i < nElementCount ; i++ )
for( const auto& rElementName : rLib.aElementNames )
{
XMLElement* pElement = new XMLElement( XMLNS_LIBRARY_PREFIX ":element" );
Reference< xml::sax::XAttributeList > xElementAttribs;
xElementAttribs = static_cast< xml::sax::XAttributeList* >( pElement );
pElement->addAttribute( XMLNS_LIBRARY_PREFIX ":name",
pElementNames[i] );
rElementName );
pLibElement->addSubElement( pElement );
}
}
pLibElement->dump( xOut.get() );
......
......@@ -619,24 +619,15 @@ void DocumentDigitalSignatures::showCertificate(
sal_Bool DocumentDigitalSignatures::isAuthorTrusted(
const Reference< css::security::XCertificate >& Author )
{
bool bFound = false;
OUString sSerialNum = xmlsecurity::bigIntegerToNumericString( Author->getSerialNumber() );
Sequence< SvtSecurityOptions::Certificate > aTrustedAuthors = SvtSecurityOptions().GetTrustedAuthors();
const SvtSecurityOptions::Certificate* pAuthors = aTrustedAuthors.getConstArray();
const SvtSecurityOptions::Certificate* pAuthorsEnd = pAuthors + aTrustedAuthors.getLength();
for ( ; pAuthors != pAuthorsEnd; ++pAuthors )
{
SvtSecurityOptions::Certificate aAuthor = *pAuthors;
if ( ( aAuthor[0] == Author->getIssuerName() ) && ( aAuthor[1] == sSerialNum ) )
{
bFound = true;
break;
}
}
return bFound;
return std::any_of(aTrustedAuthors.begin(), aTrustedAuthors.end(),
[&Author, &sSerialNum](const SvtSecurityOptions::Certificate& rAuthor) {
return ( rAuthor[0] == Author->getIssuerName() )
&& ( rAuthor[1] == sSerialNum );
});
}
uno::Sequence<Reference<css::security::XCertificate>>
......
......@@ -198,23 +198,23 @@ void CertificateChooser::ImplInitialize()
// fill list of certificates; the first entry will be selected
for ( sal_Int32 nC = 0; nC < nCertificates; ++nC )
for ( const auto& xCert : xCerts )
{
std::shared_ptr<UserData> userData = std::make_shared<UserData>();
userData->xCertificate = xCerts[ nC ];
userData->xCertificate = xCert;
userData->xSecurityContext = secContext;
userData->xSecurityEnvironment = secEnvironment;
mvUserData.push_back(userData);
OUString sIssuer = xmlsec::GetContentPart( xCerts[ nC ]->getIssuerName() );
OUString sIssuer = xmlsec::GetContentPart( xCert->getIssuerName() );
m_xCertLB->append();
int nRow = m_xCertLB->n_children() - 1;
m_xCertLB->set_text(nRow, xmlsec::GetContentPart(xCerts[nC]->getSubjectName()), 0);
m_xCertLB->set_text(nRow, xmlsec::GetContentPart(xCert->getSubjectName()), 0);
m_xCertLB->set_text(nRow, sIssuer, 1);
m_xCertLB->set_text(nRow, xmlsec::GetCertificateKind(xCerts[nC]->getCertificateKind()), 2);
m_xCertLB->set_text(nRow, utl::GetDateString(xCerts[nC]->getNotValidAfter()), 3);
m_xCertLB->set_text(nRow, UsageInClearText(xCerts[nC]->getCertificateUsage()), 4);
m_xCertLB->set_text(nRow, xmlsec::GetCertificateKind(xCert->getCertificateKind()), 2);
m_xCertLB->set_text(nRow, utl::GetDateString(xCert->getNotValidAfter()), 3);
m_xCertLB->set_text(nRow, UsageInClearText(xCert->getCertificateUsage()), 4);
OUString sId(OUString::number(reinterpret_cast<sal_Int64>(userData.get())));
m_xCertLB->set_id(nRow, sId);
......@@ -226,7 +226,7 @@ void CertificateChooser::ImplInitialize()
m_xCertLB->select(nRow);
else if ( meAction == UserAction::Encrypt &&
aUserOpts.GetEncryptToSelf() )
mxEncryptToSelf = xCerts[nC];
mxEncryptToSelf = xCert;
}
#endif
}
......
......@@ -363,10 +363,9 @@ MacroSecurityTrustedSourcesTP::MacroSecurityTrustedSourcesTP(weld::Container* pP
m_xTrustFileLocLB->set_sensitive(!mbURLsReadonly);
m_xAddLocPB->set_sensitive(!mbURLsReadonly);
sal_Int32 nEntryCnt = aSecureURLs.getLength();
for (sal_Int32 i = 0; i < nEntryCnt; ++i)
for (const auto& rSecureURL : aSecureURLs)
{
OUString aSystemFileURL( aSecureURLs[ i ] );
OUString aSystemFileURL( rSecureURL );
osl::FileBase::getSystemPathFromFileURL( aSystemFileURL, aSystemFileURL );
m_xTrustFileLocLB->append_text(aSystemFileURL);
}
......
......@@ -20,6 +20,7 @@
#include <sal/config.h>
#include <rtl/ustring.hxx>
#include <framework/xmlsignaturetemplateimpl.hxx>
#include <comphelper/sequence.hxx>
using namespace ::com::sun::star::uno ;
using ::com::sun::star::lang::XMultiServiceFactory ;
......@@ -97,12 +98,7 @@ OUString SAL_CALL XMLSignatureTemplateImpl::getImplementationName() {
/* XServiceInfo */
sal_Bool SAL_CALL XMLSignatureTemplateImpl::supportsService( const OUString& serviceName) {
Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
const OUString* pArray = seqServiceNames.getConstArray() ;
for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
if( *( pArray + i ) == serviceName )
return true ;
}
return false ;
return comphelper::findValue(seqServiceNames, serviceName) != -1;
}
/* XServiceInfo */
......
......@@ -87,42 +87,40 @@ static void ImplFillElementList(
{
Reference < css::container::XNameAccess > xElements( rxStore, UNO_QUERY );
Sequence< OUString > aElements = xElements->getElementNames();
sal_Int32 nElements = aElements.getLength();
const OUString* pNames = aElements.getConstArray();
for ( sal_Int32 n = 0; n < nElements; n++ )
for ( const auto& rName : aElements )
{
if (pNames[n] == "[Content_Types].xml")
if (rName == "[Content_Types].xml")
// OOXML
continue;
// If the user enabled validating according to OOo 3.0
// then mimetype and all content of META-INF must be excluded.
if (mode != DocumentSignatureAlgorithm::OOo3_2
&& (pNames[n] == "META-INF" || pNames[n] == "mimetype"))
&& (rName == "META-INF" || rName == "mimetype"))
{
continue;
}
else
{
OUString sEncName = ::rtl::Uri::encode(
pNames[n], rtl_UriCharClassRelSegment,
rName, rtl_UriCharClassRelSegment,
rtl_UriEncodeStrict, RTL_TEXTENCODING_UTF8);
if (sEncName.isEmpty() && !pNames[n].isEmpty())
if (sEncName.isEmpty() && !rName.isEmpty())
throw css::uno::RuntimeException("Failed to encode element name of XStorage", nullptr);
if ( rxStore->isStreamElement( pNames[n] ) )
if ( rxStore->isStreamElement( rName ) )
{
//Exclude documentsignatures.xml!
if (pNames[n] ==
if (rName ==
DocumentSignatureHelper::GetDocumentContentSignatureDefaultStreamName())
continue;
OUString aFullName( rRootStorageName + sEncName );
rList.push_back(aFullName);
}
else if ( bRecursive && rxStore->isStorageElement( pNames[n] ) )
else if ( bRecursive && rxStore->isStorageElement( rName ) )
{
Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( pNames[n], css::embed::ElementModes::READ );
Reference < css::embed::XStorage > xSubStore = rxStore->openStorageElement( rName, css::embed::ElementModes::READ );
OUString aFullRootName( rRootStorageName + sEncName + "/" );
ImplFillElementList(rList, xSubStore, aFullRootName, bRecursive, mode);
}
......@@ -217,14 +215,12 @@ DocumentSignatureHelper::CreateElementList(
// Object folders...
Reference < css::container::XNameAccess > xElements( rxStore, UNO_QUERY );
Sequence< OUString > aElementNames = xElements->getElementNames();
sal_Int32 nElements = aElementNames.getLength();
const OUString* pNames = aElementNames.getConstArray();
for ( sal_Int32 n = 0; n < nElements; n++ )
for ( const auto& rName : aElementNames )
{
if ( ( pNames[n].match( "Object " ) ) && rxStore->isStorageElement( pNames[n] ) )
if ( ( rName.match( "Object " ) ) && rxStore->isStorageElement( rName ) )
{
Reference < css::embed::XStorage > xTmpSubStore = rxStore->openStorageElement( pNames[n], css::embed::ElementModes::READ );
ImplFillElementList(aElements, xTmpSubStore, pNames[n]+aSep, true, mode);
Reference < css::embed::XStorage > xTmpSubStore = rxStore->openStorageElement( rName, css::embed::ElementModes::READ );
ImplFillElementList(aElements, xTmpSubStore, rName+aSep, true, mode);
}
}
}
......
......@@ -181,15 +181,12 @@ bool DocumentSignatureManager::isXML(const OUString& rURI)
if (readManifest())
{
for (int i = 0; i < m_manifest.getLength(); i++)
for (const uno::Sequence<beans::PropertyValue>& entry : m_manifest)
{
const uno::Sequence<beans::PropertyValue>& entry = m_manifest[i];
OUString sPath, sMediaType;
bool bEncrypted = false;
for (int j = 0; j < entry.getLength(); j++)
for (const beans::PropertyValue& prop : entry)
{
const beans::PropertyValue& prop = entry[j];
if (prop.Name == sPropFullPath)
prop.Value >>= sPath;
else if (prop.Name == sPropMediaType)
......
......@@ -142,12 +142,7 @@ OUString SAL_CALL SecurityEnvironment_NssImpl::getImplementationName() {
/* XServiceInfo */
sal_Bool SAL_CALL SecurityEnvironment_NssImpl::supportsService( const OUString& serviceName) {
Sequence< OUString > seqServiceNames = getSupportedServiceNames() ;
const OUString* pArray = seqServiceNames.getConstArray() ;
for( sal_Int32 i = 0 ; i < seqServiceNames.getLength() ; i ++ ) {
if( *( pArray + i ) == serviceName )
return true ;
}
return false ;
return comphelper::findValue(seqServiceNames, serviceName) != -1;
}
/* XServiceInfo */
......@@ -545,9 +540,9 @@ verifyCertificate( const Reference< csss::XCertificate >& aCert,
{
//prepare the intermediate certificates
for (sal_Int32 i = 0; i < intermediateCerts.getLength(); i++)
for (const auto& rIntermediateCert : intermediateCerts)
{
Sequence<sal_Int8> der = intermediateCerts[i]->getEncoded();
Sequence<sal_Int8> der = rIntermediateCert->getEncoded();
SECItem item;
item.type = siBuffer;
item.data = reinterpret_cast<unsigned char*>(der.getArray());
......@@ -559,7 +554,7 @@ verifyCertificate( const Reference< csss::XCertificate >& aCert,
PR_TRUE /* copyDER */);
if (!certTmp)
{
SAL_INFO("xmlsecurity.xmlsec", "Failed to add a temporary certificate: " << intermediateCerts[i]->getIssuerName());
SAL_INFO("xmlsecurity.xmlsec", "Failed to add a temporary certificate: " << rIntermediateCert->getIssuerName());
}
else
......
......@@ -76,10 +76,11 @@ static const xmlChar** attrlist_to_nxmlstr( const cssu::Sequence< cssxcsax::XMLA
return nullptr ;
}
for( int i = 0 , j = 0 ; j < nLength ; ++j )
int i = 0;
for( const auto& rAttr : aAttributes )
{
attname = ous_to_xmlstr( aAttributes[j].sName ) ;
attvalue = ous_to_xmlstr( aAttributes[j].sValue ) ;
attname = ous_to_xmlstr( rAttr.sName ) ;
attvalue = ous_to_xmlstr( rAttr.sValue ) ;
if( attname != nullptr && attvalue != nullptr )
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment