Ignore:
Timestamp:
Dec 17, 2001, 12:21:18 AM (24 years ago)
Author:
umoeller
Message:

Lots of changes for icons and refresh.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/dosh2.c

    r123 r124  
    392392 *      -- ERROR_NOT_ENOUGH_MEMORY: malloc() failed.
    393393 *
    394  *      -- ERROR_INVALID_EXE_SIGNATURE: unknown
    395  *          executable type... the given file probably
    396  *          isn't even an executable.
     394 *      -- ERROR_INVALID_EXE_SIGNATURE (191): header is
     395 *              neither plain DOS, nor NE, nor LX, nor PE.
     396 *              The given file probably isn't even an
     397 *              executable. This you will get if you
     398 *              pass in COM, BAT, or CMD files.
     399 *
     400 *      -- ERROR_BAD_EXE_FORMAT (193): header was
     401 *              recognized, but the header data was
     402 *              not understood.
    397403 *
    398404 *      -- ERROR_INVALID_PARAMETER: ppExec is NULL.
     
    419425 *      I am not sure whether PE supports such things
    420426 *      as well... if so, it should be supported too.
     427 *
     428 *      @@todo:
     429 *
     430 *          win95 \WINDOWS\extract.exe is NE with a non-standard format
     431 *          win16 \WINDOWS\EXPAND.EXE
     432 *          win16 \WINDOWS\MSD.EXE"
    421433 *
    422434 *@@added V0.9.0 [umoeller]
     
    428440 *@@changed V0.9.16 (2001-12-08) [umoeller]: now using OPEN_SHARE_DENYWRITE
    429441 *@@changed V0.9.16 (2001-12-08) [umoeller]: fLibrary was never set, works for LX and NE now
     442 *@@changed V0.9.16 (2001-12-08) [umoeller]: some speed optimizations, changed some return codes
    430443 */
    431444
     
    496509                    // remove the DOS header info, since we have none
    497510                    // V0.9.12 (2001-05-03) [umoeller]
    498                     free (pExec->pDosExeHeader);
    499                     pExec->pDosExeHeader = 0;
     511                    FREE(pExec->pDosExeHeader);
    500512                }
    501513                else
     
    523535                    // was found) or pDosExeHeader->ulNewHeaderOfs
    524536                    // V0.9.12 (2001-05-03) [umoeller]
    525                     CHAR    achNewHeaderType[2] = "";
    526537                    ULONG   cbRead;
    527 
    528                     cbRead = sizeof(achNewHeaderType);
    529                     if (!(arc = doshReadAt(hFile,
    530                                            ulNewHeaderOfs,
    531                                            FILE_BEGIN,
    532                                            &cbRead,
    533                                            achNewHeaderType)))
     538                    PBYTE   pbHeader;
     539
     540                    // now, we used to read in the first two chars
     541                    // to check out if we have PE or LX or NE and
     542                    // then read the header accordingly... but
     543                    // that wasn't terribly efficient. So load
     544                    // a chunk of data and then do a realloc()
     545                    // instead.
     546
     547                    // take the largest of LXHEADER and NEHEADER and PEHEADER
     548                    cbRead = sizeof(LXHEADER);
     549                    if (sizeof(NEHEADER) > cbRead)
     550                        cbRead = sizeof(NEHEADER);
     551                    if (sizeof(PEHEADER) > cbRead)
     552                        cbRead = sizeof(PEHEADER);
     553
     554                    if (!(pbHeader = malloc(cbRead)))
     555                        arc = ERROR_NOT_ENOUGH_MEMORY;
     556                    else if (!(arc = doshReadAt(hFile,
     557                                                ulNewHeaderOfs,
     558                                                FILE_BEGIN,
     559                                                &cbRead,
     560                                                pbHeader)))
    534561                    {
    535562                        PBYTE   pbCheckOS = NULL;
    536563
    537                         // reset file ptr
    538                         /* DosSetFilePtr(hFile,
    539                                       ulNewHeaderOfs,
    540                                       FILE_BEGIN,
    541                                       &ulLocal);
    542                            */
     564                        PSZ     achNewHeaderType = (PSZ)pbHeader;
    543565
    544566                        if (!memcmp(achNewHeaderType, "NE", 2))
     
    546568                            // New Executable:
    547569                            pExec->ulExeFormat = EXEFORMAT_NE;
    548                             // allocate NE header
    549                             if (pExec->pNEHeader = (PNEHEADER)malloc(sizeof(NEHEADER)))
     570
     571                            if (cbRead < sizeof(NEHEADER))
    550572                            {
    551                                 // read in NE header
    552                                 pExec->cbNEHeader = sizeof(NEHEADER);
    553                                 if (!(arc = doshReadAt(hFile,
    554                                                        ulNewHeaderOfs,
    555                                                        FILE_BEGIN,
    556                                                        &pExec->cbNEHeader,
    557                                                        (PBYTE)pExec->pNEHeader)))
    558                                 {
    559                                     if (pExec->cbNEHeader == sizeof(NEHEADER))
    560                                     {
    561                                         pbCheckOS = &pExec->pNEHeader->bTargetOS;
    562                                         // set library flag V0.9.16 (2001-12-08) [umoeller]
    563                                         if (pExec->pNEHeader->usFlags & 0x8000)
    564                                             // library:
    565                                             pExec->fLibrary = TRUE;
    566                                     }
    567                                     else
    568                                         // V0.9.16 (2001-12-08) [umoeller]
    569                                         arc = ERROR_INVALID_EXE_SIGNATURE;
    570                                 }
     573                                arc = ERROR_BAD_EXE_FORMAT;
     574                                FREE(pbHeader);
    571575                            }
    572576                            else
    573                                 arc = ERROR_NOT_ENOUGH_MEMORY;
     577                            {
     578                                pExec->pNEHeader = (PNEHEADER)realloc(pbHeader,
     579                                                                      sizeof(NEHEADER));
     580                                pExec->cbNEHeader = sizeof(NEHEADER);
     581                                pbCheckOS = &pExec->pNEHeader->bTargetOS;
     582                                // set library flag V0.9.16 (2001-12-08) [umoeller]
     583                                if (pExec->pNEHeader->usFlags & 0x8000)
     584                                    // library:
     585                                    pExec->fLibrary = TRUE;
     586                            }
    574587                        }
    575                         else if (   (memcmp(achNewHeaderType, "LX", 2) == 0)
    576                                  || (memcmp(achNewHeaderType, "LE", 2) == 0)
     588                        else if (    (!memcmp(achNewHeaderType, "LX", 2))
     589                                  || (!memcmp(achNewHeaderType, "LE", 2))
    577590                                            // this is used by SMARTDRV.EXE
    578591                                )
     
    580593                            // OS/2 Linear Executable:
    581594                            pExec->ulExeFormat = EXEFORMAT_LX;
    582                             // allocate LX header
    583                             if (pExec->pLXHeader = (PLXHEADER)malloc(sizeof(LXHEADER)))
     595
     596                            if (cbRead < sizeof(LXHEADER))
    584597                            {
     598                                arc = ERROR_BAD_EXE_FORMAT;
     599                                FREE(pbHeader);
     600                            }
     601                            else
     602                            {
     603                                pExec->pLXHeader = (PLXHEADER)realloc(pbHeader,
     604                                                                      sizeof(LXHEADER));
    585605                                // read in LX header
    586606                                pExec->cbLXHeader = sizeof(LXHEADER);
    587                                 if (!(arc = doshReadAt(hFile,
    588                                                        ulNewHeaderOfs,
    589                                                        FILE_BEGIN,
    590                                                        &pExec->cbLXHeader,
    591                                                        (PBYTE)pExec->pLXHeader)))
     607                                pbCheckOS = (PBYTE)(&pExec->pLXHeader->usTargetOS);
     608                                // set library flag V0.9.16 (2001-12-08) [umoeller]
     609                                if (pExec->pLXHeader->ulFlags & 0x8000)
     610                                    // library:
     611                                    pExec->fLibrary = TRUE;
     612                            }
     613                        }
     614                        else if (!memcmp(achNewHeaderType, "PE", 2))
     615                        {
     616                            pExec->ulExeFormat = EXEFORMAT_PE;
     617
     618                            if (cbRead < sizeof(PEHEADER))
     619                            {
     620                                arc = ERROR_BAD_EXE_FORMAT;
     621                                FREE(pbHeader);
     622                            }
     623                            else
     624                            {
     625                                // PE has a standard header of 24 bytes
     626                                // plus an extended header, so check what
     627                                // we've got
     628                                ULONG cbPE =   24
     629                                             + ((PPEHEADER)pbHeader)->usHeaderSize;
     630                                pExec->pPEHeader = (PPEHEADER)realloc(pbHeader,
     631                                                                      cbPE);
     632
     633                                pExec->cbPEHeader = cbPE;
     634                                pExec->ulOS = EXEOS_WIN32;
     635                                pExec->f32Bits = TRUE;
     636
     637                                // we have the first 24 bytes already, so
     638                                // go for the next chunk
     639                                if (cbRead = pExec->pPEHeader->usHeaderSize)
    592640                                {
    593                                     if (pExec->cbLXHeader == sizeof(LXHEADER))
     641                                    if (!(arc = doshReadAt(hFile,
     642                                                           ulNewHeaderOfs + 24,
     643                                                           FILE_BEGIN,
     644                                                           &cbRead,
     645                                                           (PBYTE)pExec->pPEHeader + 24)))
    594646                                    {
    595                                         pbCheckOS = (PBYTE)(&pExec->pLXHeader->usTargetOS);
    596                                         // set library flag V0.9.16 (2001-12-08) [umoeller]
    597                                         if (pExec->pLXHeader->ulFlags & 0x8000)
    598                                             // library:
    599                                             pExec->fLibrary = TRUE;
    600647                                    }
    601648                                    else
    602                                         // V0.9.16 (2001-12-08) [umoeller]
    603                                         arc = ERROR_INVALID_EXE_SIGNATURE;
     649                                        arc = ERROR_BAD_EXE_FORMAT;
    604650                                }
    605                             }
    606                             else
    607                                 arc = ERROR_NOT_ENOUGH_MEMORY;
    608                         }
    609                         else if (memcmp(achNewHeaderType, "PE", 2) == 0)
    610                         {
    611                             PEHEADER PEHeader = {0};
    612 
    613                             pExec->ulExeFormat = EXEFORMAT_PE;
    614                             pExec->ulOS = EXEOS_WIN32;
    615                             pExec->f32Bits = TRUE;
    616 
    617                             // V0.9.10 (2001-04-08) [lafaix]
    618                             // read in standard PE header
    619                             cbRead = 24;
    620                             if (!(arc = doshReadAt(hFile,
    621                                                    ulNewHeaderOfs,
    622                                                    FILE_BEGIN,
    623                                                    &cbRead,
    624                                                    (PBYTE)&PEHeader)))
    625                             {
    626                                 // allocate PE header
    627                                 if (pExec->pPEHeader = (PPEHEADER)malloc(24 + PEHeader.usHeaderSize))
    628                                 {
    629                                     // copy standard PE header
    630                                     memcpy(pExec->pPEHeader,
    631                                            &PEHeader,
    632                                            24);
    633 
    634                                     // read in optional PE header
    635                                     if (!(arc = DosRead(hFile,
    636                                                         &pExec->pPEHeader->usReserved3,
    637                                                         PEHeader.usHeaderSize,
    638                                                         &pExec->cbPEHeader)))
    639                                         pExec->cbPEHeader += 24;
    640                                 }
    641                                 else
    642                                     arc = ERROR_NOT_ENOUGH_MEMORY;
    643651                            }
    644652                        }
     
    648656
    649657                        if (pbCheckOS)
     658                        {
    650659                            // BYTE to check for operating system
    651660                            // (NE and LX):
     
    671680                                break;
    672681                            }
    673                     } // end if (!(arc = DosSetFilePtr(hFile,
     682                        }
     683                    }
    674684                }
    675685            } // end if (!(arc = DosSetFilePtr(hFile,
     
    18411851        {
    18421852            // 32-bit OS/2 executable:
    1843             if (cResources = pExec->pLXHeader->ulResTblCnt)
     1853            PLXHEADER pLXHeader = pExec->pLXHeader;
     1854            if (cResources = pLXHeader->ulResTblCnt)
    18441855            {
    18451856                #pragma pack(1)     // V0.9.9 (2001-04-02) [umoeller]
     
    18761887
    18771888                ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    1878                                           pExec->pLXHeader->ulResTblOfs
     1889                                          pLXHeader->ulResTblOfs
    18791890                                            + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    18801891                                          FILE_BEGIN,
     
    18961907                for (i = 0; i < cResources; i++)
    18971908                {
    1898                     ULONG ulOfsThis =   pExec->pLXHeader->ulObjTblOfs
     1909                    ULONG ulOfsThis =   pLXHeader->ulObjTblOfs
    18991910                                      + ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
    19001911                                      + (   sizeof(ot)
     
    19251936        else if (pExec->ulExeFormat == EXEFORMAT_NE)
    19261937        {
     1938            PNEHEADER pNEHeader = pExec->pNEHeader;
     1939
    19271940            if (pExec->ulOS == EXEOS_OS2)
    19281941            {
    19291942                // 16-bit OS/2 executable:
    1930                 cResources = pExec->pNEHeader->usResSegmCount;
     1943                cResources = pNEHeader->usResSegmCount;
    19311944
    19321945                if (cResources)
     
    19561969
    19571970                    ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    1958                                               pExec->pNEHeader->usResTblOfs
     1971                                              pNEHeader->usResTblOfs
    19591972                                                + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    19601973                                              FILE_BEGIN,
     
    19751988                        ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    19761989                                                  ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
    1977                                                     + pExec->pNEHeader->usSegTblOfs
     1990                                                    + pNEHeader->usSegTblOfs
    19781991                                                    + (sizeof(ns)
    1979                                                     * (  pExec->pNEHeader->usSegTblEntries
    1980                                                        - pExec->pNEHeader->usResSegmCount
     1992                                                    * (  pNEHeader->usSegTblEntries
     1993                                                       - pNEHeader->usResSegmCount
    19811994                                                       + i)),
    19821995                                                    FILE_BEGIN,
     
    20012014
    20022015                ENSURE(DosSetFilePtr(pExec->hfExe,
    2003                                      pExec->pNEHeader->usResTblOfs
     2016                                     pNEHeader->usResTblOfs
    20042017                                       + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    20052018                                     FILE_BEGIN,
     
    20552068
    20562069                    ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    2057                                               pExec->pNEHeader->usResTblOfs
     2070                                              pNEHeader->usResTblOfs
    20582071                                                + ulNewHeaderOfs,
    20592072                                              FILE_BEGIN,
     
    22082221    APIRET arc;
    22092222
     2223    PLXHEADER pLXHeader;
     2224
    22102225    if (!pExec)
    22112226        arc = ERROR_INVALID_PARAMETER;
    2212     else if (pExec->ulExeFormat != EXEFORMAT_LX)
    2213         arc = ERROR_INVALID_EXE_SIGNATURE;
    22142227    else if (pExec->fLXMapsLoaded)
    22152228        // already loaded:
    22162229        arc = NO_ERROR;
     2230    else if (    (pExec->ulExeFormat != EXEFORMAT_LX)
     2231              || (!(pLXHeader = pExec->pLXHeader))
     2232            )
     2233        arc = ERROR_INVALID_EXE_SIGNATURE;
    22172234    else
    22182235    {
    2219         PLXHEADER pLXHeader = pExec->pLXHeader;
    22202236        ULONG ulNewHeaderOfs = 0;
    22212237        ULONG cb;
     
    22632279                                            (PBYTE)pExec->pObjPageTbl)))
    22642280                   )
    2265                   ;
     2281                {
     2282                }
    22662283            }
    22672284        }
     
    22902307    FREE(pExec->pObjPageTbl);
    22912308    pExec->fLXMapsLoaded = FALSE;
     2309}
     2310
     2311/*
     2312 *@@ doshLoadOS2NEMaps:
     2313 *
     2314 *      This works only if
     2315 *
     2316 *      --  ulExeFormat == EXEFORMAT_NE and
     2317 *
     2318 *      --  ulOS == EXEOS_OS2,
     2319 *
     2320 *      but not with Win16 NE executables.
     2321 *
     2322 *@@added V0.9.16 (2001-12-08) [umoeller]
     2323 */
     2324
     2325APIRET doshLoadOS2NEMaps(PEXECUTABLE pExec)
     2326{
     2327    APIRET arc;
     2328
     2329    PNEHEADER pNEHeader;
     2330
     2331    if (!pExec)
     2332        arc = ERROR_INVALID_PARAMETER;
     2333    else if (pExec->fOS2NEMapsLoaded)
     2334        // already loaded:
     2335        arc = NO_ERROR;
     2336    else if (    (pExec->ulExeFormat != EXEFORMAT_NE)
     2337              || (pExec->ulOS != EXEOS_OS2)
     2338              || (!(pNEHeader = pExec->pNEHeader))
     2339            )
     2340        arc = ERROR_INVALID_EXE_SIGNATURE;
     2341    else
     2342    {
     2343        ULONG ulNewHeaderOfs = 0;
     2344        ULONG cb;
     2345
     2346        if (pExec->pDosExeHeader)
     2347            // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
     2348            ulNewHeaderOfs = pExec->pDosExeHeader->ulNewHeaderOfs;
     2349
     2350        // resource table
     2351        if (    (!(arc = doshAllocArray(pNEHeader->usResSegmCount,
     2352                                        sizeof(OS2NERESTBLENTRY),
     2353                                        (PBYTE*)&pExec->paOS2NEResTblEntry,
     2354                                        &cb)))
     2355             && (!(arc = doshReadAt(pExec->hfExe,
     2356                                    pNEHeader->usResTblOfs
     2357                                      + ulNewHeaderOfs,
     2358                                    FILE_BEGIN,
     2359                                    &cb,
     2360                                    (PBYTE)pExec->paOS2NEResTblEntry)))
     2361            )
     2362        {
     2363            // resource segments
     2364            if (    (!(arc = doshAllocArray(pNEHeader->usResSegmCount,
     2365                                            sizeof(OS2NESEGMENT),
     2366                                            (PBYTE*)&pExec->paOS2NESegments,
     2367                                            &cb)))
     2368                 && (!(arc = doshReadAt(pExec->hfExe,
     2369                                        pNEHeader->usResTblOfs
     2370                                          + ulNewHeaderOfs
     2371                                          - cb, // pNEHeader->usResSegmCount * sizeof(struct new_seg)
     2372                                        FILE_BEGIN,
     2373                                        &cb,
     2374                                        (PBYTE)pExec->paOS2NESegments)))
     2375                )
     2376            {
     2377            }
     2378        }
     2379
     2380        if (!arc)
     2381            pExec->fOS2NEMapsLoaded = TRUE;
     2382        else
     2383            doshFreeNEMaps(pExec);
     2384    }
     2385
     2386    return (arc);
     2387}
     2388
     2389/*
     2390 *@@ doshFreeNEMaps:
     2391 *
     2392 *@@added V0.9.16 (2001-12-08) [umoeller]
     2393 */
     2394
     2395VOID doshFreeNEMaps(PEXECUTABLE pExec)
     2396{
     2397    FREE(pExec->paOS2NEResTblEntry);
     2398    FREE(pExec->paOS2NESegments);
     2399    pExec->fOS2NEMapsLoaded = FALSE;
    22922400}
    22932401
     
    23372445
    23382446        doshFreeLXMaps(pExec);
     2447        doshFreeNEMaps(pExec);
    23392448
    23402449        // fixed the memory leaks with the missing fields,
Note: See TracChangeset for help on using the changeset viewer.