Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / binaryurp / source / marshal.cxx
blob009cd06968e78d2d74fb7c06d6984c1674ba84d5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <cassert>
23 #include <vector>
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"
43 #include "bridge.hxx"
44 #include "cache.hxx"
45 #include "lessoperators.hxx"
46 #include "marshal.hxx"
48 namespace binaryurp {
50 namespace {
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) {
64 if (value < 0xFF) {
65 Marshal::write8(buffer, static_cast< sal_uInt8 >(value));
66 } else {
67 Marshal::write8(buffer, 0xFF);
68 Marshal::write32(buffer, value);
72 void writeString(
73 std::vector< unsigned char > * buffer, OUString const & value)
75 assert(buffer != nullptr);
76 OString v;
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)
94 assert(bridge.is());
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)
120 assert(
121 type.is() &&
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();
132 assert(value.is());
133 typelib_TypeClass tc = value.get()->eTypeClass;
134 if (tc <= typelib_TypeClass_ANY) {
135 write8(buffer, static_cast< sal_uInt8 >(tc));
136 } else {
137 bool found;
138 sal_uInt16 idx = state_.typeCache.add(value, &found);
139 if (found) {
140 write8(buffer, static_cast< sal_uInt8 >(tc));
141 write16(buffer, idx);
142 } else {
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)
153 bool found;
154 sal_uInt16 idx;
155 if ( oid.isEmpty() ) {
156 found = true;
157 idx = cache::ignore;
158 } else {
159 idx = state_.oidCache.add(oid, &found);
161 if (found) {
162 write8(buffer, 0);
163 } else {
164 writeString(buffer, oid);
166 write16(buffer, idx);
169 void Marshal::writeTid(
170 std::vector< unsigned char > * buffer, rtl::ByteSequence const & tid)
172 bool found;
173 sal_uInt16 idx = state_.tidCache.add(tid, &found);
174 if (found) {
175 write8(buffer, 0);
176 } else {
177 sal_Sequence * p = tid.getHandle();
178 writeValue(
179 buffer,
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());
191 type.makeComplete();
192 switch (type.get()->eTypeClass) {
193 case typelib_TypeClass_VOID:
194 break;
195 case typelib_TypeClass_BOOLEAN:
196 assert(*static_cast< sal_uInt8 const * >(value) <= 1);
197 SAL_FALLTHROUGH;
198 case typelib_TypeClass_BYTE:
199 write8(buffer, *static_cast< sal_uInt8 const * >(value));
200 break;
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));
205 break;
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));
211 break;
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));
216 break;
217 case typelib_TypeClass_STRING:
218 writeString(
219 buffer,
220 OUString(*static_cast< rtl_uString * const * >(value)));
221 break;
222 case typelib_TypeClass_TYPE:
223 writeType(
224 buffer,
225 css::uno::TypeDescription(
226 *static_cast< typelib_TypeDescriptionReference * const * >(
227 value)));
228 break;
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);
235 break;
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 * >(
243 type.get())->
244 pType);
245 assert(ctd.is());
246 if (ctd.get()->eTypeClass == typelib_TypeClass_BYTE) {
247 buffer->insert(
248 buffer->end(), p->elements, p->elements + p->nElements);
249 } else {
250 for (sal_Int32 i = 0; i != p->nElements; ++i) {
251 writeValue(buffer, ctd, p->elements + i * ctd.get()->nSize);
254 break;
256 case typelib_TypeClass_STRUCT:
257 case typelib_TypeClass_EXCEPTION:
258 writeMemberValues(buffer, type, value);
259 break;
260 case typelib_TypeClass_INTERFACE:
261 writeOid(
262 buffer,
263 bridge_->registerOutgoingInterface(
264 css::uno::UnoInterfaceReference(
265 *static_cast< uno_Interface * const * >(value)),
266 type));
267 break;
268 default:
269 assert(false); // this cannot happen
270 break;
274 void Marshal::writeMemberValues(
275 std::vector< unsigned char > * buffer,
276 css::uno::TypeDescription const & type, void const * aggregateValue)
278 assert(
279 type.is() &&
280 (type.get()->eTypeClass == typelib_TypeClass_STRUCT ||
281 type.get()->eTypeClass == typelib_TypeClass_EXCEPTION) &&
282 aggregateValue != nullptr);
283 type.makeComplete();
284 typelib_CompoundTypeDescription * ctd =
285 reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
286 if (ctd->pBaseTypeDescription != nullptr) {
287 writeMemberValues(
288 buffer,
289 css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
290 aggregateValue);
292 for (sal_Int32 i = 0; i != ctd->nMembers; ++i) {
293 writeValue(
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: */