Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #include "strcase.h" |
27 | | |
28 | | /* |
29 | | * curl_strequal() is for doing "raw" case insensitive strings. This is meant |
30 | | * to be locale independent and only compare strings we know are safe for |
31 | | * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for |
32 | | * further explanations as to why this function is necessary. |
33 | | */ |
34 | | |
35 | | static int casecompare(const char *first, const char *second) |
36 | 0 | { |
37 | 0 | while(*first) { |
38 | 0 | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) |
39 | | /* get out of the loop as soon as they do not match */ |
40 | 0 | return 0; |
41 | 0 | first++; |
42 | 0 | second++; |
43 | 0 | } |
44 | | /* If we are here either the strings are the same or the length is different. |
45 | | We can just test if the "current" character is non-zero for one and zero |
46 | | for the other. Note that the characters may not be exactly the same even |
47 | | if they match, we only want to compare zero-ness. */ |
48 | 0 | return !*first == !*second; |
49 | 0 | } |
50 | | |
51 | | static int ncasecompare(const char *first, const char *second, size_t max) |
52 | 7.07k | { |
53 | 7.45k | while(*first && max) { |
54 | 7.32k | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) |
55 | 6.94k | return 0; |
56 | 380 | max--; |
57 | 380 | first++; |
58 | 380 | second++; |
59 | 380 | } |
60 | 137 | if(max == 0) |
61 | 66 | return 1; /* they are equal this far */ |
62 | | |
63 | 71 | return Curl_raw_toupper(*first) == Curl_raw_toupper(*second); |
64 | 137 | } |
65 | | |
66 | | /* |
67 | | * Only "raw" case insensitive strings. This is meant to be locale independent |
68 | | * and only compare strings we know are safe for this. |
69 | | * |
70 | | * The function is capable of comparing a-z case insensitively. |
71 | | * |
72 | | * Result is 1 if text matches and 0 if not. |
73 | | */ |
74 | | |
75 | | /* --- public function --- */ |
76 | | int curl_strequal(const char *first, const char *second) |
77 | 0 | { |
78 | 0 | if(first && second) |
79 | | /* both pointers point to something then compare them */ |
80 | 0 | return casecompare(first, second); |
81 | | |
82 | | /* if both pointers are NULL then treat them as equal */ |
83 | 0 | return NULL == first && NULL == second; |
84 | 0 | } |
85 | | |
86 | | /* --- public function --- */ |
87 | | int curl_strnequal(const char *first, const char *second, size_t max) |
88 | 7.07k | { |
89 | 7.07k | if(first && second) |
90 | | /* both pointers point to something then compare them */ |
91 | 7.07k | return ncasecompare(first, second, max); |
92 | | |
93 | | /* if both pointers are NULL then treat them as equal if max is non-zero */ |
94 | 0 | return NULL == first && NULL == second && max; |
95 | 7.07k | } |