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

Generated by: LCOV version 1.11