strcase.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_strcasecompare() is for doing "raw" case insensitive strings. This is
  77. * meant to be locale independent and only compare strings we know are safe
  78. * for this. See
  79. * https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for some
  80. * further explanation to why this function is necessary.
  81. *
  82. * @unittest: 1301
  83. */
  84. int Curl_strcasecompare(const char *first, const char *second)
  85. {
  86. while(*first && *second) {
  87. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
  88. /* get out of the loop as soon as they don't match */
  89. return 0;
  90. first++;
  91. second++;
  92. }
  93. /* If we're here either the strings are the same or the length is different.
  94. We can just test if the "current" character is non-zero for one and zero
  95. for the other. Note that the characters may not be exactly the same even
  96. if they match, we only want to compare zero-ness. */
  97. return !*first == !*second;
  98. }
  99. int Curl_safe_strcasecompare(const char *first, const char *second)
  100. {
  101. if(first && second)
  102. /* both pointers point to something then compare them */
  103. return Curl_strcasecompare(first, second);
  104. /* if both pointers are NULL then treat them as equal */
  105. return (NULL == first && NULL == second);
  106. }
  107. /*
  108. * @unittest: 1301
  109. */
  110. int Curl_strncasecompare(const char *first, const char *second, size_t max)
  111. {
  112. while(*first && *second && max) {
  113. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
  114. break;
  115. }
  116. max--;
  117. first++;
  118. second++;
  119. }
  120. if(0 == max)
  121. return 1; /* they are equal this far */
  122. return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
  123. }
  124. /* Copy an upper case version of the string from src to dest. The
  125. * strings may overlap. No more than n characters of the string are copied
  126. * (including any NUL) and the destination string will NOT be
  127. * NUL-terminated if that limit is reached.
  128. */
  129. void Curl_strntoupper(char *dest, const char *src, size_t n)
  130. {
  131. if(n < 1)
  132. return;
  133. do {
  134. *dest++ = Curl_raw_toupper(*src);
  135. } while(*src++ && --n);
  136. }
  137. /* Copy a lower case version of the string from src to dest. The
  138. * strings may overlap. No more than n characters of the string are copied
  139. * (including any NUL) and the destination string will NOT be
  140. * NUL-terminated if that limit is reached.
  141. */
  142. void Curl_strntolower(char *dest, const char *src, size_t n)
  143. {
  144. if(n < 1)
  145. return;
  146. do {
  147. *dest++ = Curl_raw_tolower(*src);
  148. } while(*src++ && --n);
  149. }
  150. /* Compare case-sensitive NUL-terminated strings, taking care of possible
  151. * null pointers. Return true if arguments match.
  152. */
  153. bool Curl_safecmp(char *a, char *b)
  154. {
  155. if(a && b)
  156. return !strcmp(a, b);
  157. return !a && !b;
  158. }
  159. /* --- public functions --- */
  160. int curl_strequal(const char *first, const char *second)
  161. {
  162. return Curl_strcasecompare(first, second);
  163. }
  164. int curl_strnequal(const char *first, const char *second, size_t max)
  165. {
  166. return Curl_strncasecompare(first, second, max);
  167. }