2002-09-27 22:01:33 +00:00
|
|
|
// util.cxx - general-purpose utility functions.
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 2002 Curtis L. Olson - http://www.flightgear.org/~curt
|
2002-09-27 22:01:33 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-09-27 22:01:33 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
2011-10-17 16:41:59 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2003-06-11 14:18:24 +00:00
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
2015-03-24 16:11:42 +00:00
|
|
|
#include <cmath>
|
2003-01-24 03:08:23 +00:00
|
|
|
|
2008-04-13 21:12:36 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
|
2003-06-11 14:18:24 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2003-01-24 03:08:23 +00:00
|
|
|
#include <simgear/debug/logstream.hxx>
|
2011-01-19 18:36:04 +00:00
|
|
|
#include <simgear/math/SGLimits.hxx>
|
|
|
|
#include <simgear/math/SGMisc.hxx>
|
2003-01-24 03:08:23 +00:00
|
|
|
|
2015-03-13 17:54:44 +00:00
|
|
|
#include <GUI/MessageBox.hxx>
|
2003-01-24 03:08:23 +00:00
|
|
|
#include "fg_io.hxx"
|
|
|
|
#include "fg_props.hxx"
|
|
|
|
#include "globals.hxx"
|
2002-09-27 22:01:33 +00:00
|
|
|
#include "util.hxx"
|
|
|
|
|
2007-01-31 20:43:10 +00:00
|
|
|
#ifdef OSG_LIBRARY_STATIC
|
|
|
|
#include "osgDB/Registry"
|
|
|
|
#endif
|
2003-01-24 03:08:23 +00:00
|
|
|
|
2011-10-17 16:41:59 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2002-09-27 22:01:33 +00:00
|
|
|
// Originally written by Alex Perry.
|
|
|
|
double
|
|
|
|
fgGetLowPass (double current, double target, double timeratio)
|
|
|
|
{
|
|
|
|
if ( timeratio < 0.0 ) {
|
2007-05-01 17:03:50 +00:00
|
|
|
if ( timeratio < -1.0 ) {
|
2002-09-27 22:01:33 +00:00
|
|
|
// time went backwards; kill the filter
|
|
|
|
current = target;
|
|
|
|
} else {
|
|
|
|
// ignore mildly negative time
|
|
|
|
}
|
|
|
|
} else if ( timeratio < 0.2 ) {
|
|
|
|
// Normal mode of operation; fast
|
|
|
|
// approximation to exp(-timeratio)
|
|
|
|
current = current * (1.0 - timeratio) + target * timeratio;
|
|
|
|
} else if ( timeratio > 5.0 ) {
|
|
|
|
// Huge time step; assume filter has settled
|
|
|
|
current = target;
|
|
|
|
} else {
|
|
|
|
// Moderate time step; non linear response
|
|
|
|
double keep = exp(-timeratio);
|
|
|
|
current = current * keep + target * (1.0 - keep);
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:54:44 +00:00
|
|
|
static string_list read_allowed_paths;
|
|
|
|
static string_list write_allowed_paths;
|
|
|
|
|
2015-11-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* Allowed paths here are absolute, and may contain _one_ *,
|
|
|
|
* which matches any string
|
|
|
|
*/
|
2015-03-13 17:54:44 +00:00
|
|
|
void fgInitAllowedPaths()
|
2008-07-11 16:36:54 +00:00
|
|
|
{
|
2016-07-04 08:06:42 +00:00
|
|
|
if(SGPath("ygjmyfvhhnvdoesnotexist").realpath().utf8Str() == "ygjmyfvhhnvdoesnotexist"){
|
2015-11-21 21:37:19 +00:00
|
|
|
// Forbid using this version of fgValidatePath() with older
|
|
|
|
// (not normalizing non-existent files) versions of realpath(),
|
|
|
|
// as that would be a security hole
|
2017-04-09 16:38:38 +00:00
|
|
|
flightgear::fatalMessageBoxThenExit(
|
|
|
|
"Nasal initialization error",
|
|
|
|
"Version mismatch - please update simgear");
|
2015-11-21 21:37:19 +00:00
|
|
|
}
|
2015-03-13 17:54:44 +00:00
|
|
|
read_allowed_paths.clear();
|
|
|
|
write_allowed_paths.clear();
|
2016-06-21 11:29:04 +00:00
|
|
|
|
2016-10-18 21:35:10 +00:00
|
|
|
PathList read_paths = globals->get_extra_read_allowed_paths();
|
2016-10-09 14:21:09 +00:00
|
|
|
read_paths.push_back(globals->get_fg_root());
|
|
|
|
read_paths.push_back(globals->get_fg_home());
|
2015-11-22 11:27:39 +00:00
|
|
|
|
2016-10-09 14:21:09 +00:00
|
|
|
for( PathList::const_iterator it = read_paths.begin(); it != read_paths.end(); ++it )
|
2016-01-12 10:19:26 +00:00
|
|
|
{
|
|
|
|
// if we get the initialization order wrong, better to have an
|
|
|
|
// obvious error than a can-read-everything security hole...
|
2016-10-09 14:21:09 +00:00
|
|
|
if (it->isNull()) {
|
2017-04-09 16:38:38 +00:00
|
|
|
flightgear::fatalMessageBoxThenExit(
|
|
|
|
"Nasal initialization error",
|
|
|
|
"Empty string in FG_ROOT, FG_HOME, FG_AIRCRAFT, FG_SCENERY or "
|
|
|
|
"--allow-nasal-read, or fgInitAllowedPaths() called too early");
|
2016-01-12 10:19:26 +00:00
|
|
|
}
|
2016-07-04 21:22:32 +00:00
|
|
|
read_allowed_paths.push_back(it->realpath().utf8Str() + "/*");
|
2016-07-04 08:06:42 +00:00
|
|
|
read_allowed_paths.push_back(it->realpath().utf8Str());
|
2015-11-22 11:27:39 +00:00
|
|
|
}
|
2016-10-09 14:21:09 +00:00
|
|
|
|
|
|
|
std::string fg_home = globals->get_fg_home().realpath().utf8Str();
|
2016-07-04 21:22:32 +00:00
|
|
|
write_allowed_paths.push_back(fg_home + "/*.sav");
|
|
|
|
write_allowed_paths.push_back(fg_home + "/*.log");
|
|
|
|
write_allowed_paths.push_back(fg_home + "/cache/*");
|
|
|
|
write_allowed_paths.push_back(fg_home + "/Export/*");
|
|
|
|
write_allowed_paths.push_back(fg_home + "/state/*.xml");
|
|
|
|
write_allowed_paths.push_back(fg_home + "/aircraft-data/*.xml");
|
|
|
|
write_allowed_paths.push_back(fg_home + "/Wildfire/*.xml");
|
2018-04-13 07:08:07 +00:00
|
|
|
write_allowed_paths.push_back(fg_home + "/runtime-jetways/*.xml");
|
2016-07-04 21:22:32 +00:00
|
|
|
write_allowed_paths.push_back(fg_home + "/Input/Joysticks/*.xml");
|
2015-03-13 17:54:44 +00:00
|
|
|
|
2015-03-13 18:07:24 +00:00
|
|
|
// Check that it works
|
2016-06-21 11:29:04 +00:00
|
|
|
std::string homePath = globals->get_fg_home().utf8Str();
|
2016-07-03 22:59:40 +00:00
|
|
|
if(!fgValidatePath(homePath + "/../no.log",true).isNull() ||
|
|
|
|
!fgValidatePath(homePath + "/no.logt",true).isNull() ||
|
|
|
|
!fgValidatePath(homePath + "/nolog",true).isNull() ||
|
|
|
|
!fgValidatePath(homePath + "no.log",true).isNull() ||
|
|
|
|
!fgValidatePath(homePath + "\\..\\no.log",false).isNull() ||
|
|
|
|
fgValidatePath(homePath + "/aircraft-data/yes..xml",true).isNull() ||
|
|
|
|
fgValidatePath(homePath + "/.\\yes.bmp",false).isNull()) {
|
2017-04-09 16:38:38 +00:00
|
|
|
flightgear::fatalMessageBoxThenExit(
|
|
|
|
"Nasal initialization error",
|
|
|
|
"The FG_HOME directory must not be inside any of the FG_ROOT, "
|
|
|
|
"FG_AIRCRAFT, FG_SCENERY or --allow-nasal-read directories",
|
|
|
|
"(check that you have not accidentally included an extra ':', "
|
|
|
|
"as an empty part means the current directory)");
|
2015-03-13 17:54:44 +00:00
|
|
|
}
|
2008-07-11 16:36:54 +00:00
|
|
|
}
|
|
|
|
|
2015-11-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* Check whether Nasal is allowed to access a path
|
|
|
|
* Warning: because this always (not just on Windows) treats both \ and /
|
|
|
|
* as path separators, and accepts relative paths (check-to-use race if
|
|
|
|
* the current directory changes),
|
|
|
|
* always use the returned path not the original one
|
|
|
|
*/
|
2016-07-03 22:59:40 +00:00
|
|
|
SGPath fgValidatePath (const SGPath& path, bool write)
|
2015-03-13 17:54:44 +00:00
|
|
|
{
|
2015-11-21 21:37:19 +00:00
|
|
|
// Normalize the path (prevents ../../.. or symlink trickery)
|
2016-07-04 08:06:42 +00:00
|
|
|
std::string normed_path = path.realpath().utf8Str();
|
2015-11-21 21:37:19 +00:00
|
|
|
|
2015-03-13 17:54:44 +00:00
|
|
|
const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
|
2015-03-13 21:57:03 +00:00
|
|
|
size_t star_pos;
|
2015-03-13 17:54:44 +00:00
|
|
|
|
|
|
|
// Check against each allowed pattern
|
|
|
|
for( string_list::const_iterator it = allowed_paths.begin();
|
|
|
|
it != allowed_paths.end();
|
|
|
|
++it )
|
|
|
|
{
|
|
|
|
star_pos = it->find('*');
|
|
|
|
if (star_pos == std::string::npos) {
|
|
|
|
if (!(it->compare(normed_path))) {
|
2016-07-03 22:59:40 +00:00
|
|
|
return SGPath::fromUtf8(normed_path);
|
2015-03-13 17:54:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
|
|
|
|
&& !(it->substr(0,star_pos)
|
|
|
|
.compare(normed_path.substr(0,star_pos))) /* before-star parts match */
|
|
|
|
&& !(it->substr(star_pos+1,it->size()-star_pos-1)
|
|
|
|
.compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
|
|
|
|
it->size()-star_pos-1))) /* after-star parts match */) {
|
2016-07-03 22:59:40 +00:00
|
|
|
return SGPath::fromUtf8(normed_path);
|
2015-03-13 17:54:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no match found
|
2016-07-03 22:59:40 +00:00
|
|
|
return SGPath();
|
2015-03-13 17:54:44 +00:00
|
|
|
}
|
2016-07-03 22:59:40 +00:00
|
|
|
|
2002-09-27 22:01:33 +00:00
|
|
|
// end of util.cxx
|
|
|
|
|