ei.cfg Removal/Restore Utility


SUBMITTED BY: Guest

DATE: Dec. 4, 2013, 12:55 p.m.

FORMAT: C++

SIZE: 5.5 kB

HITS: 964

  1. /**
  2. * ei.cfg Removal/Restore Utility
  3. * Copyright (C) Kai Liu. Licensed under a standard BSD-style license.
  4. * CRC16 code was adapted from 7-Zip, which is licensed under the GPL.
  5. **/
  6. //#include <windows.h>
  7. #pragma intrinsic(memset, memcmp, memcpy)
  8. #define CB_WORK_RANGE (0x1000000)
  9. #define CCH_WORK_RANGE (CB_WORK_RANGE/sizeof(WCHAR))
  10. #define CCH_PATH_BUFFER (MAX_PATH << 1)
  11. typedef struct {
  12. UINT16 ident;
  13. UINT16 version;
  14. UINT8 checksum;
  15. UINT8 reserved;
  16. UINT16 serial;
  17. UINT16 crc;
  18. UINT16 crclen;
  19. UINT32 location;
  20. } UDFTAG, *PUDFTAG;
  21. typedef struct {
  22. UINT32 cbExtent;
  23. BYTE location[6];
  24. BYTE iu[6];
  25. } LONGALLOC, *PLONGALLOC;
  26. typedef struct {
  27. UDFTAG tag;
  28. UINT16 version;
  29. UINT8 characteristics;
  30. UINT8 cbIdentifier;
  31. LONGALLOC ICB;
  32. UINT16 cbIU;
  33. BYTE misc[2];
  34. } UDFFILEDESC, *PUDFFILEDESC;
  35. // CRC16 adapted from 7-Zip
  36. void Crc16GenerateTable();
  37. UINT16 Crc16Calc(const void *data, size_t size);
  38. // CRC16 adapted from 7-Zip
  39. #pragma comment(linker, "/version:1.1")
  40. #pragma comment(linker, "/entry:eicfg_remover")
  41. void eicfg_remover( )
  42. {
  43. PVOID pvBuffer = LocalAlloc(LMEM_FIXED, CB_WORK_RANGE);
  44. // Set up the OPENFILENAME structure
  45. TCHAR szFileName[CCH_PATH_BUFFER];
  46. OPENFILENAME ofn;
  47. ZeroMemory(&ofn, sizeof(ofn));
  48. ofn.lStructSize = sizeof(ofn);
  49. ofn.lpstrFilter = TEXT("ISO Image (*.iso)\0*.iso\0");
  50. ofn.lpstrFile = szFileName;
  51. ofn.lpstrFile[0] = 0;
  52. ofn.nMaxFile = CCH_PATH_BUFFER;
  53. ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  54. if (pvBuffer && GetOpenFileName(&ofn))
  55. {
  56. HANDLE hFile = CreateFile(
  57. ofn.lpstrFile,
  58. GENERIC_READ | GENERIC_WRITE,
  59. 0,
  60. NULL,
  61. OPEN_EXISTING,
  62. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
  63. NULL
  64. );
  65. UINT64 cbFile;
  66. DWORD cbOperation;
  67. if ( hFile != INVALID_HANDLE_VALUE &&
  68. GetFileSizeEx(hFile, (PLARGE_INTEGER)&cbFile) &&
  69. cbFile > CB_WORK_RANGE &&
  70. ReadFile(hFile, pvBuffer, CB_WORK_RANGE, &cbOperation, NULL) &&
  71. cbOperation == CB_WORK_RANGE )
  72. {
  73. PWSTR pszBuffer = pvBuffer;
  74. UINT32 posStart;
  75. PUDFFILEDESC pDesc = NULL;
  76. int i;
  77. // Locate the file name entry
  78. for (i = 0x100; i < CCH_WORK_RANGE - 0x100; ++i)
  79. {
  80. static const WCHAR szNeedle[] = L"ei.cfg";
  81. if (memcmp(pszBuffer + i, szNeedle, sizeof(szNeedle) - sizeof(WCHAR)) == 0)
  82. {
  83. posStart = i * sizeof(WCHAR) - sizeof(UDFFILEDESC);
  84. pDesc = (PUDFFILEDESC)((PBYTE)pvBuffer + posStart);
  85. if ( pDesc->tag.ident == 257 &&
  86. pDesc->tag.version == 2 &&
  87. pDesc->tag.reserved == 0 &&
  88. pDesc->tag.crclen == sizeof(UDFFILEDESC) - sizeof(UDFTAG) + (sizeof(szNeedle) - sizeof(WCHAR)) &&
  89. pDesc->cbIdentifier == sizeof(szNeedle) - 1 &&
  90. pDesc->cbIU == 0 &&
  91. (pDesc->characteristics == 0 || pDesc->characteristics == 5) )
  92. {
  93. // Passed sanity check
  94. break;
  95. }
  96. else
  97. {
  98. pDesc = NULL;
  99. }
  100. }
  101. }
  102. if (pDesc)
  103. {
  104. PBYTE pbRaw = (PBYTE)pDesc;
  105. // Toggle whether the file is deleted
  106. pDesc->characteristics = (pDesc->characteristics) ? 0 : 5;
  107. // Recalculate the descriptor CRC16
  108. Crc16GenerateTable();
  109. pDesc->tag.crc = Crc16Calc(&pDesc->version, pDesc->tag.crclen);
  110. // Recalculate the tag checksum
  111. pDesc->tag.checksum = 0;
  112. for (i = 0; i < 4; ++i)
  113. pDesc->tag.checksum += pbRaw[i];
  114. for (i = 5; i < 16; ++i)
  115. pDesc->tag.checksum += pbRaw[i];
  116. // Update the ISO file
  117. SetFilePointer(hFile, posStart, NULL, FILE_BEGIN);
  118. WriteFile(hFile, pDesc, sizeof(UDFFILEDESC), &cbOperation, NULL);
  119. MessageBox(
  120. NULL,
  121. (pDesc->characteristics) ? TEXT("ei.cfg removed") : TEXT("ei.cfg restored"),
  122. NULL,
  123. MB_ICONINFORMATION
  124. );
  125. }
  126. else
  127. {
  128. MessageBox(NULL, TEXT("ei.cfg not found."), NULL, MB_ICONERROR);
  129. }
  130. }
  131. else
  132. {
  133. MessageBox(NULL, TEXT("Input error."), NULL, MB_ICONERROR);
  134. }
  135. if (hFile != INVALID_HANDLE_VALUE)
  136. CloseHandle(hFile);
  137. }
  138. LocalFree(pvBuffer);
  139. ExitProcess(0);
  140. }
  141. // CRC16 adapted from 7-Zip
  142. #define CRC16_INIT_VAL 0
  143. #define CRC16_GET_DIGEST(crc) (crc)
  144. #define CRC16_UPDATE_BYTE(crc, b) (g_Crc16Table[(((crc) >> 8) ^ (b)) & 0xFF] ^ ((crc) << 8))
  145. #define kCrc16Poly 0x1021
  146. UINT16 g_Crc16Table[256];
  147. void Crc16GenerateTable()
  148. {
  149. UINT32 i;
  150. int j;
  151. for (i = 0; i < 256; i++)
  152. {
  153. UINT32 r = (i << 8);
  154. for (j = 8; j > 0; j--)
  155. r = ((r & 0x8000) ? ((r << 1) ^ kCrc16Poly) : (r << 1)) & 0xFFFF;
  156. g_Crc16Table[i] = (UINT16)r;
  157. }
  158. }
  159. UINT16 Crc16_Update(UINT16 v, const void *data, size_t size)
  160. {
  161. const BYTE *p = (const BYTE *)data;
  162. for (; size > 0 ; size--, p++)
  163. v = CRC16_UPDATE_BYTE(v, *p);
  164. return v;
  165. }
  166. UINT16 Crc16Calc(const void *data, size_t size)
  167. {
  168. return Crc16_Update(CRC16_INIT_VAL, data, size);
  169. }
  170. // CRC16 adapted from 7-Zip

comments powered by Disqus