diff --git a/include/private/strhelper.h b/include/private/strhelper.h index 55e817c2..6d44eae8 100644 --- a/include/private/strhelper.h +++ b/include/private/strhelper.h @@ -40,4 +40,7 @@ copy_cstring_length( const char *str, size_t length ); void to_upper_case( char *str ); +int +strncasecmp_custom( const char *s1, const char *s2, size_t n ); + #endif /* __STUMPLESS_PRIVATE_STRHELPER_H */ diff --git a/src/facility.c b/src/facility.c index a7819e39..dff55c6d 100644 --- a/src/facility.c +++ b/src/facility.c @@ -21,7 +21,6 @@ #include #include "private/facility.h" #include "private/strhelper.h" -#include "private/memory.h" static char *facility_enum_to_string[] = { STUMPLESS_FOREACH_FACILITY( GENERATE_STRING ) @@ -44,39 +43,27 @@ enum stumpless_facility stumpless_get_facility_enum_from_buffer(const char *facility_buffer, size_t facility_buffer_length) { size_t facility_bound; size_t i; - char *facility_name; const int str_offset = 19; // to ommit "STUMPLESS_FACILITY_" - size_t buf_length; facility_bound = sizeof( facility_enum_to_string ) / sizeof( facility_enum_to_string[0] ); - facility_name = copy_cstring_with_length(facility_buffer, &buf_length); - if( !facility_name ) { - return -1; - } - - to_upper_case(facility_name); for( i = 0; i < facility_bound; i++ ) { - if( strcmp( facility_name, facility_enum_to_string[i] + str_offset ) == 0 ) { - free_mem( facility_name ); + if( strncasecmp_custom( facility_buffer, facility_enum_to_string[i] + str_offset, facility_buffer_length ) == 0 ) { return i << 3; } } // exeption, for 'security' return 'auth' enum value - if( strcmp( facility_name, "SECURITY" ) == 0 ) { - free_mem( facility_name ); + if( strncasecmp_custom( facility_buffer, "SECURITY", facility_buffer_length ) == 0 ) { return STUMPLESS_FACILITY_AUTH_VALUE; } // exeption, for 'authpriv' not presented in enum list - if( strcmp( facility_name, "AUTHPRIV" ) == 0 ) { - free_mem( facility_name ); + if( strncasecmp_custom( facility_buffer, "AUTHPRIV", facility_buffer_length ) == 0 ) { return STUMPLESS_FACILITY_AUTH2_VALUE; } - free_mem( facility_name ); return -1; } diff --git a/src/prival.c b/src/prival.c index f5b39ba3..05d0abd2 100644 --- a/src/prival.c +++ b/src/prival.c @@ -26,13 +26,12 @@ #include #include "private/config.h" #include "private/config/wrapper/locale.h" -#include "private/error.h" #include "private/facility.h" #include "private/memory.h" #include "private/prival.h" #include "private/severity.h" -#include "private/strhelper.h" #include "private/validate.h" +#include "private/error.h" const char * stumpless_get_prival_string( int prival ) { @@ -64,7 +63,6 @@ stumpless_prival_from_string( const char *string ) { int prival; int severity; int facility; - const char *param; const char *period; const char *sec_period; size_t len; @@ -103,15 +101,7 @@ stumpless_prival_from_string( const char *string ) { // Calculate the facility length, up to the first period character len = period - string; - // Copy the facility substring to the param buffer - param = copy_cstring_length( string, len ); - if( !param ) { - return -1; - } - - facility = stumpless_get_facility_enum( param ); - - free_mem( param ); + facility = stumpless_get_facility_enum_from_buffer( string, len ); if( facility < 0 ) { raise_invalid_param( ); @@ -122,15 +112,7 @@ stumpless_prival_from_string( const char *string ) { len++; len = slen - len; - // Copy the severity substring to the param buffer - param = copy_cstring_length( period + 1, len ); - if( !param ) { - return -1; - } - - severity = stumpless_get_severity_enum( param ); - - free_mem( param ); + severity = stumpless_get_severity_enum_from_buffer( period + 1, len ); if( severity < 0 ) { raise_invalid_param( ); diff --git a/src/severity.c b/src/severity.c index fa2ec160..2a20bcdb 100644 --- a/src/severity.c +++ b/src/severity.c @@ -21,7 +21,6 @@ #include #include "private/severity.h" #include "private/strhelper.h" -#include "private/memory.h" static char *severity_enum_to_string[] = { STUMPLESS_FOREACH_SEVERITY( GENERATE_STRING ) @@ -42,42 +41,29 @@ enum stumpless_severity stumpless_get_severity_enum(const char *severity_string) enum stumpless_severity stumpless_get_severity_enum_from_buffer(const char *severity_buffer, size_t severity_buffer_length) { size_t severity_bound; size_t i; - char *severity_name; const int str_offset = 19; // to ommit "STUMPLESS_SEVERITY_" - size_t buf_length; severity_bound = sizeof( severity_enum_to_string ) / sizeof( severity_enum_to_string[0] ); - severity_name = copy_cstring_with_length( severity_buffer, &buf_length ); - if( !severity_name ) { - return -1; - } - - to_upper_case( severity_name ); for( i = 0; i < severity_bound; i++ ) { - if( strcmp( severity_name, severity_enum_to_string[i] + str_offset ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp_custom( severity_buffer, severity_enum_to_string[i] + str_offset, severity_buffer_length ) == 0 ) { return i; } } - if( strcmp( severity_name, "PANIC" ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp_custom( severity_buffer, "PANIC", severity_buffer_length ) == 0 ) { return STUMPLESS_SEVERITY_EMERG_VALUE; } - if( strcmp( severity_name, "ERROR" ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp_custom( severity_buffer, "ERROR", severity_buffer_length ) == 0 ) { return STUMPLESS_SEVERITY_ERR_VALUE; } - if( strcmp( severity_name, "WARN" ) == 0 ) { - free_mem( severity_name ); + if( strncasecmp_custom( severity_buffer, "WARN", severity_buffer_length ) == 0 ) { return STUMPLESS_SEVERITY_WARNING_VALUE; } - free_mem( severity_name ); return -1; } diff --git a/src/strhelper.c b/src/strhelper.c index 82d9a437..6db7d017 100644 --- a/src/strhelper.c +++ b/src/strhelper.c @@ -79,3 +79,16 @@ to_upper_case( char *str ) { str[i] = toupper( str[i] ); } } + +int +strncasecmp_custom( const char *s1, const char *s2, size_t n ) { + if (n != 0) { + do { + if (tolower(*s1) != tolower(*s2++)) + return tolower(*s1) - tolower(*--s2); + if (*s1++ == '\0') + break; + } while (--n != 0); + } + return 0; +} diff --git a/test/function/facility.cpp b/test/function/facility.cpp index ca9092be..b3292f1c 100644 --- a/test/function/facility.cpp +++ b/test/function/facility.cpp @@ -16,11 +16,9 @@ * limitations under the License. */ -#include #include #include #include "test/helper/assert.hpp" -#include "test/helper/memory_allocation.hpp" namespace { @@ -111,19 +109,6 @@ namespace { EXPECT_NO_ERROR; } - TEST( GetFacilityEnum, InvalidMemFacility ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_get_facility_enum( "user" ); - EXPECT_EQ( result, -1 ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - TEST( GetFacilityEnum, NoSuchFacility ) { int result; @@ -131,18 +116,4 @@ namespace { EXPECT_EQ( result, -1 ); } - TEST( GetFacilityEnumFromBuffer, InvalidMemFacility ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_get_facility_enum_from_buffer( "user", sizeof( "user" ) ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - } diff --git a/test/function/prival.cpp b/test/function/prival.cpp index f7943058..7c2623bf 100644 --- a/test/function/prival.cpp +++ b/test/function/prival.cpp @@ -21,7 +21,6 @@ #include #include #include "test/helper/assert.hpp" -#include "test/helper/memory_allocation.hpp" namespace { @@ -112,34 +111,6 @@ namespace { EXPECT_ERROR_ID_EQ( STUMPLESS_INVALID_PARAM_STRING ); } - TEST( GetPrivalFromString, InvalidMemFacilityPriority ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_prival_from_string( "user.err" ); - EXPECT_EQ( result, -1 ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - - TEST( GetPrivalFromString, InvalidMemSeverityPriority ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL_ON_SIZE( 4 ) ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_prival_from_string( "syslog.err" ); - EXPECT_EQ( result, -1 ); - - EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - TEST(GetPrivalString, ValidPrival) { int prival; const char *result; diff --git a/test/function/severity.cpp b/test/function/severity.cpp index 12d4afd8..70f431a9 100644 --- a/test/function/severity.cpp +++ b/test/function/severity.cpp @@ -16,11 +16,9 @@ * limitations under the License. */ -#include #include #include #include "test/helper/assert.hpp" -#include "test/helper/memory_allocation.hpp" namespace { @@ -100,19 +98,6 @@ namespace { EXPECT_NO_ERROR; } - TEST( GetSeverityEnum, InvalidMemSeverity ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_get_severity_enum( "info" ); - EXPECT_EQ( result, -1 ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - TEST( GetSeverityEnum, NoSuchSeverity ) { int severity_count = 0; int result; @@ -128,18 +113,4 @@ namespace { EXPECT_EQ( result, -1 ); } - TEST( GetSeverityEnumFromBuffer, InvalidMemSeverity ) { - int result; - void * (*set_malloc_result)(size_t); - set_malloc_result = stumpless_set_malloc( MALLOC_FAIL ); - ASSERT_NOT_NULL( set_malloc_result ); - - result = stumpless_get_severity_enum_from_buffer( "info", sizeof("info") ); - EXPECT_EQ( result, -1 ); - EXPECT_ERROR_ID_EQ( STUMPLESS_MEMORY_ALLOCATION_FAILURE ); - - set_malloc_result = stumpless_set_malloc( malloc ); - EXPECT_TRUE( set_malloc_result == malloc ); - } - }