tdf#92620 - Adjust error message about exceeding legacy module size

Adjusted the error message about exceeding legacy module size and
removed the code for saving image version 11. Modules using image
version 11 still can be loaded. Saving modules always result in
an image version higher than image version 11 depending on the
size of the module.

In addition, some minor performance issues (construction of the error
message and the correct list of modules) were fixed.

Change-Id: I3bde9fcc1596b63446193c836fa7b5cb06eb7d97
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149687
Tested-by: Jenkins
Reviewed-by: Andreas Heinisch <andreas.heinisch@yahoo.de>
diff --git a/basic/source/basmgr/basmgr.cxx b/basic/source/basmgr/basmgr.cxx
index 206b917..f7a4fb2 100644
--- a/basic/source/basmgr/basmgr.cxx
+++ b/basic/source/basmgr/basmgr.cxx
@@ -1390,7 +1390,7 @@ void BasicManager::SetGlobalUNOConstant( const OUString& rName, const uno::Any& 
    pStandardLib->Insert( xUnoObj.get() );
}

bool BasicManager::LegacyPsswdBinaryLimitExceeded( std::vector< OUString >& _out_rModuleNames )
bool BasicManager::ImgVersion12PsswdBinaryLimitExceeded( std::vector< OUString >& _out_rModuleNames )
{
    try
    {
@@ -1409,19 +1409,16 @@ bool BasicManager::LegacyPsswdBinaryLimitExceeded( std::vector< OUString >& _out

            uno::Reference< container::XNameAccess > xScriptLibrary( xScripts->getByName( scriptElementName ), uno::UNO_QUERY_THROW );
            const uno::Sequence< OUString > aElementNames( xScriptLibrary->getElementNames() );
            sal_Int32 nLen = aElementNames.getLength();

            std::vector< OUString > aBigModules( nLen );
            sal_Int32 nBigModules = 0;

            std::vector<OUString> aBigModules;
            for ( auto const & libraryElementName : aElementNames )
            {
                SbModule* pMod = pBasicLib->FindModule( libraryElementName );
                if ( pMod && pMod->ExceedsLegacyModuleSize() )
                    aBigModules[ nBigModules++ ] = libraryElementName;
                if ( pMod && pMod->ExceedsImgVersion12ModuleSize() )
                    aBigModules.push_back(libraryElementName);
            }

            if ( nBigModules )
            if (!aBigModules.empty())
            {
                _out_rModuleNames.swap(aBigModules);
                return true;
diff --git a/basic/source/classes/image.cxx b/basic/source/classes/image.cxx
index eab5fe9..ee49094 100644
--- a/basic/source/classes/image.cxx
+++ b/basic/source/classes/image.cxx
@@ -373,31 +373,13 @@ done:

bool SbiImage::Save( SvStream& r, sal_uInt32 nVer )
{
    bool bLegacy = ( nVer < B_IMG_VERSION_12 );

    // detect if old code exceeds legacy limits
    // if so, then disallow save
    if ( bLegacy && ExceedsLegacyLimits() )
    {
        SbiImage aEmptyImg;
        aEmptyImg.aName = aName;
        aEmptyImg.Save( r, B_IMG_VERSION_11 );
        return true;
    }
    // First of all the header
    sal_uInt64 nStart = SbiOpenRecord( r, FileOffset::Module, 1 );
    sal_uInt64 nPos;

    eCharSet = GetSOStoreTextEncoding( eCharSet );
    if ( bLegacy )
    {
        r.WriteInt32( B_IMG_VERSION_11 );
    }
    else
    {
        r.WriteInt32( nVer );
    }
    r .WriteInt32( eCharSet )
    r .WriteInt32( nVer )
      .WriteInt32( eCharSet )
      .WriteInt32( nDimBase )
      .WriteInt16( static_cast<sal_uInt16>(nFlags) )
      .WriteInt16( 0 )
@@ -429,17 +411,7 @@ bool SbiImage::Save( SvStream& r, sal_uInt32 nVer )
    if (aCode.size() && r.good())
    {
        nPos = SbiOpenRecord( r, FileOffset::PCode, 1 );
        if ( bLegacy )
        {
            PCodeBuffConvertor<sal_uInt32, sal_uInt16> aNewToLegacy(aCode.data(), aCode.size());
            aNewToLegacy.convert();
            aLegacyPCode = aNewToLegacy.GetBuffer();
            r.WriteBytes(aLegacyPCode.data(), aLegacyPCode.size());
        }
        else
        {
            r.WriteBytes(aCode.data(), aCode.size());
        }
        r.WriteBytes(aCode.data(), aCode.size());
        SbiCloseRecord( r, nPos );
    }
    // String-Pool?
@@ -715,4 +687,10 @@ bool SbiImage::ExceedsLegacyLimits()
    return (nStringSize > 0xFF00) || (CalcLegacyOffset(aCode.size()) > 0xFF00);
}

bool SbiImage::ExceedsImgVersion12Limits()
{
    const sal_Int16 nMax = std::numeric_limits<sal_Int16>::max();
    return nStringSize >= nMax || CalcLegacyOffset(aCode.size()) >= nMax;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basic/source/classes/sbxmod.cxx b/basic/source/classes/sbxmod.cxx
index 5b7ee24..15e4719 100644
--- a/basic/source/classes/sbxmod.cxx
+++ b/basic/source/classes/sbxmod.cxx
@@ -1667,11 +1667,11 @@ std::pair<bool, sal_uInt32> SbModule::StoreData( SvStream& rStrm ) const
    }
}

bool SbModule::ExceedsLegacyModuleSize()
bool SbModule::ExceedsImgVersion12ModuleSize()
{
    if ( !IsCompiled() )
        Compile();
    return pImage && pImage->ExceedsLegacyLimits();
    return pImage && pImage->ExceedsImgVersion12Limits();
}

namespace {
diff --git a/basic/source/inc/filefmt.hxx b/basic/source/inc/filefmt.hxx
index b04b7ab..38dfa95 100644
--- a/basic/source/inc/filefmt.hxx
+++ b/basic/source/inc/filefmt.hxx
@@ -45,7 +45,6 @@
//                       new integer type suffix 'b')
//

#define B_IMG_VERSION_11 0x00000011
#define B_IMG_VERSION_12 0x00000012
#define B_IMG_VERSION_13 0x00000013

diff --git a/basic/source/inc/image.hxx b/basic/source/inc/image.hxx
index 2e44239..5a4522b 100644
--- a/basic/source/inc/image.hxx
+++ b/basic/source/inc/image.hxx
@@ -96,6 +96,7 @@ public:
    sal_uInt32  CalcNewOffset( sal_Int16 nOffset );
    void        ReleaseLegacyBuffer();
    bool        ExceedsLegacyLimits();
    bool        ExceedsImgVersion12Limits();
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/basic/source/uno/scriptcont.cxx b/basic/source/uno/scriptcont.cxx
index 7c059ff..f534856 100644
--- a/basic/source/uno/scriptcont.cxx
+++ b/basic/source/uno/scriptcont.cxx
@@ -572,7 +572,7 @@ bool SfxScriptLibraryContainer::implStorePasswordLibrary( SfxLibrary* pLib, cons
    // save/saveas etc are handled in sfxbasemodel::storeSelf &
    // sfxbasemodel::impl_store
    std::vector<OUString> aNames;
    if ( bExport && pBasicMgr->LegacyPsswdBinaryLimitExceeded(aNames) )
    if ( bExport && pBasicMgr->ImgVersion12PsswdBinaryLimitExceeded(aNames) )
    {
        if ( xHandler.is() )
        {
diff --git a/include/basic/basmgr.hxx b/include/basic/basmgr.hxx
index 32710a5..072d92a6 100644
--- a/include/basic/basmgr.hxx
+++ b/include/basic/basmgr.hxx
@@ -172,11 +172,11 @@ public:
    /** retrieves a global constant in the basic library, referring to some UNO object, returns true if a value is found ( value is in aOut ) false otherwise. */
    bool            GetGlobalUNOConstant( const OUString& rName, css::uno::Any& aOut );
    /** determines whether there are password-protected modules whose size exceeds the
        legacy module size
        B_IMG_VERSION_12 module size
        @param _out_rModuleNames
            takes the names of modules whose size exceeds the legacy limit
            takes the names of modules whose size exceeds the B_IMG_VERSION_12 limit
    */
    bool            LegacyPsswdBinaryLimitExceeded( std::vector< OUString >& _out_rModuleNames );
    bool            ImgVersion12PsswdBinaryLimitExceeded( std::vector< OUString >& _out_rModuleNames );
    bool HasExeCode( std::u16string_view );
    /// determines whether the Basic Manager has a given macro, given by fully qualified name
    bool            HasMacro( OUString const& i_fullyQualifiedName ) const;
diff --git a/include/basic/sbmod.hxx b/include/basic/sbmod.hxx
index 4ff300b..10b5421 100644
--- a/include/basic/sbmod.hxx
+++ b/include/basic/sbmod.hxx
@@ -120,7 +120,7 @@ public:
    // Store only image, no source (needed for new password protection)
    SAL_DLLPRIVATE void StoreBinaryData( SvStream& );
    SAL_DLLPRIVATE void LoadBinaryData( SvStream& );
    SAL_DLLPRIVATE bool ExceedsLegacyModuleSize();
    SAL_DLLPRIVATE bool ExceedsImgVersion12ModuleSize();
    SAL_DLLPRIVATE void fixUpMethodStart( bool bCvtToLegacy, SbiImage* pImg = nullptr ) const;
    SAL_DLLPRIVATE bool HasExeCode();
    bool     IsVBASupport() const { return mbVBASupport; }
diff --git a/sfx2/source/appl/appbaslib.cxx b/sfx2/source/appl/appbaslib.cxx
index 69e2f45..bfeafa1 100644
--- a/sfx2/source/appl/appbaslib.cxx
+++ b/sfx2/source/appl/appbaslib.cxx
@@ -150,13 +150,13 @@ void SfxBasicManagerHolder::impl_releaseContainers()
    mxDialogContainer.clear();
}

bool SfxBasicManagerHolder::LegacyPsswdBinaryLimitExceeded( std::vector< OUString >& sModules )
bool SfxBasicManagerHolder::ImgVersion12PsswdBinaryLimitExceeded( std::vector< OUString >& sModules )
{
#if !HAVE_FEATURE_SCRIPTING
    (void) sModules;
#else
    if ( mpBasicManager )
        return mpBasicManager->LegacyPsswdBinaryLimitExceeded( sModules );
        return mpBasicManager->ImgVersion12PsswdBinaryLimitExceeded( sModules );
#endif
    return true;
}
diff --git a/sfx2/source/doc/objstor.cxx b/sfx2/source/doc/objstor.cxx
index bb8007a..44b9730 100644
--- a/sfx2/source/doc/objstor.cxx
+++ b/sfx2/source/doc/objstor.cxx
@@ -3812,7 +3812,7 @@ bool SfxObjectShell::QuerySaveSizeExceededModules_Impl( const uno::Reference< ta
    std::vector< OUString > sModules;
    if ( xHandler.is() )
    {
        if( pImpl->aBasicManager.LegacyPsswdBinaryLimitExceeded( sModules ) )
        if( pImpl->aBasicManager.ImgVersion12PsswdBinaryLimitExceeded( sModules ) )
        {
            rtl::Reference<ModuleSizeExceeded> pReq =  new ModuleSizeExceeded( sModules );
            xHandler->handle( pReq );
diff --git a/sfx2/source/inc/appbaslib.hxx b/sfx2/source/inc/appbaslib.hxx
index 30473f2..04f4199 100644
--- a/sfx2/source/inc/appbaslib.hxx
+++ b/sfx2/source/inc/appbaslib.hxx
@@ -85,7 +85,7 @@ public:
    /** checks if any modules in the SfxLibraryContainer exceed the binary
        limits.
    */
    bool LegacyPsswdBinaryLimitExceeded( std::vector< OUString >& sModules );
    bool ImgVersion12PsswdBinaryLimitExceeded( std::vector< OUString >& sModules );

    virtual void Notify(SfxBroadcaster& rBC, SfxHint const& rHint) override;

diff --git a/uui/inc/ids.hrc b/uui/inc/ids.hrc
index 66f3623..a1739e6 100644
--- a/uui/inc/ids.hrc
+++ b/uui/inc/ids.hrc
@@ -38,7 +38,7 @@ const std::pair<TranslateId, ErrCode> RID_UUI_ERRHDL[] =
      ERRCODE_UUI_IO_ALREADYEXISTS },
    { NC_("RID_UUI_ERRHDL", "Target already exists."),
      ERRCODE_UUI_IO_TARGETALREADYEXISTS },
    { NC_("RID_UUI_ERRHDL", "You are about to save/export a password protected BASIC library containing module(s) \n$(ARG1)\nwhich are too large to store in binary format. If you wish users that don't have access to the library password to be able to run macros in those module(s) you must split those modules into a number of smaller modules. Do you wish to continue to save/export this library?"),
    { NC_("RID_UUI_ERRHDL", "You are saving a password protected Basic library containing the following large module(s): \n$(ARG1)\nStoring those large module(s) in binary format, which is necessary for password protection, makes them unreadable in versions older than LibreOffice 5.0.3. If you want this please split the module into smaller pieces."),
      ERRCODE_UUI_IO_MODULESIZEEXCEEDED },
    { NC_("RID_UUI_ERRHDL", "Beware!\n\nYou are about to load a very unusual sort of file ($(ARG2)) from the URL:\n\n$(ARG1)\n\nAre you certain that this file is a legacy document created many years ago?"),
      ERRCODE_UUI_IO_EXOTICFILEFORMAT },
diff --git a/uui/source/iahndl.cxx b/uui/source/iahndl.cxx
index ce5dfff..dfa3b96 100644
--- a/uui/source/iahndl.cxx
+++ b/uui/source/iahndl.cxx
@@ -84,6 +84,7 @@

#include "iahndl.hxx"
#include "nameclashdlg.hxx"
#include <comphelper/string.hxx>

using ::com::sun::star::uno::Sequence;
using ::com::sun::star::uno::UNO_QUERY;
@@ -382,19 +383,8 @@ UUIInteractionHelper::handleRequest_impl(
        if (aAnyRequest >>= aModSizeException )
        {
            std::vector< OUString > aArguments;
            uno::Sequence< OUString > sModules
                = aModSizeException.Names;
            if ( sModules.hasElements() )
            {
                OUStringBuffer aName;
                for ( sal_Int32 index=0; index< sModules.getLength(); ++index )
                {
                    if ( index )
                        aName.append(",");
                    aName.append(sModules[index]);
                }
                aArguments.push_back( aName.makeStringAndClear() );
            }
            aArguments.push_back(
                comphelper::string::convertCommaSeparated(aModSizeException.Names));
            handleErrorHandlerRequest( task::InteractionClassification_WARNING,
                                       ERRCODE_UUI_IO_MODULESIZEEXCEEDED,
                                       aArguments,