summaryrefslogtreecommitdiff
path: root/include/tools/solar.h
blob: 721573287685b965400f55379ec32f48a5ad148d (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
/* -*- 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 .
 */

#ifndef INCLUDED_TOOLS_SOLAR_H
#define INCLUDED_TOOLS_SOLAR_H

#include <sal/types.h>
#include <osl/endian.h>

/** Intermediate type to solve type clash with Windows headers.
 Should be removed as soon as all code parts have been reviewed
 and the correct type is known. Most of the times ULONG is meant
 to be a 32-Bit unsigned integer type as sal_uInt32 is often
 used for data exchange or for similar method args. */
typedef sal_uIntPtr    sal_uLong; /* Replaces type ULONG */

// misc. macros to leverage platform and compiler differences

#define DELETEZ( p )    ( delete p,p = NULL )

// solar binary types

/* Solar (portable) Binary (exchange) Type; OSI 6 subset
   always little endian;
   not necessarily aligned */

typedef sal_uInt8   SVBT16[2];
typedef sal_uInt8   SVBT32[4];
typedef sal_uInt8   SVBT64[8];

#ifdef __cplusplus

inline sal_uInt16 SVBT16ToUInt16( const SVBT16 p ) { return static_cast<sal_uInt16>
                                                     (static_cast<sal_uInt16>(p[0])
                                                   + (static_cast<sal_uInt16>(p[1]) <<  8)); }
inline sal_Int16 SVBT16ToInt16( const SVBT16 p ) { return sal_Int16(SVBT16ToUInt16(p)); }
inline sal_uInt32 SVBT32ToUInt32 ( const SVBT32 p ) { return static_cast<sal_uInt32>
                                                     (static_cast<sal_uInt32>(p[0])
                                                   + (static_cast<sal_uInt32>(p[1]) <<  8)
                                                   + (static_cast<sal_uInt32>(p[2]) << 16)
                                                   + (static_cast<sal_uInt32>(p[3]) << 24)); }
#if defined OSL_LITENDIAN
inline double   SVBT64ToDouble( const SVBT64 p )
{
    double n;
    reinterpret_cast<sal_uInt8*>(&n)[0] = p[0];
    reinterpret_cast<sal_uInt8*>(&n)[1] = p[1];
    reinterpret_cast<sal_uInt8*>(&n)[2] = p[2];
    reinterpret_cast<sal_uInt8*>(&n)[3] = p[3];
    reinterpret_cast<sal_uInt8*>(&n)[4] = p[4];
    reinterpret_cast<sal_uInt8*>(&n)[5] = p[5];
    reinterpret_cast<sal_uInt8*>(&n)[6] = p[6];
    reinterpret_cast<sal_uInt8*>(&n)[7] = p[7];
    return n;
}
#else
inline double   SVBT64ToDouble( const SVBT64 p ) { double n;
                                                    reinterpret_cast<sal_uInt8*>(&n)[0] = p[7];
                                                    reinterpret_cast<sal_uInt8*>(&n)[1] = p[6];
                                                    reinterpret_cast<sal_uInt8*>(&n)[2] = p[5];
                                                    reinterpret_cast<sal_uInt8*>(&n)[3] = p[4];
                                                    reinterpret_cast<sal_uInt8*>(&n)[4] = p[3];
                                                    reinterpret_cast<sal_uInt8*>(&n)[5] = p[2];
                                                    reinterpret_cast<sal_uInt8*>(&n)[6] = p[1];
                                                    reinterpret_cast<sal_uInt8*>(&n)[7] = p[0];
                                                    return n; }
#endif

inline void     ShortToSVBT16( sal_uInt16 n, SVBT16 p )
{
    p[0] = static_cast<sal_uInt8>(n);
    p[1] = static_cast<sal_uInt8>(n >>  8);
}
inline void     UInt32ToSVBT32 ( sal_uInt32  n, SVBT32 p )
{
    p[0] = static_cast<sal_uInt8>(n);
    p[1] = static_cast<sal_uInt8>(n >>  8);
    p[2] = static_cast<sal_uInt8>(n >> 16);
    p[3] = static_cast<sal_uInt8>(n >> 24);
}
inline void     Int32ToSVBT32 ( sal_Int32  n, SVBT32 p ) { UInt32ToSVBT32(sal_uInt32(n), p); }
#if defined OSL_LITENDIAN
inline void     DoubleToSVBT64( double n, SVBT64 p ) { p[0] = reinterpret_cast<sal_uInt8*>(&n)[0];
                                                       p[1] = reinterpret_cast<sal_uInt8*>(&n)[1];
                                                       p[2] = reinterpret_cast<sal_uInt8*>(&n)[2];
                                                       p[3] = reinterpret_cast<sal_uInt8*>(&n)[3];
                                                       p[4] = reinterpret_cast<sal_uInt8*>(&n)[4];
                                                       p[5] = reinterpret_cast<sal_uInt8*>(&n)[5];
                                                       p[6] = reinterpret_cast<sal_uInt8*>(&n)[6];
                                                       p[7] = reinterpret_cast<sal_uInt8*>(&n)[7]; }
#else
inline void     DoubleToSVBT64( double n, SVBT64 p ) { p[0] = reinterpret_cast<sal_uInt8*>(&n)[7];
                                                       p[1] = reinterpret_cast<sal_uInt8*>(&n)[6];
                                                       p[2] = reinterpret_cast<sal_uInt8*>(&n)[5];
                                                       p[3] = reinterpret_cast<sal_uInt8*>(&n)[4];
                                                       p[4] = reinterpret_cast<sal_uInt8*>(&n)[3];
                                                       p[5] = reinterpret_cast<sal_uInt8*>(&n)[2];
                                                       p[6] = reinterpret_cast<sal_uInt8*>(&n)[1];
                                                       p[7] = reinterpret_cast<sal_uInt8*>(&n)[0]; }
#endif
#endif

#endif

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