From cc9ad1f3fec54f593d54027cc131deca4a347a4e Mon Sep 17 00:00:00 2001 From: Urban Wallasch Date: Sun, 27 Oct 2019 02:23:20 +0200 Subject: [PATCH] * Replaced str[n]casecmp() with own str_[n]icmp(), fixed case agnostic string search. --- telehttpd.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/telehttpd.c b/telehttpd.c index ec0cacd..27298e0 100644 --- a/telehttpd.c +++ b/telehttpd.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -99,11 +99,32 @@ static int check_shm( bool retry ) { return 0; } -static char *strcasestr( const char *haystack, const char *needle ) { - size_t l = strlen( needle ); - for ( char *p = (char *)haystack; *p; ++p ) - if ( strncasecmp( p, needle, l ) == 0 ) - return p; +static int str_icmp( const char *s1, const char *s2 ) { + int r; + do { + r = tolower( (unsigned char)*s1 ) - tolower( (unsigned char)*s2 ); + } + while ( 0 == r && *s1++ && *s2++ ); + return r; +} + +static int str_nicmp( const char *s1, const char *s2, size_t n ) { + int r; + if ( 0 == n ) + return 0; + do { + r = tolower( (unsigned char)*s1 ) - tolower( (unsigned char)*s2 ); + } + while ( 0 == r && --n && *s1++ && *s2++ ); + return r; +} + +static char *str_istr( const char *haystack, const char *needle ) { + size_t n = strlen( needle ); + do { + if ( str_nicmp( haystack, needle, n ) == 0 ) + return (char *)haystack; + } while ( *haystack++ ); return NULL; } @@ -111,7 +132,7 @@ static size_t copy_field_val( char *buf, size_t bufsz, const char *needle, const const char *s; size_t l; - if ( NULL != ( s = strcasestr( haystack, needle ) ) ) { + if ( NULL != ( s = str_istr( haystack, needle ) ) ) { const char *e; s += strlen( needle ); if ( NULL != ( e = strstr( s, "\r\n" ) ) ) @@ -249,7 +270,7 @@ static int respond( int sock, const char *req ) { sockwrite( sock, hdr, n ); break; } - if ( 0 < ret && 0 != strcasecmp( conn, "keep-alive" ) ) + if ( 0 < ret && 0 != str_icmp( conn, "keep-alive" ) ) ret = 0; return ret; } -- 2.30.2