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 <stdio.h>
21 #include <string.h>
22 
23 #include <rtl/strbuf.hxx>
24 #include <rtl/character.hxx>
25 
26 #include <tools/stream.hxx>
27 #include <tools/globname.hxx>
28 
29 // ImpSvGlobalName ------------------------------------------------------------
ImpSvGlobalName(const ImpSvGlobalName & rObj)30 ImpSvGlobalName::ImpSvGlobalName( const ImpSvGlobalName & rObj )
31     : szData(rObj.szData)
32 {
33 }
34 
ImpSvGlobalName(sal_uInt32 n1,sal_uInt16 n2,sal_uInt16 n3,sal_uInt8 b8,sal_uInt8 b9,sal_uInt8 b10,sal_uInt8 b11,sal_uInt8 b12,sal_uInt8 b13,sal_uInt8 b14,sal_uInt8 b15)35 ImpSvGlobalName::ImpSvGlobalName(sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
36                           sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
37                           sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15)
38 {
39     szData.Data1 = n1;
40     szData.Data2 = n2;
41     szData.Data3 = n3;
42     szData.Data4[0] = b8;
43     szData.Data4[1] = b9;
44     szData.Data4[2] = b10;
45     szData.Data4[3] = b11;
46     szData.Data4[4] = b12;
47     szData.Data4[5] = b13;
48     szData.Data4[6] = b14;
49     szData.Data4[7] = b15;
50 }
51 
operator ==(const ImpSvGlobalName & rObj) const52 bool ImpSvGlobalName::operator == ( const ImpSvGlobalName & rObj ) const
53 {
54     return !memcmp( &szData, &rObj.szData, sizeof( szData ) );
55 }
56 
57 // SvGlobalName ----------------------------------------------------------------
SvGlobalName()58 SvGlobalName::SvGlobalName() :
59     pImp()
60 {
61 }
62 
SvGlobalName(const SvGUID & rId)63 SvGlobalName::SvGlobalName( const SvGUID & rId ) :
64     pImp( ImpSvGlobalName( rId ) )
65 {
66 }
67 
SvGlobalName(sal_uInt32 n1,sal_uInt16 n2,sal_uInt16 n3,sal_uInt8 b8,sal_uInt8 b9,sal_uInt8 b10,sal_uInt8 b11,sal_uInt8 b12,sal_uInt8 b13,sal_uInt8 b14,sal_uInt8 b15)68 SvGlobalName::SvGlobalName( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
69                             sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
70                             sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 ) :
71     pImp( ImpSvGlobalName(n1, n2, n3, b8, b9, b10, b11, b12, b13, b14, b15) )
72 {
73 }
74 
SvGlobalName(const css::uno::Sequence<sal_Int8> & aSeq)75 SvGlobalName::SvGlobalName( const css::uno::Sequence < sal_Int8 >& aSeq )
76 {
77     // create SvGlobalName from a platform independent representation
78     SvGUID aResult = {};
79     if ( aSeq.getLength() == 16 )
80     {
81         aResult.Data1 = ( ( ( ( ( static_cast<sal_uInt8>(aSeq[0]) << 8 ) + static_cast<sal_uInt8>(aSeq[1]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[2]) ) << 8 ) + static_cast<sal_uInt8>(aSeq[3]);
82         aResult.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]);
83         aResult.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]);
84         for( int nInd = 0; nInd < 8; nInd++ )
85             aResult.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]);
86     }
87 
88     pImp = ::o3tl::cow_wrapper< ImpSvGlobalName >(aResult);
89 }
90 
~SvGlobalName()91 SvGlobalName::~SvGlobalName()
92 {
93 }
94 
operator =(const SvGlobalName & rObj)95 SvGlobalName & SvGlobalName::operator = ( const SvGlobalName & rObj )
96 {
97     pImp = rObj.pImp;
98 
99     return *this;
100 }
101 
operator =(SvGlobalName && rObj)102 SvGlobalName & SvGlobalName::operator = ( SvGlobalName && rObj ) noexcept
103 {
104     pImp = std::move(rObj.pImp);
105     return *this;
106 }
107 
WriteSvGlobalName(SvStream & rOStr,const SvGlobalName & rObj)108 SvStream& WriteSvGlobalName( SvStream& rOStr, const SvGlobalName & rObj )
109 {
110     rOStr.WriteUInt32( rObj.pImp->szData.Data1 );
111     rOStr.WriteUInt16( rObj.pImp->szData.Data2 );
112     rOStr.WriteUInt16( rObj.pImp->szData.Data3 );
113     rOStr.WriteBytes( &rObj.pImp->szData.Data4, 8 );
114     return rOStr;
115 }
116 
operator >>(SvStream & rStr,SvGlobalName & rObj)117 SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj )
118 {
119     // the non-const dereferencing operator
120     // ensures pImp is unique
121     rStr.ReadUInt32( rObj.pImp->szData.Data1 );
122     rStr.ReadUInt16( rObj.pImp->szData.Data2 );
123     rStr.ReadUInt16( rObj.pImp->szData.Data3 );
124     rStr.ReadBytes( &rObj.pImp->szData.Data4, 8 );
125     return rStr;
126 }
127 
128 
operator <(const SvGlobalName & rObj) const129 bool SvGlobalName::operator < ( const SvGlobalName & rObj ) const
130 {
131     if( pImp->szData.Data3 < rObj.pImp->szData.Data3 )
132         return true;
133     else if( pImp->szData.Data3 > rObj.pImp->szData.Data3 )
134         return false;
135 
136     if( pImp->szData.Data2 < rObj.pImp->szData.Data2 )
137         return true;
138     else if( pImp->szData.Data2 > rObj.pImp->szData.Data2 )
139         return false;
140 
141     return pImp->szData.Data1 < rObj.pImp->szData.Data1;
142 }
143 
operator ==(const SvGlobalName & rObj) const144 bool SvGlobalName::operator == ( const SvGlobalName & rObj ) const
145 {
146     return pImp == rObj.pImp;
147 }
148 
MakeFromMemory(void const * pData)149 void SvGlobalName::MakeFromMemory( void const * pData )
150 {
151     memcpy( &pImp->szData, pData, sizeof( pImp->szData ) );
152 }
153 
MakeId(const OUString & rIdStr)154 bool SvGlobalName::MakeId( const OUString & rIdStr )
155 {
156     const sal_Unicode *pStr = rIdStr.getStr();
157     if( rIdStr.getLength() == 36
158       && '-' == pStr[ 8 ]  && '-' == pStr[ 13 ]
159       && '-' == pStr[ 18 ] && '-' == pStr[ 23 ] )
160     {
161         sal_uInt32 nFirst = 0;
162         int i = 0;
163         for( i = 0; i < 8; i++ )
164         {
165             if( rtl::isAsciiHexDigit( *pStr ) )
166                 if( rtl::isAsciiDigit( *pStr ) )
167                     nFirst = nFirst * 16 + (*pStr - '0');
168                 else
169                     nFirst = nFirst * 16 + (rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
170             else
171                 return false;
172             pStr++;
173         }
174 
175         sal_uInt16 nSec = 0;
176         pStr++;
177         for( i = 0; i < 4; i++ )
178         {
179             if( rtl::isAsciiHexDigit( *pStr ) )
180                 if( rtl::isAsciiDigit( *pStr ) )
181                     nSec = nSec * 16 + (*pStr - '0');
182                 else
183                     nSec = nSec * 16 + static_cast<sal_uInt16>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
184             else
185                 return false;
186             pStr++;
187         }
188 
189         sal_uInt16 nThird = 0;
190         pStr++;
191         for( i = 0; i < 4; i++ )
192         {
193             if( rtl::isAsciiHexDigit( *pStr ) )
194                 if( rtl::isAsciiDigit( *pStr ) )
195                     nThird = nThird * 16 + (*pStr - '0');
196                 else
197                     nThird = nThird * 16 + static_cast<sal_uInt16>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
198             else
199                 return false;
200             pStr++;
201         }
202 
203         sal_Int8 szRemain[ 8 ] = {};
204         pStr++;
205         for( i = 0; i < 16; i++ )
206         {
207             if( rtl::isAsciiHexDigit( *pStr ) )
208                 if( rtl::isAsciiDigit( *pStr ) )
209                     szRemain[i/2] = szRemain[i/2] * 16 + (*pStr - '0');
210                 else
211                     szRemain[i/2] = szRemain[i/2] * 16 + static_cast<sal_Int8>(rtl::toAsciiUpperCase( *pStr ) - 'A' + 10 );
212             else
213                 return false;
214             pStr++;
215             if( i == 3 )
216                 pStr++;
217         }
218 
219         memcpy(&pImp->szData.Data1, &nFirst, sizeof(nFirst));
220         memcpy(&pImp->szData.Data2, &nSec, sizeof(nSec));
221         memcpy(&pImp->szData.Data3, &nThird, sizeof(nThird));
222         memcpy(&pImp->szData.Data4, szRemain, 8);
223         return true;
224     }
225     return false;
226 }
227 
GetHexName() const228 OUString SvGlobalName::GetHexName() const
229 {
230     OStringBuffer aHexBuffer(36);
231 
232     char buf[ 10 ];
233     sprintf( buf, "%8.8" SAL_PRIXUINT32, pImp->szData.Data1 );
234     aHexBuffer.append(buf);
235     aHexBuffer.append('-');
236     sprintf( buf, "%4.4X", pImp->szData.Data2 );
237     aHexBuffer.append(buf);
238     aHexBuffer.append('-');
239     sprintf( buf, "%4.4X", pImp->szData.Data3 );
240     aHexBuffer.append(buf);
241     aHexBuffer.append('-');
242     for( int i = 0; i < 2; i++ )
243     {
244         sprintf( buf, "%2.2x", pImp->szData.Data4[ i ] );
245         aHexBuffer.append(buf);
246     }
247     aHexBuffer.append('-');
248     for( int i = 2; i < 8; i++ )
249     {
250         sprintf( buf, "%2.2x", pImp->szData.Data4[ i ] );
251         aHexBuffer.append(buf);
252     }
253     return OStringToOUString(aHexBuffer.makeStringAndClear(), RTL_TEXTENCODING_ASCII_US);
254 }
255 
GetByteSequence() const256 css::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const
257 {
258     // platform independent representation of a "GlobalName"
259     // maybe transported remotely
260     css::uno::Sequence< sal_Int8 > aResult( 16 );
261 
262     aResult[ 0] = static_cast<sal_Int8>(pImp->szData.Data1 >> 24);
263     aResult[ 1] = static_cast<sal_Int8>((pImp->szData.Data1 << 8 ) >> 24);
264     aResult[ 2] = static_cast<sal_Int8>((pImp->szData.Data1 << 16 ) >> 24);
265     aResult[ 3] = static_cast<sal_Int8>((pImp->szData.Data1 << 24 ) >> 24);
266     aResult[ 4] = static_cast<sal_Int8>(pImp->szData.Data2 >> 8);
267     aResult[ 5] = static_cast<sal_Int8>((pImp->szData.Data2 << 8 ) >> 8);
268     aResult[ 6] = static_cast<sal_Int8>(pImp->szData.Data3 >> 8);
269     aResult[ 7] = static_cast<sal_Int8>((pImp->szData.Data3 << 8 ) >> 8);
270     aResult[ 8] = pImp->szData.Data4[ 0 ];
271     aResult[ 9] = pImp->szData.Data4[ 1 ];
272     aResult[10] = pImp->szData.Data4[ 2 ];
273     aResult[11] = pImp->szData.Data4[ 3 ];
274     aResult[12] = pImp->szData.Data4[ 4 ];
275     aResult[13] = pImp->szData.Data4[ 5 ];
276     aResult[14] = pImp->szData.Data4[ 6 ];
277     aResult[15] = pImp->szData.Data4[ 7 ];
278 
279     return aResult;
280 }
281 
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
283