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>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <com/sun/star/uno/RuntimeException.hpp>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <com/sun/star/uno/XInterface.hpp>
29 #include <cppu/unotype.hxx>
30 #include <rtl/byteseq.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/textcvt.h>
33 #include <rtl/textenc.h>
34 #include <rtl/ustring.h>
35 #include <rtl/ustring.hxx>
36 #include <sal/types.h>
37 #include <typelib/typeclass.h>
38 #include <typelib/typedescription.h>
39 #include <typelib/typedescription.hxx>
40 #include <uno/dispatcher.hxx>
42 #include "binaryany.hxx"
45 #include "lessoperators.hxx"
46 #include "marshal.hxx"
52 void write64(std::vector
< unsigned char > * buffer
, sal_uInt64 value
) {
53 Marshal::write8(buffer
, value
>> 56);
54 Marshal::write8(buffer
, (value
>> 48) & 0xFF);
55 Marshal::write8(buffer
, (value
>> 40) & 0xFF);
56 Marshal::write8(buffer
, (value
>> 32) & 0xFF);
57 Marshal::write8(buffer
, (value
>> 24) & 0xFF);
58 Marshal::write8(buffer
, (value
>> 16) & 0xFF);
59 Marshal::write8(buffer
, (value
>> 8) & 0xFF);
60 Marshal::write8(buffer
, value
& 0xFF);
63 void writeCompressed(std::vector
< unsigned char > * buffer
, sal_uInt32 value
) {
65 Marshal::write8(buffer
, static_cast< sal_uInt8
>(value
));
67 Marshal::write8(buffer
, 0xFF);
68 Marshal::write32(buffer
, value
);
73 std::vector
< unsigned char > * buffer
, OUString
const & value
)
75 assert(buffer
!= nullptr);
77 if (!value
.convertToString(
78 &v
, RTL_TEXTENCODING_UTF8
,
79 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
80 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
)))
82 throw css::uno::RuntimeException(
83 "UNO string contains invalid UTF-16 sequence");
85 writeCompressed(buffer
, static_cast< sal_uInt32
>(v
.getLength()));
86 buffer
->insert(buffer
->end(), v
.getStr(), v
.getStr() + v
.getLength());
91 Marshal::Marshal(rtl::Reference
< Bridge
> const & bridge
, WriterState
& state
):
92 bridge_(bridge
), state_(state
)
97 Marshal::~Marshal() {}
99 void Marshal::write8(std::vector
< unsigned char > * buffer
, sal_uInt8 value
) {
100 assert(buffer
!= nullptr);
101 buffer
->push_back(value
);
104 void Marshal::write16(std::vector
< unsigned char > * buffer
, sal_uInt16 value
) {
105 write8(buffer
, value
>> 8);
106 write8(buffer
, value
& 0xFF);
109 void Marshal::write32(std::vector
< unsigned char > * buffer
, sal_uInt32 value
) {
110 write8(buffer
, value
>> 24);
111 write8(buffer
, (value
>> 16) & 0xFF);
112 write8(buffer
, (value
>> 8) & 0xFF);
113 write8(buffer
, value
& 0xFF);
116 void Marshal::writeValue(
117 std::vector
< unsigned char > * buffer
,
118 css::uno::TypeDescription
const & type
, BinaryAny
const & value
)
122 (type
.get()->eTypeClass
== typelib_TypeClass_ANY
||
123 value
.getType().equals(type
)));
124 writeValue(buffer
, type
, value
.getValue(type
));
127 void Marshal::writeType(
128 std::vector
< unsigned char > * buffer
,
129 css::uno::TypeDescription
const & value
)
131 value
.makeComplete();
133 typelib_TypeClass tc
= value
.get()->eTypeClass
;
134 if (tc
<= typelib_TypeClass_ANY
) {
135 write8(buffer
, static_cast< sal_uInt8
>(tc
));
138 sal_uInt16 idx
= state_
.typeCache
.add(value
, &found
);
140 write8(buffer
, static_cast< sal_uInt8
>(tc
));
141 write16(buffer
, idx
);
143 write8(buffer
, static_cast< sal_uInt8
>(tc
) | 0x80);
144 write16(buffer
, idx
);
145 writeString(buffer
, OUString(value
.get()->pTypeName
));
150 void Marshal::writeOid(
151 std::vector
< unsigned char > * buffer
, OUString
const & oid
)
155 if ( oid
.isEmpty() ) {
159 idx
= state_
.oidCache
.add(oid
, &found
);
164 writeString(buffer
, oid
);
166 write16(buffer
, idx
);
169 void Marshal::writeTid(
170 std::vector
< unsigned char > * buffer
, rtl::ByteSequence
const & tid
)
173 sal_uInt16 idx
= state_
.tidCache
.add(tid
, &found
);
177 sal_Sequence
* p
= tid
.getHandle();
180 css::uno::TypeDescription(
181 cppu::UnoType
< css::uno::Sequence
< sal_Int8
> >::get()), &p
);
183 write16(buffer
, idx
);
186 void Marshal::writeValue(
187 std::vector
< unsigned char > * buffer
,
188 css::uno::TypeDescription
const & type
, void const * value
)
190 assert(buffer
!= nullptr && type
.is());
192 switch (type
.get()->eTypeClass
) {
193 case typelib_TypeClass_VOID
:
195 case typelib_TypeClass_BOOLEAN
:
196 assert(*static_cast< sal_uInt8
const * >(value
) <= 1);
198 case typelib_TypeClass_BYTE
:
199 write8(buffer
, *static_cast< sal_uInt8
const * >(value
));
201 case typelib_TypeClass_SHORT
:
202 case typelib_TypeClass_UNSIGNED_SHORT
:
203 case typelib_TypeClass_CHAR
:
204 write16(buffer
, *static_cast< sal_uInt16
const * >(value
));
206 case typelib_TypeClass_LONG
:
207 case typelib_TypeClass_UNSIGNED_LONG
:
208 case typelib_TypeClass_FLOAT
:
209 case typelib_TypeClass_ENUM
:
210 write32(buffer
, *static_cast< sal_uInt32
const * >(value
));
212 case typelib_TypeClass_HYPER
:
213 case typelib_TypeClass_UNSIGNED_HYPER
:
214 case typelib_TypeClass_DOUBLE
:
215 write64(buffer
, *static_cast< sal_uInt64
const * >(value
));
217 case typelib_TypeClass_STRING
:
220 OUString(*static_cast< rtl_uString
* const * >(value
)));
222 case typelib_TypeClass_TYPE
:
225 css::uno::TypeDescription(
226 *static_cast< typelib_TypeDescriptionReference
* const * >(
229 case typelib_TypeClass_ANY
:
231 uno_Any
const * p
= static_cast< uno_Any
const * >(value
);
232 css::uno::TypeDescription
t(p
->pType
);
233 writeType(buffer
, t
);
234 writeValue(buffer
, t
, p
->pData
);
237 case typelib_TypeClass_SEQUENCE
:
239 sal_Sequence
* p
= *static_cast< sal_Sequence
* const * >(value
);
240 writeCompressed(buffer
, static_cast< sal_uInt32
>(p
->nElements
));
241 css::uno::TypeDescription
ctd(
242 reinterpret_cast< typelib_IndirectTypeDescription
* >(
246 if (ctd
.get()->eTypeClass
== typelib_TypeClass_BYTE
) {
248 buffer
->end(), p
->elements
, p
->elements
+ p
->nElements
);
250 for (sal_Int32 i
= 0; i
!= p
->nElements
; ++i
) {
251 writeValue(buffer
, ctd
, p
->elements
+ i
* ctd
.get()->nSize
);
256 case typelib_TypeClass_STRUCT
:
257 case typelib_TypeClass_EXCEPTION
:
258 writeMemberValues(buffer
, type
, value
);
260 case typelib_TypeClass_INTERFACE
:
263 bridge_
->registerOutgoingInterface(
264 css::uno::UnoInterfaceReference(
265 *static_cast< uno_Interface
* const * >(value
)),
269 assert(false); // this cannot happen
274 void Marshal::writeMemberValues(
275 std::vector
< unsigned char > * buffer
,
276 css::uno::TypeDescription
const & type
, void const * aggregateValue
)
280 (type
.get()->eTypeClass
== typelib_TypeClass_STRUCT
||
281 type
.get()->eTypeClass
== typelib_TypeClass_EXCEPTION
) &&
282 aggregateValue
!= nullptr);
284 typelib_CompoundTypeDescription
* ctd
=
285 reinterpret_cast< typelib_CompoundTypeDescription
* >(type
.get());
286 if (ctd
->pBaseTypeDescription
!= nullptr) {
289 css::uno::TypeDescription(&ctd
->pBaseTypeDescription
->aBase
),
292 for (sal_Int32 i
= 0; i
!= ctd
->nMembers
; ++i
) {
294 buffer
, css::uno::TypeDescription(ctd
->ppTypeRefs
[i
]),
295 (static_cast< char const * >(aggregateValue
) +
296 ctd
->pMemberOffsets
[i
]));
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */