strcase.c 7.3 KB

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