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 1742 : void write64(std::vector< unsigned char > * buffer, sal_uInt64 value) {
54 1742 : Marshal::write8(buffer, value >> 56);
55 1742 : Marshal::write8(buffer, (value >> 48) & 0xFF);
56 1742 : Marshal::write8(buffer, (value >> 40) & 0xFF);
57 1742 : Marshal::write8(buffer, (value >> 32) & 0xFF);
58 1742 : Marshal::write8(buffer, (value >> 24) & 0xFF);
59 1742 : Marshal::write8(buffer, (value >> 16) & 0xFF);
60 1742 : Marshal::write8(buffer, (value >> 8) & 0xFF);
61 1742 : Marshal::write8(buffer, value & 0xFF);
62 1742 : }
63 :
64 141309 : void writeCompressed(std::vector< unsigned char > * buffer, sal_uInt32 value) {
65 141309 : if (value < 0xFF) {
66 141277 : Marshal::write8(buffer, static_cast< sal_uInt8 >(value));
67 : } else {
68 32 : Marshal::write8(buffer, 0xFF);
69 32 : Marshal::write32(buffer, value);
70 : }
71 141309 : }
72 :
73 131493 : void writeString(
74 : std::vector< unsigned char > * buffer, OUString const & value)
75 : {
76 : assert(buffer != 0);
77 131493 : OString v;
78 131493 : if (!value.convertToString(
79 : &v, RTL_TEXTENCODING_UTF8,
80 : (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
81 131493 : 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 131493 : writeCompressed(buffer, static_cast< sal_uInt32 >(v.getLength()));
88 131493 : buffer->insert(buffer->end(), v.getStr(), v.getStr() + v.getLength());
89 131493 : }
90 :
91 : }
92 :
93 54 : Marshal::Marshal(rtl::Reference< Bridge > const & bridge, WriterState & state):
94 54 : bridge_(bridge), state_(state)
95 : {
96 : assert(bridge.is());
97 54 : }
98 :
99 53 : Marshal::~Marshal() {}
100 :
101 3083697 : void Marshal::write8(std::vector< unsigned char > * buffer, sal_uInt8 value) {
102 : assert(buffer != 0);
103 3083697 : buffer->push_back(value);
104 3083697 : }
105 :
106 229677 : void Marshal::write16(std::vector< unsigned char > * buffer, sal_uInt16 value) {
107 229677 : write8(buffer, value >> 8);
108 229677 : write8(buffer, value & 0xFF);
109 229677 : }
110 :
111 494760 : void Marshal::write32(std::vector< unsigned char > * buffer, sal_uInt32 value) {
112 494760 : write8(buffer, value >> 24);
113 494760 : write8(buffer, (value >> 16) & 0xFF);
114 494760 : write8(buffer, (value >> 8) & 0xFF);
115 494760 : write8(buffer, value & 0xFF);
116 494760 : }
117 :
118 206903 : 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 206903 : writeValue(buffer, type, value.getValue(type));
127 206903 : }
128 :
129 154713 : void Marshal::writeType(
130 : std::vector< unsigned char > * buffer,
131 : css::uno::TypeDescription const & value)
132 : {
133 154713 : value.makeComplete();
134 : assert(value.is());
135 154713 : typelib_TypeClass tc = value.get()->eTypeClass;
136 154713 : if (tc <= typelib_TypeClass_ANY) {
137 110497 : write8(buffer, static_cast< sal_uInt8 >(tc));
138 : } else {
139 : bool found;
140 44216 : sal_uInt16 idx = state_.typeCache.add(value, &found);
141 44216 : if (found) {
142 42542 : write8(buffer, static_cast< sal_uInt8 >(tc));
143 42542 : write16(buffer, idx);
144 : } else {
145 1674 : write8(buffer, static_cast< sal_uInt8 >(tc) | 0x80);
146 1674 : write16(buffer, idx);
147 1674 : writeString(buffer, OUString(value.get()->pTypeName));
148 : }
149 : }
150 154713 : }
151 :
152 86055 : void Marshal::writeOid(
153 : std::vector< unsigned char > * buffer, OUString const & oid)
154 : {
155 : bool found;
156 : sal_uInt16 idx;
157 86055 : if (oid.getLength() == 0) {
158 8438 : found = true;
159 8438 : idx = cache::ignore;
160 : } else {
161 77617 : idx = state_.oidCache.add(oid, &found);
162 : }
163 86055 : if (found) {
164 50641 : write8(buffer, 0);
165 : } else {
166 35414 : writeString(buffer, oid);
167 : }
168 86055 : write16(buffer, idx);
169 86055 : }
170 :
171 6762 : void Marshal::writeTid(
172 : std::vector< unsigned char > * buffer, rtl::ByteSequence const & tid)
173 : {
174 : bool found;
175 6762 : sal_uInt16 idx = state_.tidCache.add(tid, &found);
176 6762 : if (found) {
177 6561 : write8(buffer, 0);
178 : } else {
179 201 : sal_Sequence * p = tid.getHandle();
180 : writeValue(
181 : buffer,
182 : css::uno::TypeDescription(
183 201 : cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()), &p);
184 : }
185 6762 : write16(buffer, idx);
186 6762 : }
187 :
188 662745 : 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 662745 : type.makeComplete();
194 662745 : switch (type.get()->eTypeClass) {
195 : case typelib_TypeClass_VOID:
196 35851 : break;
197 : case typelib_TypeClass_BOOLEAN:
198 : assert(*static_cast< sal_uInt8 const * >(value) <= 1);
199 : // fall through
200 : case typelib_TypeClass_BYTE:
201 66270 : write8(buffer, *static_cast< sal_uInt8 const * >(value));
202 66270 : break;
203 : case typelib_TypeClass_SHORT:
204 : case typelib_TypeClass_UNSIGNED_SHORT:
205 : case typelib_TypeClass_CHAR:
206 92644 : write16(buffer, *static_cast< sal_uInt16 const * >(value));
207 92644 : break;
208 : case typelib_TypeClass_LONG:
209 : case typelib_TypeClass_UNSIGNED_LONG:
210 : case typelib_TypeClass_FLOAT:
211 : case typelib_TypeClass_ENUM:
212 82728 : write32(buffer, *static_cast< sal_uInt32 const * >(value));
213 82728 : break;
214 : case typelib_TypeClass_HYPER:
215 : case typelib_TypeClass_UNSIGNED_HYPER:
216 : case typelib_TypeClass_DOUBLE:
217 1742 : write64(buffer, *static_cast< sal_uInt64 const * >(value));
218 1742 : break;
219 : case typelib_TypeClass_STRING:
220 : writeString(
221 : buffer,
222 94405 : OUString(*static_cast< rtl_uString * const * >(value)));
223 94405 : break;
224 : case typelib_TypeClass_TYPE:
225 : writeType(
226 : buffer,
227 : css::uno::TypeDescription(
228 : *static_cast< typelib_TypeDescriptionReference * const * >(
229 20086 : value)));
230 20086 : break;
231 : case typelib_TypeClass_ANY:
232 : {
233 132828 : uno_Any const * p = static_cast< uno_Any const * >(value);
234 132828 : css::uno::TypeDescription t(p->pType);
235 132828 : writeType(buffer, t);
236 132828 : writeValue(buffer, t, p->pData);
237 132828 : break;
238 : }
239 : case typelib_TypeClass_SEQUENCE:
240 : {
241 9816 : sal_Sequence * p = *static_cast< sal_Sequence * const * >(value);
242 9816 : writeCompressed(buffer, static_cast< sal_uInt32 >(p->nElements));
243 : css::uno::TypeDescription ctd(
244 : reinterpret_cast< typelib_IndirectTypeDescription * >(
245 9816 : type.get())->
246 9816 : pType);
247 : assert(ctd.is());
248 9816 : if (ctd.get()->eTypeClass == typelib_TypeClass_BYTE) {
249 : buffer->insert(
250 322 : buffer->end(), p->elements, p->elements + p->nElements);
251 : } else {
252 108688 : for (sal_Int32 i = 0; i != p->nElements; ++i) {
253 99194 : writeValue(buffer, ctd, p->elements + i * ctd.get()->nSize);
254 : }
255 : }
256 9816 : break;
257 : }
258 : case typelib_TypeClass_STRUCT:
259 : case typelib_TypeClass_EXCEPTION:
260 42594 : writeMemberValues(buffer, type, value);
261 42594 : 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 83781 : type));
269 83781 : break;
270 : default:
271 : assert(false); // this cannot happen
272 0 : break;
273 : }
274 662745 : }
275 :
276 49668 : 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 49668 : type.makeComplete();
286 : typelib_CompoundTypeDescription * ctd =
287 49668 : reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
288 49668 : if (ctd->pBaseTypeDescription != 0) {
289 : writeMemberValues(
290 : buffer,
291 : css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
292 7074 : aggregateValue);
293 : }
294 273287 : for (sal_Int32 i = 0; i != ctd->nMembers; ++i) {
295 : writeValue(
296 223619 : buffer, css::uno::TypeDescription(ctd->ppTypeRefs[i]),
297 223619 : (static_cast< char const * >(aggregateValue) +
298 447238 : ctd->pMemberOffsets[i]));
299 : }
300 49668 : }
301 :
302 : }
303 :
304 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|