summaryrefslogtreecommitdiffstats
path: root/bridges/source/cpp_uno/cc5_solaris_sparc64/exceptions.cxx
blob: 2a55fcdf2d88a54caf2b96e0fc6dff453823f502 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* -*- 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 <cstddef>
#include <cstring>
#include <map>
#include <utility>
#include <vector>

#include "bridges/cpp_uno/shared/arraypointer.hxx"
#include "com/sun/star/uno/Reference.hxx"
#include "com/sun/star/uno/RuntimeException.hpp"
#include "com/sun/star/uno/XInterface.hpp"
#include "com/sun/star/uno/genfunc.hxx"
#include "osl/diagnose.h"
#include "osl/mutex.hxx"
#include "rtl/strbuf.hxx"
#include "rtl/string.hxx"
#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 "uno/any2.h"
#include "uno/data.h"
#include "uno/mapping.h"

#include "exceptions.hxx"
#include "flushcode.hxx"

namespace {

namespace css = com::sun::star;

typedef void (* Function)(void *);

Function toFunction(void * pointer) {
#pragma disable_warn
    return reinterpret_cast< Function >(pointer);
#pragma enable_warn
}

bool toUnoName(char const * rttiName, OUString * unoName) {
    rtl::OStringBuffer buf;
    for (;;) {
        char const * p = std::strchr(rttiName, ':');
        if (p == NULL) {
            buf.append(rttiName);
            break;
        }
        if (p - rttiName > SAL_MAX_INT32) {
            return false;
        }
        buf.append(rttiName, sal::static_int_cast< sal_Int32 >(p - rttiName));
        buf.append(".");
        while (*p == ':') {
            ++p;
        }
        rttiName = p;
    }
    *unoName = rtl::OStringToOUString(
        buf.makeStringAndClear(), RTL_TEXTENCODING_UTF8);
        //TODO: check conversion failure
    return true;
}

class NistHash {
public:
    NistHash(rtl::OString const & text);

    sal_uInt32 hashdata[5];

private:
    static sal_uInt32 f1(sal_uInt32 x, sal_uInt32 y, sal_uInt32 z)
    { return z ^ (x & (y ^ z)); }

    static sal_uInt32 f2(sal_uInt32 x, sal_uInt32 y, sal_uInt32 z)
    { return x ^ y ^ z; }

    static sal_uInt32 f3(sal_uInt32 x, sal_uInt32 y, sal_uInt32 z)
    { return (x & y) + (z & (x ^ y)); }

    static sal_uInt32 rotl(sal_uInt32 value, sal_uInt32 bits)
    { return (value << bits) | (value >> (32 - bits)); }

    sal_uInt32 expand_nostore(sal_uInt32 index) {
        return data[index & 15] ^ data[(index - 14) & 15] ^
            data[(index - 8) & 15] ^ data[(index - 3) & 15];
    }

    sal_uInt32 expand_store(sal_uInt32 index) {
        return data[index & 15] ^= data[(index - 14) & 15] ^
            data[(index - 8) & 15] ^ data[(index - 3) & 15];
    }

    void subRound(
        sal_uInt32 a, sal_uInt32 & b, sal_uInt32 c, sal_uInt32 d,
        sal_uInt32 & e, sal_uInt32 constant, sal_uInt32 datum,
        sal_uInt32 function)
    {
        e += rotl(a, 5);
        switch (function) {
            case 1:
                e += f1(b, c, d);
                break;
            case 2:
            case 4:
                e += f2(b, c, d);
                break;
            case 3:
                e += f3(b, c, d);
                break;
        }
        e += constant + datum;
        b = rotl(b, 30);
    }

    void transform();

