rawstr.c 3.7 KB

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