21#include <com/sun/star/packages/WrongPasswordException.hpp>
22#include <com/sun/star/xml/sax/Parser.hpp>
23#include <com/sun/star/xml/sax/InputSource.hpp>
24#include <com/sun/star/xml/sax/Writer.hpp>
25#include <com/sun/star/io/XTruncate.hpp>
26#include <com/sun/star/embed/ElementModes.hpp>
27#include <com/sun/star/embed/XEncryptionProtectedSource.hpp>
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/embed/XTransactedObject.hpp>
30#include <com/sun/star/task/ErrorCodeIOException.hpp>
31#include <com/sun/star/script/ModuleType.hpp>
32#include <com/sun/star/lang/XMultiServiceFactory.hpp>
48#include <com/sun/star/util/VetoException.hpp>
71 static constexpr OUStringLiteral
script =
u"script";
76 static constexpr OUStringLiteral
script =
u"script";
81 static constexpr OUStringLiteral xba =
u"xba";
86 static constexpr OUStringLiteral sBasic =
u"Basic";
95 if( !rPassword.isEmpty() )
107 catch(
const NoSuchElementException& ) {}
119 init( OUString(), xStorage );
129 const OUString& aLibInfoFileURL,
130 const OUString& StorageURL,
134 aLibInfoFileURL, StorageURL,
ReadOnly );
140 aRetAny <<= OUString();
150 const OUString& aElementName,
157 OSL_ENSURE( xTruncate.is(),
"Currently only the streams that can be truncated are expected!" );
158 if ( xTruncate.is() )
160 xTruncate->truncate();
162 xWriter->setOutputStream( xOutput );
165 aMod.
aName = aElementName;
167 Any aElement = xLib->getByName( aElementName );
168 aElement >>= aMod.
aCode;
171 if( xModInfo.is() && xModInfo->hasModuleInfo( aElementName ) )
173 script::ModuleInfo aModInfo = xModInfo->getModuleInfo( aElementName );
174 switch( aModInfo.ModuleType )
176 case ModuleType::NORMAL:
179 case ModuleType::CLASS:
182 case ModuleType::FORM:
185 case ModuleType::DOCUMENT:
188 case ModuleType::UNKNOWN:
200 const OUString& aElementName,
const OUString& aFile,
208 bool bStorage = xInStream.is();
219 xInput =
mxSFI->openFileRead( aFile );
233 source.aInputStream = xInput;
234 source.sSystemId = aFile;
241 xParser->setDocumentHandler( ::xmlscript::importScriptModule( aMod ) );
242 xParser->parseStream( source );
250 aRetAny <<= aMod.
aCode;
268 xFactory->createInstance(
"ooo.vba.VBAGlobals");
274 script::ModuleInfo aModInfo;
275 aModInfo.ModuleType = ModuleType::UNKNOWN;
278 aModInfo.ModuleType = ModuleType::NORMAL;
282 aModInfo.ModuleType = ModuleType::CLASS;
286 aModInfo.ModuleType = ModuleType::FORM;
291 aModInfo.ModuleType = ModuleType::DOCUMENT;
298 mxCodeNameAccess.set( xSF->createInstance(
"ooo.vba.VBAObjectModuleObjectProvider"), UNO_QUERY );
306 aModInfo.ModuleObject.set(
mxCodeNameAccess->getByName( aElementName), uno::UNO_QUERY );
308 catch(
const uno::Exception&)
310 SAL_WARN(
"basic",
"Failed to get document object for " << aElementName );
316 if( xVBAModuleInfo.is() )
318 if( xVBAModuleInfo->hasModuleInfo( aElementName ) )
320 xVBAModuleInfo->removeModuleInfo( aElementName );
322 xVBAModuleInfo->insertModuleInfo( aElementName, aModInfo );
337 auto xStorage = tools::make_ref<SotStorage>(
false, aFile );
340 auto pBasicManager = std::make_unique<BasicManager> ( *xStorage, aFile );
344 pBasicManager->SetLibraryContainerInfo( aInfo );
366 throw IllegalArgumentException(
"!passwordProtected", getXWeak(), 1);
373 (
const OUString& Name,
const OUString& Password )
379 throw IllegalArgumentException(
"!PasswordProtected || PasswordVerified", getXWeak(), 1);
382 bool bSuccess =
false;
414 const OUString& OldPassword,
415 const OUString& NewPassword )
419 if( OldPassword == NewPassword )
423 bool bOldPassword = !OldPassword.isEmpty();
424 bool bNewPassword = !NewPassword.isEmpty();
429 throw IllegalArgumentException();
434 bool bKillCryptedFiles =
false;
435 bool bKillUnencryptedFiles =
false;
444 throw IllegalArgumentException();
451 throw IllegalArgumentException();
471 bKillCryptedFiles =
true;
496 bKillUnencryptedFiles =
true;
500 if( !(bKillCryptedFiles || bKillUnencryptedFiles) )
504 sal_Int32 nNameCount = aElementNames.getLength();
505 const OUString* pNames = aElementNames.getConstArray();
509 for( sal_Int32
i = 0 ;
i < nNameCount ;
i++ )
511 OUString aElementName = pNames[
i ];
514 aElementInetObj.
insertName( aElementName,
false,
517 if( bKillUnencryptedFiles )
527 if(
mxSFI->exists( aElementPath ) )
529 mxSFI->kill( aElementPath );
540 if ( xEncrStream.is() )
542 xEncrStream->setEncryptionPassword( aPass );
549 const OUString& aName,
550 const uno::Reference< embed::XStorage >& xStorage,
551 const css::uno::Reference< css::task::XInteractionHandler >& xHandler )
558 const css::uno::Reference< css::embed::XStorage >& xStorage,
559 const OUString& aTargetURL,
561 const css::uno::Reference< css::task::XInteractionHandler >& xHandler )
566 OSL_ENSURE( pBasicMgr,
"SfxScriptLibraryContainer::implStorePasswordLibrary: cannot do this without a BasicManager!" );
574 std::vector<OUString> aNames;
580 xHandler->handle( pReq );
581 if ( pReq->isAbort() )
583 throw util::VetoException();
594 sal_Int32 nNameCount = aElementNames.getLength();
595 const OUString* pNames = aElementNames.getConstArray();
597 bool bLink =
pLib->mbLink;
598 bool bStorage = xStorage.is() && !bLink;
601 for( sal_Int32 i = 0 ;
i < nNameCount ;
i++ )
603 OUString aElementName = pNames[
i ];
609 OUString aCodeStreamName = aElementName +
".bin";
612 uno::Reference< io::XStream > xCodeStream = xStorage->openStreamElement(
614 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
616 if ( !xCodeStream.is() )
618 throw uno::RuntimeException(
"null returned from openStreamElement");
623 sal_Int32
const nSize = aMemStream.
Tell();
624 if (nSize < 0) { abort(); }
627 memcpy( pData, aMemStream.
GetData(), nSize );
632 throw io::IOException();
634 xOut->writeBytes( aBinSeq );
637 catch(
const uno::Exception& )
643 if(
pLib->mbPasswordVerified ||
pLib->mbDoc50Password )
647 SAL_WARN(
"basic",
"invalid library element '" << aElementName <<
"'.");
651 OUString aSourceStreamName = aElementName +
".xml";
654 uno::Reference< io::XStream > xSourceStream = xStorage->openStreamElement(
656 embed::ElementModes::READWRITE );
657 uno::Reference< beans::XPropertySet > xProps( xSourceStream, uno::UNO_QUERY_THROW );
658 xProps->setPropertyValue(
"MediaType",
uno::Any( OUString(
"text/xml" ) ) );
667 catch(
const uno::Exception& )
669 OSL_FAIL(
"Problem on storing of password library!" );
683 else if(
pLib->mbPasswordVerified || bExport )
692 OUString aLibDirPath;
700 if( !xSFI->isFolder( aLibDirPath ) )
702 xSFI->createFolder( aLibDirPath );
710 for( sal_Int32 i = 0 ;
i < nNameCount ;
i++ )
712 OUString aElementName = pNames[
i ];
715 aElementInetObj.insertName( aElementName,
false,
718 aElementInetObj.setExtension( u
"pba" );
723 SAL_WARN(
"basic",
"invalid library element '" << aElementName <<
"'.");
729 uno::Reference< embed::XStorage > xElementRootStorage =
732 embed::ElementModes::READWRITE );
733 if ( !xElementRootStorage.is() )
735 throw uno::RuntimeException(
"null returned from GetStorageFromURL");
741 uno::Reference< io::XStream > xCodeStream = xElementRootStorage->openStreamElement(
748 sal_Int32
const nSize = aMemStream.
Tell();
749 if (nSize < 0) { abort(); }
752 memcpy( pData, aMemStream.
GetData(), nSize );
757 xOut->writeBytes( aBinSeq );
763 OUString aSourceStreamName(
"source.xml" );
765 uno::Reference< io::XStream > xSourceStream;
768 xSourceStream = xElementRootStorage->openStreamElement(
773 uno::Reference< embed::XEncryptionProtectedSource > xEncr( xSourceStream, uno::UNO_QUERY_THROW );
774 xEncr->setEncryptionPassword(
pLib->maPassword );
776 catch(
const css::packages::WrongPasswordException& )
778 xSourceStream = xElementRootStorage->openEncryptedStreamElement(
784 uno::Reference< beans::XPropertySet > xProps( xSourceStream, uno::UNO_QUERY_THROW );
785 xProps->setPropertyValue(
"MediaType",
uno::Any( OUString(
"text/xml" ) ) );
793 uno::Reference< embed::XTransactedObject > xTransact( xElementRootStorage, uno::UNO_QUERY_THROW );
796 catch(
const uno::Exception& )
803 catch(
const Exception& )
811 (
SfxLibrary* pLib,
const OUString& Name,
bool bVerifyPasswordOnly )
815 bool bLink =
pLib->mbLink;
816 bool bStorage =
mxStorage.is() && !bLink;
830 bool bLoadBinary =
false;
831 if( !pScriptLib->
mbLoadedBinary && !bVerifyPasswordOnly && !
pLib->mbPasswordVerified )
834 OSL_ENSURE( pBasicMgr,
"SfxScriptLibraryContainer::implLoadPasswordLibrary: cannot do this without a BasicManager!" );
835 bool bLoaded = pScriptLib->
mbLoaded;
837 pBasicLib = pBasicMgr ? pBasicMgr->
GetLib(
Name ) :
nullptr;
847 bool bLoadSource =
false;
855 sal_Int32 nNameCount = aElementNames.getLength();
856 const OUString* pNames = aElementNames.getConstArray();
864 if ( !xLibrariesStor.is() )
866 throw uno::RuntimeException(
"null returned from openStorageElement");
869 if ( !xLibraryStor.is() )
871 throw uno::RuntimeException(
"null returned from openStorageElement");
874 catch(
const uno::Exception& )
876 OSL_FAIL(
"### couldn't open sub storage for library" );
880 for( sal_Int32
i = 0 ;
i < nNameCount ;
i++ )
882 OUString aElementName = pNames[
i ];
890 pMod = pBasicLib->
MakeModule( aElementName, OUString() );
894 OUString aCodeStreamName= aElementName +
".bin";
900 if ( !xCodeStream.is() )
902 throw uno::RuntimeException(
"null returned from openStreamElement");
905 if ( !pStream || pStream->GetError() )
908 throw task::ErrorCodeIOException(
909 (
"utl::UcbStreamHelper::CreateStream failed for \""
910 + aCodeStreamName +
"\": 0x"
911 + OUString::number(nError, 16)),
918 catch(
const uno::Exception& )
925 if( bLoadSource || bVerifyPasswordOnly )
928 OUString aSourceStreamName = aElementName +
".xml";
935 if ( !xSourceStream.is() )
937 throw uno::RuntimeException(
"null returned from openEncryptedStreamElement");
940 if ( !bVerifyPasswordOnly )
943 if ( !xInStream.is() )
945 throw io::IOException();
949 aElementName, aSourceStreamName,
951 if(
pLib->hasByName( aElementName ) )
953 if( aAny.hasValue() )
955 pLib->maNameContainer->replaceByName( aElementName, aAny );
960 pLib->maNameContainer->insertByName( aElementName, aAny );
964 catch(
const uno::Exception& )
977 for( sal_Int32
i = 0 ;
i < nNameCount ;
i++ )
979 OUString aElementName = pNames[
i ];
982 aElementInetObj.
insertName( aElementName,
false,
993 }
catch(
const uno::Exception& )
998 if ( xElementRootStorage.is() )
1006 pMod = pBasicLib->
MakeModule( aElementName, OUString() );
1017 if ( !pStream || pStream->GetError() )
1020 throw task::ErrorCodeIOException(
1021 (
"utl::UcbStreamHelper::CreateStream failed"
1023 + OUString::number(nError, 16)),
1031 catch(
const uno::Exception& )
1038 if( bLoadSource || bVerifyPasswordOnly )
1043 OUString aSourceStreamName(
"source.xml" );
1048 if ( !xSourceStream.is() )
1050 throw uno::RuntimeException(
"null returned from openEncryptedStreamElement");
1052 if ( !bVerifyPasswordOnly )
1055 if ( !xInStream.is() )
1057 throw io::IOException();
1064 if(
pLib->hasByName( aElementName ) )
1066 if( aAny.hasValue() )
1068 pLib->maNameContainer->replaceByName( aElementName, aAny );
1073 pLib->maNameContainer->insertByName( aElementName, aAny );
1077 catch (
const uno::Exception& )
1103 OSL_ENSURE( pBasicMgr,
"we need a basicmanager, really we do" );
1116 return "com.sun.star.comp.sfx2.ScriptLibraryContainer";
1121 return {
"com.sun.star.script.DocumentScriptLibraryContainer",
1122 "com.sun.star.script.ScriptLibraryContainer"};
1131 , mbLoadedSource( false )
1132 , mbLoadedBinary( false )
1138 const OUString& aLibInfoFileURL,
1139 const OUString& aStorageURL,
1141 : SfxLibrary( _rModifiable,
cppu::
UnoType<OUString>::
get(), xSFI,
1142 aLibInfoFileURL, aStorageURL,
ReadOnly)
1143 , mbLoadedSource( false )
1144 , mbLoadedBinary( false )
1170 (
const OUString&,
const OUString& )
1174 < css::embed::XStorage >& )
1181 OUString sModuleText;
1182 rElement >>= sModuleText;
1183 return ( !sModuleText.isEmpty() );
1198 throw NoSuchElementException();
1206 ModuleInfoMap::iterator it =
mModuleInfo.find( ModuleName );
1219 throw ElementExistException();
1228 throw NoSuchElementException();
1234extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
1236 css::uno::Sequence<css::uno::Any>
const &)
StarBASIC * GetLib(sal_uInt16 nLib) const
bool HasExeCode(std::u16string_view)
bool ImgVersion12PsswdBinaryLimitExceeded(std::vector< OUString > &_out_rModuleNames)
determines whether there are password-protected modules whose size exceeds the B_IMG_VERSION_12 modul...
static DialogMask HandleError(ErrCode nId, weld::Window *pParent=nullptr, DialogMask nMask=DialogMask::MAX)
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
bool insertName(std::u16string_view rTheName, bool bAppendFinalSlash=false, sal_Int32 nIndex=LAST_SEGMENT, EncodeMechanism eMechanism=EncodeMechanism::WasEncoded, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
bool setExtension(std::u16string_view rTheExtension, sal_Int32 nIndex=LAST_SEGMENT, bool bIgnoreFinalSlash=true, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
SAL_DLLPRIVATE void LoadBinaryData(SvStream &)
SAL_DLLPRIVATE void StoreBinaryData(SvStream &)
SbModule * MakeModule(const OUString &rName, const OUString &rSrc)
SbModule * FindModule(std::u16string_view)
virtual void SetModified(bool) override
void setModified(bool _bModified)
OUString maLibElementFileExtension
OUString createAppLibraryFolder(SfxLibrary *pLib, std::u16string_view aName)
css::uno::Reference< css::uno::XComponentContext > mxContext
void init(const OUString &rInitialDocumentURL, const css::uno::Reference< css::embed::XStorage > &_rxInitialStorage)
virtual void SAL_CALL loadLibrary(const OUString &Name) override
css::uno::Reference< css::ucb::XSimpleFileAccess3 > mxSFI
css::uno::Reference< css::embed::XStorage > mxStorage
virtual sal_Bool SAL_CALL getVBACompatibilityMode() override
ModifiableHelper maModifiable
BasicManager * getBasicManager()
void storeLibraries_Impl(const css::uno::Reference< css::embed::XStorage > &xStorage, bool bComplete)
SfxLibrary * getImplLib(const OUString &rLibraryName)
css::uno::WeakReference< css::frame::XModel > mxOwnerDocument
bool implIsModified() const
virtual bool isLoadedStorable()
virtual css::uno::Sequence< OUString > SAL_CALL getElementNames() override
void implSetModified(bool _bIsModified)
virtual css::uno::Any createEmptyLibraryElement() override
virtual void SAL_CALL changeLibraryPassword(const OUString &Name, const OUString &OldPassword, const OUString &NewPassword) override
virtual sal_Bool SAL_CALL isLibraryPasswordProtected(const OUString &Name) override
virtual rtl::Reference< SfxLibrary > implCreateLibraryLink(const OUString &aName, const OUString &aLibInfoFileURL, const OUString &StorageURL, bool ReadOnly) override
virtual rtl::Reference< SfxLibrary > implCreateLibrary(const OUString &aName) override
virtual bool implLoadPasswordLibrary(SfxLibrary *pLib, const OUString &Name, bool bVerifyPasswordOnly=false) override
virtual OUString getOldInfoFileName() const override
virtual bool isLibraryElementValid(const css::uno::Any &rElement) const override
virtual css::uno::Any importLibraryElement(const css::uno::Reference< css::container::XNameContainer > &xLibrary, const OUString &aElementName, const OUString &aFile, const css::uno::Reference< css::io::XInputStream > &xElementStream) override
virtual OUString getInfoFileName() const override
virtual bool implStorePasswordLibrary(SfxLibrary *pLib, const OUString &aName, const css::uno::Reference< css::embed::XStorage > &xStorage, const css::uno::Reference< css::task::XInteractionHandler > &Handler) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
css::uno::Reference< css::container::XNameAccess > mxCodeNameAccess
virtual OUString getLibElementFileExtension() const override
void setLibraryPassword(const OUString &rLibraryName, const OUString &rPassword)
virtual OUString getLibrariesDir() const override
virtual sal_Bool SAL_CALL verifyLibraryPassword(const OUString &Name, const OUString &Password) override
virtual void importFromOldStorage(const OUString &aFile) override
virtual rtl::Reference< SfxLibraryContainer > createInstanceImpl() override
virtual void writeLibraryElement(const css::uno::Reference< css::container::XNameContainer > &xLibrary, const OUString &aElementName, const css::uno::Reference< css::io::XOutputStream > &xOutput) override
SfxScriptLibraryContainer()
virtual void onNewRootStorage() override
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL HasExecutableCode(const OUString &) override
virtual sal_Bool SAL_CALL isLibraryPasswordVerified(const OUString &Name) override
SfxScriptLibrary(ModifiableHelper &_rModifiable, const css::uno::Reference< css::ucb::XSimpleFileAccess3 > &xSFI)
virtual bool isLibraryElementValid(const css::uno::Any &rElement) const override
virtual void storeResources() override
virtual sal_Bool SAL_CALL hasModuleInfo(const OUString &ModuleName) override
virtual void SAL_CALL removeModuleInfo(const OUString &ModuleName) override
virtual css::script::ModuleInfo SAL_CALL getModuleInfo(const OUString &ModuleName) override
virtual void storeResourcesToStorage(const css::uno::Reference< css::embed::XStorage > &xStorage) override
virtual void SAL_CALL insertModuleInfo(const OUString &ModuleName, const css::script::ModuleInfo &ModuleInfo) override
virtual bool isLoadedStorable() override
virtual void storeResourcesToURL(const OUString &URL, const css::uno::Reference< css::task::XInteractionHandler > &xHandler) override
static bool containsValidModule(const css::uno::Any &_rElement)
virtual void storeResourcesAsURL(const OUString &URL, const OUString &NewName) override
virtual bool isModified() override
ModuleInfoMap mModuleInfo
static css::uno::Reference< css::embed::XStorage > GetStorageFromURL(const OUString &aURL, sal_Int32 nStorageMode, const css::uno::Reference< css::uno::XComponentContext > &rxContext=css::uno::Reference< css::uno::XComponentContext >())
static std::unique_ptr< SvStream > CreateStream(const OUString &rFileName, StreamMode eOpenMode, css::uno::Reference< css::awt::XWindow > xParentWin=nullptr)
#define ERRCODE_IO_GENERAL
Reference< XSingleServiceFactory > xFactory
#define SAL_WARN(area, stream)
std::unique_ptr< sal_Int32[]> pData
IMPLEMENT_FORWARD_XTYPEPROVIDER2(SfxDialogLibrary, SfxLibrary, SfxDialogLibrary_BASE)
IMPLEMENT_FORWARD_XINTERFACE2(SfxDialogLibrary, SfxLibrary, SfxDialogLibrary_BASE)
static void setStreamKey(const uno::Reference< io::XStream > &xStream, const OUString &aPass)
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
void exportScriptModule(Reference< xml::sax::XWriter > const &xOut, const ModuleDescriptor &rMod)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_sfx2_ScriptLibraryContainer_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
#define ERRCTX_SFX_LOADBASIC
Reference< XModel > xModel