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 <cstdlib>
24 : #include <new>
25 : #include <vector>
26 :
27 : #include "boost/noncopyable.hpp"
28 : #include "com/sun/star/io/IOException.hpp"
29 : #include "com/sun/star/uno/Reference.hxx"
30 : #include "com/sun/star/uno/RuntimeException.hpp"
31 : #include "com/sun/star/uno/Sequence.hxx"
32 : #include "com/sun/star/uno/XInterface.hpp"
33 : #include "cppu/unotype.hxx"
34 : #include "rtl/byteseq.hxx"
35 : #include "rtl/ref.hxx"
36 : #include "rtl/textcvt.h"
37 : #include "rtl/textenc.h"
38 : #include "rtl/ustring.h"
39 : #include "rtl/ustring.hxx"
40 : #include "sal/types.h"
41 : #include "typelib/typeclass.h"
42 : #include "typelib/typedescription.h"
43 : #include "typelib/typedescription.hxx"
44 : #include "uno/any2.h"
45 : #include "uno/data.h"
46 : #include "uno/dispatcher.hxx"
47 :
48 : #include "binaryany.hxx"
49 : #include "bridge.hxx"
50 : #include "cache.hxx"
51 : #include "readerstate.hxx"
52 : #include "unmarshal.hxx"
53 :
54 : namespace binaryurp {
55 :
56 : namespace {
57 :
58 24885 : void * allocate(sal_Size size) {
59 24885 : void * p = rtl_allocateMemory(size);
60 24885 : if (p == 0) {
61 0 : throw std::bad_alloc();
62 : }
63 24885 : return p;
64 : }
65 :
66 16941 : std::vector< BinaryAny >::iterator copyMemberValues(
67 : css::uno::TypeDescription const & type,
68 : std::vector< BinaryAny >::iterator const & it, void * buffer) throw ()
69 : {
70 : assert(
71 : type.is() &&
72 : (type.get()->eTypeClass == typelib_TypeClass_STRUCT ||
73 : type.get()->eTypeClass == typelib_TypeClass_EXCEPTION) &&
74 : buffer != 0);
75 16941 : type.makeComplete();
76 16941 : std::vector< BinaryAny >::iterator i(it);
77 : typelib_CompoundTypeDescription * ctd =
78 16941 : reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
79 16941 : if (ctd->pBaseTypeDescription != 0) {
80 : i = copyMemberValues(
81 : css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase), i,
82 87 : buffer);
83 : }
84 71883 : for (sal_Int32 j = 0; j != ctd->nMembers; ++j) {
85 : uno_type_copyData(
86 109884 : static_cast< char * >(buffer) + ctd->pMemberOffsets[j],
87 : const_cast< void * >(
88 109884 : i++->getValue(css::uno::TypeDescription(ctd->ppTypeRefs[j]))),
89 164826 : ctd->ppTypeRefs[j], 0);
90 : }
91 16941 : return i;
92 : }
93 :
94 : }
95 :
96 502794 : Unmarshal::Unmarshal(
97 : rtl::Reference< Bridge > const & bridge, ReaderState & state,
98 : css::uno::Sequence< sal_Int8 > const & buffer):
99 502794 : bridge_(bridge), state_(state), buffer_(buffer)
100 : {
101 502794 : data_ = reinterpret_cast< sal_uInt8 const * >(buffer_.getConstArray());
102 502794 : end_ = data_ + buffer_.getLength();
103 502794 : }
104 :
105 502794 : Unmarshal::~Unmarshal() {}
106 :
107 1234266 : sal_uInt8 Unmarshal::read8() {
108 1234266 : check(1);
109 1234266 : return *data_++;
110 : }
111 :
112 554687 : sal_uInt16 Unmarshal::read16() {
113 554687 : check(2);
114 554687 : sal_uInt16 n = static_cast< sal_uInt16 >(*data_++) << 8;
115 554687 : return n | *data_++;
116 : }
117 :
118 562517 : sal_uInt32 Unmarshal::read32() {
119 562517 : check(4);
120 562517 : sal_uInt32 n = static_cast< sal_uInt32 >(*data_++) << 24;
121 562517 : n |= static_cast< sal_uInt32 >(*data_++) << 16;
122 562517 : n |= static_cast< sal_uInt32 >(*data_++) << 8;
123 562517 : return n | *data_++;
124 : }
125 :
126 233381 : css::uno::TypeDescription Unmarshal::readType() {
127 233381 : sal_uInt8 flags = read8();
128 233381 : typelib_TypeClass tc = static_cast< typelib_TypeClass >(flags & 0x7F);
129 233381 : switch (tc) {
130 : case typelib_TypeClass_VOID:
131 : case typelib_TypeClass_BOOLEAN:
132 : case typelib_TypeClass_BYTE:
133 : case typelib_TypeClass_SHORT:
134 : case typelib_TypeClass_UNSIGNED_SHORT:
135 : case typelib_TypeClass_LONG:
136 : case typelib_TypeClass_UNSIGNED_LONG:
137 : case typelib_TypeClass_HYPER:
138 : case typelib_TypeClass_UNSIGNED_HYPER:
139 : case typelib_TypeClass_FLOAT:
140 : case typelib_TypeClass_DOUBLE:
141 : case typelib_TypeClass_CHAR:
142 : case typelib_TypeClass_STRING:
143 : case typelib_TypeClass_TYPE:
144 : case typelib_TypeClass_ANY:
145 79897 : if ((flags & 0x80) != 0) {
146 : throw css::io::IOException(
147 : "binaryurp::Unmarshal: cache flag of simple type is set",
148 0 : css::uno::Reference< css::uno::XInterface >());
149 : }
150 : return css::uno::TypeDescription(
151 : *typelib_static_type_getByTypeClass(
152 79897 : static_cast< typelib_TypeClass >(tc)));
153 : case typelib_TypeClass_SEQUENCE:
154 : case typelib_TypeClass_ENUM:
155 : case typelib_TypeClass_STRUCT:
156 : case typelib_TypeClass_EXCEPTION:
157 : case typelib_TypeClass_INTERFACE:
158 : {
159 153484 : sal_uInt16 idx = readCacheIndex();
160 153484 : if ((flags & 0x80) == 0) {
161 151684 : if (idx == cache::ignore || !state_.typeCache[idx].is()) {
162 : throw css::io::IOException(
163 : "binaryurp::Unmarshal: unknown type cache index",
164 0 : css::uno::Reference< css::uno::XInterface >());
165 : }
166 151684 : return state_.typeCache[idx];
167 : } else {
168 1800 : OUString const str(readString());
169 3600 : css::uno::TypeDescription t(str);
170 3600 : if (!t.is() ||
171 1800 : t.get()->eTypeClass != static_cast< typelib_TypeClass >(tc))
172 : {
173 :
174 : throw css::io::IOException(
175 0 : "binaryurp::Unmarshal: type with unknown name: " + str,
176 0 : css::uno::Reference< css::uno::XInterface >());
177 : }
178 3634 : for (css::uno::TypeDescription t2(t);
179 1834 : t2.get()->eTypeClass == typelib_TypeClass_SEQUENCE;)
180 : {
181 35 : t2.makeComplete();
182 70 : t2 = css::uno::TypeDescription(
183 : reinterpret_cast< typelib_IndirectTypeDescription * >(
184 70 : t2.get())->pType);
185 35 : if (!t2.is()) {
186 : throw css::io::IOException(
187 : ("binaryurp::Unmarshal: sequence type with unknown"
188 : " component type"),
189 0 : css::uno::Reference< css::uno::XInterface >());
190 : }
191 35 : switch (t2.get()->eTypeClass) {
192 : case typelib_TypeClass_VOID:
193 : case typelib_TypeClass_EXCEPTION:
194 : throw css::io::IOException(
195 : ("binaryurp::Unmarshal: sequence type with bad"
196 : " component type"),
197 1 : css::uno::Reference< css::uno::XInterface >());
198 : default:
199 34 : break;
200 : }
201 1800 : }
202 1799 : if (idx != cache::ignore) {
203 1798 : state_.typeCache[idx] = t;
204 : }
205 3599 : return t;
206 : }
207 : }
208 : default:
209 : throw css::io::IOException(
210 : "binaryurp::Unmarshal: type of unknown type class",
211 0 : css::uno::Reference< css::uno::XInterface >());
212 : }
213 : }
214 :
215 312284 : OUString Unmarshal::readOid() {
216 312284 : OUString oid(readString());
217 2740443 : for (sal_Int32 i = 0; i != oid.getLength(); ++i) {
218 2428159 : if (oid[i] > 0x7F) {
219 : throw css::io::IOException(
220 : "binaryurp::Unmarshal: OID contains non-ASCII character",
221 0 : css::uno::Reference< css::uno::XInterface >());
222 : }
223 : }
224 312284 : sal_uInt16 idx = readCacheIndex();
225 312284 : if (oid.isEmpty() && idx != cache::ignore) {
226 66579 : if (state_.oidCache[idx].isEmpty()) {
227 : throw css::io::IOException(
228 : "binaryurp::Unmarshal: unknown OID cache index",
229 0 : css::uno::Reference< css::uno::XInterface >());
230 : }
231 66579 : return state_.oidCache[idx];
232 : }
233 245705 : if (idx != cache::ignore) {
234 50621 : state_.oidCache[idx] = oid;
235 : }
236 245705 : return oid;
237 : }
238 :
239 47860 : rtl::ByteSequence Unmarshal::readTid() {
240 : rtl::ByteSequence tid(
241 : *static_cast< sal_Sequence * const * >(
242 : readSequence(
243 : css::uno::TypeDescription(
244 47860 : cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get())).
245 : getValue(
246 : css::uno::TypeDescription(
247 95720 : cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()))));
248 47860 : sal_uInt16 idx = readCacheIndex();
249 47860 : if (tid.getLength() == 0) {
250 47573 : if (idx == cache::ignore || state_.tidCache[idx].getLength() == 0) {
251 : throw css::io::IOException(
252 : "binaryurp::Unmarshal: unknown TID cache index",
253 0 : css::uno::Reference< css::uno::XInterface >());
254 : }
255 47573 : return state_.tidCache[idx];
256 : }
257 287 : if (idx != cache::ignore) {
258 287 : state_.tidCache[idx] = tid;
259 : }
260 287 : return tid;
261 : }
262 :
263 686122 : BinaryAny Unmarshal::readValue(css::uno::TypeDescription const & type) {
264 : assert(type.is());
265 686122 : switch (type.get()->eTypeClass) {
266 : default:
267 0 : std::abort(); // this cannot happen
268 : // pseudo fall-through to avoid compiler warnings
269 : case typelib_TypeClass_VOID:
270 5312 : return BinaryAny();
271 : case typelib_TypeClass_BOOLEAN:
272 : {
273 26059 : sal_uInt8 v = read8();
274 26059 : if (v > 1) {
275 : throw css::io::IOException(
276 : "binaryurp::Unmarshal: boolean of unknown value",
277 0 : css::uno::Reference< css::uno::XInterface >());
278 : }
279 26059 : return BinaryAny(type, &v);
280 : }
281 : case typelib_TypeClass_BYTE:
282 : {
283 23 : sal_uInt8 v = read8();
284 23 : return BinaryAny(type, &v);
285 : }
286 : case typelib_TypeClass_SHORT:
287 : case typelib_TypeClass_UNSIGNED_SHORT:
288 : case typelib_TypeClass_CHAR:
289 : {
290 41059 : sal_uInt16 v = read16();
291 41059 : return BinaryAny(type, &v);
292 : }
293 : case typelib_TypeClass_LONG:
294 : case typelib_TypeClass_UNSIGNED_LONG:
295 : case typelib_TypeClass_FLOAT:
296 : {
297 57278 : sal_uInt32 v = read32();
298 57278 : return BinaryAny(type, &v);
299 : }
300 : case typelib_TypeClass_HYPER:
301 : case typelib_TypeClass_UNSIGNED_HYPER:
302 : case typelib_TypeClass_DOUBLE:
303 : {
304 22299 : sal_uInt64 v = read64();
305 22299 : return BinaryAny(type, &v);
306 : }
307 : case typelib_TypeClass_STRING:
308 : {
309 193975 : OUString v(readString());
310 193975 : return BinaryAny(type, &v.pData);
311 : }
312 : case typelib_TypeClass_TYPE:
313 : {
314 21917 : css::uno::TypeDescription v(readType());
315 21917 : typelib_TypeDescription * p = v.get();
316 21917 : return BinaryAny(type, &p);
317 : }
318 : case typelib_TypeClass_ANY:
319 : {
320 83843 : css::uno::TypeDescription t(readType());
321 83843 : if (t.get()->eTypeClass == typelib_TypeClass_ANY) {
322 : throw css::io::IOException(
323 : "binaryurp::Unmarshal: any of type ANY",
324 0 : css::uno::Reference< css::uno::XInterface >());
325 : }
326 83843 : return readValue(t);
327 : }
328 : case typelib_TypeClass_SEQUENCE:
329 8871 : type.makeComplete();
330 8871 : return readSequence(type);
331 : case typelib_TypeClass_ENUM:
332 : {
333 2431 : sal_Int32 v = static_cast< sal_Int32 >(read32());
334 2431 : type.makeComplete();
335 : typelib_EnumTypeDescription * etd =
336 2431 : reinterpret_cast< typelib_EnumTypeDescription * >(type.get());
337 2431 : bool found = false;
338 7158 : for (sal_Int32 i = 0; i != etd->nEnumValues; ++i) {
339 7158 : if (etd->pEnumValues[i] == v) {
340 2431 : found = true;
341 2431 : break;
342 : }
343 : }
344 2431 : if (!found) {
345 : throw css::io::IOException(
346 : "binaryurp::Unmarshal: unknown enum value",
347 0 : css::uno::Reference< css::uno::XInterface >());
348 : }
349 2431 : return BinaryAny(type, &v);
350 : }
351 : case typelib_TypeClass_STRUCT:
352 : case typelib_TypeClass_EXCEPTION:
353 : {
354 16854 : std::vector< BinaryAny > as;
355 16854 : readMemberValues(type, &as);
356 16854 : void * buf = allocate(type.get()->nSize);
357 16854 : copyMemberValues(type, as.begin(), buf);
358 : uno_Any raw;
359 : raw.pType = reinterpret_cast< typelib_TypeDescriptionReference * >(
360 16854 : type.get());
361 16854 : raw.pData = buf;
362 16854 : raw.pReserved = 0;
363 16854 : return BinaryAny(raw);
364 : }
365 : case typelib_TypeClass_INTERFACE:
366 : {
367 : css::uno::UnoInterfaceReference obj(
368 206201 : bridge_->registerIncomingInterface(readOid(), type));
369 206201 : return BinaryAny(type, &obj.m_pUnoI);
370 : }
371 : }
372 : }
373 :
374 502793 : void Unmarshal::done() const {
375 502793 : if (data_ != end_) {
376 : throw css::io::IOException(
377 : "binaryurp::Unmarshal: block contains excess data",
378 0 : css::uno::Reference< css::uno::XInterface >());
379 : }
380 502793 : }
381 :
382 2882136 : void Unmarshal::check(sal_Int32 size) const {
383 2882136 : if (end_ - data_ < size) {
384 : throw css::io::IOException(
385 : "binaryurp::Unmarshal: trying to read past end of block",
386 0 : css::uno::Reference< css::uno::XInterface >());
387 : }
388 2882136 : }
389 :
390 564790 : sal_uInt32 Unmarshal::readCompressed() {
391 564790 : sal_uInt8 n = read8();
392 564790 : return n == 0xFF ? read32() : n;
393 : }
394 :
395 513628 : sal_uInt16 Unmarshal::readCacheIndex() {
396 513628 : sal_uInt16 idx = read16();
397 513628 : if (idx >= cache::size && idx != cache::ignore) {
398 : throw css::io::IOException(
399 : "binaryurp::Unmarshal: cache index out of range",
400 0 : css::uno::Reference< css::uno::XInterface >());
401 : }
402 513628 : return idx;
403 : }
404 :
405 22299 : sal_uInt64 Unmarshal::read64() {
406 22299 : check(8);
407 22299 : sal_uInt64 n = static_cast< sal_uInt64 >(*data_++) << 56;
408 22299 : n |= static_cast< sal_uInt64 >(*data_++) << 48;
409 22299 : n |= static_cast< sal_uInt64 >(*data_++) << 40;
410 22299 : n |= static_cast< sal_uInt64 >(*data_++) << 32;
411 22299 : n |= static_cast< sal_uInt64 >(*data_++) << 24;
412 22299 : n |= static_cast< sal_uInt64 >(*data_++) << 16;
413 22299 : n |= static_cast< sal_uInt64 >(*data_++) << 8;
414 22299 : return n | *data_++;
415 : }
416 :
417 508059 : OUString Unmarshal::readString() {
418 508059 : sal_uInt32 n = readCompressed();
419 508059 : if (n > SAL_MAX_INT32) {
420 : throw css::uno::RuntimeException(
421 : "binaryurp::Unmarshal: string size too large",
422 0 : css::uno::Reference< css::uno::XInterface >());
423 : }
424 508059 : check(static_cast< sal_Int32 >(n));
425 508059 : OUString s;
426 508059 : if (!rtl_convertStringToUString(
427 : &s.pData, reinterpret_cast< char const * >(data_),
428 : static_cast< sal_Int32 >(n), RTL_TEXTENCODING_UTF8,
429 : (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
430 : RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
431 508059 : RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
432 : {
433 : throw css::io::IOException(
434 : "binaryurp::Unmarshal: string does not contain UTF-8",
435 0 : css::uno::Reference< css::uno::XInterface >());
436 : }
437 508059 : data_ += n;
438 508059 : return s;
439 : }
440 :
441 56731 : BinaryAny Unmarshal::readSequence(css::uno::TypeDescription const & type) {
442 : assert(type.is() && type.get()->eTypeClass == typelib_TypeClass_SEQUENCE);
443 56731 : sal_uInt32 n = readCompressed();
444 56731 : if (n > SAL_MAX_INT32) {
445 : throw css::uno::RuntimeException(
446 : "binaryurp::Unmarshal: sequence size too large",
447 0 : css::uno::Reference< css::uno::XInterface >());
448 : }
449 56731 : if (n == 0) {
450 48392 : return BinaryAny(type, 0);
451 : }
452 : css::uno::TypeDescription ctd(
453 : reinterpret_cast< typelib_IndirectTypeDescription * >(
454 8339 : type.get())->pType);
455 8339 : if (ctd.get()->eTypeClass == typelib_TypeClass_BYTE) {
456 308 : check(static_cast< sal_Int32 >(n));
457 : rtl::ByteSequence s(
458 : reinterpret_cast< sal_Int8 const * >(data_),
459 308 : static_cast< sal_Int32 >(n));
460 308 : data_ += n;
461 308 : sal_Sequence * p = s.getHandle();
462 308 : return BinaryAny(type, &p);
463 : }
464 16062 : std::vector< BinaryAny > as;
465 194536 : for (sal_uInt32 i = 0; i != n; ++i) {
466 186505 : as.push_back(readValue(ctd));
467 : }
468 : assert(ctd.get()->nSize >= 0);
469 16062 : sal_uInt64 size = static_cast< sal_uInt64 >(n) *
470 16062 : static_cast< sal_uInt64 >(ctd.get()->nSize);
471 : // sal_uInt32 * sal_Int32 -> sal_uInt64 cannot overflow
472 8031 : if (size > SAL_MAX_SIZE - SAL_SEQUENCE_HEADER_SIZE) {
473 : throw css::uno::RuntimeException(
474 : "binaryurp::Unmarshal: sequence size too large",
475 0 : css::uno::Reference< css::uno::XInterface >());
476 : }
477 : void * buf = allocate(
478 8031 : SAL_SEQUENCE_HEADER_SIZE + static_cast< sal_Size >(size));
479 8031 : static_cast< sal_Sequence * >(buf)->nRefCount = 0;
480 : static_cast< sal_Sequence * >(buf)->nElements =
481 8031 : static_cast< sal_Int32 >(n);
482 194536 : for (sal_uInt32 i = 0; i != n; ++i) {
483 : uno_copyData(
484 373010 : static_cast< sal_Sequence * >(buf)->elements + i * ctd.get()->nSize,
485 559515 : const_cast< void * >(as[i].getValue(ctd)), ctd.get(), 0);
486 : }
487 16370 : return BinaryAny(type, reinterpret_cast< sal_Sequence ** >(&buf));
488 : }
489 :
490 16941 : void Unmarshal::readMemberValues(
491 : css::uno::TypeDescription const & type, std::vector< BinaryAny > * values)
492 : {
493 : assert(
494 : type.is() &&
495 : (type.get()->eTypeClass == typelib_TypeClass_STRUCT ||
496 : type.get()->eTypeClass == typelib_TypeClass_EXCEPTION) &&
497 : values != 0);
498 16941 : type.makeComplete();
499 : typelib_CompoundTypeDescription * ctd =
500 16941 : reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
501 16941 : if (ctd->pBaseTypeDescription != 0) {
502 : readMemberValues(
503 : css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
504 87 : values);
505 : }
506 71883 : for (sal_Int32 i = 0; i != ctd->nMembers; ++i) {
507 : values->push_back(
508 54942 : readValue(css::uno::TypeDescription(ctd->ppTypeRefs[i])));
509 : }
510 16941 : }
511 :
512 : }
513 :
514 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|