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

Last change on this file since 130 was 130, checked in by umoeller, 24 years ago

Executable updates, mostly.

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