Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/tools/source/ref/globname.cxx
Line
Count
Source
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 <string.h>
21
22
#include <comphelper/mimeconfighelper.hxx>
23
#include <o3tl/sprintf.hxx>
24
#include <rtl/character.hxx>
25
26
#include <tools/stream.hxx>
27
#include <tools/globname.hxx>
28
29
// SvGlobalName ----------------------------------------------------------------
30
31
SvGlobalName::SvGlobalName( const css::uno::Sequence < sal_Int8 >& aSeq )
32
43.3k
{
33
    // create SvGlobalName from a platform independent representation
34
43.3k
    if ( aSeq.getLength() == 16 )
35
43.3k
    {
36
43.3k
        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]);
37
43.3k
        m_aData.Data2 = ( static_cast<sal_uInt8>(aSeq[4]) << 8 ) + static_cast<sal_uInt8>(aSeq[5]);
38
43.3k
        m_aData.Data3 = ( static_cast<sal_uInt8>(aSeq[6]) << 8 ) + static_cast<sal_uInt8>(aSeq[7]);
39
390k
        for( int nInd = 0; nInd < 8; nInd++ )
40
346k
            m_aData.Data4[nInd] = static_cast<sal_uInt8>(aSeq[nInd+8]);
41
43.3k
    }
42
43.3k
}
43
44
SvStream& WriteSvGlobalName( SvStream& rOStr, const SvGlobalName & rObj )
45
0
{
46
0
    rOStr.WriteUInt32( rObj.m_aData.Data1 );
47
0
    rOStr.WriteUInt16( rObj.m_aData.Data2 );
48
0
    rOStr.WriteUInt16( rObj.m_aData.Data3 );
49
0
    rOStr.WriteBytes( &rObj.m_aData.Data4, 8 );
50
0
    return rOStr;
51
0
}
52
53
SvStream& operator >> ( SvStream& rStr, SvGlobalName & rObj )
54
73.0k
{
55
73.0k
    rStr.ReadUInt32( rObj.m_aData.Data1 );
56
73.0k
    rStr.ReadUInt16( rObj.m_aData.Data2 );
57
73.0k
    rStr.ReadUInt16( rObj.m_aData.Data3 );
58
73.0k
    rStr.ReadBytes( &rObj.m_aData.Data4, 8 );
59
73.0k
    return rStr;
60
73.0k
}
61
62
void SvGlobalName::MakeFromMemory( void const * pData )
63
0
{
64
0
    memcpy( &m_aData, pData, sizeof( m_aData ) );
65
0
}
66
67
bool SvGlobalName::MakeId( std::u16string_view rIdStr )
68
415
{
69
415
    const sal_Unicode *pStr = rIdStr.data();
70
415
    if( rIdStr.size() != 36
71
415
      || '-' != pStr[ 8 ]  || '-' != pStr[ 13 ]
72
415
      || '-' != pStr[ 18 ] || '-' != pStr[ 23 ] )
73
0
        return false;
74
75
415
    SvGUID aGuid = {};
76
415
    auto asciiHexDigitToNumber = [](sal_Unicode c) -> sal_uInt8
77
13.2k
    {
78
13.2k
        if (rtl::isAsciiDigit(c))
79
9.53k
            return c - '0';
80
3.74k
        else
81
3.74k
            return rtl::toAsciiUpperCase(c) - 'A' + 10;
82
13.2k
    };
83
84
3.73k
    for( int i = 0; i < 8; i++ )
85
3.32k
    {
86
3.32k
        if( !rtl::isAsciiHexDigit( *pStr ) )
87
0
            return false;
88
3.32k
        aGuid.Data1 = aGuid.Data1 * 16 + asciiHexDigitToNumber( *pStr++ );
89
3.32k
    }
90
91
415
    pStr++;
92
2.07k
    for( int i = 0; i < 4; i++ )
93
1.66k
    {
94
1.66k
        if( !rtl::isAsciiHexDigit( *pStr ) )
95
0
            return false;
96
1.66k
        aGuid.Data2 = aGuid.Data2 * 16 + asciiHexDigitToNumber( *pStr++ );
97
1.66k
    }
98
99
415
    pStr++;
100
2.07k
    for( int i = 0; i < 4; i++ )
101
1.66k
    {
102
1.66k
        if( !rtl::isAsciiHexDigit( *pStr ) )
103
0
            return false;
104
1.66k
        aGuid.Data3 = aGuid.Data3 * 16 + asciiHexDigitToNumber( *pStr++ );
105
1.66k
    }
106
107
415
    pStr++;
108
7.05k
    for( int i = 0; i < 16; i++ )
109
6.64k
    {
110
6.64k
        if( !rtl::isAsciiHexDigit( *pStr ) )
111
0
            return false;
112
6.64k
        aGuid.Data4[i/2] = aGuid.Data4[i/2] * 16 + asciiHexDigitToNumber( *pStr++ );
113
6.64k
        if( i == 3 )
114
415
            pStr++;
115
6.64k
    }
116
117
415
    m_aData = aGuid;
118
415
    return true;
119
415
}
120
121
OUString SvGlobalName::GetHexName() const
122
76
{
123
76
    char buf[ 37 ];
124
76
    int n = o3tl::sprintf(buf,
125
76
                    "%8.8" SAL_PRIXUINT32 "-%4.4X-%4.4X-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x",
126
76
                    m_aData.Data1, m_aData.Data2, m_aData.Data3,
127
76
                    m_aData.Data4[0], m_aData.Data4[1], m_aData.Data4[2], m_aData.Data4[3],
128
76
                    m_aData.Data4[4], m_aData.Data4[5], m_aData.Data4[6], m_aData.Data4[7]);
129
76
    assert(n == 36);
130
76
    return OUString::createFromAscii(std::string_view(buf, n));
131
76
}
132
133
css::uno::Sequence < sal_Int8 > SvGlobalName::GetByteSequence() const
134
46.1k
{
135
    // platform independent representation of a "GlobalName"
136
    // maybe transported remotely
137
46.1k
    return comphelper::MimeConfigurationHelper::GetSequenceClassID(
138
46.1k
        m_aData.Data1, m_aData.Data2, m_aData.Data3,
139
46.1k
        m_aData.Data4[0], m_aData.Data4[1], m_aData.Data4[2], m_aData.Data4[3],
140
46.1k
        m_aData.Data4[4], m_aData.Data4[5], m_aData.Data4[6], m_aData.Data4[7]);
141
46.1k
}
142
143
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */