1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
| program decompiler_fichier_chm;
{$APPTYPE CONSOLE}
uses
Windows,
Classes,
ActiveX,
AxCtrls,
ComObj,
SysUtils;
const
CLSID_ITStorage: TGUID = (D1: $5D02926A; D2: $212E; D3: $11D0;
D4: ($9D, $F9, $00, $A0, $C9, $22, $E6, $EC));
IID_ITStorage: TGUID = (D1: $88CC31DE; D2: $27AB; D3: $11D0;
D4: ($9D, $F9, $00, $A0, $C9, $22, $E6, $EC));
var
SaveFolderPathLength: Integer;
type
TCompactionLev = (COMPACT_DATA, COMPACT_DATA_AND_PATH);
PItsControlData = ^TItsControlData;
_ITS_Control_Data = record
cdwControlData: UINT;
adwControlData: array [0..0] of UINT;
end;
TItsControlData=_ITS_Control_Data;
IItsStorage = interface(IUnknown)
['{88CC31DE-27AB-11D0-9DF9-00A0C922E6EC}']
function StgCreateDocFile(const pwcsName: PWideChar; grfMode: DWORD;
reserved: DWORD; var ppstgOpen: IStorage): HRESULT; stdcall;
function StgCreateDocFileOnILockBytes(plkbyt: ILockBytes; grfMode: DWORD;
reserved: DWORD; var ppstgOpen: IStorage): HRESULT; stdcall;
function StgIsStorageFile(const pwcsName: PWideChar): HRESULT; stdcall;
function StgIsStorageILockBytes(plkbyt: ILockBytes): HRESULT; stdcall;
function StgOpenStorage(const pwcsName: PWideChar; pstgPriority: IStorage;
grfMode: DWORD; snbExclude: TSNB; reserved: DWORD;
var ppstgOpen: IStorage): HRESULT; stdcall;
function StgOpenStorageOnILockBytes(plkbyt: ILockBytes;
pStgPriority: IStorage; grfMode: DWORD; snbExclude: TSNB;
reserved: DWORD; var ppstgOpen: IStorage): HRESULT; stdcall;
function StgSetTimes(const lpszName: PWideChar;
const pctime, patime, pmtime: TFileTime): HRESULT; stdcall;
function SetControlData(pControlData: PItsControlData): HRESULT; stdcall;
function DefaultControlData(
var ppControlData: PItsControlData): HRESULT; stdcall;
function Compact(const pwcsName: PWideChar;
iLev: TCompactionLev): HRESULT; stdcall;
end;
procedure ShowHelp;
begin
Writeln('Utiliser: decompiler_fichier_chm.exe [Chemin à Fichier CHM ]');
end;
procedure ExtractLog(IsStorage: Boolean; const Value: string);
begin
if IsStorage then
Writeln('Dossier créé: ',
Copy(Value, SaveFolderPathLength, Length(Value)))
else
Writeln('Extraction du Stream : ',
Copy(Value, SaveFolderPathLength, Length(Value)))
end;
function WaitKeyboardInput(const Promt: string): Char;
var
Done: Boolean;
IR: INPUT_RECORD;
hCon: THandle;
NumOfEvents,
NumOfEventsRead: DWORD;
I: Integer;
begin
Writeln;
Writeln(Promt);
Result := #0;
Done := False;
hCon := GetStdHandle(STD_INPUT_HANDLE);
try
while not Done do
begin
if not GetNumberOfConsoleInputEvents(hCon, NumOfEvents) then
raise EInOutError.CreateFmt(
'GetNumberOfConsoleInputEvents manqué %s',
[SysErrorMessage(GetLastError)]);
if NumOfEvents <= 0 then Continue;
for I := 0 to NumOfEvents - 1 do
begin
if (not ReadConsoleInput(hCon, ir, 1, NumOfEventsRead)) then
raise EInOutError.CreateFmt(
'ReadConsoleInput manqué %s', [SysErrorMessage(GetLastError)]);
Done :=
(NumOfEventsRead = 1) and
(IR.EventType = KEY_EVENT) and
TKeyEventRecord(IR.Event).bKeyDown;
if Done then
Result := TKeyEventRecord(IR.Event).AsciiChar;
end;
end;
except
on E : Exception do
Writeln(Format('Exception: %s', [E.Message]));
end;
end;
procedure ClearScreen;
var
ActualCoord, ZeroCoord: TCoord;
cWritten: DWORD;
hStdout: THandle;
chFillChar: Char;
begin
hStdout := GetStdHandle(STD_OUTPUT_HANDLE);
ActualCoord := GetLargestConsoleWindowSize(hStdout);
ZeroMemory(@ZeroCoord, SizeOf(TCoord));
chFillChar := ' ';
FillConsoleOutputCharacter(hStdout, chFillChar,
ActualCoord.X * ActualCoord.Y, ZeroCoord, cWritten);
SetConsoleCursorPosition(hStdout, ZeroCoord);
end;
function ValidStorage(const Path: string): Boolean;
var
ITS: IItsStorage;
begin
Result := False;
OleCheck(CoCreateInstance(CLSID_ITStorage, nil,
CLSCTX_INPROC_SERVER, IID_ITStorage, ITS));
if FileExists(Path) then
Result := ITS.StgIsStorageFile(StringToOleStr(Path)) = S_OK;
end;
procedure ExtractStream(const RootPath: string; Root: IStorage;
const StreamName: string);
var
TmpStream: IStream;
OS: TOleStream;
FS: TFileStream;
FilePath: string;
begin
OleCheck(Root.OpenStream(StringToOleStr(StreamName),
nil, STGM_READ or STGM_SHARE_EXCLUSIVE, 0, TmpStream));
OS := TOleStream.Create(TmpStream);
try
FilePath := IncludeTrailingPathDelimiter(RootPath) + StreamName;
FS := TFileStream.Create(FilePath, fmCreate);
try
OS.Position := 0;
FS.CopyFrom(OS, OS.Size);
ExtractLog(False, FilePath);
finally
FS.Free;
end;
finally
OS.Free;
end;
end;
procedure ExtractFolder(const RootPath: string; Root: IStorage);
var
ShellMalloc: IMalloc;
Enum: IEnumStatStg;
Fetched: Int64;
TmpElement: TStatStg;
ChildFolder: IStorage;
ChildPath: string;
begin
if (CoGetMalloc(1, ShellMalloc) <> S_OK) or (ShellMalloc = nil) then
raise EComponentError.Create('CoGetMalloc manqué.');
OleCheck(Root.EnumElements(0, nil, 0, Enum));
Fetched := 1;
while Fetched > 0 do
if Enum.Next(1, TmpElement, @Fetched) = S_OK then
if ShellMalloc.DidAlloc(TmpElement.pwcsName) = 1 then
try
if TmpElement.dwType = STGTY_STORAGE then
begin
OleCheck(Root.OpenStorage(TmpElement.pwcsName, nil,
STGM_READ or STGM_SHARE_EXCLUSIVE, nil, 0, ChildFolder));
ChildPath := IncludeTrailingPathDelimiter(RootPath) +
string(TmpElement.pwcsName);
ForceDirectories(ChildPath);
ExtractLog(True, ChildPath);
ExtractFolder(ChildPath, ChildFolder);
end
else
begin
ExtractStream(RootPath, Root, string(TmpElement.pwcsName));
end;
finally
ShellMalloc.Free(TmpElement.pwcsName);
end;
end;
procedure Decompile(const Path: string);
var
ITS: IItsStorage;
Root: IStorage;
SaveFolderPath: string;
begin
OleCheck(CoCreateInstance(CLSID_ITStorage, nil,
CLSCTX_INPROC_SERVER, IID_ITStorage, ITS));
OleCheck(ITS.StgOpenStorage(StringToOleStr(Path), nil,
STGM_READ or STGM_SHARE_EXCLUSIVE, nil, 0, Root));
SaveFolderPath := ExtractFileName(Path);
SaveFolderPath := Copy(SaveFolderPath, 1, Length(SaveFolderPath) -
Length(ExtractFileExt(Path)));
SaveFolderPath := ExtractFilePath(Path) +
UpperCase(SaveFolderPath) + '_DUMP';
ForceDirectories(SaveFolderPath);
Writeln('Dossier créé du dump: ', SaveFolderPath);
SaveFolderPathLength := Length(SaveFolderPath) + 1;
ExtractFolder(SaveFolderPath, Root);
Writeln;
Writeln('Toutes les taches ont été executéés');
end;
begin
try
ClearScreen;
CoInitialize(nil);
if ParamCount = 0 then
ShowHelp
else
begin
if ValidStorage(ParamStr(1)) then
Decompile(ParamStr(1))
else
Writeln(ExtractFileName(ParamStr(1)), ' Format du fichier incorrect.');
end;
except
on E: Exception do
Writeln(E.Message);
end;
WaitKeyboardInput('Appuyez sur n''importe quelle touche pour continuer...')
end. |
Partager