summaryrefslogtreecommitdiffstats
path: root/binaryurp/source/marshal.cxx
blob: 7d60cbf4de8a711beeae0eb963a4f5055a61800b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#include <cassert>
#include <vector>

#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/Sequence.hxx>
#include <cppu/unotype.hxx>
#include <rtl/byteseq.hxx>
#include <rtl/string.hxx>
#include <rtl/textcvt.h>
#include <rtl/textenc.h>
#include <rtl/ustring.h>
#include <rtl/ustring.hxx>
#include <sal/types.h>
#include <typelib/typeclass.h>
#include <typelib/typedescription.h>
#include <typelib/typedescription.hxx>
#include <uno/dispatcher.hxx>

#include "binaryany.hxx"
#include "bridge.hxx"
#include "cache.hxx"
#include "lessoperators.hxx"
#include "marshal.hxx"

namespace binaryurp {

namespace {

void write64(std::vector< unsigned char > * buffer, sal_uInt64 value) {
    Marshal::write8(buffer, value >> 56);
    Marshal::write8(buffer, (value >> 48) & 0xFF);
    Marshal::write8(buffer, (value >> 40) & 0xFF);
    Marshal::write8(buffer, (value >> 32) & 0xFF);
    Marshal::write8(buffer, (value >> 24) & 0xFF);
    Marshal::write8(buffer, (value >> 16) & 0xFF);
    Marshal::write8(buffer, (value >> 8) & 0xFF);
    Marshal::write8(buffer, value & 0xFF);
}

void writeCompressed(std::vector< unsigned char > * buffer, sal_uInt32 value) {
    if (value < 0xFF) {
        Marshal::write8(buffer, static_cast< sal_uInt8 >(value));
    } else {
        Marshal::write8(buffer, 0xFF);
        Marshal::write32(buffer, value);
    }
}

void writeString(
    std::vector< unsigned char > * buffer, OUString const & value)
{
    assert(buffer != nullptr);
    OString v;
    if (!value.convertToString(
            &v, RTL_TEXTENCODING_UTF8,
            (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
             RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
    {
        throw css::uno::RuntimeException(
            "UNO string contains invalid UTF-16 sequence");
    }
    writeCompressed(buffer, static_cast< sal_uInt32 >(v.getLength()));
    buffer->insert(buffer->end(), v.getStr(), v.getStr() + v.getLength());
}

}

Marshal::Marshal(rtl::Reference< Bridge > const & bridge, WriterState & state):
    bridge_(bridge), state_(state)
{
    assert(bridge.is());
}

Marshal::~Marshal() {}

void Marshal::write8(std::vector< unsigned char > * buffer, sal_uInt8 value) {
    assert(buffer != nullptr);
    buffer->push_back(value);
}

void Marshal::write16(std::vector< unsigned char > * buffer, sal_uInt16 value) {
    write8(buffer, value >> 8);
    write8(buffer, value & 0xFF);
}

void Marshal::write32(std::vector< unsigned char > * buffer, sal_uInt32 value) {
    write8(buffer, value >> 24);
    write8(buffer, (value >> 16) & 0xFF);
    write8(buffer, (value >> 8) & 0xFF);
    write8(buffer, value & 0xFF);
}

void Marshal::writeValue(
    std::vector< unsigned char > * buffer,
    css::uno::TypeDescription const & type, BinaryAny const & value)
{
    assert(
        type.is() &&
        (type.get()->eTypeClass == typelib_TypeClass_ANY ||
         value.getType().equals(type)));
    writeValue(buffer, type, value.getValue(type));
}

void Marshal::writeType(
    std::vector< unsigned char > * buffer,
    css::uno::TypeDescription const & value)
{
    value.makeComplete();
    assert(value.is());
    typelib_TypeClass tc = value.get()->eTypeClass;
    if (tc <= typelib_TypeClass_ANY) {
        write8(buffer, static_cast< sal_uInt8 >(tc));
    } else {
        bool found;
        sal_uInt16 idx = state_.typeCache.add(value, &found);
        if (found) {
            write8(buffer, static_cast< sal_uInt8 >(tc));
            write16(buffer, idx);
        } else {
            write8(buffer, static_cast< sal_uInt8 >(tc) | 0x80);
            write16(buffer, idx);
            writeString(buffer, OUString(value.get()->pTypeName));
        }
    }
}

void Marshal::writeOid(
    std::vector< unsigned char > * buffer, OUString const & oid)
{
    bool found;
    sal_uInt16 idx;
    if ( oid.isEmpty() ) {
        found = true;
        idx = cache::ignore;
    } else {
        idx = state_.oidCache.add(oid, &found);
    }
    if (found) {
        write8(buffer, 0);
    } else {
        writeString(buffer, oid);
    }
    write16(buffer, idx);
}

void Marshal::writeTid(
    std::vector< unsigned char > * buffer, rtl::ByteSequence const & tid)
{
    bool found;
    sal_uInt16 idx = state_.tidCache.add(tid, &found);
    if (found) {
        write8(buffer, 0);
    } else {
        sal_Sequence * p = tid.getHandle();
        writeValue(
            buffer,
            css::uno::TypeDescription(
                cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()), &p);
    }
    write16(buffer, idx);
}

void Marshal::writeValue(
    std::vector< unsigned char > * buffer,
    css::uno::TypeDescription const & type, void const * value)
{
    assert(buffer != nullptr && type.is());
    type.makeComplete();
    switch (type.get()->eTypeClass) {
    case typelib_TypeClass_VOID:
        break;
    case typelib_TypeClass_BOOLEAN:
        assert(*static_cast< sal_uInt8 const * >(value) <= 1);
        [[fallthrough]];
    case typelib_TypeClass_BYTE:
        write8(buffer, *static_cast< sal_uInt8 const * >(value));
        break;
    case typelib_TypeClass_SHORT:
    case typelib_TypeClass_UNSIGNED_SHORT:
    case typelib_TypeClass_CHAR:
        write16(buffer, *static_cast< sal_uInt16 const * >(value));
        break;
    case typelib_TypeClass_LONG:
    case typelib_TypeClass_UNSIGNED_LONG:
    case typelib_TypeClass_FLOAT:
    case typelib_TypeClass_ENUM:
        write32(buffer, *static_cast< sal_uInt32 const * >(value));
        break;
    case typelib_TypeClass_HYPER:
    case typelib_TypeClass_UNSIGNED_HYPER:
    case typelib_TypeClass_DOUBLE:
        write64(buffer, *static_cast< sal_uInt64 const * >(value));
        break;
    case typelib_TypeClass_STRING:
        writeString(
            buffer,
            OUString(*static_cast< rtl_uString * const * >(value)));
        break;
    case typelib_TypeClass_TYPE:
        writeType(
            buffer,
            css::uno::TypeDescription(
                *static_cast< typelib_TypeDescriptionReference * const * >(
                    value)));
        break;
    case typelib_TypeClass_ANY:
        {
            uno_Any const * p = static_cast< uno_Any const * >(value);
            css::uno::TypeDescription t(p->pType);
            writeType(buffer, t);
            writeValue(buffer, t, p->pData);
            break;
        }
    case typelib_TypeClass_SEQUENCE:
        {
            sal_Sequence * p = *static_cast< sal_Sequence * const * >(value);
            writeCompressed(buffer, static_cast< sal_uInt32 >(p->nElements));
            css::uno::TypeDescription ctd(
                reinterpret_cast< typelib_IndirectTypeDescription * >(
                    type.get())->
                pType);
            assert(ctd.is());
            if (ctd.get()->eTypeClass == typelib_TypeClass_BYTE) {
                buffer->insert(
                    buffer->end(), p->elements, p->elements + p->nElements);
            } else {
                for (sal_Int32 i = 0; i != p->nElements; ++i) {
                    writeValue(buffer, ctd, p->elements + i * ctd.get()->nSize);
                }
            }
            break;
        }
    case typelib_TypeClass_STRUCT:
    case typelib_TypeClass_EXCEPTION:
        writeMemberValues(buffer, type, value);
        break;
    case typelib_TypeClass_INTERFACE:
        writeOid(
            buffer,
            bridge_->registerOutgoingInterface(
                css::uno::UnoInterfaceReference(
                    *static_cast< uno_Interface * const * >(value)),
                type));
        break;
    default:
        assert(false); // this cannot happen
        break;
    }
}

void Marshal::writeMemberValues(
    std::vector< unsigned char > * buffer,
    css::uno::TypeDescription const & type, void const * aggregateValue)
{
    assert(
        type.is() &&
        (type.get()->eTypeClass == typelib_TypeClass_STRUCT ||
         type.get()->eTypeClass == typelib_TypeClass_EXCEPTION) &&
        aggregateValue != nullptr);
    type.makeComplete();
    typelib_CompoundTypeDescription * ctd =
        reinterpret_cast< typelib_CompoundTypeDescription * >(type.get());
    if (ctd->pBaseTypeDescription != nullptr) {
        writeMemberValues(
            buffer,
            css::uno::TypeDescription(&ctd->pBaseTypeDescription->aBase),
            aggregateValue);
    }
    for (sal_Int32 i = 0; i != ctd->nMembers; ++i) {
        writeValue(
            buffer, css::uno::TypeDescription(ctd->ppTypeRefs[i]),
            (static_cast< char const * >(aggregateValue) +
             ctd->pMemberOffsets[i]));
    }
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */