xref: /AOO41X/main/basic/source/runtime/dllmgr.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include "precompiled_basic.hxx"
29*cdf0e10cSrcweir #include "sal/config.h"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <algorithm>
32*cdf0e10cSrcweir #include <cstddef>
33*cdf0e10cSrcweir #include <list>
34*cdf0e10cSrcweir #include <map>
35*cdf0e10cSrcweir #include <vector>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include "basic/sbx.hxx"
38*cdf0e10cSrcweir #include "basic/sbxvar.hxx"
39*cdf0e10cSrcweir #include "runtime.hxx"
40*cdf0e10cSrcweir #include "osl/thread.h"
41*cdf0e10cSrcweir #include "rtl/ref.hxx"
42*cdf0e10cSrcweir #include "rtl/string.hxx"
43*cdf0e10cSrcweir #include "rtl/ustring.hxx"
44*cdf0e10cSrcweir #include "salhelper/simplereferenceobject.hxx"
45*cdf0e10cSrcweir #include "tools/svwin.h"
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #undef max
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #include "dllmgr.hxx"
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir /* Open issues:
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir    Only 32-bit Windows for now.
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir    Missing support for functions returning structs (see TODO in call()).
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir    Missing support for additional data types (64 bit integers, Any, ...; would
58*cdf0e10cSrcweir    trigger OSL_ASSERT(false) in various switches).
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir    It is assumed that the variables passed into SbiDllMgr::Call to represent
61*cdf0e10cSrcweir    the arguments and return value have types that exactly match the Declare
62*cdf0e10cSrcweir    statement; it would be better if this code had access to the function
63*cdf0e10cSrcweir    signature from the Declare statement, so that it could convert the passed
64*cdf0e10cSrcweir    variables accordingly.
65*cdf0e10cSrcweir */
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir #if defined WNT // only 32-bit Windows, actually
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir extern "C" {
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir int __stdcall DllMgr_call32(FARPROC, void const * stack, std::size_t size);
72*cdf0e10cSrcweir double __stdcall DllMgr_callFp(FARPROC, void const * stack, std::size_t size);
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir namespace {
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir char * address(std::vector< char > & blob) {
79*cdf0e10cSrcweir     return blob.empty() ? 0 : &blob[0];
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir SbError convert(rtl::OUString const & source, rtl::OString * target) {
83*cdf0e10cSrcweir     return
84*cdf0e10cSrcweir         source.convertToString(
85*cdf0e10cSrcweir             target, osl_getThreadTextEncoding(),
86*cdf0e10cSrcweir             (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
87*cdf0e10cSrcweir              RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))
88*cdf0e10cSrcweir         ? ERRCODE_NONE : ERRCODE_BASIC_BAD_ARGUMENT;
89*cdf0e10cSrcweir         //TODO: more specific errcode?
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir SbError convert(char const * source, sal_Int32 length, rtl::OUString * target) {
93*cdf0e10cSrcweir     return
94*cdf0e10cSrcweir         rtl_convertStringToUString(
95*cdf0e10cSrcweir             &target->pData, source, length, osl_getThreadTextEncoding(),
96*cdf0e10cSrcweir             (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
97*cdf0e10cSrcweir              RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
98*cdf0e10cSrcweir              RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))
99*cdf0e10cSrcweir         ? ERRCODE_NONE : ERRCODE_BASIC_BAD_ARGUMENT;
100*cdf0e10cSrcweir         //TODO: more specific errcode?
101*cdf0e10cSrcweir }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir struct UnmarshalData {
104*cdf0e10cSrcweir     UnmarshalData(SbxVariable * theVariable, void * theBuffer):
105*cdf0e10cSrcweir         variable(theVariable), buffer(theBuffer) {}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     SbxVariable * variable;
108*cdf0e10cSrcweir     void * buffer;
109*cdf0e10cSrcweir };
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir struct StringData: public UnmarshalData {
112*cdf0e10cSrcweir     StringData(SbxVariable * theVariable, void * theBuffer, bool theSpecial):
113*cdf0e10cSrcweir         UnmarshalData(theVariable, theBuffer), special(theSpecial) {}
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     bool special;
116*cdf0e10cSrcweir };
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir class MarshalData: private boost::noncopyable {
119*cdf0e10cSrcweir public:
120*cdf0e10cSrcweir     std::vector< char > * newBlob() {
121*cdf0e10cSrcweir         blobs_.push_front(std::vector< char >());
122*cdf0e10cSrcweir         return &blobs_.front();
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     std::vector< UnmarshalData > unmarshal;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     std::vector< StringData > unmarshalStrings;
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir private:
130*cdf0e10cSrcweir     std::list< std::vector< char > > blobs_;
131*cdf0e10cSrcweir };
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir std::size_t align(std::size_t address, std::size_t alignment) {
134*cdf0e10cSrcweir     // alignment = 2^k for some k >= 0
135*cdf0e10cSrcweir     return (address + (alignment - 1)) & ~(alignment - 1);
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir char * align(
139*cdf0e10cSrcweir     std::vector< char > & blob, std::size_t alignment, std::size_t offset,
140*cdf0e10cSrcweir     std::size_t add)
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir     std::vector< char >::size_type n = blob.size();
143*cdf0e10cSrcweir     n = align(n - offset, alignment) + offset; //TODO: overflow in align()
144*cdf0e10cSrcweir     blob.resize(n + add); //TODO: overflow
145*cdf0e10cSrcweir     return address(blob) + n;
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir template< typename T > void add(
149*cdf0e10cSrcweir     std::vector< char > & blob, T const & data, std::size_t alignment,
150*cdf0e10cSrcweir     std::size_t offset)
151*cdf0e10cSrcweir {
152*cdf0e10cSrcweir     *reinterpret_cast< T * >(align(blob, alignment, offset, sizeof (T))) = data;
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir std::size_t alignment(SbxVariable * variable) {
156*cdf0e10cSrcweir     OSL_ASSERT(variable != 0);
157*cdf0e10cSrcweir     if ((variable->GetType() & SbxARRAY) == 0) {
158*cdf0e10cSrcweir         switch (variable->GetType()) {
159*cdf0e10cSrcweir         case SbxINTEGER:
160*cdf0e10cSrcweir             return 2;
161*cdf0e10cSrcweir         case SbxLONG:
162*cdf0e10cSrcweir         case SbxSINGLE:
163*cdf0e10cSrcweir         case SbxSTRING:
164*cdf0e10cSrcweir             return 4;
165*cdf0e10cSrcweir         case SbxDOUBLE:
166*cdf0e10cSrcweir             return 8;
167*cdf0e10cSrcweir         case SbxOBJECT:
168*cdf0e10cSrcweir             {
169*cdf0e10cSrcweir                 std::size_t n = 1;
170*cdf0e10cSrcweir                 SbxArray * props = PTR_CAST(SbxObject, variable->GetObject())->
171*cdf0e10cSrcweir                     GetProperties();
172*cdf0e10cSrcweir                 for (sal_uInt16 i = 0; i < props->Count(); ++i) {
173*cdf0e10cSrcweir                     n = std::max(n, alignment(props->Get(i)));
174*cdf0e10cSrcweir                 }
175*cdf0e10cSrcweir                 return n;
176*cdf0e10cSrcweir             }
177*cdf0e10cSrcweir         case SbxBOOL:
178*cdf0e10cSrcweir         case SbxBYTE:
179*cdf0e10cSrcweir             return 1;
180*cdf0e10cSrcweir         default:
181*cdf0e10cSrcweir             OSL_ASSERT(false);
182*cdf0e10cSrcweir             return 1;
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir     } else {
185*cdf0e10cSrcweir         SbxDimArray * arr = PTR_CAST(SbxDimArray, variable->GetObject());
186*cdf0e10cSrcweir         int dims = arr->GetDims();
187*cdf0e10cSrcweir         std::vector< sal_Int32 > low(dims);
188*cdf0e10cSrcweir         for (int i = 0; i < dims; ++i) {
189*cdf0e10cSrcweir             sal_Int32 up;
190*cdf0e10cSrcweir             arr->GetDim32(i + 1, low[i], up);
191*cdf0e10cSrcweir         }
192*cdf0e10cSrcweir         return alignment(arr->Get32(&low[0]));
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir }
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir SbError marshal(
197*cdf0e10cSrcweir     bool outer, SbxVariable * variable, bool special,
198*cdf0e10cSrcweir     std::vector< char > & blob, std::size_t offset, MarshalData & data);
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir SbError marshalString(
201*cdf0e10cSrcweir     SbxVariable * variable, bool special, MarshalData & data, void ** buffer)
202*cdf0e10cSrcweir {
203*cdf0e10cSrcweir     OSL_ASSERT(variable != 0 && buffer != 0);
204*cdf0e10cSrcweir     rtl::OString str;
205*cdf0e10cSrcweir     SbError e = convert(variable->GetString(), &str);
206*cdf0e10cSrcweir     if (e != ERRCODE_NONE) {
207*cdf0e10cSrcweir         return e;
208*cdf0e10cSrcweir     }
209*cdf0e10cSrcweir     std::vector< char > * blob = data.newBlob();
210*cdf0e10cSrcweir     blob->insert(
211*cdf0e10cSrcweir         blob->begin(), str.getStr(), str.getStr() + str.getLength() + 1);
212*cdf0e10cSrcweir     *buffer = address(*blob);
213*cdf0e10cSrcweir     data.unmarshalStrings.push_back(StringData(variable, *buffer, special));
214*cdf0e10cSrcweir     return ERRCODE_NONE;
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir SbError marshalStruct(
218*cdf0e10cSrcweir     SbxVariable * variable, std::vector< char > & blob, std::size_t offset,
219*cdf0e10cSrcweir     MarshalData & data)
220*cdf0e10cSrcweir {
221*cdf0e10cSrcweir     OSL_ASSERT(variable != 0);
222*cdf0e10cSrcweir     SbxArray * props = PTR_CAST(SbxObject, variable->GetObject())->
223*cdf0e10cSrcweir         GetProperties();
224*cdf0e10cSrcweir     for (sal_uInt16 i = 0; i < props->Count(); ++i) {
225*cdf0e10cSrcweir         SbError e = marshal(false, props->Get(i), false, blob, offset, data);
226*cdf0e10cSrcweir         if (e != ERRCODE_NONE) {
227*cdf0e10cSrcweir             return e;
228*cdf0e10cSrcweir         }
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir     return ERRCODE_NONE;
231*cdf0e10cSrcweir }
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir SbError marshalArray(
234*cdf0e10cSrcweir     SbxVariable * variable, std::vector< char > & blob, std::size_t offset,
235*cdf0e10cSrcweir     MarshalData & data)
236*cdf0e10cSrcweir {
237*cdf0e10cSrcweir     OSL_ASSERT(variable != 0);
238*cdf0e10cSrcweir     SbxDimArray * arr = PTR_CAST(SbxDimArray, variable->GetObject());
239*cdf0e10cSrcweir     int dims = arr->GetDims();
240*cdf0e10cSrcweir     std::vector< sal_Int32 > low(dims);
241*cdf0e10cSrcweir     std::vector< sal_Int32 > up(dims);
242*cdf0e10cSrcweir     for (int i = 0; i < dims; ++i) {
243*cdf0e10cSrcweir         arr->GetDim32(i + 1, low[i], up[i]);
244*cdf0e10cSrcweir     }
245*cdf0e10cSrcweir     for (std::vector< sal_Int32 > idx = low;;) {
246*cdf0e10cSrcweir         SbError e = marshal(
247*cdf0e10cSrcweir             false, arr->Get32(&idx[0]), false, blob, offset, data);
248*cdf0e10cSrcweir         if (e != ERRCODE_NONE) {
249*cdf0e10cSrcweir             return e;
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir         int i = dims - 1;
252*cdf0e10cSrcweir         while (idx[i] == up[i]) {
253*cdf0e10cSrcweir             idx[i] = low[i];
254*cdf0e10cSrcweir             if (i == 0) {
255*cdf0e10cSrcweir                 return ERRCODE_NONE;
256*cdf0e10cSrcweir             }
257*cdf0e10cSrcweir             --i;
258*cdf0e10cSrcweir         }
259*cdf0e10cSrcweir         ++idx[i];
260*cdf0e10cSrcweir     }
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir // 8-aligned structs are only 4-aligned on stack, so alignment of members in
264*cdf0e10cSrcweir // such structs must take that into account via "offset"
265*cdf0e10cSrcweir SbError marshal(
266*cdf0e10cSrcweir     bool outer, SbxVariable * variable, bool special,
267*cdf0e10cSrcweir     std::vector< char > & blob, std::size_t offset, MarshalData & data)
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir     OSL_ASSERT(variable != 0);
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir     SbxDataType eVarType = variable->GetType();
272*cdf0e10cSrcweir     bool bByVal = (variable->GetFlags() & SBX_REFERENCE) == 0;
273*cdf0e10cSrcweir     if( !bByVal && !SbiRuntime::isVBAEnabled() && eVarType == SbxSTRING )
274*cdf0e10cSrcweir         bByVal = true;
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir     if (bByVal) {
277*cdf0e10cSrcweir         if ((eVarType & SbxARRAY) == 0) {
278*cdf0e10cSrcweir             switch (eVarType) {
279*cdf0e10cSrcweir             case SbxINTEGER:
280*cdf0e10cSrcweir                 add(blob, variable->GetInteger(), outer ? 4 : 2, offset);
281*cdf0e10cSrcweir                 break;
282*cdf0e10cSrcweir             case SbxLONG:
283*cdf0e10cSrcweir                 add(blob, variable->GetLong(), 4, offset);
284*cdf0e10cSrcweir                 break;
285*cdf0e10cSrcweir             case SbxSINGLE:
286*cdf0e10cSrcweir                 add(blob, variable->GetSingle(), 4, offset);
287*cdf0e10cSrcweir                 break;
288*cdf0e10cSrcweir             case SbxDOUBLE:
289*cdf0e10cSrcweir                 add(blob, variable->GetDouble(), outer ? 4 : 8, offset);
290*cdf0e10cSrcweir                 break;
291*cdf0e10cSrcweir             case SbxSTRING:
292*cdf0e10cSrcweir                 {
293*cdf0e10cSrcweir                     void * p;
294*cdf0e10cSrcweir                     SbError e = marshalString(variable, special, data, &p);
295*cdf0e10cSrcweir                     if (e != ERRCODE_NONE) {
296*cdf0e10cSrcweir                         return e;
297*cdf0e10cSrcweir                     }
298*cdf0e10cSrcweir                     add(blob, p, 4, offset);
299*cdf0e10cSrcweir                     break;
300*cdf0e10cSrcweir                 }
301*cdf0e10cSrcweir             case SbxOBJECT:
302*cdf0e10cSrcweir                 {
303*cdf0e10cSrcweir                     align(blob, outer ? 4 : alignment(variable), offset, 0);
304*cdf0e10cSrcweir                     SbError e = marshalStruct(variable, blob, offset, data);
305*cdf0e10cSrcweir                     if (e != ERRCODE_NONE) {
306*cdf0e10cSrcweir                         return e;
307*cdf0e10cSrcweir                     }
308*cdf0e10cSrcweir                     break;
309*cdf0e10cSrcweir                 }
310*cdf0e10cSrcweir             case SbxBOOL:
311*cdf0e10cSrcweir                 add(blob, variable->GetBool(), outer ? 4 : 1, offset);
312*cdf0e10cSrcweir                 break;
313*cdf0e10cSrcweir             case SbxBYTE:
314*cdf0e10cSrcweir                 add(blob, variable->GetByte(), outer ? 4 : 1, offset);
315*cdf0e10cSrcweir                 break;
316*cdf0e10cSrcweir             default:
317*cdf0e10cSrcweir                 OSL_ASSERT(false);
318*cdf0e10cSrcweir                 break;
319*cdf0e10cSrcweir             }
320*cdf0e10cSrcweir         } else {
321*cdf0e10cSrcweir             SbError e = marshalArray(variable, blob, offset, data);
322*cdf0e10cSrcweir             if (e != ERRCODE_NONE) {
323*cdf0e10cSrcweir                 return e;
324*cdf0e10cSrcweir             }
325*cdf0e10cSrcweir         }
326*cdf0e10cSrcweir     } else {
327*cdf0e10cSrcweir         if ((eVarType & SbxARRAY) == 0) {
328*cdf0e10cSrcweir             switch (eVarType) {
329*cdf0e10cSrcweir             case SbxINTEGER:
330*cdf0e10cSrcweir             case SbxLONG:
331*cdf0e10cSrcweir             case SbxSINGLE:
332*cdf0e10cSrcweir             case SbxDOUBLE:
333*cdf0e10cSrcweir             case SbxBOOL:
334*cdf0e10cSrcweir             case SbxBYTE:
335*cdf0e10cSrcweir                 add(blob, variable->data(), 4, offset);
336*cdf0e10cSrcweir                 break;
337*cdf0e10cSrcweir             case SbxSTRING:
338*cdf0e10cSrcweir                 {
339*cdf0e10cSrcweir                     std::vector< char > * blob2 = data.newBlob();
340*cdf0e10cSrcweir                     void * p;
341*cdf0e10cSrcweir                     SbError e = marshalString(variable, special, data, &p);
342*cdf0e10cSrcweir                     if (e != ERRCODE_NONE) {
343*cdf0e10cSrcweir                         return e;
344*cdf0e10cSrcweir                     }
345*cdf0e10cSrcweir                     add(*blob2, p, 4, 0);
346*cdf0e10cSrcweir                     add(blob, address(*blob2), 4, offset);
347*cdf0e10cSrcweir                     break;
348*cdf0e10cSrcweir                 }
349*cdf0e10cSrcweir             case SbxOBJECT:
350*cdf0e10cSrcweir                 {
351*cdf0e10cSrcweir                     std::vector< char > * blob2 = data.newBlob();
352*cdf0e10cSrcweir                     SbError e = marshalStruct(variable, *blob2, 0, data);
353*cdf0e10cSrcweir                     if (e != ERRCODE_NONE) {
354*cdf0e10cSrcweir                         return e;
355*cdf0e10cSrcweir                     }
356*cdf0e10cSrcweir                     void * p = address(*blob2);
357*cdf0e10cSrcweir                     if (outer) {
358*cdf0e10cSrcweir                         data.unmarshal.push_back(UnmarshalData(variable, p));
359*cdf0e10cSrcweir                     }
360*cdf0e10cSrcweir                     add(blob, p, 4, offset);
361*cdf0e10cSrcweir                     break;
362*cdf0e10cSrcweir                 }
363*cdf0e10cSrcweir             default:
364*cdf0e10cSrcweir                 OSL_ASSERT(false);
365*cdf0e10cSrcweir                 break;
366*cdf0e10cSrcweir             }
367*cdf0e10cSrcweir         } else {
368*cdf0e10cSrcweir             std::vector< char > * blob2 = data.newBlob();
369*cdf0e10cSrcweir             SbError e = marshalArray(variable, *blob2, 0, data);
370*cdf0e10cSrcweir             if (e != ERRCODE_NONE) {
371*cdf0e10cSrcweir                 return e;
372*cdf0e10cSrcweir             }
373*cdf0e10cSrcweir             void * p = address(*blob2);
374*cdf0e10cSrcweir             if (outer) {
375*cdf0e10cSrcweir                 data.unmarshal.push_back(UnmarshalData(variable, p));
376*cdf0e10cSrcweir             }
377*cdf0e10cSrcweir             add(blob, p, 4, offset);
378*cdf0e10cSrcweir         }
379*cdf0e10cSrcweir     }
380*cdf0e10cSrcweir     return ERRCODE_NONE;
381*cdf0e10cSrcweir }
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir template< typename T > T read(void const ** pointer) {
384*cdf0e10cSrcweir     T const * p = static_cast< T const * >(*pointer);
385*cdf0e10cSrcweir     *pointer = static_cast< void const * >(p + 1);
386*cdf0e10cSrcweir     return *p;
387*cdf0e10cSrcweir }
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir void const * unmarshal(SbxVariable * variable, void const * data) {
390*cdf0e10cSrcweir     OSL_ASSERT(variable != 0);
391*cdf0e10cSrcweir     if ((variable->GetType() & SbxARRAY) == 0) {
392*cdf0e10cSrcweir         switch (variable->GetType()) {
393*cdf0e10cSrcweir         case SbxINTEGER:
394*cdf0e10cSrcweir             variable->PutInteger(read< sal_Int16 >(&data));
395*cdf0e10cSrcweir             break;
396*cdf0e10cSrcweir         case SbxLONG:
397*cdf0e10cSrcweir             variable->PutLong(read< sal_Int32 >(&data));
398*cdf0e10cSrcweir             break;
399*cdf0e10cSrcweir         case SbxSINGLE:
400*cdf0e10cSrcweir             variable->PutSingle(read< float >(&data));
401*cdf0e10cSrcweir             break;
402*cdf0e10cSrcweir         case SbxDOUBLE:
403*cdf0e10cSrcweir             variable->PutDouble(read< double >(&data));
404*cdf0e10cSrcweir             break;
405*cdf0e10cSrcweir         case SbxSTRING:
406*cdf0e10cSrcweir             read< char * >(&data); // handled by unmarshalString
407*cdf0e10cSrcweir             break;
408*cdf0e10cSrcweir         case SbxOBJECT:
409*cdf0e10cSrcweir             {
410*cdf0e10cSrcweir                 data = reinterpret_cast< void const * >(
411*cdf0e10cSrcweir                     align(
412*cdf0e10cSrcweir                         reinterpret_cast< sal_uIntPtr >(data),
413*cdf0e10cSrcweir                         alignment(variable)));
414*cdf0e10cSrcweir                 SbxArray * props = PTR_CAST(SbxObject, variable->GetObject())->
415*cdf0e10cSrcweir                     GetProperties();
416*cdf0e10cSrcweir                 for (sal_uInt16 i = 0; i < props->Count(); ++i) {
417*cdf0e10cSrcweir                     data = unmarshal(props->Get(i), data);
418*cdf0e10cSrcweir                 }
419*cdf0e10cSrcweir                 break;
420*cdf0e10cSrcweir             }
421*cdf0e10cSrcweir         case SbxBOOL:
422*cdf0e10cSrcweir             variable->PutBool(read< sal_Bool >(&data));
423*cdf0e10cSrcweir             break;
424*cdf0e10cSrcweir         case SbxBYTE:
425*cdf0e10cSrcweir             variable->PutByte(read< sal_uInt8 >(&data));
426*cdf0e10cSrcweir             break;
427*cdf0e10cSrcweir         default:
428*cdf0e10cSrcweir             OSL_ASSERT(false);
429*cdf0e10cSrcweir             break;
430*cdf0e10cSrcweir         }
431*cdf0e10cSrcweir     } else {
432*cdf0e10cSrcweir         SbxDimArray * arr = PTR_CAST(SbxDimArray, variable->GetObject());
433*cdf0e10cSrcweir         int dims = arr->GetDims();
434*cdf0e10cSrcweir         std::vector< sal_Int32 > low(dims);
435*cdf0e10cSrcweir         std::vector< sal_Int32 > up(dims);
436*cdf0e10cSrcweir         for (int i = 0; i < dims; ++i) {
437*cdf0e10cSrcweir             arr->GetDim32(i + 1, low[i], up[i]);
438*cdf0e10cSrcweir         }
439*cdf0e10cSrcweir         for (std::vector< sal_Int32 > idx = low;;) {
440*cdf0e10cSrcweir             data = unmarshal(arr->Get32(&idx[0]), data);
441*cdf0e10cSrcweir             int i = dims - 1;
442*cdf0e10cSrcweir             while (idx[i] == up[i]) {
443*cdf0e10cSrcweir                 idx[i] = low[i];
444*cdf0e10cSrcweir                 if (i == 0) {
445*cdf0e10cSrcweir                     goto done;
446*cdf0e10cSrcweir                 }
447*cdf0e10cSrcweir                 --i;
448*cdf0e10cSrcweir             }
449*cdf0e10cSrcweir             ++idx[i];
450*cdf0e10cSrcweir         }
451*cdf0e10cSrcweir     done:;
452*cdf0e10cSrcweir     }
453*cdf0e10cSrcweir     return data;
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir SbError unmarshalString(StringData const & data, SbxVariable & result) {
457*cdf0e10cSrcweir     rtl::OUString str;
458*cdf0e10cSrcweir     if (data.buffer != 0) {
459*cdf0e10cSrcweir         char const * p = static_cast< char const * >(data.buffer);
460*cdf0e10cSrcweir         sal_Int32 len;
461*cdf0e10cSrcweir         if (data.special) {
462*cdf0e10cSrcweir             len = static_cast< sal_Int32 >(result.GetULong());
463*cdf0e10cSrcweir             if (len < 0) { // i.e., DWORD result >= 2^31
464*cdf0e10cSrcweir                 return ERRCODE_BASIC_BAD_ARGUMENT;
465*cdf0e10cSrcweir                     //TODO: more specific errcode?
466*cdf0e10cSrcweir             }
467*cdf0e10cSrcweir         } else {
468*cdf0e10cSrcweir             len = rtl_str_getLength(p);
469*cdf0e10cSrcweir         }
470*cdf0e10cSrcweir         SbError e = convert(p, len, &str);
471*cdf0e10cSrcweir         if (e != ERRCODE_NONE) {
472*cdf0e10cSrcweir             return e;
473*cdf0e10cSrcweir         }
474*cdf0e10cSrcweir     }
475*cdf0e10cSrcweir     data.variable->PutString(String(str));
476*cdf0e10cSrcweir     return ERRCODE_NONE;
477*cdf0e10cSrcweir }
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir struct ProcData {
480*cdf0e10cSrcweir     rtl::OString name;
481*cdf0e10cSrcweir     FARPROC proc;
482*cdf0e10cSrcweir };
483*cdf0e10cSrcweir 
484*cdf0e10cSrcweir SbError call(
485*cdf0e10cSrcweir     rtl::OUString const & dll, ProcData const & proc, SbxArray * arguments,
486*cdf0e10cSrcweir     SbxVariable & result)
487*cdf0e10cSrcweir {
488*cdf0e10cSrcweir     std::vector< char > stack;
489*cdf0e10cSrcweir     MarshalData data;
490*cdf0e10cSrcweir     // For DWORD GetLogicalDriveStringsA(DWORD nBufferLength, LPSTR lpBuffer)
491*cdf0e10cSrcweir     // from kernel32, upon return, filled lpBuffer length is result DWORD, which
492*cdf0e10cSrcweir     // requires special handling in unmarshalString; other functions might
493*cdf0e10cSrcweir     // require similar treatment, too:
494*cdf0e10cSrcweir     bool special =
495*cdf0e10cSrcweir         dll.equalsIgnoreAsciiCaseAsciiL(
496*cdf0e10cSrcweir             RTL_CONSTASCII_STRINGPARAM("KERNEL32.DLL")) &&
497*cdf0e10cSrcweir         (proc.name ==
498*cdf0e10cSrcweir          rtl::OString(RTL_CONSTASCII_STRINGPARAM("GetLogicalDriveStringsA")));
499*cdf0e10cSrcweir     for (sal_uInt16 i = 1; i < (arguments == 0 ? 0 : arguments->Count()); ++i) {
500*cdf0e10cSrcweir         SbError e = marshal(
501*cdf0e10cSrcweir             true, arguments->Get(i), special && i == 2, stack, stack.size(),
502*cdf0e10cSrcweir             data);
503*cdf0e10cSrcweir         if (e != ERRCODE_NONE) {
504*cdf0e10cSrcweir             return e;
505*cdf0e10cSrcweir         }
506*cdf0e10cSrcweir         align(stack, 4, 0, 0);
507*cdf0e10cSrcweir     }
508*cdf0e10cSrcweir     switch (result.GetType()) {
509*cdf0e10cSrcweir     case SbxEMPTY:
510*cdf0e10cSrcweir         DllMgr_call32(proc.proc, address(stack), stack.size());
511*cdf0e10cSrcweir         break;
512*cdf0e10cSrcweir     case SbxINTEGER:
513*cdf0e10cSrcweir         result.PutInteger(
514*cdf0e10cSrcweir             static_cast< sal_Int16 >(
515*cdf0e10cSrcweir                 DllMgr_call32(proc.proc, address(stack), stack.size())));
516*cdf0e10cSrcweir         break;
517*cdf0e10cSrcweir     case SbxLONG:
518*cdf0e10cSrcweir         result.PutLong(
519*cdf0e10cSrcweir             static_cast< sal_Int32 >(
520*cdf0e10cSrcweir                 DllMgr_call32(proc.proc, address(stack), stack.size())));
521*cdf0e10cSrcweir         break;
522*cdf0e10cSrcweir     case SbxSINGLE:
523*cdf0e10cSrcweir         result.PutSingle(
524*cdf0e10cSrcweir             static_cast< float >(
525*cdf0e10cSrcweir                 DllMgr_callFp(proc.proc, address(stack), stack.size())));
526*cdf0e10cSrcweir         break;
527*cdf0e10cSrcweir     case SbxDOUBLE:
528*cdf0e10cSrcweir         result.PutDouble(
529*cdf0e10cSrcweir             DllMgr_callFp(proc.proc, address(stack), stack.size()));
530*cdf0e10cSrcweir         break;
531*cdf0e10cSrcweir     case SbxSTRING:
532*cdf0e10cSrcweir         {
533*cdf0e10cSrcweir             char const * s1 = reinterpret_cast< char const * >(
534*cdf0e10cSrcweir                 DllMgr_call32(proc.proc, address(stack), stack.size()));
535*cdf0e10cSrcweir             rtl::OUString s2;
536*cdf0e10cSrcweir             SbError e = convert(s1, rtl_str_getLength(s1), &s2);
537*cdf0e10cSrcweir             if (e != ERRCODE_NONE) {
538*cdf0e10cSrcweir                 return e;
539*cdf0e10cSrcweir             }
540*cdf0e10cSrcweir             result.PutString(String(s2));
541*cdf0e10cSrcweir             break;
542*cdf0e10cSrcweir         }
543*cdf0e10cSrcweir     case SbxOBJECT:
544*cdf0e10cSrcweir         //TODO
545*cdf0e10cSrcweir         DllMgr_call32(proc.proc, address(stack), stack.size());
546*cdf0e10cSrcweir         break;
547*cdf0e10cSrcweir     case SbxBOOL:
548*cdf0e10cSrcweir         result.PutBool(
549*cdf0e10cSrcweir             static_cast< sal_Bool >(
550*cdf0e10cSrcweir                 DllMgr_call32(proc.proc, address(stack), stack.size())));
551*cdf0e10cSrcweir         break;
552*cdf0e10cSrcweir     case SbxBYTE:
553*cdf0e10cSrcweir         result.PutByte(
554*cdf0e10cSrcweir             static_cast< sal_uInt8 >(
555*cdf0e10cSrcweir                 DllMgr_call32(proc.proc, address(stack), stack.size())));
556*cdf0e10cSrcweir         break;
557*cdf0e10cSrcweir     default:
558*cdf0e10cSrcweir         OSL_ASSERT(false);
559*cdf0e10cSrcweir         break;
560*cdf0e10cSrcweir     }
561*cdf0e10cSrcweir     for (sal_uInt16 i = 1; i < (arguments == 0 ? 0 : arguments->Count()); ++i) {
562*cdf0e10cSrcweir         arguments->Get(i)->ResetFlag(SBX_REFERENCE);
563*cdf0e10cSrcweir             //TODO: skipped for errors?!?
564*cdf0e10cSrcweir     }
565*cdf0e10cSrcweir     for (std::vector< UnmarshalData >::iterator i(data.unmarshal.begin());
566*cdf0e10cSrcweir          i != data.unmarshal.end(); ++i)
567*cdf0e10cSrcweir     {
568*cdf0e10cSrcweir         unmarshal(i->variable, i->buffer);
569*cdf0e10cSrcweir     }
570*cdf0e10cSrcweir     for (std::vector< StringData >::iterator i(data.unmarshalStrings.begin());
571*cdf0e10cSrcweir          i != data.unmarshalStrings.end(); ++i)
572*cdf0e10cSrcweir     {
573*cdf0e10cSrcweir         SbError e = unmarshalString(*i, result);
574*cdf0e10cSrcweir         if (e != ERRCODE_NONE) {
575*cdf0e10cSrcweir             return e;
576*cdf0e10cSrcweir         }
577*cdf0e10cSrcweir     }
578*cdf0e10cSrcweir     return ERRCODE_NONE;
579*cdf0e10cSrcweir }
580*cdf0e10cSrcweir 
581*cdf0e10cSrcweir SbError getProcData(HMODULE handle, rtl::OUString const & name, ProcData * proc)
582*cdf0e10cSrcweir {
583*cdf0e10cSrcweir     OSL_ASSERT(proc != 0);
584*cdf0e10cSrcweir     if (name.getLength() != 0 && name[0] == '@') { //TODO: "@" vs. "#"???
585*cdf0e10cSrcweir         sal_Int32 n = name.copy(1).toInt32(); //TODO: handle bad input
586*cdf0e10cSrcweir         if (n <= 0 || n > 0xFFFF) {
587*cdf0e10cSrcweir             return ERRCODE_BASIC_BAD_ARGUMENT; //TODO: more specific errcode?
588*cdf0e10cSrcweir         }
589*cdf0e10cSrcweir         FARPROC p = GetProcAddress(handle, reinterpret_cast< LPCSTR >(n));
590*cdf0e10cSrcweir         if (p != 0) {
591*cdf0e10cSrcweir             proc->name = rtl::OString(RTL_CONSTASCII_STRINGPARAM("#")) +
592*cdf0e10cSrcweir                 rtl::OString::valueOf(n);
593*cdf0e10cSrcweir             proc->proc = p;
594*cdf0e10cSrcweir             return ERRCODE_NONE;
595*cdf0e10cSrcweir         }
596*cdf0e10cSrcweir     } else {
597*cdf0e10cSrcweir         rtl::OString name8;
598*cdf0e10cSrcweir         SbError e = convert(name, &name8);
599*cdf0e10cSrcweir         if (e != ERRCODE_NONE) {
600*cdf0e10cSrcweir             return e;
601*cdf0e10cSrcweir         }
602*cdf0e10cSrcweir         FARPROC p = GetProcAddress(handle, name8.getStr());
603*cdf0e10cSrcweir         if (p != 0) {
604*cdf0e10cSrcweir             proc->name = name8;
605*cdf0e10cSrcweir             proc->proc = p;
606*cdf0e10cSrcweir             return ERRCODE_NONE;
607*cdf0e10cSrcweir         }
608*cdf0e10cSrcweir         sal_Int32 i = name8.indexOf('#');
609*cdf0e10cSrcweir         if (i != -1) {
610*cdf0e10cSrcweir             name8 = name8.copy(0, i);
611*cdf0e10cSrcweir             p = GetProcAddress(handle, name8.getStr());
612*cdf0e10cSrcweir             if (p != 0) {
613*cdf0e10cSrcweir                 proc->name = name8;
614*cdf0e10cSrcweir                 proc->proc = p;
615*cdf0e10cSrcweir                 return ERRCODE_NONE;
616*cdf0e10cSrcweir             }
617*cdf0e10cSrcweir         }
618*cdf0e10cSrcweir         rtl::OString real(
619*cdf0e10cSrcweir             rtl::OString(RTL_CONSTASCII_STRINGPARAM("_")) + name8);
620*cdf0e10cSrcweir         p = GetProcAddress(handle, real.getStr());
621*cdf0e10cSrcweir         if (p != 0) {
622*cdf0e10cSrcweir             proc->name = real;
623*cdf0e10cSrcweir             proc->proc = p;
624*cdf0e10cSrcweir             return ERRCODE_NONE;
625*cdf0e10cSrcweir         }
626*cdf0e10cSrcweir         real = name8 + rtl::OString(RTL_CONSTASCII_STRINGPARAM("A"));
627*cdf0e10cSrcweir         p = GetProcAddress(handle, real.getStr());
628*cdf0e10cSrcweir         if (p != 0) {
629*cdf0e10cSrcweir             proc->name = real;
630*cdf0e10cSrcweir             proc->proc = p;
631*cdf0e10cSrcweir             return ERRCODE_NONE;
632*cdf0e10cSrcweir         }
633*cdf0e10cSrcweir     }
634*cdf0e10cSrcweir     return ERRCODE_BASIC_PROC_UNDEFINED;
635*cdf0e10cSrcweir }
636*cdf0e10cSrcweir 
637*cdf0e10cSrcweir struct Dll: public salhelper::SimpleReferenceObject {
638*cdf0e10cSrcweir private:
639*cdf0e10cSrcweir     typedef std::map< rtl::OUString, ProcData > Procs;
640*cdf0e10cSrcweir 
641*cdf0e10cSrcweir     virtual ~Dll();
642*cdf0e10cSrcweir 
643*cdf0e10cSrcweir public:
644*cdf0e10cSrcweir     Dll(): handle(0) {}
645*cdf0e10cSrcweir 
646*cdf0e10cSrcweir     SbError getProc(rtl::OUString const & name, ProcData * proc);
647*cdf0e10cSrcweir 
648*cdf0e10cSrcweir     HMODULE handle;
649*cdf0e10cSrcweir     Procs procs;
650*cdf0e10cSrcweir };
651*cdf0e10cSrcweir 
652*cdf0e10cSrcweir Dll::~Dll() {
653*cdf0e10cSrcweir     if (handle != 0 && !FreeLibrary(handle)) {
654*cdf0e10cSrcweir         OSL_TRACE("FreeLibrary(%p) failed with %u", handle, GetLastError());
655*cdf0e10cSrcweir     }
656*cdf0e10cSrcweir }
657*cdf0e10cSrcweir 
658*cdf0e10cSrcweir SbError Dll::getProc(rtl::OUString const & name, ProcData * proc) {
659*cdf0e10cSrcweir     Procs::iterator i(procs.find(name));
660*cdf0e10cSrcweir     if (i != procs.end()) {
661*cdf0e10cSrcweir         *proc = i->second;
662*cdf0e10cSrcweir         return ERRCODE_NONE;
663*cdf0e10cSrcweir     }
664*cdf0e10cSrcweir     SbError e = getProcData(handle, name, proc);
665*cdf0e10cSrcweir     if (e == ERRCODE_NONE) {
666*cdf0e10cSrcweir         procs.insert(Procs::value_type(name, *proc));
667*cdf0e10cSrcweir     }
668*cdf0e10cSrcweir     return e;
669*cdf0e10cSrcweir }
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir rtl::OUString fullDllName(rtl::OUString const & name) {
672*cdf0e10cSrcweir     rtl::OUString full(name);
673*cdf0e10cSrcweir     if (full.indexOf('.') == -1) {
674*cdf0e10cSrcweir         full += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".DLL"));
675*cdf0e10cSrcweir     }
676*cdf0e10cSrcweir     return full;
677*cdf0e10cSrcweir }
678*cdf0e10cSrcweir 
679*cdf0e10cSrcweir }
680*cdf0e10cSrcweir 
681*cdf0e10cSrcweir struct SbiDllMgr::Impl: private boost::noncopyable {
682*cdf0e10cSrcweir private:
683*cdf0e10cSrcweir     typedef std::map< rtl::OUString, rtl::Reference< Dll > > Dlls;
684*cdf0e10cSrcweir 
685*cdf0e10cSrcweir public:
686*cdf0e10cSrcweir     Dll * getDll(rtl::OUString const & name);
687*cdf0e10cSrcweir 
688*cdf0e10cSrcweir     Dlls dlls;
689*cdf0e10cSrcweir };
690*cdf0e10cSrcweir 
691*cdf0e10cSrcweir Dll * SbiDllMgr::Impl::getDll(rtl::OUString const & name) {
692*cdf0e10cSrcweir     Dlls::iterator i(dlls.find(name));
693*cdf0e10cSrcweir     if (i == dlls.end()) {
694*cdf0e10cSrcweir         i = dlls.insert(Dlls::value_type(name, new Dll)).first;
695*cdf0e10cSrcweir         HMODULE h = LoadLibraryW(reinterpret_cast<LPCWSTR>(name.getStr()));
696*cdf0e10cSrcweir         if (h == 0) {
697*cdf0e10cSrcweir             dlls.erase(i);
698*cdf0e10cSrcweir             return 0;
699*cdf0e10cSrcweir         }
700*cdf0e10cSrcweir         i->second->handle = h;
701*cdf0e10cSrcweir     }
702*cdf0e10cSrcweir     return i->second.get();
703*cdf0e10cSrcweir }
704*cdf0e10cSrcweir 
705*cdf0e10cSrcweir SbError SbiDllMgr::Call(
706*cdf0e10cSrcweir     rtl::OUString const & function, rtl::OUString const & library,
707*cdf0e10cSrcweir     SbxArray * arguments, SbxVariable & result, bool cdeclConvention)
708*cdf0e10cSrcweir {
709*cdf0e10cSrcweir     if (cdeclConvention) {
710*cdf0e10cSrcweir         return ERRCODE_BASIC_NOT_IMPLEMENTED;
711*cdf0e10cSrcweir     }
712*cdf0e10cSrcweir     rtl::OUString dllName(fullDllName(library));
713*cdf0e10cSrcweir     Dll * dll = impl_->getDll(dllName);
714*cdf0e10cSrcweir     if (dll == 0) {
715*cdf0e10cSrcweir         return ERRCODE_BASIC_BAD_DLL_LOAD;
716*cdf0e10cSrcweir     }
717*cdf0e10cSrcweir     ProcData proc;
718*cdf0e10cSrcweir     SbError e = dll->getProc(function, &proc);
719*cdf0e10cSrcweir     if (e != ERRCODE_NONE) {
720*cdf0e10cSrcweir         return e;
721*cdf0e10cSrcweir     }
722*cdf0e10cSrcweir     return call(dllName, proc, arguments, result);
723*cdf0e10cSrcweir }
724*cdf0e10cSrcweir 
725*cdf0e10cSrcweir void SbiDllMgr::FreeDll(rtl::OUString const & library) {
726*cdf0e10cSrcweir     impl_->dlls.erase(library);
727*cdf0e10cSrcweir }
728*cdf0e10cSrcweir 
729*cdf0e10cSrcweir #else
730*cdf0e10cSrcweir 
731*cdf0e10cSrcweir struct SbiDllMgr::Impl {};
732*cdf0e10cSrcweir 
733*cdf0e10cSrcweir SbError SbiDllMgr::Call(
734*cdf0e10cSrcweir     rtl::OUString const &, rtl::OUString const &, SbxArray *, SbxVariable &,
735*cdf0e10cSrcweir     bool)
736*cdf0e10cSrcweir {
737*cdf0e10cSrcweir     return ERRCODE_BASIC_NOT_IMPLEMENTED;
738*cdf0e10cSrcweir }
739*cdf0e10cSrcweir 
740*cdf0e10cSrcweir void SbiDllMgr::FreeDll(rtl::OUString const &) {}
741*cdf0e10cSrcweir 
742*cdf0e10cSrcweir #endif
743*cdf0e10cSrcweir 
744*cdf0e10cSrcweir SbiDllMgr::SbiDllMgr(): impl_(new Impl) {}
745*cdf0e10cSrcweir 
746*cdf0e10cSrcweir SbiDllMgr::~SbiDllMgr() {}
747