Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / rtl / bootstrap.cxx
bloba3ada36a44395b0124804d92eae70a4e3d6763c4
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 .
19 #include <config_folders.h>
21 #include <rtl/bootstrap.h>
22 #include <rtl/bootstrap.hxx>
23 #include <osl/diagnose.h>
24 #include <osl/process.h>
25 #include <osl/file.hxx>
26 #include <osl/mutex.hxx>
27 #include <osl/profile.hxx>
28 #include <osl/security.hxx>
29 #include <rtl/string.hxx>
30 #include <rtl/ustrbuf.hxx>
31 #include <rtl/ustring.hxx>
32 #include <rtl/byteseq.hxx>
33 #include <sal/log.hxx>
34 #include <o3tl/lru_map.hxx>
35 #include <o3tl/string_view.hxx>
37 #include <utility>
38 #include <vector>
39 #include <algorithm>
40 #include <cstddef>
41 #include <string_view>
42 #include <unordered_map>
44 #ifdef ANDROID
45 #include <osl/detail/android-bootstrap.h>
46 #endif
48 #ifdef EMSCRIPTEN
49 #include <osl/detail/emscripten-bootstrap.h>
50 #endif
52 #ifdef IOS
53 #include <premac.h>
54 #import <Foundation/Foundation.h>
55 #include <postmac.h>
56 #endif
58 using osl::DirectoryItem;
59 using osl::FileStatus;
61 namespace
64 struct Bootstrap_Impl;
66 constexpr std::u16string_view VND_SUN_STAR_PATHNAME = u"vnd.sun.star.pathname:";
68 bool isPathnameUrl(std::u16string_view url)
70 return o3tl::matchIgnoreAsciiCase(url, VND_SUN_STAR_PATHNAME);
73 bool resolvePathnameUrl(OUString * url)
75 OSL_ASSERT(url);
76 if (!isPathnameUrl(*url) ||
77 (osl::FileBase::getFileURLFromSystemPath(
78 url->copy(VND_SUN_STAR_PATHNAME.size()), *url) ==
79 osl::FileBase::E_None))
81 return true;
83 *url = OUString();
84 return false;
87 enum class LookupMode {
88 NORMAL, URE_BOOTSTRAP,
89 URE_BOOTSTRAP_EXPANSION };
91 struct ExpandRequestLink {
92 ExpandRequestLink const * next;
93 Bootstrap_Impl const * file;
94 OUString key;
97 OUString expandMacros(
98 Bootstrap_Impl const * file, std::u16string_view text, LookupMode mode,
99 ExpandRequestLink const * requestStack);
101 OUString recursivelyExpandMacros(
102 Bootstrap_Impl const * file, std::u16string_view text, LookupMode mode,
103 Bootstrap_Impl const * requestFile, OUString const & requestKey,
104 ExpandRequestLink const * requestStack)
106 for (; requestStack; requestStack = requestStack->next) {
107 if (requestStack->file == requestFile &&
108 requestStack->key == requestKey)
110 return "***RECURSION DETECTED***";
113 ExpandRequestLink link = { requestStack, requestFile, requestKey };
114 return expandMacros(file, text, mode, &link);
117 struct rtl_bootstrap_NameValue
119 OUString sName;
120 OUString sValue;
122 rtl_bootstrap_NameValue()
124 rtl_bootstrap_NameValue(OUString name, OUString value )
125 : sName(std::move( name )),
126 sValue(std::move( value ))
130 } // end namespace
132 typedef std::vector<rtl_bootstrap_NameValue> NameValueVector;
134 static bool find(
135 NameValueVector const & vector, OUString const & key,
136 OUString * value)
138 OSL_ASSERT(value);
139 auto i = std::find_if(vector.begin(), vector.end(),
140 [&key](const rtl_bootstrap_NameValue& rNameValue) { return rNameValue.sName == key; });
141 if (i != vector.end())
143 *value = i->sValue;
144 return true;
146 return false;
149 namespace
151 NameValueVector rtl_bootstrap_set_vector;
154 static bool getFromCommandLineArgs(
155 OUString const & key, OUString * value )
157 OSL_ASSERT(value);
159 static NameValueVector nameValueVector = []()
161 NameValueVector tmp;
163 sal_Int32 nArgCount = osl_getCommandArgCount();
164 for(sal_Int32 i = 0; i < nArgCount; ++ i)
166 OUString pArg;
167 osl_getCommandArg( i, &pArg.pData );
168 if( (pArg.startsWith("-") || pArg.startsWith("/") ) &&
169 pArg.match("env:", 1) )
171 sal_Int32 nIndex = pArg.indexOf( '=' );
173 if( nIndex >= 0 )
175 rtl_bootstrap_NameValue nameValue;
176 nameValue.sName = pArg.copy( 5, nIndex - 5 );
177 nameValue.sValue = pArg.copy( nIndex+1 );
179 if( i == nArgCount-1 &&
180 nameValue.sValue.getLength() &&
181 nameValue.sValue[nameValue.sValue.getLength()-1] == 13 )
183 // avoid the 13 linefeed for the last argument,
184 // when the executable is started from a script,
185 // that was edited on windows
186 nameValue.sValue = nameValue.sValue.copy(0,nameValue.sValue.getLength()-1);
189 tmp.push_back( nameValue );
193 return tmp;
194 }();
196 return find(nameValueVector, key, value);
199 static void getExecutableDirectory_Impl(rtl_uString ** ppDirURL)
201 OUString fileName;
202 osl_getExecutableFile(&(fileName.pData));
204 sal_Int32 nDirEnd = fileName.lastIndexOf('/');
205 OSL_ENSURE(nDirEnd >= 0, "Cannot locate executable directory");
207 rtl_uString_newFromStr_WithLength(ppDirURL,fileName.getStr(),nDirEnd);
210 static OUString & getIniFileName_Impl()
212 static OUString aStaticName = []() {
213 OUString fileName;
215 #if defined IOS
216 // On iOS hardcode the inifile as "rc" in the .app
217 // directory. Apps are self-contained anyway, there is no
218 // possibility to have several "applications" in the same
219 // installation location with different inifiles.
220 const char *inifile = [[@"vnd.sun.star.pathname:" stringByAppendingString: [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: @"rc"]] UTF8String];
221 fileName = OUString(inifile, strlen(inifile), RTL_TEXTENCODING_UTF8);
222 resolvePathnameUrl(&fileName);
223 #elif defined ANDROID
224 // Apps are self-contained on Android, too, can as well hardcode
225 // it as "rc" in the "/assets" directory, i.e. inside the app's
226 // .apk (zip) archive as the /assets/rc file.
227 fileName = OUString("vnd.sun.star.pathname:/assets/rc");
228 resolvePathnameUrl(&fileName);
229 #elif defined(EMSCRIPTEN)
230 fileName = OUString("vnd.sun.star.pathname:/instdir/program/sofficerc");
231 resolvePathnameUrl(&fileName);
232 #else
233 if (getFromCommandLineArgs("INIFILENAME", &fileName))
235 resolvePathnameUrl(&fileName);
237 else
239 osl_getExecutableFile(&(fileName.pData));
241 // get rid of a potential executable extension
242 OUString progExt = ".bin";
243 if (fileName.getLength() > progExt.getLength()
244 && o3tl::equalsIgnoreAsciiCase(fileName.subView(fileName.getLength() - progExt.getLength()), progExt))
246 fileName = fileName.copy(0, fileName.getLength() - progExt.getLength());
249 progExt = ".exe";
250 if (fileName.getLength() > progExt.getLength()
251 && o3tl::equalsIgnoreAsciiCase(fileName.subView(fileName.getLength() - progExt.getLength()), progExt))
253 fileName = fileName.copy(0, fileName.getLength() - progExt.getLength());
256 // append config file suffix
257 fileName += SAL_CONFIGFILE("");
259 #ifdef MACOSX
260 // We keep only executables in the MacOS folder, and all
261 // rc files in LIBO_ETC_FOLDER (typically "Resources").
262 sal_Int32 off = fileName.lastIndexOf( "/MacOS/" );
263 if (off != -1)
264 fileName = fileName.replaceAt(off + 1, strlen("MacOS"), u"" LIBO_ETC_FOLDER);
265 #endif
267 #endif
269 return fileName;
270 }();
272 return aStaticName;
275 // ensure the given file url has no final slash
277 static void EnsureNoFinalSlash (OUString & url)
279 sal_Int32 i = url.getLength();
281 if (i > 0 && url[i - 1] == '/')
282 url = url.copy(0, i - 1);
285 namespace {
287 struct Bootstrap_Impl
289 sal_Int32 _nRefCount;
290 Bootstrap_Impl * _base_ini;
292 NameValueVector _nameValueVector;
293 OUString _iniName;
295 explicit Bootstrap_Impl (OUString const & rIniName);
296 ~Bootstrap_Impl();
298 static void * operator new (std::size_t n)
299 { return malloc (sal_uInt32(n)); }
300 static void operator delete (void * p , std::size_t)
301 { free (p); }
303 bool getValue(
304 OUString const & key, rtl_uString ** value,
305 rtl_uString * defaultValue, LookupMode mode, bool override,
306 ExpandRequestLink const * requestStack) const;
307 bool getDirectValue(
308 OUString const & key, rtl_uString ** value, LookupMode mode,
309 ExpandRequestLink const * requestStack) const;
310 bool getAmbienceValue(
311 OUString const & key, rtl_uString ** value, LookupMode mode,
312 ExpandRequestLink const * requestStack) const;
313 void expandValue(
314 rtl_uString ** value, OUString const & text, LookupMode mode,
315 Bootstrap_Impl const * requestFile, OUString const & requestKey,
316 ExpandRequestLink const * requestStack) const;
321 Bootstrap_Impl::Bootstrap_Impl( OUString const & rIniName )
322 : _nRefCount( 0 ),
323 _base_ini( nullptr ),
324 _iniName (rIniName)
326 OUString base_ini(getIniFileName_Impl());
327 // normalize path
328 FileStatus status( osl_FileStatus_Mask_FileURL );
329 DirectoryItem dirItem;
330 if (DirectoryItem::get(base_ini, dirItem) == DirectoryItem::E_None &&
331 dirItem.getFileStatus(status) == DirectoryItem::E_None)
333 base_ini = status.getFileURL();
334 if (rIniName != base_ini)
336 _base_ini = static_cast< Bootstrap_Impl * >(
337 rtl_bootstrap_args_open(base_ini.pData));
340 SAL_INFO("sal.bootstrap", "Bootstrap_Impl(): sFile=" << _iniName);
341 oslFileHandle handle;
342 if (!_iniName.isEmpty() &&
343 osl_openFile(_iniName.pData, &handle, osl_File_OpenFlag_Read) == osl_File_E_None)
345 rtl::ByteSequence seq;
347 while (osl_readLine(handle , reinterpret_cast<sal_Sequence **>(&seq)) == osl_File_E_None)
349 OString line(reinterpret_cast<const char *>(seq.getConstArray()), seq.getLength());
350 sal_Int32 nIndex = line.indexOf('=');
351 if (nIndex >= 1)
353 struct rtl_bootstrap_NameValue nameValue;
354 nameValue.sName = OStringToOUString(o3tl::trim(line.subView(0,nIndex)), RTL_TEXTENCODING_ASCII_US);
355 nameValue.sValue = OStringToOUString(o3tl::trim(line.subView(nIndex+1)), RTL_TEXTENCODING_UTF8);
357 SAL_INFO("sal.bootstrap", "pushing: name=" << nameValue.sName << " value=" << nameValue.sValue);
359 _nameValueVector.push_back(nameValue);
362 osl_closeFile(handle);
364 else
366 SAL_INFO( "sal.bootstrap", "couldn't open file: " << _iniName );
370 Bootstrap_Impl::~Bootstrap_Impl()
372 if (_base_ini)
373 rtl_bootstrap_args_close( _base_ini );
376 namespace {
378 Bootstrap_Impl * get_static_bootstrap_handle()
380 static Bootstrap_Impl* s_handle = []() {
381 OUString iniName(getIniFileName_Impl());
382 Bootstrap_Impl* that = static_cast<Bootstrap_Impl*>(rtl_bootstrap_args_open(iniName.pData));
383 if (!that)
385 that = new Bootstrap_Impl(iniName);
386 ++that->_nRefCount;
388 return that;
389 }();
391 return s_handle;
394 struct FundamentalIniData
396 rtlBootstrapHandle ini;
398 FundamentalIniData()
400 OUString uri;
401 ini =
402 (get_static_bootstrap_handle()->getValue(
403 "URE_BOOTSTRAP", &uri.pData, nullptr, LookupMode::NORMAL, false,
404 nullptr)
405 && resolvePathnameUrl(&uri))
406 ? rtl_bootstrap_args_open(uri.pData) : nullptr;
409 ~FundamentalIniData() { rtl_bootstrap_args_close(ini); }
411 FundamentalIniData(const FundamentalIniData&) = delete;
412 FundamentalIniData& operator=(const FundamentalIniData&) = delete;
415 FundamentalIniData& FundamentalIni()
417 static FundamentalIniData SINGLETON;
418 return SINGLETON;
423 bool Bootstrap_Impl::getValue(
424 OUString const & key, rtl_uString ** value, rtl_uString * defaultValue,
425 LookupMode mode, bool override, ExpandRequestLink const * requestStack)
426 const
428 if (mode == LookupMode::NORMAL && key == "URE_BOOTSTRAP")
429 mode = LookupMode::URE_BOOTSTRAP;
431 if (override && getDirectValue(key, value, mode, requestStack))
432 return true;
434 if (key == "_OS")
436 rtl_uString_assign(
437 value, OUString(RTL_OS).pData);
438 return true;
441 if (key == "_ARCH")
443 rtl_uString_assign(
444 value, OUString(RTL_ARCH).pData);
445 return true;
448 if (key == "_CPPU_ENV")
450 rtl_uString_assign(
451 value,
452 (OUString(
453 SAL_STRINGIFY(CPPU_ENV)).
454 pData));
455 return true;
458 #if defined ANDROID || defined EMSCRIPTEN
459 if (key == "APP_DATA_DIR")
461 const char *app_data_dir = lo_get_app_data_dir();
462 rtl_uString_assign(
463 value, OUString(app_data_dir, strlen(app_data_dir), RTL_TEXTENCODING_UTF8).pData);
464 return true;
466 #endif
468 #ifdef IOS
469 if (key == "APP_DATA_DIR")
471 const char *app_data_dir = [[[[NSBundle mainBundle] bundlePath] stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLPathAllowedCharacterSet]] UTF8String];
472 rtl_uString_assign(
473 value, OUString(app_data_dir, strlen(app_data_dir), RTL_TEXTENCODING_UTF8).pData);
474 return true;
476 #endif
478 if (key == "ORIGIN")
480 rtl_uString_assign(
481 value,
482 _iniName.copy(
483 0, std::max<sal_Int32>(0, _iniName.lastIndexOf('/'))).pData);
484 return true;
487 if (getAmbienceValue(key, value, mode, requestStack))
488 return true;
490 if (key == "SYSUSERCONFIG")
492 OUString v;
493 bool b = osl::Security().getConfigDir(v);
494 EnsureNoFinalSlash(v);
495 rtl_uString_assign(value, v.pData);
496 return b;
499 if (key == "SYSUSERHOME")
501 OUString v;
502 bool b = osl::Security().getHomeDir(v);
503 EnsureNoFinalSlash(v);
504 rtl_uString_assign(value, v.pData);
505 return b;
508 if (key == "SYSBINDIR")
510 getExecutableDirectory_Impl(value);
511 return true;
514 if (_base_ini != nullptr && _base_ini->getDirectValue(key, value, mode, requestStack))
515 return true;
517 if (!override && getDirectValue(key, value, mode, requestStack))
518 return true;
520 if (mode == LookupMode::NORMAL)
522 FundamentalIniData const & d = FundamentalIni();
523 Bootstrap_Impl const * b = static_cast<Bootstrap_Impl const *>(d.ini);
524 if (b != nullptr && b != this && b->getDirectValue(key, value, mode, requestStack))
525 return true;
528 if (defaultValue != nullptr)
530 rtl_uString_assign(value, defaultValue);
531 return true;
534 rtl_uString_new(value);
535 return false;
538 bool Bootstrap_Impl::getDirectValue(
539 OUString const & key, rtl_uString ** value, LookupMode mode,
540 ExpandRequestLink const * requestStack) const
542 OUString v;
543 if (find(_nameValueVector, key, &v))
545 expandValue(value, v, mode, this, key, requestStack);
546 return true;
549 return false;
552 bool Bootstrap_Impl::getAmbienceValue(
553 OUString const & key, rtl_uString ** value, LookupMode mode,
554 ExpandRequestLink const * requestStack) const
556 OUString v;
557 bool f;
560 osl::MutexGuard g(osl::Mutex::getGlobalMutex());
561 f = find(rtl_bootstrap_set_vector, key, &v);
564 if (f || getFromCommandLineArgs(key, &v) ||
565 osl_getEnvironment(key.pData, &v.pData) == osl_Process_E_None)
567 expandValue(value, v, mode, nullptr, key, requestStack);
568 return true;
571 return false;
574 void Bootstrap_Impl::expandValue(
575 rtl_uString ** value, OUString const & text, LookupMode mode,
576 Bootstrap_Impl const * requestFile, OUString const & requestKey,
577 ExpandRequestLink const * requestStack) const
579 rtl_uString_assign(
580 value,
581 (mode == LookupMode::URE_BOOTSTRAP && isPathnameUrl(text) ?
582 text :
583 recursivelyExpandMacros(
584 this, text,
585 (mode == LookupMode::URE_BOOTSTRAP ?
586 LookupMode::URE_BOOTSTRAP_EXPANSION : mode),
587 requestFile, requestKey, requestStack)).pData);
590 namespace {
592 typedef std::unordered_map< OUString, Bootstrap_Impl * > bootstrap_map_t;
593 bootstrap_map_t bootstrap_map;
597 rtlBootstrapHandle SAL_CALL rtl_bootstrap_args_open(rtl_uString * pIniName)
599 static o3tl::lru_map<OUString,OUString> fileUrlLookupCache(16);
601 OUString originalIniName( pIniName );
602 OUString iniName;
604 osl::ResettableMutexGuard guard(osl::Mutex::getGlobalMutex());
605 auto cacheIt = fileUrlLookupCache.find(originalIniName);
606 bool foundInCache = cacheIt != fileUrlLookupCache.end();
607 if (foundInCache)
608 iniName = cacheIt->second;
609 guard.clear();
611 // normalize path
612 if (!foundInCache)
614 FileStatus status(osl_FileStatus_Mask_FileURL);
615 DirectoryItem dirItem;
616 if (DirectoryItem::get(originalIniName, dirItem) != DirectoryItem::E_None ||
617 dirItem.getFileStatus(status) != DirectoryItem::E_None)
619 return nullptr;
621 iniName = status.getFileURL();
624 guard.reset();
625 if (!foundInCache)
626 fileUrlLookupCache.insert({originalIniName, iniName});
627 Bootstrap_Impl * that;
628 auto iFind(bootstrap_map.find(iniName));
629 if (iFind == bootstrap_map.end())
631 guard.clear();
632 that = new Bootstrap_Impl(iniName);
633 guard.reset();
634 iFind = bootstrap_map.find(iniName);
635 if (iFind == bootstrap_map.end())
637 ++that->_nRefCount;
638 ::std::pair< bootstrap_map_t::iterator, bool > insertion(
639 bootstrap_map.emplace(iniName, that));
640 OSL_ASSERT(insertion.second);
642 else
644 Bootstrap_Impl * obsolete = that;
645 that = iFind->second;
646 ++that->_nRefCount;
647 guard.clear();
648 delete obsolete;
651 else
653 that = iFind->second;
654 ++that->_nRefCount;
656 return static_cast< rtlBootstrapHandle >( that );
659 void SAL_CALL rtl_bootstrap_args_close(rtlBootstrapHandle handle) SAL_THROW_EXTERN_C()
661 if (!handle)
662 return;
664 Bootstrap_Impl * that = static_cast< Bootstrap_Impl * >( handle );
666 osl::MutexGuard guard(osl::Mutex::getGlobalMutex());
667 OSL_ASSERT(bootstrap_map.find(that->_iniName)->second == that);
668 --that->_nRefCount;
670 if (that->_nRefCount != 0)
671 return;
673 std::size_t const nLeaking = 8; // only hold up to 8 files statically
674 if (bootstrap_map.size() > nLeaking)
676 ::std::size_t erased = bootstrap_map.erase( that->_iniName );
677 if (erased != 1) {
678 OSL_ASSERT( false );
680 delete that;
684 sal_Bool SAL_CALL rtl_bootstrap_get_from_handle(
685 rtlBootstrapHandle handle,
686 rtl_uString * pName,
687 rtl_uString ** ppValue,
688 rtl_uString * pDefault
691 osl::MutexGuard guard(osl::Mutex::getGlobalMutex());
693 bool found = false;
694 if(ppValue && pName)
696 if (!handle)
697 handle = get_static_bootstrap_handle();
699 found = static_cast< Bootstrap_Impl * >(handle)->getValue(
700 pName, ppValue, pDefault, LookupMode::NORMAL, false, nullptr );
703 return found;
706 void SAL_CALL rtl_bootstrap_get_iniName_from_handle (
707 rtlBootstrapHandle handle,
708 rtl_uString ** ppIniName
711 if(!ppIniName)
712 return;
714 if(handle)
716 Bootstrap_Impl * pImpl = static_cast<Bootstrap_Impl*>(handle);
717 rtl_uString_assign(ppIniName, pImpl->_iniName.pData);
719 else
721 const OUString & iniName = getIniFileName_Impl();
722 rtl_uString_assign(ppIniName, iniName.pData);
726 void SAL_CALL rtl_bootstrap_setIniFileName (
727 rtl_uString * pName
730 osl::MutexGuard guard(osl::Mutex::getGlobalMutex());
731 OUString & file = getIniFileName_Impl();
732 file = pName;
735 sal_Bool SAL_CALL rtl_bootstrap_get (
736 rtl_uString * pName,
737 rtl_uString ** ppValue,
738 rtl_uString * pDefault
741 return rtl_bootstrap_get_from_handle(nullptr, pName, ppValue, pDefault);
744 void SAL_CALL rtl_bootstrap_set (
745 rtl_uString * pName,
746 rtl_uString * pValue
749 const OUString name(pName);
750 const OUString value(pValue);
752 osl::MutexGuard guard(osl::Mutex::getGlobalMutex());
754 for (auto & item : rtl_bootstrap_set_vector)
756 if (item.sName == name)
758 item.sValue = value;
759 return;
763 SAL_INFO("sal.bootstrap", "explicitly setting: name=" << name << " value=" <<value);
765 rtl_bootstrap_set_vector.emplace_back(name, value);
768 void SAL_CALL rtl_bootstrap_expandMacros_from_handle(
769 rtlBootstrapHandle handle,
770 rtl_uString ** macro)
772 if (!handle)
773 handle = get_static_bootstrap_handle();
775 OUString expanded(expandMacros(static_cast< Bootstrap_Impl * >(handle),
776 OUString::unacquired(macro),
777 LookupMode::NORMAL, nullptr));
778 rtl_uString_assign(macro, expanded.pData);
781 void SAL_CALL rtl_bootstrap_expandMacros(rtl_uString ** macro)
783 rtl_bootstrap_expandMacros_from_handle(nullptr, macro);
786 void rtl_bootstrap_encode(rtl_uString const * value, rtl_uString ** encoded)
788 OSL_ASSERT(value);
789 OUStringBuffer b(value->length+5);
790 for (sal_Int32 i = 0; i < value->length; ++i)
792 sal_Unicode c = value->buffer[i];
793 if (c == '$' || c == '\\')
794 b.append('\\');
796 b.append(c);
799 rtl_uString_assign(encoded, b.makeStringAndClear().pData);
802 namespace {
804 int hex(sal_Unicode c)
806 return
807 c >= '0' && c <= '9' ? c - '0' :
808 c >= 'A' && c <= 'F' ? c - 'A' + 10 :
809 c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
812 sal_Unicode read(std::u16string_view text, std::size_t * pos, bool * escaped)
814 OSL_ASSERT(pos && *pos < text.length() && escaped);
815 sal_Unicode c = text[(*pos)++];
816 if (c == '\\')
818 int n1, n2, n3, n4;
819 if (*pos < text.length() - 4 && text[*pos] == 'u' &&
820 ((n1 = hex(text[*pos + 1])) >= 0) &&
821 ((n2 = hex(text[*pos + 2])) >= 0) &&
822 ((n3 = hex(text[*pos + 3])) >= 0) &&
823 ((n4 = hex(text[*pos + 4])) >= 0))
825 *pos += 5;
826 *escaped = true;
827 return static_cast< sal_Unicode >(
828 (n1 << 12) | (n2 << 8) | (n3 << 4) | n4);
831 if (*pos < text.length())
833 *escaped = true;
834 return text[(*pos)++];
838 *escaped = false;
839 return c;
842 OUString lookup(
843 Bootstrap_Impl const * file, LookupMode mode, bool override,
844 OUString const & key, ExpandRequestLink const * requestStack)
846 OUString v;
847 (file == nullptr ? get_static_bootstrap_handle() : file)->getValue(
848 key, &v.pData, nullptr, mode, override, requestStack);
849 return v;
852 OUString expandMacros(
853 Bootstrap_Impl const * file, std::u16string_view text, LookupMode mode,
854 ExpandRequestLink const * requestStack)
856 SAL_INFO("sal.bootstrap", "expandMacros called with: " << OUString(text));
857 OUStringBuffer buf(2048);
859 for (std::size_t i = 0; i < text.length();)
861 bool escaped;
862 sal_Unicode c = read(text, &i, &escaped);
863 if (escaped || c != '$')
865 buf.append(c);
867 else
869 if (i < text.length() && text[i] == '{')
871 ++i;
872 std::size_t p = i;
873 sal_Int32 nesting = 0;
874 OUString seg[3];
875 int n = 0;
877 while (i < text.length())
879 std::size_t j = i;
880 c = read(text, &i, &escaped);
882 if (!escaped)
884 switch (c)
886 case '{':
887 ++nesting;
888 break;
889 case '}':
890 if (nesting == 0)
892 seg[n++] = text.substr(p, j - p);
893 goto done;
895 else
897 --nesting;
899 break;
900 case ':':
901 if (nesting == 0 && n < 2)
903 seg[n++] = text.substr(p, j - p);
904 p = i;
906 break;
910 done:
911 for (int j = 0; j < n; ++j)
913 seg[j] = expandMacros(file, seg[j], mode, requestStack);
916 if (n == 3 && seg[0] != ".override" && seg[1].isEmpty())
918 // For backward compatibility, treat ${file::key} the
919 // same as just ${file:key}:
920 seg[1] = seg[2];
921 n = 2;
924 if (n == 1)
926 buf.append(lookup(file, mode, false, seg[0], requestStack));
928 else if (n == 2)
930 rtl::Bootstrap b(seg[0]);
931 Bootstrap_Impl * f = static_cast< Bootstrap_Impl * >(b.getHandle());
932 buf.append(lookup(f, mode, false, seg[1], requestStack));
934 else if (n == 3 && seg[0] == ".override")
936 rtl::Bootstrap b(seg[1]);
937 Bootstrap_Impl * f = static_cast< Bootstrap_Impl * >(b.getHandle());
938 buf.append(lookup(f, mode, f != nullptr, seg[2], requestStack));
940 else
942 // Going through osl::Profile, this code erroneously
943 // does not recursively expand macros in the resulting
944 // replacement text (and if it did, it would fail to
945 // detect cycles that pass through here):
946 buf.append(
947 OStringToOUString(
948 osl::Profile(seg[0]).readString(
949 OUStringToOString(
950 seg[1], RTL_TEXTENCODING_UTF8),
951 OUStringToOString(
952 seg[2], RTL_TEXTENCODING_UTF8),
953 OString()),
954 RTL_TEXTENCODING_UTF8));
957 else
959 OUStringBuffer kbuf(sal_Int32(text.length()));
960 for (; i < text.length();)
962 std::size_t j = i;
963 c = read(text, &j, &escaped);
964 if (!escaped &&
965 (c == ' ' || c == '$' || c == '-' || c == '/' ||
966 c == ';' || c == '\\'))
968 break;
971 kbuf.append(c);
972 i = j;
975 buf.append(
976 lookup(
977 file, mode, false, kbuf.makeStringAndClear(),
978 requestStack));
983 OUString result(buf.makeStringAndClear());
984 SAL_INFO("sal.bootstrap", "expandMacros result: " << result);
986 return result;
991 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */