1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
29 /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
30 its behavior is altered by the current locale. */
31 char Curl_raw_toupper(char in)
33 #if !defined(CURL_DOES_CONVERSIONS)
34 if(in >= 'a' && in <= 'z')
35 return (char)('A' + in - 'a');
97 * Curl_strcasecompare() is for doing "raw" case insensitive strings. This is
98 * meant to be locale independent and only compare strings we know are safe
100 * https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for some
101 * further explanation to why this function is necessary.
103 * The function is capable of comparing a-z case insensitively even for
109 int Curl_strcasecompare(const char *first, const char *second)
111 while(*first && *second) {
112 if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
113 /* get out of the loop as soon as they don't match */
118 /* we do the comparison here (possibly again), just to make sure that if the
119 loop above is skipped because one of the strings reached zero, we must not
120 return this as a successful match */
121 return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
124 int Curl_safe_strcasecompare(const char *first, const char *second)
127 /* both pointers point to something then compare them */
128 return Curl_strcasecompare(first, second);
130 /* if both pointers are NULL then treat them as equal */
131 return (NULL == first && NULL == second);
137 int Curl_strncasecompare(const char *first, const char *second, size_t max)
139 while(*first && *second && max) {
140 if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
148 return 1; /* they are equal this far */
150 return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
153 /* Copy an upper case version of the string from src to dest. The
154 * strings may overlap. No more than n characters of the string are copied
155 * (including any NUL) and the destination string will NOT be
156 * NUL-terminated if that limit is reached.
158 void Curl_strntoupper(char *dest, const char *src, size_t n)
164 *dest++ = Curl_raw_toupper(*src);
165 } while(*src++ && --n);
168 /* --- public functions --- */
170 int curl_strequal(const char *first, const char *second)
172 return Curl_strcasecompare(first, second);
174 int curl_strnequal(const char *first, const char *second, size_t max)
176 return Curl_strncasecompare(first, second, max);