diff options
Diffstat (limited to 'cppu/source/uno')
-rw-r--r-- | cppu/source/uno/EnvDcp.cxx | 43 | ||||
-rw-r--r-- | cppu/source/uno/EnvStack.cxx | 382 | ||||
-rw-r--r-- | cppu/source/uno/IdentityMapping.cxx | 105 | ||||
-rw-r--r-- | cppu/source/uno/IdentityMapping.hxx | 31 | ||||
-rw-r--r-- | cppu/source/uno/any.cxx | 142 | ||||
-rw-r--r-- | cppu/source/uno/assign.hxx | 449 | ||||
-rw-r--r-- | cppu/source/uno/cascade_mapping.cxx | 308 | ||||
-rw-r--r-- | cppu/source/uno/cascade_mapping.hxx | 32 | ||||
-rw-r--r-- | cppu/source/uno/check.cxx | 335 | ||||
-rw-r--r-- | cppu/source/uno/constr.hxx | 138 | ||||
-rw-r--r-- | cppu/source/uno/copy.hxx | 655 | ||||
-rw-r--r-- | cppu/source/uno/data.cxx | 316 | ||||
-rw-r--r-- | cppu/source/uno/destr.hxx | 352 | ||||
-rw-r--r-- | cppu/source/uno/eq.hxx | 635 | ||||
-rw-r--r-- | cppu/source/uno/lbenv.cxx | 1147 | ||||
-rw-r--r-- | cppu/source/uno/lbmap.cxx | 753 | ||||
-rw-r--r-- | cppu/source/uno/loadmodule.cxx | 75 | ||||
-rw-r--r-- | cppu/source/uno/loadmodule.hxx | 47 | ||||
-rw-r--r-- | cppu/source/uno/prim.hxx | 153 | ||||
-rw-r--r-- | cppu/source/uno/sequence.cxx | 912 |
20 files changed, 7010 insertions, 0 deletions
diff --git a/cppu/source/uno/EnvDcp.cxx b/cppu/source/uno/EnvDcp.cxx new file mode 100644 index 0000000000..c5597dec47 --- /dev/null +++ b/cppu/source/uno/EnvDcp.cxx @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <uno/EnvDcp.h> + + +void uno_EnvDcp_getTypeName(rtl_uString const * pEnvDcp, rtl_uString ** ppEnvTypeName) +{ + sal_Int32 colIdx = rtl_ustr_indexOfChar_WithLength(pEnvDcp->buffer, pEnvDcp->length, ':'); + if (colIdx >= 0) + rtl_uString_newFromStr_WithLength(ppEnvTypeName, pEnvDcp->buffer, colIdx); + + else + rtl_uString_newFromStr(ppEnvTypeName, pEnvDcp->buffer); +} + +void uno_EnvDcp_getPurpose(rtl_uString const * pEnvDcp, rtl_uString ** ppEnvPurpose) +{ + sal_Int32 colIdx = rtl_ustr_indexOfChar_WithLength(pEnvDcp->buffer, pEnvDcp->length, ':'); + if (colIdx >= 0) + rtl_uString_newFromStr_WithLength(ppEnvPurpose, pEnvDcp->buffer + colIdx, pEnvDcp->length - colIdx); + + else + rtl_uString_new(ppEnvPurpose); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/EnvStack.cxx b/cppu/source/uno/EnvStack.cxx new file mode 100644 index 0000000000..34fea77f46 --- /dev/null +++ b/cppu/source/uno/EnvStack.cxx @@ -0,0 +1,382 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <uno/environment.hxx> +#include <uno/lbnames.h> + +#include <cppu/EnvDcp.hxx> +#include <cppu/Enterable.hxx> + +#include <osl/thread.h> +#include <osl/thread.hxx> +#include <o3tl/string_view.hxx> + +#include <mutex> +#include <unordered_map> + +using namespace com::sun::star; + +namespace { + +struct oslThreadIdentifier_equal +{ + bool operator()(oslThreadIdentifier s1, oslThreadIdentifier s2) const; +}; + +} + +bool oslThreadIdentifier_equal::operator()(oslThreadIdentifier s1, oslThreadIdentifier s2) const +{ + bool result = s1 == s2; + + return result; +} + +namespace { + +struct oslThreadIdentifier_hash +{ + size_t operator()(oslThreadIdentifier s1) const; +}; + +} + +size_t oslThreadIdentifier_hash::operator()(oslThreadIdentifier s1) const +{ + return s1; +} + +typedef std::unordered_map<oslThreadIdentifier, + uno_Environment *, + oslThreadIdentifier_hash, + oslThreadIdentifier_equal> ThreadMap; + +namespace +{ + std::mutex s_threadMap_mutex; + ThreadMap s_threadMap; +} + +static void s_setCurrent(uno_Environment * pEnv) +{ + oslThreadIdentifier threadId = osl::Thread::getCurrentIdentifier(); + + std::scoped_lock guard(s_threadMap_mutex); + if (pEnv) + { + s_threadMap[threadId] = pEnv; + } + else + { + ThreadMap::iterator iEnv = s_threadMap.find(threadId); + if( iEnv != s_threadMap.end()) + s_threadMap.erase(iEnv); + } +} + +static uno_Environment * s_getCurrent() +{ + uno_Environment * pEnv = nullptr; + + oslThreadIdentifier threadId = osl::Thread::getCurrentIdentifier(); + + std::scoped_lock guard(s_threadMap_mutex); + ThreadMap::iterator iEnv = s_threadMap.find(threadId); + if(iEnv != s_threadMap.end()) + pEnv = iEnv->second; + + return pEnv; +} + + +extern "C" void SAL_CALL uno_getCurrentEnvironment(uno_Environment ** ppEnv, rtl_uString * pTypeName) + SAL_THROW_EXTERN_C() +{ + if (*ppEnv) + { + (*ppEnv)->release(*ppEnv); + *ppEnv = nullptr; + } + + OUString currPurpose; + + uno_Environment * pCurrEnv = s_getCurrent(); + if (pCurrEnv) // no environment means no purpose + currPurpose = cppu::EnvDcp::getPurpose(pCurrEnv->pTypeName); + + if (pTypeName && rtl_uString_getLength(pTypeName)) + { + OUString envDcp = OUString::unacquired(&pTypeName) + currPurpose; + + uno_getEnvironment(ppEnv, envDcp.pData, nullptr); + } + else + { + if (pCurrEnv) + { + *ppEnv = pCurrEnv; + (*ppEnv)->acquire(*ppEnv); + } + else + { + OUString uno_envDcp(UNO_LB_UNO); + uno_getEnvironment(ppEnv, uno_envDcp.pData, nullptr); + } + } +} + +static OUString s_getPrefix(std::u16string_view str1, std::u16string_view str2) +{ + sal_Int32 nIndex1 = 0; + sal_Int32 nIndex2 = 0; + sal_Int32 sim = 0; + + std::u16string_view token1; + std::u16string_view token2; + + do + { + token1 = o3tl::getToken(str1, 0, ':', nIndex1); + token2 = o3tl::getToken(str2, 0, ':', nIndex2); + + if (token1 == token2) + sim += token1.size() + 1; + } + while(nIndex1 == nIndex2 && nIndex1 >= 0 && token1 == token2); + + OUString result; + + if (sim) + result = str1.substr(0, sim - 1); + + return result; +} + +static int s_getNextEnv(uno_Environment ** ppEnv, uno_Environment * pCurrEnv, uno_Environment * pTargetEnv) +{ + int res = 0; + + std::u16string_view nextPurpose; + + OUString currPurpose; + if (pCurrEnv) + currPurpose = cppu::EnvDcp::getPurpose(pCurrEnv->pTypeName); + + OUString targetPurpose; + if (pTargetEnv) + targetPurpose = cppu::EnvDcp::getPurpose(pTargetEnv->pTypeName); + + OUString intermPurpose(s_getPrefix(currPurpose, targetPurpose)); + if (currPurpose.getLength() > intermPurpose.getLength()) + { + sal_Int32 idx = currPurpose.lastIndexOf(':'); + nextPurpose = currPurpose.subView(0, idx); + + res = -1; + } + else if (intermPurpose.getLength() < targetPurpose.getLength()) + { + sal_Int32 idx = targetPurpose.indexOf(':', intermPurpose.getLength() + 1); + if (idx == -1) + nextPurpose = targetPurpose; + + else + nextPurpose = targetPurpose.subView(0, idx); + + res = 1; + } + + if (!nextPurpose.empty()) + { + OUString next_envDcp = OUString::Concat(UNO_LB_UNO) + nextPurpose; + uno_getEnvironment(ppEnv, next_envDcp.pData, nullptr); + } + else + { + if (*ppEnv) + (*ppEnv)->release(*ppEnv); + + *ppEnv = nullptr; + } + + return res; +} + +extern "C" { static void s_pull(va_list * pParam) +{ + uno_EnvCallee * pCallee = va_arg(*pParam, uno_EnvCallee *); + va_list * pXparam = va_arg(*pParam, va_list *); + + pCallee(pXparam); +}} + +static void s_callInto_v(uno_Environment * pEnv, uno_EnvCallee * pCallee, va_list * pParam) +{ + cppu::Enterable * pEnterable = static_cast<cppu::Enterable *>(pEnv->pReserved); + if (pEnterable) + pEnterable->callInto(s_pull, pCallee, pParam); + + else + pCallee(pParam); +} + +static void s_callInto(uno_Environment * pEnv, uno_EnvCallee * pCallee, ...) +{ + va_list param; + + va_start(param, pCallee); + s_callInto_v(pEnv, pCallee, ¶m); + va_end(param); +} + +static void s_callOut_v(uno_Environment * pEnv, uno_EnvCallee * pCallee, va_list * pParam) +{ + cppu::Enterable * pEnterable = static_cast<cppu::Enterable *>(pEnv->pReserved); + if (pEnterable) + pEnterable->callOut_v(pCallee, pParam); + + else + pCallee(pParam); +} + +static void s_callOut(uno_Environment * pEnv, uno_EnvCallee * pCallee, ...) +{ + va_list param; + + va_start(param, pCallee); + s_callOut_v(pEnv, pCallee, ¶m); + va_end(param); +} + +static void s_environment_invoke_v(uno_Environment *, uno_Environment *, uno_EnvCallee *, va_list *); + +extern "C" { static void s_environment_invoke_vv(va_list * pParam) +{ + uno_Environment * pCurrEnv = va_arg(*pParam, uno_Environment *); + uno_Environment * pTargetEnv = va_arg(*pParam, uno_Environment *); + uno_EnvCallee * pCallee = va_arg(*pParam, uno_EnvCallee *); + va_list * pXparam = va_arg(*pParam, va_list *); + + s_environment_invoke_v(pCurrEnv, pTargetEnv, pCallee, pXparam); +}} + +static void s_environment_invoke_v(uno_Environment * pCurrEnv, uno_Environment * pTargetEnv, uno_EnvCallee * pCallee, va_list * pParam) +{ + uno_Environment * pNextEnv = nullptr; + switch(s_getNextEnv(&pNextEnv, pCurrEnv, pTargetEnv)) + { + case -1: + s_setCurrent(pNextEnv); + s_callOut(pCurrEnv, s_environment_invoke_vv, pNextEnv, pTargetEnv, pCallee, pParam); + s_setCurrent(pCurrEnv); + break; + + case 0: { + uno_Environment * hld = s_getCurrent(); + s_setCurrent(pCurrEnv); + pCallee(pParam); + s_setCurrent(hld); + } + break; + + case 1: + s_setCurrent(pNextEnv); + s_callInto(pNextEnv, s_environment_invoke_vv, pNextEnv, pTargetEnv, pCallee, pParam); + s_setCurrent(pCurrEnv); + break; + } + + if (pNextEnv) + pNextEnv->release(pNextEnv); +} + +extern "C" void SAL_CALL uno_Environment_invoke_v(uno_Environment * pTargetEnv, uno_EnvCallee * pCallee, va_list * pParam) + SAL_THROW_EXTERN_C() +{ + s_environment_invoke_v(s_getCurrent(), pTargetEnv, pCallee, pParam); +} + +extern "C" void SAL_CALL uno_Environment_invoke(uno_Environment * pEnv, uno_EnvCallee * pCallee, ...) + SAL_THROW_EXTERN_C() +{ + va_list param; + + va_start(param, pCallee); + uno_Environment_invoke_v(pEnv, pCallee, ¶m); + va_end(param); +} + +extern "C" void SAL_CALL uno_Environment_enter(uno_Environment * pTargetEnv) + SAL_THROW_EXTERN_C() +{ + uno_Environment * pNextEnv = nullptr; + uno_Environment * pCurrEnv = s_getCurrent(); + + int res; + while ( (res = s_getNextEnv(&pNextEnv, pCurrEnv, pTargetEnv)) != 0) + { + cppu::Enterable * pEnterable; + + switch(res) + { + case -1: + pEnterable = static_cast<cppu::Enterable *>(pCurrEnv->pReserved); + if (pEnterable) + pEnterable->leave(); + pCurrEnv->release(pCurrEnv); + break; + + case 1: + pNextEnv->acquire(pNextEnv); + pEnterable = static_cast<cppu::Enterable *>(pNextEnv->pReserved); + if (pEnterable) + pEnterable->enter(); + break; + } + + s_setCurrent(pNextEnv); + pCurrEnv = pNextEnv; + } +} + +int SAL_CALL uno_Environment_isValid(uno_Environment * pEnv, rtl_uString ** pReason) + SAL_THROW_EXTERN_C() +{ + int result = 1; + + OUString typeName(cppu::EnvDcp::getTypeName(pEnv->pTypeName)); + if (typeName == UNO_LB_UNO) + { + cppu::Enterable * pEnterable = static_cast<cppu::Enterable *>(pEnv->pReserved); + if (pEnterable) + result = pEnterable->isValid(reinterpret_cast<OUString *>(pReason)); + } + else + { + OUString envDcp = UNO_LB_UNO + cppu::EnvDcp::getPurpose(pEnv->pTypeName); + + uno::Environment env(envDcp); + + result = env.isValid(reinterpret_cast<OUString *>(pReason)); + } + + return result; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/IdentityMapping.cxx b/cppu/source/uno/IdentityMapping.cxx new file mode 100644 index 0000000000..32ec32c073 --- /dev/null +++ b/cppu/source/uno/IdentityMapping.cxx @@ -0,0 +1,105 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "IdentityMapping.hxx" + +#include <typelib/typedescription.h> +#include <uno/mapping.h> +#include <uno/environment.hxx> +#include <utility> + +#include <osl/interlck.h> + + +using namespace ::com::sun::star; + +namespace { + +struct IdentityMapping : public uno_Mapping +{ + sal_Int32 m_nRef; + uno::Environment m_env; + + explicit IdentityMapping(uno::Environment aEnv); +}; + +} + +extern "C" +{ + +static void s_free(uno_Mapping * pMapping) +{ + delete static_cast<IdentityMapping *>(pMapping); +} + +static void s_acquire(uno_Mapping * pMapping) +{ + static OUString s_purpose; + + if (1 == osl_atomic_increment(&static_cast<IdentityMapping *>(pMapping)->m_nRef)) + { + uno_registerMapping( + &pMapping, + s_free, + static_cast<IdentityMapping *>(pMapping)->m_env.get(), + static_cast<IdentityMapping *>(pMapping)->m_env.get(), + s_purpose.pData); + } +} + +static void s_release(uno_Mapping * pMapping) +{ + if (!osl_atomic_decrement(&static_cast<IdentityMapping *>(pMapping )->m_nRef)) + uno_revokeMapping(pMapping); +} + +static void s_mapInterface(uno_Mapping * pMapping, + void ** ppOut, + void * pInterface, + SAL_UNUSED_PARAMETER typelib_InterfaceTypeDescription * /*pInterfaceTypeDescr*/) +{ + *ppOut = pInterface; + + if (pInterface) + { + IdentityMapping * that = static_cast<IdentityMapping *>(pMapping); + + (that->m_env.get()->pExtEnv->acquireInterface)(that->m_env.get()->pExtEnv, pInterface); + } +} +} + + +IdentityMapping::IdentityMapping(uno::Environment aEnv) + : m_nRef(0), + m_env(std::move(aEnv)) +{ + uno_Mapping::acquire = s_acquire; + uno_Mapping::release = s_release; + uno_Mapping::mapInterface = s_mapInterface; +} + + +uno_Mapping * createIdentityMapping(uno::Environment const & rEnv) +{ + return new IdentityMapping(rEnv); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/IdentityMapping.hxx b/cppu/source/uno/IdentityMapping.hxx new file mode 100644 index 0000000000..8ba081b009 --- /dev/null +++ b/cppu/source/uno/IdentityMapping.hxx @@ -0,0 +1,31 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <uno/mapping.h> + +namespace com::sun::star::uno +{ +class Environment; +} + +uno_Mapping* createIdentityMapping(const css::uno::Environment& rEnv); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/any.cxx b/cppu/source/uno/any.cxx new file mode 100644 index 0000000000..e26b97286d --- /dev/null +++ b/cppu/source/uno/any.cxx @@ -0,0 +1,142 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include "copy.hxx" +#include "destr.hxx" + +using namespace cppu; + + +extern "C" +{ + +void SAL_CALL uno_type_any_assign( + uno_Any * pDest, void * pSource, + typelib_TypeDescriptionReference * pType, + uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + _destructAny( pDest, release ); + if (pType) + { + _copyConstructAny( pDest, pSource, pType, nullptr, acquire, nullptr ); + } + else + { + CONSTRUCT_EMPTY_ANY( pDest ); + } +} + +void SAL_CALL uno_any_assign( + uno_Any * pDest, void * pSource, + typelib_TypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + _destructAny( pDest, release ); + if (pTypeDescr) + { + _copyConstructAny( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, acquire, nullptr ); + } + else + { + CONSTRUCT_EMPTY_ANY( pDest ); + } +} + +void SAL_CALL uno_type_any_construct( + uno_Any * pDest, void * pSource, + typelib_TypeDescriptionReference * pType, + uno_AcquireFunc acquire ) + SAL_THROW_EXTERN_C() +{ + if (pType) + { + _copyConstructAny( pDest, pSource, pType, nullptr, acquire, nullptr ); + } + else + { + CONSTRUCT_EMPTY_ANY( pDest ); + } +} + +void SAL_CALL uno_any_construct( + uno_Any * pDest, void * pSource, + typelib_TypeDescription * pTypeDescr, + uno_AcquireFunc acquire ) + SAL_THROW_EXTERN_C() +{ + if (pTypeDescr) + { + _copyConstructAny( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, acquire, nullptr ); + } + else + { + CONSTRUCT_EMPTY_ANY( pDest ); + } +} + +void SAL_CALL uno_type_any_constructAndConvert( + uno_Any * pDest, void * pSource, + typelib_TypeDescriptionReference * pType, + uno_Mapping * mapping ) + SAL_THROW_EXTERN_C() +{ + if (pType) + { + _copyConstructAny( pDest, pSource, pType, nullptr, nullptr, mapping ); + } + else + { + CONSTRUCT_EMPTY_ANY( pDest ); + } +} + +void SAL_CALL uno_any_constructAndConvert( + uno_Any * pDest, void * pSource, + typelib_TypeDescription * pTypeDescr, + uno_Mapping * mapping ) + SAL_THROW_EXTERN_C() +{ + if (pTypeDescr) + { + _copyConstructAny( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, nullptr, mapping ); + } + else + { + CONSTRUCT_EMPTY_ANY( pDest ); + } +} + +void SAL_CALL uno_any_destruct( uno_Any * pValue, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + _destructAny( pValue, release ); +} + +void SAL_CALL uno_any_clear( uno_Any * pValue, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + _destructAny( pValue, release ); + CONSTRUCT_EMPTY_ANY( pValue ); +} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/assign.hxx b/cppu/source/uno/assign.hxx new file mode 100644 index 0000000000..3e2893fde8 --- /dev/null +++ b/cppu/source/uno/assign.hxx @@ -0,0 +1,449 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + +#include "prim.hxx" +#include "destr.hxx" +#include "constr.hxx" +#include "copy.hxx" + + +namespace cppu +{ + + +//#### assignment ################################################################################## + + +inline void _assignInterface( + void ** ppDest, void * pSource, + uno_AcquireFunc acquire, uno_ReleaseFunc release ) + +{ + _acquire( pSource, acquire ); + void * const pToBeReleased = *ppDest; + *ppDest = pSource; + _release( pToBeReleased, release ); +} + +inline void * _queryInterface( + void * pSource, + typelib_TypeDescriptionReference * pDestType, + uno_QueryInterfaceFunc queryInterface ) +{ + if (pSource) + { + if (nullptr == queryInterface) + queryInterface = binuno_queryInterface; + pSource = (*queryInterface)( pSource, pDestType ); + } + return pSource; +} + +bool assignStruct( + void * pDest, void * pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release ); + +inline bool _assignStruct( + void * pDest, void * pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release ) +{ + if (pTypeDescr->pBaseTypeDescription) + { + // copy base value + if (! assignStruct( pDest, pSource, pTypeDescr->pBaseTypeDescription, + queryInterface, acquire, release )) + { + return false; + } + } + // then copy members + typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs; + sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets; + sal_Int32 nDescr = pTypeDescr->nMembers; + while (nDescr--) + { + if (! ::uno_type_assignData( static_cast<char *>(pDest) + pMemberOffsets[nDescr], + ppTypeRefs[nDescr], + static_cast<char *>(pSource) + pMemberOffsets[nDescr], + ppTypeRefs[nDescr], + queryInterface, acquire, release )) + { + return false; + } + } + return true; +} + +inline bool _assignData( + void * pDest, + typelib_TypeDescriptionReference * pDestType, typelib_TypeDescription * pDestTypeDescr, + void * pSource, + typelib_TypeDescriptionReference * pSourceType, typelib_TypeDescription * pSourceTypeDescr, + uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release ) +{ + if (pDest == pSource) + return _type_equals( pDestType, pSourceType ); + + if (! pSource) + { + _destructData( pDest, pDestType, pDestTypeDescr, release ); + _defaultConstructData( pDest, pDestType, pDestTypeDescr ); + return true; + } + while (typelib_TypeClass_ANY == pSourceType->eTypeClass) + { + pSourceTypeDescr = nullptr; + pSourceType = static_cast<uno_Any *>(pSource)->pType; + pSource = static_cast<uno_Any *>(pSource)->pData; + if (pDest == pSource) + return true; + } + + switch (pDestType->eTypeClass) + { + case typelib_TypeClass_VOID: + return pSourceType->eTypeClass == typelib_TypeClass_VOID; + case typelib_TypeClass_CHAR: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_CHAR: + *static_cast<sal_Unicode *>(pDest) = *static_cast<sal_Unicode *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_BOOLEAN: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BOOLEAN: + *static_cast<sal_Bool *>(pDest) = bool(*static_cast<sal_Bool *>(pSource)); + return true; + default: + return false; + } + case typelib_TypeClass_BYTE: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<sal_Int8 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_SHORT: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<sal_Int16 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_Int16 *>(pDest) = *static_cast<sal_Int16 *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_UNSIGNED_SHORT: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<sal_uInt16 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_uInt16 *>(pDest) = *static_cast<sal_uInt16 *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_LONG: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int16 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_uInt16 *>(pSource); + return true; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int32 *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_UNSIGNED_LONG: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_Int16 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_uInt16 *>(pSource); + return true; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + *static_cast<sal_uInt32 *>(pDest) = *static_cast<sal_uInt32 *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_HYPER: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int16 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_uInt16 *>(pSource); + return true; + case typelib_TypeClass_LONG: + *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int32 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_LONG: + *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_uInt32 *>(pSource); + return true; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int64 *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_UNSIGNED_HYPER: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_Int16 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_uInt16 *>(pSource); + return true; + case typelib_TypeClass_LONG: + *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_Int32 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_LONG: + *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_uInt32 *>(pSource); + return true; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + *static_cast<sal_uInt64 *>(pDest) = *static_cast<sal_uInt64 *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_FLOAT: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<float *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + *static_cast<float *>(pDest) = *static_cast<sal_Int16 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<float *>(pDest) = *static_cast<sal_uInt16 *>(pSource); + return true; + case typelib_TypeClass_FLOAT: + *static_cast<float *>(pDest) = *static_cast<float *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_DOUBLE: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_BYTE: + *static_cast<double *>(pDest) = *static_cast<sal_Int8 *>(pSource); + return true; + case typelib_TypeClass_SHORT: + *static_cast<double *>(pDest) = *static_cast<sal_Int16 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<double *>(pDest) = *static_cast<sal_uInt16 *>(pSource); + return true; + case typelib_TypeClass_LONG: + *static_cast<double *>(pDest) = *static_cast<sal_Int32 *>(pSource); + return true; + case typelib_TypeClass_UNSIGNED_LONG: + *static_cast<double *>(pDest) = *static_cast<sal_uInt32 *>(pSource); + return true; + case typelib_TypeClass_FLOAT: + *static_cast<double *>(pDest) = *static_cast<float *>(pSource); + return true; + case typelib_TypeClass_DOUBLE: + *static_cast<double *>(pDest) = *static_cast<double *>(pSource); + return true; + default: + return false; + } + case typelib_TypeClass_STRING: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_STRING: + ::rtl_uString_assign( static_cast<rtl_uString **>(pDest), *static_cast<rtl_uString **>(pSource) ); + return true; + default: + return false; + } + case typelib_TypeClass_TYPE: + switch (pSourceType->eTypeClass) + { + case typelib_TypeClass_TYPE: + { + typelib_TypeDescriptionReference ** pp = static_cast<typelib_TypeDescriptionReference **>(pDest); + ::typelib_typedescriptionreference_release( *pp ); + *pp = *static_cast<typelib_TypeDescriptionReference **>(pSource); + TYPE_ACQUIRE( *pp ); + return true; + } + default: + return false; + } + case typelib_TypeClass_ANY: + _destructAny( static_cast<uno_Any *>(pDest), release ); + _copyConstructAny( static_cast<uno_Any *>(pDest), pSource, pSourceType, pSourceTypeDescr, acquire, nullptr ); + return true; + case typelib_TypeClass_ENUM: + if (_type_equals( pDestType, pSourceType )) + { + *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int32 *>(pSource); + return true; + } + return false; + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + if (typelib_TypeClass_STRUCT == pSourceType->eTypeClass || + typelib_TypeClass_EXCEPTION == pSourceType->eTypeClass) + { + bool bRet = false; + if (pSourceTypeDescr) + { + typelib_CompoundTypeDescription * pTypeDescr = + reinterpret_cast<typelib_CompoundTypeDescription *>(pSourceTypeDescr); + while (pTypeDescr && + !_type_equals(pTypeDescr->aBase.pWeakRef, pDestType)) + { + pTypeDescr = pTypeDescr->pBaseTypeDescription; + } + if (pTypeDescr) + { + bRet = _assignStruct( + pDest, pSource, pTypeDescr, queryInterface, acquire, release ); + } + } + else + { + TYPELIB_DANGER_GET( &pSourceTypeDescr, pSourceType ); + typelib_CompoundTypeDescription * pTypeDescr = + reinterpret_cast<typelib_CompoundTypeDescription *>(pSourceTypeDescr); + while (pTypeDescr && + !_type_equals(pTypeDescr->aBase.pWeakRef, pDestType)) + { + pTypeDescr = pTypeDescr->pBaseTypeDescription; + } + if (pTypeDescr) + { + bRet = _assignStruct( + pDest, pSource, pTypeDescr, queryInterface, acquire, release ); + } + TYPELIB_DANGER_RELEASE( pSourceTypeDescr ); + } + return bRet; + } + return false; + case typelib_TypeClass_SEQUENCE: + if (!_type_equals( pDestType, pSourceType )) + return false; + // check self assignment (only after _type_equals, to account for shared static empty): + if (*static_cast<uno_Sequence **>(pSource) != *static_cast<uno_Sequence **>(pDest)) + { + osl_atomic_increment( &(*static_cast<uno_Sequence **>(pSource))->nRefCount ); + idestructSequence( + *static_cast<uno_Sequence **>(pDest), pDestType, pDestTypeDescr, release ); + *static_cast<uno_Sequence **>(pDest) = *static_cast<uno_Sequence **>(pSource); + } + return true; + case typelib_TypeClass_INTERFACE: + if (typelib_TypeClass_INTERFACE != pSourceType->eTypeClass) + return false; + if (_type_equals( pDestType, pSourceType )) + { + _assignInterface( static_cast<void **>(pDest), *static_cast<void **>(pSource), acquire, release ); + return true; + } + else if (*static_cast< void ** >(pSource) == nullptr) + { + // A null reference of any interface type can be converted to a null + // reference of any other interface type: + void * const pToBeReleased = *static_cast< void ** >(pDest); + *static_cast< void ** >(pDest) = nullptr; + _release( pToBeReleased, release ); + return true; + } + else + { + if (pSourceTypeDescr) + { + typelib_TypeDescription * pTD = pSourceTypeDescr; + while (pTD && !_type_equals( pTD->pWeakRef, pDestType )) + { + pTD = &reinterpret_cast<typelib_InterfaceTypeDescription *>(pTD)->pBaseTypeDescription->aBase; + } + if (pTD) // is base of dest + { + _assignInterface( static_cast<void **>(pDest), *static_cast<void **>(pSource), acquire, release ); + return true; + } + } + + // query for interface: + void * pQueried = _queryInterface( *static_cast<void **>(pSource), + pDestType, queryInterface ); + if (pQueried != nullptr) { + void * const pToBeReleased = *static_cast<void **>(pDest); + *static_cast<void **>(pDest) = pQueried; + _release( pToBeReleased, release ); + } + return (pQueried != nullptr); + } + default: + OSL_ASSERT(false); + return false; + } +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/cascade_mapping.cxx b/cppu/source/uno/cascade_mapping.cxx new file mode 100644 index 0000000000..f03d88e529 --- /dev/null +++ b/cppu/source/uno/cascade_mapping.cxx @@ -0,0 +1,308 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <osl/interlck.h> +#include <rtl/ustring.hxx> +#include <uno/environment.hxx> +#include <uno/lbnames.h> +#include <uno/mapping.hxx> +#include <uno/dispatcher.h> +#include <o3tl/string_view.hxx> + +#include <cppu/EnvDcp.hxx> + +#include "cascade_mapping.hxx" + +using namespace com::sun::star; + +namespace { + +class MediatorMapping : public uno_Mapping +{ + oslInterlockedCount m_refCount; + + uno::Mapping m_from2uno; + uno::Mapping m_uno2to; + + uno::Environment m_from; + uno::Environment m_interm; + uno::Environment m_to; + +public: + void acquire(); + void release(); + + void mapInterface(void ** ppOut, + void * pInterface, + typelib_InterfaceTypeDescription * pInterfaceTypeDescr); + MediatorMapping(uno_Environment * pFrom, + uno_Environment * pInterm, + uno_Environment * pTo); +}; + +} + +extern "C" { +static void s_acquire(uno_Mapping * mapping) +{ + MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping); + pMediatorMapping->acquire(); +} + +static void s_release(uno_Mapping * mapping) +{ + MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping); + pMediatorMapping->release(); +} + +static void s_mapInterface( + uno_Mapping * mapping, + void ** ppOut, + void * pInterface, + typelib_InterfaceTypeDescription * pInterfaceTypeDescr) +{ + MediatorMapping * pMediatorMapping = static_cast<MediatorMapping *>(mapping); + pMediatorMapping->mapInterface(ppOut, pInterface, pInterfaceTypeDescr); +} +} + +MediatorMapping::MediatorMapping(uno_Environment * pFrom, + uno_Environment * pInterm, + uno_Environment * pTo) + : m_refCount(0), + m_from2uno(pFrom, pInterm), + m_uno2to (pInterm, pTo), + m_from (pFrom), + m_interm (pInterm), + m_to (pTo) +{ + if (!m_from2uno.get() || !m_uno2to.get()) + abort(); + + uno_Mapping::acquire = s_acquire; + uno_Mapping::release = s_release; + uno_Mapping::mapInterface = s_mapInterface; +} + +void MediatorMapping::acquire() +{ + osl_atomic_increment(&m_refCount); +} + +void MediatorMapping::release() +{ + if (osl_atomic_decrement(&m_refCount) == 0) + { + ::uno_revokeMapping(this); + } +} + +extern "C" { static void s_mapInterface_v(va_list * pParam) +{ + void ** ppOut = va_arg(*pParam, void **); + void * pInterface = va_arg(*pParam, void *); + typelib_InterfaceTypeDescription * pInterfaceTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *); + uno_Mapping * pMapping = va_arg(*pParam, uno_Mapping *); + + pMapping->mapInterface(pMapping, ppOut, pInterface, pInterfaceTypeDescr); +}} + +void MediatorMapping::mapInterface( + void ** ppOut, + void * pInterface, + typelib_InterfaceTypeDescription * pInterfaceTypeDescr) +{ + if (*ppOut != nullptr) + { + uno_ExtEnvironment * env = m_to.get()->pExtEnv; + OSL_ASSERT( env != nullptr ); + env->releaseInterface( env, *ppOut ); + *ppOut = nullptr; + } + + void * ret = nullptr; + uno_Interface * pUnoI = nullptr; + + m_from.invoke(s_mapInterface_v, &pUnoI, pInterface, pInterfaceTypeDescr, m_from2uno.get()); + + m_uno2to.mapInterface(&ret, pUnoI, pInterfaceTypeDescr); + + if (pUnoI) + m_interm.get()->pExtEnv->releaseInterface(m_interm.get()->pExtEnv, pUnoI); + + *ppOut = ret; +} + +extern "C" { static void s_MediatorMapping_free(uno_Mapping * pMapping) + SAL_THROW_EXTERN_C() +{ + delete static_cast<MediatorMapping *>(pMapping); +}} + + +static OUString getPrefix(std::u16string_view str1, std::u16string_view str2) +{ + sal_Int32 nIndex1 = 0; + sal_Int32 nIndex2 = 0; + sal_Int32 sim = 0; + + std::u16string_view token1; + std::u16string_view token2; + + do + { + token1 = o3tl::getToken(str1, 0, ':', nIndex1); + token2 = o3tl::getToken(str2, 0, ':', nIndex2); + + if (token1 == token2) + sim += token1.size() + 1; + } + while(nIndex1 == nIndex2 && nIndex1 >= 0 && token1 == token2); + + OUString result; + + if (sim) + result = str1.substr(0, sim - 1); + + return result; +} + +// OUString str1("abc:def:ghi"); +// OUString str2("abc:def"); +// OUString str3("abc"); +// OUString str4(""); + +// OUString pref; + +// pref = getPrefix(str1, str1); +// pref = getPrefix(str1, str2); +// pref = getPrefix(str1, str3); +// pref = getPrefix(str1, str4); + +// pref = getPrefix(str2, str1); +// pref = getPrefix(str3, str1); +// pref = getPrefix(str4, str1); + + +void getCascadeMapping(uno_Mapping ** ppMapping, + uno_Environment * pFrom, + uno_Environment * pTo, + rtl_uString * pAddPurpose) +{ + if (pAddPurpose && pAddPurpose->length) + return; + + OUString uno_envType(UNO_LB_UNO); + + OUString from_envType = cppu::EnvDcp::getTypeName(pFrom->pTypeName); + OUString to_envType = cppu::EnvDcp::getTypeName(pTo->pTypeName); + OUString from_envPurpose = cppu::EnvDcp::getPurpose(pFrom->pTypeName); + OUString to_envPurpose = cppu::EnvDcp::getPurpose(pTo->pTypeName); + +#ifdef LOG_CALLING_named_purpose_getMapping + OString s_from_name = OUStringToOString(pFrom->pTypeName, RTL_TEXTENCODING_ASCII_US); + OString s_to_name = OUStringToOString(pTo->pTypeName, RTL_TEXTENCODING_ASCII_US); + + std::cerr << __FUNCTION__ << " - creating mediation "; + std::cerr << "pFrom: " << s_from_name.getStr(); + std::cerr <<" pTo: " << s_to_name.getStr() << std::endl; +#endif + + if (from_envPurpose == to_envPurpose) // gcc:bla => uno:bla + return; + + // reaching this point means, we need a mediated mapping!!! + // we generally mediate via uno[:free] + uno_Environment * pInterm = nullptr; + + // chained uno -> uno + if (from_envType == uno_envType && to_envType == uno_envType) + { + OUString purpose = getPrefix(from_envPurpose, to_envPurpose); + + OUString uno_envDcp = uno_envType + purpose; + + // direct mapping possible? + // uno:bla-->uno:bla:blubb + if (from_envPurpose == purpose) + { + OUString rest = to_envPurpose.copy(purpose.getLength()); + + sal_Int32 index = rest.indexOf(':', 1); + if (index == -1) + { + uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData); + return; + } + + uno_envDcp += rest.subView(0, index); + } + else if (to_envPurpose == purpose) + { + OUString rest = from_envPurpose.copy(purpose.getLength()); + + sal_Int32 index = rest.indexOf(':', 1); + if (index == -1) + { + uno_getMapping(ppMapping, pFrom, pTo, rest.copy(1).pData); + return; + } + + uno_envDcp += rest.subView(0, index); + } + + uno_getEnvironment(&pInterm, uno_envDcp.pData, nullptr); + } + else if (from_envType != uno_envType && to_envType == uno_envType) // <ANY> -> UNO ? + // mediate via uno:purpose(fromEnv) + { + OUString envDcp = uno_envType + from_envPurpose; + uno_getEnvironment(&pInterm, envDcp.pData, nullptr); + } + else if (from_envType == uno_envType && to_envType != uno_envType) // UNO -> <ANY>? + // mediate via uno(context) + { + OUString envDcp = uno_envType + to_envPurpose; + uno_getEnvironment(&pInterm, envDcp.pData, nullptr); + } + else // everything else + // mediate via uno:purpose + { + OUString purpose = getPrefix(from_envPurpose, to_envPurpose); + + OUString uno_envDcp = uno_envType + purpose; + + uno_getEnvironment(&pInterm, uno_envDcp.pData, nullptr); + } + + uno_Mapping * pMapping = new MediatorMapping(pFrom, pInterm, pTo); + pInterm->release(pInterm); + + + pMapping->acquire(pMapping); + + ::uno_registerMapping(&pMapping, s_MediatorMapping_free, pFrom, pTo, pAddPurpose); + + if (*ppMapping) + (*ppMapping)->release(*ppMapping); + + *ppMapping = pMapping; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/cascade_mapping.hxx b/cppu/source/uno/cascade_mapping.hxx new file mode 100644 index 0000000000..438143f6b7 --- /dev/null +++ b/cppu/source/uno/cascade_mapping.hxx @@ -0,0 +1,32 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <uno/environment.h> +#include <uno/mapping.h> +#include <rtl/ustring.h> + + +void getCascadeMapping(uno_Mapping ** ppMapping, + uno_Environment * pFrom, + uno_Environment * pTo, + rtl_uString * pAddPurpose ); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/check.cxx b/cppu/source/uno/check.cxx new file mode 100644 index 0000000000..561434aa55 --- /dev/null +++ b/cppu/source/uno/check.cxx @@ -0,0 +1,335 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <cassert> + +#include <cppu/macros.hxx> +#include <rtl/ustring.hxx> +#include <sal/types.h> +#include <uno/any2.h> + + +namespace { + +#if defined( _WIN32) +#pragma pack(push, 8) +#endif + +struct C1 +{ + sal_Int16 n1; +}; +struct C2 : public C1 +{ + sal_Int32 n2 CPPU_GCC3_ALIGN( C1 ); +}; +struct C3 : public C2 +{ + double d3; + sal_Int32 n3; +}; +struct C4 : public C3 +{ + sal_Int32 n4 CPPU_GCC3_ALIGN( C3 ); + double d4; +}; +struct C5 : public C4 +{ + sal_Int64 n5; + sal_Bool b5; +}; +struct C6 : public C1 +{ + C5 c6 CPPU_GCC3_ALIGN( C1 ); + sal_Bool b6; +}; + +struct D +{ + sal_Int16 d; + sal_Int32 e; +}; +struct E +{ + // [-loplugin:fakebool] false positive: + sal_Bool a; + // [-loplugin:fakebool] false positive: + sal_Bool b; + // [-loplugin:fakebool] false positive: + sal_Bool c; + sal_Int16 d; + sal_Int32 e; +}; + +struct M +{ + sal_Int32 n; + sal_Int16 o; +}; + +struct N : public M +{ + sal_Int16 p CPPU_GCC3_ALIGN( M ); +}; +struct N2 +{ + M m; + sal_Int16 p; +}; + +struct O : public M +{ + double p; + sal_Int16 q; +}; +struct O2 : public O +{ + sal_Int16 p2 CPPU_GCC3_ALIGN( O ); +}; + +struct P : public N +{ + double p2; +}; + +struct empty +{ +}; +struct second : public empty +{ + int a; +}; + +struct AlignSize_Impl +{ + sal_Int16 nInt16; + double dDouble; +}; + +struct Char1 +{ + char c1; +}; +struct Char2 : public Char1 +{ + char c2 CPPU_GCC3_ALIGN( Char1 ); +}; +struct Char3 : public Char2 +{ + char c3 CPPU_GCC3_ALIGN( Char2 ); +}; +enum Enum +{ + v = SAL_MAX_ENUM +}; + +#ifdef _WIN32 +# pragma pack(pop) +#endif + +// [-loplugin:fakebool] false positive: +static_assert( static_cast<sal_Bool>(true) == sal_True, + "must be binary compatible" ); +// [-loplugin:fakebool] false positive: +static_assert( static_cast<sal_Bool>(false) == sal_False, + "must be binary compatible" ); +#if SAL_TYPES_ALIGNMENT8 == 2 +static_assert(offsetof(AlignSize_Impl, dDouble) == 2, "offsetof(AlignSize_Impl, dDouble) != 2"); +static_assert(sizeof(AlignSize_Impl) == 10, "sizeof(AlignSize_Impl) != 10"); +#elif SAL_TYPES_ALIGNMENT8 == 4 +static_assert(offsetof(AlignSize_Impl, dDouble) == 4, "offsetof(AlignSize_Impl, dDouble) != 4"); +static_assert(sizeof(AlignSize_Impl) == 12, "sizeof(AlignSize_Impl) != 12"); +#elif SAL_TYPES_ALIGNMENT8 == 8 +static_assert(offsetof(AlignSize_Impl, dDouble) == 8, "offsetof(AlignSize_Impl, dDouble) != 8"); +static_assert(sizeof(AlignSize_Impl) == 16, "sizeof(AlignSize_Impl) != 16"); +#else +# error unexpected alignment of 8 byte types +#endif + +// sequence +static_assert((SAL_SEQUENCE_HEADER_SIZE % 8) == 0, "binary compatibility test failed: (SAL_SEQUENCE_HEADER_SIZE % 8) == 0!!!"); +// enum +static_assert(sizeof(Enum) == sizeof(sal_Int32), "binary compatibility test failed: (sizeof(Enum) == sizeof(sal_Int32))"); +// any +static_assert(sizeof(void *) >= sizeof(sal_Int32), "binary compatibility test failed: (sizeof(void *) >= sizeof(sal_Int32))"); +static_assert(sizeof(uno_Any) == sizeof(void *) * 3, "binary compatibility test failed: (sizeof(uno_Any) == sizeof(void *) * 3"); +static_assert(offsetof(uno_Any, pType) == 0, "offsetof(uno_Any, pType) != 0"); +static_assert(offsetof(uno_Any, pData) == 1 * sizeof(void *), "offsetof(uno_Any, pTData) != (1 * sizeof(void *))"); +static_assert(offsetof(uno_Any, pReserved) == 2 * sizeof(void *), "offsetof(uno_Any, pReserved) != (2 * sizeof(void *))"); +// string +static_assert(sizeof(OUString) == sizeof(rtl_uString *), "binary compatibility test failed: sizeof(OUString) != sizeof(rtl_uString *)"); +// struct +#if SAL_TYPES_ALIGNMENT8 == 2 +static_assert(sizeof(M) == 6, "sizeof(M) != 6"); +static_assert(sizeof(N) == 8, "sizeof(N) != 8"); +static_assert(sizeof(N2) == 8, "sizeof(N2) != 8"); +static_assert(offsetof(N2, p) == 6, "offsetof(N2, p) != 6"); +#else +static_assert(sizeof(M) == 8, "sizeof(M) != 8"); +static_assert(sizeof(N) == 12, "sizeof(N) != 12"); +static_assert(sizeof(N2) == 12, "sizeof(N2) != 12"); +static_assert(offsetof(N2, p) == 8, "offsetof(N2, p) != 8"); +#endif +static_assert(offsetof(M, o) == 4, "offsetof(M, o) != 4"); + +#if SAL_TYPES_ALIGNMENT8 == 2 +static_assert(sizeof(O) == 16, "sizeof(O) != 16"); +#elif SAL_TYPES_ALIGNMENT8 == 4 +static_assert(sizeof(O) == 20, "sizeof(O) != 20"); +#elif SAL_TYPES_ALIGNMENT8 == 8 +static_assert(sizeof(O) == 24, "sizeof(O) != 24"); +#else +# error unexpected alignment of 8 byte types +#endif + +#if SAL_TYPES_ALIGNMENT8 == 2 +static_assert(sizeof(C2) == 6, "sizeof(C2) != 6"); +static_assert(sizeof(D) == 6, "sizeof(D) != 6"); +static_assert(offsetof(D, e) == 2, "offsetof(D, e) != 2"); +static_assert(offsetof(E, e) == 6, "offsetof(E, e) != 6"); +#else +static_assert(sizeof(C2) == 8, "sizeof(C2) != 8"); +static_assert(sizeof(D) == 8, "sizeof(D) != 8"); +static_assert(offsetof(D, e) == 4, "offsetof(D, e) != 4"); +static_assert(offsetof(E, e) == 8, "offsetof(E, e) != 8"); +#endif + +static_assert(sizeof(C1) == 2, "sizeof(C1) != 2"); +static_assert(offsetof(E, d) == 4, "offsetof(E, d) != 4"); + +#if SAL_TYPES_ALIGNMENT8 == 2 +static_assert(sizeof(C3) == 18, "sizeof(C3) != 18"); +static_assert(sizeof(C4) == 30, "sizeof(C4) != 30"); +static_assert(sizeof(C5) == 40, "sizeof(C5) != 40"); +static_assert(sizeof(C6) == 44, "sizeof(C6) != 44"); + +static_assert(sizeof(O2) == 18, "sizeof(O2) != 18"); +#elif SAL_TYPES_ALIGNMENT8 == 4 +static_assert(sizeof(C3) == 20, "sizeof(C3) != 20"); +static_assert(sizeof(C4) == 32, "sizeof(C4) != 32"); +static_assert(sizeof(C5) == 44, "sizeof(C5) != 44"); +static_assert(sizeof(C6) == 52, "sizeof(C6) != 52"); + +static_assert(sizeof(O2) == 24, "sizeof(O2) != 24"); +#elif SAL_TYPES_ALIGNMENT8 == 8 +static_assert(sizeof(C3) == 24, "sizeof(C3) != 24"); +static_assert(sizeof(C4) == 40, "sizeof(C4) != 40"); +static_assert(sizeof(C5) == 56, "sizeof(C5) != 56"); +static_assert(sizeof(C6) == 72, "sizeof(C6) != 72"); + +static_assert(sizeof(O2) == 32, "sizeof(O2) != 32"); +#else +# error unexpected alignment of 8 byte types +#endif + +static_assert(sizeof(Char3) == 3, "sizeof(Char3) != 3"); + +#if SAL_TYPES_ALIGNMENT8 == 2 +// max alignment is 2 +static_assert(sizeof(P) == 16, "sizeof(P) != 16"); +#elif SAL_TYPES_ALIGNMENT8 == 4 +// max alignment is 4 +static_assert(sizeof(P) == 20, "sizeof(P) != 20"); +#elif SAL_TYPES_ALIGNMENT8 == 8 +// alignment of P is 8, because of P[] ... +static_assert(sizeof(P) == 24, "sizeof(P) != 24"); +static_assert(sizeof(second) == sizeof(int), "sizeof(second) != sizeof(int)"); +#else +# error unexpected alignment of 8 byte types +#endif + +#if OSL_DEBUG_LEVEL > 0 && !defined NDEBUG + +struct Char4 +{ +#if defined __GNUC__ && (__GNUC__ < 12 || (__GNUC__ == 12 && __GNUC_MINOR__ < 1)) && !defined __clang__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wattributes" +#endif + [[maybe_unused]] Char3 chars; +#if defined __GNUC__ && (__GNUC__ < 12 || (__GNUC__ == 12 && __GNUC_MINOR__ < 1)) && !defined __clang__ +#pragma GCC diagnostic pop +#endif + char c; +}; + +template<typename T1, typename T2> std::size_t OFFSET_OF(T2 T1::* p) { + return reinterpret_cast< size_t >(reinterpret_cast<char *>(&(reinterpret_cast<T1 *>(16)->*p)) -16); +} + +class BinaryCompatible_Impl +{ +public: + BinaryCompatible_Impl(); +}; +BinaryCompatible_Impl::BinaryCompatible_Impl() +{ + assert(OFFSET_OF(&N::p) == 8); + + assert(OFFSET_OF(&C2::n2) == 4); + +#if SAL_TYPES_ALIGNMENT8 == 2 + assert(OFFSET_OF(&C3::d3) == 6); + assert(OFFSET_OF(&C3::n3) == 14); + assert(OFFSET_OF(&C4::n4) == 18); + assert(OFFSET_OF(&C4::d4) == 22); + assert(OFFSET_OF(&C5::n5) == 30); + assert(OFFSET_OF(&C5::b5) == 38); + assert(OFFSET_OF(&C6::c6) == 2); + assert(OFFSET_OF(&C6::b6) == 42); + + assert(OFFSET_OF(&O2::p2) == 16); +#elif SAL_TYPES_ALIGNMENT8 == 4 + assert(OFFSET_OF(&C3::d3) == 8); + assert(OFFSET_OF(&C3::n3) == 16); + assert(OFFSET_OF(&C4::n4) == 20); + assert(OFFSET_OF(&C4::d4) == 24); + assert(OFFSET_OF(&C5::n5) == 32); + assert(OFFSET_OF(&C5::b5) == 40); + assert(OFFSET_OF(&C6::c6) == 4); + assert(OFFSET_OF(&C6::b6) == 48); + + assert(OFFSET_OF(&O2::p2) == 20); +#elif SAL_TYPES_ALIGNMENT8 == 8 + assert(OFFSET_OF(&C3::d3) == 8); + assert(OFFSET_OF(&C3::n3) == 16); + assert(OFFSET_OF(&C4::n4) == 24); + assert(OFFSET_OF(&C4::d4) == 32); + assert(OFFSET_OF(&C5::n5) == 40); + assert(OFFSET_OF(&C5::b5) == 48); + assert(OFFSET_OF(&C6::c6) == 8); + assert(OFFSET_OF(&C6::b6) == 64); + + assert(OFFSET_OF(&O2::p2) == 24); +#else +# error unexpected alignment of 8 byte types +#endif + + assert(OFFSET_OF(&Char4::c) == 3); +} + +BinaryCompatible_Impl aTest; + +#endif + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/constr.hxx b/cppu/source/uno/constr.hxx new file mode 100644 index 0000000000..9ca2eebf8e --- /dev/null +++ b/cppu/source/uno/constr.hxx @@ -0,0 +1,138 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + +#include "prim.hxx" +#include <osl/diagnose.h> + +namespace cppu +{ + + +//#### construction ################################################################################ + + +void defaultConstructStruct( + void * pMem, + typelib_CompoundTypeDescription * pCompType ); + +inline void _defaultConstructStruct( + void * pMem, + typelib_CompoundTypeDescription * pTypeDescr ) +{ + if (pTypeDescr->pBaseTypeDescription) + { + defaultConstructStruct( pMem, pTypeDescr->pBaseTypeDescription ); + } + + typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs; + sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets; + sal_Int32 nDescr = pTypeDescr->nMembers; + + while (nDescr--) + { + ::uno_type_constructData( static_cast<char *>(pMem) + pMemberOffsets[nDescr], ppTypeRefs[nDescr] ); + } +} + + +inline void _defaultConstructData( + void * pMem, + typelib_TypeDescriptionReference * pType, + typelib_TypeDescription * pTypeDescr ) +{ + switch (pType->eTypeClass) + { + case typelib_TypeClass_CHAR: + *static_cast<sal_Unicode *>(pMem) = '\0'; + break; + case typelib_TypeClass_BOOLEAN: + *static_cast<sal_Bool *>(pMem) = false; + break; + case typelib_TypeClass_BYTE: + *static_cast<sal_Int8 *>(pMem) = 0; + break; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_Int16 *>(pMem) = 0; + break; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + *static_cast<sal_Int32 *>(pMem) = 0; + break; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + *static_cast<sal_Int64 *>(pMem) = 0; + break; + case typelib_TypeClass_FLOAT: + *static_cast<float *>(pMem) = 0.0; + break; + case typelib_TypeClass_DOUBLE: + *static_cast<double *>(pMem) = 0.0; + break; + case typelib_TypeClass_STRING: + *static_cast<rtl_uString **>(pMem) = nullptr; + ::rtl_uString_new( static_cast<rtl_uString **>(pMem) ); + break; + case typelib_TypeClass_TYPE: + *static_cast<typelib_TypeDescriptionReference **>(pMem) = _getVoidType(); + break; + case typelib_TypeClass_ANY: + CONSTRUCT_EMPTY_ANY( static_cast<uno_Any *>(pMem) ); + break; + case typelib_TypeClass_ENUM: + if (pTypeDescr) + { + *static_cast<sal_Int32 *>(pMem) = reinterpret_cast<typelib_EnumTypeDescription *>(pTypeDescr)->nDefaultEnumValue; + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + *static_cast<sal_Int32 *>(pMem) = reinterpret_cast<typelib_EnumTypeDescription *>(pTypeDescr)->nDefaultEnumValue; + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + if (pTypeDescr) + { + _defaultConstructStruct( pMem, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr) ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + _defaultConstructStruct( pMem, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr) ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_SEQUENCE: + *static_cast<uno_Sequence **>(pMem) = createEmptySequence(); + break; + case typelib_TypeClass_INTERFACE: + *static_cast<void **>(pMem) = nullptr; // either cpp or c-uno interface + break; + default: + OSL_ASSERT(false); + break; + } +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/copy.hxx b/cppu/source/uno/copy.hxx new file mode 100644 index 0000000000..6a71d413fa --- /dev/null +++ b/cppu/source/uno/copy.hxx @@ -0,0 +1,655 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + +#include "prim.hxx" +#include "constr.hxx" +#include <cassert> +#include <cstddef> +#include <cstdlib> +#include <type_traits> + +namespace cppu +{ + + +//#### copy construction ########################################################################### + +namespace { + +// The non-dynamic prefix of sal_Sequence (aka uno_Sequence): +struct SequencePrefix { + sal_Int32 nRefCount; + sal_Int32 nElements; +}; +static_assert(sizeof (SequencePrefix) < sizeof (uno_Sequence)); +static_assert(offsetof(SequencePrefix, nRefCount) == offsetof(uno_Sequence, nRefCount)); +static_assert( + std::is_same_v<decltype(SequencePrefix::nRefCount), decltype(uno_Sequence::nRefCount)>); +static_assert(offsetof(SequencePrefix, nElements) == offsetof(uno_Sequence, nElements)); +static_assert( + std::is_same_v<decltype(SequencePrefix::nElements), decltype(uno_Sequence::nElements)>); + +} + +inline uno_Sequence * allocSeq( + sal_Int32 nElementSize, sal_Int32 nElements ) +{ + OSL_ASSERT( nElements >= 0 && nElementSize >= 0 ); + uno_Sequence * pSeq = nullptr; + sal_uInt32 nSize = calcSeqMemSize( nElementSize, nElements ); + if (nSize > 0) + { + pSeq = static_cast<uno_Sequence *>(std::malloc( nSize )); + if (pSeq != nullptr) + { + // header init, going via SequencePrefix to avoid UBSan insufficient-object-size + // warnings when `nElements == 0` and thus `nSize < sizeof (uno_Sequence)`: + auto const header = reinterpret_cast<SequencePrefix *>(pSeq); + header->nRefCount = 1; + header->nElements = nElements; + } + } + return pSeq; +} + + +void copyConstructStruct( + void * pDest, void * pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_Mapping * mapping ); + +inline void _copyConstructStruct( + void * pDest, void * pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_Mapping * mapping ) +{ + if (pTypeDescr->pBaseTypeDescription) + { + // copy base value + copyConstructStruct( pDest, pSource, pTypeDescr->pBaseTypeDescription, acquire, mapping ); + } + + // then copy members + typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs; + sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets; + sal_Int32 nDescr = pTypeDescr->nMembers; + + if (mapping) + { + while (nDescr--) + { + ::uno_type_copyAndConvertData( + static_cast<char *>(pDest) + pMemberOffsets[nDescr], + static_cast<char *>(pSource) + pMemberOffsets[nDescr], + ppTypeRefs[nDescr], mapping ); + } + } + else + { + while (nDescr--) + { + ::uno_type_copyData( + static_cast<char *>(pDest) + pMemberOffsets[nDescr], + static_cast<char *>(pSource) + pMemberOffsets[nDescr], + ppTypeRefs[nDescr], acquire ); + } + } +} + + +uno_Sequence * copyConstructSequence( + uno_Sequence * pSource, + typelib_TypeDescriptionReference * pElementType, + uno_AcquireFunc acquire, uno_Mapping * mapping ); + + +inline void _copyConstructAnyFromData( + uno_Any * pDestAny, void * pSource, + typelib_TypeDescriptionReference * pType, typelib_TypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_Mapping * mapping ) +{ + TYPE_ACQUIRE( pType ); + pDestAny->pType = pType; + + switch (pType->eTypeClass) + { + case typelib_TypeClass_CHAR: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Unicode *>(pDestAny->pData) = *static_cast<sal_Unicode *>(pSource); + break; + case typelib_TypeClass_BOOLEAN: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Bool *>(pDestAny->pData) = bool(*static_cast<sal_Bool *>(pSource)); + break; + case typelib_TypeClass_BYTE: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Int8 *>(pDestAny->pData) = *static_cast<sal_Int8 *>(pSource); + break; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Int16 *>(pDestAny->pData) = *static_cast<sal_Int16 *>(pSource); + break; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Int32 *>(pDestAny->pData) = *static_cast<sal_Int32 *>(pSource); + break; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + if (sizeof(void *) >= sizeof(sal_Int64)) + pDestAny->pData = &pDestAny->pReserved; + else + pDestAny->pData = std::malloc( sizeof(sal_Int64) ); + assert(pDestAny->pData); + *static_cast<sal_Int64 *>(pDestAny->pData) = *static_cast<sal_Int64 *>(pSource); + break; + case typelib_TypeClass_FLOAT: + if (sizeof(void *) >= sizeof(float)) + pDestAny->pData = &pDestAny->pReserved; + else + pDestAny->pData = std::malloc( sizeof(float) ); + assert(pDestAny->pData); + *static_cast<float *>(pDestAny->pData) = *static_cast<float *>(pSource); + break; + case typelib_TypeClass_DOUBLE: + if (sizeof(void *) >= sizeof(double)) + pDestAny->pData = &pDestAny->pReserved; + else + pDestAny->pData = std::malloc( sizeof(double) ); + assert(pDestAny->pData); + *static_cast<double *>(pDestAny->pData) = *static_cast<double *>(pSource); + break; + case typelib_TypeClass_STRING: + ::rtl_uString_acquire( *static_cast<rtl_uString **>(pSource) ); + pDestAny->pData = &pDestAny->pReserved; + *static_cast<rtl_uString **>(pDestAny->pData) = *static_cast<rtl_uString **>(pSource); + break; + case typelib_TypeClass_TYPE: + TYPE_ACQUIRE( *static_cast<typelib_TypeDescriptionReference **>(pSource) ); + pDestAny->pData = &pDestAny->pReserved; + *static_cast<typelib_TypeDescriptionReference **>(pDestAny->pData) = *static_cast<typelib_TypeDescriptionReference **>(pSource); + break; + case typelib_TypeClass_ANY: + OSL_FAIL( "### unexpected nested any!" ); + break; + case typelib_TypeClass_ENUM: + pDestAny->pData = &pDestAny->pReserved; + // enum is forced to 32bit long + *static_cast<sal_Int32 *>(pDestAny->pData) = *static_cast<sal_Int32 *>(pSource); + break; + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + if (pTypeDescr) + { + pDestAny->pData = std::malloc( pTypeDescr->nSize ); + _copyConstructStruct( + pDestAny->pData, pSource, + reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), + acquire, mapping ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + pDestAny->pData = std::malloc( pTypeDescr->nSize ); + _copyConstructStruct( + pDestAny->pData, pSource, + reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), + acquire, mapping ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_SEQUENCE: + pDestAny->pData = &pDestAny->pReserved; + if (pTypeDescr) + { + *static_cast<uno_Sequence **>(pDestAny->pData) = copyConstructSequence( + *static_cast<uno_Sequence **>(pSource), + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + acquire, mapping ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + *static_cast<uno_Sequence **>(pDestAny->pData) = copyConstructSequence( + *static_cast<uno_Sequence **>(pSource), + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + acquire, mapping ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_INTERFACE: + pDestAny->pData = &pDestAny->pReserved; + if (mapping) + { + pDestAny->pReserved = _map( *static_cast<void **>(pSource), pType, pTypeDescr, mapping ); + } + else + { + pDestAny->pReserved = *static_cast<void **>(pSource); + _acquire( pDestAny->pReserved, acquire ); + } + break; + default: + OSL_ASSERT(false); + break; + } +} + +inline void _copyConstructAny( + uno_Any * pDestAny, void * pSource, + typelib_TypeDescriptionReference * pType, typelib_TypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_Mapping * mapping ) +{ + if (typelib_TypeClass_VOID == pType->eTypeClass) + { + CONSTRUCT_EMPTY_ANY( pDestAny ); + } + else + { + if (typelib_TypeClass_ANY == pType->eTypeClass) + { + if (pSource) + { + pType = static_cast<uno_Any *>(pSource)->pType; + if (typelib_TypeClass_VOID == pType->eTypeClass) + { + CONSTRUCT_EMPTY_ANY( pDestAny ); + return; + } + pTypeDescr = nullptr; + pSource = static_cast<uno_Any *>(pSource)->pData; + } + else + { + CONSTRUCT_EMPTY_ANY( pDestAny ); + return; + } + } + if (pSource) + { + _copyConstructAnyFromData( pDestAny, pSource, pType, pTypeDescr, acquire, mapping ); + } + else // default construct + { + TYPE_ACQUIRE( pType ); + pDestAny->pType = pType; + switch (pType->eTypeClass) + { + case typelib_TypeClass_CHAR: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Unicode *>(pDestAny->pData) = '\0'; + break; + case typelib_TypeClass_BOOLEAN: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Bool *>(pDestAny->pData) = false; + break; + case typelib_TypeClass_BYTE: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Int8 *>(pDestAny->pData) = 0; + break; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Int16 *>(pDestAny->pData) = 0; + break; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<sal_Int32 *>(pDestAny->pData) = 0; + break; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + if (sizeof(void *) >= sizeof(sal_Int64)) + pDestAny->pData = &pDestAny->pReserved; + else + pDestAny->pData = std::malloc( sizeof(sal_Int64) ); + assert(pDestAny->pData); + *static_cast<sal_Int64 *>(pDestAny->pData) = 0; + break; + case typelib_TypeClass_FLOAT: + if (sizeof(void *) >= sizeof(float)) + pDestAny->pData = &pDestAny->pReserved; + else + pDestAny->pData = std::malloc( sizeof(float) ); + assert(pDestAny->pData); + *static_cast<float *>(pDestAny->pData) = 0.0; + break; + case typelib_TypeClass_DOUBLE: + if (sizeof(void *) >= sizeof(double)) + pDestAny->pData = &pDestAny->pReserved; + else + pDestAny->pData = std::malloc( sizeof(double) ); + assert(pDestAny->pData); + *static_cast<double *>(pDestAny->pData) = 0.0; + break; + case typelib_TypeClass_STRING: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<rtl_uString **>(pDestAny->pData) = nullptr; + ::rtl_uString_new( static_cast<rtl_uString **>(pDestAny->pData) ); + break; + case typelib_TypeClass_TYPE: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<typelib_TypeDescriptionReference **>(pDestAny->pData) = _getVoidType(); + break; + case typelib_TypeClass_ENUM: + pDestAny->pData = &pDestAny->pReserved; + if (pTypeDescr) + { + *static_cast<sal_Int32 *>(pDestAny->pData) = reinterpret_cast<typelib_EnumTypeDescription *>(pTypeDescr)->nDefaultEnumValue; + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + *static_cast<sal_Int32 *>(pDestAny->pData) = reinterpret_cast<typelib_EnumTypeDescription *>(pTypeDescr)->nDefaultEnumValue; + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + if (pTypeDescr) + { + pDestAny->pData = std::malloc( pTypeDescr->nSize ); + _defaultConstructStruct( + pDestAny->pData, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr) ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + pDestAny->pData = std::malloc( pTypeDescr->nSize ); + _defaultConstructStruct( + pDestAny->pData, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr) ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_SEQUENCE: + pDestAny->pData = &pDestAny->pReserved; + *static_cast<uno_Sequence **>(pDestAny->pData) = createEmptySequence(); + break; + case typelib_TypeClass_INTERFACE: + pDestAny->pData = &pDestAny->pReserved; + pDestAny->pReserved = nullptr; // either cpp or c-uno interface + break; + default: + OSL_ASSERT(false); + break; + } + } + } +} + +inline uno_Sequence * icopyConstructSequence( + uno_Sequence * pSource, + typelib_TypeDescriptionReference * pElementType, + uno_AcquireFunc acquire, uno_Mapping * mapping ) +{ + typelib_TypeClass eTypeClass = pElementType->eTypeClass; + if (!mapping || + (eTypeClass <= typelib_TypeClass_ENUM && + eTypeClass != typelib_TypeClass_ANY)) + { + osl_atomic_increment( &pSource->nRefCount ); + return pSource; + } + else // create new sequence + { + uno_Sequence * pDest; + sal_Int32 nElements = pSource->nElements; + if (nElements) + { + switch (eTypeClass) + { + case typelib_TypeClass_ANY: + { + pDest = allocSeq( sizeof (uno_Any), nElements ); + if (pDest != nullptr) + { + uno_Any * pDestElements = reinterpret_cast<uno_Any *>(pDest->elements); + uno_Any * pSourceElements = reinterpret_cast<uno_Any *>(pSource->elements); + for ( sal_Int32 nPos = nElements; nPos--; ) + { + typelib_TypeDescriptionReference * pType = + pSourceElements[nPos].pType; + if (typelib_TypeClass_VOID == pType->eTypeClass) + { + CONSTRUCT_EMPTY_ANY( &pDestElements[nPos] ); + } + else + { + _copyConstructAnyFromData( + &pDestElements[nPos], + pSourceElements[nPos].pData, + pType, nullptr, + acquire, mapping ); + } + } + } + break; + } + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + sal_Int32 nElementSize = pElementTypeDescr->nSize; + char * pSourceElements = pSource->elements; + pDest = allocSeq( nElementSize, nElements ); + if (pDest != nullptr) + { + char * pElements = pDest->elements; + for ( sal_Int32 nPos = nElements; nPos--; ) + { + _copyConstructStruct( + pElements + (nPos * nElementSize), + pSourceElements + (nPos * nElementSize), + reinterpret_cast<typelib_CompoundTypeDescription *>( + pElementTypeDescr), + acquire, mapping ); + } + } + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + break; + } + case typelib_TypeClass_SEQUENCE: // sequence of sequence + { + // coverity[suspicious_sizeof] - sizeof(uno_Sequence*) is correct here + pDest = allocSeq( sizeof (uno_Sequence *), nElements ); + if (pDest != nullptr) + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + typelib_TypeDescriptionReference * pSeqElementType = + reinterpret_cast<typelib_IndirectTypeDescription *>( + pElementTypeDescr)->pType; + + uno_Sequence ** pDestElements = + reinterpret_cast<uno_Sequence **>(pDest->elements); + uno_Sequence ** pSourceElements = + reinterpret_cast<uno_Sequence **>(pSource->elements); + for ( sal_Int32 nPos = nElements; nPos--; ) + { + uno_Sequence * pNew = copyConstructSequence( + pSourceElements[nPos], + pSeqElementType, + acquire, mapping ); + OSL_ASSERT( pNew != nullptr ); + // ought never be a memory allocation problem, + // because of reference counted sequence handles + pDestElements[ nPos ] = pNew; + } + + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + } + break; + } + case typelib_TypeClass_INTERFACE: + { + pDest = allocSeq( sizeof (void *), nElements ); + if (pDest != nullptr) + { + char * pElements = pDest->elements; + void ** pSourceElements = reinterpret_cast<void **>(pSource->elements); + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + for ( sal_Int32 nPos = nElements; nPos--; ) + { + reinterpret_cast<void **>(pElements)[nPos] = nullptr; + if (pSourceElements[nPos]) + { + (*mapping->mapInterface)( + mapping, reinterpret_cast<void **>(pElements) + nPos, + pSourceElements[nPos], + reinterpret_cast<typelib_InterfaceTypeDescription *>( + pElementTypeDescr) ); + } + } + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + } + break; + } + default: + OSL_FAIL( "### unexpected sequence element type!" ); + pDest = nullptr; + break; + } + } + else // empty sequence + { + pDest = allocSeq( 0, 0 ); + } + + return pDest; + } +} + + +inline void _copyConstructData( + void * pDest, void * pSource, + typelib_TypeDescriptionReference * pType, typelib_TypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_Mapping * mapping ) +{ + switch (pType->eTypeClass) + { + case typelib_TypeClass_CHAR: + *static_cast<sal_Unicode *>(pDest) = *static_cast<sal_Unicode *>(pSource); + break; + case typelib_TypeClass_BOOLEAN: + *static_cast<sal_Bool *>(pDest) = bool(*static_cast<sal_Bool *>(pSource)); + break; + case typelib_TypeClass_BYTE: + *static_cast<sal_Int8 *>(pDest) = *static_cast<sal_Int8 *>(pSource); + break; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + *static_cast<sal_Int16 *>(pDest) = *static_cast<sal_Int16 *>(pSource); + break; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int32 *>(pSource); + break; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + *static_cast<sal_Int64 *>(pDest) = *static_cast<sal_Int64 *>(pSource); + break; + case typelib_TypeClass_FLOAT: + *static_cast<float *>(pDest) = *static_cast<float *>(pSource); + break; + case typelib_TypeClass_DOUBLE: + *static_cast<double *>(pDest) = *static_cast<double *>(pSource); + break; + case typelib_TypeClass_STRING: + ::rtl_uString_acquire( *static_cast<rtl_uString **>(pSource) ); + *static_cast<rtl_uString **>(pDest) = *static_cast<rtl_uString **>(pSource); + break; + case typelib_TypeClass_TYPE: + TYPE_ACQUIRE( *static_cast<typelib_TypeDescriptionReference **>(pSource) ); + *static_cast<typelib_TypeDescriptionReference **>(pDest) = *static_cast<typelib_TypeDescriptionReference **>(pSource); + break; + case typelib_TypeClass_ANY: + _copyConstructAny( + static_cast<uno_Any *>(pDest), static_cast<uno_Any *>(pSource)->pData, + static_cast<uno_Any *>(pSource)->pType, nullptr, + acquire, mapping ); + break; + case typelib_TypeClass_ENUM: + *static_cast<sal_Int32 *>(pDest) = *static_cast<sal_Int32 *>(pSource); + break; + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + if (pTypeDescr) + { + _copyConstructStruct( + pDest, pSource, + reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), + acquire, mapping ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + _copyConstructStruct( + pDest, pSource, + reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), + acquire, mapping ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_SEQUENCE: + if (mapping) + { + if (pTypeDescr) + { + *static_cast<uno_Sequence **>(pDest) = icopyConstructSequence( + *static_cast<uno_Sequence **>(pSource), + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + acquire, mapping ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + *static_cast<uno_Sequence **>(pDest) = icopyConstructSequence( + *static_cast<uno_Sequence **>(pSource), + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + acquire, mapping ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + } + else + { + osl_atomic_increment( &(*static_cast<uno_Sequence **>(pSource))->nRefCount ); + *static_cast<uno_Sequence **>(pDest) = *static_cast<uno_Sequence **>(pSource); + } + break; + case typelib_TypeClass_INTERFACE: + if (mapping) + *static_cast<void **>(pDest) = _map( *static_cast<void **>(pSource), pType, pTypeDescr, mapping ); + else + { + *static_cast<void **>(pDest) = *static_cast<void **>(pSource); + _acquire( *static_cast<void **>(pDest), acquire ); + } + break; + default: + break; + } +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/data.cxx b/cppu/source/uno/data.cxx new file mode 100644 index 0000000000..339e650fe4 --- /dev/null +++ b/cppu/source/uno/data.cxx @@ -0,0 +1,316 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <sal/log.hxx> +#include <uno/data.h> + +#include "constr.hxx" +#include "destr.hxx" +#include "copy.hxx" +#include "assign.hxx" +#include "eq.hxx" + +using namespace ::cppu; + +namespace cppu +{ + +// Sequence<>() (default ctor) relies on this being static: +uno_Sequence g_emptySeq = { 1, 0, { 0 } }; +typelib_TypeDescriptionReference * g_pVoidType = nullptr; + + +void * binuno_queryInterface( void * pUnoI, typelib_TypeDescriptionReference * pDestType ) +{ + // init queryInterface() td + static typelib_TypeDescription* g_pQITD = []() { + typelib_TypeDescriptionReference* type_XInterface + = *typelib_static_type_getByTypeClass(typelib_TypeClass_INTERFACE); + typelib_InterfaceTypeDescription* pTXInterfaceDescr = nullptr; + TYPELIB_DANGER_GET(reinterpret_cast<typelib_TypeDescription**>(&pTXInterfaceDescr), + type_XInterface); + assert(pTXInterfaceDescr->ppAllMembers); + typelib_TypeDescription* pQITD = nullptr; + typelib_typedescriptionreference_getDescription(&pQITD, + pTXInterfaceDescr->ppAllMembers[0]); + // coverity[callee_ptr_arith] - not a bug + TYPELIB_DANGER_RELEASE(&pTXInterfaceDescr->aBase); + return pQITD; + }(); + + uno_Any aRet, aExc; + uno_Any * pExc = &aExc; + void * aArgs[ 1 ]; + aArgs[ 0 ] = &pDestType; + (*static_cast<uno_Interface *>(pUnoI)->pDispatcher)( + static_cast<uno_Interface *>(pUnoI), g_pQITD, &aRet, aArgs, &pExc ); + + uno_Interface * ret = nullptr; + if (nullptr == pExc) + { + typelib_TypeDescriptionReference * ret_type = aRet.pType; + switch (ret_type->eTypeClass) + { + case typelib_TypeClass_VOID: // common case + typelib_typedescriptionreference_release( ret_type ); + break; + case typelib_TypeClass_INTERFACE: + // tweaky... avoiding acquire/ release pair + typelib_typedescriptionreference_release( ret_type ); + ret = static_cast<uno_Interface *>(aRet.pReserved); // serving acquired interface + break; + default: + _destructAny( &aRet, nullptr ); + break; + } + } + else + { + SAL_WARN( + "cppu", + "exception occurred querying for interface " + << OUString(pDestType->pTypeName) << ": [" + << OUString(pExc->pType->pTypeName) << "] " + << *static_cast<OUString const *>(pExc->pData)); + // Message is very first member + uno_any_destruct( pExc, nullptr ); + } + return ret; +} + + +void defaultConstructStruct( + void * pMem, + typelib_CompoundTypeDescription * pCompType ) +{ + _defaultConstructStruct( pMem, pCompType ); +} + +void copyConstructStruct( + void * pDest, void * pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_Mapping * mapping ) +{ + _copyConstructStruct( pDest, pSource, pTypeDescr, acquire, mapping ); +} + +void destructStruct( + void * pValue, + typelib_CompoundTypeDescription * pTypeDescr, + uno_ReleaseFunc release ) +{ + _destructStruct( pValue, pTypeDescr, release ); +} + +bool equalStruct( + void * pDest, void *pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) +{ + return _equalStruct( pDest, pSource, pTypeDescr, queryInterface, release ); +} + +bool assignStruct( + void * pDest, void * pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release ) +{ + return _assignStruct( pDest, pSource, pTypeDescr, queryInterface, acquire, release ); +} + + +uno_Sequence * copyConstructSequence( + uno_Sequence * pSource, + typelib_TypeDescriptionReference * pElementType, + uno_AcquireFunc acquire, uno_Mapping * mapping ) +{ + return icopyConstructSequence( pSource, pElementType, acquire, mapping ); +} + + +void destructSequence( + uno_Sequence * pSequence, + typelib_TypeDescriptionReference * pType, + typelib_TypeDescription * pTypeDescr, + uno_ReleaseFunc release ) +{ + idestructSequence( pSequence, pType, pTypeDescr, release ); +} + + +bool equalSequence( + uno_Sequence * pDest, uno_Sequence * pSource, + typelib_TypeDescriptionReference * pElementType, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) +{ + return _equalSequence( pDest, pSource, pElementType, queryInterface, release ); +} + +} + +extern "C" +{ + +void SAL_CALL uno_type_constructData( + void * pMem, typelib_TypeDescriptionReference * pType ) + SAL_THROW_EXTERN_C() +{ + _defaultConstructData( pMem, pType, nullptr ); +} + +void SAL_CALL uno_constructData( + void * pMem, typelib_TypeDescription * pTypeDescr ) + SAL_THROW_EXTERN_C() +{ + _defaultConstructData( pMem, pTypeDescr->pWeakRef, pTypeDescr ); +} + +void SAL_CALL uno_type_destructData( + void * pValue, typelib_TypeDescriptionReference * pType, + uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + _destructData( pValue, pType, nullptr, release ); +} + +void SAL_CALL uno_destructData( + void * pValue, + typelib_TypeDescription * pTypeDescr, + uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + _destructData( pValue, pTypeDescr->pWeakRef, pTypeDescr, release ); +} + +void SAL_CALL uno_type_copyData( + void * pDest, void * pSource, + typelib_TypeDescriptionReference * pType, + uno_AcquireFunc acquire ) + SAL_THROW_EXTERN_C() +{ + _copyConstructData( pDest, pSource, pType, nullptr, acquire, nullptr ); +} + +void SAL_CALL uno_copyData( + void * pDest, void * pSource, + typelib_TypeDescription * pTypeDescr, + uno_AcquireFunc acquire ) + SAL_THROW_EXTERN_C() +{ + _copyConstructData( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, acquire, nullptr ); +} + +void SAL_CALL uno_type_copyAndConvertData( + void * pDest, void * pSource, + typelib_TypeDescriptionReference * pType, + uno_Mapping * mapping ) + SAL_THROW_EXTERN_C() +{ + _copyConstructData( pDest, pSource, pType, nullptr, nullptr, mapping ); +} + +void SAL_CALL uno_copyAndConvertData( + void * pDest, void * pSource, + typelib_TypeDescription * pTypeDescr, + uno_Mapping * mapping ) + SAL_THROW_EXTERN_C() +{ + _copyConstructData( pDest, pSource, pTypeDescr->pWeakRef, pTypeDescr, nullptr, mapping ); +} + +sal_Bool SAL_CALL uno_type_equalData( + void * pVal1, typelib_TypeDescriptionReference * pVal1Type, + void * pVal2, typelib_TypeDescriptionReference * pVal2Type, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + return _equalData( + pVal1, pVal1Type, nullptr, + pVal2, pVal2Type, + queryInterface, release ); +} + +sal_Bool SAL_CALL uno_equalData( + void * pVal1, typelib_TypeDescription * pVal1TD, + void * pVal2, typelib_TypeDescription * pVal2TD, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + return _equalData( + pVal1, pVal1TD->pWeakRef, pVal1TD, + pVal2, pVal2TD->pWeakRef, + queryInterface, release ); +} + +sal_Bool SAL_CALL uno_type_assignData( + void * pDest, typelib_TypeDescriptionReference * pDestType, + void * pSource, typelib_TypeDescriptionReference * pSourceType, + uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + return _assignData( + pDest, pDestType, nullptr, + pSource, pSourceType, nullptr, + queryInterface, acquire, release ); +} + +sal_Bool SAL_CALL uno_assignData( + void * pDest, typelib_TypeDescription * pDestTD, + void * pSource, typelib_TypeDescription * pSourceTD, + uno_QueryInterfaceFunc queryInterface, uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + return _assignData( + pDest, pDestTD->pWeakRef, pDestTD, + pSource, pSourceTD->pWeakRef, pSourceTD, + queryInterface, acquire, release ); +} + +sal_Bool SAL_CALL uno_type_isAssignableFromData( + typelib_TypeDescriptionReference * pAssignable, + void * pFrom, typelib_TypeDescriptionReference * pFromType, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + if (::typelib_typedescriptionreference_isAssignableFrom( pAssignable, pFromType )) + return true; + if (typelib_TypeClass_INTERFACE != pFromType->eTypeClass || + typelib_TypeClass_INTERFACE != pAssignable->eTypeClass) + { + return false; + } + + // query + if (nullptr == pFrom) + return false; + void * pInterface = *static_cast<void **>(pFrom); + if (nullptr == pInterface) + return false; + + if (nullptr == queryInterface) + queryInterface = binuno_queryInterface; + void * p = (*queryInterface)( pInterface, pAssignable ); + _release( p, release ); + return (nullptr != p); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/destr.hxx b/cppu/source/uno/destr.hxx new file mode 100644 index 0000000000..13e4ca044c --- /dev/null +++ b/cppu/source/uno/destr.hxx @@ -0,0 +1,352 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + +#include <sal/config.h> +#include <osl/diagnose.h> + +#include <cassert> +#include <cstdlib> + +#include "prim.hxx" + +namespace cppu +{ + + +//#### destruction ################################################################################# + + +void destructStruct( + void * pValue, + typelib_CompoundTypeDescription * pTypeDescr, + uno_ReleaseFunc release ); + +inline void _destructStruct( + void * pValue, + typelib_CompoundTypeDescription * pTypeDescr, + uno_ReleaseFunc release ) +{ + if (pTypeDescr->pBaseTypeDescription) + { + destructStruct( pValue, pTypeDescr->pBaseTypeDescription, release ); + } + + typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs; + sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets; + sal_Int32 nDescr = pTypeDescr->nMembers; + while (nDescr--) + { + ::uno_type_destructData( + static_cast<char *>(pValue) + pMemberOffsets[nDescr], + ppTypeRefs[nDescr], release ); + } +} + + +void destructSequence( + uno_Sequence * pSequence, + typelib_TypeDescriptionReference * pType, + typelib_TypeDescription * pTypeDescr, + uno_ReleaseFunc release ); + + +inline void _destructAny( + uno_Any * pAny, + uno_ReleaseFunc release ) +{ + typelib_TypeDescriptionReference * pType = pAny->pType; + + switch (pType->eTypeClass) + { + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + if (sizeof(void *) < sizeof(sal_Int64)) + { + std::free( pAny->pData ); + } + break; + case typelib_TypeClass_FLOAT: + if (sizeof(void *) < sizeof(float)) + { + std::free( pAny->pData ); + } + break; + case typelib_TypeClass_DOUBLE: + if (sizeof(void *) < sizeof(double)) + { + std::free( pAny->pData ); + } + break; + case typelib_TypeClass_STRING: + ::rtl_uString_release( static_cast<rtl_uString *>(pAny->pReserved) ); + break; + case typelib_TypeClass_TYPE: + ::typelib_typedescriptionreference_release( + static_cast<typelib_TypeDescriptionReference *>(pAny->pReserved) ); + break; + case typelib_TypeClass_ANY: + OSL_FAIL( "### unexpected nested any!" ); + ::uno_any_destruct( static_cast<uno_Any *>(pAny->pData), release ); + std::free( pAny->pData ); + break; + case typelib_TypeClass_TYPEDEF: + OSL_FAIL( "### unexpected typedef!" ); + break; + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + { + typelib_TypeDescription * pTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + _destructStruct( pAny->pData, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), release ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + std::free( pAny->pData ); + break; + } + case typelib_TypeClass_SEQUENCE: + { + destructSequence( + static_cast<uno_Sequence *>(pAny->pReserved), pType, nullptr, release ); + break; + } + case typelib_TypeClass_INTERFACE: + _release( pAny->pReserved, release ); + break; + default: + break; + } +#if OSL_DEBUG_LEVEL > 0 + pAny->pData = reinterpret_cast<void *>(uintptr_t(0xdeadbeef)); +#endif + + ::typelib_typedescriptionreference_release( pType ); +} + +inline sal_Int32 idestructElements( + void * pElements, typelib_TypeDescriptionReference * pElementType, + sal_Int32 nStartIndex, sal_Int32 nStopIndex, + uno_ReleaseFunc release ) +{ + switch (pElementType->eTypeClass) + { + case typelib_TypeClass_CHAR: + return sal_Int32(sizeof(sal_Unicode)); + case typelib_TypeClass_BOOLEAN: + return sal_Int32(sizeof(sal_Bool)); + case typelib_TypeClass_BYTE: + return sal_Int32(sizeof(sal_Int8)); + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + return sal_Int32(sizeof(sal_Int16)); + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + return sal_Int32(sizeof(sal_Int32)); + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + return sal_Int32(sizeof(sal_Int64)); + case typelib_TypeClass_FLOAT: + return sal_Int32(sizeof(float)); + case typelib_TypeClass_DOUBLE: + return sal_Int32(sizeof(double)); + + case typelib_TypeClass_STRING: + { + rtl_uString ** pDest = static_cast<rtl_uString **>(pElements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + ::rtl_uString_release( pDest[nPos] ); + } + return sal_Int32(sizeof(rtl_uString *)); + } + case typelib_TypeClass_TYPE: + { + typelib_TypeDescriptionReference ** pDest = static_cast<typelib_TypeDescriptionReference **>(pElements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + ::typelib_typedescriptionreference_release( pDest[nPos] ); + } + return sal_Int32(sizeof(typelib_TypeDescriptionReference *)); + } + case typelib_TypeClass_ANY: + { + uno_Any * pDest = static_cast<uno_Any *>(pElements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + _destructAny( &pDest[nPos], release ); + } + return sal_Int32(sizeof(uno_Any)); + } + case typelib_TypeClass_ENUM: + return sal_Int32(sizeof(sal_Int32)); + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + sal_Int32 nElementSize = pElementTypeDescr->nSize; + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + _destructStruct( + static_cast<char *>(pElements) + (nElementSize * nPos), + reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr), + release ); + } + sal_Int32 nSize = pElementTypeDescr->nSize; + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + return nSize; + } + case typelib_TypeClass_SEQUENCE: + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + uno_Sequence ** pDest = static_cast<uno_Sequence **>(pElements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + destructSequence( + pDest[nPos], + pElementTypeDescr->pWeakRef, pElementTypeDescr, + release ); + } + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + return sal_Int32(sizeof(uno_Sequence *)); + } + case typelib_TypeClass_INTERFACE: + { + if (release) + { + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + void * p = static_cast<void **>(pElements)[nPos]; + if (p) + { + (*release)( p ); + } + } + } + else + { + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + uno_Interface * p = static_cast<uno_Interface **>(pElements)[nPos]; + if (p) + { + (*p->release)( p ); + } + } + } + return sal_Int32(sizeof(void *)); + } + default: + OSL_ASSERT(false); + return 0; + } +} + +inline void idestroySequence( + uno_Sequence * pSeq, + typelib_TypeDescriptionReference * pType, + typelib_TypeDescription * pTypeDescr, + uno_ReleaseFunc release ) +{ + assert(pSeq != nullptr); + assert(pSeq->nRefCount == 0); + if (pSeq->nElements > 0) + { + if (pTypeDescr) + { + idestructElements( + pSeq->elements, + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, 0, + pSeq->nElements, release ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + idestructElements( + pSeq->elements, + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, 0, + pSeq->nElements, release ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + } + std::free( pSeq ); +} + +inline void idestructSequence( + uno_Sequence * pSeq, + typelib_TypeDescriptionReference * pType, + typelib_TypeDescription * pTypeDescr, + uno_ReleaseFunc release ) +{ + if (osl_atomic_decrement( &pSeq->nRefCount ) == 0) + { + idestroySequence(pSeq, pType, pTypeDescr, release); + } +} + +inline void _destructData( + void * pValue, + typelib_TypeDescriptionReference * pType, + typelib_TypeDescription * pTypeDescr, + uno_ReleaseFunc release ) +{ + switch (pType->eTypeClass) + { + case typelib_TypeClass_STRING: + ::rtl_uString_release( *static_cast<rtl_uString **>(pValue) ); + break; + case typelib_TypeClass_TYPE: + ::typelib_typedescriptionreference_release( *static_cast<typelib_TypeDescriptionReference **>(pValue) ); + break; + case typelib_TypeClass_ANY: + _destructAny( static_cast<uno_Any *>(pValue), release ); + break; + case typelib_TypeClass_TYPEDEF: + OSL_FAIL( "### unexpected typedef!" ); + break; + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + if (pTypeDescr) + { + _destructStruct( pValue, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), release ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + _destructStruct( pValue, reinterpret_cast<typelib_CompoundTypeDescription *>(pTypeDescr), release ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + break; + case typelib_TypeClass_SEQUENCE: + { + idestructSequence( + *static_cast<uno_Sequence **>(pValue), pType, pTypeDescr, release ); + break; + } + case typelib_TypeClass_INTERFACE: + _release( *static_cast<void **>(pValue), release ); + break; + default: + break; + } +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/eq.hxx b/cppu/source/uno/eq.hxx new file mode 100644 index 0000000000..60a3728867 --- /dev/null +++ b/cppu/source/uno/eq.hxx @@ -0,0 +1,635 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + +#include <cmath> +#include <string.h> + +#include <o3tl/intcmp.hxx> +#include <osl/diagnose.h> +#include <rtl/ustring.hxx> + +#include "prim.hxx" + + +namespace cppu +{ + + +//#### equality #################################################################################### + + +inline bool _equalObject( + void * pI1, void * pI2, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) +{ + if (pI1 == pI2) + return true; + if ((nullptr == pI1) || (nullptr == pI2)) + return false; + bool bRet = false; + + typelib_TypeDescriptionReference * type_XInterface = + * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE ); + if (nullptr == queryInterface) + queryInterface = binuno_queryInterface; + pI1 = (*queryInterface)( pI1, type_XInterface ); + if (nullptr != pI1) + { + pI2 = (*queryInterface)( pI2, type_XInterface ); + if (nullptr != pI2) + { + bRet = (pI1 == pI2); + _release( pI2, release ); + } + _release( pI1, release ); + } + return bRet; +} + + +bool equalStruct( + void * pDest, void *pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ); + +inline bool _equalStruct( + void * pDest, void *pSource, + typelib_CompoundTypeDescription * pTypeDescr, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) +{ + if (pTypeDescr->pBaseTypeDescription && + !equalStruct( pDest, pSource, pTypeDescr->pBaseTypeDescription, queryInterface, release )) + { + return false; + } + + typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs; + sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets; + sal_Int32 nDescr = pTypeDescr->nMembers; + + while (nDescr--) + { + sal_Int32 nOffset = pMemberOffsets[nDescr]; + if (! ::uno_type_equalData( static_cast<char *>(pDest) + nOffset, + ppTypeRefs[nDescr], + static_cast<char *>(pSource) + nOffset, + ppTypeRefs[nDescr], + queryInterface, release )) + { + return false; + } + } + return true; +} + +bool equalSequence( + uno_Sequence * pDest, uno_Sequence * pSource, + typelib_TypeDescriptionReference * pElementType, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ); + +inline bool _equalSequence( + uno_Sequence * pDest, uno_Sequence * pSource, + typelib_TypeDescriptionReference * pElementType, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) +{ + if (pDest == pSource) + return true; + sal_Int32 nElements = pDest->nElements; + if (nElements != pSource->nElements) + return false; + if (! nElements) + return true; + + void * pDestElements = pDest->elements; + void * pSourceElements = pSource->elements; + + switch (pElementType->eTypeClass) + { + case typelib_TypeClass_CHAR: + return (0 == memcmp( pDestElements, pSourceElements, sizeof(sal_Unicode) * nElements )); + case typelib_TypeClass_BOOLEAN: + { + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if (bool(static_cast<sal_Bool *>(pDestElements)[nPos]) != + bool(static_cast<sal_Bool *>(pSourceElements)[nPos])) + { + return false; + } + } + return true; + } + case typelib_TypeClass_BYTE: + return (0 == memcmp( pDestElements, pSourceElements, sizeof(sal_Int8) * nElements )); + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + return (0 == memcmp( pDestElements, pSourceElements, sizeof(sal_Int16) * nElements )); + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + return (0 == memcmp( pDestElements, pSourceElements, sizeof(sal_Int32) * nElements )); + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + return (0 == memcmp( pDestElements, pSourceElements, sizeof(sal_Int64) * nElements )); + case typelib_TypeClass_FLOAT: + { + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if (static_cast<float *>(pDestElements)[nPos] != static_cast<float *>(pSourceElements)[nPos]) + return false; + } + return true; + } + case typelib_TypeClass_DOUBLE: + { + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if (static_cast<double *>(pDestElements)[nPos] != static_cast<double *>(pSourceElements)[nPos]) + return false; + } + return true; + } + case typelib_TypeClass_STRING: + { + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if ( static_cast<OUString *>(pDestElements)[nPos] != static_cast<const OUString *>(pSourceElements)[nPos] ) + return false; + } + return true; + } + case typelib_TypeClass_TYPE: + { + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if (! _type_equals( static_cast<typelib_TypeDescriptionReference **>(pDestElements)[nPos], + static_cast<typelib_TypeDescriptionReference **>(pSourceElements)[nPos] )) + { + return false; + } + } + return true; + } + case typelib_TypeClass_ANY: + { + for ( sal_Int32 nPos = nElements; nPos--; ) + { + uno_Any * pDest2 = static_cast<uno_Any *>(pDestElements) + nPos; + uno_Any * pSource2 = static_cast<uno_Any *>(pSourceElements) + nPos; + if (! ::uno_type_equalData( pDest2->pData, pDest2->pType, + pSource2->pData, pSource2->pType, + queryInterface, release )) + { + return false; + } + } + return true; + } + case typelib_TypeClass_ENUM: + return (0 == memcmp( pDestElements, pSourceElements, sizeof(sal_Int32) * nElements )); + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + sal_Int32 nElementSize = pElementTypeDescr->nSize; + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if (! _equalStruct( static_cast<char *>(pDestElements) + (nPos * nElementSize), + static_cast<char *>(pSourceElements) + (nPos * nElementSize), + reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr), + queryInterface, release )) + { + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + return false; + } + } + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + return true; + } + case typelib_TypeClass_SEQUENCE: // sequence of sequence + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + typelib_TypeDescriptionReference * pSeqElementType = + reinterpret_cast<typelib_IndirectTypeDescription *>(pElementTypeDescr)->pType; + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if (! equalSequence( static_cast<uno_Sequence **>(pDestElements)[nPos], + static_cast<uno_Sequence **>(pSourceElements)[nPos], + pSeqElementType, queryInterface, release )) + { + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + return false; + } + } + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + return true; + } + case typelib_TypeClass_INTERFACE: + { + for ( sal_Int32 nPos = nElements; nPos--; ) + { + if (! _equalObject( static_cast<void **>(pDestElements)[nPos], static_cast<void **>(pSourceElements)[nPos], + queryInterface, release )) + { + return false; + } + } + return true; + } + default: + OSL_ASSERT(false); + return false; + } +} + +inline bool _equalData( + void * pDest, + typelib_TypeDescriptionReference * pDestType, typelib_TypeDescription * pDestTypeDescr, + void * pSource, + typelib_TypeDescriptionReference * pSourceType, + uno_QueryInterfaceFunc queryInterface, uno_ReleaseFunc release ) +{ + typelib_TypeClass eSourceTypeClass, eDestTypeClass; + while (typelib_TypeClass_ANY == (eDestTypeClass = pDestType->eTypeClass)) + { + pDestTypeDescr = nullptr; + pDestType = static_cast<uno_Any *>(pDest)->pType; + pDest = static_cast<uno_Any *>(pDest)->pData; + } + while (typelib_TypeClass_ANY == (eSourceTypeClass = pSourceType->eTypeClass)) + { + pSourceType = static_cast<uno_Any *>(pSource)->pType; + pSource = static_cast<uno_Any *>(pSource)->pData; + } + + switch (eDestTypeClass) + { + case typelib_TypeClass_VOID: + return eSourceTypeClass == typelib_TypeClass_VOID; + case typelib_TypeClass_CHAR: + return eSourceTypeClass == typelib_TypeClass_CHAR + && *static_cast<sal_Unicode *>(pDest) == *static_cast<sal_Unicode *>(pSource); + case typelib_TypeClass_BOOLEAN: + return eSourceTypeClass == typelib_TypeClass_BOOLEAN + && (bool(*static_cast<sal_Bool *>(pDest)) + == bool(*static_cast<sal_Bool *>(pSource))); + case typelib_TypeClass_BYTE: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return o3tl::cmp_equal( + *static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int8 *>(pSource)); + case typelib_TypeClass_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int16 *>(pSource)); + case typelib_TypeClass_UNSIGNED_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int8 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); + case typelib_TypeClass_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_UNSIGNED_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int8 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); + case typelib_TypeClass_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_UNSIGNED_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int8 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (static_cast<float>(*static_cast<sal_Int8 *>(pDest)) == *static_cast<float *>(pSource)); + case typelib_TypeClass_DOUBLE: + return (static_cast<double>(*static_cast<sal_Int8 *>(pDest)) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_SHORT: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return o3tl::cmp_equal( + *static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int8 *>(pSource)); + case typelib_TypeClass_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int16 *>(pSource)); + case typelib_TypeClass_UNSIGNED_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int16 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); + case typelib_TypeClass_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_UNSIGNED_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int16 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); + case typelib_TypeClass_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_UNSIGNED_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int16 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (static_cast<float>(*static_cast<sal_Int16 *>(pDest)) == *static_cast<float *>(pSource)); + case typelib_TypeClass_DOUBLE: + return (static_cast<double>(*static_cast<sal_Int16 *>(pDest)) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_UNSIGNED_SHORT: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return o3tl::cmp_equal( + *static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int8 *>(pSource)); + case typelib_TypeClass_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int16 *>(pSource)); + case typelib_TypeClass_UNSIGNED_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_uInt16 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); + case typelib_TypeClass_LONG: + return o3tl::cmp_equal( + *static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_UNSIGNED_LONG: + return o3tl::cmp_equal( + *static_cast<sal_uInt16 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); + case typelib_TypeClass_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_uInt16 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_UNSIGNED_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_uInt16 *>(pDest), *static_cast<sal_uInt64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (static_cast<float>(*static_cast<sal_uInt16 *>(pDest)) == *static_cast<float *>(pSource)); + case typelib_TypeClass_DOUBLE: + return (static_cast<double>(*static_cast<sal_uInt16 *>(pDest)) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_LONG: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return o3tl::cmp_equal( + *static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int8 *>(pSource)); + case typelib_TypeClass_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int16 *>(pSource)); + case typelib_TypeClass_UNSIGNED_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int32 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); + case typelib_TypeClass_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_UNSIGNED_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int32 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); + case typelib_TypeClass_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_UNSIGNED_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int32 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (static_cast<float>(*static_cast<sal_Int32 *>(pDest)) == *static_cast<float *>(pSource)); + case typelib_TypeClass_DOUBLE: + return (static_cast<double>(*static_cast<sal_Int32 *>(pDest)) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_UNSIGNED_LONG: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return o3tl::cmp_equal( + *static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int8 *>(pSource)); + case typelib_TypeClass_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int16 *>(pSource)); + case typelib_TypeClass_UNSIGNED_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_uInt32 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); + case typelib_TypeClass_LONG: + return o3tl::cmp_equal( + *static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_UNSIGNED_LONG: + return o3tl::cmp_equal( + *static_cast<sal_uInt32 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); + case typelib_TypeClass_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_uInt32 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_UNSIGNED_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_uInt32 *>(pDest), *static_cast<sal_uInt64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (static_cast<float>(*static_cast<sal_uInt32 *>(pDest)) == *static_cast<float *>(pSource)); + case typelib_TypeClass_DOUBLE: + return (static_cast<double>(*static_cast<sal_uInt32 *>(pDest)) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_HYPER: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return o3tl::cmp_equal( + *static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int8 *>(pSource)); + case typelib_TypeClass_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int16 *>(pSource)); + case typelib_TypeClass_UNSIGNED_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_Int64 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); + case typelib_TypeClass_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_UNSIGNED_LONG: + return o3tl::cmp_equal( + *static_cast<sal_Int64 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); + case typelib_TypeClass_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_UNSIGNED_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_Int64 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (static_cast<float>(*static_cast<sal_Int64 *>(pDest)) == *static_cast<float *>(pSource)); + case typelib_TypeClass_DOUBLE: + return (static_cast<double>(*static_cast<sal_Int64 *>(pDest)) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_UNSIGNED_HYPER: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return o3tl::cmp_equal( + *static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int8 *>(pSource)); + case typelib_TypeClass_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int16 *>(pSource)); + case typelib_TypeClass_UNSIGNED_SHORT: + return o3tl::cmp_equal( + *static_cast<sal_uInt64 *>(pDest), *static_cast<sal_uInt16 *>(pSource)); + case typelib_TypeClass_LONG: + return o3tl::cmp_equal( + *static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_UNSIGNED_LONG: + return o3tl::cmp_equal( + *static_cast<sal_uInt64 *>(pDest), *static_cast<sal_uInt32 *>(pSource)); + case typelib_TypeClass_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_uInt64 *>(pDest), *static_cast<sal_Int64 *>(pSource)); + case typelib_TypeClass_UNSIGNED_HYPER: + return o3tl::cmp_equal( + *static_cast<sal_uInt64 *>(pDest), *static_cast<sal_uInt64 *>(pSource)); + case typelib_TypeClass_FLOAT: + if (::floor( *static_cast<float *>(pSource) ) != *static_cast<float *>(pSource) || *static_cast<float *>(pSource) < 0) + return false; + return (*static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<float *>(pSource))); + case typelib_TypeClass_DOUBLE: + if (::floor( *static_cast<double *>(pSource) ) != *static_cast<double *>(pSource) || *static_cast<double *>(pSource) < 0) + return false; + return (*static_cast<sal_uInt64 *>(pDest) == static_cast<sal_uInt64>(*static_cast<double *>(pSource))); + default: + return false; + } + case typelib_TypeClass_FLOAT: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return (*static_cast<float *>(pDest) == static_cast<float>(*static_cast<sal_Int8 *>(pSource))); + case typelib_TypeClass_SHORT: + return (*static_cast<float *>(pDest) == static_cast<float>(*static_cast<sal_Int16 *>(pSource))); + case typelib_TypeClass_UNSIGNED_SHORT: + return (*static_cast<float *>(pDest) == static_cast<float>(*static_cast<sal_uInt16 *>(pSource))); + case typelib_TypeClass_LONG: + return (*static_cast<float *>(pDest) == static_cast<float>(*static_cast<sal_Int32 *>(pSource))); + case typelib_TypeClass_UNSIGNED_LONG: + return (*static_cast<float *>(pDest) == static_cast<float>(*static_cast<sal_uInt32 *>(pSource))); + case typelib_TypeClass_HYPER: + return (*static_cast<float *>(pDest) == static_cast<float>(*static_cast<sal_Int64 *>(pSource))); + case typelib_TypeClass_UNSIGNED_HYPER: + if (::floor( *static_cast<float *>(pDest) ) != *static_cast<float *>(pDest) || *static_cast<float *>(pDest) < 0) + return false; + return (static_cast<sal_uInt64>(*static_cast<float *>(pDest)) == *static_cast<sal_uInt64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (*static_cast<float *>(pDest) == *static_cast<float *>(pSource)); + case typelib_TypeClass_DOUBLE: + return (static_cast<double>(*static_cast<float *>(pDest)) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_DOUBLE: + switch (eSourceTypeClass) + { + case typelib_TypeClass_BYTE: + return (*static_cast<double *>(pDest) == static_cast<double>(*static_cast<sal_Int8 *>(pSource))); + case typelib_TypeClass_SHORT: + return (*static_cast<double *>(pDest) == static_cast<double>(*static_cast<sal_Int16 *>(pSource))); + case typelib_TypeClass_UNSIGNED_SHORT: + return (*static_cast<double *>(pDest) == static_cast<double>(*static_cast<sal_uInt16 *>(pSource))); + case typelib_TypeClass_LONG: + return (*static_cast<double *>(pDest) == static_cast<double>(*static_cast<sal_Int32 *>(pSource))); + case typelib_TypeClass_UNSIGNED_LONG: + return (*static_cast<double *>(pDest) == static_cast<double>(*static_cast<sal_uInt32 *>(pSource))); + case typelib_TypeClass_HYPER: + return (*static_cast<double *>(pDest) == static_cast<double>(*static_cast<sal_Int64 *>(pSource))); + case typelib_TypeClass_UNSIGNED_HYPER: + if (::floor( *static_cast<double *>(pDest) ) != *static_cast<double *>(pDest) || *static_cast<double *>(pDest) < 0) + return false; + return (static_cast<sal_uInt64>(*static_cast<double *>(pDest)) == *static_cast<sal_uInt64 *>(pSource)); + case typelib_TypeClass_FLOAT: + return (*static_cast<double *>(pDest) == static_cast<double>(*static_cast<float *>(pSource))); + case typelib_TypeClass_DOUBLE: + return (*static_cast<double *>(pDest) == *static_cast<double *>(pSource)); + default: + return false; + } + case typelib_TypeClass_STRING: + return eSourceTypeClass == typelib_TypeClass_STRING + && *static_cast<OUString *>(pDest) == + *static_cast<OUString const *>(pSource); + case typelib_TypeClass_TYPE: + return eSourceTypeClass == typelib_TypeClass_TYPE + && _type_equals( + *static_cast<typelib_TypeDescriptionReference **>(pDest), + *static_cast<typelib_TypeDescriptionReference **>(pSource) ); + case typelib_TypeClass_ENUM: + return (_type_equals( pDestType, pSourceType ) && + *static_cast<sal_Int32 *>(pDest) == *static_cast<sal_Int32 *>(pSource)); + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + if (! _type_equals( pDestType, pSourceType )) + return false; + if (pDestTypeDescr) + { + return _equalStruct( + pDest, pSource, + reinterpret_cast<typelib_CompoundTypeDescription *>(pDestTypeDescr), + queryInterface, release ); + } + else + { + TYPELIB_DANGER_GET( &pDestTypeDescr, pDestType ); + bool bRet = _equalStruct( + pDest, pSource, + reinterpret_cast<typelib_CompoundTypeDescription *>(pDestTypeDescr), + queryInterface, release ); + TYPELIB_DANGER_RELEASE( pDestTypeDescr ); + return bRet; + } + case typelib_TypeClass_SEQUENCE: + if (_type_equals( pDestType, pSourceType )) + { + if (pDestTypeDescr) + { + return _equalSequence( + *static_cast<uno_Sequence **>(pDest), *static_cast<uno_Sequence **>(pSource), + reinterpret_cast<typelib_IndirectTypeDescription *>(pDestTypeDescr)->pType, + queryInterface, release ); + } + else + { + TYPELIB_DANGER_GET( &pDestTypeDescr, pDestType ); + bool bRet = _equalSequence( + *static_cast<uno_Sequence **>(pDest), *static_cast<uno_Sequence **>(pSource), + reinterpret_cast<typelib_IndirectTypeDescription *>(pDestTypeDescr)->pType, + queryInterface, release ); + TYPELIB_DANGER_RELEASE( pDestTypeDescr ); + return bRet; + } + } + return false; + case typelib_TypeClass_INTERFACE: + if (typelib_TypeClass_INTERFACE == eSourceTypeClass) + return _equalObject( *static_cast<void **>(pDest), *static_cast<void **>(pSource), queryInterface, release ); + break; + default: + OSL_ASSERT(false); + break; + } + return false; +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/lbenv.cxx b/cppu/source/uno/lbenv.cxx new file mode 100644 index 0000000000..472feae34b --- /dev/null +++ b/cppu/source/uno/lbenv.cxx @@ -0,0 +1,1147 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifdef DISABLE_DYNLOADING +#include <config_java.h> +#endif + +#include <cppu/EnvDcp.hxx> + +#include <sal/log.hxx> +#include <osl/diagnose.h> +#include <osl/interlck.h> +#include <osl/mutex.hxx> +#include <osl/module.hxx> +#include <osl/process.h> +#include <rtl/process.h> +#include <rtl/string.hxx> +#include <rtl/ustring.hxx> +#include <rtl/ustrbuf.hxx> +#include <typelib/typedescription.h> +#include <uno/dispatcher.h> +#include <uno/environment.h> +#include <uno/lbnames.h> +#include "prim.hxx" +#include "loadmodule.hxx" + +#include <string_view> +#include <unordered_map> +#include <utility> +#include <vector> +#include <stdio.h> + + +namespace +{ + + +bool td_equals( typelib_InterfaceTypeDescription const * pTD1, + typelib_InterfaceTypeDescription const * pTD2 ) +{ + return (pTD1 == pTD2 || + (pTD1->aBase.pTypeName->length == pTD2->aBase.pTypeName->length && + ::rtl_ustr_compare( + pTD1->aBase.pTypeName->buffer, + pTD2->aBase.pTypeName->buffer ) == 0)); +} + +struct uno_DefaultEnvironment; + + +struct InterfaceEntry +{ + sal_Int32 refCount; + void * pInterface; + uno_freeProxyFunc fpFreeProxy; + typelib_InterfaceTypeDescription * pTypeDescr; +}; + +struct ObjectEntry +{ + OUString oid; + std::vector< InterfaceEntry > aInterfaces; + sal_Int32 nRef; + bool mixedObject; + + explicit ObjectEntry( OUString aOId_ ); + + void append( + uno_DefaultEnvironment * pEnv, + void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr, + uno_freeProxyFunc fpFreeProxy ); + InterfaceEntry * find( + typelib_InterfaceTypeDescription * pTypeDescr ); + sal_Int32 find( void const * iface_ptr, std::size_t pos ) const; +}; + + +struct FctPtrHash +{ + std::size_t operator () ( const void * pKey ) const + { return reinterpret_cast< std::size_t>( pKey ); } +}; + + +// mapping from environment name to environment +typedef std::unordered_map< + OUString, uno_Environment * > OUString2EnvironmentMap; + +// mapping from ptr to object entry +typedef std::unordered_map< + void *, ObjectEntry *, FctPtrHash > Ptr2ObjectMap; +// mapping from oid to object entry +typedef std::unordered_map< + OUString, ObjectEntry * > OId2ObjectMap; + +struct EnvironmentsData +{ + ::osl::Mutex mutex; + OUString2EnvironmentMap aName2EnvMap; + + EnvironmentsData() : isDisposing(false) {} + ~EnvironmentsData(); + + void getEnvironment( + uno_Environment ** ppEnv, std::u16string_view rEnvDcp, void * pContext ); + void registerEnvironment( uno_Environment ** ppEnv ); + void getRegisteredEnvironments( + uno_Environment *** pppEnvs, sal_Int32 * pnLen, + uno_memAlloc memAlloc, std::u16string_view rEnvDcp ); + + bool isDisposing; +}; + +EnvironmentsData& theEnvironmentsData() +{ + static EnvironmentsData SINGLETON; + return SINGLETON; +} + +struct uno_DefaultEnvironment : public uno_ExtEnvironment +{ + sal_Int32 nRef; + sal_Int32 nWeakRef; + + ::osl::Mutex mutex; + Ptr2ObjectMap aPtr2ObjectMap; + OId2ObjectMap aOId2ObjectMap; + + uno_DefaultEnvironment( + const OUString & rEnvDcp_, void * pContext_ ); + ~uno_DefaultEnvironment(); +}; + + +ObjectEntry::ObjectEntry( OUString aOId_ ) + : oid(std::move( aOId_ )), + nRef( 0 ), + mixedObject( false ) +{ + aInterfaces.reserve( 2 ); +} + + +void ObjectEntry::append( + uno_DefaultEnvironment * pEnv, + void * pInterface, typelib_InterfaceTypeDescription * pTypeDescr, + uno_freeProxyFunc fpFreeProxy ) +{ + InterfaceEntry aNewEntry; + if (! fpFreeProxy) + (*pEnv->acquireInterface)( pEnv, pInterface ); + aNewEntry.refCount = 1; + aNewEntry.pInterface = pInterface; + aNewEntry.fpFreeProxy = fpFreeProxy; + typelib_typedescription_acquire( &pTypeDescr->aBase ); + aNewEntry.pTypeDescr = pTypeDescr; + + std::pair< Ptr2ObjectMap::iterator, bool > i( + pEnv->aPtr2ObjectMap.emplace( pInterface, this ) ); + SAL_WARN_IF( + !i.second && (find(pInterface, 0) == -1 || i.first->second != this), + "cppu", + "map already contains " << i.first->second << " != " << this << " for " + << pInterface); + aInterfaces.push_back( aNewEntry ); +} + + +InterfaceEntry * ObjectEntry::find( + typelib_InterfaceTypeDescription * pTypeDescr_ ) +{ + OSL_ASSERT( ! aInterfaces.empty() ); + if (aInterfaces.empty()) + return nullptr; + + // shortcut common case: + OUString const & type_name = + OUString::unacquired( &pTypeDescr_->aBase.pTypeName ); + if ( type_name == "com.sun.star.uno.XInterface" ) + { + return aInterfaces.data(); + } + + std::size_t nSize = aInterfaces.size(); + for ( std::size_t nPos = 0; nPos < nSize; ++nPos ) + { + typelib_InterfaceTypeDescription * pITD = + aInterfaces[ nPos ].pTypeDescr; + while (pITD) + { + if (td_equals( pITD, pTypeDescr_ )) + return &aInterfaces[ nPos ]; + pITD = pITD->pBaseTypeDescription; + } + } + return nullptr; +} + + +sal_Int32 ObjectEntry::find( + void const * iface_ptr, std::size_t pos ) const +{ + std::size_t size = aInterfaces.size(); + for ( ; pos < size; ++pos ) + { + if (aInterfaces[ pos ].pInterface == iface_ptr) + return pos; + } + return -1; +} + +extern "C" +{ + + +static void defenv_registerInterface( + uno_ExtEnvironment * pEnv, void ** ppInterface, + rtl_uString * pOId, typelib_InterfaceTypeDescription * pTypeDescr ) +{ + OSL_ENSURE( pEnv && ppInterface && pOId && pTypeDescr, "### null ptr!" ); + OUString const & rOId = OUString::unacquired( &pOId ); + + uno_DefaultEnvironment * that = + static_cast< uno_DefaultEnvironment * >( pEnv ); + ::osl::ClearableMutexGuard guard( that->mutex ); + + // try to insert dummy 0: + std::pair<OId2ObjectMap::iterator, bool> const insertion( + that->aOId2ObjectMap.emplace( rOId, nullptr ) ); + if (insertion.second) + { + ObjectEntry * pOEntry = new ObjectEntry( rOId ); + insertion.first->second = pOEntry; + ++pOEntry->nRef; // another register call on object + pOEntry->append( that, *ppInterface, pTypeDescr, nullptr ); + } + else // object entry exists + { + ObjectEntry * pOEntry = insertion.first->second; + ++pOEntry->nRef; // another register call on object + InterfaceEntry * pIEntry = pOEntry->find( pTypeDescr ); + + if (pIEntry) // type entry exists + { + ++pIEntry->refCount; + if (pIEntry->pInterface != *ppInterface) + { + void * pInterface = pIEntry->pInterface; + (*pEnv->acquireInterface)( pEnv, pInterface ); + guard.clear(); + (*pEnv->releaseInterface)( pEnv, *ppInterface ); + *ppInterface = pInterface; + } + } + else + { + pOEntry->append( that, *ppInterface, pTypeDescr, nullptr ); + } + } +} + + +static void defenv_registerProxyInterface( + uno_ExtEnvironment * pEnv, void ** ppInterface, uno_freeProxyFunc freeProxy, + rtl_uString * pOId, typelib_InterfaceTypeDescription * pTypeDescr ) +{ + OSL_ENSURE( pEnv && ppInterface && pOId && pTypeDescr && freeProxy, + "### null ptr!" ); + OUString const & rOId = OUString::unacquired( &pOId ); + + uno_DefaultEnvironment * that = + static_cast< uno_DefaultEnvironment * >( pEnv ); + ::osl::ClearableMutexGuard guard( that->mutex ); + + // try to insert dummy 0: + std::pair<OId2ObjectMap::iterator, bool> const insertion( + that->aOId2ObjectMap.emplace( rOId, nullptr ) ); + if (insertion.second) + { + ObjectEntry * pOEntry = new ObjectEntry( rOId ); + insertion.first->second = pOEntry; + ++pOEntry->nRef; // another register call on object + pOEntry->append( that, *ppInterface, pTypeDescr, freeProxy ); + } + else // object entry exists + { + ObjectEntry * pOEntry = insertion.first->second; + + // first registration was an original, then registerProxyInterface(): + pOEntry->mixedObject |= + (!pOEntry->aInterfaces.empty() && + pOEntry->aInterfaces[ 0 ].fpFreeProxy == nullptr); + + ++pOEntry->nRef; // another register call on object + InterfaceEntry * pIEntry = pOEntry->find( pTypeDescr ); + + if (pIEntry) // type entry exists + { + if (pIEntry->pInterface == *ppInterface) + { + ++pIEntry->refCount; + } + else + { + void * pInterface = pIEntry->pInterface; + (*pEnv->acquireInterface)( pEnv, pInterface ); + --pOEntry->nRef; // manual revoke of proxy to be freed + guard.clear(); + (*freeProxy)( pEnv, *ppInterface ); + *ppInterface = pInterface; + } + } + else + { + pOEntry->append( that, *ppInterface, pTypeDescr, freeProxy ); + } + } +} + + +static void s_stub_defenv_revokeInterface(va_list * pParam) +{ + uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *); + void * pInterface = va_arg(*pParam, void *); + + OSL_ENSURE( pEnv && pInterface, "### null ptr!" ); + uno_DefaultEnvironment * that = + static_cast< uno_DefaultEnvironment * >( pEnv ); + ::osl::ClearableMutexGuard guard( that->mutex ); + + Ptr2ObjectMap::const_iterator const iFind( + that->aPtr2ObjectMap.find( pInterface ) ); + assert(iFind != that->aPtr2ObjectMap.end()); + ObjectEntry * pOEntry = iFind->second; + if (! --pOEntry->nRef) + { + // cleanup maps + that->aOId2ObjectMap.erase( pOEntry->oid ); + sal_Int32 nPos; + for ( nPos = pOEntry->aInterfaces.size(); nPos--; ) + { + that->aPtr2ObjectMap.erase( pOEntry->aInterfaces[nPos].pInterface ); + } + + // the last proxy interface of the environment might kill this + // environment, because of releasing its language binding!!! + guard.clear(); + + // release interfaces + for ( nPos = pOEntry->aInterfaces.size(); nPos--; ) + { + InterfaceEntry const & rEntry = pOEntry->aInterfaces[nPos]; + typelib_typedescription_release( &rEntry.pTypeDescr->aBase ); + if (rEntry.fpFreeProxy) // is proxy or used interface? + { + (*rEntry.fpFreeProxy)( pEnv, rEntry.pInterface ); + } + else + { + (*pEnv->releaseInterface)( pEnv, rEntry.pInterface ); + } + } + + delete pOEntry; + } + else if (pOEntry->mixedObject) + { + OSL_ASSERT( !pOEntry->aInterfaces.empty() && + pOEntry->aInterfaces[ 0 ].fpFreeProxy == nullptr ); + + sal_Int32 index = pOEntry->find( pInterface, 1 ); + OSL_ASSERT( index > 0 ); + if (index > 0) + { + InterfaceEntry & entry = pOEntry->aInterfaces[ index ]; + OSL_ASSERT( entry.pInterface == pInterface ); + if (entry.fpFreeProxy != nullptr) + { + --entry.refCount; + if (entry.refCount == 0) + { + uno_freeProxyFunc fpFreeProxy = entry.fpFreeProxy; + typelib_TypeDescription * pTypeDescr = + reinterpret_cast< typelib_TypeDescription * >( + entry.pTypeDescr ); + + pOEntry->aInterfaces.erase( + pOEntry->aInterfaces.begin() + index ); + if (pOEntry->find( pInterface, index ) < 0) + { + // proxy ptr not registered for another interface: + // remove from ptr map + std::size_t erased = + that->aPtr2ObjectMap.erase( pInterface ); + OSL_ASSERT( erased == 1 ); + } + + guard.clear(); + + typelib_typedescription_release( pTypeDescr ); + (*fpFreeProxy)( pEnv, pInterface ); + } + } + } + } +} + +static void defenv_revokeInterface(uno_ExtEnvironment * pEnv, void * pInterface) +{ + uno_Environment_invoke(&pEnv->aBase, s_stub_defenv_revokeInterface, pEnv, pInterface); +} + + +static void defenv_getObjectIdentifier( + uno_ExtEnvironment * pEnv, rtl_uString ** ppOId, void * pInterface ) +{ + OSL_ENSURE( pEnv && ppOId && pInterface, "### null ptr!" ); + if (*ppOId) + { + ::rtl_uString_release( *ppOId ); + *ppOId = nullptr; + } + + uno_DefaultEnvironment * that = + static_cast< uno_DefaultEnvironment * >( pEnv ); + ::osl::ClearableMutexGuard guard( that->mutex ); + + Ptr2ObjectMap::const_iterator const iFind( + that->aPtr2ObjectMap.find( pInterface ) ); + if (iFind == that->aPtr2ObjectMap.end()) + { + guard.clear(); + (*pEnv->computeObjectIdentifier)( pEnv, ppOId, pInterface ); + } + else + { + rtl_uString * hstr = iFind->second->oid.pData; + rtl_uString_acquire( hstr ); + *ppOId = hstr; + } +} + + +static void defenv_getRegisteredInterface( + uno_ExtEnvironment * pEnv, void ** ppInterface, + rtl_uString * pOId, typelib_InterfaceTypeDescription * pTypeDescr ) +{ + OSL_ENSURE( pEnv && ppInterface && pOId && pTypeDescr, "### null ptr!" ); + if (*ppInterface) + { + (*pEnv->releaseInterface)( pEnv, *ppInterface ); + *ppInterface = nullptr; + } + + OUString const & rOId = OUString::unacquired( &pOId ); + uno_DefaultEnvironment * that = + static_cast< uno_DefaultEnvironment * >( pEnv ); + ::osl::MutexGuard guard( that->mutex ); + + OId2ObjectMap::const_iterator const iFind + ( that->aOId2ObjectMap.find( rOId ) ); + if (iFind != that->aOId2ObjectMap.end()) + { + InterfaceEntry const * pIEntry = iFind->second->find( pTypeDescr ); + if (pIEntry) + { + (*pEnv->acquireInterface)( pEnv, pIEntry->pInterface ); + *ppInterface = pIEntry->pInterface; + } + } +} + + +static void defenv_getRegisteredInterfaces( + uno_ExtEnvironment * pEnv, void *** pppInterfaces, sal_Int32 * pnLen, + uno_memAlloc memAlloc ) +{ + assert(pEnv && pppInterfaces && pnLen && memAlloc && "### null ptr!"); + uno_DefaultEnvironment * that = + static_cast< uno_DefaultEnvironment * >( pEnv ); + ::osl::MutexGuard guard( that->mutex ); + + sal_Int32 nLen = that->aPtr2ObjectMap.size(); + sal_Int32 nPos = 0; + void ** ppInterfaces = static_cast<void **>((*memAlloc)( nLen * sizeof (void *) )); + + for (const auto& rEntry : that->aPtr2ObjectMap) + { + ppInterfaces[nPos] = rEntry.first; + (*pEnv->acquireInterface)( pEnv, ppInterfaces[nPos] ); + nPos++; + } + + *pppInterfaces = ppInterfaces; + *pnLen = nLen; +} + + +static void defenv_acquire( uno_Environment * pEnv ) +{ + uno_DefaultEnvironment * that = reinterpret_cast<uno_DefaultEnvironment *>(pEnv); + osl_atomic_increment( &that->nWeakRef ); + osl_atomic_increment( &that->nRef ); +} + + +static void defenv_release( uno_Environment * pEnv ) +{ + uno_DefaultEnvironment * that = reinterpret_cast<uno_DefaultEnvironment *>(pEnv); + if (! osl_atomic_decrement( &that->nRef )) + { + // invoke dispose callback + if (pEnv->environmentDisposing) + { + (*pEnv->environmentDisposing)( pEnv ); + } + + OSL_ENSURE( that->aOId2ObjectMap.empty(), "### object entries left!" ); + } + // free memory if no weak refs left + if (! osl_atomic_decrement( &that->nWeakRef )) + { + delete that; + } +} + + +static void defenv_acquireWeak( uno_Environment * pEnv ) +{ + uno_DefaultEnvironment * that = reinterpret_cast<uno_DefaultEnvironment *>(pEnv); + osl_atomic_increment( &that->nWeakRef ); +} + + +static void defenv_releaseWeak( uno_Environment * pEnv ) +{ + uno_DefaultEnvironment * that = reinterpret_cast<uno_DefaultEnvironment *>(pEnv); + if (! osl_atomic_decrement( &that->nWeakRef )) + { + delete that; + } +} + + +static void defenv_harden( + uno_Environment ** ppHardEnv, uno_Environment * pEnv ) +{ + if (*ppHardEnv) + { + (*(*ppHardEnv)->release)( *ppHardEnv ); + *ppHardEnv = nullptr; + } + + EnvironmentsData & rData = theEnvironmentsData(); + + if (rData.isDisposing) + return; + + uno_DefaultEnvironment * that = reinterpret_cast<uno_DefaultEnvironment *>(pEnv); + { + ::osl::MutexGuard guard( rData.mutex ); + if (1 == osl_atomic_increment( &that->nRef )) // is dead + { + that->nRef = 0; + return; + } + } + osl_atomic_increment( &that->nWeakRef ); + *ppHardEnv = pEnv; +} + + +static void defenv_dispose( SAL_UNUSED_PARAMETER uno_Environment * ) +{ +} +} + + +uno_DefaultEnvironment::uno_DefaultEnvironment( + const OUString & rEnvDcp_, void * pContext_ ) + : nRef( 0 ), + nWeakRef( 0 ) +{ + uno_Environment * that = reinterpret_cast< uno_Environment * >(this); + that->pReserved = nullptr; + // functions + that->acquire = defenv_acquire; + that->release = defenv_release; + that->acquireWeak = defenv_acquireWeak; + that->releaseWeak = defenv_releaseWeak; + that->harden = defenv_harden; + that->dispose = defenv_dispose; + that->pExtEnv = this; + // identifier + ::rtl_uString_acquire( rEnvDcp_.pData ); + that->pTypeName = rEnvDcp_.pData; + that->pContext = pContext_; + + // will be late initialized + that->environmentDisposing = nullptr; + + uno_ExtEnvironment::registerInterface = defenv_registerInterface; + uno_ExtEnvironment::registerProxyInterface = defenv_registerProxyInterface; + uno_ExtEnvironment::revokeInterface = defenv_revokeInterface; + uno_ExtEnvironment::getObjectIdentifier = defenv_getObjectIdentifier; + uno_ExtEnvironment::getRegisteredInterface = defenv_getRegisteredInterface; + uno_ExtEnvironment::getRegisteredInterfaces = + defenv_getRegisteredInterfaces; + +} + + +uno_DefaultEnvironment::~uno_DefaultEnvironment() +{ + ::rtl_uString_release( aBase.pTypeName ); +} + + +void writeLine( + void * stream, const char * pLine, const char * pFilter ) +{ + if (pFilter && *pFilter) + { + // lookup pFilter in pLine + while (*pLine) + { + if (*pLine == *pFilter) + { + sal_Int32 nPos = 1; + while (pLine[nPos] && pFilter[nPos] == pLine[nPos]) + { + ++nPos; + } + if (! pFilter[nPos]) + { + if (stream) + { + fprintf( static_cast<FILE *>(stream), "%s\n", pLine ); + } + else + { + SAL_WARN("cppu", pLine ); + } + } + } + ++pLine; + } + } + else + { + if (stream) + { + fprintf( static_cast<FILE *>(stream), "%s\n", pLine ); + } + else + { + fprintf( stderr, "%s\n", pLine ); + } + } +} + + +void writeLine( + void * stream, std::u16string_view rLine, const char * pFilter ) +{ + OString aLine( OUStringToOString( + rLine, RTL_TEXTENCODING_ASCII_US ) ); + writeLine( stream, aLine.getStr(), pFilter ); +} + +} + +extern "C" void SAL_CALL uno_dumpEnvironment( + void * stream, uno_Environment * pEnv, const char * pFilter ) + SAL_THROW_EXTERN_C() +{ + OSL_ENSURE( pEnv, "### null ptr!" ); + OUStringBuffer buf; + + if (! pEnv->pExtEnv) + { + writeLine( stream, "###################################" + "###########################################", pFilter ); + buf.append( OUString::Concat("environment: ") + OUString::unacquired(&pEnv->pTypeName) ); + writeLine( stream, buf, pFilter ); + buf.setLength(0); + writeLine( stream, "NO INTERFACE INFORMATION AVAILABLE!", pFilter ); + return; + } + + writeLine( stream, "########################################" + "######################################", pFilter ); + buf.append( OUString::Concat("environment dump: ") + OUString::unacquired(&pEnv->pTypeName) ); + writeLine( stream, buf, pFilter ); + buf.setLength(0); + + uno_DefaultEnvironment * that = + reinterpret_cast< uno_DefaultEnvironment * >(pEnv); + ::osl::MutexGuard guard( that->mutex ); + + Ptr2ObjectMap ptr2obj( that->aPtr2ObjectMap ); + for (const auto& rEntry : that->aOId2ObjectMap) + { + ObjectEntry * pOEntry = rEntry.second; + + buf.append( "+ " ); + if (pOEntry->mixedObject) + buf.append( "mixed " ); + buf.append( "object entry: nRef=" + + OUString::number(pOEntry->nRef) + + "; oid=\"" + + pOEntry->oid + + "\"" ); + writeLine( stream, buf, pFilter ); + buf.setLength(0); + + for ( std::size_t nPos = 0; + nPos < pOEntry->aInterfaces.size(); ++nPos ) + { + const InterfaceEntry & rIEntry = pOEntry->aInterfaces[nPos]; + + buf.append( OUString::Concat(" - ") + + OUString::unacquired(&rIEntry.pTypeDescr->aBase.pTypeName) ); + if (rIEntry.fpFreeProxy) + { + buf.append( "; proxy free=0x" + + OUString::number( reinterpret_cast< sal_IntPtr >(rIEntry.fpFreeProxy), 16 ) ); + } + else + { + buf.append( "; original" ); + } + buf.append( "; ptr=0x" + + OUString::number(reinterpret_cast< sal_IntPtr >(rIEntry.pInterface), 16 ) ); + + if (pOEntry->find( rIEntry.pInterface, nPos + 1 ) < 0) + { + std::size_t erased = ptr2obj.erase( rIEntry.pInterface ); + if (erased != 1) + { + buf.append( " (ptr not found in map!)" ); + } + } + writeLine( stream, buf, pFilter ); + buf.setLength(0); + } + } + if (! ptr2obj.empty()) + writeLine( stream, "ptr map inconsistency!!!", pFilter ); + writeLine( stream, "#####################################" + "#########################################", pFilter ); +} + + +extern "C" void SAL_CALL uno_dumpEnvironmentByName( + void * stream, rtl_uString * pEnvDcp, const char * pFilter ) + SAL_THROW_EXTERN_C() +{ + uno_Environment * pEnv = nullptr; + uno_getEnvironment( &pEnv, pEnvDcp, nullptr ); + if (pEnv) + { + ::uno_dumpEnvironment( stream, pEnv, pFilter ); + (*pEnv->release)( pEnv ); + } + else + { + writeLine( + stream, + Concat2View("environment \"" + OUString::unacquired(&pEnvDcp) + "\" does not exist!"), + pFilter ); + } +} + +namespace +{ + +const OUString & unoenv_getStaticOIdPart() +{ + static auto const theStaticOIdPart = [] { + OUStringBuffer aRet( 64 ); + aRet.append( "];" ); + // pid + oslProcessInfo info; + info.Size = sizeof(oslProcessInfo); + if (::osl_getProcessInfo( nullptr, osl_Process_IDENTIFIER, &info ) == + osl_Process_E_None) + { + aRet.append( static_cast<sal_Int64>(info.Ident), 16 ); + } + else + { + aRet.append( "unknown process id" ); + } + // good guid + sal_uInt8 ar[16]; + ::rtl_getGlobalProcessId( ar ); + aRet.append( ';' ); + for (unsigned char i : ar) + aRet.append( static_cast<sal_Int32>(i), 16 ); + + return aRet.makeStringAndClear(); + }(); + return theStaticOIdPart; +} + +} + +extern "C" +{ + + +static void unoenv_computeObjectIdentifier( + uno_ExtEnvironment * pEnv, rtl_uString ** ppOId, void * pInterface ) +{ + assert(pEnv && ppOId && pInterface && "### null ptr!"); + if (*ppOId) + { + ::rtl_uString_release( *ppOId ); + *ppOId = nullptr; + } + + uno_Interface * pUnoI = static_cast<uno_Interface *>( + ::cppu::binuno_queryInterface( + pInterface, *typelib_static_type_getByTypeClass( + typelib_TypeClass_INTERFACE ) )); + if (nullptr == pUnoI) + return; + + (*pUnoI->release)( pUnoI ); + OUString aStr( + // interface + OUString::number( reinterpret_cast< sal_IntPtr >(pUnoI), 16 ) + ";" + // environment[context] + + OUString::unacquired(&pEnv->aBase.pTypeName) + "[" + + OUString::number( reinterpret_cast< sal_IntPtr >( + reinterpret_cast< + uno_Environment * >(pEnv)->pContext ), 16 ) + // process;good guid + + unoenv_getStaticOIdPart() ); + *ppOId = aStr.pData; + ::rtl_uString_acquire( *ppOId ); +} + + +static void unoenv_acquireInterface( + SAL_UNUSED_PARAMETER uno_ExtEnvironment *, void * pUnoI_ ) +{ + uno_Interface * pUnoI = static_cast< uno_Interface * >(pUnoI_); + (*pUnoI->acquire)( pUnoI ); +} + + +static void unoenv_releaseInterface( + SAL_UNUSED_PARAMETER uno_ExtEnvironment *, void * pUnoI_ ) +{ + uno_Interface * pUnoI = static_cast< uno_Interface * >(pUnoI_); + (*pUnoI->release)( pUnoI ); +} +} + +namespace { + +EnvironmentsData::~EnvironmentsData() +{ + ::osl::MutexGuard guard( mutex ); + isDisposing = true; + + for ( const auto& rEntry : aName2EnvMap ) + { + uno_Environment * pWeak = rEntry.second; + uno_Environment * pHard = nullptr; + (*pWeak->harden)( &pHard, pWeak ); + (*pWeak->releaseWeak)( pWeak ); + + if (pHard) + { + (*pHard->dispose)( pHard ); // send explicit dispose + (*pHard->release)( pHard ); + } + } +} + + +void EnvironmentsData::getEnvironment( + uno_Environment ** ppEnv, std::u16string_view rEnvDcp, void * pContext ) +{ + if (*ppEnv) + { + (*(*ppEnv)->release)( *ppEnv ); + *ppEnv = nullptr; + } + + OUString aKey = OUString::number( reinterpret_cast< sal_IntPtr >(pContext) ) + rEnvDcp; + + // try to find registered mapping + OUString2EnvironmentMap::const_iterator const iFind( + aName2EnvMap.find( aKey ) ); + if (iFind != aName2EnvMap.end()) + { + uno_Environment * pWeak = iFind->second; + (*pWeak->harden)( ppEnv, pWeak ); + } +} + + +void EnvironmentsData::registerEnvironment( uno_Environment ** ppEnv ) +{ + OSL_ENSURE( ppEnv, "### null ptr!" ); + uno_Environment * pEnv = *ppEnv; + + OUString aKey = + OUString::number( reinterpret_cast< sal_IntPtr >(pEnv->pContext) ) + + OUString::unacquired(&pEnv->pTypeName); + + // try to find registered environment + OUString2EnvironmentMap::const_iterator const iFind( + aName2EnvMap.find( aKey ) ); + if (iFind == aName2EnvMap.end()) + { + (*pEnv->acquireWeak)( pEnv ); + std::pair< OUString2EnvironmentMap::iterator, bool > insertion ( + aName2EnvMap.emplace( aKey, pEnv ) ); + SAL_WARN_IF( !insertion.second, "cppu", "key " << aKey << " already in env map" ); + } + else + { + uno_Environment * pHard = nullptr; + uno_Environment * pWeak = iFind->second; + (*pWeak->harden)( &pHard, pWeak ); + if (pHard) + { + (*pEnv->release)( pEnv ); + *ppEnv = pHard; + } + else // registered one is dead + { + (*pWeak->releaseWeak)( pWeak ); + (*pEnv->acquireWeak)( pEnv ); + aName2EnvMap[ aKey ] = pEnv; + } + } +} + + +void EnvironmentsData::getRegisteredEnvironments( + uno_Environment *** pppEnvs, sal_Int32 * pnLen, uno_memAlloc memAlloc, + std::u16string_view rEnvDcp ) +{ + assert(pppEnvs && pnLen && memAlloc && "### null ptr!"); + + // max size + std::vector<uno_Environment*> aFounds(aName2EnvMap.size()); + sal_Int32 nSize = 0; + + // find matching environment + for ( const auto& rEntry : aName2EnvMap ) + { + uno_Environment * pWeak = rEntry.second; + if (rEnvDcp.empty() || + rEnvDcp == OUString::unacquired(&pWeak->pTypeName) ) + { + aFounds[nSize] = nullptr; + (*pWeak->harden)( &aFounds[nSize], pWeak ); + if (aFounds[nSize]) + ++nSize; + } + } + + *pnLen = nSize; + if (nSize) + { + *pppEnvs = static_cast<uno_Environment **>((*memAlloc)( + sizeof (uno_Environment *) * nSize )); + OSL_ASSERT( *pppEnvs ); + while (nSize--) + { + (*pppEnvs)[nSize] = aFounds[nSize]; + } + } + else + { + *pppEnvs = nullptr; + } +} + +bool loadEnv(OUString const & cLibStem, + uno_Environment * pEnv) +{ +#ifdef DISABLE_DYNLOADING + uno_initEnvironmentFunc fpInit; + + if ( cLibStem == CPPU_CURRENT_LANGUAGE_BINDING_NAME "_uno" ) + fpInit = CPPU_ENV_uno_initEnvironment; +#if HAVE_FEATURE_JAVA + else if ( cLibStem == "java_uno" ) + fpInit = java_uno_initEnvironment; +#endif + else + { + SAL_INFO("cppu", ": Unhandled env: " << cLibStem); + return false; + } +#else + // late init with some code from matching uno language binding + // will be unloaded by environment + osl::Module aMod; + try { + bool bMod = cppu::detail::loadModule(aMod, cLibStem); + if (!bMod) + return false; + } + catch(...) { + // Catch everything and convert to return false + return false; + } + + + uno_initEnvironmentFunc fpInit = reinterpret_cast<uno_initEnvironmentFunc>(aMod.getSymbol(UNO_INIT_ENVIRONMENT)); + + if (!fpInit) + return false; + + aMod.release(); +#endif + + (*fpInit)( pEnv ); // init of environment + return true; +} + +} + +extern "C" +{ + + +static uno_Environment * initDefaultEnvironment( + const OUString & rEnvDcp, void * pContext ) +{ + // coverity[leaked_storage : FALSE] - lifetime is controlled by acquire()/release() calls + uno_Environment * pEnv = &(new uno_DefaultEnvironment( rEnvDcp, pContext ))->aBase; + (*pEnv->acquire)( pEnv ); + + OUString envTypeName = cppu::EnvDcp::getTypeName(rEnvDcp); + + // create default environment + if ( envTypeName == UNO_LB_UNO ) + { + uno_DefaultEnvironment * that = reinterpret_cast<uno_DefaultEnvironment *>(pEnv); + that->computeObjectIdentifier = unoenv_computeObjectIdentifier; + that->acquireInterface = unoenv_acquireInterface; + that->releaseInterface = unoenv_releaseInterface; + + OUString envPurpose = cppu::EnvDcp::getPurpose(rEnvDcp); + if (!envPurpose.isEmpty()) + { + OUString libStem( + OUString::Concat(envPurpose.subView(envPurpose.lastIndexOf(':') + 1)) + "_uno_uno"); + if(!loadEnv(libStem, pEnv)) + { + pEnv->release(pEnv); + return nullptr; + } + } + } + else + { + // late init with some code from matching uno language binding + OUString aStr( envTypeName + "_uno" ); + + if (!loadEnv(aStr, pEnv)) + { + pEnv->release(pEnv); + return nullptr; + } + } + + return pEnv; +} + + +void SAL_CALL uno_createEnvironment( + uno_Environment ** ppEnv, rtl_uString * pEnvDcp, void * pContext ) + SAL_THROW_EXTERN_C() +{ + assert(ppEnv && "### null ptr!"); + if (*ppEnv) + (*(*ppEnv)->release)( *ppEnv ); + + OUString const & rEnvDcp = OUString::unacquired( &pEnvDcp ); + *ppEnv = initDefaultEnvironment( rEnvDcp, pContext ); +} + +void SAL_CALL uno_getEnvironment( + uno_Environment ** ppEnv, rtl_uString * pEnvDcp, void * pContext ) + SAL_THROW_EXTERN_C() +{ + assert(ppEnv && "### null ptr!"); + OUString const & rEnvDcp = OUString::unacquired( &pEnvDcp ); + + EnvironmentsData & rData = theEnvironmentsData(); + + ::osl::MutexGuard guard( rData.mutex ); + rData.getEnvironment( ppEnv, rEnvDcp, pContext ); + if (! *ppEnv) + { + *ppEnv = initDefaultEnvironment( rEnvDcp, pContext ); + if (*ppEnv) + { + // register new environment: + rData.registerEnvironment( ppEnv ); + } + } +} + +void SAL_CALL uno_getRegisteredEnvironments( + uno_Environment *** pppEnvs, sal_Int32 * pnLen, uno_memAlloc memAlloc, + rtl_uString * pEnvDcp ) + SAL_THROW_EXTERN_C() +{ + EnvironmentsData & rData = theEnvironmentsData(); + + ::osl::MutexGuard guard( rData.mutex ); + rData.getRegisteredEnvironments( + pppEnvs, pnLen, memAlloc, + (pEnvDcp ? OUString(pEnvDcp) : OUString()) ); +} + +} // extern "C" + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/lbmap.cxx b/cppu/source/uno/lbmap.cxx new file mode 100644 index 0000000000..04e4c28eee --- /dev/null +++ b/cppu/source/uno/lbmap.cxx @@ -0,0 +1,753 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifdef DISABLE_DYNLOADING +#include <config_java.h> +#endif + +#include "IdentityMapping.hxx" + +#include <cassert> +#include <mutex> +#include <set> +#include <unordered_map> +#include <utility> + +#include <rtl/ustring.hxx> +#include <rtl/ustrbuf.hxx> +#include <osl/module.hxx> +#include <osl/diagnose.h> +#include <osl/mutex.hxx> +#include <osl/interlck.h> +#include <sal/log.hxx> + +#include <uno/dispatcher.h> +#include <uno/mapping.h> +#include <uno/lbnames.h> +#include <uno/environment.hxx> + +#include <typelib/typedescription.h> + +#include <cppu/EnvDcp.hxx> +#include "cascade_mapping.hxx" +#include "loadmodule.hxx" + +using namespace osl; +using namespace com::sun::star::uno; + +namespace cppu +{ + +namespace { + +class Mapping +{ + uno_Mapping * _pMapping; + +public: + inline explicit Mapping( uno_Mapping * pMapping = nullptr ); + inline Mapping( const Mapping & rMapping ); + Mapping(Mapping && other) noexcept : _pMapping(other._pMapping) + { other._pMapping = nullptr; } + inline ~Mapping(); + inline Mapping & operator = ( uno_Mapping * pMapping ); + Mapping & operator = ( const Mapping & rMapping ) + { return operator = ( rMapping._pMapping ); } + Mapping & operator =(Mapping && other) noexcept { + if (_pMapping != nullptr) { + (*_pMapping->release)(_pMapping); + } + _pMapping = other._pMapping; + other._pMapping = nullptr; + return *this; + } + uno_Mapping * get() const + { return _pMapping; } + bool is() const + { return (_pMapping != nullptr); } +}; + +} + +inline Mapping::Mapping( uno_Mapping * pMapping ) + : _pMapping( pMapping ) +{ + if (_pMapping) + (*_pMapping->acquire)( _pMapping ); +} + +inline Mapping::Mapping( const Mapping & rMapping ) + : _pMapping( rMapping._pMapping ) +{ + if (_pMapping) + (*_pMapping->acquire)( _pMapping ); +} + +inline Mapping::~Mapping() +{ + if (_pMapping) + (*_pMapping->release)( _pMapping ); +} + +inline Mapping & Mapping::operator = ( uno_Mapping * pMapping ) +{ + if (pMapping) + (*pMapping->acquire)( pMapping ); + if (_pMapping) + (*_pMapping->release)( _pMapping ); + _pMapping = pMapping; + return *this; +} + +namespace { + +struct MappingEntry +{ + sal_Int32 nRef; + uno_Mapping * pMapping; + uno_freeMappingFunc freeMapping; + OUString aMappingName; + + MappingEntry( + uno_Mapping * pMapping_, uno_freeMappingFunc freeMapping_, + OUString aMappingName_ ) + : nRef( 1 ) + , pMapping( pMapping_ ) + , freeMapping( freeMapping_ ) + , aMappingName(std::move( aMappingName_ )) + {} +}; + +struct FctPtrHash +{ + size_t operator()( uno_Mapping * pKey ) const + { return reinterpret_cast<size_t>(pKey); } +}; + +} + +typedef std::unordered_map< + OUString, MappingEntry * > t_OUString2Entry; +typedef std::unordered_map< + uno_Mapping *, MappingEntry *, FctPtrHash > t_Mapping2Entry; + +namespace { + +struct MappingsData +{ + Mutex aMappingsMutex; + t_OUString2Entry aName2Entry; + t_Mapping2Entry aMapping2Entry; + + std::mutex aCallbacksMutex; + std::set< uno_getMappingFunc > + aCallbacks; + + std::mutex aNegativeLibsMutex; + std::set<OUString> aNegativeLibs; +}; + +} + +static MappingsData & getMappingsData() +{ + //TODO This memory is leaked; see #i63473# for when this should be + // changed again: + static MappingsData * s_p(new MappingsData); + + return *s_p; +} + +namespace { + +/** + * This class mediates two different mapping via uno, e.g. form any language to uno, + * then from uno to any other language. + */ +struct uno_Mediate_Mapping : public uno_Mapping +{ + sal_Int32 nRef; + + Environment aFrom; + Environment aTo; + + Mapping aFrom2Uno; + Mapping aUno2To; + + OUString aAddPurpose; + + uno_Mediate_Mapping( + Environment aFrom_, Environment aTo_, + Mapping aFrom2Uno_, Mapping aUno2To_, + OUString aAddPurpose ); +}; + +} + +extern "C" +{ + +static void mediate_free( uno_Mapping * pMapping ) +{ + delete static_cast< uno_Mediate_Mapping * >( pMapping ); +} + +static void mediate_acquire( uno_Mapping * pMapping ) +{ + if (1 == osl_atomic_increment( + & static_cast< uno_Mediate_Mapping * >( pMapping )->nRef )) + { + uno_registerMapping( + &pMapping, mediate_free, + static_cast< uno_Mediate_Mapping * >( pMapping )->aFrom.get(), + static_cast< uno_Mediate_Mapping * >( pMapping )->aTo.get(), + static_cast< uno_Mediate_Mapping * >( pMapping )->aAddPurpose.pData ); + } +} + +static void mediate_release( uno_Mapping * pMapping ) +{ + if (! osl_atomic_decrement( + & static_cast< uno_Mediate_Mapping * >( pMapping )->nRef )) + { + uno_revokeMapping( pMapping ); + } +} + +static void mediate_mapInterface( + uno_Mapping * pMapping, + void ** ppOut, void * pInterface, + typelib_InterfaceTypeDescription * pInterfaceTypeDescr ) +{ + OSL_ENSURE( pMapping && ppOut, "### null ptr!" ); + if (!(pMapping && ppOut)) + return; + + uno_Mediate_Mapping * that = static_cast< uno_Mediate_Mapping * >( pMapping ); + uno_Mapping * pFrom2Uno = that->aFrom2Uno.get(); + + uno_Interface * pUnoI = nullptr; + (*pFrom2Uno->mapInterface)( pFrom2Uno, reinterpret_cast<void **>(&pUnoI), pInterface, pInterfaceTypeDescr ); + if (nullptr == pUnoI) + { + void * pOut = *ppOut; + if (nullptr != pOut) + { + uno_ExtEnvironment * pTo = that->aTo.get()->pExtEnv; + OSL_ENSURE( nullptr != pTo, "### cannot release out interface: leaking!" ); + if (nullptr != pTo) + (*pTo->releaseInterface)( pTo, pOut ); + *ppOut = nullptr; // set to 0 anyway, because mapping was not successful! + } + } + else + { + uno_Mapping * pUno2To = that->aUno2To.get(); + (*pUno2To->mapInterface)( pUno2To, ppOut, pUnoI, pInterfaceTypeDescr ); + (*pUnoI->release)( pUnoI ); + } +} +} + +uno_Mediate_Mapping::uno_Mediate_Mapping( + Environment aFrom_, Environment aTo_, + Mapping aFrom2Uno_, Mapping aUno2To_, + OUString aAddPurpose_ ) + : nRef( 1 ) + , aFrom(std::move( aFrom_ )) + , aTo(std::move( aTo_ )) + , aFrom2Uno(std::move( aFrom2Uno_ )) + , aUno2To(std::move( aUno2To_ )) + , aAddPurpose(std::move( aAddPurpose_ )) +{ + uno_Mapping::acquire = mediate_acquire; + uno_Mapping::release = mediate_release; + uno_Mapping::mapInterface = mediate_mapInterface; +} + + +static OUString getMappingName( + const Environment & rFrom, const Environment & rTo, std::u16string_view rAddPurpose ) +{ + return + OUString::Concat(rAddPurpose) + + ";" + + rFrom.getTypeName() + + "[" + + OUString::number( reinterpret_cast< sal_IntPtr >(rFrom.get()), 16 ) + + "];" + + rTo.getTypeName() + + "[" + + OUString::number( reinterpret_cast< sal_IntPtr >(rTo.get()), 16 ) + + "]"; +} + +static OUString getBridgeName( + const Environment & rFrom, const Environment & rTo, std::u16string_view rAddPurpose ) +{ + OUStringBuffer aBridgeName( 16 ); + if (!rAddPurpose.empty()) + { + aBridgeName.append( OUString::Concat(rAddPurpose) + "_" ); + } + aBridgeName.append( + EnvDcp::getTypeName(rFrom.getTypeName()) + + "_" + + EnvDcp::getTypeName(rTo.getTypeName()) ); + return aBridgeName.makeStringAndClear(); +} + +#ifndef DISABLE_DYNLOADING + +static void setNegativeBridge( const OUString & rBridgeName ) +{ + MappingsData & rData = getMappingsData(); + std::scoped_lock aGuard( rData.aNegativeLibsMutex ); + rData.aNegativeLibs.insert( rBridgeName ); +} + +#endif + +#ifdef DISABLE_DYNLOADING + +static uno_ext_getMappingFunc selectMapFunc( const OUString & rBridgeName ) +{ + if (rBridgeName.equalsAscii( CPPU_CURRENT_LANGUAGE_BINDING_NAME "_uno" )) + return CPPU_ENV_uno_ext_getMapping; +#if HAVE_FEATURE_JAVA + if (rBridgeName.equalsAscii( "java" "_uno" )) + return java_uno_ext_getMapping; +#endif + +#if 0 + // I don't think the affine or log bridges will be needed on any + // DISABLE_DYNLOADING platform (iOS at least, possibly Android), but if + // somebody wants to experiment, need to find out then whether these are + // needed. + if (rBridgeName.equalsAscii( "affine_uno_uno" )) + return affine_uno_uno_ext_getMapping; + if (rBridgeName.equalsAscii( "log_uno_uno" )) + return log_uno_uno_ext_getMapping; +#endif + return 0; +} + +#else + +static bool loadModule(osl::Module & rModule, const OUString & rBridgeName) +{ + bool bNeg; + { + MappingsData & rData = getMappingsData(); + std::scoped_lock aGuard( rData.aNegativeLibsMutex ); + const auto iFind( rData.aNegativeLibs.find( rBridgeName ) ); + bNeg = (iFind != rData.aNegativeLibs.end()); + } + + if (!bNeg) + { + bool bModule; + try { + bModule = cppu::detail::loadModule(rModule, rBridgeName); + } + catch(...) { + // convert throw to return false + bModule = false; + } + + if (bModule) + return true; + + setNegativeBridge( rBridgeName ); // no load again + } + return false; +} + +#endif + + +static Mapping loadExternalMapping( + const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose ) +{ + OSL_ASSERT( rFrom.is() && rTo.is() ); + if (rFrom.is() && rTo.is()) + { +#ifdef DISABLE_DYNLOADING + OUString aName; + uno_ext_getMappingFunc fpGetMapFunc = 0; + + if (EnvDcp::getTypeName(rFrom.getTypeName()) == UNO_LB_UNO) + { + aName = getBridgeName( rTo, rFrom, rAddPurpose ); + fpGetMapFunc = selectMapFunc( aName ); + } + if (! fpGetMapFunc) + { + aName = getBridgeName( rFrom, rTo, rAddPurpose ); + fpGetMapFunc = selectMapFunc( aName ); + } + if (! fpGetMapFunc) + { + aName = getBridgeName( rTo, rFrom, rAddPurpose ); + fpGetMapFunc = selectMapFunc( aName ); + } + + if (! fpGetMapFunc) + { + SAL_INFO("cppu", "Could not find mapfunc for " << aName); + return Mapping(); + } + + if (fpGetMapFunc) + { + Mapping aExt; + (*fpGetMapFunc)( (uno_Mapping **)&aExt, rFrom.get(), rTo.get() ); + OSL_ASSERT( aExt.is() ); + if (aExt.is()) + return aExt; + SAL_INFO("cppu", "Could not load external mapping for " << aName); + } +#else + // find proper lib + osl::Module aModule; + bool bModule(false); + OUString aName; + + if ( EnvDcp::getTypeName(rFrom.getTypeName()) == UNO_LB_UNO ) + { + aName = getBridgeName( rTo, rFrom, rAddPurpose ); + bModule = loadModule( aModule, aName ); + } + if (!bModule) + { + aName = getBridgeName( rFrom, rTo, rAddPurpose ); + bModule = loadModule( aModule, aName ); + } + if (!bModule) + { + aName = getBridgeName( rTo, rFrom, rAddPurpose ); + bModule = loadModule( aModule, aName ); + } + + if (bModule) + { + uno_ext_getMappingFunc fpGetMapFunc = + reinterpret_cast<uno_ext_getMappingFunc>(aModule.getSymbol( UNO_EXT_GETMAPPING )); + + if (fpGetMapFunc) + { + Mapping aExt; + (*fpGetMapFunc)( reinterpret_cast<uno_Mapping **>(&aExt), rFrom.get(), rTo.get() ); + OSL_ASSERT( aExt.is() ); + if (aExt.is()) + { + aModule.release(); + return aExt; + } + } + aModule.unload(); + setNegativeBridge( aName ); + } +#endif + } + return Mapping(); +} + + +static Mapping getDirectMapping( + const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose = OUString() ) + +{ + OSL_ASSERT( rFrom.is() && rTo.is() ); + if (rFrom.is() && rTo.is()) + { + MappingsData & rData = getMappingsData(); + ClearableMutexGuard aGuard( rData.aMappingsMutex ); + + // try to find registered mapping + const t_OUString2Entry::const_iterator iFind( rData.aName2Entry.find( + getMappingName( rFrom, rTo, rAddPurpose ) ) ); + + if (iFind == rData.aName2Entry.end()) + { + aGuard.clear(); + return loadExternalMapping( rFrom, rTo, rAddPurpose ); + } + return Mapping( (*iFind).second->pMapping ); + } + return Mapping(); +} + + +static Mapping createMediateMapping( + const Environment & rFrom, const Environment & rTo, + const Mapping & rFrom2Uno, const Mapping & rUno2To, + const OUString & rAddPurpose ) +{ + uno_Mapping * pRet = new uno_Mediate_Mapping( + rFrom, rTo, rFrom2Uno, rUno2To, rAddPurpose ); // ref count initially 1 + uno_registerMapping( + &pRet, mediate_free, rFrom.get(), rTo.get(), rAddPurpose.pData ); + Mapping aRet( pRet ); + (*pRet->release)( pRet ); + return aRet; +} + +static Mapping getMediateMapping( + const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose ) +{ + Environment aUno; + Mapping aUno2To; + + // backwards: from dest to source of mapping chain + + // connect to uno + OUString aUnoEnvTypeName( UNO_LB_UNO ); + if (rTo.getTypeName() == aUnoEnvTypeName) // to is uno + { + aUno = rTo; + // no Uno2To mapping necessary + } + else + { + // get registered uno env + ::uno_getEnvironment( reinterpret_cast<uno_Environment **>(&aUno), aUnoEnvTypeName.pData, nullptr ); + + aUno2To = getDirectMapping( aUno, rTo ); + // : uno <-> to + if (! aUno2To.is()) + return Mapping(); + } + + // connect to uno + if (!rAddPurpose.isEmpty()) // insert purpose mapping between new ano_uno <-> uno + { + // create anonymous uno env + Environment aAnUno; + ::uno_createEnvironment( reinterpret_cast<uno_Environment **>(&aAnUno), aUnoEnvTypeName.pData, nullptr ); + + Mapping aAnUno2Uno( getDirectMapping( aAnUno, aUno, rAddPurpose ) ); + if (! aAnUno2Uno.is()) + return Mapping(); + + if (aUno2To.is()) // to is not uno + { + // create another purposed mediate mapping + aUno2To = createMediateMapping( aAnUno, rTo, aAnUno2Uno, aUno2To, rAddPurpose ); + // : ano_uno <-> uno <-> to + } + else + { + aUno2To = aAnUno2Uno; + // : ano_uno <-> to (i.e., uno) + } + aUno = aAnUno; + } + + Mapping aFrom2Uno( getDirectMapping( rFrom, aUno ) ); + if (aFrom2Uno.is() && aUno2To.is()) + { + return createMediateMapping( rFrom, rTo, aFrom2Uno, aUno2To, rAddPurpose ); + // : from <-> some uno ... + } + + return Mapping(); +} +} + +using namespace ::cppu; + +extern "C" +{ + +void SAL_CALL uno_getMapping( + uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo, + rtl_uString * pAddPurpose ) + SAL_THROW_EXTERN_C() +{ + assert(ppMapping != nullptr); + assert(pFrom != nullptr); + assert(pTo != nullptr); + if (*ppMapping) + { + (*(*ppMapping)->release)( *ppMapping ); + *ppMapping = nullptr; + } + + Mapping aRet; + Environment aFrom( pFrom ), aTo( pTo ); + + OUString aAddPurpose; + if (pAddPurpose) + aAddPurpose = pAddPurpose; + + MappingsData & rData = getMappingsData(); + + // try registered mapping + { + MutexGuard aGuard( rData.aMappingsMutex ); + const t_OUString2Entry::const_iterator iFind( rData.aName2Entry.find( + getMappingName( aFrom, aTo, aAddPurpose ) ) ); + if (iFind != rData.aName2Entry.end()) + aRet = (*iFind).second->pMapping; + } + + // See if an identity mapping does fit. + if (!aRet.is() && pFrom == pTo && aAddPurpose.isEmpty()) + aRet = createIdentityMapping(pFrom); + + if (!aRet.is()) + { + getCascadeMapping(ppMapping, pFrom, pTo, pAddPurpose); + + if (*ppMapping) + return; + + // try callback chain + { + std::unique_lock aGuard(rData.aCallbacksMutex); + for (const auto& rCallback : rData.aCallbacks) + { + (*rCallback)(ppMapping, pFrom, pTo, aAddPurpose.pData); + if (*ppMapping) + return; + } + } + + aRet = loadExternalMapping( aFrom, aTo, aAddPurpose ); // direct try + if (! aRet.is()) + aRet = getMediateMapping( aFrom, aTo, aAddPurpose ); // try via uno + } + + if (aRet.is()) + { + (*aRet.get()->acquire)( aRet.get() ); + *ppMapping = aRet.get(); + } +} + +void SAL_CALL uno_getMappingByName( + uno_Mapping ** ppMapping, rtl_uString * pFrom, rtl_uString * pTo, + rtl_uString * pAddPurpose ) + SAL_THROW_EXTERN_C() +{ + assert(ppMapping && pFrom && pTo && "### null ptr!"); + if (*ppMapping) + { + (*(*ppMapping)->release)( *ppMapping ); + *ppMapping = nullptr; + } + + uno_Environment * pEFrom = nullptr; + uno_getEnvironment( &pEFrom, pFrom, nullptr ); + OSL_ENSURE( pEFrom, "### cannot get source environment!" ); + if (pEFrom) + { + uno_Environment * pETo = nullptr; + uno_getEnvironment( &pETo, pTo, nullptr ); + OSL_ENSURE( pETo, "### cannot get target environment!" ); + if (pETo) + { + ::uno_getMapping( ppMapping, pEFrom, pETo, pAddPurpose ); + (*pETo->release)( pETo ); + } + (*pEFrom->release)( pEFrom ); + } +} + + +void SAL_CALL uno_registerMapping( + uno_Mapping ** ppMapping, uno_freeMappingFunc freeMapping, + uno_Environment * pFrom, uno_Environment * pTo, rtl_uString * pAddPurpose ) + SAL_THROW_EXTERN_C() +{ + MappingsData & rData = getMappingsData(); + ClearableMutexGuard aGuard( rData.aMappingsMutex ); + + const t_Mapping2Entry::const_iterator iFind( rData.aMapping2Entry.find( *ppMapping ) ); + if (iFind == rData.aMapping2Entry.end()) + { + OUString aMappingName( + getMappingName( pFrom, pTo, pAddPurpose ? OUString(pAddPurpose) : OUString() ) ); + SAL_INFO("cppu", "> inserting new mapping: " << aMappingName); + // count initially 1 + MappingEntry * pEntry = new MappingEntry( *ppMapping, freeMapping, aMappingName ); + rData.aName2Entry[ aMappingName ] = pEntry; + rData.aMapping2Entry[ *ppMapping ] = pEntry; + } + else + { + MappingEntry * pEntry = (*iFind).second; + ++pEntry->nRef; + + if (pEntry->pMapping != *ppMapping) // exchange mapping to be registered + { + (*pEntry->pMapping->acquire)( pEntry->pMapping ); + --pEntry->nRef; // correct count; kill mapping to be registered + aGuard.clear(); + (*freeMapping)( *ppMapping ); + *ppMapping = pEntry->pMapping; + } + } +} + +void SAL_CALL uno_revokeMapping( + uno_Mapping * pMapping ) + SAL_THROW_EXTERN_C() +{ + MappingsData & rData = getMappingsData(); + ClearableMutexGuard aGuard( rData.aMappingsMutex ); + + const t_Mapping2Entry::const_iterator iFind( rData.aMapping2Entry.find( pMapping ) ); + assert(iFind != rData.aMapping2Entry.end() && "pMapping must be registered to be removed"); + MappingEntry * pEntry = (*iFind).second; + if (! --pEntry->nRef) + { + rData.aMapping2Entry.erase( pEntry->pMapping ); + rData.aName2Entry.erase( pEntry->aMappingName ); + aGuard.clear(); + SAL_INFO("cppu", "> revoking mapping " << pEntry->aMappingName); + (*pEntry->freeMapping)( pEntry->pMapping ); + delete pEntry; + } +} + + +void SAL_CALL uno_registerMappingCallback( + uno_getMappingFunc pCallback ) + SAL_THROW_EXTERN_C() +{ + OSL_ENSURE( pCallback, "### null ptr!" ); + MappingsData & rData = getMappingsData(); + std::unique_lock aGuard( rData.aCallbacksMutex ); + rData.aCallbacks.insert( pCallback ); +} + +void SAL_CALL uno_revokeMappingCallback( + uno_getMappingFunc pCallback ) + SAL_THROW_EXTERN_C() +{ + OSL_ENSURE( pCallback, "### null ptr!" ); + MappingsData & rData = getMappingsData(); + std::unique_lock aGuard( rData.aCallbacksMutex ); + rData.aCallbacks.erase( pCallback ); +} +} // extern "C" + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/loadmodule.cxx b/cppu/source/uno/loadmodule.cxx new file mode 100644 index 0000000000..f2811a5958 --- /dev/null +++ b/cppu/source/uno/loadmodule.cxx @@ -0,0 +1,75 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <sal/config.h> + +#include <cassert> + +#include <osl/module.h> +#include <osl/module.hxx> +#include <rtl/malformeduriexception.hxx> +#include <rtl/uri.hxx> +#include <rtl/ustring.hxx> +#include <sal/log.hxx> + +#include "loadmodule.hxx" + +namespace cppu::detail { + +#ifndef DISABLE_DYNLOADING + +bool loadModule(osl::Module& rModule, OUString const & name) { + static OUString base = [] { + OUString url; + if (!osl::Module::getUrlFromAddress( + reinterpret_cast<oslGenericFunction>(&loadModule), url)) + { + SAL_WARN("cppu", "osl::Module::getUrlFromAddress failed"); + return OUString(); + } + assert(!url.isEmpty()); + return url; + }(); + if (base.isEmpty()) { + SAL_INFO("cppu", "osl::Module::getUrlFromAddress had failed"); + return false; + } + OUString b = +#if defined SAL_DLLPREFIX + SAL_DLLPREFIX + +#endif + name + + SAL_DLLEXTENSION; + try { + b = rtl::Uri::convertRelToAbs(base, b); + } catch (rtl::MalformedUriException & e) { + SAL_INFO("cppu", "rtl::MalformedUriException <" << e.getMessage() << ">"); + return false; + } + return rModule.load( + b, + SAL_LOADMODULE_GLOBAL | SAL_LOADMODULE_LAZY); +} + +#endif + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/loadmodule.hxx b/cppu/source/uno/loadmodule.hxx new file mode 100644 index 0000000000..694b140249 --- /dev/null +++ b/cppu/source/uno/loadmodule.hxx @@ -0,0 +1,47 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <sal/config.h> +#include <rtl/ustring.hxx> + +namespace osl +{ +class Module; +} + +namespace cppu::detail +{ +#ifndef DISABLE_DYNLOADING + +/** Load a module. + + @param name + the nucleus of a module name (without any "lib...so", ".dll", etc. + decoration, and without a path). + + @return false if the module could not be loaded, otherwise true +*/ +bool loadModule(osl::Module& rModule, OUString const& name); + +#endif +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/prim.hxx b/cppu/source/uno/prim.hxx new file mode 100644 index 0000000000..733baec0f6 --- /dev/null +++ b/cppu/source/uno/prim.hxx @@ -0,0 +1,153 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +#pragma once + +#include <typelib/typedescription.h> +#include <typelib/typeclass.h> +#include <uno/sequence2.h> +#include <uno/any2.h> +#include <uno/data.h> +#include <uno/mapping.h> +#include <uno/dispatcher.h> + +#include <osl/interlck.h> +#include <stdint.h> + +namespace cppu +{ + +extern uno_Sequence g_emptySeq; +extern typelib_TypeDescriptionReference * g_pVoidType; + + +inline void * _map( + void * p, + typelib_TypeDescriptionReference * pType, typelib_TypeDescription * pTypeDescr, + uno_Mapping * mapping ) + +{ + void * pRet = nullptr; + if (p) + { + if (pTypeDescr) + { + (*mapping->mapInterface)( + mapping, &pRet, p, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTypeDescr) ); + } + else + { + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + (*mapping->mapInterface)( + mapping, &pRet, p, reinterpret_cast<typelib_InterfaceTypeDescription *>(pTypeDescr) ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + } + return pRet; +} + +inline void _acquire( void * p, uno_AcquireFunc acquire ) +{ + if (p) + { + if (acquire) + { + (*acquire)( p ); + } + else + { + (*static_cast<uno_Interface *>(p)->acquire)( static_cast<uno_Interface *>(p) ); + } + } +} + +inline void _release( void * p, uno_ReleaseFunc release ) +{ + if (p) + { + if (release) + { + (*release)( p ); + } + else + { + (*static_cast<uno_Interface *>(p)->release)( static_cast<uno_Interface *>(p) ); + } + } +} + + +inline sal_uInt32 calcSeqMemSize( + sal_Int32 nElementSize, sal_Int32 nElements ) +{ + sal_uInt64 nSize = + static_cast<sal_uInt64>(SAL_SEQUENCE_HEADER_SIZE) + + (static_cast<sal_uInt64>(nElementSize) * static_cast<sal_uInt64>(nElements)); + if (nSize > 0xffffffffU) + return 0; + else + return static_cast<sal_uInt32>(nSize); +} + + +inline uno_Sequence * createEmptySequence() +{ + osl_atomic_increment( &g_emptySeq.nRefCount ); + return &g_emptySeq; +} + +inline typelib_TypeDescriptionReference * _getVoidType() +{ + if (! g_pVoidType) + { + g_pVoidType = * ::typelib_static_type_getByTypeClass( typelib_TypeClass_VOID ); + } + ::typelib_typedescriptionreference_acquire( g_pVoidType ); + return g_pVoidType; +} + +inline void CONSTRUCT_EMPTY_ANY(uno_Any * pAny) { + pAny->pType = _getVoidType(); +#if OSL_DEBUG_LEVEL > 0 + pAny->pData = reinterpret_cast<void *>(uintptr_t(0xdeadbeef)); +#else + pAny->pData = pAny; +#endif +} + +#define TYPE_ACQUIRE( pType ) \ + osl_atomic_increment( &(pType)->nRefCount ); + + +extern "C" void * binuno_queryInterface( + void * pUnoI, typelib_TypeDescriptionReference * pDestType ); + + +inline bool _type_equals( + typelib_TypeDescriptionReference const * pType1, typelib_TypeDescriptionReference const * pType2 ) + +{ + return (pType1 == pType2 || + (pType1->eTypeClass == pType2->eTypeClass && + pType1->pTypeName->length == pType2->pTypeName->length && + ::rtl_ustr_compare( pType1->pTypeName->buffer, pType2->pTypeName->buffer ) == 0)); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/cppu/source/uno/sequence.cxx b/cppu/source/uno/sequence.cxx new file mode 100644 index 0000000000..c467f2c387 --- /dev/null +++ b/cppu/source/uno/sequence.cxx @@ -0,0 +1,912 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/config.h> + +#include <cassert> +#include <string.h> + +#include <osl/diagnose.h> +#include <osl/interlck.h> +#include <typelib/typedescription.h> +#include <uno/data.h> +#include <uno/sequence2.h> + +#include "constr.hxx" +#include "copy.hxx" +#include "destr.hxx" + + +using namespace cppu; + +namespace cppu +{ + + +static uno_Sequence * reallocSeq( + uno_Sequence * pReallocate, std::size_t nElementSize, sal_Int32 nElements ) +{ + OSL_ASSERT( nElements >= 0 ); + uno_Sequence * pNew = nullptr; + sal_uInt32 nSize = calcSeqMemSize( nElementSize, nElements ); + if (nSize > 0) + { + if (pReallocate == nullptr) + { + pNew = static_cast<uno_Sequence *>(std::malloc( nSize )); + } + else + { + pNew = static_cast<uno_Sequence *>(std::realloc( pReallocate, nSize )); + } + if (pNew != nullptr) + { + // header init + pNew->nRefCount = 1; + pNew->nElements = nElements; + } + } + return pNew; +} + + +static bool idefaultConstructElements( + uno_Sequence ** ppSeq, + typelib_TypeDescriptionReference * pElementType, + sal_Int32 nStartIndex, sal_Int32 nStopIndex, + sal_Int32 nAlloc ) // >= 0 means (re)alloc memory for nAlloc elements +{ + uno_Sequence * pSeq = *ppSeq; + switch (pElementType->eTypeClass) + { + case typelib_TypeClass_CHAR: + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(sal_Unicode), nAlloc ); + if (pSeq != nullptr) + { + memset( + pSeq->elements + (sizeof(sal_Unicode) * nStartIndex), + 0, + sizeof(sal_Unicode) * (nStopIndex - nStartIndex) ); + } + break; + case typelib_TypeClass_BOOLEAN: + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(sal_Bool), nAlloc ); + if (pSeq != nullptr) + { + memset( + pSeq->elements + (sizeof(sal_Bool) * nStartIndex), + 0, + sizeof(sal_Bool) * (nStopIndex - nStartIndex) ); + } + break; + case typelib_TypeClass_BYTE: + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(sal_Int8), nAlloc ); + if (pSeq != nullptr) + { + memset( + pSeq->elements + (sizeof(sal_Int8) * nStartIndex), + 0, + sizeof(sal_Int8) * (nStopIndex - nStartIndex) ); + } + break; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(sal_Int16), nAlloc ); + if (pSeq != nullptr) + { + memset( + pSeq->elements + (sizeof(sal_Int16) * nStartIndex), + 0, + sizeof(sal_Int16) * (nStopIndex - nStartIndex) ); + } + break; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc ); + if (pSeq != nullptr) + { + memset( + pSeq->elements + (sizeof(sal_Int32) * nStartIndex), + 0, + sizeof(sal_Int32) * (nStopIndex - nStartIndex) ); + } + break; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(sal_Int64), nAlloc ); + if (pSeq != nullptr) + { + memset( + pSeq->elements + (sizeof(sal_Int64) * nStartIndex), + 0, + sizeof(sal_Int64) * (nStopIndex - nStartIndex) ); + } + break; + case typelib_TypeClass_FLOAT: + { + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(float), nAlloc ); + if (pSeq != nullptr) + { + float * pElements = reinterpret_cast<float *>(pSeq->elements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + pElements[nPos] = 0.0; + } + } + break; + } + case typelib_TypeClass_DOUBLE: + { + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(double), nAlloc ); + if (pSeq != nullptr) + { + double * pElements = reinterpret_cast<double *>(pSeq->elements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + pElements[nPos] = 0.0; + } + } + break; + } + case typelib_TypeClass_STRING: + { + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(rtl_uString *), nAlloc ); + if (pSeq != nullptr) + { + rtl_uString ** pElements = reinterpret_cast<rtl_uString **>(pSeq->elements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + pElements[nPos] = nullptr; + rtl_uString_new( &pElements[nPos] ); + } + } + break; + } + case typelib_TypeClass_TYPE: + { + if (nAlloc >= 0) + { + pSeq = reallocSeq( + pSeq, sizeof(typelib_TypeDescriptionReference *), nAlloc ); + } + if (pSeq != nullptr) + { + typelib_TypeDescriptionReference ** pElements = + reinterpret_cast<typelib_TypeDescriptionReference **>(pSeq->elements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + pElements[nPos] = _getVoidType(); + } + } + break; + } + case typelib_TypeClass_ANY: + { + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(uno_Any), nAlloc ); + if (pSeq != nullptr) + { + uno_Any * pElements = reinterpret_cast<uno_Any *>(pSeq->elements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + CONSTRUCT_EMPTY_ANY( &pElements[nPos] ); + } + } + break; + } + case typelib_TypeClass_ENUM: + { + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc ); + if (pSeq != nullptr) + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + sal_Int32 eEnum = + reinterpret_cast<typelib_EnumTypeDescription *>( + pElementTypeDescr)->nDefaultEnumValue; + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + + sal_Int32 * pElements = reinterpret_cast<sal_Int32 *>(pSeq->elements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + pElements[nPos] = eEnum; + } + } + break; + } + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + sal_Int32 nElementSize = pElementTypeDescr->nSize; + + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, nElementSize, nAlloc ); + if (pSeq != nullptr) + { + char * pElements = pSeq->elements; + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + _defaultConstructStruct( + pElements + (nElementSize * nPos), + reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr) ); + } + } + + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + break; + } + case typelib_TypeClass_SEQUENCE: + { + if (nAlloc >= 0) + { + // coverity[suspicious_sizeof : FALSE] - sizeof(uno_Sequence*) is correct here + pSeq = reallocSeq(pSeq, sizeof(uno_Sequence*), nAlloc); + } + if (pSeq != nullptr) + { + uno_Sequence ** pElements = + reinterpret_cast<uno_Sequence **>(pSeq->elements); + for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos ) + { + pElements[nPos] = createEmptySequence(); + } + } + break; + } + case typelib_TypeClass_INTERFACE: // either C++ or C-UNO interface + if (nAlloc >= 0) + pSeq = reallocSeq( pSeq, sizeof(void *), nAlloc ); + if (pSeq != nullptr) + { + memset( + pSeq->elements + (sizeof(void *) * nStartIndex), + 0, + sizeof(void *) * (nStopIndex - nStartIndex) ); + } + break; + default: + OSL_FAIL( "### unexpected element type!" ); + pSeq = nullptr; + break; + } + + if (pSeq == nullptr) + { + OSL_ASSERT( nAlloc >= 0 ); // must have been an allocation failure + return false; + } + *ppSeq = pSeq; + return true; +} + +// coverity[ -tainted_data_sink : arg-1 ] +static bool icopyConstructFromElements( + uno_Sequence ** ppSeq, void * pSourceElements, + typelib_TypeDescriptionReference * pElementType, + sal_Int32 nStopIndex, + uno_AcquireFunc acquire, + sal_Int32 nAlloc ) +{ + uno_Sequence * pSeq = *ppSeq; + switch (pElementType->eTypeClass) + { + case typelib_TypeClass_CHAR: + pSeq = reallocSeq( pSeq, sizeof(sal_Unicode), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(sal_Unicode) * nStopIndex ); + } + break; + case typelib_TypeClass_BOOLEAN: + pSeq = reallocSeq( pSeq, sizeof(sal_Bool), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(sal_Bool) * nStopIndex ); + } + break; + case typelib_TypeClass_BYTE: + pSeq = reallocSeq( pSeq, sizeof(sal_Int8), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(sal_Int8) * nStopIndex ); + } + break; + case typelib_TypeClass_SHORT: + case typelib_TypeClass_UNSIGNED_SHORT: + pSeq = reallocSeq( pSeq, sizeof(sal_Int16), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(sal_Int16) * nStopIndex ); + } + break; + case typelib_TypeClass_LONG: + case typelib_TypeClass_UNSIGNED_LONG: + pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(sal_Int32) * nStopIndex ); + } + break; + case typelib_TypeClass_HYPER: + case typelib_TypeClass_UNSIGNED_HYPER: + pSeq = reallocSeq( pSeq, sizeof(sal_Int64), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(sal_Int64) * nStopIndex ); + } + break; + case typelib_TypeClass_FLOAT: + pSeq = reallocSeq( pSeq, sizeof(float), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(float) * nStopIndex ); + } + break; + case typelib_TypeClass_DOUBLE: + pSeq = reallocSeq( pSeq, sizeof(double), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(double) * nStopIndex ); + } + break; + case typelib_TypeClass_ENUM: + pSeq = reallocSeq( pSeq, sizeof(sal_Int32), nAlloc ); + if (pSeq != nullptr) + { + memcpy( + pSeq->elements, + pSourceElements, + sizeof(sal_Int32) * nStopIndex ); + } + break; + case typelib_TypeClass_STRING: + { + pSeq = reallocSeq( pSeq, sizeof(rtl_uString *), nAlloc ); + if (pSeq != nullptr) + { + rtl_uString ** pDestElements = reinterpret_cast<rtl_uString **>(pSeq->elements); + for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos ) + { + // This code tends to trigger coverity's overrun-buffer-arg warning + // coverity[index_parm_via_loop_bound] - https://communities.coverity.com/thread/2993 + ::rtl_uString_acquire( + static_cast<rtl_uString **>(pSourceElements)[nPos] ); + pDestElements[nPos] = static_cast<rtl_uString **>(pSourceElements)[nPos]; + } + } + break; + } + case typelib_TypeClass_TYPE: + { + pSeq = reallocSeq( + pSeq, sizeof(typelib_TypeDescriptionReference *), nAlloc ); + if (pSeq != nullptr) + { + typelib_TypeDescriptionReference ** pDestElements = + reinterpret_cast<typelib_TypeDescriptionReference **>(pSeq->elements); + for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos ) + { + TYPE_ACQUIRE( + static_cast<typelib_TypeDescriptionReference **>( + pSourceElements)[nPos] ); + pDestElements[nPos] = + static_cast<typelib_TypeDescriptionReference **>( + pSourceElements)[nPos]; + } + } + break; + } + case typelib_TypeClass_ANY: + { + pSeq = reallocSeq( pSeq, sizeof(uno_Any), nAlloc ); + if (pSeq != nullptr) + { + uno_Any * pDestElements = reinterpret_cast<uno_Any *>(pSeq->elements); + for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos ) + { + uno_Any * pSource = static_cast<uno_Any *>(pSourceElements) + nPos; + _copyConstructAny( + &pDestElements[nPos], + pSource->pData, + pSource->pType, nullptr, + acquire, nullptr ); + } + } + break; + } + case typelib_TypeClass_STRUCT: + case typelib_TypeClass_EXCEPTION: + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + sal_Int32 nElementSize = pElementTypeDescr->nSize; + + pSeq = reallocSeq( pSeq, nElementSize, nAlloc ); + if (pSeq != nullptr) + { + char * pDestElements = pSeq->elements; + + typelib_CompoundTypeDescription * pTypeDescr = + reinterpret_cast<typelib_CompoundTypeDescription *>(pElementTypeDescr); + for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos ) + { + char * pDest = + pDestElements + (nElementSize * nPos); + char * pSource = + static_cast<char *>(pSourceElements) + (nElementSize * nPos); + + if (pTypeDescr->pBaseTypeDescription) + { + // copy base value + _copyConstructStruct( + pDest, pSource, + pTypeDescr->pBaseTypeDescription, acquire, nullptr ); + } + + // then copy members + typelib_TypeDescriptionReference ** ppTypeRefs = + pTypeDescr->ppTypeRefs; + sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets; + sal_Int32 nDescr = pTypeDescr->nMembers; + + while (nDescr--) + { + ::uno_type_copyData( + pDest + pMemberOffsets[nDescr], + pSource + pMemberOffsets[nDescr], + ppTypeRefs[nDescr], acquire ); + } + } + } + + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + break; + } + case typelib_TypeClass_SEQUENCE: // sequence of sequence + { + // coverity[suspicious_sizeof : FALSE] - sizeof(uno_Sequence*) is correct here + pSeq = reallocSeq(pSeq, sizeof(uno_Sequence*), nAlloc); + if (pSeq != nullptr) + { + typelib_TypeDescription * pElementTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType ); + typelib_TypeDescriptionReference * pSeqElementType = + reinterpret_cast<typelib_IndirectTypeDescription *>(pElementTypeDescr)->pType; + uno_Sequence ** pDestElements = reinterpret_cast<uno_Sequence **>(pSeq->elements); + for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos ) + { + uno_Sequence * pNew = icopyConstructSequence( + static_cast<uno_Sequence **>(pSourceElements)[nPos], + pSeqElementType, acquire, nullptr ); + OSL_ASSERT( pNew != nullptr ); + // ought never be a memory allocation problem, + // because of reference counted sequence handles + pDestElements[ nPos ] = pNew; + } + TYPELIB_DANGER_RELEASE( pElementTypeDescr ); + } + break; + } + case typelib_TypeClass_INTERFACE: + { + pSeq = reallocSeq( pSeq, sizeof(void *), nAlloc ); + if (pSeq != nullptr) + { + void ** pDestElements = reinterpret_cast<void **>(pSeq->elements); + for ( sal_Int32 nPos = 0; nPos < nStopIndex; ++nPos ) + { + pDestElements[nPos] = static_cast<void **>(pSourceElements)[nPos]; + _acquire( pDestElements[nPos], acquire ); + } + } + break; + } + default: + OSL_FAIL( "### unexpected element type!" ); + pSeq = nullptr; + break; + } + + if (pSeq == nullptr) + { + return false; // allocation failure + } + *ppSeq = pSeq; + return true; +} + + +static bool ireallocSequence( + uno_Sequence ** ppSequence, + typelib_TypeDescriptionReference * pElementType, + sal_Int32 nSize, + uno_AcquireFunc acquire, uno_ReleaseFunc release ) +{ + bool ret = true; + uno_Sequence * pSeq = *ppSequence; + sal_Int32 nElements = pSeq->nElements; + + if (pSeq->nRefCount > 1 || + // not mem-copyable elements? + typelib_TypeClass_ANY == pElementType->eTypeClass || + typelib_TypeClass_STRUCT == pElementType->eTypeClass || + typelib_TypeClass_EXCEPTION == pElementType->eTypeClass) + { + // split sequence and construct new one from scratch + uno_Sequence * pNew = nullptr; + + sal_Int32 nRest = nSize - nElements; + sal_Int32 nCopy = (nRest > 0 ? nElements : nSize); + + if (nCopy >= 0) + { + ret = icopyConstructFromElements( + &pNew, pSeq->elements, pElementType, + nCopy, acquire, + nSize ); // alloc to nSize + } + if (ret && nRest > 0) + { + ret = idefaultConstructElements( + &pNew, pElementType, + nCopy, nSize, + nCopy >= 0 ? -1 /* no mem allocation */ : nSize ); + } + + if (ret) + { + // destruct sequence + if (osl_atomic_decrement( &pSeq->nRefCount ) == 0) + { + if (nElements > 0) + { + idestructElements( + pSeq->elements, pElementType, + 0, nElements, release ); + } + std::free( pSeq ); + } + *ppSequence = pNew; + } + } + else + { + OSL_ASSERT( pSeq->nRefCount == 1 ); + if (nSize > nElements) // default construct the rest + { + ret = idefaultConstructElements( + ppSequence, pElementType, + nElements, nSize, + nSize ); // realloc to nSize + } + else // or destruct the rest and realloc mem + { + sal_Int32 nElementSize = idestructElements( + pSeq->elements, pElementType, + nSize, nElements, release ); + // warning: it is assumed that the following will never fail, + // else this leads to a sequence null handle + *ppSequence = reallocSeq( pSeq, nElementSize, nSize ); + OSL_ASSERT( *ppSequence != nullptr ); + ret = (*ppSequence != nullptr); + } + } + + return ret; +} + +} + +extern "C" +{ + +sal_Bool SAL_CALL uno_type_sequence_construct( + uno_Sequence ** ppSequence, typelib_TypeDescriptionReference * pType, + void * pElements, sal_Int32 len, + uno_AcquireFunc acquire ) + SAL_THROW_EXTERN_C() +{ + assert( len >= 0 ); + bool ret; + if (len) + { + typelib_TypeDescription * pTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + + typelib_TypeDescriptionReference * pElementType = + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType; + + *ppSequence = nullptr; + if (pElements == nullptr) + { + ret = idefaultConstructElements( + ppSequence, pElementType, + 0, len, + len ); // alloc to len + } + else + { + ret = icopyConstructFromElements( + ppSequence, pElements, pElementType, + len, acquire, + len ); // alloc to len + } + + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + else + { + *ppSequence = createEmptySequence(); + ret = true; + } + + OSL_ASSERT( (*ppSequence != nullptr) == ret ); + return ret; +} + + +sal_Bool SAL_CALL uno_sequence_construct( + uno_Sequence ** ppSequence, typelib_TypeDescription * pTypeDescr, + void * pElements, sal_Int32 len, + uno_AcquireFunc acquire ) + SAL_THROW_EXTERN_C() +{ + bool ret; + if (len > 0) + { + typelib_TypeDescriptionReference * pElementType = + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType; + + *ppSequence = nullptr; + if (pElements == nullptr) + { + ret = idefaultConstructElements( + ppSequence, pElementType, + 0, len, + len ); // alloc to len + } + else + { + ret = icopyConstructFromElements( + ppSequence, pElements, pElementType, + len, acquire, + len ); // alloc to len + } + } + else + { + *ppSequence = createEmptySequence(); + ret = true; + } + + OSL_ASSERT( (*ppSequence != nullptr) == ret ); + return ret; +} + + +sal_Bool SAL_CALL uno_type_sequence_realloc( + uno_Sequence ** ppSequence, typelib_TypeDescriptionReference * pType, + sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + assert(ppSequence && "### null ptr!"); + assert(nSize >= 0 && "### new size must be at least 0!"); + + bool ret = true; + if (nSize != (*ppSequence)->nElements) + { + typelib_TypeDescription * pTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + ret = ireallocSequence( + ppSequence, reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + nSize, acquire, release ); + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + return ret; +} + + +sal_Bool SAL_CALL uno_sequence_realloc( + uno_Sequence ** ppSequence, typelib_TypeDescription * pTypeDescr, + sal_Int32 nSize, uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + OSL_ENSURE( ppSequence, "### null ptr!" ); + OSL_ENSURE( nSize >= 0, "### new size must be at least 0!" ); + + bool ret = true; + if (nSize != (*ppSequence)->nElements) + { + ret = ireallocSequence( + ppSequence, reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + nSize, acquire, release ); + } + return ret; +} + + +sal_Bool SAL_CALL uno_type_sequence_reference2One( + uno_Sequence ** ppSequence, + typelib_TypeDescriptionReference * pType, + uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + OSL_ENSURE( ppSequence, "### null ptr!" ); + bool ret = true; + uno_Sequence * pSequence = *ppSequence; + if (pSequence->nRefCount > 1) + { + uno_Sequence * pNew = nullptr; + if (pSequence->nElements > 0) + { + typelib_TypeDescription * pTypeDescr = nullptr; + TYPELIB_DANGER_GET( &pTypeDescr, pType ); + + ret = icopyConstructFromElements( + &pNew, pSequence->elements, + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + pSequence->nElements, acquire, + pSequence->nElements ); // alloc nElements + if (ret) + { + idestructSequence( *ppSequence, pType, pTypeDescr, release ); + *ppSequence = pNew; + } + + TYPELIB_DANGER_RELEASE( pTypeDescr ); + } + else + { + pNew = allocSeq( 0, 0 ); + ret = (pNew != nullptr); + if (ret) + { + // easy destruction of empty sequence: + if (osl_atomic_decrement( &pSequence->nRefCount ) == 0) + std::free( pSequence ); + *ppSequence = pNew; + } + } + } + return ret; +} + + +sal_Bool SAL_CALL uno_sequence_reference2One( + uno_Sequence ** ppSequence, + typelib_TypeDescription * pTypeDescr, + uno_AcquireFunc acquire, uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + OSL_ENSURE( ppSequence, "### null ptr!" ); + bool ret = true; + uno_Sequence * pSequence = *ppSequence; + if (pSequence->nRefCount > 1) + { + uno_Sequence * pNew = nullptr; + if (pSequence->nElements > 0) + { + ret = icopyConstructFromElements( + &pNew, pSequence->elements, + reinterpret_cast<typelib_IndirectTypeDescription *>(pTypeDescr)->pType, + pSequence->nElements, acquire, + pSequence->nElements ); // alloc nElements + if (ret) + { + idestructSequence( + pSequence, pTypeDescr->pWeakRef, pTypeDescr, release ); + *ppSequence = pNew; + } + } + else + { + pNew = allocSeq( 0, 0 ); + ret = (pNew != nullptr); + if (ret) + { + // easy destruction of empty sequence: + if (osl_atomic_decrement( &pSequence->nRefCount ) == 0) + std::free( pSequence ); + *ppSequence = pNew; + } + } + + } + return ret; +} + + +void SAL_CALL uno_sequence_assign( + uno_Sequence ** ppDest, + uno_Sequence * pSource, + typelib_TypeDescription * pTypeDescr, + uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + if (*ppDest != pSource) + { + osl_atomic_increment( &pSource->nRefCount ); + idestructSequence( *ppDest, pTypeDescr->pWeakRef, pTypeDescr, release ); + *ppDest = pSource; + } +} + + +void SAL_CALL uno_type_sequence_assign( + uno_Sequence ** ppDest, + uno_Sequence * pSource, + typelib_TypeDescriptionReference * pType, + uno_ReleaseFunc release ) + SAL_THROW_EXTERN_C() +{ + if (*ppDest != pSource) + { + osl_atomic_increment( &pSource->nRefCount ); + idestructSequence( *ppDest, pType, nullptr, release ); + *ppDest = pSource; + } +} + +void uno_type_sequence_destroy( + uno_Sequence * sequence, typelib_TypeDescriptionReference * type, + uno_ReleaseFunc release) + SAL_THROW_EXTERN_C() +{ + idestroySequence(sequence, type, nullptr, release); +} + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |