1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | *
|
---|
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.se/docs/copyright.html.
|
---|
13 | *
|
---|
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.
|
---|
17 | *
|
---|
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
19 | * KIND, either express or implied.
|
---|
20 | *
|
---|
21 | ***************************************************************************/
|
---|
22 |
|
---|
23 | #include "curl_setup.h"
|
---|
24 |
|
---|
25 | #include <curl/curl.h>
|
---|
26 |
|
---|
27 | #include "strcase.h"
|
---|
28 |
|
---|
29 | static char raw_tolower(char in);
|
---|
30 |
|
---|
31 | /* Portable, consistent toupper. Do not use toupper() because its behavior is
|
---|
32 | altered by the current locale. */
|
---|
33 | char Curl_raw_toupper(char in)
|
---|
34 | {
|
---|
35 | if(in >= 'a' && in <= 'z')
|
---|
36 | return (char)('A' + in - 'a');
|
---|
37 | return in;
|
---|
38 | }
|
---|
39 |
|
---|
40 |
|
---|
41 | /* Portable, consistent tolower. Do not use tolower() because its behavior is
|
---|
42 | altered by the current locale. */
|
---|
43 | static char raw_tolower(char in)
|
---|
44 | {
|
---|
45 | if(in >= 'A' && in <= 'Z')
|
---|
46 | return (char)('a' + in - 'A');
|
---|
47 | return in;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Curl_strcasecompare() is for doing "raw" case insensitive strings. This is
|
---|
52 | * meant to be locale independent and only compare strings we know are safe
|
---|
53 | * for this. See
|
---|
54 | * https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for some
|
---|
55 | * further explanation to why this function is necessary.
|
---|
56 | *
|
---|
57 | * @unittest: 1301
|
---|
58 | */
|
---|
59 |
|
---|
60 | int Curl_strcasecompare(const char *first, const char *second)
|
---|
61 | {
|
---|
62 | while(*first && *second) {
|
---|
63 | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
|
---|
64 | /* get out of the loop as soon as they don't match */
|
---|
65 | break;
|
---|
66 | first++;
|
---|
67 | second++;
|
---|
68 | }
|
---|
69 | /* we do the comparison here (possibly again), just to make sure that if the
|
---|
70 | loop above is skipped because one of the strings reached zero, we must not
|
---|
71 | return this as a successful match */
|
---|
72 | return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
|
---|
73 | }
|
---|
74 |
|
---|
75 | int Curl_safe_strcasecompare(const char *first, const char *second)
|
---|
76 | {
|
---|
77 | if(first && second)
|
---|
78 | /* both pointers point to something then compare them */
|
---|
79 | return Curl_strcasecompare(first, second);
|
---|
80 |
|
---|
81 | /* if both pointers are NULL then treat them as equal */
|
---|
82 | return (NULL == first && NULL == second);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * @unittest: 1301
|
---|
87 | */
|
---|
88 | int Curl_strncasecompare(const char *first, const char *second, size_t max)
|
---|
89 | {
|
---|
90 | while(*first && *second && max) {
|
---|
91 | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
|
---|
92 | break;
|
---|
93 | }
|
---|
94 | max--;
|
---|
95 | first++;
|
---|
96 | second++;
|
---|
97 | }
|
---|
98 | if(0 == max)
|
---|
99 | return 1; /* they are equal this far */
|
---|
100 |
|
---|
101 | return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /* Copy an upper case version of the string from src to dest. The
|
---|
105 | * strings may overlap. No more than n characters of the string are copied
|
---|
106 | * (including any NUL) and the destination string will NOT be
|
---|
107 | * NUL-terminated if that limit is reached.
|
---|
108 | */
|
---|
109 | void Curl_strntoupper(char *dest, const char *src, size_t n)
|
---|
110 | {
|
---|
111 | if(n < 1)
|
---|
112 | return;
|
---|
113 |
|
---|
114 | do {
|
---|
115 | *dest++ = Curl_raw_toupper(*src);
|
---|
116 | } while(*src++ && --n);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /* Copy a lower case version of the string from src to dest. The
|
---|
120 | * strings may overlap. No more than n characters of the string are copied
|
---|
121 | * (including any NUL) and the destination string will NOT be
|
---|
122 | * NUL-terminated if that limit is reached.
|
---|
123 | */
|
---|
124 | void Curl_strntolower(char *dest, const char *src, size_t n)
|
---|
125 | {
|
---|
126 | if(n < 1)
|
---|
127 | return;
|
---|
128 |
|
---|
129 | do {
|
---|
130 | *dest++ = raw_tolower(*src);
|
---|
131 | } while(*src++ && --n);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /* Compare case-sensitive NUL-terminated strings, taking care of possible
|
---|
135 | * null pointers. Return true if arguments match.
|
---|
136 | */
|
---|
137 | bool Curl_safecmp(char *a, char *b)
|
---|
138 | {
|
---|
139 | if(a && b)
|
---|
140 | return !strcmp(a, b);
|
---|
141 | return !a && !b;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* --- public functions --- */
|
---|
145 |
|
---|
146 | int curl_strequal(const char *first, const char *second)
|
---|
147 | {
|
---|
148 | return Curl_strcasecompare(first, second);
|
---|
149 | }
|
---|
150 | int curl_strnequal(const char *first, const char *second, size_t max)
|
---|
151 | {
|
---|
152 | return Curl_strncasecompare(first, second, max);
|
---|
153 | }
|
---|