    sal_uInt32 data[16];
};

NistHash::NistHash(rtl::OString const & text) {
    hashdata[0] = 0x67452301;
    hashdata[1] = 0xefcdab89;
    hashdata[2] = 0x98badcfe;
    hashdata[3] = 0x10325476;
    hashdata[4] = 0xc3d2e1f0;
    char const * p = text.getStr();
    sal_Int32 n = text.getLength();
    while (n >= sizeof data) {
        std::memcpy(data, p, sizeof data);
        p += sizeof data;
        n -= sizeof data;
        transform();
    }
    std::memcpy(data, p, n);
    reinterpret_cast< unsigned char *>(data)[n++] = 0x80;
    if (n > sizeof data - 8) {
        std::memset(reinterpret_cast< char * >(data) + n, 0, sizeof data - n);
        transform();
        std::memset(data, 0, sizeof data  - 8);
    } else {
        std::memset(
            reinterpret_cast< char * >(data) + n, 0, sizeof data - 8 - n);
    }
    data[14] = 0;
    data[15] = text.getLength() << 3;
    transform();
}

void NistHash::transform() {
    sal_uInt32 const K2 = 0x5A827999;
    sal_uInt32 const K3 = 0x6ED9EBA1;
    sal_uInt32 const K5 = 0x8F1BBCDC;
    sal_uInt32 const K10 = 0xCA62C1D6;
    sal_uInt32 a = hashdata[0];
    sal_uInt32 b = hashdata[1];
    sal_uInt32 c = hashdata[2];
    sal_uInt32 d = hashdata[3];
    sal_uInt32 e = hashdata[4];
    subRound(a, b, c, d, e, K2, data[ 0], 1);
    subRound(e, a, b, c, d, K2, data[ 1], 1);
    subRound(d, e, a, b, c, K2, data[ 2], 1);
    subRound(c, d, e, a, b, K2, data[ 3], 1);
    subRound(b, c, d, e, a, K2, data[ 4], 1);
    subRound(a, b, c, d, e, K2, data[ 5], 1);
    subRound(e, a, b, c, d, K2, data[ 6], 1);
    subRound(d, e, a, b, c, K2, data[ 7], 1);
    subRound(c, d, e, a, b, K2, data[ 8], 1);
    subRound(b, c, d, e, a, K2, data[ 9], 1);
    subRound(a, b, c, d, e, K2, data[10], 1);
    subRound(e, a, b, c, d, K2, data[11], 1);
    subRound(d, e, a, b, c, K2, data[12], 1);
    subRound(c, d, e, a, b, K2, data[13], 1);
    subRound(b, c, d, e, a, K2, data[14], 1);
    subRound(a, b, c, d, e, K2, data[15], 1);
    subRound(e, a, b, c, d, K2, expand_store(16), 1);
    subRound(d, e, a, b, c, K2, expand_store(17), 1);
    subRound(c, d, e, a, b, K2, expand_store(18), 1);
    subRound(b, c, d, e, a, K2, expand_store(19), 1);
    subRound(a, b, c, d, e, K3, expand_store(20), 2);
    subRound(e, a, b, c, d, K3, expand_store(21), 2);
    subRound(d, e, a, b, c, K3, expand_store(22), 2);
    subRound(c, d, e, a, b, K3, expand_store(23), 2);
    subRound(b, c, d, e, a, K3, expand_store(24), 2);
    subRound(a, b, c, d, e, K3, expand_store(25), 2);
    subRound(e, a, b, c, d, K3, expand_store(26), 2);
    subRound(d, e, a, b, c, K3, expand_store(27), 2);
    subRound(c, d, e, a, b, K3, expand_store(28), 2);
    subRound(b, c, d, e, a, K3, expand_store(29), 2);
    subRound(a, b, c, d, e, K3, expand_store(30), 2);
    subRound(e, a, b, c, d, K3, expand_store(31), 2);
    subRound(d, e, a, b, c, K3, expand_store(32), 2);
    subRound(c, d, e, a, b, K3, expand_store(33), 2);
    subRound(b, c, d, e, a, K3, expand_store(34), 2);
    subRound(a, b, c, d, e, K3, expand_store(35), 2);
    subRound(e, a, b, c, d, K3, expand_store(36), 2);
    subRound(d, e, a, b, c, K3, expand_store(37), 2);
    subRound(c, d, e, a, b, K3, expand_store(38), 2);
    subRound(b, c, d, e, a, K3, expand_store(39), 2);
    subRound(a, b, c, d, e, K5, expand_store(40), 3);
    subRound(e, a, b, c, d, K5, expand_store(41), 3);
    subRound(d, e, a, b, c, K5, expand_store(42), 3);
    subRound(c, d, e, a, b, K5, expand_store(43), 3);
    subRound(b, c, d, e, a, K5, expand_store(44), 3);
    subRound(a, b, c, d, e, K5, expand_store(45), 3);
    subRound(e, a, b, c, d, K5, expand_store(46), 3);
    subRound(d, e, a, b, c, K5, expand_store(47), 3);
    subRound(c, d, e, a, b, K5, expand_store(48), 3);
    subRound(b, c, d, e, a, K5, expand_store(49), 3);
    subRound(a, b, c, d, e, K5, expand_store(50), 3);
    subRound(e, a, b, c, d, K5, expand_store(51), 3);
    subRound(d, e, a, b, c, K5, expand_store(52), 3);
    subRound(c, d, e, a, b, K5, expand_store(53), 3);
    subRound(b, c, d, e, a, K5, expand_store(54), 3);
    subRound(a, b, c, d, e, K5, expand_store(55), 3);
    subRound(e, a, b, c, d, K5, expand_store(56), 3);
    subRound(d, e, a, b, c, K5, expand_store(57), 3);
    subRound(c, d, e, a, b, K5, expand_store(58), 3);
    subRound(b, c, d, e, a, K5, expand_store(59), 3);
    subRound(a, b, c, d, e, K10, expand_store(60), 4);
    subRound(e, a, b, c, d, K10, expand_store(61), 4);
    subRound(d, e, a, b, c, K10, expand_store(62), 4);
    subRound(c, d, e, a, b, K10, expand_store(63), 4);
    subRound(b, c, d, e, a, K10, expand_store(64), 4);
    subRound(a, b, c, d, e, K10, expand_store(65), 4);
    subRound(e, a, b, c, d, K10, expand_store(66), 4);
    subRound(d, e, a, b, c, K10, expand_store(67), 4);
    subRound(c, d, e, a, b, K10, expand_store(68), 4);
    subRound(b, c, d, e, a, K10, expand_store(69), 4);
    subRound(a, b, c, d, e, K10, expand_store(70), 4);
    subRound(e, a, b, c, d, K10, expand_store(71), 4);
    subRound(d, e, a, b, c, K10, expand_store(72), 4);
    subRound(c, d, e, a, b, K10, expand_store(73), 4);
    subRound(b, c, d, e, a, K10, expand_store(74), 4);
    subRound(a, b, c, d, e, K10, expand_store(75), 4);
    subRound(e, a, b, c, d, K10, expand_store(76), 4);
    subRound(d, e, a, b, c, K10, expand_nostore(77), 4);
    subRound(c, d, e, a, b, K10, expand_nostore(78), 4);
    subRound(b, c, d, e, a, K10, expand_nostore(79), 4);
    hashdata[0] += a;
    hashdata[1] += b;
    hashdata[2] += c;
    hashdata[3] += d;
    hashdata[4] += e;
}

class RttiMap {
public:
    static __Crun::static_type_info const * get(
        typelib_CompoundTypeDescription const * type);

private:
    RttiMap(); // not defined
    RttiMap(RttiMap &); // not defined
    ~RttiMap(); // not defined
    void operator =(RttiMap &); // not defined

    struct Data {
        __Crun::static_type_info * info;
        rtl::OString cppName;
        std::vector< __Crun::class_base_descr > bases;
    };
    typedef std::map< OUString, Data > Map;

    static void toCppNames(
        OUString const & unoName, OString * cppName,
        OString * rttiName);

    static Data const & get_(typelib_CompoundTypeDescription const * type);

    static osl::Mutex m_mutex;
    static Map * m_map;
};

osl::Mutex RttiMap::m_mutex;
RttiMap::Map * RttiMap::m_map;

__Crun::static_type_info const * RttiMap::get(
    typelib_CompoundTypeDescription const * type)
{
    osl::MutexGuard g(m_mutex);
    if (m_map == NULL) {
        m_map = new Map; // leaked
    }
    return get_(type).info;
}

void RttiMap::toCppNames(
    OUString const & unoName, OString * cppName,
    OString * rttiName)
{
    OSL_ASSERT(cppName != NULL && rttiName != NULL);
    rtl::OStringBuffer bc;
    rtl::OStringBuffer br;
    br.append("__1n");
    for (sal_Int32 i = 0; i != -1;) {
        OUString tok(unoName.getToken(0, '.', i));
        bc.append(OUStringToOString(tok, RTL_TEXTENCODING_UTF8));
            // conversion should never fail, as tok should be well-formed ASCII
        if (i != -1) {
            bc.append("::");
        }
        sal_Int32 len = tok.getLength();
        sal_Int32 pos = br.getLength();
        for (sal_Int32 n = len / 26; n > 0; n /= 26) {
            br.insert(pos, static_cast< char >('a' + (n % 26)));
        }
        br.append(static_cast< char >('A' + (len % 26)));
        for (sal_Int32 j = 0; j < len; ++j) {
            sal_Unicode c = tok[j];
            OSL_ASSERT(
                c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c == '_' ||
                c >= 'a' && c <= 'z');
            if (c == 'Q') {
                br.append("QdD");
            } else {
                br.append(static_cast< char >(c));
            }
        }
    }
    br.append('_');
    *cppName = bc.makeStringAndClear();
    *rttiName = br.makeStringAndClear();
}

RttiMap::Data const & RttiMap::get_(
    typelib_CompoundTypeDescription const * type)
{
    OUString name(type->aBase.pTypeName);
    Map::iterator it(m_map->find(name));
    if (it == m_map->end()) {
        it = m_map->insert(std::make_pair(name, Data())).first;
        Data & data = it->second;
        rtl::OString rttiName;
        toCppNames(name, &data.cppName, &rttiName);
        data.info = new __Crun::static_type_info;
        data.info->ty_name = data.cppName.getStr() -
            reinterpret_cast< char * >(&data.info->ty_name);
        data.info->reserved = 0;
        NistHash hash(rttiName);
        data.info->type_hash[0] = hash.hashdata[0];
        data.info->type_hash[1] = hash.hashdata[1];
        data.info->type_hash[2] = hash.hashdata[2];
        data.info->type_hash[3] = hash.hashdata[3];
        data.info->flags = 0;
        data.info->cv_qualifiers = 0;
        if (type->pBaseTypeDescription != NULL) {
            data.bases = get_(type->pBaseTypeDescription).bases;
            OSL_ASSERT(!data.bases.empty());
            data.bases.back().offset = 0;
        }
        __Crun::class_base_descr last;
        last.type_hash[0] = data.info->type_hash[0];
        last.type_hash[1] = data.info->type_hash[1];
        last.type_hash[2] = data.info->type_hash[2];
        last.type_hash[3] = data.info->type_hash[3];
        last.offset = 0x8000000000000000;
        data.bases.push_back(last);
        data.info->base_table = reinterpret_cast< char * >(&data.bases[0]) -
            reinterpret_cast< char * >(&data.info->base_table);
    }
    return it->second;
}

void deleteException(
    void * exception, unsigned int * thunk, typelib_TypeDescription * type)
{
    uno_destructData(
        exception, type,
        reinterpret_cast< uno_ReleaseFunc >(css::uno::cpp_release));
    typelib_typedescription_release(type);
    delete[] thunk;
}

}

