LibreOffice Module binaryurp (master) 1
marshal.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <cassert>
23#include <vector>
24
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>
39
40#include "binaryany.hxx"
41#include "bridge.hxx"
42#include "cache.hxx"
43#include "lessoperators.hxx"
44#include "marshal.hxx"
45
46namespace binaryurp {
47
48namespace {
49
50void 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);
59}
60
61void 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);
67 }
68}
69
70void writeString(
71 std::vector< unsigned char > * buffer, OUString const & value)
72{
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)))
79 {
80 throw css::uno::RuntimeException(
81 "UNO string contains invalid UTF-16 sequence");
82 }
83 writeCompressed(buffer, static_cast< sal_uInt32 >(v.getLength()));
84 buffer->insert(buffer->end(), v.getStr(), v.getStr() + v.getLength());
85}
86
87}
88
90 bridge_(bridge), state_(state)
91{
92 assert(bridge.is());
93}
94
96
97void Marshal::write8(std::vector< unsigned char > * buffer, sal_uInt8 value) {
98 assert(buffer != nullptr);
99 buffer->push_back(value);
100}
101
102void Marshal::write16(std::vector< unsigned char > * buffer, sal_uInt16 value) {
103 write8(buffer, value >> 8);
104 write8(buffer, value & 0xFF);
105}
106
107void 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);
112}
113
115 std::vector< unsigned char > * buffer,
116 css::uno::TypeDescription const & type, BinaryAny const & value)
117{
118 assert(
119 type.is() &&
120 (type.get()->eTypeClass == typelib_TypeClass_ANY ||
121 value.getType().equals(type)));
122 writeValue(buffer, type, value.getValue(type));
123}
124
126 std::vector< unsigned char > * buffer,
127 css::uno::TypeDescription const & value)
128{
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));
144 }
145 }
146}
147
149 std::vector< unsigned char > * buffer, OUString const & oid)
150{
151 bool found;
152 sal_uInt16 idx;
153 if ( oid.isEmpty() ) {
154 found = true;
156 } else {
157 idx = state_.oidCache.add(oid, &found);
158 }
159 if (found) {
160 write8(buffer, 0);
161 } else {
162 writeString(buffer, oid);
163 }
164 write16(buffer, idx);
165}
166
168 std::vector< unsigned char > * buffer, rtl::ByteSequence const & tid)
169{
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();
177 buffer,
178 css::uno::TypeDescription(
179 cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()), &p);
180 }
181 write16(buffer, idx);
182}
183
185 std::vector< unsigned char > * buffer,
186 css::uno::TypeDescription const & type, void const * value)
187{
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:
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:
228 {
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;
234 }
235 case typelib_TypeClass_SEQUENCE:
236 {
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);
250 }
251 }
252 break;
253 }
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;
269 }
270}
271
273 std::vector< unsigned char > * buffer,
274 css::uno::TypeDescription const & type, void const * aggregateValue)
275{
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) {
286 buffer,
287 css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
288 aggregateValue);
289 }
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]));
295 }
296}
297
298}
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
IdxType add(const T &rContent, bool *pbFound)
Definition: cache.hxx:49
static void write16(std::vector< unsigned char > *buffer, sal_uInt16 value)
Definition: marshal.cxx:102
void writeType(std::vector< unsigned char > *buffer, com::sun::star::uno::TypeDescription const &value)
Definition: marshal.cxx:125
void writeValue(std::vector< unsigned char > *buffer, com::sun::star::uno::TypeDescription const &type, BinaryAny const &value)
rtl::Reference< Bridge > bridge_
Definition: marshal.hxx:82
void writeTid(std::vector< unsigned char > *buffer, rtl::ByteSequence const &tid)
Definition: marshal.cxx:167
void writeOid(std::vector< unsigned char > *buffer, OUString const &oid)
Definition: marshal.cxx:148
Marshal(rtl::Reference< Bridge > const &bridge, WriterState &state)
Definition: marshal.cxx:89
static void write32(std::vector< unsigned char > *buffer, sal_uInt32 value)
Definition: marshal.cxx:107
WriterState & state_
Definition: marshal.hxx:83
static void write8(std::vector< unsigned char > *buffer, sal_uInt8 value)
Definition: marshal.cxx:97
void writeMemberValues(std::vector< unsigned char > *buffer, com::sun::star::uno::TypeDescription const &type, void const *aggregateValue)
Definition: marshal.cxx:272
Any value
float v
const sal_uInt16 idx[]
void * p
struct _uno_Any uno_Any
css::uno::UnoInterfaceReference get()
int i
sal_uInt32 writeString(sal_uInt8 *buffer, const sal_Unicode *v)
Cache< rtl::ByteSequence > tidCache
Definition: writerstate.hxx:44
Cache< OUString > oidCache
Definition: writerstate.hxx:42
Cache< com::sun::star::uno::TypeDescription > typeCache
Definition: writerstate.hxx:40
unsigned char sal_uInt8
ResultType type