Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / binaryurp / source / marshal.cxx
blob476fb19b13aa4427631de6cef58442f412e68f4a
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 "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"
43 #include "binaryany.hxx"
44 #include "bridge.hxx"
45 #include "cache.hxx"
46 #include "lessoperators.hxx"
47 #include "marshal.hxx"
49 namespace binaryurp {
51 namespace {
53 void write64(std::vector< unsigned char > * buffer, sal_uInt64 value) {
54 Marshal::write8(buffer, value >> 56);
55 Marshal::write8(buffer, (value >> 48) & 0xFF);
56 Marshal::write8(buffer, (value >> 40) & 0xFF);
57 Marshal::write8(buffer, (value >> 32) & 0xFF);
58 Marshal::write8(buffer, (value >> 24) & 0xFF);
59 Marshal::write8(buffer, (value >> 16) & 0xFF);
60 Marshal::write8(buffer, (value >> 8) & 0xFF);
61 Marshal::write8(buffer, value & 0xFF);
64 void writeCompressed(std::vector< unsigned char > * buffer, sal_uInt32 value) {
65 if (value < 0xFF) {
66 Marshal::write8(buffer, static_cast< sal_uInt8 >(value));
67 } else {
68 Marshal::write8(buffer, 0xFF);
69 Marshal::write32(buffer, value);
73 void writeString(
74 std::vector< unsigned char > * buffer, OUString const & value)
76 assert(buffer != 0);
77 OString v;
78 if (!value.convertToString(
79 &v, RTL_TEXTENCODING_UTF8,
80 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
81 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
83 throw css::uno::RuntimeException(
84 "UNO string contains invalid UTF-16 sequence",
85 css::uno::Reference< css::uno::XInterface >());
87 writeCompressed(buffer, static_cast< sal_uInt32 >(v.getLength()));
88 buffer->insert(buffer->end(), v.getStr(), v.getStr() + v.getLength());
93 Marshal::Marshal(rtl::Reference< Bridge > const & bridge, WriterState & state):
94 bridge_(bridge), state_(state)
96 assert(bridge.is());
99 Marshal::~Marshal() {}
101 void Marshal::write8(std::vector< unsigned char > * buffer, sal_uInt8 value) {
102 assert(buffer != 0);
103 buffer->push_back(value);
106 void Marshal::write16(std::vector< unsigned char > * buffer, sal_uInt16 value) {
107 write8(buffer, value >> 8);
108 write8(buffer, value & 0xFF);
111 void Marshal::write32(std::vector< unsigned char > * buffer, sal_uInt32 value) {
112 write8(buffer, value >> 24);
113 write8(buffer, (value >> 16) & 0xFF);
114 write8(buffer, (value >> 8) & 0xFF);
115 write8(buffer, value & 0xFF);
118 void Marshal::writeValue(
119 std::vector< unsigned char > * buffer,
120 css::uno::TypeDescription const & type, BinaryAny const & value)
122 assert(
123 type.is() &&
124 (type.get()->eTypeClass == typelib_TypeClass_ANY ||
125 value.getType().equals(type)));
126 writeValue(buffer, type, value.getValue(type));
129 void Marshal::writeType(
130 std::vector< unsigned char > * buffer,
131 css::uno::TypeDescription const & value)
133 value.makeComplete();
134 assert(value.is());
135 typelib_TypeClass tc = value.get()->eTypeClass;
136 if (tc <= typelib_TypeClass_ANY) {
137 write8(buffer, static_cast< sal_uInt8 >(tc));
138 } else {
139 bool found;
140 sal_uInt16 idx = state_.typeCache.add(value, &found);
141 if (found) {
142 write8(buffer, static_cast< sal_uInt8 >(tc));
143 write16(buffer, idx);
144 } else {
145 write8(buffer, static_cast< sal_uInt8 >(tc) | 0x80);
146 write16(buffer, idx);
147 writeString(buffer, OUString(value.get()->pTypeName));
152 void Marshal::writeOid(
153 std::vector< unsigned char > * buffer, OUString const & oid)
155 bool found;
156 sal_uInt16 idx;
157 if (oid.getLength() == 0) {
158 found = true;
159 idx = cache::ignore;
160 } else {
161 idx = state_.oidCache.add(oid, &found);
163 if (found) {
164 write8(buffer, 0);
165 } else {
166 writeString(buffer, oid);
168 write16(buffer, idx);
171 void Marshal::writeTid(
172 std::vector< unsigned char > * buffer, rtl::ByteSequence const & tid)
174 bool found;
175 sal_uInt16 idx = state_.tidCache.add(tid, &found);
176 if (found) {
177 write8(buffer, 0);
178 } else {
179 sal_Sequence * p = tid.getHandle();
180 writeValue(
181 buffer,
182 css::uno::TypeDescription(
183 cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()), &p);
185 write16(buffer, idx);
188 void Marshal::writeValue(
189 std::vector< unsigned char > * buffer,
190 css::uno::TypeDescription const & type, void const * value)
192 assert(buffer != 0 && type.is());
193 type.makeComplete();
194 switch (type.get()->eTypeClass) {
195 case typelib_TypeClass_VOID:
196 break;
197 case typelib_TypeClass_BOOLEAN:
198 assert(*static_cast< sal_uInt8 const * >(value) <= 1);
199 // fall through
200 case typelib_TypeClass_BYTE:
201 write8(buffer, *static_cast< sal_uInt8 const * >(value));
202 break;
203 case typelib_TypeClass_SHORT:
204 case typelib_TypeClass_UNSIGNED_SHORT:
205 case typelib_TypeClass_CHAR:
206 write16(buffer, *static_cast< sal_uInt16 const * >(value));
207 break;
208 case typelib_TypeClass_LONG:
209 case typelib_TypeClass_UNSIGNED_LONG:
210 case typelib_TypeClass_FLOAT:
211 case typelib_TypeClass_ENUM:
212 write32(buffer, *static_cast< sal_uInt32 const * >(value));
213 break;
214 case typelib_TypeClass_HYPER:
215 case typelib_TypeClass_UNSIGNED_HYPER:
216 case typelib_TypeClass_DOUBLE:
217 write64(buffer, *static_cast< sal_uInt64 const * >(value));
218 break;
219 case typelib_TypeClass_STRING:
220 writeString(
221 buffer,
222 OUString(*static_cast< rtl_uString * const * >(value)));
223 break;
224 case typelib_TypeClass_TYPE:
225 writeType(
226 buffer,
227 css::uno::TypeDescription(
228 *static_cast< typelib_TypeDescriptionReference * const * >(
229 value)));
230 break;
231 case typelib_TypeClass_ANY:
233 uno_Any const * p = static_cast< uno_Any const * >(value);
234 css::uno::TypeDescription t(p->pType);
235 writeType(buffer, t);
236 writeValue(buffer, t, p->pData);
237 break;
239 case typelib_TypeClass_SEQUENCE:
241 sal_Sequence * p = *static_cast< sal_Sequence * const * >(value);
242 writeCompressed(buffer, static_cast< sal_uInt32 >(p->nElements));
243 css::uno::TypeDescription ctd(
244 reinterpret_cast< typelib_IndirectTypeDescription * >(
245 type.get())->
246 pType);
247 assert(ctd.is());
248 if (ctd.get()->eTypeClass == typelib_TypeClass_BYTE) {
249 buffer->insert(
250 buffer->end(), p->elements, p->elements + p->nElements);
251 } else {
252 for (sal_Int32 i = 0; i != p->nElements; ++i) {
253 writeValue(buffer, ctd, p->elements + i * ctd.get()->nSize);
256 break;
258 case typelib_TypeClass_STRUCT:
259 case typelib_TypeClass_EXCEPTION:
260 writeMemberValues(buffer, type, value);
261 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 type));
269 break;
270 default:
271 assert(false); // this cannot happen
272 break;
276 void Marshal::writeMemberValues(
277 std::vector< unsigned char > * buffer,
278 css::uno::TypeDescription const & type, void const * aggregateValue)
280 assert(
281 type.is() &&
282 (type.get()->eTypeClass == typelib_TypeClass_STRUCT ||
283 type.get()->eTypeClass == typelib_TypeClass_EXCEPTION) &&
284 aggregateValue != 0);
285 type.makeComplete();
286 typelib_CompoundTypeDescription * ctd =
287 reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
288 if (ctd->pBaseTypeDescription != 0) {
289 writeMemberValues(
290 buffer,
291 css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
292 aggregateValue);
294 for (sal_Int32 i = 0; i != ctd->nMembers; ++i) {
295 writeValue(
296 buffer, css::uno::TypeDescription(ctd->ppTypeRefs[i]),
297 (static_cast< char const * >(aggregateValue) +
298 ctd->pMemberOffsets[i]));
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */