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

Last change on this file since 257 was 257, checked in by umoeller, 22 years ago

Fixes that have piled up in the last three months.

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