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

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

Coupla fixes.

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