/src/gdal/curl/lib/strequal.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include <curl/curl.h> |
28 | | #include "strcase.h" |
29 | | |
30 | | /* |
31 | | * curl_strequal() is for doing "raw" case insensitive strings. This is meant |
32 | | * to be locale independent and only compare strings we know are safe for |
33 | | * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for |
34 | | * further explanations as to why this function is necessary. |
35 | | */ |
36 | | |
37 | | static int casecompare(const char *first, const char *second) |
38 | 9.32k | { |
39 | 24.4k | while(*first) { |
40 | 22.9k | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) |
41 | | /* get out of the loop as soon as they do not match */ |
42 | 7.84k | return 0; |
43 | 15.0k | first++; |
44 | 15.0k | second++; |
45 | 15.0k | } |
46 | | /* If we are here either the strings are the same or the length is different. |
47 | | We can just test if the "current" character is non-zero for one and zero |
48 | | for the other. Note that the characters may not be exactly the same even |
49 | | if they match, we only want to compare zero-ness. */ |
50 | 1.47k | return !*first == !*second; |
51 | 9.32k | } |
52 | | |
53 | | static int ncasecompare(const char *first, const char *second, size_t max) |
54 | 7.96k | { |
55 | 38.1k | while(*first && max) { |
56 | 30.9k | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) |
57 | 828 | return 0; |
58 | 30.1k | max--; |
59 | 30.1k | first++; |
60 | 30.1k | second++; |
61 | 30.1k | } |
62 | 7.14k | if(0 == max) |
63 | 7.14k | return 1; /* they are equal this far */ |
64 | | |
65 | 1 | return Curl_raw_toupper(*first) == Curl_raw_toupper(*second); |
66 | 7.14k | } |
67 | | |
68 | | /* --- public function --- */ |
69 | | int curl_strequal(const char *first, const char *second) |
70 | 9.51k | { |
71 | 9.51k | if(first && second) |
72 | | /* both pointers point to something then compare them */ |
73 | 9.32k | return casecompare(first, second); |
74 | | |
75 | | /* if both pointers are NULL then treat them as equal */ |
76 | 186 | return NULL == first && NULL == second; |
77 | 9.51k | } |
78 | | |
79 | | /* --- public function --- */ |
80 | | int curl_strnequal(const char *first, const char *second, size_t max) |
81 | 7.96k | { |
82 | 7.96k | if(first && second) |
83 | | /* both pointers point to something then compare them */ |
84 | 7.96k | return ncasecompare(first, second, max); |
85 | | |
86 | | /* if both pointers are NULL then treat them as equal if max is non-zero */ |
87 | 0 | return NULL == first && NULL == second && max; |
88 | 7.96k | } |