Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.4 KiB

  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // strstr.c
  4. //
  5. // This file contains most commonly used string operation. ALl the setup project should link here
  6. // or add the common utility here to avoid duplicating code everywhere or using CRT runtime.
  7. //
  8. // Created 4\15\997 inateeg got from shlwapi
  9. //
  10. ///////////////////////////////////////////////////////////////////////////////////////////////////
  11. #include <windows.h>
  12. #include "sdsutils.h"
  13. //=================================================================================================
  14. //
  15. //=================================================================================================
  16. /*
  17. * ChrCmpI - Case insensitive character comparison for DBCS
  18. * Assumes w1, wMatch are characters to be compared;
  19. * HIBYTE of wMatch is 0 if not a DBC
  20. * Return FALSE if match, TRUE if not
  21. */
  22. BOOL ChrCmpIA(WORD w1, WORD wMatch)
  23. {
  24. char sz1[3], sz2[3];
  25. if (IsDBCSLeadByte(sz1[0] = LOBYTE(w1)))
  26. {
  27. sz1[1] = HIBYTE(w1);
  28. sz1[2] = '\0';
  29. }
  30. else
  31. sz1[1] = '\0';
  32. *(WORD FAR *)sz2 = wMatch;
  33. sz2[2] = '\0';
  34. return lstrcmpiA(sz1, sz2);
  35. }
  36. /*
  37. * ANSIStrChrI - Find first occurrence of character in string, case insensitive
  38. * Assumes lpStart points to start of null terminated string
  39. * wMatch is the character to match
  40. * returns ptr to the first occurrence of ch in str, NULL if not found.
  41. */
  42. LPSTR FAR PASCAL ANSIStrChrI(LPCSTR lpStart, WORD wMatch)
  43. {
  44. wMatch = (UINT)(IsDBCSLeadByte(LOBYTE(wMatch)) ? wMatch : LOBYTE(wMatch));
  45. for ( ; *lpStart; lpStart = AnsiNext(lpStart))
  46. {
  47. if (!ChrCmpIA(*(UNALIGNED WORD FAR *)lpStart, wMatch))
  48. return((LPSTR)lpStart);
  49. }
  50. return (NULL);
  51. }
  52. /*
  53. * StrCmpNI - Compare n bytes, case insensitive
  54. *
  55. * returns See lstrcmpi return values.
  56. */
  57. int FAR PASCAL StrCmpNIA(LPCSTR lpStr1, LPCSTR lpStr2, int nChar)
  58. {
  59. int i;
  60. LPCSTR lpszEnd = lpStr1 + nChar;
  61. for ( ; (lpszEnd > lpStr1) && (*lpStr1 || *lpStr2); (lpStr1 = AnsiNext(lpStr1)), (lpStr2 = AnsiNext(lpStr2))) {
  62. WORD w1;
  63. WORD w2;
  64. // If either pointer is at the null terminator already,
  65. // we want to copy just one byte to make sure we don't read
  66. // past the buffer (might be at a page boundary).
  67. w1 = (*lpStr1) ? *(UNALIGNED WORD *)lpStr1 : 0;
  68. w2 = (UINT)(IsDBCSLeadByte(*lpStr2)) ? *(UNALIGNED WORD *)lpStr2 : (WORD)(BYTE)(*lpStr2);
  69. i = ChrCmpIA(w1, w2);
  70. if (i)
  71. return i;
  72. }
  73. return 0;
  74. }
  75. /*
  76. * ANSiStrStrI - Search for first occurrence of a substring, case insensitive
  77. *
  78. * Assumes lpFirst points to source string
  79. * lpSrch points to string to search for
  80. * returns first occurrence of string if successful; NULL otherwise
  81. */
  82. LPSTR FAR PASCAL ANSIStrStrI(LPCSTR lpFirst, LPCSTR lpSrch)
  83. {
  84. UINT uLen;
  85. WORD wMatch;
  86. uLen = (UINT)lstrlenA(lpSrch);
  87. wMatch = *(UNALIGNED WORD FAR *)lpSrch;
  88. for ( ; (lpFirst = ANSIStrChrI(lpFirst, wMatch)) != 0 && StrCmpNIA(lpFirst, lpSrch, uLen);
  89. lpFirst=AnsiNext(lpFirst))
  90. continue; /* continue until we hit the end of the string or get a match */
  91. return((LPSTR)lpFirst);
  92. }