namespace bridges { namespace cpp_uno { namespace cc5_solaris_sparc64 {

void raiseException(uno_Any * exception, uno_Mapping * unoToCpp) {
    bridges::cpp_uno::shared::ArrayPointer< unsigned long > thunkPtr(
        new unsigned long[4]);
    typelib_TypeDescription * type = NULL;
    typelib_typedescriptionreference_getDescription(&type, exception->pType);
    __Crun::static_type_info const * rtti = RttiMap::get(
        reinterpret_cast< typelib_CompoundTypeDescription * >(type));
    void * exc = __Crun::ex_alloc(type->nSize);
    uno_copyAndConvertData(exc, exception->pData, type, unoToCpp);
    uno_any_destruct(exception, NULL);
    unsigned long * thunk = thunkPtr.release();
    // 0*4: rd %pc, %o1:
    // 1*4: ldx %o1, (6-0)*4, %o3:
    thunk[0] = 0x93414000D65A6018;
    // 2*4: jmpl %o3, %g0, %g0:
    // 3*4: ldx %o1, (4-0)*4, %o2:
    thunk[1] = 0x81C2C000D45A6010;
    // 4*4: .xword type:
    thunk[2] = reinterpret_cast< unsigned long >(type);
    // 6*4: .xword deleteException:
    thunk[3] = reinterpret_cast< unsigned long >(deleteException);
    flushCode(thunk, thunk + 4);
    __Crun::ex_throw(exc, rtti, toFunction(thunk));
}

void fillUnoException(
    void * cppException, char const * cppName, uno_Any * unoException,
    uno_Mapping * cppToUno)
{
    OUString name;
    typelib_TypeDescription * type = NULL;
    if (toUnoName(cppName, &name)) {
        typelib_typedescription_getByName(&type, name.pData);
    }
    if (type == NULL || type->eTypeClass != typelib_TypeClass_EXCEPTION) {
        css::uno::RuntimeException exc(
            "Not a UNO exception type: " + name),
            css::uno::Reference< css::uno::XInterface >());
        uno_type_any_constructAndConvert(
            unoException, &exc, getCppuType(&exc).getTypeLibType(), cppToUno);
    } else {
        uno_any_constructAndConvert(unoException, cppException, type, cppToUno);
    }
    if (type != NULL) {
        typelib_typedescription_release(type);
    }
}

} } }

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