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.

92 lines
2.8 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. * StrCmpN - Compare n bytes
  18. *
  19. * returns See lstrcmp return values.
  20. */
  21. int
  22. StrCmpNA(
  23. LPCSTR lpStr1,
  24. LPCSTR lpStr2,
  25. int nChar)
  26. {
  27. LPCSTR lpszEnd = lpStr1 + nChar;
  28. for ( ; (lpszEnd > lpStr1) && (*lpStr1 || *lpStr2); lpStr1 = AnsiNext(lpStr1), lpStr2 = AnsiNext(lpStr2)) {
  29. WORD w1;
  30. WORD w2;
  31. // If either pointer is at the null terminator already,
  32. // we want to copy just one byte to make sure we don't read
  33. // past the buffer (might be at a page boundary).
  34. w1 = (*lpStr1) ? *(UNALIGNED WORD *)lpStr1 : 0;
  35. w2 = (*lpStr2) ? *(UNALIGNED WORD *)lpStr2 : 0;
  36. // (ChrCmpA returns FALSE if the characters match)
  37. // Do the characters match?
  38. if (ChrCmpA_inline(w1, w2))
  39. {
  40. // No; determine the lexical value of the comparison
  41. // (since ChrCmp just returns true/false).
  42. char sz1[4];
  43. char sz2[4];
  44. // Since the character may be a DBCS character; we
  45. // copy two bytes into each temporary buffer
  46. // (in preparation for the lstrcmp call).
  47. (*(WORD *)sz1) = w1;
  48. (*(WORD *)sz2) = w2;
  49. // Add null terminators to temp buffers
  50. *AnsiNext(sz1) = 0;
  51. *AnsiNext(sz2) = 0;
  52. return lstrcmpA(sz1, sz2);
  53. }
  54. }
  55. return 0;
  56. }
  57. /*
  58. * ANSIStrStr - Search for first occurrence of a substring
  59. *
  60. * Assumes lpSource points to source string
  61. * lpSrch points to string to search for
  62. * returns first occurrence of string if successful; NULL otherwise
  63. */
  64. PSTR ANSIStrStr(LPCSTR lpFirst, LPCSTR lpSrch)
  65. {
  66. UINT uLen;
  67. WORD wMatch;
  68. uLen = (UINT)lstrlen(lpSrch);
  69. wMatch = *(UNALIGNED WORD FAR *)lpSrch;
  70. for ( ; (lpFirst=ANSIStrChr(lpFirst, wMatch))!=0 && StrCmpNA(lpFirst, lpSrch, uLen);
  71. lpFirst=AnsiNext(lpFirst))
  72. continue; /* continue until we hit the end of the string or get a match */
  73. return((LPSTR)lpFirst);
  74. }