diff --git a/src/ezc.cpp b/src/ezc.cpp index 767d1d0..3b7058e 100644 --- a/src/ezc.cpp +++ b/src/ezc.cpp @@ -186,27 +186,51 @@ void Pattern::Clear() -bool Pattern::CheckFileName(const char * name) +bool Pattern::HasFileAtBeginning(const char * path, const char * file) { -size_t i; + for(; *path && *file; ++path, ++file) + { + if( *path != *file ) + return false; + } - // we do not allow a slash or backslash in the name - for(i=0 ; name[i]!=0 ; ++i) - if( name[i] == '/' || name[i] == '\\' ) + if( *file != 0 ) + return false; + + // "\" is from a dos path syntax + if( *path==0 || *path=='\\' || *path=='/' ) + return true; + +return false; +} + + +/* + ".." is not allowed in the file path + you cannot go up from your template directory +*/ +bool Pattern::IsFileCorrect(const char * name) +{ + while( *name ) + { + if( HasFileAtBeginning(name, "..") ) return false; - // "." is not allowed - if( i==1 && name[0]=='.' ) - return false; + // looking for the next slash of backslash + while( *name && *name!='\\' && *name!='/' ) + name += 1; - // ".." is not allowed too - if( i==2 && name[0]=='.' && name[1]=='.' ) - return false; + // skipping the slash (or backslash) + if( *name ) + name += 1; + } return true; } + + /* 'name' must be a relative path - without a slash or backslash */ @@ -221,7 +245,7 @@ std::string Pattern::ReadFile(const std::string & name) */ std::string Pattern::ReadFile(const char * name) { - if( !CheckFileName(name) ) + if( !IsFileCorrect(name) ) return CreateMsg("incorrect file name:", name); std::string result; @@ -1321,7 +1345,6 @@ void Generator::MakeTextIfoneno(Pattern::Item & item) void Generator::MakeTextIs(Pattern::Item & item) { bool info_res1, info_res2; -bool res = false; if( item.functions.size() != 2 ) return; @@ -1339,7 +1362,6 @@ bool res = false; void Generator::MakeTextIsno(Pattern::Item & item) { bool info_res1, info_res2; -bool res = false; if( item.functions.size() != 2 ) return; diff --git a/src/ezc.h b/src/ezc.h index faf4a88..110b0fc 100644 --- a/src/ezc.h +++ b/src/ezc.h @@ -153,7 +153,8 @@ private: std::string ReadFile(const std::string & name); std::string ReadFile(const char * name); - bool CheckFileName(const char * name); + bool HasFileAtBeginning(const char * path, const char * file); + bool IsFileCorrect(const char * name); bool ReadFileFromDir(const std::string & dir, const char * name, std::string & result); int ReadCharInText();