From: Josh Date: Mon, 9 Jun 2014 23:00:50 +0000 (-0400) Subject: changing portvar parser to a 'var' parser X-Git-Tag: 3.0.0-233~1481^2~2^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8a27fdfa7ab4b38ea07bd2dcf2c82b9b594ced6;p=thirdparty%2Fsnort3.git changing portvar parser to a 'var' parser --- diff --git a/tools/snort2lua/CMakeLists.txt b/tools/snort2lua/CMakeLists.txt index 9e89e35fa..a3b9a72a8 100644 --- a/tools/snort2lua/CMakeLists.txt +++ b/tools/snort2lua/CMakeLists.txt @@ -1,6 +1,6 @@ -#include_directories(${CMAKE_CURRENT_SOURCE_DIRECTORY}) +include_directories("${CMAKE_CURRENT_SOURCE_DIR}") add_subdirectory(keyword_states) @@ -10,6 +10,8 @@ add_executable(snort2lua converter.cc init_state.h init_state.cc + snort2lua_util.h + snort2lua_util.cc ) target_link_libraries( snort2lua diff --git a/tools/snort2lua/converter.cc b/tools/snort2lua/converter.cc index be7e7a298..448751d14 100644 --- a/tools/snort2lua/converter.cc +++ b/tools/snort2lua/converter.cc @@ -21,8 +21,11 @@ #include #include "converter.h" +#include "conversion_state.h" #include "init_state.h" +ConversionState* Converter::state = nullptr; + #if 0 Converter::Converter(Converter* c) { @@ -32,22 +35,52 @@ Converter::Converter(Converter* c) Converter::~Converter(){} #endif +void Converter::set_state(ConversionState* c) +{ + delete state; + state = c; +} + void Converter::reset_state() -{ - set_state(new InitState); +{ + set_state(new InitState(this)); } -bool Converter::convert_line(std::string& data, std::ofstream& out){ +bool Converter::convert_line(std::stringstream& data, bool last_line, std::ofstream& out){ if ( state ) - return state->convert(data, out); - - std::cout << "Converter:: must call reset state first!" << std::endl; + return state->convert(data, last_line, out); return false; } + +void Converter::print_line(std::stringstream& in) +{ + int pos = in.tellg(); + std::ostringstream oss; + oss << in.rdbuf(); + std::cout << "DEBUG: " << oss.str() << std::endl; + in.seekg(pos); +} + +void Converter::print_line(std::ostringstream& in) +{ + std::cout << "DEBUG: " << in.str() << std::endl; +} +void Converter::print_line(std::string& in) +{ + std::cout << "DEBUG: " << in << std::endl; +} + #if 0 +void Converter::inititalize() +{ + state = this; +} + + + void Converter::set_state(Converter* c){ delete state; state = c; diff --git a/tools/snort2lua/converter.h b/tools/snort2lua/converter.h index b01405c28..a97ceb506 100644 --- a/tools/snort2lua/converter.h +++ b/tools/snort2lua/converter.h @@ -23,44 +23,29 @@ #define CONVERTER_H #include +#include +#include - +class ConversionState; class Converter { public: + Converter(){} virtual ~Converter() {}; void reset_state(); - bool convert_line(std::string& data, std::ofstream& out); - virtual bool convert(std::string& data, std::ofstream& out)=0; - -protected: - Converter(Converter* c){ - state = c; - } - void set_state(Converter* c) - { - delete state; - state = c; - } - + bool convert_line(std::stringstream& data, bool last_line, std::ofstream& out); + void set_state(ConversionState* c); + void print_line(std::stringstream& in); + void print_line(std::ostringstream& in); + void print_line(std::string& in); private: - Converter* state; + static ConversionState* state; }; -typedef Converter* (*conv_new_f)(); - -struct ConvertMap -{ - std::string keyword; - conv_new_f ctor; -}; - - - #endif diff --git a/tools/snort2lua/init_state.cc b/tools/snort2lua/init_state.cc index 492c88923..a94ccb3fc 100644 --- a/tools/snort2lua/init_state.cc +++ b/tools/snort2lua/init_state.cc @@ -22,16 +22,16 @@ #include #include +#include #include "init_state.h" #include "keyword_states/keywords_api.h" -InitState::InitState() : Converter(this) {} +InitState::InitState(Converter* cv) : ConversionState(cv) {} -bool InitState::convert(std::string& data, std::ofstream& out) +bool InitState::convert(std::stringstream& data_stream, bool /*last_line*/, std::ofstream& out) { std::string keyword; - std::stringstream data_stream(data); while ( data_stream >> keyword ) @@ -39,8 +39,10 @@ bool InitState::convert(std::string& data, std::ofstream& out) if( keyword.front() == '#') { - data.erase(data.begin()); - out << "-- " << data << std::endl; + keyword.erase(keyword.begin()); + std::ostringstream oss; + oss << data_stream.rdbuf(); + out << "--" << keyword << oss.str(); return true; } else @@ -49,7 +51,7 @@ bool InitState::convert(std::string& data, std::ofstream& out) { if (p->keyword.compare(0, p->keyword.size(), keyword) == 0) { - set_state(p->ctor()); + converter->set_state(p->ctor(converter)); return true; } } diff --git a/tools/snort2lua/init_state.h b/tools/snort2lua/init_state.h index 4e9bd9181..86f1a9db8 100644 --- a/tools/snort2lua/init_state.h +++ b/tools/snort2lua/init_state.h @@ -21,17 +21,18 @@ #include #include +#include "conversion_state.h" #include "converter.h" #ifndef INIT_STATE_H #define INIT_STATE_H -class InitState : public Converter +class InitState : public ConversionState { public: - InitState(); + InitState(Converter* cv); virtual ~InitState() {}; - virtual bool convert(std::string& data, std::ofstream&); + virtual bool convert(std::stringstream& data, bool last_line, std::ofstream&); }; diff --git a/tools/snort2lua/keyword_states/CMakeLists.txt b/tools/snort2lua/keyword_states/CMakeLists.txt index ab1161883..883e3d5d8 100644 --- a/tools/snort2lua/keyword_states/CMakeLists.txt +++ b/tools/snort2lua/keyword_states/CMakeLists.txt @@ -1,6 +1,6 @@ add_library( keyword_states - portvar.cc + var.cc keywords_api.h keywords_api.cc ) diff --git a/tools/snort2lua/keyword_states/keywords_api.cc b/tools/snort2lua/keyword_states/keywords_api.cc index aa3e3813c..147b611d1 100644 --- a/tools/snort2lua/keyword_states/keywords_api.cc +++ b/tools/snort2lua/keyword_states/keywords_api.cc @@ -23,10 +23,14 @@ extern const ConvertMap *portvar_map; +extern const ConvertMap *ipvar_map; +extern const ConvertMap *var_map; const std::vector keywords = //const ConvertMap* keywords[] = { portvar_map, + ipvar_map, + var_map, // nullptr, }; diff --git a/tools/snort2lua/keyword_states/keywords_api.h b/tools/snort2lua/keyword_states/keywords_api.h index 2d4ed1290..b89b947a1 100644 --- a/tools/snort2lua/keyword_states/keywords_api.h +++ b/tools/snort2lua/keyword_states/keywords_api.h @@ -20,7 +20,7 @@ // keywords_api.h author Josh Rosenbaum #include -#include "../converter.h" +#include "../conversion_state.h" extern const std::vector keywords; //extern const ConvertMap* keywords[]; diff --git a/tools/snort2lua/keyword_states/portvar.cc b/tools/snort2lua/keyword_states/portvar.cc deleted file mode 100644 index 1ba9348a8..000000000 --- a/tools/snort2lua/keyword_states/portvar.cc +++ /dev/null @@ -1,57 +0,0 @@ -/* -** Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved. - * Copyright (C) 2002-2013 Sourcefire, Inc. - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License Version 2 as - * published by the Free Software Foundation. You may not use, modify or - * distribute this program under any other version of the GNU General - * Public License. - * - * 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 - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -// portvar.cc author Josh Rosenbaum - -#include "../converter.h" - -namespace { - -class PortVar : public Converter -{ -public: - PortVar() : Converter(this) {}; - virtual ~PortVar() {}; - virtual bool convert(std::string& data, std::ofstream&); - -}; - -} // namespace - -bool PortVar::convert(std::string& data, std::ofstream& out) -{ - return true; -} - -/************************** - ******* A P I *********** - **************************/ - -static Converter* ctor() -{ - return new PortVar; -} - -static const ConvertMap keyword_portvar = -{ - "portvar", - ctor, -}; - -const ConvertMap* portvar_map = &keyword_portvar; diff --git a/tools/snort2lua/keyword_states/var.cc b/tools/snort2lua/keyword_states/var.cc new file mode 100644 index 000000000..6971dbed5 --- /dev/null +++ b/tools/snort2lua/keyword_states/var.cc @@ -0,0 +1,142 @@ +/* +** Copyright (C) 2014 Cisco and/or its affiliates. All rights reserved. + * Copyright (C) 2002-2013 Sourcefire, Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License Version 2 as + * published by the Free Software Foundation. You may not use, modify or + * distribute this program under any other version of the GNU General + * Public License. + * + * 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +// portvar.cc author Josh Rosenbaum + +#include +#include +#include + +#include "conversion_state.h" +#include "converter.h" +#include "snort2lua_util.h" + + +namespace { + +class PortVar : public ConversionState +{ +public: + PortVar(Converter* cv); + virtual ~PortVar() {}; + virtual bool convert(std::stringstream& data, bool last_line, std::ofstream&); + +private: + bool first_line; + +}; + +} // namespace + + +PortVar::PortVar(Converter* cv) : ConversionState(cv) +{ + first_line = true; +} + +bool PortVar::convert(std::stringstream& data_stream, bool last_line, std::ofstream& out) +{ + std::string keyword; + std::string ports;// converter->print_line(data_stream); + + if (first_line) + { + if (data_stream >> keyword) + { + out << keyword << " = " << std::endl; + out << "[[" << std::endl; + + first_line = false; + } + else + { + return false; + } + } + + if(!(data_stream >> ports)) + return false; + + if(ports.front() == '[' && ports.back() == ']') + { + std::vector port_list; + int count = 11; + + ports.erase(ports.begin()); + ports.pop_back(); + util::split(ports, ',', port_list); + + for(std::string elem : port_list) + { + if(count >= 11) + { + if (elem.compare(*port_list.begin())) + out << std::endl; + out << " "; // fourth space added below + count = 0; + } + + if(elem.front() == '$') + elem.erase(elem.begin()); + + out << ' ' << std::setw(5) << std::left << elem; + count++; + } + } + else + { + out << " " << std::setw(5) << std::left << ports; + } + + if(last_line) + out << std::endl << "]]" << std::endl; + + return true; +} + +/************************** + ******* A P I *********** + **************************/ + +static ConversionState* ctor(Converter* cv) +{ + return new PortVar(cv); +} + +static const ConvertMap keyword_portvar = +{ + "portvar", + ctor, +}; + +static const ConvertMap keyword_ipvar = +{ + "ipvar", + ctor, +}; + +static const ConvertMap keyword_var = +{ + "var", + ctor, +}; + +const ConvertMap* portvar_map = &keyword_portvar; +const ConvertMap* ipvar_map = &keyword_ipvar; +const ConvertMap* var_map = &keyword_var; diff --git a/tools/snort2lua/snort2lua.cc b/tools/snort2lua/snort2lua.cc index ba989ae39..78652df82 100644 --- a/tools/snort2lua/snort2lua.cc +++ b/tools/snort2lua/snort2lua.cc @@ -25,14 +25,41 @@ #include "converter.h" #include "init_state.h" + +#if 0 +#include +#include +#include +#include + +static inline std::string *ltrim(std::string *s) +{ + s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(std::isspace)))); +} + + +static inline std::string *rtrim(std::string *s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(std::isspace))).base(), s.end()); +}; + +static inline std::string &trim(std::string &s) +{ + return ltrim(rtrim(s)); +} +#endif + static bool convert(std::ifstream& in, std::ofstream& out) { - Converter* state = new InitState(); + Converter cv; + cv.reset_state(); +// state->inititalize(); +// Converter::reset_state(); while(!in.eof()) { std::string next_line; - bool line_continuation = false; + bool last_line = true; std::getline(in, next_line); @@ -44,15 +71,31 @@ static bool convert(std::ifstream& in, std::ofstream& out) { if (next_line.back() == '\\') { - line_continuation = true; + last_line = false; next_line.pop_back(); } - state->convert(next_line, out); + std::stringstream data_stream(next_line); + while(data_stream.tellg() != -1) + { + #if 0 + std::cout << data_stream.str() << std::endl; + std::cout << "size of data_stream: " << data_stream.str().size() << std::endl; + std::cout << "size of tellg: " << data_stream.tellg() << std::endl; + std::cout << "is empty? " << data_stream.str().empty() << std::endl; +// cv.print_line(data_stream); +#endif + if (!cv.convert_line(data_stream, last_line, out)) + { + std::cout << "ERROR: Failed to convert line: " << std::endl; + std::cout << "\t\t" << next_line << std::endl; + return false; + } + } } - if (!line_continuation) - state->reset_state(); + if (last_line) + cv.reset_state(); } return true;