rawstr.c 3.6 KB

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