Source code of Windows XP (NT5)
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.

144 lines
4.2 KiB

  1. /*Includes-----------------------------------------------------------*/
  2. #include "msrating.h"
  3. #pragma hdrstop
  4. #include <npassert.h>
  5. #include "ratings.h"
  6. #include "roll.h"
  7. /*IsUrlInFile---------------------------------------------------------------*/
  8. /*
  9. Return VAlue length of Best Match
  10. */
  11. static HRESULT IsUrlInFile(LPCTSTR pszTargetUrl, char **ppRating, const char* pFile, DWORD dwFile, HANDLE hAbortEvent, void* (WINAPI *MemAlloc)(long size))
  12. {
  13. LocalListRecordHeader *pllrh;
  14. DWORD dwBytesRead;
  15. HRESULT hrRet = S_OK;
  16. int nBest, nActual, nCmp;
  17. const char *pBest;
  18. BOOL fAbort;
  19. dwBytesRead = 0;
  20. nBest = 0;
  21. nActual = strlenf(pszTargetUrl);
  22. fAbort = FALSE;
  23. //Go through each recored until there is a match good enough or an abort
  24. while (!fAbort && nActual != nBest && dwBytesRead <= dwFile)
  25. {
  26. pllrh = (LocalListRecordHeader*) pFile;
  27. if (pllrh->nUrl > nBest &&
  28. (
  29. (pllrh->nUrl == nActual)
  30. ||
  31. (
  32. pllrh->nUrl < nActual &&
  33. (
  34. (pszTargetUrl[pllrh->nUrl] == '\\')
  35. ||
  36. (pszTargetUrl[pllrh->nUrl] == '/')
  37. ||
  38. (pszTargetUrl[pllrh->nUrl] == ':')
  39. )
  40. )
  41. )
  42. )
  43. {
  44. nCmp = strnicmpf(pFile+sizeof(LocalListRecordHeader), pszTargetUrl, pllrh->nUrl);
  45. if (0==nCmp)
  46. {
  47. nBest = pllrh->nUrl;
  48. pBest = pFile;
  49. hrRet = pllrh->hrRet;
  50. }
  51. //the local list is alphabetized
  52. else if (1==nCmp) break;
  53. }
  54. dwBytesRead += pllrh->nUrl + pllrh->nRating + sizeof(LocalListRecordHeader);
  55. pFile += pllrh->nUrl + pllrh->nRating + sizeof(LocalListRecordHeader);
  56. fAbort = (WAIT_OBJECT_0 == WaitForSingleObject(hAbortEvent, 0));
  57. }
  58. //was the match close enough??!?!?
  59. if (!fAbort && nBest)
  60. {
  61. //yes, now try to copy rating
  62. pllrh = (LocalListRecordHeader*) pBest;
  63. if (pllrh->nRating)
  64. {
  65. *ppRating = (char*) MemAlloc(pllrh->nRating+1);
  66. if (*ppRating)
  67. {
  68. CopyMemory(*ppRating, pBest + sizeof(LocalListRecordHeader) + pllrh->nUrl, pllrh->nRating);
  69. (*ppRating)[pllrh->nRating] = 0;
  70. }
  71. }
  72. }
  73. else
  74. {
  75. //no... oh well
  76. hrRet = E_RATING_NOT_FOUND;
  77. }
  78. return hrRet;
  79. }
  80. /*RatingObtainFromLocalList-------------------------------------------------*/
  81. /*
  82. Grab rating information from local file.
  83. Should operate synchronously and take small amount of time.
  84. Doesn't check pOrd->fAbort too often.
  85. */
  86. HRESULT RatingHelperProcLocalList(LPCTSTR pszTargetUrl, HANDLE hAbortEvent, void* (WINAPI *MemAlloc)(long size), char **ppRatingOut)
  87. {
  88. DWORD dwFile;
  89. HRESULT hrRet = E_RATING_NOT_FOUND;
  90. HANDLE hFile, hMap;
  91. BOOL fAbort;
  92. const char *pFile;
  93. ASSERT(ppRatingOut);
  94. //Open and check from approved list
  95. hFile = CreateFile(
  96. FILE_NAME_LIST, GENERIC_READ,
  97. FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
  98. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  99. if (hFile != INVALID_HANDLE_VALUE)
  100. {
  101. dwFile = GetFileSize(hFile, NULL);
  102. hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  103. if (hMap)
  104. {
  105. pFile = (const char*) MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
  106. if (pFile)
  107. {
  108. //check for correct file type
  109. if (BATCAVE_LOCAL_LIST_MAGIC_COOKIE == *((DWORD*) pFile))
  110. {
  111. pFile += sizeof(DWORD);
  112. dwFile -= sizeof(DWORD);
  113. fAbort = (WAIT_OBJECT_0 == WaitForSingleObject(hAbortEvent, 0));
  114. if (!fAbort) hrRet = IsUrlInFile(pszTargetUrl, ppRatingOut, pFile, dwFile, hAbortEvent, MemAlloc);
  115. pFile -= sizeof(DWORD);
  116. }
  117. }
  118. dwFile = (DWORD) UnmapViewOfFile((LPVOID)pFile);
  119. CloseHandle(hMap);
  120. }
  121. CloseHandle(hFile);
  122. }
  123. return hrRet;
  124. }