lok: jsbuilder: avoid sending early id to client side
[LibreOffice.git] / binaryurp / source / marshal.cxx
blob7d60cbf4de8a711beeae0eb963a4f5055a61800b
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/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"
41 #include "bridge.hxx"
42 #include "cache.hxx"
43 #include "lessoperators.hxx"
44 #include "marshal.hxx"
46 namespace binaryurp {
48 namespace {
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) {
62 if (value < 0xFF) {
63 Marshal::write8(buffer, static_cast< sal_uInt8 >(value));
64 } else {
65 Marshal::write8(buffer, 0xFF);
66 Marshal::write32(buffer, value);
70 void writeString(
71 std::vector< unsigned char > * buffer, OUString const & value)
73 assert(buffer != nullptr);
74 OString v;
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)
92 assert(bridge.is());
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)
118 assert(
119 type.is() &&
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();
130 assert(value.is());
131 typelib_TypeClass tc = value.get()->eTypeClass;
132 if (tc <= typelib_TypeClass_ANY) {
133 write8(buffer, static_cast< sal_uInt8 >(tc));
134 } else {
135 bool found;
136 sal_uInt16 idx = state_.typeCache.add(value, &found);
137 if (found) {
138 write8(buffer, static_cast< sal_uInt8 >(tc));
139 write16(buffer, idx);
140 } else {
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)
151 bool found;
152 sal_uInt16 idx;
153 if ( oid.isEmpty() ) {
154 found = true;
155 idx = cache::ignore;
156 } else {
157 idx = state_.oidCache.add(oid, &found);
159 if (found) {
160 write8(buffer, 0);
161 } else {
162 writeString(buffer, oid);
164 write16(buffer, idx);
167 void Marshal::writeTid(
168 std::vector< unsigned char > * buffer, rtl::ByteSequence const & tid)
170 bool found;
171 sal_uInt16 idx = state_.tidCache.add(tid, &found);
172 if (found) {
173 write8(buffer, 0);
174 } else {
175 sal_Sequence * p = tid.getHandle();
176 writeValue(
177 buffer,
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());
189 type.makeComplete();
190 switch (type.get()->eTypeClass) {
191 case typelib_TypeClass_VOID:
192 break;
193 case typelib_TypeClass_BOOLEAN:
194 assert(*static_cast< sal_uInt8 const * >(value) <= 1);
195 [[fallthrough]];
196 case typelib_TypeClass_BYTE:
197 write8(buffer, *static_cast< sal_uInt8 const * >(value));
198 break;
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));
203 break;
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));
209 break;
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));
214 break;
215 case typelib_TypeClass_STRING:
216 writeString(
217 buffer,
218 OUString(*static_cast< rtl_uString * const * >(value)));
219 break;
220 case typelib_TypeClass_TYPE:
221 writeType(
222 buffer,
223 css::uno::TypeDescription(
224 *static_cast< typelib_TypeDescriptionReference * const * >(
225 value)));
226 break;
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);
233 break;
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 * >(
241 type.get())->
242 pType);
243 assert(ctd.is());
244 if (ctd.get()->eTypeClass == typelib_TypeClass_BYTE) {
245 buffer->insert(
246 buffer->end(), p->elements, p->elements + p->nElements);
247 } else {
248 for (sal_Int32 i = 0; i != p->nElements; ++i) {
249 writeValue(buffer, ctd, p->elements + i * ctd.get()->nSize);
252 break;
254 case typelib_TypeClass_STRUCT:
255 case typelib_TypeClass_EXCEPTION:
256 writeMemberValues(buffer, type, value);
257 break;
258 case typelib_TypeClass_INTERFACE:
259 writeOid(
260 buffer,
261 bridge_->registerOutgoingInterface(
262 css::uno::UnoInterfaceReference(
263 *static_cast< uno_Interface * const * >(value)),
264 type));
265 break;
266 default:
267 assert(false); // this cannot happen
268 break;
272 void Marshal::writeMemberValues(
273 std::vector< unsigned char > * buffer,
274 css::uno::TypeDescription const & type, void const * aggregateValue)
276 assert(
277 type.is() &&
278 (type.get()->eTypeClass == typelib_TypeClass_STRUCT ||
279 type.get()->eTypeClass == typelib_TypeClass_EXCEPTION) &&
280 aggregateValue != nullptr);
281 type.makeComplete();
282 typelib_CompoundTypeDescription * ctd =
283 reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
284 if (ctd->pBaseTypeDescription != nullptr) {
285 writeMemberValues(
286 buffer,
287 css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
288 aggregateValue);
290 for (sal_Int32 i = 0; i != ctd->nMembers; ++i) {
291 writeValue(
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: */