Coverage Report

Created: 2025-07-11 06:33

/src/PROJ/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
0
{
39
0
  while(*first) {
40
0
    if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
41
      /* get out of the loop as soon as they do not match */
42
0
      return 0;
43
0
    first++;
44
0
    second++;
45
0
  }
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
0
  return !*first == !*second;
51
0
}
52
53
static int ncasecompare(const char *first, const char *second, size_t max)
54
0
{
55
0
  while(*first && max) {
56
0
    if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
57
0
      return 0;
58
0
    max--;
59
0
    first++;
60
0
    second++;
61
0
  }
62
0
  if(0 == max)
63
0
    return 1; /* they are equal this far */
64
65
0
  return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
66
0
}
67
68
/*
69
 * Only "raw" case insensitive strings. This is meant to be locale independent
70
 * and only compare strings we know are safe for this.
71
 *
72
 * The function is capable of comparing a-z case insensitively.
73
 *
74
 * Result is 1 if text matches and 0 if not.
75
 */
76
77
/* --- public function --- */
78
int curl_strequal(const char *first, const char *second)
79
0
{
80
0
  if(first && second)
81
    /* both pointers point to something then compare them */
82
0
    return casecompare(first, second);
83
84
  /* if both pointers are NULL then treat them as equal */
85
0
  return NULL == first && NULL == second;
86
0
}
87
88
/* --- public function --- */
89
int curl_strnequal(const char *first, const char *second, size_t max)
90
0
{
91
0
  if(first && second)
92
    /* both pointers point to something then compare them */
93
0
    return ncasecompare(first, second, max);
94
95
  /* if both pointers are NULL then treat them as equal if max is non-zero */
96
0
  return NULL == first && NULL == second && max;
97
0
}