1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
32 #include <basic/sbx.hxx>
33 #include <basic/sbxvar.hxx>
34 #include "runtime.hxx"
35 #include <osl/thread.h>
36 #include <rtl/ref.hxx>
37 #include <rtl/string.hxx>
38 #include <rtl/ustring.hxx>
39 #include <sal/log.hxx>
40 #include <salhelper/simplereferenceobject.hxx>
41 #include <o3tl/char16_t2wchar_t.hxx>
48 using namespace css::uno
;
52 Missing support for functions returning structs (see TODO in call()).
54 Missing support for additional data types (64 bit integers, Any, ...; would
55 trigger assert(false) in various switches).
57 It is assumed that the variables passed into SbiDllMgr::Call to represent
58 the arguments and return value have types that exactly match the Declare
59 statement; it would be better if this code had access to the function
60 signature from the Declare statement, so that it could convert the passed
61 variables accordingly.
66 int __stdcall
DllMgr_call32(FARPROC
, void const * stack
, std::size_t size
);
67 double __stdcall
DllMgr_callFp(FARPROC
, void const * stack
, std::size_t size
);
73 char * address(std::vector
< char > & blob
) {
74 return blob
.empty() ? 0 : &blob
[0];
77 ErrCode
convert(OUString
const & source
, OString
* target
) {
79 source
.convertToString(
80 target
, osl_getThreadTextEncoding(),
81 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
82 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
))
83 ? ERRCODE_NONE
: ERRCODE_BASIC_BAD_ARGUMENT
;
84 //TODO: more specific errcode?
87 ErrCode
convert(char const * source
, sal_Int32 length
, OUString
* target
) {
89 rtl_convertStringToUString(
90 &target
->pData
, source
, length
, osl_getThreadTextEncoding(),
91 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
|
92 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
|
93 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR
))
94 ? ERRCODE_NONE
: ERRCODE_BASIC_BAD_ARGUMENT
;
95 //TODO: more specific errcode?
98 struct UnmarshalData
{
99 UnmarshalData(SbxVariable
* theVariable
, void * theBuffer
):
100 variable(theVariable
), buffer(theBuffer
) {}
102 SbxVariable
* variable
;
106 struct StringData
: public UnmarshalData
{
107 StringData(SbxVariable
* theVariable
, void * theBuffer
, bool theSpecial
):
108 UnmarshalData(theVariable
, theBuffer
), special(theSpecial
) {}
115 MarshalData() = default;
116 MarshalData(const MarshalData
&) = delete;
117 const MarshalData
& operator=(const MarshalData
&) = delete;
119 std::vector
< char > * newBlob() {
120 blobs_
.push_back(std::vector
< char >());
121 return &blobs_
.back();
124 std::vector
< UnmarshalData
> unmarshal
;
126 std::vector
< StringData
> unmarshalStrings
;
129 std::vector
< std::vector
< char > > blobs_
;
132 std::size_t align(std::size_t address
, std::size_t alignment
) {
133 // alignment = 2^k for some k >= 0
134 return (address
+ (alignment
- 1)) & ~(alignment
- 1);
138 std::vector
< char > & blob
, std::size_t alignment
, std::size_t offset
,
141 std::vector
< char >::size_type n
= blob
.size();
142 n
= align(n
- offset
, alignment
) + offset
; //TODO: overflow in align()
143 blob
.resize(n
+ add
); //TODO: overflow
144 return address(blob
) + n
;
147 template< typename T
> void add(
148 std::vector
< char > & blob
, T
const & data
, std::size_t alignment
,
151 *reinterpret_cast< T
* >(align(blob
, alignment
, offset
, sizeof (T
))) = data
;
154 std::size_t alignment(SbxVariable
* variable
) {
155 assert(variable
!= 0);
156 if ((variable
->GetType() & SbxARRAY
) == 0) {
157 switch (variable
->GetType()) {
169 SbxObject
* pobj
= dynamic_cast<SbxObject
*>(variable
->GetObject());
171 SbxArray
* props
= pobj
->GetProperties();
172 for (sal_uInt32 i
= 0; i
< props
->Count(); ++i
)
174 n
= std::max(n
, alignment(props
->Get(i
)));
186 SbxDimArray
* arr
= dynamic_cast<SbxDimArray
*>( variable
->GetObject() );
188 sal_Int32 dims
= arr
->GetDims();
189 std::vector
< sal_Int32
> low(dims
);
190 for (sal_Int32 i
= 0; i
< dims
; ++i
) {
192 arr
->GetDim(i
+ 1, low
[i
], up
);
194 return alignment(arr
->Get(&low
[0]));
199 bool outer
, SbxVariable
* variable
, bool special
,
200 std::vector
< char > & blob
, std::size_t offset
, MarshalData
& data
);
202 ErrCode
marshalString(
203 SbxVariable
* variable
, bool special
, MarshalData
& data
, void ** buffer
)
205 assert(variable
!= 0 && buffer
!= 0);
207 ErrCode e
= convert(variable
->GetOUString(), &str
);
208 if (e
!= ERRCODE_NONE
) {
211 std::vector
< char > * blob
= data
.newBlob();
213 blob
->begin(), str
.getStr(), str
.getStr() + str
.getLength() + 1);
214 *buffer
= address(*blob
);
215 data
.unmarshalStrings
.push_back(StringData(variable
, *buffer
, special
));
219 ErrCode
marshalStruct(
220 SbxVariable
* variable
, std::vector
< char > & blob
, std::size_t offset
,
223 assert(variable
!= 0);
224 SbxObject
* pobj
= dynamic_cast<SbxObject
*>(variable
->GetObject());
226 SbxArray
* props
= pobj
->GetProperties();
227 for (sal_uInt32 i
= 0; i
< props
->Count(); ++i
)
229 ErrCode e
= marshal(false, props
->Get(i
), false, blob
, offset
, data
);
230 if (e
!= ERRCODE_NONE
) {
237 ErrCode
marshalArray(
238 SbxVariable
* variable
, std::vector
< char > & blob
, std::size_t offset
,
241 assert(variable
!= 0);
242 SbxDimArray
* arr
= dynamic_cast<SbxDimArray
*>( variable
->GetObject() );
244 sal_Int32 dims
= arr
->GetDims();
245 std::vector
< sal_Int32
> low(dims
);
246 std::vector
< sal_Int32
> up(dims
);
247 for (sal_Int32 i
= 0; i
< dims
; ++i
) {
248 arr
->GetDim(i
+ 1, low
[i
], up
[i
]);
250 for (std::vector
< sal_Int32
> idx
= low
;;) {
251 ErrCode e
= marshal(false, arr
->Get(&idx
[0]), false, blob
, offset
, data
);
252 if (e
!= ERRCODE_NONE
) {
255 sal_Int32 i
= dims
- 1;
256 while (idx
[i
] == up
[i
]) {
267 // 8-aligned structs are only 4-aligned on stack, so alignment of members in
268 // such structs must take that into account via "offset"
270 bool outer
, SbxVariable
* variable
, bool special
,
271 std::vector
< char > & blob
, std::size_t offset
, MarshalData
& data
)
273 assert(variable
!= 0);
275 SbxDataType eVarType
= variable
->GetType();
276 bool bByVal
= !(variable
->GetFlags() & SbxFlagBits::Reference
);
277 if( !bByVal
&& !SbiRuntime::isVBAEnabled() && eVarType
== SbxSTRING
)
281 if ((eVarType
& SbxARRAY
) == 0) {
284 add(blob
, variable
->GetInteger(), outer
? 4 : 2, offset
);
287 add(blob
, variable
->GetLong(), 4, offset
);
290 add(blob
, variable
->GetSingle(), 4, offset
);
293 add(blob
, variable
->GetDouble(), outer
? 4 : 8, offset
);
298 ErrCode e
= marshalString(variable
, special
, data
, &p
);
299 if (e
!= ERRCODE_NONE
) {
302 add(blob
, p
, 4, offset
);
307 align(blob
, outer
? 4 : alignment(variable
), offset
, 0);
308 ErrCode e
= marshalStruct(variable
, blob
, offset
, data
);
309 if (e
!= ERRCODE_NONE
) {
315 add(blob
, variable
->GetBool(), outer
? 4 : 1, offset
);
318 add(blob
, variable
->GetByte(), outer
? 4 : 1, offset
);
325 ErrCode e
= marshalArray(variable
, blob
, offset
, data
);
326 if (e
!= ERRCODE_NONE
) {
331 if ((eVarType
& SbxARRAY
) == 0) {
339 add(blob
, variable
->data(), 4, offset
);
344 ErrCode e
= marshalString(variable
, special
, data
, &p
);
345 if (e
!= ERRCODE_NONE
) {
348 std::vector
< char > * blob2
= data
.newBlob();
349 add(*blob2
, p
, 4, 0);
350 add(blob
, address(*blob2
), 4, offset
);
355 std::vector
< char > * blob2
= data
.newBlob();
356 ErrCode e
= marshalStruct(variable
, *blob2
, 0, data
);
357 if (e
!= ERRCODE_NONE
) {
360 void * p
= address(*blob2
);
362 data
.unmarshal
.push_back(UnmarshalData(variable
, p
));
364 add(blob
, p
, 4, offset
);
372 std::vector
< char > * blob2
= data
.newBlob();
373 ErrCode e
= marshalArray(variable
, *blob2
, 0, data
);
374 if (e
!= ERRCODE_NONE
) {
377 void * p
= address(*blob2
);
379 data
.unmarshal
.push_back(UnmarshalData(variable
, p
));
381 add(blob
, p
, 4, offset
);
387 template< typename T
> T
read(void const ** pointer
) {
388 T
const * p
= static_cast< T
const * >(*pointer
);
389 *pointer
= static_cast< void const * >(p
+ 1);
393 void const * unmarshal(SbxVariable
* variable
, void const * data
) {
394 assert(variable
!= 0);
395 if ((variable
->GetType() & SbxARRAY
) == 0) {
396 switch (variable
->GetType()) {
398 variable
->PutInteger(read
< sal_Int16
>(&data
));
401 variable
->PutLong(read
< sal_Int32
>(&data
));
404 variable
->PutSingle(read
< float >(&data
));
407 variable
->PutDouble(read
< double >(&data
));
410 read
< char * >(&data
); // handled by unmarshalString
414 data
= reinterpret_cast< void const * >(
416 reinterpret_cast< sal_uIntPtr
>(data
),
417 alignment(variable
)));
418 SbxObject
* pobj
= dynamic_cast<SbxObject
*>(variable
->GetObject());
420 SbxArray
* props
= pobj
->GetProperties();
421 for (sal_uInt32 i
= 0; i
< props
->Count(); ++i
)
423 data
= unmarshal(props
->Get(i
), data
);
428 variable
->PutBool(read
< sal_Bool
>(&data
));
431 variable
->PutByte(read
< sal_uInt8
>(&data
));
438 SbxDimArray
* arr
= dynamic_cast<SbxDimArray
*>( variable
->GetObject() );
440 sal_Int32 dims
= arr
->GetDims();
441 std::vector
< sal_Int32
> low(dims
);
442 std::vector
< sal_Int32
> up(dims
);
443 for (sal_Int32 i
= 0; i
< dims
; ++i
) {
444 arr
->GetDim(i
+ 1, low
[i
], up
[i
]);
446 for (std::vector
< sal_Int32
> idx
= low
;;) {
447 data
= unmarshal(arr
->Get(&idx
[0]), data
);
448 sal_Int32 i
= dims
- 1;
449 while (idx
[i
] == up
[i
]) {
463 ErrCode
unmarshalString(StringData
const & data
, SbxVariable
& result
) {
465 if (data
.buffer
!= 0) {
466 char const * p
= static_cast< char const * >(data
.buffer
);
469 len
= static_cast< sal_Int32
>(result
.GetULong());
470 if (len
< 0) { // i.e., DWORD result >= 2^31
471 return ERRCODE_BASIC_BAD_ARGUMENT
;
472 //TODO: more specific errcode?
475 len
= rtl_str_getLength(p
);
477 ErrCode e
= convert(p
, len
, &str
);
478 if (e
!= ERRCODE_NONE
) {
482 data
.variable
->PutString(str
);
492 OUString
const & dll
, ProcData
const & proc
, SbxArray
* arguments
,
493 SbxVariable
& result
)
495 std::vector
< char > stack
;
497 // For DWORD GetLogicalDriveStringsA(DWORD nBufferLength, LPSTR lpBuffer)
498 // from kernel32, upon return, filled lpBuffer length is result DWORD, which
499 // requires special handling in unmarshalString; other functions might
500 // require similar treatment, too:
501 bool special
= dll
.equalsIgnoreAsciiCase("KERNEL32.DLL") &&
502 (proc
.name
== OString("GetLogicalDriveStringsA"));
503 for (sal_uInt32 i
= 1; i
< (arguments
== 0 ? 0 : arguments
->Count()); ++i
)
505 ErrCode e
= marshal(true, arguments
->Get(i
), special
&& i
== 2, stack
, stack
.size(),
507 if (e
!= ERRCODE_NONE
) {
510 align(stack
, 4, 0, 0);
512 switch (result
.GetType()) {
514 DllMgr_call32(proc
.proc
, address(stack
), stack
.size());
518 static_cast< sal_Int16
>(
519 DllMgr_call32(proc
.proc
, address(stack
), stack
.size())));
523 static_cast< sal_Int32
>(
524 DllMgr_call32(proc
.proc
, address(stack
), stack
.size())));
528 static_cast< float >(
529 DllMgr_callFp(proc
.proc
, address(stack
), stack
.size())));
533 DllMgr_callFp(proc
.proc
, address(stack
), stack
.size()));
537 char const * s1
= reinterpret_cast< char const * >(
538 DllMgr_call32(proc
.proc
, address(stack
), stack
.size()));
540 ErrCode e
= convert(s1
, rtl_str_getLength(s1
), &s2
);
541 if (e
!= ERRCODE_NONE
) {
544 result
.PutString(s2
);
549 DllMgr_call32(proc
.proc
, address(stack
), stack
.size());
553 bool(DllMgr_call32(proc
.proc
, address(stack
), stack
.size())));
557 static_cast< sal_uInt8
>(
558 DllMgr_call32(proc
.proc
, address(stack
), stack
.size())));
564 for (sal_uInt32 i
= 1; i
< (arguments
== 0 ? 0 : arguments
->Count()); ++i
)
566 arguments
->Get(i
)->ResetFlag(SbxFlagBits::Reference
);
567 //TODO: skipped for errors?!?
569 for (auto& rUnmarshalData
: data
.unmarshal
)
571 unmarshal(rUnmarshalData
.variable
, rUnmarshalData
.buffer
);
573 for (const auto& rStringData
: data
.unmarshalStrings
)
575 ErrCode e
= unmarshalString(rStringData
, result
);
576 if (e
!= ERRCODE_NONE
) {
583 ErrCode
getProcData(HMODULE handle
, OUString
const & name
, ProcData
* proc
)
586 if ( !name
.isEmpty() && name
[0] == '@' ) { //TODO: "@" vs. "#"???
587 sal_Int32 n
= name
.copy(1).toInt32(); //TODO: handle bad input
588 if (n
<= 0 || n
> 0xFFFF) {
589 return ERRCODE_BASIC_BAD_ARGUMENT
; //TODO: more specific errcode?
591 FARPROC p
= GetProcAddress(handle
, reinterpret_cast< LPCSTR
>(n
));
593 proc
->name
= OString("#") + OString::number(n
);
599 ErrCode e
= convert(name
, &name8
);
600 if (e
!= ERRCODE_NONE
) {
603 FARPROC p
= GetProcAddress(handle
, name8
.getStr());
609 sal_Int32 i
= name8
.indexOf('#');
611 name8
= name8
.copy(0, i
);
612 p
= GetProcAddress(handle
, name8
.getStr());
619 OString
real(OString("_") + name8
);
620 p
= GetProcAddress(handle
, real
.getStr());
626 real
= name8
+ OString("A");
627 p
= GetProcAddress(handle
, real
.getStr());
634 return ERRCODE_BASIC_PROC_UNDEFINED
;
637 struct Dll
: public salhelper::SimpleReferenceObject
{
639 typedef std::map
< OUString
, ProcData
> Procs
;
646 ErrCode
getProc(OUString
const & name
, ProcData
* proc
);
653 if (handle
!= 0 && !FreeLibrary(handle
)) {
654 SAL_WARN("basic", "FreeLibrary(" << handle
<< ") failed with " << GetLastError());
658 ErrCode
Dll::getProc(OUString
const & name
, ProcData
* proc
) {
659 Procs::iterator
i(procs
.find(name
));
660 if (i
!= procs
.end()) {
664 ErrCode e
= getProcData(handle
, name
, proc
);
665 if (e
== ERRCODE_NONE
) {
666 procs
.emplace(name
, *proc
);
671 OUString
fullDllName(OUString
const & name
) {
673 if (full
.indexOf('.') == -1) {
681 struct SbiDllMgr::Impl
{
683 typedef std::map
< OUString
, rtl::Reference
< Dll
> > Dlls
;
687 Impl(const Impl
&) = delete;
688 const Impl
& operator=(const Impl
&) = delete;
690 Dll
* getDll(OUString
const & name
);
695 Dll
* SbiDllMgr::Impl::getDll(OUString
const & name
) {
696 Dlls::iterator
i(dlls
.find(name
));
697 if (i
== dlls
.end()) {
698 i
= dlls
.emplace(name
, new Dll
).first
;
699 HMODULE h
= LoadLibraryW(o3tl::toW(name
.getStr()));
704 i
->second
->handle
= h
;
706 return i
->second
.get();
709 ErrCode
SbiDllMgr::Call(
710 std::u16string_view function
, std::u16string_view library
,
711 SbxArray
* arguments
, SbxVariable
& result
, bool cdeclConvention
)
713 if (cdeclConvention
) {
714 return ERRCODE_BASIC_NOT_IMPLEMENTED
;
716 OUString
dllName(fullDllName(OUString(library
)));
717 Dll
* dll
= impl_
->getDll(dllName
);
719 return ERRCODE_BASIC_BAD_DLL_LOAD
;
722 ErrCode e
= dll
->getProc(OUString(function
), &proc
);
723 if (e
!= ERRCODE_NONE
) {
726 return call(dllName
, proc
, arguments
, result
);
729 void SbiDllMgr::FreeDll(OUString
const & library
) {
730 impl_
->dlls
.erase(library
);
733 SbiDllMgr::SbiDllMgr(): impl_(new Impl
) {}
735 SbiDllMgr::~SbiDllMgr() {}
737 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */