xref: /aosp_15_r20/external/curl/lib/strcase.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker 
25*6236dae4SAndroid Build Coastguard Worker #include "curl_setup.h"
26*6236dae4SAndroid Build Coastguard Worker 
27*6236dae4SAndroid Build Coastguard Worker #include <curl/curl.h>
28*6236dae4SAndroid Build Coastguard Worker 
29*6236dae4SAndroid Build Coastguard Worker #include "strcase.h"
30*6236dae4SAndroid Build Coastguard Worker 
31*6236dae4SAndroid Build Coastguard Worker /* Mapping table to go from lowercase to uppercase for plain ASCII.*/
32*6236dae4SAndroid Build Coastguard Worker static const unsigned char touppermap[256] = {
33*6236dae4SAndroid Build Coastguard Worker 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
34*6236dae4SAndroid Build Coastguard Worker 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
35*6236dae4SAndroid Build Coastguard Worker 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
36*6236dae4SAndroid Build Coastguard Worker 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78,
37*6236dae4SAndroid Build Coastguard Worker 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 65,
38*6236dae4SAndroid Build Coastguard Worker 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
39*6236dae4SAndroid Build Coastguard Worker 85, 86, 87, 88, 89, 90, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133,
40*6236dae4SAndroid Build Coastguard Worker 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
41*6236dae4SAndroid Build Coastguard Worker 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
42*6236dae4SAndroid Build Coastguard Worker 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181,
43*6236dae4SAndroid Build Coastguard Worker 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
44*6236dae4SAndroid Build Coastguard Worker 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213,
45*6236dae4SAndroid Build Coastguard Worker 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
46*6236dae4SAndroid Build Coastguard Worker 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245,
47*6236dae4SAndroid Build Coastguard Worker 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
48*6236dae4SAndroid Build Coastguard Worker };
49*6236dae4SAndroid Build Coastguard Worker 
50*6236dae4SAndroid Build Coastguard Worker /* Mapping table to go from uppercase to lowercase for plain ASCII.*/
51*6236dae4SAndroid Build Coastguard Worker static const unsigned char tolowermap[256] = {
52*6236dae4SAndroid Build Coastguard Worker 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
53*6236dae4SAndroid Build Coastguard Worker 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
54*6236dae4SAndroid Build Coastguard Worker 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
55*6236dae4SAndroid Build Coastguard Worker 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
56*6236dae4SAndroid Build Coastguard Worker 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 91, 92, 93, 94, 95,
57*6236dae4SAndroid Build Coastguard Worker 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
58*6236dae4SAndroid Build Coastguard Worker 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
59*6236dae4SAndroid Build Coastguard Worker 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
60*6236dae4SAndroid Build Coastguard Worker 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
61*6236dae4SAndroid Build Coastguard Worker 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
62*6236dae4SAndroid Build Coastguard Worker 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
63*6236dae4SAndroid Build Coastguard Worker 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
64*6236dae4SAndroid Build Coastguard Worker 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
65*6236dae4SAndroid Build Coastguard Worker 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
66*6236dae4SAndroid Build Coastguard Worker 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
67*6236dae4SAndroid Build Coastguard Worker };
68*6236dae4SAndroid Build Coastguard Worker 
69*6236dae4SAndroid Build Coastguard Worker 
70*6236dae4SAndroid Build Coastguard Worker /* Portable, consistent toupper. Do not use toupper() because its behavior is
71*6236dae4SAndroid Build Coastguard Worker    altered by the current locale. */
Curl_raw_toupper(char in)72*6236dae4SAndroid Build Coastguard Worker char Curl_raw_toupper(char in)
73*6236dae4SAndroid Build Coastguard Worker {
74*6236dae4SAndroid Build Coastguard Worker   return (char)touppermap[(unsigned char) in];
75*6236dae4SAndroid Build Coastguard Worker }
76*6236dae4SAndroid Build Coastguard Worker 
77*6236dae4SAndroid Build Coastguard Worker 
78*6236dae4SAndroid Build Coastguard Worker /* Portable, consistent tolower. Do not use tolower() because its behavior is
79*6236dae4SAndroid Build Coastguard Worker    altered by the current locale. */
Curl_raw_tolower(char in)80*6236dae4SAndroid Build Coastguard Worker char Curl_raw_tolower(char in)
81*6236dae4SAndroid Build Coastguard Worker {
82*6236dae4SAndroid Build Coastguard Worker   return (char)tolowermap[(unsigned char) in];
83*6236dae4SAndroid Build Coastguard Worker }
84*6236dae4SAndroid Build Coastguard Worker 
85*6236dae4SAndroid Build Coastguard Worker /*
86*6236dae4SAndroid Build Coastguard Worker  * curl_strequal() is for doing "raw" case insensitive strings. This is meant
87*6236dae4SAndroid Build Coastguard Worker  * to be locale independent and only compare strings we know are safe for
88*6236dae4SAndroid Build Coastguard Worker  * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
89*6236dae4SAndroid Build Coastguard Worker  * further explanations as to why this function is necessary.
90*6236dae4SAndroid Build Coastguard Worker  */
91*6236dae4SAndroid Build Coastguard Worker 
casecompare(const char * first,const char * second)92*6236dae4SAndroid Build Coastguard Worker static int casecompare(const char *first, const char *second)
93*6236dae4SAndroid Build Coastguard Worker {
94*6236dae4SAndroid Build Coastguard Worker   while(*first && *second) {
95*6236dae4SAndroid Build Coastguard Worker     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
96*6236dae4SAndroid Build Coastguard Worker       /* get out of the loop as soon as they do not match */
97*6236dae4SAndroid Build Coastguard Worker       return 0;
98*6236dae4SAndroid Build Coastguard Worker     first++;
99*6236dae4SAndroid Build Coastguard Worker     second++;
100*6236dae4SAndroid Build Coastguard Worker   }
101*6236dae4SAndroid Build Coastguard Worker   /* If we are here either the strings are the same or the length is different.
102*6236dae4SAndroid Build Coastguard Worker      We can just test if the "current" character is non-zero for one and zero
103*6236dae4SAndroid Build Coastguard Worker      for the other. Note that the characters may not be exactly the same even
104*6236dae4SAndroid Build Coastguard Worker      if they match, we only want to compare zero-ness. */
105*6236dae4SAndroid Build Coastguard Worker   return !*first == !*second;
106*6236dae4SAndroid Build Coastguard Worker }
107*6236dae4SAndroid Build Coastguard Worker 
108*6236dae4SAndroid Build Coastguard Worker /* --- public function --- */
curl_strequal(const char * first,const char * second)109*6236dae4SAndroid Build Coastguard Worker int curl_strequal(const char *first, const char *second)
110*6236dae4SAndroid Build Coastguard Worker {
111*6236dae4SAndroid Build Coastguard Worker   if(first && second)
112*6236dae4SAndroid Build Coastguard Worker     /* both pointers point to something then compare them */
113*6236dae4SAndroid Build Coastguard Worker     return casecompare(first, second);
114*6236dae4SAndroid Build Coastguard Worker 
115*6236dae4SAndroid Build Coastguard Worker   /* if both pointers are NULL then treat them as equal */
116*6236dae4SAndroid Build Coastguard Worker   return (NULL == first && NULL == second);
117*6236dae4SAndroid Build Coastguard Worker }
118*6236dae4SAndroid Build Coastguard Worker 
ncasecompare(const char * first,const char * second,size_t max)119*6236dae4SAndroid Build Coastguard Worker static int ncasecompare(const char *first, const char *second, size_t max)
120*6236dae4SAndroid Build Coastguard Worker {
121*6236dae4SAndroid Build Coastguard Worker   while(*first && *second && max) {
122*6236dae4SAndroid Build Coastguard Worker     if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
123*6236dae4SAndroid Build Coastguard Worker       return 0;
124*6236dae4SAndroid Build Coastguard Worker     max--;
125*6236dae4SAndroid Build Coastguard Worker     first++;
126*6236dae4SAndroid Build Coastguard Worker     second++;
127*6236dae4SAndroid Build Coastguard Worker   }
128*6236dae4SAndroid Build Coastguard Worker   if(0 == max)
129*6236dae4SAndroid Build Coastguard Worker     return 1; /* they are equal this far */
130*6236dae4SAndroid Build Coastguard Worker 
131*6236dae4SAndroid Build Coastguard Worker   return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
132*6236dae4SAndroid Build Coastguard Worker }
133*6236dae4SAndroid Build Coastguard Worker 
134*6236dae4SAndroid Build Coastguard Worker /* --- public function --- */
curl_strnequal(const char * first,const char * second,size_t max)135*6236dae4SAndroid Build Coastguard Worker int curl_strnequal(const char *first, const char *second, size_t max)
136*6236dae4SAndroid Build Coastguard Worker {
137*6236dae4SAndroid Build Coastguard Worker   if(first && second)
138*6236dae4SAndroid Build Coastguard Worker     /* both pointers point to something then compare them */
139*6236dae4SAndroid Build Coastguard Worker     return ncasecompare(first, second, max);
140*6236dae4SAndroid Build Coastguard Worker 
141*6236dae4SAndroid Build Coastguard Worker   /* if both pointers are NULL then treat them as equal if max is non-zero */
142*6236dae4SAndroid Build Coastguard Worker   return (NULL == first && NULL == second && max);
143*6236dae4SAndroid Build Coastguard Worker }
144*6236dae4SAndroid Build Coastguard Worker /* Copy an upper case version of the string from src to dest. The
145*6236dae4SAndroid Build Coastguard Worker  * strings may overlap. No more than n characters of the string are copied
146*6236dae4SAndroid Build Coastguard Worker  * (including any NUL) and the destination string will NOT be
147*6236dae4SAndroid Build Coastguard Worker  * NUL-terminated if that limit is reached.
148*6236dae4SAndroid Build Coastguard Worker  */
Curl_strntoupper(char * dest,const char * src,size_t n)149*6236dae4SAndroid Build Coastguard Worker void Curl_strntoupper(char *dest, const char *src, size_t n)
150*6236dae4SAndroid Build Coastguard Worker {
151*6236dae4SAndroid Build Coastguard Worker   if(n < 1)
152*6236dae4SAndroid Build Coastguard Worker     return;
153*6236dae4SAndroid Build Coastguard Worker 
154*6236dae4SAndroid Build Coastguard Worker   do {
155*6236dae4SAndroid Build Coastguard Worker     *dest++ = Curl_raw_toupper(*src);
156*6236dae4SAndroid Build Coastguard Worker   } while(*src++ && --n);
157*6236dae4SAndroid Build Coastguard Worker }
158*6236dae4SAndroid Build Coastguard Worker 
159*6236dae4SAndroid Build Coastguard Worker /* Copy a lower case version of the string from src to dest. The
160*6236dae4SAndroid Build Coastguard Worker  * strings may overlap. No more than n characters of the string are copied
161*6236dae4SAndroid Build Coastguard Worker  * (including any NUL) and the destination string will NOT be
162*6236dae4SAndroid Build Coastguard Worker  * NUL-terminated if that limit is reached.
163*6236dae4SAndroid Build Coastguard Worker  */
Curl_strntolower(char * dest,const char * src,size_t n)164*6236dae4SAndroid Build Coastguard Worker void Curl_strntolower(char *dest, const char *src, size_t n)
165*6236dae4SAndroid Build Coastguard Worker {
166*6236dae4SAndroid Build Coastguard Worker   if(n < 1)
167*6236dae4SAndroid Build Coastguard Worker     return;
168*6236dae4SAndroid Build Coastguard Worker 
169*6236dae4SAndroid Build Coastguard Worker   do {
170*6236dae4SAndroid Build Coastguard Worker     *dest++ = Curl_raw_tolower(*src);
171*6236dae4SAndroid Build Coastguard Worker   } while(*src++ && --n);
172*6236dae4SAndroid Build Coastguard Worker }
173*6236dae4SAndroid Build Coastguard Worker 
174*6236dae4SAndroid Build Coastguard Worker /* Compare case-sensitive NUL-terminated strings, taking care of possible
175*6236dae4SAndroid Build Coastguard Worker  * null pointers. Return true if arguments match.
176*6236dae4SAndroid Build Coastguard Worker  */
Curl_safecmp(char * a,char * b)177*6236dae4SAndroid Build Coastguard Worker bool Curl_safecmp(char *a, char *b)
178*6236dae4SAndroid Build Coastguard Worker {
179*6236dae4SAndroid Build Coastguard Worker   if(a && b)
180*6236dae4SAndroid Build Coastguard Worker     return !strcmp(a, b);
181*6236dae4SAndroid Build Coastguard Worker   return !a && !b;
182*6236dae4SAndroid Build Coastguard Worker }
183*6236dae4SAndroid Build Coastguard Worker 
184*6236dae4SAndroid Build Coastguard Worker /*
185*6236dae4SAndroid Build Coastguard Worker  * Curl_timestrcmp() returns 0 if the two strings are identical. The time this
186*6236dae4SAndroid Build Coastguard Worker  * function spends is a function of the shortest string, not of the contents.
187*6236dae4SAndroid Build Coastguard Worker  */
Curl_timestrcmp(const char * a,const char * b)188*6236dae4SAndroid Build Coastguard Worker int Curl_timestrcmp(const char *a, const char *b)
189*6236dae4SAndroid Build Coastguard Worker {
190*6236dae4SAndroid Build Coastguard Worker   int match = 0;
191*6236dae4SAndroid Build Coastguard Worker   int i = 0;
192*6236dae4SAndroid Build Coastguard Worker 
193*6236dae4SAndroid Build Coastguard Worker   if(a && b) {
194*6236dae4SAndroid Build Coastguard Worker     while(1) {
195*6236dae4SAndroid Build Coastguard Worker       match |= a[i]^b[i];
196*6236dae4SAndroid Build Coastguard Worker       if(!a[i] || !b[i])
197*6236dae4SAndroid Build Coastguard Worker         break;
198*6236dae4SAndroid Build Coastguard Worker       i++;
199*6236dae4SAndroid Build Coastguard Worker     }
200*6236dae4SAndroid Build Coastguard Worker   }
201*6236dae4SAndroid Build Coastguard Worker   else
202*6236dae4SAndroid Build Coastguard Worker     return a || b;
203*6236dae4SAndroid Build Coastguard Worker   return match;
204*6236dae4SAndroid Build Coastguard Worker }
205