source: trunk/src/helpers/exeh.c@ 365

Last change on this file since 365 was 261, checked in by pr, 21 years ago

XWP bug 583

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 127.5 KB
Line 
1
2/*
3 *@@sourcefile exeh.c:
4 * contains code to load and parse executable headers
5 * and resources. See exehOpen for details.
6 *
7 * This file is new with V0.9.16 (2002-01-05) [umoeller]
8 * and contains code formerly in dosh2.c.
9 *
10 * Function prefixes:
11 * -- exe* executable helper functions.
12 *
13 * Note: Version numbering in this file relates to XWorkplace version
14 * numbering.
15 *
16 *@@header "helpers\exeh.h"
17 */
18
19/*
20 * This file Copyright (C) 2000-2002 Ulrich M”ller,
21 * Martin Lafaix.
22 * This file is part of the "XWorkplace helpers" source package.
23 * This is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published
25 * by the Free Software Foundation, in version 2 as it comes in the
26 * "COPYING" file of the XWorkplace main distribution.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 */
32
33#define OS2EMX_PLAIN_CHAR
34 // this is needed for "os2emx.h"; if this is defined,
35 // emx will define PSZ as _signed_ char, otherwise
36 // as unsigned char
37
38#define INCL_DOSMODULEMGR
39#define INCL_DOSPROCESS
40#define INCL_DOSEXCEPTIONS
41#define INCL_DOSSESMGR
42#define INCL_DOSQUEUES
43#define INCL_DOSMISC
44#define INCL_DOSDEVICES
45#define INCL_DOSDEVIOCTL
46#define INCL_DOSERRORS
47
48#define INCL_WINPROGRAMLIST
49#include <os2.h>
50
51// #include <stdlib.h>
52#include <string.h>
53#include <stdio.h>
54#include <setjmp.h>
55
56#include "setup.h" // code generation and debugging options
57
58#include "helpers\dosh.h"
59#include "helpers\ensure.h"
60#include "helpers\except.h"
61#include "helpers\exeh.h"
62#include "helpers\standards.h"
63#include "helpers\stringh.h"
64
65#pragma hdrstop
66
67/*
68 *@@category: Helpers\Control program helpers\Executable info
69 * these functions can retrieve BLDLEVEL information,
70 * imported modules information, exported functions information,
71 * and resources information from any executable module. See
72 * exehOpen.
73 */
74
75/********************************************************************
76 *
77 * Executable functions
78 *
79 ********************************************************************/
80
81/*
82 *@@ exehOpen:
83 * this opens the specified executable file
84 * (which can be an .EXE, .COM, .DLL, or
85 * driver file) for use with the other
86 * exeh* functions.
87 *
88 * Basically this does a plain DosOpen on the
89 * executable file and reads in the various
90 * executable headers manually. Since DosLoadModule
91 * et al is never used, the OS/2 executable loader
92 * is completely circumvented. (Side note:
93 * DosLoadModule cannot be used on EXE files in
94 * the first place.)
95 *
96 * To be more precise, this uses doshOpen internally
97 * and can thus profit from the caching that is
98 * implemented there (V0.9.16).
99 *
100 * If no error occurs, NO_ERROR is returned
101 * and a pointer to a new EXECUTABLE structure
102 * is stored in *ppExec. Consider this pointer a
103 * handle and pass it to exehClose to clean up.
104 *
105 * If NO_ERROR is returned, all the fields through
106 * ulOS are set in EXECUTABLE. The psz* fields
107 * which follow afterwards require an additional
108 * call to exehQueryBldLevel.
109 *
110 * NOTE: If NO_ERROR is returned, the executable
111 * file is kept open by this function. It will
112 * only be closed when you call exehClose.
113 *
114 * If errors occur, this function returns the
115 * following error codes:
116 *
117 * -- ERROR_NOT_ENOUGH_MEMORY: malloc() failed.
118 *
119 * -- ERROR_INVALID_EXE_SIGNATURE (191): header is
120 * neither plain DOS, nor NE, nor LX, nor PE.
121 * The given file probably isn't even an
122 * executable.
123 *
124 * -- ERROR_BAD_EXE_FORMAT (193): header was
125 * recognized, but the header data was
126 * not understood. Also this might be
127 * returned for .COM files which are
128 * not recognized.
129 *
130 * -- ERROR_INVALID_PARAMETER: ppExec is NULL.
131 *
132 * plus those of doshOpen and doshReadAt.
133 *
134 * The following executable types are supported
135 * (see EXECUTABLE for details):
136 *
137 * -- Plain DOS 3.x executable without new header.
138 *
139 * -- New Executable (NE), used by Win16 and
140 * 16-bit OS/2 and still many of today's drivers.
141 *
142 * -- Linear Executable (LX), OS/2 2.x and above.
143 *
144 * -- Portable Executable (PE), used by Win32.
145 *
146 * -- For files with the .COM, .BAT, or .CMD
147 * extensions, this will _not_ open the
148 * executable file, but set flags in EXECUTABLE
149 * only. Most importantly, EXECUTABLE.pDosExeHeader
150 * will be NULL.
151 *
152 * V0.9.12 adds support for NOSTUB executables,
153 * which are new-style executables (NE or LX)
154 * without a leading DOS header. JFS.IFS uses that
155 * format, for example. The executable then starts
156 * directly with the NE or LX header. I am not sure
157 * whether PE supports such beasts as well... if
158 * so, it should be supported too.
159 *
160 * Note that not all of the other exeh* functions
161 * support all of the executable types. See the
162 * respective function descriptions for remarks.
163 *
164 * @@todo:
165 *
166 * win95 \WINDOWS\extract.exe is NE with a non-standard format
167 * win16 \WINDOWS\EXPAND.EXE
168 * win16 \WINDOWS\MSD.EXE"
169 *
170 *@@added V0.9.0 [umoeller]
171 *@@changed V0.9.1 (2000-02-13) [umoeller]: fixed 32-bits flag
172 *@@changed V0.9.7 (2000-12-20) [lafaix]: fixed ulNewHeaderOfs
173 *@@changed V0.9.10 (2001-04-08) [lafaix]: added PE support
174 *@@changed V0.9.10 (2001-04-08) [umoeller]: now setting ppExec only if NO_ERROR is returned
175 *@@changed V0.9.12 (2001-05-03) [umoeller]: added support for NOSTUB newstyle executables
176 *@@changed V0.9.16 (2001-12-08) [umoeller]: now using OPEN_SHARE_DENYWRITE
177 *@@changed V0.9.16 (2001-12-08) [umoeller]: fLibrary was never set, works for LX, NE, and PE now
178 *@@changed V0.9.16 (2001-12-08) [umoeller]: speed optimizations, changed some return codes
179 *@@changed V0.9.16 (2002-01-04) [umoeller]: added fixes for COM, BAT, CMD extensions
180 *@@changed V1.0.0 (2002-08-18) [umoeller]: this was completely broken for files without extensions (os2krnl)
181 */
182
183APIRET exehOpen(const char* pcszExecutable,
184 PEXECUTABLE* ppExec)
185{
186 APIRET arc = NO_ERROR;
187
188 PEXECUTABLE pExec = NULL;
189
190 PXFILE pFile = NULL;
191 ULONG cbFile = 0;
192 PCSZ pExt;
193 BOOL fOpenFile = FALSE;
194 BOOL fLoadNewHeader = FALSE;
195 ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
196
197 if (!ppExec)
198 return ERROR_INVALID_PARAMETER;
199
200 if (!(pExec = (PEXECUTABLE)malloc(sizeof(EXECUTABLE))))
201 return ERROR_NOT_ENOUGH_MEMORY;
202
203 memset(pExec, 0, sizeof(EXECUTABLE));
204
205 // check some of the default extensions
206 // V0.9.16 (2002-01-04) [umoeller]
207 if (!(pExt = doshGetExtension(pcszExecutable)))
208 {
209 // has no extension: then open file!
210 // fixed V1.0.0 (2002-08-18) [umoeller]
211 fOpenFile = TRUE;
212 }
213 else
214 {
215 // has extension:
216 if (!stricmp(pExt, "COM"))
217 {
218 // I am not willing to find out more about the
219 // .COM executable format, so for this one case,
220 // let OS/2 determine what we have here
221 // (otherwise we do _not_ use DosQueryAppType
222 // because it's quite an expensive call)
223 ULONG ulDosAppType = 0;
224 if (!(arc = DosQueryAppType((PSZ)pcszExecutable, &ulDosAppType)))
225 {
226 if (ulDosAppType & FAPPTYP_DOS) // 0x20
227 pExec->ulOS = EXEOS_DOS3;
228 else
229 {
230 ULONG fl = ulDosAppType & FAPPTYP_WINDOWAPI; // 0x03
231 if ( (fl == FAPPTYP_WINDOWCOMPAT) // 0x02)
232 || (fl == FAPPTYP_NOTWINDOWCOMPAT) // 0x01)
233 )
234 pExec->ulOS = EXEOS_OS2;
235 else
236 arc = ERROR_BAD_EXE_FORMAT;
237 }
238
239 pExec->ulExeFormat = EXEFORMAT_COM;
240 }
241 }
242 else if (!stricmp(pExt, "BAT"))
243 {
244 pExec->ulOS = EXEOS_DOS3;
245 pExec->ulExeFormat = EXEFORMAT_TEXT_BATCH;
246 }
247 else if (!stricmp(pExt, "CMD"))
248 {
249 pExec->ulOS = EXEOS_OS2;
250 pExec->ulExeFormat = EXEFORMAT_TEXT_CMD;
251 }
252 else
253 fOpenFile = TRUE;
254 }
255
256 if ( (fOpenFile) // none of the above
257 && (!(arc = doshOpen((PSZ)pcszExecutable,
258 XOPEN_READ_EXISTING,
259 &cbFile,
260 &pFile)))
261 )
262 {
263 // file opened successfully:
264 pExec->pFile = pFile;
265 pExec->cbDosExeHeader = sizeof(DOSEXEHEADER);
266
267 // read old DOS EXE header
268 if ((arc = doshReadAt(pFile,
269 0,
270 &pExec->cbDosExeHeader, // in/out
271 (PBYTE)&pExec->DosExeHeader,
272 DRFL_FAILIFLESS)))
273 pExec->cbDosExeHeader = 0;
274 else
275 {
276 // now check if we really have a DOS header
277 if (pExec->DosExeHeader.usDosExeID != 0x5a4d)
278 {
279 // arc = ERROR_INVALID_EXE_SIGNATURE;
280
281 // V0.9.12 (2001-05-03) [umoeller]
282 // try loading new header directly; there are
283 // drivers which were built with NOSTUB, and
284 // the exe image starts out with the NE or LX
285 // image directly (try JFS.IFS)
286 fLoadNewHeader = TRUE;
287 // ulNewHeaderOfs is 0 now
288
289 // remove the DOS header info, since we have none
290 // V0.9.12 (2001-05-03) [umoeller]
291 // FREE(pExec->pDosExeHeader);
292 pExec->cbDosExeHeader = 0;
293 }
294 else
295 {
296 // V1.0.3 (2004-10-24) [pr]: Some non-DOS EXEs have a relocation table
297 // offset which is 0 - these were previously identified as DOS EXEs.
298 // we have a DOS header:
299 if ( ( (pExec->DosExeHeader.usRelocTableOfs == 0)
300 || (pExec->DosExeHeader.usRelocTableOfs >= sizeof(DOSEXEHEADER))
301 )
302 && (pExec->DosExeHeader.ulNewHeaderOfs != 0)
303 )
304 {
305 // we have a new header offset:
306 fLoadNewHeader = TRUE;
307 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
308 }
309 else
310 {
311 // else DOS:
312 pExec->ulOS = EXEOS_DOS3;
313 pExec->ulExeFormat = EXEFORMAT_OLDDOS;
314 }
315 }
316 }
317 }
318
319 if (fLoadNewHeader)
320 {
321 // either LX or PE or NE:
322 // read in new header...
323 // ulNewHeaderOfs is now either 0 (if no DOS header
324 // was found) or pDosExeHeader->ulNewHeaderOfs
325 // V0.9.12 (2001-05-03) [umoeller]
326
327 // read in the first two bytes to find out
328 // what extended header type we have; note,
329 // PE uses four bytes here
330 CHAR achNewHeaderType[4] = "???";
331 ULONG cbRead = 4;
332
333 if (!(arc = doshReadAt(pFile,
334 ulNewHeaderOfs,
335 &cbRead,
336 achNewHeaderType,
337 DRFL_FAILIFLESS)))
338 {
339 PBYTE pbCheckOS = NULL;
340
341 if (!memcmp(achNewHeaderType, "NE", 2))
342 {
343 // New Executable:
344 pExec->ulExeFormat = EXEFORMAT_NE;
345 cbRead = sizeof(NEHEADER);
346
347 // go read in the complete header then
348 // (doshReadAt has this in the cache)
349 if (!(pExec->pNEHeader = (PNEHEADER)malloc(cbRead)))
350 arc = ERROR_NOT_ENOUGH_MEMORY;
351 else if (!(arc = doshReadAt(pFile,
352 ulNewHeaderOfs,
353 &cbRead,
354 (PBYTE)pExec->pNEHeader,
355 0)))
356 {
357 if (cbRead < sizeof(NEHEADER))
358 arc = ERROR_BAD_EXE_FORMAT;
359 else
360 {
361 pExec->cbNEHeader = cbRead;
362 pbCheckOS = &pExec->pNEHeader->bTargetOS;
363 // set library flag V0.9.16 (2001-12-08) [umoeller]
364 if (pExec->pNEHeader->usFlags & 0x8000)
365 // library:
366 pExec->fLibrary = TRUE;
367 }
368 }
369 }
370 else if ( (!memcmp(achNewHeaderType, "LX", 2))
371 || (!memcmp(achNewHeaderType, "LE", 2))
372 // this is used by SMARTDRV.EXE
373 )
374 {
375 // OS/2 Linear Executable:
376 pExec->ulExeFormat = EXEFORMAT_LX;
377 cbRead = sizeof(LXHEADER);
378
379 // go read in the complete header then
380 // (doshReadAt has this in the cache)
381 if (!(pExec->pLXHeader = (PLXHEADER)malloc(cbRead)))
382 arc = ERROR_NOT_ENOUGH_MEMORY;
383 else if (!(arc = doshReadAt(pFile,
384 ulNewHeaderOfs,
385 &cbRead,
386 (PBYTE)pExec->pLXHeader,
387 0)))
388 {
389 if (cbRead < sizeof(LXHEADER))
390 arc = ERROR_BAD_EXE_FORMAT;
391 else
392 {
393 pExec->cbLXHeader = cbRead;
394 pbCheckOS = (PBYTE)(&pExec->pLXHeader->usTargetOS);
395 // set library flag V0.9.16 (2001-12-08) [umoeller]
396 if (pExec->pLXHeader->ulFlags & 0x8000)
397 // library:
398 pExec->fLibrary = TRUE;
399 }
400 }
401 }
402 else if (!memcmp(achNewHeaderType, "PE\0\0", 4))
403 {
404 pExec->ulExeFormat = EXEFORMAT_PE;
405
406 // PE has a standard header of 24 bytes
407 // plus an extended header, so check
408 // what we've got
409 if (!(pExec->pPEHeader = (PPEHEADER)malloc(sizeof(PEHEADER))))
410 arc = ERROR_NOT_ENOUGH_MEMORY;
411 else
412 {
413 ULONG ulOfs = ulNewHeaderOfs + 4;
414 PPEHEADER pPEHeader = pExec->pPEHeader;
415
416 // null the entire header
417 memset(pExec->pPEHeader,
418 0,
419 sizeof(PEHEADER));
420
421 // copy sig
422 pPEHeader->ulSignature = *((PULONG)&achNewHeaderType);
423
424 // read standard header
425 cbRead = sizeof(IMAGE_FILE_HEADER);
426 if (!(arc = doshReadAt(pFile,
427 ulOfs,
428 &cbRead,
429 (PBYTE)&pPEHeader->FileHeader,
430 0)))
431 {
432 if (cbRead < sizeof(IMAGE_FILE_HEADER))
433 // only if we don't even have the
434 // standard header, return an error
435 arc = ERROR_BAD_EXE_FORMAT;
436 else
437 {
438 pExec->f32Bits = TRUE;
439 pExec->cbPEHeader = 4 + sizeof(PEHEADER); // for now
440
441 if (pPEHeader->FileHeader.fsCharacteristics & IMAGE_FILE_DLL)
442 pExec->fLibrary = TRUE;
443
444 // try extended header
445 ulOfs += sizeof(IMAGE_FILE_HEADER);
446 if ( (cbRead = pPEHeader->FileHeader.usSizeOfOptionalHeader)
447 && (cbRead <= sizeof(IMAGE_OPTIONAL_HEADER))
448 )
449 {
450 if (!(arc = doshReadAt(pFile,
451 ulOfs,
452 &cbRead,
453 (PBYTE)&pPEHeader->OptionalHeader,
454 0)))
455 {
456 if (cbRead != sizeof(IMAGE_OPTIONAL_HEADER))
457 arc = ERROR_BAD_EXE_FORMAT;
458 else switch (pPEHeader->OptionalHeader.usSubsystem)
459 {
460 // case IMAGE_SUBSYSTEM_UNKNOWN: // 0
461 // case IMAGE_SUBSYSTEM_NATIVE: // 1
462 // case IMAGE_SUBSYSTEM_OS2_CUI: // 5
463 // case IMAGE_SUBSYSTEM_POSIX_CUI: // 7
464 // for these we shouldn't set win32
465
466 case IMAGE_SUBSYSTEM_WINDOWS_GUI: // 2 // Windows GUI subsystem
467 pExec->ulOS = EXEOS_WIN32_GUI;
468 break;
469
470 case IMAGE_SUBSYSTEM_WINDOWS_CUI: // 3 // Windows character subsystem
471 pExec->ulOS = EXEOS_WIN32_CLI;
472 break;
473 }
474
475 pExec->cbPEHeader = sizeof(PEHEADER);
476 }
477 }
478 } // end else if (cbRead < sizeof(IMAGE_FILE_HEADER))
479 } // end if (!(arc = doshReadAt(pFile,
480 } // end else if (!(pExec->pPEHeader = (PPEHEADER)malloc(sizeof(PEHEADER))))
481 } // end else if (!memcmp(achNewHeaderType, "PE\0\0", 4))
482 else
483 // strange type:
484 arc = ERROR_INVALID_EXE_SIGNATURE;
485
486 if ((!arc) && (pbCheckOS))
487 {
488 // BYTE to check for operating system
489 // (NE and LX):
490 switch (*pbCheckOS)
491 {
492 case NEOS_OS2:
493 pExec->ulOS = EXEOS_OS2;
494 if (pExec->ulExeFormat == EXEFORMAT_LX)
495 pExec->f32Bits = TRUE;
496 break;
497
498 case NEOS_WIN16:
499 pExec->ulOS = EXEOS_WIN16;
500 break;
501
502 case NEOS_DOS4:
503 pExec->ulOS = EXEOS_DOS4;
504 break;
505
506 case NEOS_WIN386:
507 pExec->ulOS = EXEOS_WIN386;
508 pExec->f32Bits = TRUE;
509 break;
510 }
511 }
512 } // end if (!(arc = doshReadAt(hFile,
513 } // end if (fLoadNewHeader)
514
515 if (arc)
516 // error: clean up
517 exehClose(&pExec);
518 else
519 *ppExec = pExec;
520
521 return arc;
522}
523
524/*
525 *@@ ParseBldLevel:
526 * called from exehQueryBldLevel to parse the BLDLEVEL string.
527 *
528 * On entry, caller has copied the string into pExec->pszDescription.
529 * The string is null-terminated.
530 *
531 * The BLDLEVEL string comes in at least two basic flavors.
532 *
533 * -- The standard format is:
534 *
535 + @#VENDOR:VERSION#@DESCRIPTION
536 *
537 * DESCRIPTION can have leading spaces, but
538 * need to have them.
539 *
540 * -- However, there is an extended version in that the DESCRIPTION field
541 * is split up even more.
542 *
543 * I have seen two subformats for this.
544 *
545 * -- DANIS506.ADD and some IBM programs have a marker that seems
546 * to be that the description starts out with "##1##".
547 *
548 + ##1## DATETIME BUILDMACHINE:ASD:LANG:CTRY:REVISION:UNKNOWN:FIXPAK@@DESCRIPTION
549 *
550 * The problem is that the DATETIME field comes
551 * in several flavors. IBM uses things like
552 *
553 + "Thu Nov 30 15:30:37 2000 BWBLD228"
554 *
555 * while DANIS506.ADD has
556 *
557 + "15.12.2000 18:22:57 Nachtigall"
558 *
559 * Looks like the date/time string is standardized to have 24 characters then.
560 *
561 * -- IBM TCP/IP executables (try INETD.EXE) have something yet different on.
562 * We now try to parse that format as well, even though bldlevel.exe can't
563 * handle it. (Isn't that utility from IBM too?)
564 *
565 * Here's what I get for inetd.exe:
566 *
567 + ##built 09:16:27 Mon Sep 17 2001 -- On AURORA43;0.1@@ TCP/IP for OS/2: INETD
568 *
569 *@@added V0.9.12 (2001-05-18) [umoeller]
570 *@@changed V0.9.12 (2001-05-19) [umoeller]: added extended BLDLEVEL support
571 *@@changed V1.0.0 (2002-08-18) [umoeller]: added support for IBM TCP/IP format
572 *@@changed V1.0.0 (2002-08-18) [umoeller]: fixed DANIS506 format when an extended field had only one character
573 */
574
575STATIC VOID ParseBldLevel(PEXECUTABLE pExec)
576{
577 PCSZ pStartOfVendor,
578 pStartOfInfo,
579 pEndOfVendor;
580
581 // @#VENDOR:VERSION#@ DESCRIPTION
582 if ( (pStartOfVendor = strstr(pExec->pszDescription, "@#"))
583 && (pStartOfInfo = strstr(pStartOfVendor + 2, "#@"))
584 && (pEndOfVendor = strchr(pStartOfVendor + 2, ':'))
585 )
586 {
587 pExec->pszVendor = strhSubstr(pStartOfVendor + 2,
588 pEndOfVendor);
589 pExec->pszVersion = strhSubstr(pEndOfVendor + 1,
590 pStartOfInfo);
591 // skip "@#" in DESCRIPTION string
592 pStartOfInfo += 2;
593
594 // now check if we have extended DESCRIPTION V0.9.12 (2001-05-19) [umoeller]
595 if (strlen(pStartOfInfo) > 6)
596 {
597 if (!memcmp(pStartOfInfo, "##1##", 5))
598 {
599 // DANIS506.ADD format:
600 // "##1## 2.7.2002 19:32:34 Nachtigall::::6::@@..."
601
602 // parse that beast
603 PCSZ p = pStartOfInfo + 5;
604
605 // get build date/time
606 if (strlen(p) > 24)
607 {
608 // skip leading and trailing spaces
609 // V1.0.0 (2002-08-18) [umoeller]
610 PCSZ pStartOfDT = p,
611 pEndOfDT = p + 24;
612 // date/time seems to be fixed 24 chars in length
613
614 while (*pStartOfDT == ' ')
615 ++pStartOfDT;
616
617 while ( (*pEndOfDT == ' ')
618 && (pEndOfDT > pStartOfDT)
619 )
620 --pEndOfDT;
621
622 pExec->pszBuildDateTime = strhSubstr(pStartOfDT, pEndOfDT + 1);
623
624 /* memcpy(pExec->pszBuildDateTime,
625 p,
626 24);
627 pExec->pszBuildDateTime[24] = '\0';
628 */
629
630 // date/time seems to be fixed 24 chars in length
631 p += 24;
632
633 // now we're at the colon-separated
634 // strings, first of which is the build machine;
635 // skip leading spaces
636 while (*p == ' ')
637 p++;
638
639 if (*p)
640 {
641 char **papsz[] =
642 {
643 &pExec->pszBuildMachine,
644 &pExec->pszASD,
645 &pExec->pszLanguage,
646 &pExec->pszCountry,
647 &pExec->pszRevision,
648 &pExec->pszUnknown,
649 &pExec->pszFixpak
650 };
651 ULONG ul;
652
653 for (ul = 0;
654 ul < sizeof(papsz) / sizeof(papsz[0]);
655 ul++)
656 {
657 BOOL fStop = FALSE;
658 PCSZ pNextColon = strchr(p, ':'),
659 pDoubleAt = strstr(p, "@@");
660 if (!pNextColon)
661 {
662 // last item:
663 if (pDoubleAt)
664 pNextColon = pDoubleAt;
665 else
666 pNextColon = p + strlen(p);
667
668 fStop = TRUE;
669 }
670
671 if ( (fStop)
672 || ( (pNextColon)
673 && ( (!pDoubleAt)
674 || (pNextColon < pDoubleAt)
675 )
676 )
677 )
678 {
679 // if (pNextColon > p + 1)
680 // fixed V1.0.0 (2002-08-18) [umoeller]
681 // this failed on fields like "revision"
682 // which only had one character
683 if (pNextColon > p)
684 *(papsz[ul]) = strhSubstr(p, pNextColon);
685 }
686 else
687 break;
688
689 if (fStop)
690 break;
691
692 p = pNextColon + 1;
693 }
694 }
695 }
696
697 if (pStartOfInfo = strstr(p,
698 "@@"))
699 pStartOfInfo += 2;
700 } // end if (!memcmp(pStartOfInfo, "##1##", 5))
701 else if (!memcmp(pStartOfInfo, "##built", 7))
702 {
703 // IBM TCP/IP format:
704 // V1.0.0 (2002-08-18) [umoeller]
705
706 // ##built 09:16:27 Mon Sep 17 2001 -- On AURORA43;0.1@@ TCP/IP for OS/2: INETD
707
708 PCSZ p = pStartOfInfo + 7,
709 p2,
710 p3;
711
712 if (p3 = strchr(p, ';'))
713 {
714 while (*p == ' ')
715 ++p;
716
717 // ##built 09:16:27 Mon Sep 17 2001 -- On AURORA43;0.1@@ TCP/IP for OS/2: INETD
718 // ^ p ^ p3
719 // ^ p2
720
721 if ( (p2 = strstr(p, " -- On "))
722 && (p2 < p3)
723 )
724 {
725 pExec->pszBuildMachine = strhSubstr(p2 + 7, p3);
726 pExec->pszBuildDateTime = strhSubstr(p, p2);
727 }
728 else
729 pExec->pszBuildDateTime = strhSubstr(p, p3);
730
731 p = p3 + 1;
732 }
733
734 if (pStartOfInfo = strstr(p,
735 "@@"))
736 {
737 if (pStartOfInfo > p3)
738 {
739 // p3 points to this "0.1" string; I assume this is
740 // a "revision.fixpak" format since inetver reports
741 // four digits with this revision
742 PCSZ p4;
743 if ( (p4 = strchr(p3, '.'))
744 && (p4 < pStartOfInfo)
745 )
746 {
747 pExec->pszRevision = strhSubstr(p3 + 1, p4);
748 pExec->pszFixpak = strhSubstr(p4 + 1, pStartOfInfo);
749 }
750 else
751 pExec->pszRevision = strhSubstr(p3 + 1, pStartOfInfo);
752 }
753
754 pStartOfInfo += 2;
755 }
756 }
757 }
758
759 // -- if we had no extended DESCRIPTION,
760 // pStartOfInfo points to regular description now
761 // -- if we parse the extended DESCRIPTION above,
762 // pStartOfInfo points to after @@ now
763 // -- if we had an error, pStartOfInfo is NULL
764 if (pStartOfInfo)
765 {
766 // add the regular DESCRIPTION then
767 // skip leading spaces in info string
768 while (*pStartOfInfo == ' ')
769 ++pStartOfInfo;
770
771 if (*pStartOfInfo) // V0.9.9 (2001-04-04) [umoeller]
772 // and copy until end of string
773 pExec->pszInfo = strdup(pStartOfInfo);
774 }
775 }
776}
777
778/*
779 *@@ exehQueryBldLevel:
780 * this retrieves buildlevel information for an
781 * LX or NE executable previously opened with
782 * exehOpen.
783 *
784 * BuildLevel information must be contained in the
785 * DESCRIPTION field of an executable's module
786 * definition (.DEF) file. In order to be readable
787 * by BLDLEVEL.EXE (which ships with OS/2), this
788 * string must have the following format:
789 *
790 + Description '@#AUTHOR:VERSION#@ DESCRIPTION'
791 *
792 * Example:
793 *
794 + Description '@#Ulrich M”ller:0.9.0#@ XWorkplace Sound Support Module'
795 *
796 * The "Description" entry always ends up as the
797 * very first entry in the non-resident name table
798 * in LX and NE executables. So this is what we retrieve
799 * here.
800 *
801 * If the first entry in that table exists, NO_ERROR is
802 * returned and at least the pszDescription field in
803 * EXECUTABLE is set to that information.
804 *
805 * If that string is in IBM BLDLEVEL format, the string
806 * is automatically parsed, and the pszVendor, pszVersion,
807 * and pszInfo fields are also set. In the above examples,
808 * this would return the following information:
809 *
810 + pszVendor = "Ulrich M”ller"
811 + pszVersion = "0.9.0"
812 + pszInfo = "XWorkplace Sound Support Module"
813 *
814 * See ParseBldLevel for extended formats.
815 *
816 * If that string is not in BLDLEVEL format, only
817 * pszDescription will be set. The other fields remain
818 * NULL. Still, NO_ERROR is returned.
819 *
820 * This returns the following errors:
821 *
822 * -- ERROR_INVALID_PARAMETER: pExec is NULL.
823 *
824 * -- ERROR_INVALID_EXE_SIGNATURE (191): pExec is not in
825 * LX or NE format.
826 *
827 * -- ERROR_INVALID_DATA (13): non-resident name table not found,
828 * or table is empty.
829 *
830 * -- ERROR_NOT_ENOUGH_MEMORY: malloc() failed.
831 *
832 * plus the error codes of doshReadAt.
833 *
834 *@@added V0.9.0 [umoeller]
835 *@@changed V0.9.0 (99-10-22) [umoeller]: NE format now supported
836 *@@changed V0.9.1 (99-12-06): fixed memory leak
837 *@@changed V0.9.9 (2001-04-04) [umoeller]: added more error checking
838 *@@changed V0.9.12 (2001-05-18) [umoeller]: extracted ParseBldLevel
839 *@@changed V0.9.16 (2002-01-05) [umoeller]: optimizations
840 */
841
842APIRET exehQueryBldLevel(PEXECUTABLE pExec)
843{
844 APIRET arc = NO_ERROR;
845 PXFILE pFile;
846 ULONG ulNRNTOfs = 0;
847
848 if (!pExec)
849 return ERROR_INVALID_PARAMETER;
850
851 pFile = pExec->pFile;
852 if (pExec->ulExeFormat == EXEFORMAT_LX)
853 {
854 // OK, LX format:
855 // check if we have a non-resident name table
856 if (pExec->pLXHeader == NULL)
857 arc = ERROR_INVALID_DATA;
858 else if (pExec->pLXHeader->ulNonResdNameTblOfs == 0)
859 arc = ERROR_INVALID_DATA;
860 else
861 ulNRNTOfs = pExec->pLXHeader->ulNonResdNameTblOfs;
862 }
863 else if (pExec->ulExeFormat == EXEFORMAT_NE)
864 {
865 // OK, NE format:
866 // check if we have a non-resident name table
867 if (pExec->pNEHeader == NULL)
868 arc = ERROR_INVALID_DATA;
869 else if (pExec->pNEHeader->ulNonResdTblOfs == 0)
870 arc = ERROR_INVALID_DATA;
871 else
872 ulNRNTOfs = pExec->pNEHeader->ulNonResdTblOfs;
873 }
874 else
875 // neither LX nor NE: stop
876 arc = ERROR_INVALID_EXE_SIGNATURE;
877
878 if ( (!arc)
879 && (ulNRNTOfs)
880 )
881 {
882 ULONG cb = 2000;
883
884 PSZ pszNameTable;
885
886 if (!(pszNameTable = (PSZ)malloc(2001)))
887 arc = ERROR_NOT_ENOUGH_MEMORY;
888 else
889 {
890 // V0.9.16 (2002-01-05) [umoeller]: rewrote the following
891
892 // read from offset of non-resident name table
893 if (!(arc = doshReadAt(pFile, // file is still open
894 ulNRNTOfs, // ofs determined above
895 &cb, // 2000
896 pszNameTable,
897 0)))
898 {
899 // the string is in Pascal format, so the
900 // first byte has the length
901 BYTE bLen;
902 if (!(bLen = *pszNameTable))
903 // length byte is null:
904 arc = ERROR_INVALID_DATA;
905 else
906 {
907 // now copy the string
908 if (!(pExec->pszDescription = (PSZ)malloc(bLen + 1)))
909 arc = ERROR_NOT_ENOUGH_MEMORY;
910 else
911 {
912 memcpy(pExec->pszDescription,
913 pszNameTable + 1, // skip length byte
914 bLen); // length byte
915 // terminate string
916 pExec->pszDescription[bLen] = 0;
917
918 ParseBldLevel(pExec);
919 }
920 }
921 }
922
923 free(pszNameTable);
924 }
925 }
926
927 return arc;
928}
929
930/*
931 *@@ exehQueryProgType:
932 * attempts to sets *pulProgType to a PROGTYPE constant.
933 *
934 * Returns:
935 *
936 * -- NO_ERROR
937 *
938 * -- ERROR_INVALID_PARAMETER;
939 *
940 *@@added V1.0.1 (2003-01-17) [umoeller]
941 *@@changed V1.0.1 (2003-01-17) [umoeller]: now correctly returning PROG_PDD/VDD for NE and LX @@fixes 343
942 */
943
944APIRET exehQueryProgType(const EXECUTABLE *pExec,
945 PROGCATEGORY *pulProgType)
946{
947 APIRET arc = NO_ERROR;
948
949 if (!pExec)
950 return ERROR_INVALID_PARAMETER;
951
952 // now we have the PEXECUTABLE:
953 // check what we found
954 switch (pExec->ulOS)
955 {
956 case EXEOS_DOS3:
957 case EXEOS_DOS4:
958 *pulProgType = PROG_WINDOWEDVDM;
959 break;
960
961 case EXEOS_OS2:
962 switch (pExec->ulExeFormat)
963 {
964 case EXEFORMAT_LX:
965 switch (pExec->pLXHeader->ulFlags & E32MODMASK)
966 {
967 case E32MODPDEV:
968 *pulProgType = PROG_PDD;
969 break;
970
971 case E32MODVDEV:
972 *pulProgType = PROG_VDD;
973 break;
974
975 case E32MODDLL:
976 case E32MODPROTDLL:
977 *pulProgType = PROG_DLL;
978 break;
979
980 default:
981 // all bits clear: --> real executable
982 switch (pExec->pLXHeader->ulFlags & E32APPMASK)
983 {
984 case E32PMAPI:
985 // _Pmpf((" LX OS2 PM"));
986 *pulProgType = PROG_PM;
987 break;
988
989 case E32PMW:
990 // _Pmpf((" LX OS2 VIO"));
991 *pulProgType = PROG_WINDOWABLEVIO;
992 break;
993
994 case E32NOPMW:
995 // _Pmpf((" LX OS2 FULLSCREEN"));
996 *pulProgType = PROG_FULLSCREEN;
997 break;
998
999 default:
1000 // _Pmpf((" LX OS2 FULLSCREEN"));
1001 *pulProgType = PROG_FULLSCREEN;
1002 break;
1003 }
1004 break; // executable
1005 }
1006 break;
1007
1008 case EXEFORMAT_NE:
1009 if (pExec->fLibrary)
1010 {
1011 // there is no flag in the NE header for whether
1012 // this is a device driver, so rely on extension
1013 // V1.0.1 (2003-01-17) [umoeller]
1014 PSZ p;
1015 if ( (p = doshGetExtension(pExec->pFile->pszFilename))
1016 && ( (!stricmp(p, "ADD"))
1017 || (!stricmp(p, "DMD"))
1018 || (!stricmp(p, "FLT"))
1019 || (!stricmp(p, "IFS"))
1020 || (!stricmp(p, "SNP"))
1021 || (!stricmp(p, "SYS"))
1022 )
1023 )
1024 *pulProgType = PROG_PDD;
1025 // there can be no 16-bit VDDs, so this must be a PDD
1026 else
1027 *pulProgType = PROG_DLL;
1028 }
1029 else switch (pExec->pNEHeader->usFlags & NEAPPTYP)
1030 {
1031 case NEWINCOMPAT:
1032 // _Pmpf((" NE OS2 VIO"));
1033 *pulProgType = PROG_WINDOWABLEVIO;
1034 break;
1035
1036 case NEWINAPI:
1037 // _Pmpf((" NE OS2 PM"));
1038 *pulProgType = PROG_PM;
1039 break;
1040
1041 case NENOTWINCOMPAT:
1042 default:
1043 // _Pmpf((" NE OS2 FULLSCREEN"));
1044 *pulProgType = PROG_FULLSCREEN;
1045 break;
1046 }
1047 break;
1048
1049 case EXEFORMAT_COM:
1050 *pulProgType = PROG_WINDOWABLEVIO;
1051 break;
1052
1053 default:
1054 arc = ERROR_INVALID_EXE_SIGNATURE;
1055 }
1056 break;
1057
1058 case EXEOS_WIN16:
1059 case EXEOS_WIN386:
1060 // _Pmpf((" WIN16"));
1061 *pulProgType = PROG_31_ENHSEAMLESSCOMMON;
1062 break;
1063
1064 case EXEOS_WIN32_GUI:
1065 case EXEOS_WIN32_CLI:
1066 // _Pmpf((" WIN32"));
1067 *pulProgType = PROG_WIN32;
1068 break;
1069
1070 default:
1071 arc = ERROR_INVALID_EXE_SIGNATURE;
1072 }
1073
1074 return arc;
1075}
1076
1077/*
1078 *@@ PROGTYPESTRING:
1079 *
1080 *@@added V0.9.16 (2002-01-13) [umoeller]
1081 */
1082
1083typedef struct _PROGTYPESTRING
1084{
1085 PROGCATEGORY progc;
1086 PCSZ pcsz;
1087} PROGTYPESTRING, *PPROGTYPESTRING;
1088
1089PROGTYPESTRING G_aProgTypes[] =
1090 {
1091 PROG_DEFAULT, "PROG_DEFAULT",
1092 PROG_FULLSCREEN, "PROG_FULLSCREEN",
1093 PROG_WINDOWABLEVIO, "PROG_WINDOWABLEVIO",
1094 PROG_PM, "PROG_PM",
1095 PROG_GROUP, "PROG_GROUP",
1096 PROG_VDM, "PROG_VDM",
1097 // same as PROG_REAL, "PROG_REAL",
1098 PROG_WINDOWEDVDM, "PROG_WINDOWEDVDM",
1099 PROG_DLL, "PROG_DLL",
1100 PROG_PDD, "PROG_PDD",
1101 PROG_VDD, "PROG_VDD",
1102 PROG_WINDOW_REAL, "PROG_WINDOW_REAL",
1103 PROG_30_STD, "PROG_30_STD",
1104 // same as PROG_WINDOW_PROT, "PROG_WINDOW_PROT",
1105 PROG_WINDOW_AUTO, "PROG_WINDOW_AUTO",
1106 PROG_30_STDSEAMLESSVDM, "PROG_30_STDSEAMLESSVDM",
1107 // same as PROG_SEAMLESSVDM, "PROG_SEAMLESSVDM",
1108 PROG_30_STDSEAMLESSCOMMON, "PROG_30_STDSEAMLESSCOMMON",
1109 // same as PROG_SEAMLESSCOMMON, "PROG_SEAMLESSCOMMON",
1110 PROG_31_STDSEAMLESSVDM, "PROG_31_STDSEAMLESSVDM",
1111 PROG_31_STDSEAMLESSCOMMON, "PROG_31_STDSEAMLESSCOMMON",
1112 PROG_31_ENHSEAMLESSVDM, "PROG_31_ENHSEAMLESSVDM",
1113 PROG_31_ENHSEAMLESSCOMMON, "PROG_31_ENHSEAMLESSCOMMON",
1114 PROG_31_ENH, "PROG_31_ENH",
1115 PROG_31_STD, "PROG_31_STD",
1116
1117// Warp 4 toolkit defines, whatever these were designed for...
1118#ifndef PROG_DOS_GAME
1119 #define PROG_DOS_GAME (PROGCATEGORY)21
1120#endif
1121#ifndef PROG_WIN_GAME
1122 #define PROG_WIN_GAME (PROGCATEGORY)22
1123#endif
1124#ifndef PROG_DOS_MODE
1125 #define PROG_DOS_MODE (PROGCATEGORY)23
1126#endif
1127
1128 PROG_DOS_GAME, "PROG_DOS_GAME",
1129 PROG_WIN_GAME, "PROG_WIN_GAME",
1130 PROG_DOS_MODE, "PROG_DOS_MODE",
1131
1132 // added this V0.9.16 (2001-12-08) [umoeller]
1133 PROG_WIN32, "PROG_WIN32"
1134 };
1135
1136/*
1137 *@@ exehDescribeProgType:
1138 * returns a "PROG_*" string for the given
1139 * program type. Useful for WPProgram setup
1140 * strings and such.
1141 *
1142 *@@added V0.9.16 (2001-10-06)
1143 *@@changed V1.0.1 (2003-01-17) [umoeller]: moved this here from apps.c
1144 */
1145
1146PCSZ exehDescribeProgType(PROGCATEGORY progc) // in: from PROGDETAILS.progc
1147{
1148 ULONG ul;
1149 for (ul = 0;
1150 ul < ARRAYITEMCOUNT(G_aProgTypes);
1151 ul++)
1152 {
1153 if (G_aProgTypes[ul].progc == progc)
1154 return G_aProgTypes[ul].pcsz;
1155 }
1156
1157 return NULL;
1158}
1159
1160/*
1161 *@@ exehQueryImportedModules:
1162 * returns an array of FSYSMODULE structure describing all
1163 * imported modules.
1164 *
1165 * *pcModules receives the # of items in the array (not the
1166 * array size!). Use doshFreeImportedModules to clean up.
1167 *
1168 * This returns a standard OS/2 error code, which might be
1169 * any of the codes returned by DosSetFilePtr and DosRead.
1170 * In addition, this may return:
1171 *
1172 * -- ERROR_NOT_ENOUGH_MEMORY
1173 *
1174 * -- ERROR_INVALID_EXE_SIGNATURE: exe is in a format other
1175 * than LX or NE, which is not understood by this function.
1176 *
1177 * Even if NO_ERROR is returned, the array pointer might still
1178 * be NULL if the module contains no such data.
1179 *
1180 *@@added V0.9.9 (2001-03-11) [lafaix]
1181 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
1182 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
1183 *@@changed V0.9.10 (2001-04-10) [lafaix]: added Win16 and Win386 support
1184 *@@changed V0.9.10 (2001-04-13) [lafaix]: removed 127 characters limit
1185 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
1186 */
1187
1188APIRET exehQueryImportedModules(PEXECUTABLE pExec,
1189 PFSYSMODULE *ppaModules, // out: modules array
1190 PULONG pcModules) // out: array item count
1191{
1192 if ( (pExec)
1193 && ( (pExec->ulOS == EXEOS_OS2)
1194 || (pExec->ulOS == EXEOS_WIN16)
1195 || (pExec->ulOS == EXEOS_WIN386)
1196 )
1197 )
1198 {
1199 ENSURE_BEGIN;
1200 ULONG cModules = 0;
1201 PFSYSMODULE paModules = NULL;
1202 int i;
1203 HFILE hfExe = pExec->pFile->hf;
1204
1205 ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
1206
1207 if (pExec->cbDosExeHeader)
1208 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
1209 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
1210
1211 if (pExec->ulExeFormat == EXEFORMAT_LX)
1212 {
1213 // 32-bit OS/2 executable:
1214 cModules = pExec->pLXHeader->ulImportModTblCnt;
1215
1216 if (cModules)
1217 {
1218 ULONG cb = sizeof(FSYSMODULE) * cModules; // V0.9.9 (2001-04-03) [umoeller]
1219 ULONG ulDummy;
1220
1221 paModules = (PFSYSMODULE)malloc(cb);
1222 if (!paModules)
1223 ENSURE_FAIL(ERROR_NOT_ENOUGH_MEMORY); // V0.9.9 (2001-04-03) [umoeller]
1224
1225 memset(paModules, 0, cb); // V0.9.9 (2001-04-03) [umoeller]
1226
1227 ENSURE_SAFE(DosSetFilePtr(hfExe,
1228 pExec->pLXHeader->ulImportModTblOfs
1229 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
1230 FILE_BEGIN,
1231 &ulDummy));
1232
1233 for (i = 0; i < cModules; i++)
1234 {
1235 BYTE bLen = 0;
1236
1237 // reading the length of the module name
1238 ENSURE_SAFE(DosRead(hfExe, &bLen, 1, &ulDummy));
1239
1240 // reading the module name
1241 ENSURE_SAFE(DosRead(hfExe,
1242 paModules[i].achModuleName,
1243 bLen,
1244 &ulDummy));
1245
1246 // module names are not null terminated, so we must
1247 // do it now
1248 paModules[i].achModuleName[bLen] = 0;
1249 } // end for
1250 }
1251 } // end LX
1252 else if (pExec->ulExeFormat == EXEFORMAT_NE)
1253 {
1254 // 16-bit executable:
1255 cModules = pExec->pNEHeader->usModuleTblEntries;
1256
1257 if (cModules)
1258 {
1259 ULONG cb = sizeof(FSYSMODULE) * cModules;
1260
1261 paModules = (PFSYSMODULE)malloc(cb);
1262 if (!paModules)
1263 ENSURE_FAIL(ERROR_NOT_ENOUGH_MEMORY); // V0.9.9 (2001-04-03) [umoeller]
1264
1265 memset(paModules, 0, cb); // V0.9.9 (2001-04-03) [umoeller]
1266
1267 for (i = 0; i < cModules; i ++)
1268 {
1269 BYTE bLen;
1270 USHORT usOfs;
1271 ULONG ulDummy;
1272
1273 // the module reference table contains offsets
1274 // relative to the import table; we hence read
1275 // the offset in the module reference table, and
1276 // then we read the name in the import table
1277
1278 ENSURE_SAFE(DosSetFilePtr(hfExe,
1279 pExec->pNEHeader->usModRefTblOfs
1280 + ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
1281 + sizeof(usOfs) * i,
1282 FILE_BEGIN,
1283 &ulDummy));
1284
1285 ENSURE_SAFE(DosRead(hfExe, &usOfs, 2, &ulDummy));
1286
1287 ENSURE_SAFE(DosSetFilePtr(hfExe,
1288 pExec->pNEHeader->usImportTblOfs
1289 + ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
1290 + usOfs,
1291 FILE_BEGIN,
1292 &ulDummy));
1293
1294 ENSURE_SAFE(DosRead(hfExe, &bLen, 1, &ulDummy));
1295
1296 ENSURE_SAFE(DosRead(hfExe,
1297 paModules[i].achModuleName,
1298 bLen,
1299 &ulDummy));
1300
1301 paModules[i].achModuleName[bLen] = 0;
1302 } // end for
1303 }
1304 } // end NE
1305 else
1306 ENSURE_FAIL(ERROR_INVALID_EXE_SIGNATURE); // V0.9.9 (2001-04-03) [umoeller]
1307
1308 // no error: output data
1309 *ppaModules = paModules;
1310 *pcModules = cModules;
1311
1312 ENSURE_FINALLY;
1313 // if we had an error above, clean up
1314 free(paModules);
1315 ENSURE_END;
1316 }
1317 else
1318 ENSURE_FAIL(ERROR_INVALID_EXE_SIGNATURE); // V0.9.9 (2001-04-03) [umoeller]
1319
1320 ENSURE_OK;
1321}
1322
1323/*
1324 *@@ exehFreeImportedModules:
1325 * frees resources allocated by exehQueryImportedModules.
1326 *
1327 *@@added V0.9.9 (2001-03-11)
1328 */
1329
1330APIRET exehFreeImportedModules(PFSYSMODULE paModules)
1331{
1332 free(paModules);
1333 return NO_ERROR;
1334}
1335
1336/*
1337 *@@ ScanLXEntryTable:
1338 * returns the number of exported entries in the entry table.
1339 *
1340 * If paFunctions is not NULL, then successive entries are
1341 * filled with the found type and ordinal values.
1342 *
1343 *@@added V0.9.9 (2001-03-30) [lafaix]
1344 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
1345 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
1346 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
1347 */
1348
1349STATIC APIRET ScanLXEntryTable(PEXECUTABLE pExec,
1350 PFSYSFUNCTION paFunctions,
1351 PULONG pcEntries) // out: entry table entry count; ptr can be NULL
1352{
1353 ULONG ulDummy;
1354 USHORT usOrdinal = 1,
1355 usCurrent = 0;
1356 int i;
1357
1358 ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
1359 HFILE hfExe = pExec->pFile->hf;
1360
1361 if (pExec->cbDosExeHeader)
1362 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
1363 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
1364
1365 ENSURE(DosSetFilePtr(hfExe,
1366 pExec->pLXHeader->ulEntryTblOfs
1367 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
1368 FILE_BEGIN,
1369 &ulDummy));
1370
1371 while (TRUE)
1372 {
1373 BYTE bCnt,
1374 bType,
1375 bFlag;
1376
1377 ENSURE(DosRead(hfExe, &bCnt, 1, &ulDummy));
1378
1379 if (bCnt == 0)
1380 // end of the entry table
1381 break;
1382
1383 ENSURE(DosRead(hfExe, &bType, 1, &ulDummy));
1384
1385 switch (bType & 0x7F)
1386 {
1387 /*
1388 * unused entries
1389 *
1390 */
1391
1392 case 0:
1393 usOrdinal += bCnt;
1394 break;
1395
1396 /*
1397 * 16-bit entries
1398 *
1399 * the bundle type is followed by the object number
1400 * and by bCnt bFlag+usOffset entries
1401 *
1402 */
1403
1404 case 1:
1405 ENSURE(DosSetFilePtr(hfExe,
1406 sizeof(USHORT),
1407 FILE_CURRENT,
1408 &ulDummy));
1409
1410 for (i = 0; i < bCnt; i ++)
1411 {
1412 ENSURE(DosRead(hfExe, &bFlag, 1, &ulDummy));
1413
1414 if (bFlag & 0x01)
1415 {
1416 if (paFunctions)
1417 {
1418 paFunctions[usCurrent].ulOrdinal = usOrdinal;
1419 paFunctions[usCurrent].ulType = 1;
1420 paFunctions[usCurrent].achFunctionName[0] = 0;
1421 }
1422 usCurrent++;
1423 }
1424
1425 usOrdinal++;
1426
1427 ENSURE(DosSetFilePtr(hfExe,
1428 sizeof(USHORT),
1429 FILE_CURRENT,
1430 &ulDummy));
1431
1432 } // end for
1433 break;
1434
1435 /*
1436 * 286 call gate entries
1437 *
1438 * the bundle type is followed by the object number
1439 * and by bCnt bFlag+usOffset+usCallGate entries
1440 *
1441 */
1442
1443 case 2:
1444 ENSURE(DosSetFilePtr(hfExe,
1445 sizeof(USHORT),
1446 FILE_CURRENT,
1447 &ulDummy));
1448
1449 for (i = 0; i < bCnt; i ++)
1450 {
1451 ENSURE(DosRead(hfExe, &bFlag, 1, &ulDummy));
1452
1453 if (bFlag & 0x01)
1454 {
1455 if (paFunctions)
1456 {
1457 paFunctions[usCurrent].ulOrdinal = usOrdinal;
1458 paFunctions[usCurrent].ulType = 2;
1459 paFunctions[usCurrent].achFunctionName[0] = 0;
1460 }
1461 usCurrent++;
1462 }
1463
1464 usOrdinal++;
1465
1466 ENSURE(DosSetFilePtr(hfExe,
1467 sizeof(USHORT) + sizeof(USHORT),
1468 FILE_CURRENT,
1469 &ulDummy));
1470
1471 } // end for
1472 break;
1473
1474 /*
1475 * 32-bit entries
1476 *
1477 * the bundle type is followed by the object number
1478 * and by bCnt bFlag+ulOffset entries
1479 *
1480 */
1481
1482 case 3:
1483 ENSURE(DosSetFilePtr(hfExe,
1484 sizeof(USHORT),
1485 FILE_CURRENT,
1486 &ulDummy));
1487
1488 for (i = 0; i < bCnt; i ++)
1489 {
1490 ENSURE(DosRead(hfExe, &bFlag, 1, &ulDummy));
1491
1492 if (bFlag & 0x01)
1493 {
1494 if (paFunctions)
1495 {
1496 paFunctions[usCurrent].ulOrdinal = usOrdinal;
1497 paFunctions[usCurrent].ulType = 3;
1498 paFunctions[usCurrent].achFunctionName[0] = 0;
1499 }
1500 usCurrent++;
1501 }
1502
1503 usOrdinal++;
1504
1505 ENSURE(DosSetFilePtr(hfExe,
1506 sizeof(ULONG),
1507 FILE_CURRENT,
1508 &ulDummy));
1509 } // end for
1510 break;
1511
1512 /*
1513 * forwarder entries
1514 *
1515 * the bundle type is followed by a reserved word
1516 * and by bCnt bFlag+usModOrd+ulOffsOrdNum entries
1517 *
1518 */
1519
1520 case 4:
1521 ENSURE(DosSetFilePtr(hfExe,
1522 sizeof(USHORT),
1523 FILE_CURRENT,
1524 &ulDummy));
1525
1526 for (i = 0; i < bCnt; i ++)
1527 {
1528 ENSURE(DosSetFilePtr(hfExe,
1529 sizeof(BYTE) + sizeof(USHORT) + sizeof(ULONG),
1530 FILE_CURRENT,
1531 &ulDummy));
1532
1533 if (paFunctions)
1534 {
1535 paFunctions[usCurrent].ulOrdinal = usOrdinal;
1536 paFunctions[usCurrent].ulType = 4;
1537 paFunctions[usCurrent].achFunctionName[0] = 0;
1538 }
1539 usCurrent++;
1540
1541 usOrdinal++;
1542 } // end for
1543 break;
1544
1545 /*
1546 * unknown bundle type
1547 *
1548 * we don't know how to handle this bundle, so we must
1549 * stop parsing the entry table here (as we don't know the
1550 * bundle size); if paFunctions is not null, we fill it with
1551 * informative data
1552 */
1553
1554 default:
1555 if (paFunctions)
1556 {
1557 paFunctions[usCurrent].ulOrdinal = usOrdinal;
1558 paFunctions[usCurrent].ulType = bType;
1559 sprintf(paFunctions[usCurrent].achFunctionName,
1560 "Unknown bundle type encountered (%d). Aborting entry table scan.",
1561 bType);
1562
1563 usCurrent++;
1564 }
1565 ENSURE_FAIL(ERROR_INVALID_LIST_FORMAT);
1566 // whatever
1567 // V0.9.9 (2001-04-03) [umoeller]
1568 } // end switch (bType & 0x7F)
1569 } // end while (TRUE)
1570
1571 if (pcEntries)
1572 *pcEntries = usCurrent;
1573
1574 ENSURE_OK;
1575}
1576
1577/*
1578 *@@ ScanNEEntryTable:
1579 * returns the number of exported entries in the entry table.
1580 *
1581 * if paFunctions is not NULL, then successive entries are
1582 * filled with the found type and ordinal values.
1583 *
1584 *@@added V0.9.9 (2001-03-30) [lafaix]
1585 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
1586 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
1587 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
1588 */
1589
1590STATIC APIRET ScanNEEntryTable(PEXECUTABLE pExec,
1591 PFSYSFUNCTION paFunctions,
1592 PULONG pcEntries) // out: entry table entry count; ptr can be NULL
1593{
1594 ULONG ulDummy;
1595 USHORT usOrdinal = 1,
1596 usCurrent = 0;
1597 int i;
1598
1599 ULONG ulNewHeaderOfs = 0;
1600 HFILE hfExe = pExec->pFile->hf;
1601
1602 if (pExec->cbDosExeHeader)
1603 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
1604 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
1605
1606 ENSURE(DosSetFilePtr(hfExe,
1607 pExec->pNEHeader->usEntryTblOfs
1608 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
1609 FILE_BEGIN,
1610 &ulDummy));
1611
1612 while (TRUE)
1613 {
1614 BYTE bCnt,
1615 bType,
1616 bFlag;
1617
1618 ENSURE(DosRead(hfExe, &bCnt, 1, &ulDummy));
1619
1620 if (bCnt == 0)
1621 // end of the entry table
1622 break;
1623
1624 ENSURE(DosRead(hfExe, &bType, 1, &ulDummy));
1625
1626 if (bType)
1627 {
1628 for (i = 0; i < bCnt; i++)
1629 {
1630 ENSURE(DosRead(hfExe,
1631 &bFlag,
1632 1,
1633 &ulDummy));
1634
1635 if (bFlag & 0x01)
1636 {
1637 if (paFunctions)
1638 {
1639 paFunctions[usCurrent].ulOrdinal = usOrdinal;
1640 paFunctions[usCurrent].ulType = 1; // 16-bit entry
1641 paFunctions[usCurrent].achFunctionName[0] = 0;
1642 }
1643 usCurrent++;
1644 }
1645
1646 usOrdinal++;
1647
1648 if (bType == 0xFF)
1649 {
1650 // moveable segment
1651 ENSURE(DosSetFilePtr(hfExe,
1652 5,
1653 FILE_CURRENT,
1654 &ulDummy));
1655 }
1656 else
1657 {
1658 // fixed segment or constant (0xFE)
1659 ENSURE(DosSetFilePtr(hfExe,
1660 2,
1661 FILE_CURRENT,
1662 &ulDummy));
1663 }
1664
1665 } // end for
1666 }
1667 else
1668 usOrdinal += bCnt;
1669 } // end while (TRUE)
1670
1671 if (pcEntries)
1672 *pcEntries = usCurrent;
1673
1674 ENSURE_OK;
1675}
1676
1677/*
1678 *@@ Compare:
1679 * binary search helper
1680 *
1681 *@@added V0.9.9 (2001-04-01) [lafaix]
1682 *@@changed V0.9.9 (2001-04-07) [umoeller]: added _Optlink, or this won't compile as C++
1683 */
1684
1685STATIC int _Optlink Compare(const void *key,
1686 const void *element)
1687{
1688 USHORT usOrdinal = *((PUSHORT) key);
1689 PFSYSFUNCTION pFunction = (PFSYSFUNCTION)element;
1690
1691 if (usOrdinal > pFunction->ulOrdinal)
1692 return 1;
1693 else if (usOrdinal < pFunction->ulOrdinal)
1694 return -1;
1695
1696 return 0;
1697}
1698
1699/*
1700 *@@ ScanNameTable:
1701 * scans a resident or non-resident name table, and fills the
1702 * appropriate paFunctions entries when it encounters exported
1703 * entries names.
1704 *
1705 * This functions works for both NE and LX executables.
1706 *
1707 *@@added V0.9.9 (2001-03-30) [lafaix]
1708 *@@changed V0.9.9 (2001-04-02) [lafaix]: the first entry is special
1709 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
1710 *@@changed V0.9.9 (2001-04-05) [lafaix]: removed the 127 char limit
1711 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
1712 */
1713
1714STATIC APIRET ScanNameTable(PEXECUTABLE pExec,
1715 ULONG cFunctions,
1716 PFSYSFUNCTION paFunctions)
1717{
1718 ULONG ulDummy;
1719
1720 USHORT usOrdinal;
1721 PFSYSFUNCTION pFunction;
1722 HFILE hfExe = pExec->pFile->hf;
1723
1724 while (TRUE)
1725 {
1726 BYTE bLen;
1727 CHAR achName[256];
1728 // int i;
1729
1730 ENSURE(DosRead(hfExe, &bLen, 1, &ulDummy));
1731
1732 if (bLen == 0)
1733 // end of the name table
1734 break;
1735
1736 ENSURE(DosRead(hfExe, &achName, bLen, &ulDummy));
1737 achName[bLen] = 0;
1738
1739 ENSURE(DosRead(hfExe, &usOrdinal, sizeof(USHORT), &ulDummy));
1740
1741 if ((pFunction = (PFSYSFUNCTION)bsearch(&usOrdinal,
1742 paFunctions,
1743 cFunctions,
1744 sizeof(FSYSFUNCTION),
1745 Compare)))
1746 {
1747 memcpy(pFunction->achFunctionName,
1748 achName,
1749 bLen+1);
1750 }
1751 }
1752
1753 ENSURE_OK;
1754}
1755
1756/*
1757 *@@ exehQueryExportedFunctions:
1758 * returns an array of FSYSFUNCTION structure describing all
1759 * exported functions.
1760 *
1761 * *pcFunctions receives the # of items in the array (not the
1762 * array size!). Use doshFreeExportedFunctions to clean up.
1763 *
1764 * Note that the returned array only contains entry for exported
1765 * functions. Empty export entries are _not_ included.
1766 *
1767 * This returns a standard OS/2 error code, which might be
1768 * any of the codes returned by DosSetFilePtr and DosRead.
1769 * In addition, this may return:
1770 *
1771 * -- ERROR_NOT_ENOUGH_MEMORY
1772 *
1773 * -- ERROR_INVALID_EXE_SIGNATURE: exe is in a format other
1774 * than LX or NE, which is not understood by this function.
1775 *
1776 * -- If ERROR_INVALID_LIST_FORMAT is returned, the format of an
1777 * export entry wasn't understood here.
1778 *
1779 * Even if NO_ERROR is returned, the array pointer might still
1780 * be NULL if the module contains no such data.
1781 *
1782 *@@added V0.9.9 (2001-03-11) [lafaix]
1783 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
1784 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
1785 *@@changed V0.9.10 (2001-04-10) [lafaix]: added Win16 and Win386 support
1786 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
1787 */
1788
1789APIRET exehQueryExportedFunctions(PEXECUTABLE pExec,
1790 PFSYSFUNCTION *ppaFunctions, // out: functions array
1791 PULONG pcFunctions) // out: array item count
1792{
1793 if ( (pExec)
1794 && ( (pExec->ulOS == EXEOS_OS2)
1795 || (pExec->ulOS == EXEOS_WIN16)
1796 || (pExec->ulOS == EXEOS_WIN386)
1797 )
1798 )
1799 {
1800 ENSURE_BEGIN;
1801 ULONG cFunctions = 0;
1802 PFSYSFUNCTION paFunctions = NULL;
1803
1804 ULONG ulDummy;
1805
1806 HFILE hfExe = pExec->pFile->hf;
1807 ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
1808
1809 if (pExec->cbDosExeHeader)
1810 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
1811 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
1812
1813 if (pExec->ulExeFormat == EXEFORMAT_LX)
1814 {
1815 // It's a 32bit OS/2 executable
1816
1817 // the number of exported entry points is not stored
1818 // in the executable header; we have to count them in
1819 // the entry table
1820
1821 ENSURE(ScanLXEntryTable(pExec, NULL, &cFunctions));
1822
1823 // we now have the number of exported entries; let us
1824 // build them
1825
1826 if (cFunctions)
1827 {
1828 ULONG cb = sizeof(FSYSFUNCTION) * cFunctions;
1829
1830 paFunctions = (PFSYSFUNCTION)malloc(cb);
1831 if (!paFunctions)
1832 ENSURE_FAIL(ERROR_NOT_ENOUGH_MEMORY);
1833
1834 // we rescan the entry table (the cost is not as bad
1835 // as it may seem, due to disk caching)
1836
1837 ENSURE_SAFE(ScanLXEntryTable(pExec, paFunctions, NULL));
1838
1839 // we now scan the resident name table entries
1840
1841 ENSURE_SAFE(DosSetFilePtr(hfExe,
1842 pExec->pLXHeader->ulResdNameTblOfs
1843 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
1844 FILE_BEGIN,
1845 &ulDummy));
1846
1847 ENSURE_SAFE(ScanNameTable(pExec, cFunctions, paFunctions));
1848
1849 // we now scan the non-resident name table entries,
1850 // whose offset is _from the begining of the file_
1851
1852 ENSURE_SAFE(DosSetFilePtr(hfExe,
1853 pExec->pLXHeader->ulNonResdNameTblOfs,
1854 FILE_BEGIN,
1855 &ulDummy));
1856
1857 ENSURE_SAFE(ScanNameTable(pExec, cFunctions, paFunctions));
1858 } // end if (cFunctions)
1859 }
1860 else if (pExec->ulExeFormat == EXEFORMAT_NE)
1861 {
1862 // it's a "new" segmented 16bit executable
1863
1864 // here too the number of exported entry points
1865 // is not stored in the executable header; we
1866 // have to count them in the entry table
1867
1868 ENSURE(ScanNEEntryTable(pExec, NULL, &cFunctions));
1869
1870 // we now have the number of exported entries; let us
1871 // build them
1872
1873 if (cFunctions)
1874 {
1875 // USHORT usOrdinal = 1;
1876 // usCurrent = 0;
1877
1878 paFunctions = (PFSYSFUNCTION)malloc(sizeof(FSYSFUNCTION) * cFunctions);
1879 if (!paFunctions)
1880 ENSURE_FAIL(ERROR_NOT_ENOUGH_MEMORY);
1881
1882 // we rescan the entry table (the cost is not as bad
1883 // as it may seem, due to disk caching)
1884
1885 ENSURE_SAFE(ScanNEEntryTable(pExec, paFunctions, NULL));
1886
1887 // we now scan the resident name table entries
1888
1889 ENSURE_SAFE(DosSetFilePtr(hfExe,
1890 pExec->pNEHeader->usResdNameTblOfs
1891 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
1892 FILE_BEGIN,
1893 &ulDummy));
1894
1895 ENSURE_SAFE(ScanNameTable(pExec, cFunctions, paFunctions));
1896
1897 // we now scan the non-resident name table entries,
1898 // whose offset is _from the begining of the file_
1899
1900 ENSURE_SAFE(DosSetFilePtr(hfExe,
1901 pExec->pNEHeader->ulNonResdTblOfs,
1902 FILE_BEGIN,
1903 &ulDummy));
1904
1905 ENSURE_SAFE(ScanNameTable(pExec, cFunctions, paFunctions));
1906 }
1907 }
1908 else
1909 ENSURE_FAIL(ERROR_INVALID_EXE_SIGNATURE); // V0.9.9 (2001-04-03) [umoeller]
1910
1911 // no error: output data
1912 *ppaFunctions = paFunctions;
1913 *pcFunctions = cFunctions;
1914
1915 ENSURE_FINALLY;
1916 // if we had an error above, clean up
1917 free(paFunctions);
1918 ENSURE_END;
1919 }
1920 else
1921 ENSURE_FAIL(ERROR_INVALID_EXE_SIGNATURE); // V0.9.9 (2001-04-03) [umoeller]
1922
1923 ENSURE_OK;
1924}
1925
1926/*
1927 *@@ exehFreeExportedFunctions:
1928 * frees resources allocated by exehQueryExportedFunctions.
1929 *
1930 *@@added V0.9.9 (2001-03-11)
1931 */
1932
1933APIRET exehFreeExportedFunctions(PFSYSFUNCTION paFunctions)
1934{
1935 free(paFunctions);
1936
1937 return NO_ERROR;
1938}
1939
1940/*
1941 *@@ exehQueryResources:
1942 * returns an array of FSYSRESOURCE structures describing all
1943 * available resources in the module.
1944 *
1945 * *pcResources receives the no. of items in the array
1946 * (not the array size!). Use exehFreeResources to clean up.
1947 *
1948 * This returns a standard OS/2 error code, which might be
1949 * any of the codes returned by DosSetFilePtr and DosRead.
1950 * In addition, this may return:
1951 *
1952 * -- ERROR_NOT_ENOUGH_MEMORY
1953 *
1954 * -- ERROR_INVALID_EXE_SIGNATURE: exe is in a format other
1955 * than LX or NE, which is not understood by this function.
1956 *
1957 * Even if NO_ERROR is returned, the array pointer might still
1958 * be NULL if the module contains no such data.
1959 *
1960 *@@added V0.9.7 (2000-12-18) [lafaix]
1961 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
1962 *@@changed V0.9.10 (2001-04-10) [lafaix]: added Win16 and Win386 support
1963 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
1964 */
1965
1966APIRET exehQueryResources(PEXECUTABLE pExec, // in: executable from exehOpen
1967 PFSYSRESOURCE *ppaResources, // out: res's array
1968 PULONG pcResources) // out: array item count
1969{
1970 if ( (pExec)
1971 && ( (pExec->ulOS == EXEOS_OS2)
1972 || (pExec->ulOS == EXEOS_WIN16)
1973 || (pExec->ulOS == EXEOS_WIN386)
1974 )
1975 )
1976 {
1977 ENSURE_BEGIN;
1978 ULONG cResources = 0;
1979 PFSYSRESOURCE paResources = NULL;
1980
1981 HFILE hfExe = pExec->pFile->hf;
1982 ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
1983
1984 if (pExec->cbDosExeHeader)
1985 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
1986 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
1987
1988 if (pExec->ulExeFormat == EXEFORMAT_LX)
1989 {
1990 // 32-bit OS/2 executable:
1991 PLXHEADER pLXHeader = pExec->pLXHeader;
1992 if (cResources = pLXHeader->ulResTblCnt)
1993 {
1994 #pragma pack(1) // V0.9.9 (2001-04-02) [umoeller]
1995 struct rsrc32 // Resource Table Entry
1996 {
1997 unsigned short type; // Resource type
1998 unsigned short name; // Resource name
1999 unsigned long cb; // Resource size
2000 unsigned short obj; // Object number
2001 unsigned long offset; // Offset within object
2002 } rs;
2003
2004 struct o32_obj // Flat .EXE object table entry
2005 {
2006 unsigned long o32_size; // Object virtual size
2007 unsigned long o32_base; // Object base virtual address
2008 unsigned long o32_flags; // Attribute flags
2009 unsigned long o32_pagemap; // Object page map index
2010 unsigned long o32_mapsize; // Number of entries in object page map
2011 unsigned long o32_reserved; // Reserved
2012 } ot;
2013 #pragma pack() // V0.9.9 (2001-04-03) [umoeller]
2014
2015 ULONG cb = sizeof(FSYSRESOURCE) * cResources;
2016 ULONG ulDummy;
2017 int i;
2018 ULONG ulCurOfs;
2019
2020 paResources = (PFSYSRESOURCE)malloc(cb);
2021 if (!paResources)
2022 ENSURE_FAIL(ERROR_NOT_ENOUGH_MEMORY);
2023
2024 memset(paResources, 0, cb); // V0.9.9 (2001-04-03) [umoeller]
2025
2026 ENSURE_SAFE(DosSetFilePtr(hfExe,
2027 pLXHeader->ulResTblOfs
2028 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
2029 FILE_BEGIN,
2030 &ulDummy));
2031
2032 for (i = 0; i < cResources; i++)
2033 {
2034 ENSURE_SAFE(DosRead(hfExe, &rs, 14, &ulDummy));
2035
2036 paResources[i].ulID = rs.name;
2037 paResources[i].ulType = rs.type;
2038 paResources[i].ulSize = rs.cb;
2039 paResources[i].ulFlag = rs.obj; // Temp storage for Object
2040 // number. Will be filled
2041 // with resource flag
2042 // later.
2043 }
2044
2045 for (i = 0; i < cResources; i++)
2046 {
2047 ULONG ulOfsThis = pLXHeader->ulObjTblOfs
2048 + ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
2049 + ( sizeof(ot)
2050 * (paResources[i].ulFlag - 1));
2051
2052 ENSURE_SAFE(DosSetFilePtr(hfExe,
2053 ulOfsThis,
2054 FILE_BEGIN,
2055 &ulDummy));
2056
2057 ENSURE_SAFE(DosRead(hfExe, &ot, sizeof(ot), &ulDummy));
2058
2059 paResources[i].ulFlag = ((ot.o32_flags & OBJWRITE)
2060 ? 0
2061 : RNPURE);
2062 paResources[i].ulFlag |= ((ot.o32_flags & OBJDISCARD)
2063 ? 4096
2064 : 0);
2065 paResources[i].ulFlag |= ((ot.o32_flags & OBJSHARED)
2066 ? RNMOVE
2067 : 0);
2068 paResources[i].ulFlag |= ((ot.o32_flags & OBJPRELOAD)
2069 ? RNPRELOAD
2070 : 0);
2071 } // end for
2072 } // end if (cResources)
2073 } // end if (pExec->ulExeFormat == EXEFORMAT_LX)
2074 else if (pExec->ulExeFormat == EXEFORMAT_NE)
2075 {
2076 PNEHEADER pNEHeader = pExec->pNEHeader;
2077
2078 if (pExec->ulOS == EXEOS_OS2)
2079 {
2080 // 16-bit OS/2 executable:
2081 cResources = pNEHeader->usResSegmCount;
2082
2083 if (cResources)
2084 {
2085 #pragma pack(1) // V0.9.9 (2001-04-02) [umoeller]
2086 struct {unsigned short type; unsigned short name;} rti;
2087 struct new_seg // New .EXE segment table entry
2088 {
2089 unsigned short ns_sector; // File sector of start of segment
2090 unsigned short ns_cbseg; // Number of bytes in file
2091 unsigned short ns_flags; // Attribute flags
2092 unsigned short ns_minalloc; // Minimum allocation in bytes
2093 } ns;
2094 #pragma pack()
2095
2096 ULONG cb = sizeof(FSYSRESOURCE) * cResources;
2097 ULONG ulDummy;
2098 int i;
2099
2100 paResources = (PFSYSRESOURCE)malloc(cb);
2101 if (!paResources)
2102 ENSURE_FAIL(ERROR_NOT_ENOUGH_MEMORY);
2103
2104 memset(paResources, 0, cb); // V0.9.9 (2001-04-03) [umoeller]
2105
2106 // we first read the resources IDs and types
2107
2108 ENSURE_SAFE(DosSetFilePtr(hfExe,
2109 pNEHeader->usResTblOfs
2110 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
2111 FILE_BEGIN,
2112 &ulDummy));
2113
2114 for (i = 0; i < cResources; i++)
2115 {
2116 ENSURE_SAFE(DosRead(hfExe, &rti, sizeof(rti), &ulDummy));
2117
2118 paResources[i].ulID = rti.name;
2119 paResources[i].ulType = rti.type;
2120 }
2121
2122 // we then read their sizes and flags
2123
2124 for (i = 0; i < cResources; i++)
2125 {
2126 ENSURE_SAFE(DosSetFilePtr(hfExe,
2127 ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
2128 + pNEHeader->usSegTblOfs
2129 + (sizeof(ns)
2130 * ( pNEHeader->usSegTblEntries
2131 - pNEHeader->usResSegmCount
2132 + i)),
2133 FILE_BEGIN,
2134 &ulDummy));
2135
2136 ENSURE_SAFE(DosRead(hfExe, &ns, sizeof(ns), &ulDummy));
2137
2138 paResources[i].ulSize = ns.ns_cbseg;
2139
2140 paResources[i].ulFlag = (ns.ns_flags & OBJPRELOAD) ? RNPRELOAD : 0;
2141 paResources[i].ulFlag |= (ns.ns_flags & OBJSHARED) ? RNPURE : 0;
2142 paResources[i].ulFlag |= (ns.ns_flags & OBJDISCARD) ? RNMOVE : 0;
2143 paResources[i].ulFlag |= (ns.ns_flags & OBJDISCARD) ? 4096 : 0;
2144 }
2145 } // end if (cResources)
2146 }
2147 else
2148 {
2149 // 16-bit Windows executable
2150 USHORT usAlignShift;
2151 ULONG ulDummy;
2152
2153 ENSURE(DosSetFilePtr(hfExe,
2154 pNEHeader->usResTblOfs
2155 + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
2156 FILE_BEGIN,
2157 &ulDummy));
2158
2159 ENSURE(DosRead(hfExe,
2160 &usAlignShift,
2161 sizeof(usAlignShift),
2162 &ulDummy));
2163
2164 while (TRUE)
2165 {
2166 USHORT usTypeID;
2167 USHORT usCount;
2168
2169 ENSURE(DosRead(hfExe,
2170 &usTypeID,
2171 sizeof(usTypeID),
2172 &ulDummy));
2173
2174 if (usTypeID == 0)
2175 break;
2176
2177 ENSURE(DosRead(hfExe,
2178 &usCount,
2179 sizeof(usCount),
2180 &ulDummy));
2181
2182 ENSURE(DosSetFilePtr(hfExe,
2183 sizeof(ULONG),
2184 FILE_CURRENT,
2185 &ulDummy));
2186
2187 cResources += usCount;
2188
2189 // first pass, skip NAMEINFO table
2190 ENSURE(DosSetFilePtr(hfExe,
2191 usCount*6*sizeof(USHORT),
2192 FILE_CURRENT,
2193 &ulDummy));
2194 }
2195
2196 if (cResources)
2197 {
2198 USHORT usCurrent = 0;
2199 ULONG cb = sizeof(FSYSRESOURCE) * cResources;
2200
2201 paResources = (PFSYSRESOURCE)malloc(cb);
2202 if (!paResources)
2203 ENSURE_FAIL(ERROR_NOT_ENOUGH_MEMORY);
2204
2205 memset(paResources, 0, cb);
2206
2207 ENSURE_SAFE(DosSetFilePtr(hfExe,
2208 pNEHeader->usResTblOfs
2209 + ulNewHeaderOfs,
2210 FILE_BEGIN,
2211 &ulDummy));
2212
2213 ENSURE_SAFE(DosRead(hfExe,
2214 &usAlignShift,
2215 sizeof(usAlignShift),
2216 &ulDummy));
2217
2218 while (TRUE)
2219 {
2220 USHORT usTypeID;
2221 USHORT usCount;
2222 int i;
2223
2224 ENSURE_SAFE(DosRead(hfExe,
2225 &usTypeID,
2226 sizeof(usTypeID),
2227 &ulDummy));
2228
2229 if (usTypeID == 0)
2230 break;
2231
2232 ENSURE_SAFE(DosRead(hfExe,
2233 &usCount,
2234 sizeof(usCount),
2235 &ulDummy));
2236
2237 ENSURE_SAFE(DosSetFilePtr(hfExe,
2238 sizeof(ULONG),
2239 FILE_CURRENT,
2240 &ulDummy));
2241
2242 // second pass, read NAMEINFO table
2243 for (i = 0; i < usCount; i++)
2244 {
2245 USHORT usLength,
2246 usFlags,
2247 usID;
2248
2249 ENSURE_SAFE(DosSetFilePtr(hfExe,
2250 sizeof(USHORT),
2251 FILE_CURRENT,
2252 &ulDummy));
2253
2254 ENSURE_SAFE(DosRead(hfExe,
2255 &usLength,
2256 sizeof(USHORT),
2257 &ulDummy));
2258 ENSURE_SAFE(DosRead(hfExe,
2259 &usFlags,
2260 sizeof(USHORT),
2261 &ulDummy));
2262 ENSURE_SAFE(DosRead(hfExe,
2263 &usID,
2264 sizeof(USHORT),
2265 &ulDummy));
2266
2267 ENSURE_SAFE(DosSetFilePtr(hfExe,
2268 2*sizeof(USHORT),
2269 FILE_CURRENT,
2270 &ulDummy));
2271
2272 // !!! strings ids and types not handled yet
2273 // !!! 15th bit is used to denotes strings
2274 // !!! offsets [lafaix]
2275 paResources[usCurrent].ulType = usTypeID ^ 0x8000;
2276 paResources[usCurrent].ulID = usID ^ 0x8000;
2277 paResources[usCurrent].ulSize = usLength << usAlignShift;
2278 paResources[usCurrent].ulFlag = usFlags & 0x70;
2279
2280 usCurrent++;
2281 }
2282 }
2283 }
2284 }
2285 } // end else if (pExec->ulExeFormat == EXEFORMAT_NE)
2286 else
2287 ENSURE_FAIL(ERROR_INVALID_EXE_SIGNATURE); // V0.9.9 (2001-04-03) [umoeller]
2288
2289 *ppaResources = paResources;
2290 *pcResources = cResources;
2291
2292 ENSURE_FINALLY;
2293 // if we had an error above, clean up
2294 free(paResources);
2295 ENSURE_END;
2296 }
2297 else
2298 ENSURE_FAIL(ERROR_INVALID_EXE_SIGNATURE); // V0.9.9 (2001-04-03) [umoeller]
2299
2300 ENSURE_OK;
2301}
2302
2303/*
2304 *@@ exehFreeResources:
2305 * frees resources allocated by exehQueryResources.
2306 *
2307 *@@added V0.9.7 (2000-12-18) [lafaix]
2308 */
2309
2310APIRET exehFreeResources(PFSYSRESOURCE paResources)
2311{
2312 free(paResources);
2313 return NO_ERROR;
2314}
2315
2316/*
2317 *@@ exehLoadLXMaps:
2318 * loads the three main LX maps into the given
2319 * EXECUTABLE structure.
2320 *
2321 * This loads:
2322 *
2323 * 1) the LX resource table;
2324 *
2325 * 2) the LX object table;
2326 *
2327 * 3) the LX object _page_ table (object map).
2328 *
2329 * Note that this is not automatically called
2330 * by exehOpen to save time, since the LX
2331 * maps are not needed for all the other exe
2332 * functions. However, this does get called
2333 * from exehLoadLXResource if needed.
2334 *
2335 * This returns:
2336 *
2337 * -- NO_ERROR: all three LX maps were loaded,
2338 * and pExec->fLXMapsLoaded was set to TRUE.
2339 *
2340 * -- ERROR_INVALID_PARAMETER
2341 *
2342 * -- ERROR_INVALID_EXE_SIGNATURE: pExec does
2343 * not specify an LX executable.
2344 *
2345 * -- ERROR_NO_DATA: at least one of the structs
2346 * does not exist.
2347 *
2348 * -- ERROR_NOT_ENOUGH_MEMORY
2349 *
2350 * plus the error codes of doshReadAt.
2351 *
2352 * Call exehFreeLXMaps to clean up explicitly, but
2353 * that func automatically gets called by exehClose.
2354 *
2355 *@@added V0.9.16 (2001-12-08) [umoeller]
2356 */
2357
2358APIRET exehLoadLXMaps(PEXECUTABLE pExec)
2359{
2360 APIRET arc;
2361
2362 PLXHEADER pLXHeader;
2363
2364 if (!pExec)
2365 arc = ERROR_INVALID_PARAMETER;
2366 else if (pExec->fLXMapsLoaded)
2367 // already loaded:
2368 arc = NO_ERROR;
2369 else if ( (pExec->ulExeFormat != EXEFORMAT_LX)
2370 || (!(pLXHeader = pExec->pLXHeader))
2371 )
2372 arc = ERROR_INVALID_EXE_SIGNATURE;
2373 else
2374 {
2375 PXFILE pFile = pExec->pFile;
2376 ULONG ulNewHeaderOfs = 0;
2377 ULONG cb;
2378
2379 if (pExec->cbDosExeHeader)
2380 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
2381 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
2382
2383 // resource table
2384 if ( (!(arc = doshAllocArray(pLXHeader->ulResTblCnt,
2385 sizeof(RESOURCETABLEENTRY),
2386 (PBYTE*)&pExec->pRsTbl,
2387 &cb)))
2388 && (!(arc = doshReadAt(pFile,
2389 pLXHeader->ulResTblOfs
2390 + ulNewHeaderOfs,
2391 &cb,
2392 (PBYTE)pExec->pRsTbl,
2393 DRFL_FAILIFLESS)))
2394 )
2395 {
2396 // object table
2397 if ( (!(arc = doshAllocArray(pLXHeader->ulObjCount,
2398 sizeof(OBJECTTABLEENTRY),
2399 (PBYTE*)&pExec->pObjTbl,
2400 &cb)))
2401 && (!(arc = doshReadAt(pFile,
2402 pLXHeader->ulObjTblOfs
2403 + ulNewHeaderOfs,
2404 &cb,
2405 (PBYTE)pExec->pObjTbl,
2406 DRFL_FAILIFLESS)))
2407 )
2408 {
2409 // object page table
2410 if ( (!(arc = doshAllocArray(pLXHeader->ulPageCount,
2411 sizeof(OBJECTPAGETABLEENTRY),
2412 (PBYTE*)&pExec->pObjPageTbl,
2413 &cb)))
2414 && (!(arc = doshReadAt(pFile,
2415 pLXHeader->ulObjPageTblOfs
2416 + ulNewHeaderOfs,
2417 &cb,
2418 (PBYTE)pExec->pObjPageTbl,
2419 DRFL_FAILIFLESS)))
2420 )
2421 {
2422 }
2423 }
2424 }
2425
2426 if (!arc)
2427 pExec->fLXMapsLoaded = TRUE;
2428 else
2429 exehFreeLXMaps(pExec);
2430 }
2431
2432 return arc;
2433}
2434
2435/*
2436 *@@ exehFreeLXMaps:
2437 * frees data allocated by exehLoadLXMaps.
2438 * Gets called automatically by exehClose.
2439 *
2440 *@@added V0.9.16 (2001-12-08) [umoeller]
2441 */
2442
2443VOID exehFreeLXMaps(PEXECUTABLE pExec)
2444{
2445 FREE(pExec->pRsTbl);
2446 FREE(pExec->pObjTbl);
2447 FREE(pExec->pObjPageTbl);
2448 pExec->fLXMapsLoaded = FALSE;
2449}
2450
2451// page flags (OBJECTPAGETABLEENTRY.o32_pageflags)
2452#define VALID 0x0000 // Valid Physical Page in .EXE
2453#define ITERDATA 0x0001 // Iterated Data Page
2454#define INVALID 0x0002 // Invalid Page
2455#define ZEROED 0x0003 // Zero Filled Page
2456#define RANGE 0x0004 // Range of pages
2457#define ITERDATA2 0x0005 // Iterated Data Page Type II
2458
2459/*
2460 *@@ ExpandIterdata1:
2461 * expands a page compressed with the old exepack
2462 * method introduced with OS/2 2.0 (plain /EXEPACK).
2463 *
2464 * Returns either ERROR_BAD_FORMAT or NO_ERROR.
2465 *
2466 * (C) Knut Stange Osmundsen. Used with permission.
2467 *
2468 *@@added V0.9.16 (2001-12-08) [umoeller]
2469 */
2470
2471STATIC APIRET ExpandIterdata1(char *pabTarget, // out: page data (pagesize as in lx spec)
2472 int cbTarget, // in: sizeof *pabTarget (pagesize as in lx spec)
2473 PCSZ pabSource, // in: compressed source data in EXEPACK:1 format
2474 int cbSource) // in: sizeof *pabSource
2475{
2476 PLXITER pIter = (PLXITER)pabSource;
2477 // store the pointer for boundary checking
2478 char *pabTargetOriginal = pabTarget;
2479
2480 // validate size of data
2481 if (cbSource >= cbTarget - 2)
2482 return ERROR_BAD_FORMAT;
2483
2484 // expand the page
2485 while ( (pIter->LX_nIter)
2486 && (cbSource > 0)
2487 )
2488 {
2489 // check if we're out of bound
2490 ULONG nIter = pIter->LX_nIter,
2491 nBytes = pIter->LX_nBytes;
2492
2493 if ( (pabTarget - pabTargetOriginal + nIter * nBytes > cbTarget)
2494 || (cbSource <= 0)
2495 )
2496 return ERROR_BAD_FORMAT;
2497
2498 if (nBytes == 1)
2499 {
2500 // one databyte
2501 memset(pabTarget, pIter->LX_Iterdata, nIter);
2502 pabTarget += nIter;
2503 cbSource -= 4 + 1;
2504 pIter++;
2505 }
2506 else
2507 {
2508 int i;
2509 for (i = nIter;
2510 i > 0;
2511 i--, pabTarget += nBytes)
2512 memcpy(pabTarget, &pIter->LX_Iterdata, nBytes);
2513 cbSource -= 4 + nBytes;
2514 pIter = (PLXITER)((char*)pIter + 4 + nBytes);
2515 }
2516 }
2517
2518 // zero remaining part of the page
2519 if (pabTarget - pabTargetOriginal < cbTarget)
2520 memset(pabTarget, 0, cbTarget - (pabTarget - pabTargetOriginal));
2521
2522 return NO_ERROR;
2523}
2524
2525/*
2526 *@@ memcpyw:
2527 * a special memcpy for expandPage2 which performs a
2528 * word based copy. The difference between this, memmove
2529 * and memcpy is that we'll allways read words.
2530 *
2531 * (C) Knut Stange Osmundsen. Used with permission.
2532 *
2533 *@@added V0.9.16 (2001-12-08) [umoeller]
2534 */
2535
2536STATIC void memcpyw(char *pch1, PCSZ pch2, size_t cch)
2537{
2538 /*
2539 * Use memcpy if possible.
2540 */
2541 if ((pch2 > pch1 ? pch2 - pch1 : pch1 - pch2) >= 4)
2542 {
2543 memcpy(pch1, pch2, cch); /* BUGBUG! ASSUMES that memcpy move NO more than 4 bytes at the time! */
2544 return;
2545 }
2546
2547 /*
2548 * Difference is less than 3 bytes.
2549 */
2550 if (cch & 1)
2551 *pch1++ = *pch2++;
2552
2553 for (cch >>= 1;
2554 cch > 0;
2555 cch--, pch1 += 2, pch2 += 2)
2556 *(PUSHORT)pch1 = *(PUSHORT)pch2;
2557}
2558
2559/*
2560 *@@ memcpyb:
2561 * a special memcpy for expandPage2 which performs a memmove
2562 * operation. The difference between this and memmove is that
2563 * this one works.
2564 *
2565 * (C) Knut Stange Osmundsen. Used with permission.
2566 *
2567 *@@added V0.9.16 (2001-12-08) [umoeller]
2568 */
2569
2570STATIC void memcpyb(char *pch1, PCSZ pch2, size_t cch)
2571{
2572 /*
2573 * Use memcpy if possible.
2574 */
2575 if ((pch2 > pch1 ? pch2 - pch1 : pch1 - pch2) >= 4)
2576 {
2577 memcpy(pch1, pch2, cch);
2578 return;
2579 }
2580
2581 /*
2582 * Difference is less than 3 bytes.
2583 */
2584 while(cch--)
2585 *pch1++ = *pch2++;
2586}
2587
2588/*
2589 *@@ ExpandIterdata2:
2590 * expands a page compressed with the new exepack
2591 * method introduced with OS/2 Warp 3.0 (/EXEPACK:2).
2592 *
2593 * Returns either ERROR_BAD_FORMAT or NO_ERROR.
2594 *
2595 * (C) Knut Stange Osmundsen. Used with permission.
2596 *
2597 * Note that we call special (slow) memcpy versions
2598 * here because the standard memcpy will fail on
2599 * certain bit combinations here for some unknown
2600 * reason.
2601 *
2602 *@@added V0.9.16 (2001-12-08) [umoeller]
2603 */
2604
2605STATIC APIRET ExpandIterdata2(char *pachPage, // out: page data (pagesize as in lx spec)
2606 int cchPage, // in: sizeof *pachPage (pagesize as in lx spec)
2607 PCSZ pachSrcPage, // in: compressed source data in EXEPACK:1 format
2608 int cchSrcPage) // in: size of source buf
2609{
2610 char *pachDestPage = pachPage; /* Store the pointer for boundrary checking. */
2611
2612 while (cchSrcPage > 0)
2613 {
2614 /*
2615 * Bit 0 and 1 is the encoding type.
2616 */
2617
2618 char cSrc = *pachSrcPage;
2619
2620 switch (cSrc & 0x03)
2621 {
2622 /*
2623 *
2624 * 0 1 2 3 4 5 6 7
2625 * type | |
2626 * ----------------
2627 * cch <cch bytes of data>
2628 *
2629 * Bits 2-7 is, if not zero, the length of an uncompressed run
2630 * starting at the following byte.
2631 *
2632 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
2633 * type | | | | | |
2634 * ---------------- ---------------------- -----------------------
2635 * zero cch char to multiply
2636 *
2637 * If the bits are zero, the following two bytes describes a
2638 * 1 byte interation run. First byte is count, second is the byte to copy.
2639 * A count of zero is means end of data, and we simply stops. In that case
2640 * the rest of the data should be zero.
2641 */
2642
2643 case 0:
2644 {
2645 if (cSrc)
2646 {
2647 int cch = cSrc >> 2;
2648 if ( (cchPage >= cch)
2649 && (cchSrcPage >= cch + 1)
2650 )
2651 {
2652 memcpy(pachPage, pachSrcPage + 1, cch);
2653 pachPage += cch, cchPage -= cch;
2654 pachSrcPage += cch + 1, cchSrcPage -= cch + 1;
2655 break; // switch (cSrc & 0x03)
2656 }
2657 return ERROR_BAD_FORMAT;
2658 }
2659
2660 if (cchSrcPage >= 2)
2661 {
2662 int cch;
2663 if (cch = pachSrcPage[1])
2664 {
2665 if ( (cchSrcPage >= 3)
2666 && (cchPage >= cch)
2667 )
2668 {
2669 memset(pachPage, pachSrcPage[2], cch);
2670 pachPage += cch, cchPage -= cch;
2671 pachSrcPage += 3, cchSrcPage -= 3;
2672 break; // switch (cSrc & 0x03)
2673 }
2674 }
2675 else
2676 goto endloop;
2677 }
2678
2679 return ERROR_BAD_FORMAT;
2680 }
2681
2682 /*
2683 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2684 * type | | | | | |
2685 * ---- ------- -------------------------
2686 * cch1 cch2 - 3 offset <cch1 bytes of data>
2687 *
2688 * Two bytes layed out as described above, followed by cch1 bytes of data to be copied.
2689 * The cch2(+3) and offset describes an amount of data to be copied from the expanded
2690 * data relative to the current position. The data copied as you would expect it to be.
2691 */
2692
2693 case 1:
2694 {
2695 if (cchSrcPage >= 2)
2696 {
2697 int off = *(PUSHORT)pachSrcPage >> 7;
2698 int cch1 = cSrc >> 2 & 3;
2699 int cch2 = (cSrc >> 4 & 7) + 3;
2700 pachSrcPage += 2, cchSrcPage -= 2;
2701 if ( (cchSrcPage >= cch1)
2702 && (cchPage >= cch1 + cch2)
2703 && (pachPage + cch1 - off >= pachDestPage)
2704 )
2705 {
2706 memcpy(pachPage, pachSrcPage, cch1);
2707 pachPage += cch1, cchPage -= cch1;
2708 pachSrcPage += cch1, cchSrcPage -= cch1;
2709 memcpyb(pachPage, pachPage - off, cch2); //memmove doesn't do a good job here for some stupid reason.
2710 pachPage += cch2, cchPage -= cch2;
2711 break; // switch (cSrc & 0x03)
2712 }
2713 }
2714 return ERROR_BAD_FORMAT;
2715 }
2716
2717 /*
2718 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
2719 * type | | | |
2720 * ---- ----------------------------------
2721 * cch-3 offset
2722 *
2723 * Two bytes layed out as described above.
2724 * The cch(+3) and offset describes an amount of data to be copied from the expanded
2725 * data relative to the current position.
2726 *
2727 * If offset == 1 the data is not copied as expected, but in the memcpyw manner.
2728 */
2729
2730 case 2:
2731 {
2732 if (cchSrcPage >= 2)
2733 {
2734 int off = *(PUSHORT)pachSrcPage >> 4;
2735 int cch = (cSrc >> 2 & 3) + 3;
2736 pachSrcPage += 2, cchSrcPage -= 2;
2737 if ( (cchPage >= cch)
2738 && (pachPage - off >= pachDestPage)
2739 )
2740 {
2741 memcpyw(pachPage, pachPage - off, cch);
2742 pachPage += cch, cchPage -= cch;
2743 break; // switch (cSrc & 0x03)
2744 }
2745 }
2746 return ERROR_BAD_FORMAT;
2747 }
2748
2749 /*
2750 * 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
2751 * type | | | | | |
2752 * ---------- ---------------- ----------------------------------
2753 * cch1 cch2 offset <cch1 bytes of data>
2754 *
2755 * Three bytes layed out as described above, followed by cch1 bytes of data to be copied.
2756 * The cch2 and offset describes an amount of data to be copied from the expanded
2757 * data relative to the current position.
2758 *
2759 * If offset == 1 the data is not copied as expected, but in the memcpyw manner.
2760 */
2761
2762 case 3:
2763 {
2764 if (cchSrcPage >= 3)
2765 {
2766 int cch1 = cSrc >> 2 & 0x000f;
2767 int cch2 = *(PUSHORT)pachSrcPage >> 6 & 0x003f;
2768 int off = *(PUSHORT)(pachSrcPage + 1) >> 4;
2769 pachSrcPage += 3, cchSrcPage -= 3;
2770 if ( (cchSrcPage >= cch1)
2771 && (cchPage >= cch1 + cch2)
2772 && (pachPage - off + cch1 >= pachDestPage)
2773 )
2774 {
2775 memcpy(pachPage, pachSrcPage, cch1);
2776 pachPage += cch1, cchPage -= cch1;
2777 pachSrcPage += cch1, cchSrcPage -= cch1;
2778 memcpyw(pachPage, pachPage - off, cch2);
2779 pachPage += cch2, cchPage -= cch2;
2780 break; // switch (cSrc & 0x03)
2781 }
2782 }
2783 return ERROR_BAD_FORMAT;
2784 }
2785 } // end switch (cSrc & 0x03)
2786 }
2787
2788endloop:;
2789
2790 /*
2791 * Zero the rest of the page.
2792 */
2793 if (cchPage > 0)
2794 memset(pachPage, 0, cchPage);
2795
2796 return 0;
2797}
2798
2799/*
2800 *@@ GetOfsFromPageTableIndex:
2801 * returns the offset from the executable start
2802 * for the given page table index. This checks
2803 * the given index and returns ERROR_INVALID_SEGMENT_NUMBER
2804 * if it is invalid (for safety).
2805 *
2806 * Otherwise this returns NO_ERROR and sets
2807 * *pulPageOfs, *pulFlags, and *pulSize to
2808 * the respective fields from the page table
2809 * entry automatically.
2810 *
2811 * Note that the caller must manually add the
2812 * page data offset for resources (or whatever
2813 * other offset from the LX header is applicable).
2814 *
2815 *@@added V0.9.16 (2001-12-08) [umoeller]
2816 */
2817
2818STATIC APIRET GetOfsFromPageTableIndex(PEXECUTABLE pExec, // in: executable from exehOpen
2819 ULONG ulObjPageTblIndexThis, // in: object page table index to look for
2820 PULONG pulFlags, // out: page flags
2821 PULONG pulSize, // out: page size
2822 PULONG pulPageOfs) // out: page ofs (add pLXHeader->ulDataPagesOfs to this)
2823{
2824 OBJECTPAGETABLEENTRY *pObjPageTblEntry;
2825
2826 // watch out for out of range, or we'll trap
2827 if (ulObjPageTblIndexThis - 1 >= pExec->pLXHeader->ulPageCount)
2828 {
2829 // _Pmpf(("ulObjPageTblIndexThis %d is too large", ulObjPageTblIndexThis));
2830 return ERROR_INVALID_SEGMENT_NUMBER; // 180
2831 }
2832
2833 pObjPageTblEntry = &pExec->pObjPageTbl[ulObjPageTblIndexThis - 1];
2834
2835 // page offset: shift left by what was specified in LX header
2836 *pulPageOfs = pObjPageTblEntry->o32_pagedataoffset
2837 << pExec->pLXHeader->ulPageLeftShift;
2838 *pulFlags = pObjPageTblEntry->o32_pageflags;
2839 *pulSize = pObjPageTblEntry->o32_pagesize;
2840
2841 return NO_ERROR;
2842}
2843
2844/*
2845 *@@ exehReadLXPage:
2846 * loads and possibly unpacks one LX page.
2847 *
2848 * In order to reduce memory allocations, the
2849 * caller is responsible for allocating a temp
2850 * buffer, which must be passed in with
2851 * pabCompressed.
2852 *
2853 * Returns:
2854 *
2855 * -- NO_ERROR: pbData was filled with data,
2856 * which is pLXHeader->ulPageSize in size.
2857 *
2858 * -- ERROR_INVALID_SEGMENT_NUMBER: segment
2859 * number is out of range.
2860 *
2861 * -- ERROR_BAD_FORMAT: compressed page data
2862 * is screwed somehow, or page size is
2863 * too large.
2864 *
2865 * plus the error codes of doshReadAt.
2866 *
2867 *@@added V0.9.16 (2002-01-05) [umoeller]
2868 */
2869
2870APIRET exehReadLXPage(PEXECUTABLE pExec, // in: executable from exehOpen
2871 ULONG ulObjPageTblIndex, // in: page table index to read
2872 ULONG ulExeOffset, // in: for resources, pLXHeader->ulDataPagesOfs
2873 PBYTE pabCompressed, // in: ptr to temp buffer which must be
2874 // pLXHeader->ulPageSize + 4 bytes in size
2875 PBYTE pbData) // out: ptr to buffer which receives actual
2876 // uncompressed page data (pLXHeader->ulPageSize)
2877{
2878 APIRET arc;
2879 ULONG ulFlags,
2880 ulSize,
2881 ulOffset;
2882
2883 if (!(arc = GetOfsFromPageTableIndex(pExec,
2884 ulObjPageTblIndex,
2885 &ulFlags,
2886 &ulSize,
2887 &ulOffset)))
2888 {
2889 ULONG ulPageSize = pExec->pLXHeader->ulPageSize;
2890
2891 // if the data is not compressed, we read it directly
2892 // into caller's pbData buffer (avoid one memcpy)
2893 // V1.0.2 (2003-11-13) [umoeller]
2894 PBYTE pbTarget =
2895 (ulFlags == VALID) ? pbData : pabCompressed;
2896
2897 ulOffset += ulExeOffset;
2898
2899 /* _Pmpf((" reading pgtbl %d, ofs %d, type %s",
2900 ulObjPageTblIndex,
2901 ulOffset,
2902 (ulFlags == 0x0001) ? "ITERDATA"
2903 : (ulFlags == 0x0005) ? "ITERDATA2"
2904 : "uncompressed")); */
2905
2906 if (ulSize > ulPageSize)
2907 arc = ERROR_BAD_FORMAT;
2908 // go read the page data (might be compressed)
2909 else if (!(arc = doshReadAt(pExec->pFile,
2910 ulOffset,
2911 &ulSize,
2912 pbTarget, // pabCompressed, V1.0.2 (2003-11-13) [umoeller]
2913 0)))
2914 {
2915 // _Pmpf((" %d bytes read", ulSize));
2916
2917 // terminate buffer for decompress
2918 *(PULONG)(pabCompressed + ulSize) = 0;
2919
2920 switch (ulFlags)
2921 {
2922 case ITERDATA:
2923 // OS/2 2.x:
2924 arc = ExpandIterdata1(pbData,
2925 ulPageSize,
2926 pabCompressed,
2927 ulSize); // this page's size
2928 break;
2929
2930 case ITERDATA2:
2931 // Warp 3:
2932 arc = ExpandIterdata2(pbData,
2933 ulPageSize,
2934 pabCompressed,
2935 ulSize); // this page's size
2936 break;
2937
2938 /* V1.0.2 (2003-11-13) [umoeller]
2939 case VALID:
2940 // uncompressed
2941 memcpy(pbData,
2942 pabCompressed,
2943 ulPageSize);
2944 break;
2945 */
2946 }
2947 }
2948 }
2949
2950 return arc;
2951}
2952
2953/*
2954 *@@ exehLoadLXResource:
2955 * attempts to load the data of the resource
2956 * with the specified type and id from an LX
2957 * executable.
2958 *
2959 * If (idResource == 0), the first resource of
2960 * the specified type is loaded. Otherwise we
2961 * try to find the resource of the specified
2962 * type _and_ ID.
2963 *
2964 * If NO_ERROR is returned,
2965 *
2966 * -- *ppbResData receives a new buffer with
2967 * the raw resource data, which the caller
2968 * must free();
2969 *
2970 * -- *pulOffset receives an offset into that
2971 * buffer, where the actual resource data
2972 * starts;
2973 *
2974 * -- *pcbResData receives the size of the
2975 * following resource data (what follows
2976 * after *pulOffset).
2977 *
2978 * The reason for this slightly complicated
2979 * format is to avoid another memcpy since
2980 * resource data need not necessarily be on
2981 * an LX page boundary.
2982 *
2983 * This code will properly unpack compressed
2984 * pages in the executable so the returned
2985 * data is always unpacked and can be used
2986 * directly.
2987 *
2988 * Otherwise this returns:
2989 *
2990 * -- ERROR_INVALID_EXE_SIGNATURE: pExec is not
2991 * LX format.
2992 *
2993 * -- ERROR_NO_DATA: resource not found.
2994 *
2995 * -- ERROR_NOT_ENOUGH_MEMORY
2996 *
2997 * plus the error codes from exehReadLXPage.
2998 *
2999 *@@added V0.9.16 (2001-12-08) [umoeller]
3000 *@@changed V0.9.16 (2002-01-05) [umoeller]: largely rewritten to handle non-first icons properly
3001 */
3002
3003APIRET exehLoadLXResource(PEXECUTABLE pExec, // in: executable from exehOpen
3004 ULONG ulType, // in: RT_* type (e.g. RT_POINTER)
3005 ULONG idResource, // in: resource ID or 0 for first
3006 PBYTE *ppbResData, // out: resource data (to be free()'d)
3007 PULONG pulOffset, // out: offset of actual data in buffer
3008 PULONG pcbResData) // out: size of resource data (ptr can be NULL)
3009{
3010 APIRET arc = NO_ERROR;
3011 ULONG cResources = 0;
3012
3013 ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
3014
3015 PLXHEADER pLXHeader;
3016
3017 *ppbResData = 0;
3018
3019 /* _Pmpf((__FUNCTION__ " %s: ulType = %d, idResource %d",
3020 pExec->pFile->pszFilename,
3021 ulType, idResource)); */
3022
3023 if (!(pLXHeader = pExec->pLXHeader))
3024 return ERROR_INVALID_EXE_SIGNATURE;
3025
3026 if (pExec->cbDosExeHeader)
3027 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
3028 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
3029
3030 if (!(cResources = pLXHeader->ulResTblCnt))
3031 // no resources at all:
3032 return ERROR_NO_DATA;
3033
3034 if (!pExec->fLXMapsLoaded)
3035 arc = exehLoadLXMaps(pExec);
3036
3037 if (!arc)
3038 {
3039 // alright, we're in:
3040
3041 // run thru the resources
3042 PXFILE pFile = pExec->pFile;
3043 BOOL fPtrFound = FALSE;
3044
3045 ULONG i;
3046 for (i = 0;
3047 i < cResources;
3048 i++)
3049 {
3050 // ptr to resource table entry
3051 RESOURCETABLEENTRY *pRsEntry = &pExec->pRsTbl[i];
3052
3053 // check resource type and ID
3054 if ( (pRsEntry->type == ulType)
3055 && ( (idResource == 0)
3056 || (idResource == pRsEntry->name)
3057 )
3058 )
3059 {
3060 // hooray, found it: that was the easy part...
3061 // finding the actual resource data isn't that
3062 // trivial:
3063
3064 // first find the object that the resource
3065 // resides in, but check the bounds
3066 if (pRsEntry->obj - 1 >= pLXHeader->ulObjCount)
3067 {
3068 // _Pmpf(("pRsEntry->obj %d is too large", pRsEntry->obj));
3069 arc = ERROR_INVALID_SEGMENT_NUMBER; // 180
3070 }
3071 else
3072 {
3073 // get the object table entry from the index
3074 OBJECTTABLEENTRY *pObjTblEntry = &pExec->pObjTbl[pRsEntry->obj - 1];
3075
3076 // get the object page table index for the
3077 // first resource entry in this object; to
3078 // this index we will need to add something
3079 // which depends on pRsEntry->offset
3080 ULONG ulObjPageTblIndex = pObjTblEntry->o32_pagemap;
3081
3082 ULONG ulPageSize = pLXHeader->ulPageSize;
3083
3084 // if this resource has specified an
3085 // offset into the object, this offset
3086 // specifies the offset in the uncompressed
3087 // data of the whole object:
3088 // for example:
3089 // res 0 ofs 0 cb 9808
3090 // res 1 ofs 9808 cb 3344
3091 // res 2 ofs 13152 cb ...
3092 // and so on.
3093 // So what we need to do is read in the page
3094 // where the resource offset points into (which
3095 // might be somewhere in the middle):
3096 ULONG ulFirstPage = pRsEntry->offset
3097 / ulPageSize;
3098
3099 // get the offset of the resource data into
3100 // that first page:
3101 ULONG ulResOffsetInFirstPage = pRsEntry->offset % ulPageSize;
3102
3103 ULONG ulLastPage = (pRsEntry->offset + pRsEntry->cb - 1)
3104 / ulPageSize;
3105
3106 // and we need as many pages as the resource occupies:
3107 ULONG cPages = ulLastPage - ulFirstPage + 1;
3108
3109 ULONG cbAlloc = 0;
3110
3111 // now allocate temporary buffers
3112 PBYTE pabCompressed = NULL,
3113 pabUncompressed = NULL;
3114
3115 // 4096 bytes for each page that is read in
3116 // plus 4 extra bytes to terminate for decompression
3117 if (!(pabCompressed = (PBYTE)malloc(ulPageSize + 4)))
3118 arc = ERROR_NOT_ENOUGH_MEMORY;
3119 // 4096 * cPages for the data that is composed from that
3120 else if (!(arc = doshAllocArray(cPages,
3121 ulPageSize,
3122 &pabUncompressed,
3123 &cbAlloc)))
3124 {
3125 // current pointer into pabUncompressed
3126 PBYTE pbCurrent = pabUncompressed;
3127
3128 ULONG ul,
3129 ulPageThis;
3130
3131 /* _Pmpf((" found RT_POINTER %d, size %d, resofs %d",
3132 pRsEntry->name,
3133 pRsEntry->cb,
3134 pRsEntry->offset));
3135 _Pmpf((" ulFirstPage %d, ulResOffsetInFirstPage %d, cPages %d",
3136 ulFirstPage, ulResOffsetInFirstPage, cPages)); */
3137
3138 ulPageThis = ulObjPageTblIndex + ulFirstPage;
3139
3140 // now go for each page:
3141 for (ul = 0;
3142 (ul < cPages) && (!arc);
3143 ul++, ulPageThis++)
3144 {
3145 if (!(arc = exehReadLXPage(pExec,
3146 ulPageThis,
3147 pLXHeader->ulDataPagesOfs,
3148 pabCompressed,
3149 pbCurrent)))
3150 {
3151 // got the data:
3152 // advance target buffer pointer for
3153 // next page
3154 pbCurrent += ulPageSize;
3155
3156 // make sure we don't write too far away
3157 if (pbCurrent > pabUncompressed + cbAlloc)
3158 arc = ERROR_BAD_FORMAT;
3159 }
3160 } // end for
3161
3162 // ok, now we got all the pages that do contain
3163 // data for this resource:
3164
3165 if (!arc)
3166 {
3167 // new code without malloc/memcpy V1.0.2 (2003-11-13) [umoeller]
3168 *ppbResData = pabUncompressed;
3169 *pulOffset = ulResOffsetInFirstPage;
3170
3171
3172 /*
3173 // allocate a new buffer for caller
3174 if (!(*ppbResData = (PBYTE)malloc(pRsEntry->cb)))
3175 arc = ERROR_NOT_ENOUGH_MEMORY;
3176 else
3177 {
3178 // copy into that buffer from the offset
3179 // into the first page and the data from
3180 // the subsequent pages too
3181 memcpy(*ppbResData,
3182 pabUncompressed + ulResOffsetInFirstPage,
3183 pRsEntry->cb);
3184 }
3185 */
3186
3187 if (pcbResData)
3188 *pcbResData = pRsEntry->cb;
3189
3190 fPtrFound = TRUE;
3191 }
3192 else
3193 FREE(pabUncompressed);
3194
3195 FREE(pabCompressed);
3196 }
3197 }
3198 }
3199
3200 if (fPtrFound || arc)
3201 break;
3202
3203 } // end for
3204
3205 if ((!fPtrFound) && (!arc))
3206 arc = ERROR_NO_DATA;
3207 }
3208
3209 // _Pmpf((__FUNCTION__ ": returning %d", arc));
3210
3211 return arc;
3212}
3213
3214/*
3215 *@@ exehLoadOS2NEMaps:
3216 * loads the the two main OS/2 NE maps into the
3217 * given EXECUTABLE structure.
3218 *
3219 * This loads:
3220 *
3221 * 1) the OS/2 NE resource table;
3222 *
3223 * 2) the OS/2 NE segment table.
3224 *
3225 * Note that this is not automatically called
3226 * by exehOpen to save time, since the NE
3227 * maps are not needed for all the other exe
3228 * functions. However, this does get called
3229 * from exehLoadOS2NEResource if needed.
3230 *
3231 * This returns:
3232 *
3233 * -- NO_ERROR: both maps were loaded,
3234 * and pExec->fOS2NEMapsLoaded was set to TRUE.
3235 *
3236 * -- ERROR_INVALID_PARAMETER
3237 *
3238 * -- ERROR_INVALID_EXE_SIGNATURE: pExec does
3239 * not specify an NE executable, or the OS
3240 * flag is != OS/2. This func does not work
3241 * for Win16 executables.
3242 *
3243 * -- ERROR_NO_DATA: at least one of the structs
3244 * does not exist.
3245 *
3246 * -- ERROR_NOT_ENOUGH_MEMORY
3247 *
3248 * plus the error codes of doshReadAt.
3249 *
3250 * Call exehFreeNEMaps to clean up explicitly, but
3251 * that func automatically gets called by exehClose.
3252 *
3253 *@@added V0.9.16 (2001-12-08) [umoeller]
3254 */
3255
3256APIRET exehLoadOS2NEMaps(PEXECUTABLE pExec)
3257{
3258 APIRET arc;
3259
3260 PNEHEADER pNEHeader;
3261
3262 if (!pExec)
3263 arc = ERROR_INVALID_PARAMETER;
3264 else if (pExec->fOS2NEMapsLoaded)
3265 // already loaded:
3266 arc = NO_ERROR;
3267 else if ( (pExec->ulExeFormat != EXEFORMAT_NE)
3268 || (pExec->ulOS != EXEOS_OS2)
3269 || (!(pNEHeader = pExec->pNEHeader))
3270 )
3271 arc = ERROR_INVALID_EXE_SIGNATURE;
3272 else
3273 {
3274 PXFILE pFile = pExec->pFile;
3275 ULONG ulNewHeaderOfs = 0;
3276 ULONG cb;
3277
3278 if (pExec->cbDosExeHeader)
3279 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
3280 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
3281
3282 // resource table
3283 if ( (!(arc = doshAllocArray(pNEHeader->usResSegmCount,
3284 sizeof(OS2NERESTBLENTRY),
3285 (PBYTE*)&pExec->paOS2NEResTblEntry,
3286 &cb)))
3287 && (!(arc = doshReadAt(pFile,
3288 pNEHeader->usResTblOfs
3289 + ulNewHeaderOfs,
3290 &cb,
3291 (PBYTE)pExec->paOS2NEResTblEntry,
3292 DRFL_FAILIFLESS)))
3293 )
3294 {
3295 // resource segments
3296 if ( (!(arc = doshAllocArray(pNEHeader->usResSegmCount,
3297 sizeof(OS2NESEGMENT),
3298 (PBYTE*)&pExec->paOS2NESegments,
3299 &cb)))
3300 && (!(arc = doshReadAt(pFile,
3301 pNEHeader->usResTblOfs
3302 + ulNewHeaderOfs
3303 - cb,
3304 &cb,
3305 (PBYTE)pExec->paOS2NESegments,
3306 DRFL_FAILIFLESS)))
3307 )
3308 {
3309 }
3310 }
3311
3312 if (!arc)
3313 pExec->fOS2NEMapsLoaded = TRUE;
3314 else
3315 exehFreeNEMaps(pExec);
3316 }
3317
3318 return arc;
3319}
3320
3321/*
3322 *@@ exehFreeNEMaps:
3323 * frees data allocated by exehLoadOS2NEMaps.
3324 * Gets called automatically by exehClose.
3325 *
3326 *@@added V0.9.16 (2001-12-08) [umoeller]
3327 */
3328
3329VOID exehFreeNEMaps(PEXECUTABLE pExec)
3330{
3331 FREE(pExec->paOS2NEResTblEntry);
3332 FREE(pExec->paOS2NESegments);
3333 pExec->fOS2NEMapsLoaded = FALSE;
3334}
3335
3336/*
3337 *@@ exehLoadOS2NEResource:
3338 * attempts to load the data of the resource
3339 * with the specified type and id from an OS/2
3340 * NE executable.
3341 *
3342 * Note that NE executables with resources in
3343 * OS/2 format are very, very rare. The
3344 * only OS/2 NE executables with resources I
3345 * could find at this point were in an old 1.3
3346 * Toolkit, but with them, this code works.
3347 *
3348 * If (idResource == 0), the first resource of
3349 * the specified type is loaded. Otherwise we
3350 * try to find the resource of the specified
3351 * type _and_ ID.
3352 *
3353 * If NO_ERROR is returned, *ppbResData receives
3354 * a new buffer with the raw resource data, and
3355 * *pcbResData receives the size of that buffer.
3356 * The caller must then free() that buffer.
3357 *
3358 * Since NE doesn't support packing, the data is
3359 * unpacked always and can be used directly.
3360 *
3361 * Otherwise this returns:
3362 *
3363 * -- ERROR_INVALID_EXE_SIGNATURE: pExec is not
3364 * NE or not OS/2. This func does not work
3365 * for Win16 executables.
3366 *
3367 * -- ERROR_NO_DATA: resource not found.
3368 *
3369 * -- ERROR_BAD_FORMAT: cannot handle resource format.
3370 *
3371 *@@added V0.9.16 (2001-12-08) [umoeller]
3372 */
3373
3374APIRET exehLoadOS2NEResource(PEXECUTABLE pExec, // in: executable from exehOpen
3375 ULONG ulType, // in: RT_* type (e.g. RT_POINTER)
3376 ULONG idResource, // in: resource ID or 0 for first
3377 PBYTE *ppbResData, // out: resource data (to be free()'d)
3378 PULONG pcbResData) // out: size of resource data (ptr can be NULL)
3379{
3380 APIRET arc = NO_ERROR;
3381 ULONG cResources = 0;
3382
3383 ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
3384
3385 PNEHEADER pNEHeader;
3386
3387 if (!(pNEHeader = pExec->pNEHeader))
3388 return ERROR_INVALID_EXE_SIGNATURE;
3389
3390 if (pExec->cbDosExeHeader)
3391 // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
3392 ulNewHeaderOfs = pExec->DosExeHeader.ulNewHeaderOfs;
3393
3394 // _Pmpf((__FUNCTION__ ": entering, checking %d resources", pNEHeader->usResSegmCount));
3395
3396 if (!(cResources = pNEHeader->usResSegmCount))
3397 // no resources at all:
3398 return ERROR_NO_DATA;
3399
3400 if (!pExec->fOS2NEMapsLoaded)
3401 arc = exehLoadOS2NEMaps(pExec);
3402
3403 if (!arc)
3404 {
3405 // alright, we're in:
3406 PXFILE pFile = pExec->pFile;
3407
3408 // run thru the resources
3409 BOOL fPtrFound = FALSE;
3410
3411 ULONG i;
3412 POS2NERESTBLENTRY pResTblEntryThis = pExec->paOS2NEResTblEntry;
3413 POS2NESEGMENT pSegThis = pExec->paOS2NESegments;
3414 for (i = 0;
3415 i < cResources;
3416 i++, pResTblEntryThis++, pSegThis++)
3417 {
3418 // check resource type and ID
3419 if ( (pResTblEntryThis->usType == ulType)
3420 && ( (idResource == 0)
3421 || (idResource == pResTblEntryThis->usID)
3422 )
3423 )
3424 {
3425 // hooray, we found the resource...
3426
3427 // look up the corresponding segment
3428
3429 ULONG ulOffset = ( (ULONG)pSegThis->ns_sector
3430 << pNEHeader->usLogicalSectShift
3431 );
3432
3433 ULONG cb = pSegThis->ns_cbseg; // resource size
3434 PBYTE pb;
3435 if (!(*ppbResData = (PBYTE)malloc(cb)))
3436 arc = ERROR_NOT_ENOUGH_MEMORY;
3437 else
3438 {
3439 if (!(arc = doshReadAt(pFile,
3440 ulOffset,
3441 &cb,
3442 *ppbResData,
3443 DRFL_FAILIFLESS)))
3444 {
3445 if (pcbResData)
3446 *pcbResData = cb;
3447 fPtrFound = TRUE;
3448 }
3449 else
3450 // error reading:
3451 free(*ppbResData);
3452 }
3453 }
3454
3455 if (fPtrFound || arc)
3456 break;
3457
3458 } // end for
3459
3460 if ((!fPtrFound) && (!arc))
3461 arc = ERROR_NO_DATA;
3462 }
3463 // else
3464 // _Pmpf(("exehLoadOS2NEMaps returned %d"));
3465
3466 return arc;
3467}
3468
3469/*
3470 *@@ exehClose:
3471 * this closes an executable opened with exehOpen.
3472 * Always call this function if NO_ERROR was returned by
3473 * exehOpen.
3474 *
3475 * This automaticall calls exehFreeLXMaps and
3476 * exehFreeNEMaps.
3477 *
3478 *@@added V0.9.0 [umoeller]
3479 *@@changed V0.9.16 (2001-12-08) [umoeller]: fixed memory leaks
3480 *@@changed V0.9.16 (2001-12-08) [umoeller]: changed prototype to null the pExec ptr
3481 */
3482
3483APIRET exehClose(PEXECUTABLE *ppExec)
3484{
3485 APIRET arc = NO_ERROR;
3486 PEXECUTABLE pExec;
3487 if ( (ppExec)
3488 && (pExec = *ppExec)
3489 )
3490 {
3491 char **papsz[] =
3492 {
3493 // (char**)&pExec->pDosExeHeader,
3494 (char**)&pExec->pNEHeader,
3495 (char**)&pExec->pLXHeader,
3496 (char**)&pExec->pPEHeader,
3497
3498 &pExec->pszDescription,
3499 &pExec->pszVendor,
3500 &pExec->pszVersion,
3501 &pExec->pszInfo,
3502
3503 &pExec->pszBuildDateTime,
3504 &pExec->pszBuildMachine,
3505 &pExec->pszASD,
3506 &pExec->pszLanguage,
3507 &pExec->pszCountry,
3508 &pExec->pszRevision,
3509 &pExec->pszUnknown,
3510 &pExec->pszFixpak
3511 };
3512 ULONG ul;
3513
3514 exehFreeLXMaps(pExec);
3515 exehFreeNEMaps(pExec);
3516
3517 // fixed the memory leaks with the missing fields,
3518 // turned this into a loop
3519 for (ul = 0;
3520 ul < sizeof(papsz) / sizeof(papsz[0]);
3521 ul++)
3522 {
3523 PSZ pThis;
3524 if (pThis = *papsz[ul])
3525 {
3526 free(pThis);
3527 *papsz[ul] = NULL;
3528 }
3529 }
3530
3531 doshClose(&pExec->pFile);
3532
3533 free(pExec);
3534 *ppExec = NULL;
3535 }
3536 else
3537 arc = ERROR_INVALID_PARAMETER;
3538
3539 return arc;
3540}
3541
3542
Note: See TracBrowser for help on using the repository browser.