LCOV - code coverage report
Current view: top level - binaryurp/source - unmarshal.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 191 211 90.5 %
Date: 2015-06-13 12:38:46 Functions: 19 19 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11