]> git.mxchange.org Git - flightgear.git/blob - src/Main/util.cxx
Fix a typo breaking some takeoff-state logic.
[flightgear.git] / src / Main / util.cxx
1 // util.cxx - general-purpose utility functions.
2 // Copyright (C) 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 //
18 // $Id$
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <simgear/compiler.h>
25
26 #include <cmath>
27
28 #include <cstdlib>
29
30 #include <vector>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/SGLimits.hxx>
34 #include <simgear/math/SGMisc.hxx>
35
36 #include <GUI/MessageBox.hxx>
37 #include "fg_io.hxx"
38 #include "fg_props.hxx"
39 #include "globals.hxx"
40 #include "util.hxx"
41
42 #ifdef OSG_LIBRARY_STATIC
43 #include "osgDB/Registry"
44 #endif
45
46 using std::vector;
47
48 // Originally written by Alex Perry.
49 double
50 fgGetLowPass (double current, double target, double timeratio)
51 {
52     if ( timeratio < 0.0 ) {
53         if ( timeratio < -1.0 ) {
54                                 // time went backwards; kill the filter
55                 current = target;
56         } else {
57                                 // ignore mildly negative time
58         }
59     } else if ( timeratio < 0.2 ) {
60                                 // Normal mode of operation; fast
61                                 // approximation to exp(-timeratio)
62         current = current * (1.0 - timeratio) + target * timeratio;
63     } else if ( timeratio > 5.0 ) {
64                                 // Huge time step; assume filter has settled
65         current = target;
66     } else {
67                                 // Moderate time step; non linear response
68         double keep = exp(-timeratio);
69         current = current * keep + target * (1.0 - keep);
70     }
71
72     return current;
73 }
74
75 static string_list read_allowed_paths;
76 static string_list write_allowed_paths;
77
78 /**
79  * Allowed paths here are absolute, and may contain _one_ *,
80  * which matches any string
81  */
82 void fgInitAllowedPaths()
83 {
84     if(SGPath("ygjmyfvhhnvdoesnotexist").realpath() == "ygjmyfvhhnvdoesnotexist"){
85         // Forbid using this version of fgValidatePath() with older
86         // (not normalizing non-existent files) versions of realpath(),
87         // as that would be a security hole
88         flightgear::fatalMessageBox("Nasal initialization error",
89                                     "Version mismatch - please update simgear",
90                                     "");
91         exit(-1);
92     }
93     read_allowed_paths.clear();
94     write_allowed_paths.clear();
95     std::string fg_root = SGPath(globals->get_fg_root()).realpath();
96     std::string fg_home = SGPath(globals->get_fg_home()).realpath();
97 #if defined(_MSC_VER) /*for MS compilers */ || defined(_WIN32) /*needed for non MS windows compilers like MingW*/
98      std::string sep = "\\";
99 #else
100      std::string sep = "/";
101 #endif
102     read_allowed_paths.push_back(fg_root + sep + "*");
103     read_allowed_paths.push_back(fg_home + sep + "*");
104     string_list const aircraft_paths = globals->get_aircraft_paths();
105     string_list const scenery_paths = globals->get_secure_fg_scenery();
106     // not plain fg_scenery, to avoid making
107     // /sim/terrasync/scenery-dir a security hole
108
109     const string_list * path_lists_to_add[] = {
110       &aircraft_paths,
111       &scenery_paths
112     };
113
114     for( size_t i = 0; i < sizeof(path_lists_to_add)/sizeof(path_lists_to_add[0]); i++ )
115     {
116       for( string_list::const_iterator it = path_lists_to_add[i]->begin(); it != path_lists_to_add[i]->end();++it )
117       {
118         // if we get the initialization order wrong, better to have an
119         // obvious error than a can-read-everything security hole...
120           if (it->empty() || fg_root.empty() || fg_home.empty()){
121               flightgear::fatalMessageBox("Nasal initialization error",
122                  "Empty string in FG_ROOT, FG_HOME, FG_AIRCRAFT or FG_SCENERY",
123                                    "or fgInitAllowedPaths() called too early");
124               exit(-1);
125           }
126           read_allowed_paths.push_back(SGPath(*it).realpath() + sep + "*");
127       }
128     }
129
130     write_allowed_paths.push_back(fg_home + sep + "*.sav");
131     write_allowed_paths.push_back(fg_home + sep + "*.log");
132     write_allowed_paths.push_back(fg_home + sep + "cache" + sep + "*");
133     write_allowed_paths.push_back(fg_home + sep + "Export" + sep + "*");
134     write_allowed_paths.push_back(fg_home + sep + "state" + sep + "*.xml");
135     write_allowed_paths.push_back(fg_home + sep + "aircraft-data" + sep + "*.xml");
136     write_allowed_paths.push_back(fg_home + sep + "Wildfire" + sep + "*.xml");
137     write_allowed_paths.push_back(fg_home + sep + "runtime-jetways" + sep + "*.xml");
138     write_allowed_paths.push_back(fg_home + sep + "Input" + sep + "Joysticks" + sep + "*.xml");
139     
140     // Check that it works
141     if(!fgValidatePath(globals->get_fg_home() + "/../no.log",true).empty() ||
142         !fgValidatePath(globals->get_fg_home() + "/no.logt",true).empty() ||
143         !fgValidatePath(globals->get_fg_home() + "/nolog",true).empty() ||
144         !fgValidatePath(globals->get_fg_home() + "no.log",true).empty() ||
145         !fgValidatePath(globals->get_fg_home() + "\\..\\no.log",false).empty() ||
146         fgValidatePath(globals->get_fg_home() + "/aircraft-data/yes..xml",true).empty() ||
147         fgValidatePath(globals->get_fg_root() + "/.\\yes.bmp",false).empty()) {
148             flightgear::fatalMessageBox("Nasal initialization error",
149                                     "fgInitAllowedPaths() does not work",
150                                     "");
151             exit(-1);
152     }
153 }
154
155 /**
156  * Check whether Nasal is allowed to access a path
157  * Warning: because this always (not just on Windows) treats both \ and /
158  * as path separators, and accepts relative paths (check-to-use race if
159  * the current directory changes),
160  * always use the returned path not the original one
161  */
162 std::string fgValidatePath (const std::string& path, bool write)
163 {
164     // Normalize the path (prevents ../../.. or symlink trickery)
165     std::string normed_path = SGPath(path).realpath();
166     
167     const string_list& allowed_paths(write ? write_allowed_paths : read_allowed_paths);
168     size_t star_pos;
169     
170     // Check against each allowed pattern
171     for( string_list::const_iterator it = allowed_paths.begin();
172                                      it != allowed_paths.end();
173                                    ++it )
174     {
175         star_pos = it->find('*');
176         if (star_pos == std::string::npos) {
177             if (!(it->compare(normed_path))) {
178                 return normed_path;
179             }
180         } else {
181             if ((it->size()-1 <= normed_path.size()) /* long enough to be a potential match */
182                 && !(it->substr(0,star_pos)
183                     .compare(normed_path.substr(0,star_pos))) /* before-star parts match */
184                 && !(it->substr(star_pos+1,it->size()-star_pos-1)
185                     .compare(normed_path.substr(star_pos+1+normed_path.size()-it->size(),
186                       it->size()-star_pos-1))) /* after-star parts match */) {
187                 return normed_path;
188             }
189         }
190     }
191     // no match found
192     return "";
193 }
194 std::string fgValidatePath(const SGPath& path, bool write) { return fgValidatePath(path.str(),write); }
195 // end of util.cxx
196