strcase.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "strcase.h"
  25. static char raw_tolower(char in);
  26. /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
  27. its behavior is altered by the current locale. */
  28. char Curl_raw_toupper(char in)
  29. {
  30. #if !defined(CURL_DOES_CONVERSIONS)
  31. if(in >= 'a' && in <= 'z')
  32. return (char)('A' + in - 'a');
  33. #else
  34. switch(in) {
  35. case 'a':
  36. return 'A';
  37. case 'b':
  38. return 'B';
  39. case 'c':
  40. return 'C';
  41. case 'd':
  42. return 'D';
  43. case 'e':
  44. return 'E';
  45. case 'f':
  46. return 'F';
  47. case 'g':
  48. return 'G';
  49. case 'h':
  50. return 'H';
  51. case 'i':
  52. return 'I';
  53. case 'j':
  54. return 'J';
  55. case 'k':
  56. return 'K';
  57. case 'l':
  58. return 'L';
  59. case 'm':
  60. return 'M';
  61. case 'n':
  62. return 'N';
  63. case 'o':
  64. return 'O';
  65. case 'p':
  66. return 'P';
  67. case 'q':
  68. return 'Q';
  69. case 'r':
  70. return 'R';
  71. case 's':
  72. return 'S';
  73. case 't':
  74. return 'T';
  75. case 'u':
  76. return 'U';
  77. case 'v':
  78. return 'V';
  79. case 'w':
  80. return 'W';
  81. case 'x':
  82. return 'X';
  83. case 'y':
  84. return 'Y';
  85. case 'z':
  86. return 'Z';
  87. }
  88. #endif
  89. return in;
  90. }
  91. /* Portable, consistent tolower (remember EBCDIC). Do not use tolower() because
  92. its behavior is altered by the current locale. */
  93. static char raw_tolower(char in)
  94. {
  95. #if !defined(CURL_DOES_CONVERSIONS)
  96. if(in >= 'A' && in <= 'Z')
  97. return (char)('a' + in - 'A');
  98. #else
  99. switch(in) {
  100. case 'A':
  101. return 'a';
  102. case 'B':
  103. return 'b';
  104. case 'C':
  105. return 'c';
  106. case 'D':
  107. return 'd';
  108. case 'E':
  109. return 'e';
  110. case 'F':
  111. return 'f';
  112. case 'G':
  113. return 'g';
  114. case 'H':
  115. return 'h';
  116. case 'I':
  117. return 'i';
  118. case 'J':
  119. return 'j';
  120. case 'K':
  121. return 'k';
  122. case 'L':
  123. return 'l';
  124. case 'M':
  125. return 'm';
  126. case 'N':
  127. return 'n';
  128. case 'O':
  129. return 'o';
  130. case 'P':
  131. return 'p';
  132. case 'Q':
  133. return 'q';
  134. case 'R':
  135. return 'r';
  136. case 'S':
  137. return 's';
  138. case 'T':
  139. return 't';
  140. case 'U':
  141. return 'u';
  142. case 'V':
  143. return 'v';
  144. case 'W':
  145. return 'w';
  146. case 'X':
  147. return 'x';
  148. case 'Y':
  149. return 'y';
  150. case 'Z':
  151. return 'z';
  152. }
  153. #endif
  154. return in;
  155. }
  156. /*
  157. * Curl_strcasecompare() is for doing "raw" case insensitive strings. This is
  158. * meant to be locale independent and only compare strings we know are safe
  159. * for this. See
  160. * https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for some
  161. * further explanation to why this function is necessary.
  162. *
  163. * The function is capable of comparing a-z case insensitively even for
  164. * non-ascii.
  165. *
  166. * @unittest: 1301
  167. */
  168. int Curl_strcasecompare(const char *first, const char *second)
  169. {
  170. while(*first && *second) {
  171. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
  172. /* get out of the loop as soon as they don't match */
  173. break;
  174. first++;
  175. second++;
  176. }
  177. /* we do the comparison here (possibly again), just to make sure that if the
  178. loop above is skipped because one of the strings reached zero, we must not
  179. return this as a successful match */
  180. return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
  181. }
  182. int Curl_safe_strcasecompare(const char *first, const char *second)
  183. {
  184. if(first && second)
  185. /* both pointers point to something then compare them */
  186. return Curl_strcasecompare(first, second);
  187. /* if both pointers are NULL then treat them as equal */
  188. return (NULL == first && NULL == second);
  189. }
  190. /*
  191. * @unittest: 1301
  192. */
  193. int Curl_strncasecompare(const char *first, const char *second, size_t max)
  194. {
  195. while(*first && *second && max) {
  196. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
  197. break;
  198. }
  199. max--;
  200. first++;
  201. second++;
  202. }
  203. if(0 == max)
  204. return 1; /* they are equal this far */
  205. return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
  206. }
  207. /* Copy an upper case version of the string from src to dest. The
  208. * strings may overlap. No more than n characters of the string are copied
  209. * (including any NUL) and the destination string will NOT be
  210. * NUL-terminated if that limit is reached.
  211. */
  212. void Curl_strntoupper(char *dest, const char *src, size_t n)
  213. {
  214. if(n < 1)
  215. return;
  216. do {
  217. *dest++ = Curl_raw_toupper(*src);
  218. } while(*src++ && --n);
  219. }
  220. /* Copy a lower case version of the string from src to dest. The
  221. * strings may overlap. No more than n characters of the string are copied
  222. * (including any NUL) and the destination string will NOT be
  223. * NUL-terminated if that limit is reached.
  224. */
  225. void Curl_strntolower(char *dest, const char *src, size_t n)
  226. {
  227. if(n < 1)
  228. return;
  229. do {
  230. *dest++ = raw_tolower(*src);
  231. } while(*src++ && --n);
  232. }
  233. /* --- public functions --- */
  234. int curl_strequal(const char *first, const char *second)
  235. {
  236. return Curl_strcasecompare(first, second);
  237. }
  238. int curl_strnequal(const char *first, const char *second, size_t max)
  239. {
  240. return Curl_strncasecompare(first, second, max);
  241. }