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/RuntimeException.hpp>
26 #include <com/sun/star/uno/Sequence.hxx>
27 #include <cppu/unotype.hxx>
28 #include <rtl/byteseq.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/textcvt.h>
31 #include <rtl/textenc.h>
32 #include <rtl/ustring.h>
33 #include <rtl/ustring.hxx>
34 #include <sal/types.h>
35 #include <typelib/typeclass.h>
36 #include <typelib/typedescription.h>
37 #include <typelib/typedescription.hxx>
38 #include <uno/dispatcher.hxx>
40 #include "binaryany.hxx"
43 #include "lessoperators.hxx"
44 #include "marshal.hxx"
50 void write64(std::vector
< unsigned char > * buffer
, sal_uInt64 value
) {
51 Marshal::write8(buffer
, value
>> 56);
52 Marshal::write8(buffer
, (value
>> 48) & 0xFF);
53 Marshal::write8(buffer
, (value
>> 40) & 0xFF);
54 Marshal::write8(buffer
, (value
>> 32) & 0xFF);
55 Marshal::write8(buffer
, (value
>> 24) & 0xFF);
56 Marshal::write8(buffer
, (value
>> 16) & 0xFF);
57 Marshal::write8(buffer
, (value
>> 8) & 0xFF);
58 Marshal::write8(buffer
, value
& 0xFF);
61 void writeCompressed(std::vector
< unsigned char > * buffer
, sal_uInt32 value
) {
63 Marshal::write8(buffer
, static_cast< sal_uInt8
>(value
));
65 Marshal::write8(buffer
, 0xFF);
66 Marshal::write32(buffer
, value
);
71 std::vector
< unsigned char > * buffer
, OUString
const & value
)
73 assert(buffer
!= nullptr);
75 if (!value
.convertToString(
76 &v
, RTL_TEXTENCODING_UTF8
,
77 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
|
78 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR
)))
80 throw css::uno::RuntimeException(
81 "UNO string contains invalid UTF-16 sequence");
83 writeCompressed(buffer
, static_cast< sal_uInt32
>(v
.getLength()));
84 buffer
->insert(buffer
->end(), v
.getStr(), v
.getStr() + v
.getLength());
89 Marshal::Marshal(rtl::Reference
< Bridge
> const & bridge
, WriterState
& state
):
90 bridge_(bridge
), state_(state
)
95 Marshal::~Marshal() {}
97 void Marshal::write8(std::vector
< unsigned char > * buffer
, sal_uInt8 value
) {
98 assert(buffer
!= nullptr);
99 buffer
->push_back(value
);
102 void Marshal::write16(std::vector
< unsigned char > * buffer
, sal_uInt16 value
) {
103 write8(buffer
, value
>> 8);
104 write8(buffer
, value
& 0xFF);
107 void Marshal::write32(std::vector
< unsigned char > * buffer
, sal_uInt32 value
) {
108 write8(buffer
, value
>> 24);
109 write8(buffer
, (value
>> 16) & 0xFF);
110 write8(buffer
, (value
>> 8) & 0xFF);
111 write8(buffer
, value
& 0xFF);
114 void Marshal::writeValue(
115 std::vector
< unsigned char > * buffer
,
116 css::uno::TypeDescription
const & type
, BinaryAny
const & value
)
120 (type
.get()->eTypeClass
== typelib_TypeClass_ANY
||
121 value
.getType().equals(type
)));
122 writeValue(buffer
, type
, value
.getValue(type
));
125 void Marshal::writeType(
126 std::vector
< unsigned char > * buffer
,
127 css::uno::TypeDescription
const & value
)
129 value
.makeComplete();
131 typelib_TypeClass tc
= value
.get()->eTypeClass
;
132 if (tc
<= typelib_TypeClass_ANY
) {
133 write8(buffer
, static_cast< sal_uInt8
>(tc
));
136 sal_uInt16 idx
= state_
.typeCache
.add(value
, &found
);
138 write8(buffer
, static_cast< sal_uInt8
>(tc
));
139 write16(buffer
, idx
);
141 write8(buffer
, static_cast< sal_uInt8
>(tc
) | 0x80);
142 write16(buffer
, idx
);
143 writeString(buffer
, OUString(value
.get()->pTypeName
));
148 void Marshal::writeOid(
149 std::vector
< unsigned char > * buffer
, OUString
const & oid
)
153 if ( oid
.isEmpty() ) {
157 idx
= state_
.oidCache
.add(oid
, &found
);
162 writeString(buffer
, oid
);
164 write16(buffer
, idx
);
167 void Marshal::writeTid(
168 std::vector
< unsigned char > * buffer
, rtl::ByteSequence
const & tid
)
171 sal_uInt16 idx
= state_
.tidCache
.add(tid
, &found
);
175 sal_Sequence
* p
= tid
.getHandle();
178 css::uno::TypeDescription(
179 cppu::UnoType
< css::uno::Sequence
< sal_Int8
> >::get()), &p
);
181 write16(buffer
, idx
);
184 void Marshal::writeValue(
185 std::vector
< unsigned char > * buffer
,
186 css::uno::TypeDescription
const & type
, void const * value
)
188 assert(buffer
!= nullptr && type
.is());
190 switch (type
.get()->eTypeClass
) {
191 case typelib_TypeClass_VOID
:
193 case typelib_TypeClass_BOOLEAN
:
194 assert(*static_cast< sal_uInt8
const * >(value
) <= 1);
196 case typelib_TypeClass_BYTE
:
197 write8(buffer
, *static_cast< sal_uInt8
const * >(value
));
199 case typelib_TypeClass_SHORT
:
200 case typelib_TypeClass_UNSIGNED_SHORT
:
201 case typelib_TypeClass_CHAR
:
202 write16(buffer
, *static_cast< sal_uInt16
const * >(value
));
204 case typelib_TypeClass_LONG
:
205 case typelib_TypeClass_UNSIGNED_LONG
:
206 case typelib_TypeClass_FLOAT
:
207 case typelib_TypeClass_ENUM
:
208 write32(buffer
, *static_cast< sal_uInt32
const * >(value
));
210 case typelib_TypeClass_HYPER
:
211 case typelib_TypeClass_UNSIGNED_HYPER
:
212 case typelib_TypeClass_DOUBLE
:
213 write64(buffer
, *static_cast< sal_uInt64
const * >(value
));
215 case typelib_TypeClass_STRING
:
218 OUString(*static_cast< rtl_uString
* const * >(value
)));
220 case typelib_TypeClass_TYPE
:
223 css::uno::TypeDescription(
224 *static_cast< typelib_TypeDescriptionReference
* const * >(
227 case typelib_TypeClass_ANY
:
229 uno_Any
const * p
= static_cast< uno_Any
const * >(value
);
230 css::uno::TypeDescription
t(p
->pType
);
231 writeType(buffer
, t
);
232 writeValue(buffer
, t
, p
->pData
);
235 case typelib_TypeClass_SEQUENCE
:
237 sal_Sequence
* p
= *static_cast< sal_Sequence
* const * >(value
);
238 writeCompressed(buffer
, static_cast< sal_uInt32
>(p
->nElements
));
239 css::uno::TypeDescription
ctd(
240 reinterpret_cast< typelib_IndirectTypeDescription
* >(
244 if (ctd
.get()->eTypeClass
== typelib_TypeClass_BYTE
) {
246 buffer
->end(), p
->elements
, p
->elements
+ p
->nElements
);
248 for (sal_Int32 i
= 0; i
!= p
->nElements
; ++i
) {
249 writeValue(buffer
, ctd
, p
->elements
+ i
* ctd
.get()->nSize
);
254 case typelib_TypeClass_STRUCT
:
255 case typelib_TypeClass_EXCEPTION
:
256 writeMemberValues(buffer
, type
, value
);
258 case typelib_TypeClass_INTERFACE
:
261 bridge_
->registerOutgoingInterface(
262 css::uno::UnoInterfaceReference(
263 *static_cast< uno_Interface
* const * >(value
)),
267 assert(false); // this cannot happen
272 void Marshal::writeMemberValues(
273 std::vector
< unsigned char > * buffer
,
274 css::uno::TypeDescription
const & type
, void const * aggregateValue
)
278 (type
.get()->eTypeClass
== typelib_TypeClass_STRUCT
||
279 type
.get()->eTypeClass
== typelib_TypeClass_EXCEPTION
) &&
280 aggregateValue
!= nullptr);
282 typelib_CompoundTypeDescription
* ctd
=
283 reinterpret_cast< typelib_CompoundTypeDescription
* >(type
.get());
284 if (ctd
->pBaseTypeDescription
!= nullptr) {
287 css::uno::TypeDescription(&ctd
->pBaseTypeDescription
->aBase
),
290 for (sal_Int32 i
= 0; i
!= ctd
->nMembers
; ++i
) {
292 buffer
, css::uno::TypeDescription(ctd
->ppTypeRefs
[i
]),
293 (static_cast< char const * >(aggregateValue
) +
294 ctd
->pMemberOffsets
[i
]));
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */