diff options
author | Daniel Stenberg <daniel@haxx.se> | 2022-10-31 12:49:46 +0100 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2022-11-01 17:01:26 +0100 |
commit | 3f039dfd6f0a1d806b94cc2d5548926182485b7e (patch) | |
tree | 35ba93da831cc90582661f6ed63b3710a1570410 /lib | |
parent | 7399fa5b05ce897277b8713c659c939d068570a4 (diff) | |
download | curl-3f039dfd6f0a1d806b94cc2d5548926182485b7e.tar.gz |
strcase: use curl_str(n)equal for case insensitive matches
No point in having two entry points for the same functions.
Also merged the *safe* function treatment into these so that they can
also be used when one or both pointers are NULL.
Closes #9837
Diffstat (limited to 'lib')
-rw-r--r-- | lib/hsts.c | 8 | ||||
-rw-r--r-- | lib/http2.c | 4 | ||||
-rw-r--r-- | lib/strcase.c | 49 | ||||
-rw-r--r-- | lib/strcase.h | 8 | ||||
-rw-r--r-- | lib/url.c | 2 | ||||
-rw-r--r-- | lib/vtls/openssl.c | 2 | ||||
-rw-r--r-- | lib/vtls/vtls.c | 10 |
7 files changed, 36 insertions, 47 deletions
diff --git a/lib/hsts.c b/lib/hsts.c index 330fe37ac..c449120f3 100644 --- a/lib/hsts.c +++ b/lib/hsts.c @@ -157,7 +157,7 @@ CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname, do { while(*p && ISBLANK(*p)) p++; - if(Curl_strncasecompare("max-age=", p, 8)) { + if(strncasecompare("max-age=", p, 8)) { bool quoted = FALSE; CURLofft offt; char *endp; @@ -186,7 +186,7 @@ CURLcode Curl_hsts_parse(struct hsts *h, const char *hostname, } gotma = TRUE; } - else if(Curl_strncasecompare("includesubdomains", p, 17)) { + else if(strncasecompare("includesubdomains", p, 17)) { if(gotinc) return CURLE_BAD_FUNCTION_ARGUMENT; subdomains = TRUE; @@ -277,11 +277,11 @@ struct stsentry *Curl_hsts(struct hsts *h, const char *hostname, if(ntail < hlen) { size_t offs = hlen - ntail; if((hostname[offs-1] == '.') && - Curl_strncasecompare(&hostname[offs], sts->host, ntail)) + strncasecompare(&hostname[offs], sts->host, ntail)) return sts; } } - if(Curl_strcasecompare(hostname, sts->host)) + if(strcasecompare(hostname, sts->host)) return sts; } } diff --git a/lib/http2.c b/lib/http2.c index d8dd014db..66ebafc25 100644 --- a/lib/http2.c +++ b/lib/http2.c @@ -1024,9 +1024,9 @@ static int on_header(nghttp2_session *session, const nghttp2_frame *frame, if(!check) /* no memory */ return NGHTTP2_ERR_CALLBACK_FAILURE; - if(!Curl_strcasecompare(check, (const char *)value) && + if(!strcasecompare(check, (const char *)value) && ((conn->remote_port != conn->given->defport) || - !Curl_strcasecompare(conn->host.name, (const char *)value))) { + !strcasecompare(conn->host.name, (const char *)value))) { /* This is push is not for the same authority that was asked for in * the URL. RFC 7540 section 8.2 says: "A client MUST treat a * PUSH_PROMISE for which the server is not authoritative as a stream diff --git a/lib/strcase.c b/lib/strcase.c index 09d2a8a96..7fb9c8087 100644 --- a/lib/strcase.c +++ b/lib/strcase.c @@ -83,16 +83,13 @@ char Curl_raw_tolower(char in) } /* - * Curl_strcasecompare() is for doing "raw" case insensitive strings. This is - * meant to be locale independent and only compare strings we know are safe - * for this. See - * https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for some - * further explanation to why this function is necessary. - * - * @unittest: 1301 + * curl_strequal() is for doing "raw" case insensitive strings. This is meant + * to be locale independent and only compare strings we know are safe for + * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for + * further explanations as to why this function is necessary. */ -int Curl_strcasecompare(const char *first, const char *second) +static int casecompare(const char *first, const char *second) { while(*first && *second) { if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) @@ -108,25 +105,22 @@ int Curl_strcasecompare(const char *first, const char *second) return !*first == !*second; } -int Curl_safe_strcasecompare(const char *first, const char *second) +/* --- public function --- */ +int curl_strequal(const char *first, const char *second) { if(first && second) /* both pointers point to something then compare them */ - return Curl_strcasecompare(first, second); + return casecompare(first, second); /* if both pointers are NULL then treat them as equal */ return (NULL == first && NULL == second); } -/* - * @unittest: 1301 - */ -int Curl_strncasecompare(const char *first, const char *second, size_t max) +static int ncasecompare(const char *first, const char *second, size_t max) { while(*first && *second && max) { - if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) { - break; - } + if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) + return 0; max--; first++; second++; @@ -137,6 +131,16 @@ int Curl_strncasecompare(const char *first, const char *second, size_t max) return Curl_raw_toupper(*first) == Curl_raw_toupper(*second); } +/* --- public function --- */ +int curl_strnequal(const char *first, const char *second, size_t max) +{ + if(first && second) + /* both pointers point to something then compare them */ + return ncasecompare(first, second, max); + + /* if both pointers are NULL then treat them as equal if max is non-zero */ + return (NULL == first && NULL == second && max); +} /* Copy an upper case version of the string from src to dest. The * strings may overlap. No more than n characters of the string are copied * (including any NUL) and the destination string will NOT be @@ -198,14 +202,3 @@ int Curl_timestrcmp(const char *a, const char *b) return a || b; return match; } - -/* --- public functions --- */ - -int curl_strequal(const char *first, const char *second) -{ - return Curl_strcasecompare(first, second); -} -int curl_strnequal(const char *first, const char *second, size_t max) -{ - return Curl_strncasecompare(first, second, max); -} diff --git a/lib/strcase.h b/lib/strcase.h index 65a575385..192e0da09 100644 --- a/lib/strcase.h +++ b/lib/strcase.h @@ -35,12 +35,8 @@ * Result is 1 if text matches and 0 if not. */ -#define strcasecompare(a,b) Curl_strcasecompare(a,b) -#define strncasecompare(a,b,c) Curl_strncasecompare(a,b,c) - -int Curl_strcasecompare(const char *first, const char *second); -int Curl_safe_strcasecompare(const char *first, const char *second); -int Curl_strncasecompare(const char *first, const char *second, size_t max); +#define strcasecompare(a,b) curl_strequal(a,b) +#define strncasecompare(a,b,c) curl_strnequal(a,b,c) char Curl_raw_toupper(char in); char Curl_raw_tolower(char in); @@ -933,7 +933,7 @@ proxy_info_matches(const struct proxy_info *data, { if((data->proxytype == needle->proxytype) && (data->port == needle->port) && - Curl_safe_strcasecompare(data->host.name, needle->host.name)) + strcasecompare(data->host.name, needle->host.name)) return TRUE; return FALSE; diff --git a/lib/vtls/openssl.c b/lib/vtls/openssl.c index ad2efa558..dd9d24a7c 100644 --- a/lib/vtls/openssl.c +++ b/lib/vtls/openssl.c @@ -4364,7 +4364,7 @@ static size_t ossl_version(char *buffer, size_t size) int count; const char *ver = OpenSSL_version(OPENSSL_VERSION); const char expected[] = OSSL_PACKAGE " "; /* ie "LibreSSL " */ - if(Curl_strncasecompare(ver, expected, sizeof(expected) - 1)) { + if(strncasecompare(ver, expected, sizeof(expected) - 1)) { ver += sizeof(expected) - 1; } count = msnprintf(buffer, size, "%s/%s", OSSL_PACKAGE, ver); diff --git a/lib/vtls/vtls.c b/lib/vtls/vtls.c index 9dee5aa3b..cbe03f589 100644 --- a/lib/vtls/vtls.c +++ b/lib/vtls/vtls.c @@ -150,11 +150,11 @@ Curl_ssl_config_matches(struct ssl_primary_config *data, !Curl_timestrcmp(data->password, needle->password) && (data->authtype == needle->authtype) && #endif - Curl_safe_strcasecompare(data->cipher_list, needle->cipher_list) && - Curl_safe_strcasecompare(data->cipher_list13, needle->cipher_list13) && - Curl_safe_strcasecompare(data->curves, needle->curves) && - Curl_safe_strcasecompare(data->CRLfile, needle->CRLfile) && - Curl_safe_strcasecompare(data->pinned_key, needle->pinned_key)) + strcasecompare(data->cipher_list, needle->cipher_list) && + strcasecompare(data->cipher_list13, needle->cipher_list13) && + strcasecompare(data->curves, needle->curves) && + strcasecompare(data->CRLfile, needle->CRLfile) && + strcasecompare(data->pinned_key, needle->pinned_key)) return TRUE; return FALSE; |