-
Notifications
You must be signed in to change notification settings - Fork 5
/
extract.c
551 lines (433 loc) · 17.2 KB
/
extract.c
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// #include "shellprv.h"
#pragma hdrstop
// #include "newres.h"
// BEGIN NEW INCLUDES
#include <Windows.h>
#include <Shlwapi.h>
#include "extract.h"
#include "newexe.h"
#define try __try
#define except __except
#define ExtractIcons PrivateExtractIconsW
// END NEW INCLUDES
#define ICON_MAGIC 0
#define ICO_MAGIC1 1
#define CUR_MAGIC1 2
#define BMP_MAGIC ((WORD)'B'+((WORD)'M'<<8))
#define ANI_MAGIC ((WORD)'R'+((WORD)'I'<<8))
#define ANI_MAGIC1 ((WORD)'F'+((WORD)'F'<<8))
#define ANI_MAGIC4 ((WORD)'A'+((WORD)'C'<<8))
#define ANI_MAGIC5 ((WORD)'O'+((WORD)'N'<<8))
#define MZMAGIC ((WORD)'M'+((WORD)'Z'<<8))
#define PEMAGIC ((WORD)'P'+((WORD)'E'<<8))
#define LEMAGIC ((WORD)'L'+((WORD)'E'<<8))
#define RESOURCE_VA(x) ((x)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress)
#define RESOURCE_SIZE(x) ((x)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].Size)
#define NUMBER_OF_SECTIONS(x) ((x)->FileHeader.NumberOfSections)
#define FCC(c0,c1,c2,c3) ((DWORD)(c0)|((DWORD)(c1)<<8)|((DWORD)(c2)<<16)|((DWORD)(c3)<<24))
#define COM_FILE FCC('.', 'c', 'o', 'm')
#define BAT_FILE FCC('.', 'b', 'a', 't')
#define CMD_FILE FCC('.', 'c', 'm', 'd')
#define PIF_FILE FCC('.', 'p', 'i', 'f')
#define LNK_FILE FCC('.', 'l', 'n', 'k')
#define ICO_FILE FCC('.', 'i', 'c', 'o')
#define EXE_FILE FCC('.', 'e', 'x', 'e')
#define M_llseek(fh, lOff, iOrg) SetFilePointer((HANDLE)IntToPtr( fh ), lOff, NULL, (DWORD)iOrg)
#define MAGIC_ICON30 0
#define MAGIC_MARKZIBO ((WORD)'M'+((WORD)'Z'<<8))
#define SEEK_FROMZERO 0
#define SEEK_FROMCURRENT 1
#define SEEK_FROMEND 2
#define NSMOVE 0x0010
#define VER 0x0300
#define CCHICONPATHMAXLEN 128
// This returns a pointer to the rsrc_nameinfo of the resource with the
// given index and type, if it is found, otherwise it returns NULL.
LPBYTE FindResWithIndex(LPBYTE lpResTable, INT iResIndex, LPBYTE lpResType)
{
LPRESTYPEINFO lpResTypeInfo = (LPRESTYPEINFO)(lpResTable + sizeof(WORD));
while (lpResTypeInfo->rt_id)
{
if ((lpResTypeInfo->rt_id & RSORDID) &&
(MAKEINTRESOURCE(lpResTypeInfo->rt_id & ~RSORDID) == (LPTSTR)lpResType))
{
if (lpResTypeInfo->rt_nres > (WORD)iResIndex)
return((LPBYTE)(lpResTypeInfo + 1) + iResIndex * sizeof(RESNAMEINFO));
else
return NULL;
}
lpResTypeInfo = (LPRESTYPEINFO)((LPBYTE)(lpResTypeInfo + 1) + lpResTypeInfo->rt_nres * sizeof(RESNAMEINFO));
}
return NULL;
}
/* This returns the index (1-relative) of the given resource-id
* in the resource table, if it is found, otherwise it returns NULL.
*/
INT GetResIndex(LPBYTE lpResTable, INT iResId, LPBYTE lpResType)
{
WORD w;
LPRESNAMEINFO lpResNameInfo;
LPRESTYPEINFO lpResTypeInfo = (LPRESTYPEINFO)(lpResTable + sizeof(WORD));
while (lpResTypeInfo->rt_id)
{
if ((lpResTypeInfo->rt_id & RSORDID) && (MAKEINTRESOURCE(lpResTypeInfo->rt_id & ~RSORDID) == (LPTSTR)lpResType))
{
lpResNameInfo = (LPRESNAMEINFO)(lpResTypeInfo + 1);
for (w = 0; w < lpResTypeInfo->rt_nres; w++, lpResNameInfo++)
{
if ((lpResNameInfo->rn_id & RSORDID) && ((lpResNameInfo->rn_id & ~RSORDID) == iResId))
return(w + 1);
}
return 0;
}
lpResTypeInfo = (LPRESTYPEINFO)((LPBYTE)(lpResTypeInfo + 1) + lpResTypeInfo->rt_nres * sizeof(RESNAMEINFO));
}
return 0;
}
HANDLE SimpleLoadResource(HFILE fh, LPBYTE lpResTable, INT iResIndex, LPBYTE lpResType)
{
INT iShiftCount;
HICON hIcon;
LPBYTE lpIcon;
DWORD dwSize;
DWORD dwOffset;
LPRESNAMEINFO lpResPtr;
/* The first 2 bytes in ResTable indicate the amount other values should be
* shifted left.
*/
iShiftCount = *((WORD*)lpResTable);
lpResPtr = (LPRESNAMEINFO)FindResWithIndex(lpResTable, iResIndex, lpResType);
if (!lpResPtr)
return NULL;
/* Left shift the offset to form a LONG. */
dwOffset = MAKELONG(lpResPtr->rn_offset << iShiftCount, (lpResPtr->rn_offset) >> (16 - iShiftCount));
dwSize = lpResPtr->rn_length << iShiftCount;
if (M_llseek(fh, dwOffset, SEEK_FROMZERO) == -1L)
return NULL;
if (!(hIcon = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE, dwSize)))
return NULL;
if (!(lpIcon = GlobalLock(hIcon)))
goto SLRErr1;
if (_lread(fh, (LPVOID)lpIcon, dwSize) < dwSize)
goto SLRErr2;
GlobalUnlock(hIcon);
return hIcon;
SLRErr2:
GlobalUnlock(hIcon);
SLRErr1:
GlobalFree(hIcon);
return NULL;
}
VOID FreeIconList(HANDLE hIconList, int iKeepIcon)
{
MYICONINFO *lpIconList;
INT i;
EXTRACTICONINFO ExtractIconInfo = { NULL, NULL, NULL, 0 };
if (ExtractIconInfo.hIconList == hIconList) {
ExtractIconInfo.hIconList = NULL;
}
if (NULL != (lpIconList = (MYICONINFO *)GlobalLock(hIconList))) {
for (i = 0; i < ExtractIconInfo.nIcons; i++) {
if (i != iKeepIcon) {
DestroyIcon((lpIconList + i)->hIcon);
}
}
GlobalUnlock(hIconList);
GlobalFree(hIconList);
}
}
// Returns a handle to a list of icons
HANDLE APIENTRY InternalExtractIconListW(HANDLE hInst, LPWSTR lpszExeFileName, LPINT lpnIcons)
{
UINT cIcons, uiResult, i;
UINT * lpIDs = NULL;
HICON * lpIcons = NULL;
HGLOBAL hIconInfo = NULL;
MYICONINFO *lpIconInfo = NULL;
//
// Determine the number of icons
//
cIcons = PtrToUlong( ExtractIconW(hInst, lpszExeFileName, (UINT)-1));
if (cIcons <= 0)
return NULL;
//
// Allocate space for an array of UINT's and HICON's
//
lpIDs = GlobalAlloc(GPTR, cIcons * sizeof(UINT));
if (!lpIDs) {
goto IconList_Exit;
}
lpIcons = GlobalAlloc(GPTR, cIcons * sizeof(HICON));
if (!lpIcons) {
goto IconList_Exit;
}
//
// Allocate space for the array of icons
//
hIconInfo = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, cIcons * sizeof(MYICONINFO));
if (!hIconInfo) {
goto IconList_Exit;
}
//
// This has to be GlobalLock'ed since the handle is going to
// be passed back to the application.
//
lpIconInfo = GlobalLock(hIconInfo);
if (!lpIconInfo) {
goto IconList_Exit;
}
//
// Call ExtractIcons to do the real work.
//
uiResult = ExtractIcons(lpszExeFileName,
0,
GetSystemMetrics(SM_CXICON),
GetSystemMetrics(SM_CYICON),
lpIcons,
lpIDs,
cIcons,
0);
if (uiResult <= 0) {
goto IconList_Exit;
}
//
// Loop through the icons and fill in the array.
//
for (i=0; i < cIcons; i++) {
lpIconInfo[i].hIcon = lpIcons[i];
lpIconInfo[i].iIconId = lpIDs[i];
}
//
// Unlock the array handle.
//
GlobalUnlock(hIconInfo);
//
// Clean up allocations
//
GlobalFree(lpIDs);
GlobalFree(lpIcons);
//
// Success.
//
return hIconInfo;
IconList_Exit:
//
// Error case. Clean up and return NULL
//
if (lpIconInfo)
GlobalUnlock(hIconInfo);
if (hIconInfo)
GlobalFree(hIconInfo);
if (lpIcons)
GlobalFree(lpIcons);
if (lpIDs)
GlobalFree(lpIDs);
return NULL;
}
/* Returns the file's format: 2 for WIndows 2.X, 3 for WIndows 3.X, */
/* 0 if error. */
/* Returns the handle to the Icon resource corresponding to wIconIndex */
/* in lphIconRes, and the size of the resource in lpwSize. */
/* This is used only by Progman which needs to save the icon resource */
/* itself in the .GRP files (the actual icon handle is not wanted). */
/* */
/* 08-04-91 JohanneC Created. */
WORD APIENTRY ExtractIconResInfoW(HANDLE hInst, LPWSTR lpszFileName, WORD wIconIndex, LPWORD lpwSize, LPHANDLE lphIconRes)
{
HFILE fh;
WORD wMagic;
BOOL bNewResFormat;
HANDLE hIconDir; /* Icon directory */
LPBYTE lpIconDir;
HICON hIcon = NULL;
BOOL bFormatOK = FALSE;
INT nIconId;
WCHAR szFullPath[MAX_PATH];
int cchPath;
/* Try to open the specified file. */
/* Try to open the specified file. */
cchPath = SearchPathW(NULL, lpszFileName, NULL, ARRAYSIZE(szFullPath), szFullPath, NULL);
if (cchPath == 0 || cchPath >= MAX_PATH)
return 0;
fh = HandleToLong(CreateFileW((LPCWSTR)szFullPath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
if (fh == HandleToLong(INVALID_HANDLE_VALUE)) {
fh = HandleToLong(CreateFileW((LPCWSTR)szFullPath, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
}
if (fh == HandleToLong(INVALID_HANDLE_VALUE))
return 0;
/* Read the first two bytes in the file. */
if (_lread(fh, (LPVOID)&wMagic, sizeof(wMagic)) != sizeof(wMagic))
goto EIExit;
switch (wMagic) {
case MAGIC_ICON30:
{
INT i;
LPVOID lpIcon;
NEWHEADER NewHeader;
LPNEWHEADER lpHeader;
LPRESDIR lpResDir;
RESDIRDISK ResDirDisk;
#define MAXICONS 10
DWORD Offsets[MAXICONS];
/* Only one icon per .ICO file. */
if (wIconIndex) {
break;
}
/* Read the header and check if it is a valid ICO file. */
if (_lread(fh, ((LPBYTE)&NewHeader)+2, sizeof(NewHeader)-2) != sizeof(NewHeader)-2)
goto EICleanup1;
NewHeader.Reserved = MAGIC_ICON30;
/* Check if the file is in correct format */
if (NewHeader.ResType != 1)
goto EICleanup1;
/* Allocate enough space to create a Global Directory Resource. */
hIconDir = GlobalAlloc(GHND, (LONG)(sizeof(NEWHEADER)+NewHeader.ResCount*sizeof(RESDIR)));
if (hIconDir == NULL)
goto EICleanup1;
if ((lpHeader = (LPNEWHEADER)GlobalLock(hIconDir)) == NULL)
goto EICleanup2;
NewHeader.ResCount = (WORD)min((int)NewHeader.ResCount, MAXICONS);
// fill in this structure for user
*lpHeader = NewHeader;
// read in the stuff from disk, transfer it to a memory structure
// that user can deal with
lpResDir = (LPRESDIR)(lpHeader + 1);
for (i = 0; (WORD)i < NewHeader.ResCount; i++) {
if (_lread(fh, (LPVOID)&ResDirDisk, sizeof(ResDirDisk)) < sizeof(RESDIR))
goto EICleanup3;
Offsets[i] = ResDirDisk.Offset;
*lpResDir = *((LPRESDIR)&ResDirDisk);
lpResDir->idIcon = (WORD)(i+1); // fill in the id
lpResDir++;
}
/* Now that we have the Complete resource directory, let us find out the
* suitable form of icon (that matches the current display driver).
*/
lpIconDir = GlobalLock(hIconDir);
if (!lpIconDir) {
GlobalFree(hIconDir);
goto EIErrExit;
}
wIconIndex = (WORD)(LookupIconIdFromDirectory(lpIconDir, TRUE) - 1);
GlobalUnlock(hIconDir);
lpResDir = (LPRESDIR)(lpHeader+1) + wIconIndex;
/* Allocate memory for the Resource to be loaded. */
if ((hIcon = GlobalAlloc(GMEM_ZEROINIT | GMEM_MOVEABLE, (DWORD)lpResDir->BytesInRes)) == NULL)
goto EICleanup3;
if ((lpIcon = GlobalLock(hIcon)) == NULL)
goto EICleanup4;
/* Seek to the correct place and read in the resource */
if (M_llseek(fh, Offsets[wIconIndex], SEEK_FROMZERO) == -1L)
goto EICleanup5;
if (_lread(fh, (LPVOID)lpIcon, (DWORD)lpResDir->BytesInRes) < lpResDir->BytesInRes)
goto EICleanup5;
GlobalUnlock(hIcon);
*lphIconRes = hIcon;
*lpwSize = (WORD)lpResDir->BytesInRes;
bFormatOK = TRUE;
bNewResFormat = TRUE;
goto EICleanup3;
EICleanup5:
GlobalUnlock(hIcon);
EICleanup4:
GlobalFree(hIcon);
hIcon = (HICON)1;
EICleanup3:
GlobalUnlock(hIconDir);
EICleanup2:
GlobalFree(hIconDir);
EICleanup1:
break;
}
case MAGIC_MARKZIBO:
{
INT iTableSize;
LPBYTE lpResTable;
DWORD lOffset;
HANDLE hResTable;
NEWEXEHDR NEHeader;
/* Make sure that the file is in the NEW EXE format. */
if (M_llseek(fh, (LONG)0x3C, SEEK_FROMZERO) == -1L)
goto EIExit;
if (_lread(fh, (LPVOID)&lOffset, sizeof(lOffset)) != sizeof(lOffset))
goto EIExit;
if (lOffset == 0L)
goto EIExit;
/* Read in the EXE header. */
if (M_llseek(fh, lOffset, SEEK_FROMZERO) == -1L)
goto EIExit;
if (_lread(fh, (LPVOID)&NEHeader, sizeof(NEHeader)) != sizeof(NEHeader))
goto EIExit;
/* Is it a NEW EXE? */
if (NE_MAGIC(NEHeader) != NEMAGIC)
goto EIExit;
if ((NE_EXETYP(NEHeader) != NE_WINDOWS) &&
(NE_EXETYP(NEHeader) != NE_DEV386) &&
(NE_EXETYP(NEHeader) != NE_UNKNOWN)) /* Some Win2.X apps have NE_UNKNOWN in this field */
goto EIExit;
hIcon = NULL;
/* Are there any resources? */
if (NE_RSRCTAB(NEHeader) == NE_RESTAB(NEHeader))
goto EIExit;
/* Remember whether or not this is a Win3.0 EXE. */
bNewResFormat = (NEHeader.ne_expver >= VER);
/* Allocate space for the resource table. */
iTableSize = NE_RESTAB(NEHeader) - NE_RSRCTAB(NEHeader);
hResTable = GlobalAlloc(GMEM_ZEROINIT, (DWORD)iTableSize);
if (!hResTable)
goto EIExit;
/* Lock down the resource table. */
lpResTable = GlobalLock(hResTable);
if (!lpResTable) {
GlobalFree(hResTable);
goto EIExit;
}
/* Copy the resource table into memory. */
if (M_llseek(fh, (LONG)(lOffset + NE_RSRCTAB(NEHeader)), SEEK_FROMZERO) == -1)
goto EIErrExit;
if (_lread(fh, (LPBYTE)lpResTable, iTableSize) != (DWORD)iTableSize)
goto EIErrExit;
/* Is this a Win3.0 EXE? */
if (bNewResFormat) {
/* First, load the Icon directory. */
hIconDir = SimpleLoadResource(fh, lpResTable, (int)wIconIndex, (LPBYTE)RT_GROUP_ICON);
if (!hIconDir)
goto EIErrExit;
lpIconDir = GlobalLock(hIconDir);
if (!lpIconDir) {
GlobalFree(hIconDir);
goto EIErrExit;
}
nIconId = LookupIconIdFromDirectory(lpIconDir, TRUE);
wIconIndex = (WORD)(GetResIndex(lpResTable, nIconId, (LPBYTE)RT_ICON) - 1);
GlobalUnlock(hIconDir);
/* We're finished with the icon directory. */
GlobalFree(hIconDir);
/* Now load the selected icon. */
*lphIconRes = SimpleLoadResource(fh, lpResTable, (int)wIconIndex, (LPBYTE)RT_ICON);
}
else {
/* Simply load the specified icon. */
*lphIconRes = SimpleLoadResource(fh, lpResTable, (int)wIconIndex, (LPBYTE)RT_ICON);
}
if (*lphIconRes) {
*lpwSize = (WORD)GlobalSize(*lphIconRes);
}
bFormatOK = TRUE;
EIErrExit:
hResTable = NULL; // C4703 keeps bitching at me, oh lord!
GlobalUnlock(hResTable);
GlobalFree(hResTable);
break;
}
}
EIExit:
_lclose(fh);
hInst;
if (bFormatOK)
return (WORD)(bNewResFormat ? 3 : 2);
else
return 0;
}