Avoid potential negative array index access to cached text.
[LibreOffice.git] / tools / source / ref / globname.cxx
blobdf8ff10943eae732d30922e671d0884ebdbd7c44
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <string.h>
22 #include <comphelper/mimeconfighelper.hxx>
23 #include <o3tl/sprintf.hxx>
24 #include <rtl/character.hxx>
26 #include <tools/stream.hxx>
27 #include <tools/globname.hxx>
29 // SvGlobalName ----------------------------------------------------------------
30 SvGlobalName::SvGlobalName( const SvGUID & rId ) :
31 m_aData( rId )
35 SvGlobalName::SvGlobalName( 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 m_aData{ n1, n2, n3, { b8, b9, b10, b11, b12, b13, b14, b15 } }
42 SvGlobalName::SvGlobalName( const css::uno::Sequence < sal_Int8 >& aSeq )
44 // create SvGlobalName from a platform independent representation
45 if ( aSeq.getLength() == 16 )
47 m_aData.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]);
48 m_aData.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]);
49 m_aData.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]);
50 for( int nInd = 0; nInd < 8; nInd++ )
51 m_aData.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]);
55 SvStream& WriteSvGlobalName( SvStream& rOStr, const SvGlobalName & rObj )
57 rOStr.WriteUInt32( rObj.m_aData.Data1 );
58 rOStr.WriteUInt16( rObj.m_aData.Data2 );
59 rOStr.WriteUInt16( rObj.m_aData.Data3 );
60 rOStr.WriteBytes( &rObj.m_aData.Data4, 8 );
61 return rOStr;
64 SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj )
66 rStr.ReadUInt32( rObj.m_aData.Data1 );
67 rStr.ReadUInt16( rObj.m_aData.Data2 );
68 rStr.ReadUInt16( rObj.m_aData.Data3 );
69 rStr.ReadBytes( &rObj.m_aData.Data4, 8 );
70 return rStr;
74 bool SvGlobalName::operator < ( const SvGlobalName & rObj ) const
76 if( m_aData.Data3 < rObj.m_aData.Data3 )
77 return true;
78 else if( m_aData.Data3 > rObj.m_aData.Data3 )
79 return false;
81 if( m_aData.Data2 < rObj.m_aData.Data2 )
82 return true;
83 else if( m_aData.Data2 > rObj.m_aData.Data2 )
84 return false;
86 return m_aData.Data1 < rObj.m_aData.Data1;
89 bool SvGlobalName::operator == ( const SvGlobalName & rObj ) const
91 return memcmp(&m_aData, &rObj.m_aData, sizeof(m_aData)) == 0;
94 void SvGlobalName::MakeFromMemory( void const * pData )
96 memcpy( &m_aData, pData, sizeof( m_aData ) );
99 bool SvGlobalName::MakeId( std::u16string_view rIdStr )
101 const sal_Unicode *pStr = rIdStr.data();
102 if( rIdStr.size() != 36
103 || '-' != pStr[ 8 ] || '-' != pStr[ 13 ]
104 || '-' != pStr[ 18 ] || '-' != pStr[ 23 ] )
105 return false;
107 SvGUID aGuid = {};
108 auto asciiHexDigitToNumber = [](sal_Unicode c) -> sal_uInt8
110 if (rtl::isAsciiDigit(c))
111 return c - '0';
112 else
113 return rtl::toAsciiUpperCase(c) - 'A' + 10;
116 for( int i = 0; i < 8; i++ )
118 if( !rtl::isAsciiHexDigit( *pStr ) )
119 return false;
120 aGuid.Data1 = aGuid.Data1 * 16 + asciiHexDigitToNumber( *pStr++ );
123 pStr++;
124 for( int i = 0; i < 4; i++ )
126 if( !rtl::isAsciiHexDigit( *pStr ) )
127 return false;
128 aGuid.Data2 = aGuid.Data2 * 16 + asciiHexDigitToNumber( *pStr++ );
131 pStr++;
132 for( int i = 0; i < 4; i++ )
134 if( !rtl::isAsciiHexDigit( *pStr ) )
135 return false;
136 aGuid.Data3 = aGuid.Data3 * 16 + asciiHexDigitToNumber( *pStr++ );
139 pStr++;
140 for( int i = 0; i < 16; i++ )
142 if( !rtl::isAsciiHexDigit( *pStr ) )
143 return false;
144 aGuid.Data4[i/2] = aGuid.Data4[i/2] * 16 + asciiHexDigitToNumber( *pStr++ );
145 if( i == 3 )
146 pStr++;
149 m_aData = aGuid;
150 return true;
153 OUString SvGlobalName::GetHexName() const
155 char buf[ 37 ];
156 int n = o3tl::sprintf(buf,
157 "%8.8" SAL_PRIXUINT32 "-%4.4X-%4.4X-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
158 m_aData.Data1, m_aData.Data2, m_aData.Data3,
159 m_aData.Data4[0], m_aData.Data4[1], m_aData.Data4[2], m_aData.Data4[3],
160 m_aData.Data4[4], m_aData.Data4[5], m_aData.Data4[6], m_aData.Data4[7]);
161 assert(n == 36);
162 return OUString::createFromAscii(std::string_view(buf, n));
165 css::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const
167 // platform independent representation of a "GlobalName"
168 // maybe transported remotely
169 return comphelper::MimeConfigurationHelper::GetSequenceClassID(
170 m_aData.Data1, m_aData.Data2, m_aData.Data3,
171 m_aData.Data4[0], m_aData.Data4[1], m_aData.Data4[2], m_aData.Data4[3],
172 m_aData.Data4[4], m_aData.Data4[5], m_aData.Data4[6], m_aData.Data4[7]);
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */