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