-#include_directories(${CMAKE_CURRENT_SOURCE_DIRECTORY})
+include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
add_subdirectory(keyword_states)
converter.cc
init_state.h
init_state.cc
+ snort2lua_util.h
+ snort2lua_util.cc
)
target_link_libraries( snort2lua
#include <iostream>
#include "converter.h"
+#include "conversion_state.h"
#include "init_state.h"
+ConversionState* Converter::state = nullptr;
+
#if 0
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;
#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
#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 )
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
{
if (p->keyword.compare(0, p->keyword.size(), keyword) == 0)
{
- set_state(p->ctor());
+ converter->set_state(p->ctor(converter));
return true;
}
}
#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&);
};
add_library( keyword_states
- portvar.cc
+ var.cc
keywords_api.h
keywords_api.cc
)
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,
};
// 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[];
+++ /dev/null
-/*
-** 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;
--- /dev/null
+/*
+** 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;
#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);
{
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;