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.

96 lines
1.9 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. int _cdecl main(int argc, char** argv)
  4. {
  5. if(argc != 3)
  6. {
  7. fprintf(stderr, "Usage: infstrip <filename> <token>\n");
  8. return -1;
  9. }
  10. HANDLE hFile;
  11. hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ,
  12. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  13. if(hFile == INVALID_HANDLE_VALUE)
  14. {
  15. fprintf(stderr, "Unable to open file %s\n", argv[1]);
  16. return -1;
  17. }
  18. HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY,0,0,
  19. NULL);
  20. if(!hMap)
  21. {
  22. fprintf(stderr, "Unable to open file %s\n", argv[1]);
  23. return -1;
  24. }
  25. void* pFile;
  26. pFile = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
  27. if(!pFile)
  28. {
  29. fprintf(stderr, "Unable to open file %s\n", argv[1]);
  30. return -1;
  31. }
  32. char* pStart = reinterpret_cast<char*>(pFile);
  33. char* szSearchString = argv[2];
  34. DWORD dwSize = GetFileSize(hFile, NULL);
  35. char* pEnd = pStart + dwSize - 1;
  36. int iPos = strlen(szSearchString)-1;
  37. DWORD dwtruncationpoint = dwSize;
  38. for(; pEnd >= pStart; pEnd--)
  39. {
  40. if(*pEnd == szSearchString[iPos])
  41. {
  42. // Match.
  43. // Found the complete string.
  44. if(iPos == 0)
  45. {
  46. // Mark this as the truncation point.
  47. dwtruncationpoint = pEnd-pStart;
  48. iPos = strlen(szSearchString)-1;
  49. continue;
  50. }
  51. iPos--;
  52. }
  53. else
  54. {
  55. // Reset.
  56. iPos = strlen(szSearchString)-1;
  57. }
  58. }
  59. // Copy the file
  60. char* pNewFile = new char[dwtruncationpoint];
  61. memcpy(pNewFile, pStart, dwtruncationpoint);
  62. // Close the previous file.
  63. UnmapViewOfFile(pFile);
  64. CloseHandle(hMap);
  65. CloseHandle(hFile);
  66. // Truncate the file
  67. hFile = CreateFile(argv[1], GENERIC_WRITE, FILE_SHARE_READ,
  68. NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  69. if(hFile == INVALID_HANDLE_VALUE)
  70. {
  71. fprintf(stderr, "Error truncating file");
  72. return -1;
  73. }
  74. DWORD dwWritten = 0;
  75. if(!WriteFile(hFile, pNewFile, dwtruncationpoint, &dwWritten, NULL))
  76. {
  77. fprintf(stderr, "Error writing file");
  78. return -1;
  79. }
  80. CloseHandle(hFile);
  81. return 0;
  82. }