]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
changing portvar parser to a 'var' parser
authorJosh <jrosenba@cisco.com>
Mon, 9 Jun 2014 23:00:50 +0000 (19:00 -0400)
committerJosh <jrosenba@cisco.com>
Mon, 9 Jun 2014 23:00:50 +0000 (19:00 -0400)
tools/snort2lua/CMakeLists.txt
tools/snort2lua/converter.cc
tools/snort2lua/converter.h
tools/snort2lua/init_state.cc
tools/snort2lua/init_state.h
tools/snort2lua/keyword_states/CMakeLists.txt
tools/snort2lua/keyword_states/keywords_api.cc
tools/snort2lua/keyword_states/keywords_api.h
tools/snort2lua/keyword_states/portvar.cc [deleted file]
tools/snort2lua/keyword_states/var.cc [new file with mode: 0644]
tools/snort2lua/snort2lua.cc

index 9e89e35fa1b82c7d03aa9829d6ef6eb80ceab453..a3b9a72a8f74b2e29908d890d1dc419b9db731a1 100644 (file)
@@ -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
index be7e7a298a570b16ccf6da685525276b63a77969..448751d14c5375f1a91a8de7de5b70d649ac3100 100644 (file)
 
 #include <iostream>
 #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; 
index b01405c287756586be1ba0a29a35effb34ae1bc1..a97ceb506d49f1b56a7c1326ab6ffb7dbc677029 100644 (file)
 #define CONVERTER_H
 
 #include <string>
+#include <fstream>
+#include <sstream>
 
-
+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
index 492c889236794366fc22d380e58e987b827e3ae7..a94ccb3fc6ae0361efc563968b8abfa1afad7fba 100644 (file)
 
 #include <vector>
 #include <sstream>
+#include <iostream>
 #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;
                 }
             }
index 4e9bd9181134fdd5b9e157e09ca04206c21edd22..86f1a9db81c6216bbf753e2a96f49c77cb8ad206 100644 (file)
 
 #include <sstream>
 #include <fstream>
+#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&);
 
 };
 
index ab1161883ca7cd51218a6295730ab81d05a43652..883e3d5d8afa18ffb7c49c6552b36f9d05fdd2cf 100644 (file)
@@ -1,6 +1,6 @@
 
 add_library( keyword_states
-    portvar.cc
+    var.cc
     keywords_api.h
     keywords_api.cc
 )
index aa3e3813cc25b72ec40f5adcb8428ef6f942eae8..147b611d15b6057e991d95843884379d094202cc 100644 (file)
 
 
 extern const ConvertMap *portvar_map;
+extern const ConvertMap *ipvar_map;
+extern const ConvertMap *var_map;
 
 const std::vector<const ConvertMap*> keywords = 
 //const ConvertMap* keywords[] = 
 {
     portvar_map,
+    ipvar_map,
+    var_map,
 //    nullptr,
 };
index 2d4ed129088a5336a95abe66d35ddc6ad0f7041b..b89b947a12e20fb527c712f672b83105b1e1b42c 100644 (file)
@@ -20,7 +20,7 @@
 // keywords_api.h author Josh Rosenbaum <jorosenba@cisco.com>
 
 #include <vector>
-#include "../converter.h"
+#include "../conversion_state.h"
 
 extern const std::vector<const ConvertMap*> 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 (file)
index 1ba9348..0000000
+++ /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 <jorosenba@cisco.com>
-
-#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 (file)
index 0000000..6971dbe
--- /dev/null
@@ -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 <jorosenba@cisco.com>
+
+#include <sstream>
+#include <vector>
+#include <iomanip>
+
+#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<std::string> 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;
index ba989ae39b32aa8f784a50c9adc24374580c3221..78652df821450f403a6db5000c5913df9ba5185a 100644 (file)
 #include "converter.h"
 #include "init_state.h"
 
+
+#if 0
+#include <algorithm>
+#include <functional>
+#include <cctype>
+#include <locale>
+
+static inline std::string *ltrim(std::string *s)
+{
+    s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
+}
+
+
+static inline std::string *rtrim(std::string *s)
+{
+    s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(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;