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.

99 lines
3.7 KiB

  1. /**********************************************************************
  2. Cache Search Stuff (simple, fast strstr)
  3. Marc Miller (t-marcmi) - 1998
  4. **********************************************************************/
  5. #include "priv.h"
  6. #ifndef __HISTORY_CACHE_SEARCH__
  7. #define __HISTORY_CACHE_SEARCH__
  8. #define UNICODE_SIGNATURE 0xFFFE
  9. #define UNICODE_SIGNATURE_BACKWARDS 0xFEFF
  10. namespace CacheSearchEngine {
  11. class IWideSequentialReadStream {
  12. public:
  13. virtual BOOL GetNextChar(WCHAR &wc) = 0;
  14. };
  15. class StreamSearcher {
  16. protected:
  17. LPWSTR _pwszPreparedSearchTarget;
  18. static inline BOOL s_IsWhiteSpace(const WCHAR &wc) {
  19. return ((wc == L' ') || (wc == L'\t') || (wc == L'\n') || (wc == L'\r'));
  20. }
  21. static inline LPCWSTR s_SkipWhiteSpace(LPCWSTR pwszStr) {
  22. LPCWSTR pwszTemp = pwszStr;
  23. while(s_IsWhiteSpace(*pwszTemp))
  24. ++pwszTemp;
  25. return pwszTemp;
  26. }
  27. void _PrepareSearchTarget(LPCWSTR pwszSearchTarget);
  28. public:
  29. StreamSearcher(LPCWSTR pwszSearchTarget) { _PrepareSearchTarget(pwszSearchTarget); }
  30. ~StreamSearcher() { if ( _pwszPreparedSearchTarget ) { LocalFree(_pwszPreparedSearchTarget); _pwszPreparedSearchTarget = NULL; } }
  31. BOOL SearchCharStream(IWideSequentialReadStream &wsrs, BOOL fIsHTML = FALSE);
  32. };
  33. class StringStream : public IWideSequentialReadStream {
  34. BOOL fCleanup; // the string we hold needs to be deallocated by us
  35. LPCWSTR pwszStr;
  36. UINT uCurrentPos;
  37. public:
  38. StringStream(LPCWSTR pwszStr, BOOL fDuplicate = FALSE) : uCurrentPos(0), fCleanup(fDuplicate) {
  39. if (fDuplicate)
  40. SHStrDupW(pwszStr, const_cast<LPWSTR *>(&(this->pwszStr)));
  41. else
  42. this->pwszStr = pwszStr;
  43. }
  44. StringStream(LPCSTR pszStr, BOOL fDuplicate = FALSE) : uCurrentPos(0), fCleanup(TRUE) {
  45. SHStrDupA(pszStr, const_cast<LPWSTR *>(&(pwszStr)));
  46. }
  47. ~StringStream() {
  48. if (fCleanup)
  49. CoTaskMemFree(const_cast<LPWSTR>(pwszStr));
  50. }
  51. BOOL GetNextChar(WCHAR &wc) {
  52. wc = pwszStr[uCurrentPos];
  53. if (wc)
  54. ++uCurrentPos;
  55. return wc;
  56. }
  57. };
  58. class CacheStreamWrapper : public IWideSequentialReadStream {
  59. protected:
  60. HANDLE _hCacheStream;
  61. DWORD _dwCacheStreamLoc; // our offset into the actual cache file
  62. BOOL _fEndOfFile;
  63. // I can never remember which one is little endian and which is big endian
  64. enum DATATYPEENUM { UNICODE_DATA = 0, UNICODE_BACKWARDS_DATA, ASCII_DATA } _dataType;
  65. static inline BOOL s_IsUnicode(DATATYPEENUM dte) { return dte < ASCII_DATA; }
  66. static inline UINT s_Charsize (DATATYPEENUM dte) { return s_IsUnicode(dte) ? sizeof(USHORT) : sizeof(CHAR); }
  67. static DWORD s_dwPageSize;
  68. LPBYTE _pbBuff; /* buffer of bytes which are type-neutral
  69. _pbBuff is allocated with VirtualAlloc */
  70. LPBYTE _pbBuffPos; // current position in buffer
  71. LPBYTE _pbBuffLast; // last byte in buffer
  72. DWORD _dwBuffSize; // current valid buffer size (not allocated size)
  73. BOOL _ReadNextBlock();
  74. BOOL _GetNextByte(BYTE &b);
  75. public:
  76. CacheStreamWrapper(HANDLE hCacheStream);
  77. ~CacheStreamWrapper();
  78. BOOL GetNextChar(WCHAR &wc);
  79. };
  80. BOOL SearchCacheStream(StreamSearcher &cse, HANDLE hCacheStream, BOOL fIsHTML = FALSE);
  81. }
  82. #endif