Line data Source code
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 "boost/noncopyable.hpp"
26 : #include "com/sun/star/uno/Reference.hxx"
27 : #include "com/sun/star/uno/RuntimeException.hpp"
28 : #include "com/sun/star/uno/Sequence.hxx"
29 : #include "com/sun/star/uno/XInterface.hpp"
30 : #include "cppu/unotype.hxx"
31 : #include "rtl/byteseq.hxx"
32 : #include "rtl/string.hxx"
33 : #include "rtl/textcvt.h"
34 : #include "rtl/textenc.h"
35 : #include "rtl/ustring.h"
36 : #include "rtl/ustring.hxx"
37 : #include "sal/types.h"
38 : #include "typelib/typeclass.h"
39 : #include "typelib/typedescription.h"
40 : #include "typelib/typedescription.hxx"
41 : #include "uno/dispatcher.hxx"
42 :
43 : #include "binaryany.hxx"
44 : #include "bridge.hxx"
45 : #include "cache.hxx"
46 : #include "lessoperators.hxx"
47 : #include "marshal.hxx"
48 :
49 : namespace binaryurp {
50 :
51 : namespace {
52 :
53 0 : void write64(std::vector< unsigned char > * buffer, sal_uInt64 value) {
54 0 : Marshal::write8(buffer, value >> 56);
55 0 : Marshal::write8(buffer, (value >> 48) & 0xFF);
56 0 : Marshal::write8(buffer, (value >> 40) & 0xFF);
57 0 : Marshal::write8(buffer, (value >> 32) & 0xFF);
58 0 : Marshal::write8(buffer, (value >> 24) & 0xFF);
59 0 : Marshal::write8(buffer, (value >> 16) & 0xFF);
60 0 : Marshal::write8(buffer, (value >> 8) & 0xFF);
61 0 : Marshal::write8(buffer, value & 0xFF);
62 0 : }
63 :
64 0 : void writeCompressed(std::vector< unsigned char > * buffer, sal_uInt32 value) {
65 0 : if (value < 0xFF) {
66 0 : Marshal::write8(buffer, static_cast< sal_uInt8 >(value));
67 : } else {
68 0 : Marshal::write8(buffer, 0xFF);
69 0 : Marshal::write32(buffer, value);
70 : }
71 0 : }
72 :
73 0 : void writeString(
74 : std::vector< unsigned char > * buffer, OUString const & value)
75 : {
76 : assert(buffer != 0);
77 0 : OString v;
78 0 : if (!value.convertToString(
79 : &v, RTL_TEXTENCODING_UTF8,
80 : (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
81 0 : RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
82 : {
83 : throw css::uno::RuntimeException(
84 : "UNO string contains invalid UTF-16 sequence",
85 0 : css::uno::Reference< css::uno::XInterface >());
86 : }
87 0 : writeCompressed(buffer, static_cast< sal_uInt32 >(v.getLength()));
88 0 : buffer->insert(buffer->end(), v.getStr(), v.getStr() + v.getLength());
89 0 : }
90 :
91 : }
92 :
93 0 : Marshal::Marshal(rtl::Reference< Bridge > const & bridge, WriterState & state):
94 0 : bridge_(bridge), state_(state)
95 : {
96 : assert(bridge.is());
97 0 : }
98 :
99 0 : Marshal::~Marshal() {}
100 :
101 0 : void Marshal::write8(std::vector< unsigned char > * buffer, sal_uInt8 value) {
102 : assert(buffer != 0);
103 0 : buffer->push_back(value);
104 0 : }
105 :
106 0 : void Marshal::write16(std::vector< unsigned char > * buffer, sal_uInt16 value) {
107 0 : write8(buffer, value >> 8);
108 0 : write8(buffer, value & 0xFF);
109 0 : }
110 :
111 0 : void Marshal::write32(std::vector< unsigned char > * buffer, sal_uInt32 value) {
112 0 : write8(buffer, value >> 24);
113 0 : write8(buffer, (value >> 16) & 0xFF);
114 0 : write8(buffer, (value >> 8) & 0xFF);
115 0 : write8(buffer, value & 0xFF);
116 0 : }
117 :
118 0 : void Marshal::writeValue(
119 : std::vector< unsigned char > * buffer,
120 : css::uno::TypeDescription const & type, BinaryAny const & value)
121 : {
122 : assert(
123 : type.is() &&
124 : (type.get()->eTypeClass == typelib_TypeClass_ANY ||
125 : value.getType().equals(type)));
126 0 : writeValue(buffer, type, value.getValue(type));
127 0 : }
128 :
129 0 : void Marshal::writeType(
130 : std::vector< unsigned char > * buffer,
131 : css::uno::TypeDescription const & value)
132 : {
133 0 : value.makeComplete();
134 : assert(value.is());
135 0 : typelib_TypeClass tc = value.get()->eTypeClass;
136 0 : if (tc <= typelib_TypeClass_ANY) {
137 0 : write8(buffer, static_cast< sal_uInt8 >(tc));
138 : } else {
139 : bool found;
140 0 : sal_uInt16 idx = state_.typeCache.add(value, &found);
141 0 : if (found) {
142 0 : write8(buffer, static_cast< sal_uInt8 >(tc));
143 0 : write16(buffer, idx);
144 : } else {
145 0 : write8(buffer, static_cast< sal_uInt8 >(tc) | 0x80);
146 0 : write16(buffer, idx);
147 0 : writeString(buffer, OUString(value.get()->pTypeName));
148 : }
149 : }
150 0 : }
151 :
152 0 : void Marshal::writeOid(
153 : std::vector< unsigned char > * buffer, OUString const & oid)
154 : {
155 : bool found;
156 : sal_uInt16 idx;
157 0 : if ( oid.isEmpty() ) {
158 0 : found = true;
159 0 : idx = cache::ignore;
160 : } else {
161 0 : idx = state_.oidCache.add(oid, &found);
162 : }
163 0 : if (found) {
164 0 : write8(buffer, 0);
165 : } else {
166 0 : writeString(buffer, oid);
167 : }
168 0 : write16(buffer, idx);
169 0 : }
170 :
171 0 : void Marshal::writeTid(
172 : std::vector< unsigned char > * buffer, rtl::ByteSequence const & tid)
173 : {
174 : bool found;
175 0 : sal_uInt16 idx = state_.tidCache.add(tid, &found);
176 0 : if (found) {
177 0 : write8(buffer, 0);
178 : } else {
179 0 : sal_Sequence * p = tid.getHandle();
180 : writeValue(
181 : buffer,
182 : css::uno::TypeDescription(
183 0 : cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()), &p);
184 : }
185 0 : write16(buffer, idx);
186 0 : }
187 :
188 0 : void Marshal::writeValue(
189 : std::vector< unsigned char > * buffer,
190 : css::uno::TypeDescription const & type, void const * value)
191 : {
192 : assert(buffer != 0 && type.is());
193 0 : type.makeComplete();
194 0 : switch (type.get()->eTypeClass) {
195 : case typelib_TypeClass_VOID:
196 0 : break;
197 : case typelib_TypeClass_BOOLEAN:
198 : assert(*static_cast< sal_uInt8 const * >(value) <= 1);
199 : // fall through
200 : case typelib_TypeClass_BYTE:
201 0 : write8(buffer, *static_cast< sal_uInt8 const * >(value));
202 0 : break;
203 : case typelib_TypeClass_SHORT:
204 : case typelib_TypeClass_UNSIGNED_SHORT:
205 : case typelib_TypeClass_CHAR:
206 0 : write16(buffer, *static_cast< sal_uInt16 const * >(value));
207 0 : break;
208 : case typelib_TypeClass_LONG:
209 : case typelib_TypeClass_UNSIGNED_LONG:
210 : case typelib_TypeClass_FLOAT:
211 : case typelib_TypeClass_ENUM:
212 0 : write32(buffer, *static_cast< sal_uInt32 const * >(value));
213 0 : break;
214 : case typelib_TypeClass_HYPER:
215 : case typelib_TypeClass_UNSIGNED_HYPER:
216 : case typelib_TypeClass_DOUBLE:
217 0 : write64(buffer, *static_cast< sal_uInt64 const * >(value));
218 0 : break;
219 : case typelib_TypeClass_STRING:
220 : writeString(
221 : buffer,
222 0 : OUString(*static_cast< rtl_uString * const * >(value)));
223 0 : break;
224 : case typelib_TypeClass_TYPE:
225 : writeType(
226 : buffer,
227 : css::uno::TypeDescription(
228 : *static_cast< typelib_TypeDescriptionReference * const * >(
229 0 : value)));
230 0 : break;
231 : case typelib_TypeClass_ANY:
232 : {
233 0 : uno_Any const * p = static_cast< uno_Any const * >(value);
234 0 : css::uno::TypeDescription t(p->pType);
235 0 : writeType(buffer, t);
236 0 : writeValue(buffer, t, p->pData);
237 0 : break;
238 : }
239 : case typelib_TypeClass_SEQUENCE:
240 : {
241 0 : sal_Sequence * p = *static_cast< sal_Sequence * const * >(value);
242 0 : writeCompressed(buffer, static_cast< sal_uInt32 >(p->nElements));
243 : css::uno::TypeDescription ctd(
244 : reinterpret_cast< typelib_IndirectTypeDescription * >(
245 0 : type.get())->
246 0 : pType);
247 : assert(ctd.is());
248 0 : if (ctd.get()->eTypeClass == typelib_TypeClass_BYTE) {
249 : buffer->insert(
250 0 : buffer->end(), p->elements, p->elements + p->nElements);
251 : } else {
252 0 : for (sal_Int32 i = 0; i != p->nElements; ++i) {
253 0 : writeValue(buffer, ctd, p->elements + i * ctd.get()->nSize);
254 : }
255 : }
256 0 : break;
257 : }
258 : case typelib_TypeClass_STRUCT:
259 : case typelib_TypeClass_EXCEPTION:
260 0 : writeMemberValues(buffer, type, value);
261 0 : break;
262 : case typelib_TypeClass_INTERFACE:
263 : writeOid(
264 : buffer,
265 : bridge_->registerOutgoingInterface(
266 : css::uno::UnoInterfaceReference(
267 : *static_cast< uno_Interface * const * >(value)),
268 0 : type));
269 0 : break;
270 : default:
271 : assert(false); // this cannot happen
272 0 : break;
273 : }
274 0 : }
275 :
276 0 : void Marshal::writeMemberValues(
277 : std::vector< unsigned char > * buffer,
278 : css::uno::TypeDescription const & type, void const * aggregateValue)
279 : {
280 : assert(
281 : type.is() &&
282 : (type.get()->eTypeClass == typelib_TypeClass_STRUCT ||
283 : type.get()->eTypeClass == typelib_TypeClass_EXCEPTION) &&
284 : aggregateValue != 0);
285 0 : type.makeComplete();
286 : typelib_CompoundTypeDescription * ctd =
287 0 : reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
288 0 : if (ctd->pBaseTypeDescription != 0) {
289 : writeMemberValues(
290 : buffer,
291 : css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
292 0 : aggregateValue);
293 : }
294 0 : for (sal_Int32 i = 0; i != ctd->nMembers; ++i) {
295 : writeValue(
296 0 : buffer, css::uno::TypeDescription(ctd->ppTypeRefs[i]),
297 0 : (static_cast< char const * >(aggregateValue) +
298 0 : ctd->pMemberOffsets[i]));
299 : }
300 0 : }
301 :
302 : }
303 :
304 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|