LibreOffice Module xmlhelp (master) 1
db.cxx
Go to the documentation of this file.
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
21#include "db.hxx"
22
23#include <cstring>
24#include <utility>
25
26#include <com/sun/star/io/XSeekable.hpp>
27#include <tools/inetmime.hxx>
28
29using namespace com::sun::star::uno;
30using namespace com::sun::star::io;
31
32namespace {
33
34//TODO: Replace with C++17 std::from_chars once available:
35std::pair<sal_Int32, char const *> readInt32(char const * begin, char const * end) {
36 sal_Int32 n = 0;
37 for (; begin != end; ++begin) {
38 auto const w = INetMIME::getHexWeight(static_cast<unsigned char>(*begin));
39 if (w == -1) {
40 break;
41 }
42 n = 16 * n + w;
43 }
44 return {n, begin};
45}
46
47}
48
49namespace helpdatafileproxy {
50
51void HDFData::copyToBuffer( const char* pSrcData, int nSize )
52{
53 m_nSize = nSize;
54 m_pBuffer.reset( new char[m_nSize+1] );
55 memcpy( m_pBuffer.get(), pSrcData, m_nSize );
56 m_pBuffer[m_nSize] = 0;
57}
58
59
60// Hdf
61
62bool Hdf::implReadLenAndData( const char* pData, char const * end, int& riPos, HDFData& rValue )
63{
64 bool bSuccess = false;
65
66 // Read key len
67 const char* pStartPtr = pData + riPos;
68 auto [nKeyLen, pEndPtr] = readInt32(pStartPtr, end);
69 if( pEndPtr == pStartPtr )
70 return bSuccess;
71 riPos += (pEndPtr - pStartPtr) + 1;
72
73 const char* pKeySrc = pData + riPos;
74 rValue.copyToBuffer( pKeySrc, nKeyLen );
75 riPos += nKeyLen + 1;
76
77 bSuccess = true;
78 return bSuccess;
79}
80
81void Hdf::createHashMap( bool bOptimizeForPerformance )
82{
84 if( bOptimizeForPerformance )
85 {
86 if( m_pStringToDataMap != nullptr )
87 return;
89 }
90 else
91 {
92 if( m_pStringToValPosMap != nullptr )
93 return;
95 }
96
97 Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
98 if( !xIn.is() )
99 return;
100
102 sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
103 sal_Int32 nRead = xIn->readBytes( aData, nSize );
104
105 const char* pData = reinterpret_cast<const char*>(aData.getConstArray());
106 auto const end = pData + nRead;
107 int iPos = 0;
108 while( iPos < nRead )
109 {
110 HDFData aDBKey;
111 if( !implReadLenAndData( pData, end, iPos, aDBKey ) )
112 break;
113
114 OString aOKeyStr = aDBKey.getData();
115
116 // Read val len
117 const char* pStartPtr = pData + iPos;
118 auto [nValLen, pEndPtr] = readInt32(pStartPtr, end);
119 if( pEndPtr == pStartPtr )
120 break;
121
122 iPos += (pEndPtr - pStartPtr) + 1;
123
124 if( bOptimizeForPerformance )
125 {
126 const char* pValSrc = pData + iPos;
127 OString aValStr( pValSrc, nValLen );
128 (*m_pStringToDataMap)[aOKeyStr] = aValStr;
129 }
130 else
131 {
132 // store value start position
133 (*m_pStringToValPosMap)[aOKeyStr] = std::pair<int,int>( iPos, nValLen );
134 }
135 iPos += nValLen + 1;
136 }
137
138 xIn->closeInput();
139}
140
142{
143 m_pStringToDataMap.reset();
144 m_pStringToValPosMap.reset();
145}
146
147
149{
150}
151
152bool Hdf::getValueForKey( const OString& rKey, HDFData& rValue )
153{
154 bool bSuccess = false;
155 if( !m_xSFA.is() )
156 return bSuccess;
157
158 try
159 {
160
161 if( m_pStringToDataMap == nullptr && m_pStringToValPosMap == nullptr )
162 {
163 createHashMap( false/*bOptimizeForPerformance*/ );
164 }
165
166 if( m_pStringToValPosMap != nullptr )
167 {
168 StringToValPosMap::const_iterator it = m_pStringToValPosMap->find( rKey );
169 if( it != m_pStringToValPosMap->end() )
170 {
171 const std::pair<int,int>& rValPair = it->second;
172 int iValuePos = rValPair.first;
173 int nValueLen = rValPair.second;
174
175 Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
176 if( xIn.is() )
177 {
178 Reference< XSeekable > xXSeekable( xIn, UNO_QUERY );
179 if( xXSeekable.is() )
180 {
181 xXSeekable->seek( iValuePos );
182
184 sal_Int32 nRead = xIn->readBytes( aData, nValueLen );
185 if( nRead == nValueLen )
186 {
187 const char* pData = reinterpret_cast<const char*>(aData.getConstArray());
188 rValue.copyToBuffer( pData, nValueLen );
189 bSuccess = true;
190 }
191 }
192 xIn->closeInput();
193 }
194 }
195 }
196
197 else if( m_pStringToDataMap != nullptr )
198 {
199 StringToDataMap::const_iterator it = m_pStringToDataMap->find( rKey );
200 if( it != m_pStringToDataMap->end() )
201 {
202 const OString& rValueStr = it->second;
203 int nValueLen = rValueStr.getLength();
204 const char* pData = rValueStr.getStr();
205 rValue.copyToBuffer( pData, nValueLen );
206 bSuccess = true;
207 }
208 }
209
210 }
211 catch( Exception & )
212 {
213 bSuccess = false;
214 }
215
216 return bSuccess;
217}
218
220{
221 bool bSuccess = false;
222
223 sal_Int32 nSize = m_xSFA->getSize( m_aFileURL );
224
225 Reference< XInputStream > xIn = m_xSFA->openFileRead( m_aFileURL );
226 if( xIn.is() )
227 {
228 m_nItRead = xIn->readBytes( m_aItData, nSize );
229 if( m_nItRead == nSize )
230 {
231 bSuccess = true;
232 m_iItPos = 0;
233 }
234 else
235 {
237 }
238 }
239
240 return bSuccess;
241}
242
244{
245 bool bSuccess = false;
246
247 if( m_iItPos < m_nItRead )
248 {
249 auto const p = reinterpret_cast<const char*>(m_aItData.getConstArray());
250 if( implReadLenAndData( p, p + m_aItData.size(), m_iItPos, rKey ) )
251 {
252 if( implReadLenAndData( p, p + m_aItData.size(), m_iItPos, rValue ) )
253 bSuccess = true;
254 }
255 }
256
257 return bSuccess;
258}
259
261{
263 m_nItRead = -1;
264 m_iItPos = -1;
265}
266
267} // end of namespace helpdatafileproxy
268
269/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static int getHexWeight(sal_uInt32 nChar)
const char * getData() const
Definition: db.hxx:44
void copyToBuffer(const char *pSrcData, int nSize)
Definition: db.cxx:51
std::unique_ptr< char[]> m_pBuffer
Definition: db.hxx:35
std::unique_ptr< StringToValPosMap > m_pStringToValPosMap
Definition: db.hxx:55
std::unique_ptr< StringToDataMap > m_pStringToDataMap
Definition: db.hxx:54
void releaseHashMap()
Definition: db.cxx:141
static bool implReadLenAndData(const char *pData, char const *end, int &riPos, HDFData &rValue)
Definition: db.cxx:62
bool getNextKeyAndValue(HDFData &rKey, HDFData &rValue)
Definition: db.cxx:243
bool startIteration()
Definition: db.cxx:219
bool getValueForKey(const OString &rKey, HDFData &rValue)
Definition: db.cxx:152
void stopIteration()
Definition: db.cxx:260
void createHashMap(bool bOptimizeForPerformance)
Definition: db.cxx:81
css::uno::Reference< css::ucb::XSimpleFileAccess3 > m_xSFA
Definition: db.hxx:57
css::uno::Sequence< sal_Int8 > m_aItData
Definition: db.hxx:60
OUString m_aFileURL
Definition: db.hxx:53
void * p
sal_Int64 n
std::unique_ptr< sal_Int32[]> pData
constexpr OUStringLiteral aData
@ Exception
std::unordered_map< OString, OString > StringToDataMap
Definition: db.hxx:49
std::unordered_map< OString, std::pair< int, int > > StringToValPosMap
Definition: db.hxx:48
enumrange< T >::Iterator begin(enumrange< T >)
end
sal_Int32 w