xref: /aoo4110/main/tools/source/ref/globname.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 #include <tools/stream.hxx>
32 #include <tools/globname.hxx>
33 
34 /************** class ImpSvGlobalName ************************************/
ImpSvGlobalName(const ImpSvGlobalName & rObj)35 ImpSvGlobalName::ImpSvGlobalName( const ImpSvGlobalName & rObj )
36 {
37 	nRefCount = 0;
38 	memcpy( szData, rObj.szData, sizeof( szData ) );
39 }
40 
41 /************** class ImpSvGlobalName ************************************/
ImpSvGlobalName(int)42 ImpSvGlobalName::ImpSvGlobalName( int )
43 {
44 	nRefCount = 1;
45 	memset( szData, 0, sizeof( szData ) );
46 }
47 
48 /*************************************************************************
49 |*	  ImpSvGlobalName::operator ==()
50 *************************************************************************/
operator ==(const ImpSvGlobalName & rObj) const51 sal_Bool ImpSvGlobalName::operator == ( const ImpSvGlobalName & rObj ) const
52 {
53 	return !memcmp( szData, rObj.szData, sizeof( szData ) );
54 }
55 
56 /*************************************************************************
57 |*	  SvGlobalName::SvGlobalName()
58 *************************************************************************/
SvGlobalName()59 SvGlobalName::SvGlobalName()
60 {
61 	static ImpSvGlobalName aNoName( 0 );
62 
63 	pImp = &aNoName;
64 	pImp->nRefCount++;
65 }
66 
67 // locker die Struktur von Windows kopiert
68 #ifdef WNT
69 struct _GUID
70 #else
71 struct GUID
72 #endif
73 {
74 	sal_uInt32 Data1;
75 	sal_uInt16 Data2;
76 	sal_uInt16 Data3;
77 	sal_uInt8  Data4[8];
78 };
SvGlobalName(const CLSID & rId)79 SvGlobalName::SvGlobalName( const CLSID & rId )
80 {
81 	pImp = new ImpSvGlobalName();
82 	pImp->nRefCount++;
83 	memcpy( pImp->szData, &rId, sizeof( pImp->szData ) );
84 }
85 
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)86 SvGlobalName::SvGlobalName( sal_uInt32 n1, sal_uInt16 n2, sal_uInt16 n3,
87 							sal_uInt8 b8, sal_uInt8 b9, sal_uInt8 b10, sal_uInt8 b11,
88 							sal_uInt8 b12, sal_uInt8 b13, sal_uInt8 b14, sal_uInt8 b15 )
89 {
90 	pImp = new ImpSvGlobalName();
91 	pImp->nRefCount++;
92 
93 	*(sal_uInt32 *)pImp->szData 	  = n1;
94 	*(sal_uInt16 *)&pImp->szData[ 4 ] = n2;
95 	*(sal_uInt16 *)&pImp->szData[ 6 ] = n3;
96 	pImp->szData[ 8  ] = b8;
97 	pImp->szData[ 9  ] = b9;
98 	pImp->szData[ 10 ] = b10;
99 	pImp->szData[ 11 ] = b11;
100 	pImp->szData[ 12 ] = b12;
101 	pImp->szData[ 13 ] = b13;
102 	pImp->szData[ 14 ] = b14;
103 	pImp->szData[ 15 ] = b15;
104 }
105 
106 /*************************************************************************
107 |*	  SvGlobalName::~SvGlobalName()
108 *************************************************************************/
~SvGlobalName()109 SvGlobalName::~SvGlobalName()
110 {
111 	pImp->nRefCount--;
112 	if( !pImp->nRefCount )
113 		delete pImp;
114 }
115 
116 /*************************************************************************
117 |*	  SvGlobalName::operator = ()
118 *************************************************************************/
operator =(const SvGlobalName & rObj)119 SvGlobalName & SvGlobalName::operator = ( const SvGlobalName & rObj )
120 {
121 	rObj.pImp->nRefCount++;
122 	pImp->nRefCount--;
123 	if( !pImp->nRefCount )
124 		delete pImp;
125 	pImp = rObj.pImp;
126 	return *this;
127 }
128 
129 /*************************************************************************
130 |*	  SvGlobalName::NewImp()
131 *************************************************************************/
NewImp()132 void SvGlobalName::NewImp()
133 {
134 	if( pImp->nRefCount > 1 )
135 	{
136 		pImp->nRefCount--;
137 		pImp = new ImpSvGlobalName( *pImp );
138 		pImp->nRefCount++;
139 	}
140 }
141 
142 /*************************************************************************
143 |*	  SvGlobalName::operator << ()
144 |*	  SvGlobalName::operator >> ()
145 *************************************************************************/
operator <<(SvStream & rOStr,const SvGlobalName & rObj)146 SvStream& operator << ( SvStream& rOStr, const SvGlobalName & rObj )
147 {
148 	rOStr << *(sal_uInt32 *)rObj.pImp->szData;
149 	rOStr << *(sal_uInt16 *)&rObj.pImp->szData[ 4 ];
150 	rOStr << *(sal_uInt16 *)&rObj.pImp->szData[ 6 ];
151 	rOStr.Write( (sal_Char *)&rObj.pImp->szData[ 8 ], 8 );
152 	return rOStr;
153 }
154 
operator >>(SvStream & rStr,SvGlobalName & rObj)155 SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj )
156 {
157 	rObj.NewImp(); // kopieren, falls noetig
158 	rStr >> *(sal_uInt32 *)rObj.pImp->szData;
159 	rStr >> *(sal_uInt16 *)&rObj.pImp->szData[ 4 ];
160 	rStr >> *(sal_uInt16 *)&rObj.pImp->szData[ 6 ];
161 	rStr.Read( (sal_Char *)&rObj.pImp->szData[ 8 ], 8 );
162 	return rStr;
163 }
164 
165 
166 /*************************************************************************
167 |*	  SvGlobalName::operator < ()
168 *************************************************************************/
operator <(const SvGlobalName & rObj) const169 sal_Bool SvGlobalName::operator < ( const SvGlobalName & rObj ) const
170 {
171 	int n = memcmp( pImp->szData +6, rObj.pImp->szData +6,
172 					sizeof( pImp->szData ) -6);
173 	if( n < 0 )
174 		return sal_True;
175 	else if( n > 0 )
176 		return sal_False;
177 	else if( *(sal_uInt16 *)&pImp->szData[ 4 ] < *(sal_uInt16 *)&rObj.pImp->szData[ 4 ] )
178 		return sal_True;
179 	else if( *(sal_uInt16 *)&pImp->szData[ 4 ] == *(sal_uInt16 *)&rObj.pImp->szData[ 4 ] )
180 		return *(sal_uInt32 *)pImp->szData	< *(sal_uInt32 *)rObj.pImp->szData;
181 	else
182 		return sal_False;
183 
184 }
185 
186 /*************************************************************************
187 |*	  SvGlobalName::operator +=()
188 *************************************************************************/
operator +=(sal_uInt32 n)189 SvGlobalName & SvGlobalName::operator += ( sal_uInt32 n )
190 {
191 	NewImp();
192 	sal_uInt32 nOld = (*(sal_uInt32 *)pImp->szData);
193 	(*(sal_uInt32 *)pImp->szData) += n;
194 	if( nOld > *(sal_uInt32 *)pImp->szData )
195 		// ueberlauf
196 		(*(sal_uInt16 *)&pImp->szData[ 4 ])++;
197 	return *this;
198 }
199 
200 /*************************************************************************
201 |*	  SvGlobalName::operator ==()
202 *************************************************************************/
operator ==(const SvGlobalName & rObj) const203 sal_Bool SvGlobalName::operator == ( const SvGlobalName & rObj ) const
204 {
205 	return *pImp == *rObj.pImp;
206 }
207 
MakeFromMemory(void * pData)208 void SvGlobalName::MakeFromMemory( void * pData )
209 {
210 	NewImp();
211 	memcpy( pImp->szData, pData, sizeof( pImp->szData ) );
212 }
213 
214 /*************************************************************************
215 |*	  SvGlobalName::MakeId()
216 *************************************************************************/
MakeId(const String & rIdStr)217 sal_Bool SvGlobalName::MakeId( const String & rIdStr )
218 {
219 	ByteString	aStr( rIdStr, RTL_TEXTENCODING_ASCII_US );
220 	sal_Char * pStr = (sal_Char *)aStr.GetBuffer();
221 	if( rIdStr.Len() == 36
222 	  && '-' == pStr[ 8 ]  && '-' == pStr[ 13 ]
223 	  && '-' == pStr[ 18 ] && '-' == pStr[ 23 ] )
224 	{
225 		sal_uInt32 nFirst = 0;
226 		int i = 0;
227 		for( i = 0; i < 8; i++ )
228 		{
229 			if( isxdigit( *pStr ) )
230 				if( isdigit( *pStr ) )
231 					nFirst = nFirst * 16 + (*pStr - '0');
232 				else
233 					nFirst = nFirst * 16 + (toupper( *pStr ) - 'A' + 10 );
234 			else
235 				return sal_False;
236 			pStr++;
237 		}
238 
239 		sal_uInt16 nSec = 0;
240 		pStr++;
241 		for( i = 0; i < 4; i++ )
242 		{
243 			if( isxdigit( *pStr ) )
244 				if( isdigit( *pStr ) )
245 					nSec = nSec * 16 + (*pStr - '0');
246 				else
247 					nSec = nSec * 16 + (sal_uInt16)(toupper( *pStr ) - 'A' + 10 );
248 			else
249 				return sal_False;
250 			pStr++;
251 		}
252 
253 		sal_uInt16 nThird = 0;
254 		pStr++;
255 		for( i = 0; i < 4; i++ )
256 		{
257 			if( isxdigit( *pStr ) )
258 				if( isdigit( *pStr ) )
259 					nThird = nThird * 16 + (*pStr - '0');
260 				else
261 					nThird = nThird * 16 + (sal_uInt16)(toupper( *pStr ) - 'A' + 10 );
262 			else
263 				return sal_False;
264 			pStr++;
265 		}
266 
267 		sal_Int8 szRemain[ 8 ];
268 		memset( szRemain, 0, sizeof( szRemain ) );
269 		pStr++;
270 		for( i = 0; i < 16; i++ )
271 		{
272 			if( isxdigit( *pStr ) )
273 				if( isdigit( *pStr ) )
274 					szRemain[i/2] = szRemain[i/2] * 16 + (*pStr - '0');
275 				else
276 					szRemain[i/2] = szRemain[i/2] * 16 + (sal_Int8)(toupper( *pStr ) - 'A' + 10 );
277 			else
278 				return sal_False;
279 			pStr++;
280 			if( i == 3 )
281 				pStr++;
282 		}
283 
284 		NewImp();
285 		*(sal_uInt32 *)pImp->szData 	  = nFirst;
286 		*(sal_uInt16 *)&pImp->szData[ 4 ] = nSec;
287 		*(sal_uInt16 *)&pImp->szData[ 6 ] = nThird;
288 		memcpy( &pImp->szData[ 8 ], szRemain, 8 );
289 		return sal_True;
290 	}
291 	return sal_False;
292 }
293 
294 /*************************************************************************
295 |*	  SvGlobalName::GetctorName()
296 *************************************************************************/
GetctorName() const297 String SvGlobalName::GetctorName() const
298 {
299 	ByteString aRet;
300 
301 	sal_Char buf[ 20 ];
302 	sprintf( buf, "0x%8.8lX", (sal_uIntPtr)*(sal_uInt32 *)pImp->szData );
303 	aRet += buf;
304 	sal_uInt16 i;
305 	for( i = 4; i < 8; i += 2 )
306 	{
307 		aRet += ',';
308 		sprintf( buf, "0x%4.4X", *(sal_uInt16 *)&pImp->szData[ i ] );
309 		aRet += buf;
310 	}
311 	for( i = 8; i < 16; i++ )
312 	{
313 		aRet += ',';
314 		sprintf( buf, "0x%2.2x", pImp->szData[ i ] );
315 		aRet += buf;
316 	}
317 	return String( aRet, RTL_TEXTENCODING_ASCII_US );
318 }
319 
320 /*************************************************************************
321 |*	  SvGlobalName::GetHexName()
322 *************************************************************************/
GetHexName() const323 String SvGlobalName::GetHexName() const
324 {
325 	ByteString aRet;
326 
327 	sal_Char buf[ 10 ];
328 	sprintf( buf, "%8.8lX", (sal_uIntPtr)*(sal_uInt32 *)pImp->szData );
329 	aRet += buf;
330 	aRet += '-';
331 	sal_uInt16 i ;
332 	for( i = 4; i < 8; i += 2 )
333 	{
334 		sprintf( buf, "%4.4X", *(sal_uInt16 *)&pImp->szData[ i ] );
335 		aRet += buf;
336 		aRet += '-';
337 	}
338 	for( i = 8; i < 10; i++ )
339 	{
340 		sprintf( buf, "%2.2x", pImp->szData[ i ] );
341 		aRet += buf;
342 	}
343 	aRet += '-';
344 	for( i = 10; i < 16; i++ )
345 	{
346 		sprintf( buf, "%2.2x", pImp->szData[ i ] );
347 		aRet += buf;
348 	}
349 	return String( aRet, RTL_TEXTENCODING_ASCII_US );
350 }
351 
352 /************** SvGlobalNameList ****************************************/
353 /************************************************************************/
354 /*************************************************************************
355 |*	  SvGlobalNameList::SvGlobalNameList()
356 *************************************************************************/
SvGlobalNameList()357 SvGlobalNameList::SvGlobalNameList()
358 	: aList( 1, 1 )
359 {
360 }
361 
362 /*************************************************************************
363 |*	  SvGlobalNameList::~SvGlobalNameList()
364 *************************************************************************/
~SvGlobalNameList()365 SvGlobalNameList::~SvGlobalNameList()
366 {
367 	for( sal_uIntPtr i = Count(); i > 0; i-- )
368 	{
369 		ImpSvGlobalName * pImp = (ImpSvGlobalName *)aList.GetObject( i -1 );
370 		pImp->nRefCount--;
371 		if( !pImp->nRefCount )
372 			delete pImp;
373 	}
374 }
375 
376 /*************************************************************************
377 |*	  SvGlobalNameList::Append()
378 *************************************************************************/
Append(const SvGlobalName & rName)379 void SvGlobalNameList::Append( const SvGlobalName & rName )
380 {
381 	rName.pImp->nRefCount++;
382 	aList.Insert( rName.pImp, LIST_APPEND );
383 }
384 
385 /*************************************************************************
386 |*	  SvGlobalNameList::GetObject()
387 *************************************************************************/
GetObject(sal_uLong nPos)388 SvGlobalName SvGlobalNameList::GetObject( sal_uLong nPos )
389 {
390 	return SvGlobalName( (ImpSvGlobalName *)aList.GetObject( nPos ) );
391 }
392 
393 /*************************************************************************
394 |*	  SvGlobalNameList::IsEntry()
395 *************************************************************************/
IsEntry(const SvGlobalName & rName)396 sal_Bool SvGlobalNameList::IsEntry( const SvGlobalName & rName )
397 {
398 	for( sal_uIntPtr i = Count(); i > 0; i-- )
399 	{
400 		if( *rName.pImp == *(ImpSvGlobalName *)aList.GetObject( i -1 ) )
401 			return sal_True;
402 	}
403 	return sal_False;
404 }
405 
GetByteSequence() const406 com::sun::star::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const
407 {
408     // platform independent representation of a "GlobalName"
409     // maybe transported remotely
410     com::sun::star::uno::Sequence< sal_Int8 > aResult( 16 );
411 
412     aResult[0] = (sal_Int8) (*(sal_uInt32 *)pImp->szData >> 24);
413     aResult[1] = (sal_Int8) ((*(sal_uInt32 *)pImp->szData << 8 ) >> 24);
414     aResult[2] = (sal_Int8) ((*(sal_uInt32 *)pImp->szData << 16 ) >> 24);
415     aResult[3] = (sal_Int8) ((*(sal_uInt32 *)pImp->szData << 24 ) >> 24);
416     aResult[4] = (sal_Int8) (*(sal_uInt16 *)&pImp->szData[ 4 ] >> 8);
417     aResult[5] = (sal_Int8) ((*(sal_uInt16 *)&pImp->szData[ 4 ] << 8 ) >> 8);
418     aResult[6] = (sal_Int8) (*(sal_uInt16 *)&pImp->szData[ 6 ] >> 8);
419     aResult[7] = (sal_Int8) ((*(sal_uInt16 *)&pImp->szData[ 6 ] << 8 ) >> 8);
420     aResult[8] = pImp->szData[ 8 ];
421     aResult[9] = pImp->szData[ 9 ];
422     aResult[10] = pImp->szData[ 10 ];
423     aResult[11] = pImp->szData[ 11 ];
424     aResult[12] = pImp->szData[ 12 ];
425     aResult[13] = pImp->szData[ 13 ];
426     aResult[14] = pImp->szData[ 14 ];
427     aResult[15] = pImp->szData[ 15 ];
428 
429     return aResult;
430 }
431 
SvGlobalName(const com::sun::star::uno::Sequence<sal_Int8> & aSeq)432 SvGlobalName::SvGlobalName( const com::sun::star::uno::Sequence < sal_Int8 >& aSeq )
433 {
434     // create SvGlobalName from a platform independent representation
435     GUID aResult;
436     memset( &aResult, 0, sizeof( aResult ) );
437 	if ( aSeq.getLength() == 16 )
438 	{
439 		aResult.Data1 = ( ( ( ( ( ( sal_uInt8 )aSeq[0] << 8 ) + ( sal_uInt8 )aSeq[1] ) << 8 ) + ( sal_uInt8 )aSeq[2] ) << 8 ) + ( sal_uInt8 )aSeq[3];
440 		aResult.Data2 = ( ( sal_uInt8 )aSeq[4] << 8 ) + ( sal_uInt8 )aSeq[5];
441 		aResult.Data3 = ( ( sal_uInt8 )aSeq[6] << 8 ) + ( sal_uInt8 )aSeq[7];
442 		for( int nInd = 0; nInd < 8; nInd++ )
443 			aResult.Data4[nInd] = ( sal_uInt8 )aSeq[nInd+8];
444     }
445 
446 	pImp = new ImpSvGlobalName();
447 	pImp->nRefCount++;
448     memcpy( pImp->szData, &aResult, sizeof( pImp->szData ) );
449 }
450