| 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 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 | * 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 | { |
| 39 | while(*first) { |
| 40 | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) |
| 41 | /* get out of the loop as soon as they do not match */ |
| 42 | return 0; |
| 43 | first++; |
| 44 | second++; |
| 45 | } |
| 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 | return !*first == !*second; |
| 51 | } |
| 52 | |
| 53 | static int ncasecompare(const char *first, const char *second, size_t max) |
| 54 | { |
| 55 | while(*first && max) { |
| 56 | if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) |
| 57 | return 0; |
| 58 | max--; |
| 59 | first++; |
| 60 | second++; |
| 61 | } |
| 62 | if(0 == max) |
| 63 | return 1; /* they are equal this far */ |
| 64 | |
| 65 | return Curl_raw_toupper(*first) == Curl_raw_toupper(*second); |
| 66 | } |
| 67 | |
| 68 | /* --- public function --- */ |
| 69 | int curl_strequal(const char *first, const char *second) |
| 70 | { |
| 71 | if(first && second) |
| 72 | /* both pointers point to something then compare them */ |
| 73 | return casecompare(first, second); |
| 74 | |
| 75 | /* if both pointers are NULL then treat them as equal */ |
| 76 | return NULL == first && NULL == second; |
| 77 | } |
| 78 | |
| 79 | /* --- public function --- */ |
| 80 | int curl_strnequal(const char *first, const char *second, size_t max) |
| 81 | { |
| 82 | if(first && second) |
| 83 | /* both pointers point to something then compare them */ |
| 84 | return ncasecompare(first, second, max); |
| 85 | |
| 86 | /* if both pointers are NULL then treat them as equal if max is non-zero */ |
| 87 | return NULL == first && NULL == second && max; |
| 88 | } |