strcase.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.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. /* Portable, consistent toupper (remember EBCDIC). Do not use toupper() because
  26. its behavior is altered by the current locale. */
  27. char Curl_raw_toupper(char in)
  28. {
  29. #if !defined(CURL_DOES_CONVERSIONS)
  30. if(in >= 'a' && in <= 'z')
  31. return (char)('A' + in - 'a');
  32. #else
  33. switch(in) {
  34. case 'a':
  35. return 'A';
  36. case 'b':
  37. return 'B';
  38. case 'c':
  39. return 'C';
  40. case 'd':
  41. return 'D';
  42. case 'e':
  43. return 'E';
  44. case 'f':
  45. return 'F';
  46. case 'g':
  47. return 'G';
  48. case 'h':
  49. return 'H';
  50. case 'i':
  51. return 'I';
  52. case 'j':
  53. return 'J';
  54. case 'k':
  55. return 'K';
  56. case 'l':
  57. return 'L';
  58. case 'm':
  59. return 'M';
  60. case 'n':
  61. return 'N';
  62. case 'o':
  63. return 'O';
  64. case 'p':
  65. return 'P';
  66. case 'q':
  67. return 'Q';
  68. case 'r':
  69. return 'R';
  70. case 's':
  71. return 'S';
  72. case 't':
  73. return 'T';
  74. case 'u':
  75. return 'U';
  76. case 'v':
  77. return 'V';
  78. case 'w':
  79. return 'W';
  80. case 'x':
  81. return 'X';
  82. case 'y':
  83. return 'Y';
  84. case 'z':
  85. return 'Z';
  86. }
  87. #endif
  88. return in;
  89. }
  90. /*
  91. * Curl_raw_equal() is for doing "raw" case insensitive strings. This is meant
  92. * to be locale independent and only compare strings we know are safe for
  93. * this. See https://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
  94. * some further explanation to why this function is necessary.
  95. *
  96. * The function is capable of comparing a-z case insensitively even for
  97. * non-ascii.
  98. *
  99. * @unittest: 1301
  100. */
  101. int Curl_strcasecompare(const char *first, const char *second)
  102. {
  103. while(*first && *second) {
  104. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second))
  105. /* get out of the loop as soon as they don't match */
  106. break;
  107. first++;
  108. second++;
  109. }
  110. /* we do the comparison here (possibly again), just to make sure that if the
  111. loop above is skipped because one of the strings reached zero, we must not
  112. return this as a successful match */
  113. return (Curl_raw_toupper(*first) == Curl_raw_toupper(*second));
  114. }
  115. int Curl_safe_strcasecompare(const char *first, const char *second)
  116. {
  117. if(first && second)
  118. /* both pointers point to something then compare them */
  119. return Curl_strcasecompare(first, second);
  120. else
  121. /* if both pointers are NULL then treat them as equal */
  122. return (NULL == first && NULL == second);
  123. }
  124. /*
  125. * @unittest: 1301
  126. */
  127. int Curl_strncasecompare(const char *first, const char *second, size_t max)
  128. {
  129. while(*first && *second && max) {
  130. if(Curl_raw_toupper(*first) != Curl_raw_toupper(*second)) {
  131. break;
  132. }
  133. max--;
  134. first++;
  135. second++;
  136. }
  137. if(0 == max)
  138. return 1; /* they are equal this far */
  139. return Curl_raw_toupper(*first) == Curl_raw_toupper(*second);
  140. }
  141. /* Copy an upper case version of the string from src to dest. The
  142. * strings may overlap. No more than n characters of the string are copied
  143. * (including any NUL) and the destination string will NOT be
  144. * NUL-terminated if that limit is reached.
  145. */
  146. void Curl_strntoupper(char *dest, const char *src, size_t n)
  147. {
  148. if(n < 1)
  149. return;
  150. do {
  151. *dest++ = Curl_raw_toupper(*src);
  152. } while(*src++ && --n);
  153. }
  154. /* --- public functions --- */
  155. int curl_strequal(const char *first, const char *second)
  156. {
  157. return Curl_strcasecompare(first, second);
  158. }
  159. int curl_strnequal(const char *first, const char *second, size_t max)
  160. {
  161. return Curl_strncasecompare(first, second, max);
  162. }