Line data Source code
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 "dp_misc.h"
21 : #include "dp_persmap.h"
22 : #include "rtl/strbuf.hxx"
23 :
24 : #ifndef DISABLE_BDB2PMAP
25 : # include <vector>
26 : #endif
27 :
28 : using namespace ::rtl;
29 :
30 : // the persistent map is used to manage a handful of key-value string pairs
31 : // this implementation replaces a rather heavy-weight berkeleydb integration
32 :
33 : // the file backing up a persistent map consists of line pairs with
34 : // - a key string (encoded with chars 0x00..0x0F being escaped)
35 : // - a value string (encoded with chars 0x00..0x0F being escaped)
36 :
37 : namespace dp_misc
38 : {
39 :
40 : static const char PmapMagic[4] = {'P','m','p','1'};
41 :
42 0 : PersistentMap::PersistentMap( OUString const & url_, bool readOnly )
43 : : m_MapFile( expandUnoRcUrl(url_) )
44 : , m_bReadOnly( readOnly )
45 : , m_bIsOpen( false )
46 0 : , m_bToBeCreated( !readOnly )
47 0 : , m_bIsDirty( false )
48 : {
49 : #ifndef DISABLE_BDB2PMAP
50 0 : m_MapFileName = expandUnoRcUrl( url_ );
51 : #endif
52 0 : open();
53 0 : }
54 :
55 0 : PersistentMap::PersistentMap()
56 : : m_MapFile( OUString() )
57 : , m_bReadOnly( false )
58 : , m_bIsOpen( false )
59 : , m_bToBeCreated( false )
60 0 : , m_bIsDirty( false )
61 0 : {}
62 :
63 0 : PersistentMap::~PersistentMap()
64 : {
65 0 : if( m_bIsDirty )
66 0 : flush();
67 0 : if( m_bIsOpen )
68 0 : m_MapFile.close();
69 0 : }
70 :
71 :
72 : // replace 0x00..0x0F with "%0".."%F"
73 : // replace "%" with "%%"
74 0 : static OString encodeString( const OString& rStr)
75 : {
76 0 : const sal_Char* pChar = rStr.getStr();
77 0 : const sal_Int32 nLen = rStr.getLength();
78 0 : sal_Int32 i = nLen;
79 : // short circuit for the simple non-encoded case
80 0 : while( --i >= 0)
81 : {
82 0 : const sal_Char c = *(pChar++);
83 0 : if( (0x00 <= c) && (c <= 0x0F))
84 0 : break;
85 0 : if( c == '%')
86 0 : break;
87 : }
88 0 : if( i < 0)
89 0 : return rStr;
90 :
91 : // escape chars 0x00..0x0F with "%0".."%F"
92 0 : OStringBuffer aEncStr( nLen + 32);
93 0 : aEncStr.append( pChar - (nLen-i), nLen - i);
94 0 : while( --i >= 0)
95 : {
96 0 : sal_Char c = *(pChar++);
97 0 : if( (0x00 <= c) && (c <= 0x0F))
98 : {
99 0 : aEncStr.append( '%');
100 0 : c += (c <= 0x09) ? '0' : 'A'-10;
101 0 : } else if( c == '%')
102 0 : aEncStr.append( '%');
103 0 : aEncStr.append( c);
104 : }
105 :
106 0 : return aEncStr.makeStringAndClear();
107 : }
108 :
109 : // replace "%0".."%F" with 0x00..0x0F
110 : // replace "%%" with "%"
111 0 : static OString decodeString( const sal_Char* pEncChars, int nLen)
112 : {
113 0 : const char* pChar = pEncChars;
114 0 : sal_Int32 i = nLen;
115 : // short circuit for the simple non-encoded case
116 0 : while( --i >= 0)
117 0 : if( *(pChar++) == '%')
118 0 : break;
119 0 : if( i < 0)
120 0 : return OString( pEncChars, nLen);
121 :
122 : // replace escaped chars with their decoded counterparts
123 0 : OStringBuffer aDecStr( nLen);
124 0 : pChar = pEncChars;
125 0 : for( i = nLen; --i >= 0;)
126 : {
127 0 : sal_Char c = *(pChar++);
128 : // handle escaped character
129 0 : if( c == '%')
130 : {
131 0 : --i;
132 : OSL_ASSERT( i >= 0);
133 0 : c = *(pChar++);
134 0 : if( ('0' <= c) && (c <= '9'))
135 0 : c -= '0';
136 : else
137 : {
138 : OSL_ASSERT( ('A' <= c) && (c <= 'F'));
139 0 : c -= ('A'-10);
140 : }
141 : }
142 0 : aDecStr.append( c);
143 : }
144 :
145 0 : return aDecStr.makeStringAndClear();
146 : }
147 :
148 0 : bool PersistentMap::open()
149 : {
150 : // open the existing file
151 0 : sal_uInt32 nOpenFlags = osl_File_OpenFlag_Read;
152 0 : if( !m_bReadOnly)
153 0 : nOpenFlags |= osl_File_OpenFlag_Write;
154 :
155 0 : const osl::File::RC rcOpen = m_MapFile.open( nOpenFlags);
156 0 : m_bIsOpen = (rcOpen == osl::File::E_None);
157 :
158 : // or create later if needed
159 0 : m_bToBeCreated &= (rcOpen == osl::File::E_NOENT) && !m_bIsOpen;
160 :
161 : #ifndef DISABLE_BDB2PMAP
162 0 : if( m_bToBeCreated )
163 0 : importFromBDB();
164 : #endif
165 :
166 0 : if( !m_bIsOpen)
167 0 : return m_bToBeCreated;
168 :
169 0 : return readAll();
170 : }
171 :
172 : //______________________________________________________________________________
173 0 : bool PersistentMap::readAll()
174 : {
175 : // prepare for re-reading the map-file
176 0 : const osl::FileBase::RC nRes = m_MapFile.setPos( osl_Pos_Absolut, 0);
177 : (void)nRes;
178 0 : m_entries.clear();
179 :
180 : // read header and check magic
181 : char aHeaderBytes[ sizeof(PmapMagic)];
182 0 : sal_uInt64 nBytesRead = 0;
183 0 : m_MapFile.read( aHeaderBytes, sizeof(aHeaderBytes), nBytesRead);
184 : OSL_ASSERT( nBytesRead == sizeof(aHeaderBytes));
185 0 : if( nBytesRead != sizeof(aHeaderBytes))
186 0 : return false;
187 : // check header magic
188 0 : for( int i = 0; i < (int)sizeof(PmapMagic); ++i)
189 0 : if( aHeaderBytes[i] != PmapMagic[i])
190 0 : return false;
191 :
192 : // read key value pairs and add them to the map
193 0 : ByteSequence aKeyLine;
194 0 : ByteSequence aValLine;
195 0 : for(;;)
196 : {
197 : // read key-value line pair
198 : // an empty key name indicates the end of the line pairs
199 0 : if( m_MapFile.readLine( aKeyLine) != osl::File::E_None)
200 0 : return false;
201 0 : if( !aKeyLine.getLength())
202 : break;
203 0 : if( m_MapFile.readLine( aValLine) != osl::File::E_None)
204 0 : return false;
205 : // decode key and value strings
206 0 : const OString aKeyName = decodeString( (sal_Char*)aKeyLine.getConstArray(), aKeyLine.getLength());
207 0 : const OString aValName = decodeString( (sal_Char*)aValLine.getConstArray(), aValLine.getLength());
208 : // insert key-value pair into map
209 0 : add( aKeyName, aValName );
210 : // check end-of-file status
211 0 : sal_Bool bIsEOF = true;
212 0 : if( m_MapFile.isEndOfFile( &bIsEOF) != osl::File::E_None )
213 0 : return false;
214 0 : if( bIsEOF )
215 : break;
216 0 : }
217 :
218 0 : m_bIsDirty = false;
219 0 : return true;
220 : }
221 :
222 0 : void PersistentMap::flush()
223 : {
224 0 : if( !m_bIsDirty)
225 : return;
226 : OSL_ASSERT( !m_bReadOnly);
227 0 : if( m_bToBeCreated && !m_entries.empty())
228 : {
229 0 : const sal_uInt32 nOpenFlags = osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create;
230 0 : const osl::File::RC rcOpen = m_MapFile.open( nOpenFlags);
231 0 : m_bIsOpen = (rcOpen == osl::File::E_None);
232 0 : m_bToBeCreated = !m_bIsOpen;
233 : }
234 0 : if( !m_bIsOpen)
235 : return;
236 :
237 : // write header magic
238 0 : const osl::FileBase::RC nRes = m_MapFile.setPos( osl_Pos_Absolut, 0);
239 : (void)nRes;
240 0 : sal_uInt64 nBytesWritten = 0;
241 0 : m_MapFile.write( PmapMagic, sizeof(PmapMagic), nBytesWritten);
242 :
243 : // write key value pairs
244 0 : t_string2string_map::const_iterator it = m_entries.begin();
245 0 : for(; it != m_entries.end(); ++it) {
246 : // write line for key
247 0 : const OString aKeyString = encodeString( (*it).first);
248 0 : const sal_Int32 nKeyLen = aKeyString.getLength();
249 0 : m_MapFile.write( aKeyString.getStr(), nKeyLen, nBytesWritten);
250 : OSL_ASSERT( nKeyLen == (sal_Int32)nBytesWritten);
251 0 : m_MapFile.write( "\n", 1, nBytesWritten);
252 : // write line for value
253 0 : const OString& rValString = encodeString( (*it).second);
254 0 : const sal_Int32 nValLen = rValString.getLength();
255 0 : m_MapFile.write( rValString.getStr(), nValLen, nBytesWritten);
256 : OSL_ASSERT( nValLen == (sal_Int32)nBytesWritten);
257 0 : m_MapFile.write( "\n", 1, nBytesWritten);
258 0 : }
259 :
260 : // write a file delimiter (an empty key-string)
261 0 : m_MapFile.write( "\n", 1, nBytesWritten);
262 : // truncate file here
263 : sal_uInt64 nNewFileSize;
264 0 : if( m_MapFile.getPos( nNewFileSize) == osl::File::E_None)
265 0 : m_MapFile.setSize( nNewFileSize);
266 : // flush to disk
267 0 : m_MapFile.sync();
268 : // the in-memory map now matches to the file on disk
269 0 : m_bIsDirty = false;
270 : }
271 :
272 0 : bool PersistentMap::has( OString const & key ) const
273 : {
274 0 : return get( NULL, key );
275 : }
276 :
277 0 : bool PersistentMap::get( OString * value, OString const & key ) const
278 : {
279 0 : t_string2string_map::const_iterator it = m_entries.find( key);
280 0 : if( it == m_entries.end())
281 0 : return false;
282 0 : if( value)
283 0 : *value = it->second;
284 0 : return true;
285 : }
286 :
287 0 : void PersistentMap::add( OString const & key, OString const & value )
288 : {
289 0 : if( m_bReadOnly)
290 0 : return;
291 : typedef std::pair<t_string2string_map::iterator,bool> InsertRC;
292 0 : InsertRC r = m_entries.insert( t_string2string_map::value_type(key,value));
293 0 : m_bIsDirty = r.second;
294 : }
295 :
296 : //______________________________________________________________________________
297 0 : void PersistentMap::put( OString const & key, OString const & value )
298 : {
299 0 : add( key, value);
300 : // HACK: flush now as the extension manager does not seem
301 : // to properly destruct this object in some situations
302 0 : if(m_bIsDirty)
303 0 : flush();
304 0 : }
305 :
306 0 : bool PersistentMap::erase( OString const & key, bool flush_immediately )
307 : {
308 0 : if( m_bReadOnly)
309 0 : return false;
310 0 : size_t nCount = m_entries.erase( key);
311 0 : if( !nCount)
312 0 : return false;
313 0 : m_bIsDirty = true;
314 0 : if( flush_immediately)
315 0 : flush();
316 0 : return true;
317 : }
318 :
319 0 : t_string2string_map PersistentMap::getEntries() const
320 : {
321 : // TODO: return by const reference instead?
322 0 : return m_entries;
323 : }
324 :
325 : #ifndef DISABLE_BDB2PMAP
326 0 : bool PersistentMap::importFromBDB()
327 : {
328 0 : if( m_bReadOnly)
329 0 : return false;
330 :
331 : // get the name of its BDB counterpart
332 0 : rtl::OUString aDBName = m_MapFileName;
333 0 : if( !aDBName.endsWithAsciiL( ".pmap", 5))
334 0 : return false;
335 0 : aDBName = aDBName.replaceAt( aDBName.getLength()-5, 5, OUSTR(".db"));
336 :
337 : // open the corresponding BDB file for reading
338 0 : osl::File aDBFile( aDBName);
339 0 : osl::File::RC rc = aDBFile.open( osl_File_OpenFlag_Read);
340 0 : if( rc != osl::File::E_None)
341 0 : return false;
342 0 : sal_uInt64 nFileSize = 0;
343 0 : if( aDBFile.getSize( nFileSize) != osl::File::E_None)
344 0 : return false;
345 :
346 : // read the BDB file
347 0 : std::vector<sal_uInt8> aRawBDB( nFileSize);
348 0 : for( sal_uInt64 nOfs = 0; nOfs < nFileSize;) {
349 0 : sal_uInt64 nBytesRead = 0;
350 0 : rc = aDBFile.read( (void*)&aRawBDB[nOfs], nFileSize - nOfs, nBytesRead);
351 0 : if( (rc != osl::File::E_None) || !nBytesRead)
352 0 : return false;
353 0 : nOfs += nBytesRead;
354 : }
355 :
356 : // check BDB file header for non_encrypted Hash format v4..9
357 0 : if( nFileSize < 0x0100)
358 0 : return false;
359 0 : if( aRawBDB[24] != 0) // only not-encrypted migration
360 0 : return false;
361 0 : if( aRawBDB[25] != 8) // we expect a P_HASHMETA page
362 0 : return false;
363 0 : const bool bLE = (aRawBDB[12]==0x61 && aRawBDB[13]==0x15 && aRawBDB[14]==0x06);
364 0 : const bool bBE = (aRawBDB[15]==0x61 && aRawBDB[14]==0x15 && aRawBDB[13]==0x06);
365 0 : if( bBE == bLE)
366 0 : return false;
367 0 : if( (aRawBDB[16] < 4) || (9 < aRawBDB[16])) // version
368 0 : return false;
369 : const sal_uInt64 nPgSize = bLE
370 0 : ? (aRawBDB[20] + (aRawBDB[21]<<8) + (aRawBDB[22]<<16) + (aRawBDB[23]<<24))
371 0 : : (aRawBDB[23] + (aRawBDB[22]<<8) + (aRawBDB[21]<<16) + (aRawBDB[20]<<24));
372 0 : const int nPgCount = nFileSize / nPgSize;
373 0 : if( nPgCount * nPgSize != nFileSize)
374 0 : return false;
375 :
376 : // find PackageManager's new_style entries
377 : // using a simple heuristic for BDB_Hash pages
378 0 : int nEntryCount = 0;
379 0 : for( int nPgNo = 1; nPgNo < nPgCount; ++nPgNo) {
380 : // parse the next _db_page
381 0 : const sal_uInt8* const pPage = &aRawBDB[ nPgNo * nPgSize];
382 0 : const sal_uInt8* const pEnd = pPage + nPgSize;
383 0 : const int nHfOffset = bLE ? (pPage[22] + (pPage[23]<<8)) : (pPage[23] + (pPage[22]<<8));
384 0 : if( nHfOffset <= 0)
385 0 : continue;
386 0 : const sal_uInt8* pCur = pPage + nHfOffset;
387 : // iterate through the entries
388 0 : for(; pCur < pEnd; ++pCur) {
389 0 : if( pCur[0] != 0x01)
390 0 : continue;
391 : // get the value-candidate
392 0 : const sal_uInt8* pVal = pCur + 1;
393 0 : while( ++pCur < pEnd)
394 0 : if( (*pCur < ' ') || ((*pCur > 0x7F) && (*pCur != 0xFF)))
395 0 : break;
396 0 : if( pCur >= pEnd)
397 : break;
398 0 : if( (pCur[0] != 0x01) || (pCur[1] != 0xFF))
399 0 : continue;
400 0 : const OString aVal( (sal_Char*)pVal, pCur - pVal);
401 : // get the key-candidate
402 0 : const sal_uInt8* pKey = pCur + 1;
403 0 : while( ++pCur < pEnd)
404 0 : if( (*pCur < ' ') || ((*pCur > 0x7F) && (*pCur != 0xFF)))
405 0 : break;
406 0 : if( (pCur < pEnd) && (*pCur > 0x01))
407 0 : continue;
408 0 : const OString aKey( (sal_Char*)pKey, pCur - pKey);
409 0 : --pCur; // prepare for next round by rewinding to end of key-string
410 :
411 : // add the key/value pair
412 0 : add( aKey, aVal);
413 0 : ++nEntryCount;
414 0 : }
415 : }
416 :
417 0 : return (nEntryCount > 0);
418 : }
419 : #endif // DISABLE_BDB2PMAP
420 :
421 : }
422 :
423 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|