#include "log/messages.h"
#include "main/snort_config.h"
#include "main/thread.h"
+#include "utils/util.h"
#include "hyper_scratch_allocator.h"
HS_MODE_BLOCK, nullptr, (hs_database_t**)&db, &err) != HS_SUCCESS )
#endif
{
- ParseError("can't compile content '%s'", pattern);
+ std::string print_str;
+ uint8_to_printable_str(pattern, len, print_str);
+ ParseError("can't compile content '%s'", print_str.c_str());
hs_free_compile_error(err);
return;
}
if ( !scratcher->allocate(db) )
- ParseError("can't allocate scratch for content '%s'", pattern);
+ {
+ std::string print_str;
+ uint8_to_printable_str(pattern, len, print_str);
+ ParseError("can't allocate scratch for content '%s'", print_str.c_str());
+ }
}
HyperSearch::~HyperSearch()
(unsigned)tvp->tv_usec);
}
}
+
+static void start_hex_state(bool& hex_state, std::string& print_str)
+{
+ if (!hex_state)
+ {
+ hex_state = true;
+ print_str += "|";
+ }
+}
+
+static void end_hex_state(bool& hex_state, std::string& print_str)
+{
+ if (hex_state)
+ {
+ hex_state = false;
+ print_str += "|";
+ }
+}
+
+void uint8_to_printable_str(const uint8_t* buff, unsigned len, std::string& print_str)
+{
+ print_str.clear();
+ char output[4];
+ bool hex_state = false;
+ for (unsigned i = 0 ; i < len ; i++)
+ {
+ if ((buff[i] >= 0x20) && (buff[i] <= 0x7E))
+ {
+ end_hex_state(hex_state, print_str);
+ sprintf(output, "%c", (char)buff[i]);
+ }
+ else
+ {
+ start_hex_state(hex_state, print_str);
+ sprintf(output, "%.2x ", buff[i]);
+ }
+
+ print_str += output;
+ }
+
+ end_hex_state(hex_state, print_str);
+}
+
}
#ifdef UNIT_TEST
{
REQUIRE((gmt2local(0xffffffff1fff2f)==0));
}
+
+TEST_CASE("uint8_to_printable_str go over all options", "[util]")
+{
+ std::string print_str;
+ uint8_t pattern[] = { 0, 'a', '(', 'd', ')', 1, '\r', 2, '\n','n'};
+ uint8_to_printable_str(pattern, 10, print_str);
+ CHECK((strcmp(print_str.c_str(),"|00 |a(d)|01 0d 02 0a |n") == 0));
+}
+
+TEST_CASE("uint8_to_printable_str empty buffer", "[util]")
+{
+ std::string print_str;
+ uint8_t* pattern = nullptr;
+ uint8_to_printable_str(pattern, 0, print_str);
+ CHECK((strcmp(print_str.c_str(),"") == 0));
+}
+
+TEST_CASE("uint8_to_printable_str end with |", "[util]")
+{
+ std::string print_str;
+ uint8_t pattern[] = { 'a', 0 };
+ uint8_to_printable_str(pattern, 2, print_str);
+ CHECK((strcmp(print_str.c_str(),"a|00 |") == 0));
+}
+
#endif
SO_PUBLIC char* snort_strndup(const char*, size_t);
SO_PUBLIC const uint8_t* snort_memrchr(const uint8_t*, char, size_t);
SO_PUBLIC void ts_print(const struct timeval*, char*, bool yyyymmdd = false);
+void uint8_to_printable_str(const uint8_t* buff, unsigned len, std::string& print_str);
}
#endif