#include "treenodes.h"
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <errno.h>
-
-#include "detect.h"
+#include "framework/ips_option.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
-#include "framework/ips_option.h"
+#include "utils/util.h"
+
+#include "detect.h"
/****************************************************************************
*
static inline int clear_group_bit(BitOp* bitop, char* group)
{
- FLOWBITS_GRP* flowbits_grp;
- BitOp* GrpBitOp;
- unsigned int i, max_bytes;
-
- if ( group == NULL )
+ if ( !group )
return 0;
// FIXIT-M why is the hash lookup done at runtime for flowbits groups?
// a pointer to flowbis_grp should be in flowbits config data
// this *should* be safe but iff splay mode is disabled
- flowbits_grp = (FLOWBITS_GRP*)sfghash_find(flowbits_grp_hash, group);
- if ( flowbits_grp == NULL )
+ auto flowbits_grp = (FLOWBITS_GRP*)sfghash_find(flowbits_grp_hash, group);
+
+ if ( !flowbits_grp )
return 0;
- if ((bitop == NULL) || (bitop->get_max_bits() <= flowbits_grp->max_id) || flowbits_grp->count == 0)
+
+ if ( !bitop || (bitop->size() <= flowbits_grp->max_id) || !flowbits_grp->count )
return 0;
- GrpBitOp = flowbits_grp->GrpBitOp;
+
+ auto GrpBitOp = flowbits_grp->GrpBitOp;
/* note, max_id is an index, not a count.
* Calculate max_bytes by adding 8 to max_id, then dividing by 8. */
- max_bytes = (flowbits_grp->max_id + 8) >> 3;
- for ( i = 0; i < max_bytes; i++ )
- {
- (*bitop)[i] &= ~((*GrpBitOp)[i]);
- }
+ unsigned int max_bytes = (flowbits_grp->max_id + 8) >> 3;
+ for ( unsigned int i = 0; i < max_bytes; i++ )
+ bitop->get_buf_element(i) &= ~GrpBitOp->get_buf_element(i);
+
return 1;
}
static inline int toggle_group_bit(BitOp* bitop, char* group)
{
- FLOWBITS_GRP* flowbits_grp;
- BitOp* GrpBitOp;
- unsigned int i, max_bytes;
-
- if ( group == NULL )
+ if ( !group )
return 0;
- flowbits_grp = (FLOWBITS_GRP*)sfghash_find(flowbits_grp_hash, group);
- if ( flowbits_grp == NULL )
+
+ auto flowbits_grp = (FLOWBITS_GRP*)sfghash_find(flowbits_grp_hash, group);
+
+ if ( !flowbits_grp )
return 0;
- if ((bitop == NULL) || (bitop->get_max_bits() <= flowbits_grp->max_id) || flowbits_grp->count == 0)
+
+ if ( !bitop || (bitop->size() <= flowbits_grp->max_id) || !flowbits_grp->count )
return 0;
- GrpBitOp = flowbits_grp->GrpBitOp;
+
+ auto GrpBitOp = flowbits_grp->GrpBitOp;
/* note, max_id is an index, not a count.
* Calculate max_bytes by adding 8 to max_id, then dividing by 8. */
- max_bytes = (flowbits_grp->max_id + 8) >> 3;
- for ( i = 0; i < max_bytes; i++ )
- {
- (*bitop)[i] ^= (*GrpBitOp)[i];
- }
+ unsigned int max_bytes = (flowbits_grp->max_id + 8) >> 3;
+ for ( unsigned int i = 0; i < max_bytes; i++ )
+ bitop->get_buf_element(i) ^= GrpBitOp->get_buf_element(i);
+
return 1;
}
return 0;
for ( i = 0; i <= (unsigned int)(flowbits_grp->max_id >>3); i++ )
{
- uint8_t val = (*bitop)[i] &
- (*(flowbits_grp->GrpBitOp))[i];
- if (val != (*(flowbits_grp->GrpBitOp))[i])
+ uint8_t val = bitop->get_buf_element(i) & flowbits_grp->GrpBitOp->get_buf_element(i);
+
+ if ( val != flowbits_grp->GrpBitOp->get_buf_element(i) )
return 0;
}
return 1;
return 0;
for ( i = 0; i <= (unsigned int)(flowbits_grp->max_id >>3); i++ )
{
- uint8_t val = (*bitop)[i] &
- (*(flowbits_grp->GrpBitOp))[i];
- if (val)
+ uint8_t val = bitop->get_buf_element(i) & flowbits_grp->GrpBitOp->get_buf_element(i);
+ if ( val )
return 1;
}
return 0;
#include "file_mime_config.h"
+#include "log/messages.h"
#include "main/snort_types.h"
-
#include "file_api/file_service.h"
#include "file_mime_process.h"
#include "config.h"
#endif
-#include "ppm_module.h"
#include "detection/fp_create.h"
-#include "parser/parser.h"
+#include "detection/treenodes.h"
#include "events/event_queue.h"
-#include "utils/stats.h"
+#include "log/messages.h"
#include "sfip/sf_ip.h"
-#include "time/cpuclock.h"
+#include "utils/stats.h"
+#include "utils/util.h"
+
+#include "ppm_module.h"
#define PPM_BASE_SUSPEND_RULE_GID 1000
#define PPM_BASE_CLEAR_RULE_GID 2000
#include "dce_utils.h"
+#include <cerrno>
+
#include "framework/ips_option.h"
#include "framework/module.h"
#include "framework/parameter.h"
#include <sys/types.h>
#include "events/event_queue.h"
+#include "log/messages.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
#include "profiler/profiler.h"
// ftp_module.cc author Russ Combs <rucombs@cisco.com>
#include "ftp_module.h"
+
#include <sstream>
+#include "log/messages.h"
#include "parser/parser.h"
using namespace std;
#include "ftp_print.h"
#include "telnet_module.h"
+#include "log/messages.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
#include "profiler/profiler.h"
#include <ctype.h>
+#include "log/messages.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
#include "events/event_queue.h"
#include <sys/types.h>
#include "events/event_queue.h"
+#include "log/messages.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
#include "profiler/profiler.h"
#include "target_based/snort_protocols.h"
#include "search_engines/search_tool.h"
#include "utils/sfsnprintfappend.h"
+#include "utils/util.h"
#include "protocols/ssl.h"
#include "mime/file_mime_process.h"
#include <sys/types.h>
#include "events/event_queue.h"
+#include "log/messages.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
#include "profiler/profiler.h"
#include "target_based/snort_protocols.h"
#include "search_engines/search_tool.h"
#include "utils/sfsnprintfappend.h"
+#include "utils/util.h"
#include "protocols/ssl.h"
#include "file_api/file_api.h"
#include "mime/file_mime_process.h"
#endif
#include "sip.h"
-
#include "framework/ips_option.h"
#include "framework/module.h"
#include "framework/parameter.h"
#include "detection/detect.h"
#include "detection/detection_defines.h"
#include "hash/sfhashfcn.h"
+#include "log/messages.h"
#include "profiler/profiler.h"
//-------------------------------------------------------------------------
#include "sip.h"
+#include "detection/detect.h"
+#include "detection/detection_defines.h"
#include "framework/ips_option.h"
#include "framework/module.h"
#include "framework/parameter.h"
-#include "detection/detect.h"
-#include "detection/detection_defines.h"
#include "hash/sfhashfcn.h"
+#include "log/messages.h"
#include "profiler/profiler.h"
//-------------------------------------------------------------------------
#include <sys/types.h>
#include "events/event_queue.h"
+#include "log/messages.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
#include "profiler/profiler.h"
#include <sys/types.h>
#include "events/event_queue.h"
+#include "log/messages.h"
#include "main/snort_types.h"
#include "main/snort_debug.h"
#include "profiler/profiler.h"
// tcp_segment_descriptor.cc author davis mcpherson <davmcphe@cisco.com>
// Created on: Jul 30, 2015
+#include "log/messages.h"
#include "main/snort_debug.h"
-
-#include "stream/tcp/tcp_defs.h"
#include "protocols/tcp_options.h"
+#include "stream/tcp/tcp_defs.h"
#include "stream/tcp/tcp_event_logger.h"
#include "tcp_segment_descriptor.h"
// A simple, dynamically sized bit vector implementation
-#include <assert.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <string.h>
-
-#include "utils/util.h"
+#include <cassert>
+#include <cstring>
class BitOp
{
public:
- BitOp(unsigned int len)
- {
- assert(len);
-
- bit_buf = (uint8_t*)SnortAlloc(len);
-
- buf_size = (unsigned int)len;
- max_bits = (unsigned int)(len << 3);
- }
-
- ~BitOp()
- {
- free(bit_buf);
- }
+ BitOp(size_t);
+ ~BitOp();
void reset();
void set(unsigned int bit);
- bool is_set(unsigned int bit);
+ bool is_set(unsigned int bit) const;
void clear(unsigned int bit);
- unsigned int get_max_bits()
- { return max_bits; }
+ size_t size() const;
- //FIXIT-L This should be eliminated and better encapsulated.
- uint8_t& operator[](unsigned int pos)
- { if ( pos > buf_size) pos = 0; return bit_buf[pos]; }
+ // FIXIT-L J add operator overloads for [], &=, |=, etc
+ size_t get_buf_size() const;
+ uint8_t& get_buf_element(size_t);
+ const uint8_t& get_buf_element(size_t) const;
private:
+ uint8_t mask(size_t bit) const;
+
uint8_t* bit_buf;
- unsigned int buf_size;
- unsigned int max_bits;
+ const size_t buf_size;
};
+// -----------------------------------------------------------------------------
+// implementation
+// -----------------------------------------------------------------------------
+
+inline BitOp::BitOp(size_t len) :
+ bit_buf(new uint8_t[len]()), buf_size(len)
+{ }
+
+inline BitOp::~BitOp()
+{ delete[] bit_buf; }
+
+// FIXIT-L J ops that don't need to be inlined can probably be but into a .cc file
// Reset the bit buffer so that it can be reused
inline void BitOp::reset()
-{
- memset(bit_buf, 0, buf_size);
-}
+{ memset(bit_buf, 0, buf_size); }
// Set the bit in the specified position within the bit buffer.
inline void BitOp::set(unsigned int bit)
{
- if ( max_bits <= bit )
- {
- assert(false);
- return;
- }
- uint8_t mask = (uint8_t)(0x80 >> (bit & 7));
- bit_buf[bit >> 3] |= mask;
+ assert(size() > bit);
+ bit_buf[bit >> 3] |= mask(bit);
}
// Checks if the bit at the specified position is set
-inline bool BitOp::is_set(unsigned int bit)
+inline bool BitOp::is_set(unsigned int bit) const
{
- if ( max_bits <= bit )
- {
- assert(false);
- return false;
- }
- uint8_t mask = (uint8_t)(0x80 >> (bit & 7));
- return (mask & bit_buf[bit >> 3]);
+ assert(size() > bit);
+ return mask(bit) & bit_buf[bit >> 3];
}
// Clear the bit in the specified position within the bit buffer.
inline void BitOp::clear(unsigned int bit)
{
- if ( max_bits <= bit )
- {
- assert(false);
- return;
- }
- uint8_t mask = (uint8_t)(0x80 >> (bit & 7));
- bit_buf[bit >> 3] &= ~mask;
+ assert(size() > bit);
+ bit_buf[bit >> 3] &= ~mask(bit);
}
+inline size_t BitOp::size() const
+{ return buf_size << 3; }
+
+inline uint8_t BitOp::mask(size_t bit) const
+{ return (uint8_t)(0x80 >> (bit & 7)); }
+
+inline size_t BitOp::get_buf_size() const
+{ return buf_size; }
+
+inline uint8_t& BitOp::get_buf_element(size_t i)
+{ return bit_buf[i]; }
+
+inline const uint8_t& BitOp::get_buf_element(size_t i) const
+{ return bit_buf[i]; }
+
#endif
--- /dev/null
+#include "bitop.h"
+#include "catch/catch.hpp"
+
+static bool t_bitop_buffer_zero(BitOp& bitop)
+{
+ for ( size_t i = 0; i < bitop.get_buf_size(); ++i )
+ if ( bitop.get_buf_element(i) )
+ return false;
+
+ return true;
+}
+
+TEST_CASE( "bitop", "[bitop]" )
+{
+ BitOp bitop(3);
+
+ SECTION( "zero-initialized" )
+ {
+ CHECK( t_bitop_buffer_zero(bitop) );
+ }
+
+ SECTION( "reset" )
+ {
+ bitop.get_buf_element(0) = 0xff;
+ bitop.reset();
+
+ CHECK( t_bitop_buffer_zero(bitop) );
+ }
+
+ SECTION( "set/is_set/clear" )
+ {
+ bitop.set(6);
+
+ CHECK( bitop.get_buf_element(0) == 0x02 );
+
+ CHECK( bitop.is_set(6) );
+ CHECK_FALSE( bitop.is_set(7) );
+
+ bitop.set(7);
+ bitop.clear(6);
+
+ CHECK( bitop.get_buf_element(0) == 0x01 );
+ }
+
+ SECTION( "size" )
+ {
+ CHECK( bitop.size() == 24 );
+ }
+}
#endif
}
+#ifdef UNIT_TEST
+#include "bitop_test.cc"
+#endif