Ignore:
Timestamp:
May 3, 2001, 8:01:12 PM (24 years ago)
Author:
umoeller
Message:

Misc changes.

File:
1 edited

Legend:

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

    r64 r67  
    797797 *      -- ERROR_NOT_ENOUGH_MEMORY: malloc() failed.
    798798 *
    799  *      -- ERROR_INVALID_EXE_SIGNATURE: specified file
    800  *              has no DOS EXE header, or it does, but
    801  *              the extended header is neither LX nor
    802  *              NE nor PE.
     799 *      -- ERROR_INVALID_EXE_SIGNATURE: unknown
     800 *          executable type... the given file probably
     801 *          isn't even an executable.
    803802 *
    804803 *      -- ERROR_INVALID_PARAMETER: ppExec is NULL.
     
    806805 *      plus those of DosOpen, DosSetFilePtr, and
    807806 *      DosRead.
     807 *
     808 *      The following executable types are supported
     809 *      (see EXECUTABLE for details):
     810 *
     811 *      --  Plain DOS 3.x executable without new header.
     812 *
     813 *      --  New Executable (NE), used by Win16 and
     814 *          16-bit OS/2 and still many of today's drivers.
     815 *
     816 *      --  Linear Executable (LX), OS/2 2.x and above.
     817 *
     818 *      --  Portable Executable (PE), used by Win32.
     819 *
     820 *      V0.9.12 adds support for NOSTUB executables,
     821 *      which are new-style executables (NE or LX)
     822 *      without a leading DOS header. The executable
     823 *      then starts directly with the NE or LX header.
     824 *      I am not sure whether PE supports such things
     825 *      as well... if so, it should be supported too.
    808826 *
    809827 *@@added V0.9.0 [umoeller]
     
    812830 *@@changed V0.9.10 (2001-04-08) [lafaix]: added PE support
    813831 *@@changed V0.9.10 (2001-04-08) [umoeller]: now setting ppExec only if NO_ERROR is returned
     832 *@@changed V0.9.12 (2001-05-03) [umoeller]: added support for NOSTUB newstyle executables
    814833 */
    815834
     
    870889               )
    871890            {
     891                ULONG ulNewHeaderOfs = 0;       // V0.9.12 (2001-05-03) [umoeller]
     892                BOOL  fLoadNewHeader = FALSE;
     893
    872894                // now check if we really have a DOS header
    873895                if (pExec->pDosExeHeader->usDosExeID != 0x5a4d)
    874                     arc = ERROR_INVALID_EXE_SIGNATURE;
     896                {
     897                    // arc = ERROR_INVALID_EXE_SIGNATURE;
     898
     899                    // V0.9.12 (2001-05-03) [umoeller]
     900                    // try loading new header directly; there are
     901                    // drivers which were built with NOSTUB, and
     902                    // the exe image starts out with the NE or LX
     903                    // image directly
     904                    fLoadNewHeader = TRUE;
     905                            // ulNewHeaderOfs is 0 now
     906
     907                    // remove the DOS header info, since we have none
     908                    // V0.9.12 (2001-05-03) [umoeller]
     909                    free (pExec->pDosExeHeader);
     910                    pExec->pDosExeHeader = 0;
     911                }
    875912                else
    876913                {
     
    884921                    else
    885922                    {
    886                         // either LX or PE or NE:
    887                         // read more bytes from position specified in header
    888                         CHAR    achNewHeaderType[2] = "";
    889 
    890                         if (    (!(arc = DosSetFilePtr(hFile,
    891                                                        pExec->pDosExeHeader->ulNewHeaderOfs,
    892                                                        FILE_BEGIN,
    893                                                        &ulLocal)))
    894                                 // read two chars to find out header type
    895                              && (!(arc = DosRead(hFile,
    896                                                  &achNewHeaderType,
    897                                                  sizeof(achNewHeaderType),
    898                                                  &ulBytesRead)))
    899                            )
     923                        // we have a new header offset:
     924                        fLoadNewHeader = TRUE;
     925                        ulNewHeaderOfs = pExec->pDosExeHeader->ulNewHeaderOfs;
     926                    }
     927                }
     928
     929                if (fLoadNewHeader)
     930                {
     931                    // either LX or PE or NE:
     932                    // read in new header...
     933                    // ulNewHeaderOfs is now either 0 (if no DOS header
     934                    // was found) or pDosExeHeader->ulNewHeaderOfs
     935                    // V0.9.12 (2001-05-03) [umoeller]
     936                    CHAR    achNewHeaderType[2] = "";
     937
     938                    if (    (!(arc = DosSetFilePtr(hFile,
     939                                                   ulNewHeaderOfs,
     940                                                   FILE_BEGIN,
     941                                                   &ulLocal)))
     942                            // read two chars to find out header type
     943                         && (!(arc = DosRead(hFile,
     944                                             &achNewHeaderType,
     945                                             sizeof(achNewHeaderType),
     946                                             &ulBytesRead)))
     947                       )
     948                    {
     949                        PBYTE   pbCheckOS = NULL;
     950
     951                        // reset file ptr
     952                        DosSetFilePtr(hFile,
     953                                      ulNewHeaderOfs,
     954                                      FILE_BEGIN,
     955                                      &ulLocal);
     956
     957                        if (memcmp(achNewHeaderType, "NE", 2) == 0)
    900958                        {
    901                             PBYTE   pbCheckOS = NULL;
    902 
    903                             // reset file ptr
    904                             DosSetFilePtr(hFile,
    905                                           pExec->pDosExeHeader->ulNewHeaderOfs,
    906                                           FILE_BEGIN,
    907                                           &ulLocal);
    908 
    909                             if (memcmp(achNewHeaderType, "NE", 2) == 0)
     959                            // New Executable:
     960                            pExec->ulExeFormat = EXEFORMAT_NE;
     961                            // allocate NE header
     962                            pExec->pNEHeader = (PNEHEADER)malloc(sizeof(NEHEADER));
     963                            if (!(pExec->pNEHeader))
     964                                arc = ERROR_NOT_ENOUGH_MEMORY;
     965                            else
     966                                // read in NE header
     967                                if (!(arc = DosRead(hFile,
     968                                                    pExec->pNEHeader,
     969                                                    sizeof(NEHEADER),
     970                                                    &(pExec->cbNEHeader))))
     971                                    if (pExec->cbNEHeader == sizeof(NEHEADER))
     972                                        pbCheckOS = &(pExec->pNEHeader->bTargetOS);
     973                        }
     974                        else if (   (memcmp(achNewHeaderType, "LX", 2) == 0)
     975                                 || (memcmp(achNewHeaderType, "LE", 2) == 0)
     976                                            // this is used by SMARTDRV.EXE
     977                                )
     978                        {
     979                            // OS/2 Linear Executable:
     980                            pExec->ulExeFormat = EXEFORMAT_LX;
     981                            // allocate LX header
     982                            pExec->pLXHeader = (PLXHEADER)malloc(sizeof(LXHEADER));
     983                            if (!(pExec->pLXHeader))
     984                                arc = ERROR_NOT_ENOUGH_MEMORY;
     985                            else
     986                                // read in LX header
     987                                if (!(arc = DosRead(hFile,
     988                                                    pExec->pLXHeader,
     989                                                    sizeof(LXHEADER),
     990                                                    &(pExec->cbLXHeader))))
     991                                    if (pExec->cbLXHeader == sizeof(LXHEADER))
     992                                        pbCheckOS = (PBYTE)(&(pExec->pLXHeader->usTargetOS));
     993                        }
     994                        else if (memcmp(achNewHeaderType, "PE", 2) == 0)
     995                        {
     996                            PEHEADER PEHeader = {0};
     997
     998                            pExec->ulExeFormat = EXEFORMAT_PE;
     999                            pExec->ulOS = EXEOS_WIN32;
     1000                            pExec->f32Bits = TRUE;
     1001
     1002                            // V0.9.10 (2001-04-08) [lafaix]
     1003                            // read in standard PE header
     1004                            if (!(arc = DosRead(hFile,
     1005                                                &PEHeader,
     1006                                                24,
     1007                                                &ulBytesRead)))
    9101008                            {
    911                                 // New Executable:
    912                                 pExec->ulExeFormat = EXEFORMAT_NE;
    913                                 // allocate NE header
    914                                 pExec->pNEHeader = (PNEHEADER)malloc(sizeof(NEHEADER));
    915                                 if (!(pExec->pNEHeader))
     1009                                // allocate PE header
     1010                                pExec->pPEHeader = (PPEHEADER)malloc(24 + PEHeader.usHeaderSize);
     1011                                if (!(pExec->pPEHeader))
    9161012                                    arc = ERROR_NOT_ENOUGH_MEMORY;
    9171013                                else
    918                                     // read in NE header
     1014                                {
     1015                                    // copy standard PE header
     1016                                    memcpy(pExec->pPEHeader,
     1017                                           &PEHeader,
     1018                                           24);
     1019
     1020                                    // read in optional PE header
    9191021                                    if (!(arc = DosRead(hFile,
    920                                                         pExec->pNEHeader,
    921                                                         sizeof(NEHEADER),
    922                                                         &(pExec->cbNEHeader))))
    923                                         if (pExec->cbNEHeader == sizeof(NEHEADER))
    924                                             pbCheckOS = &(pExec->pNEHeader->bTargetOS);
    925                             }
    926                             else if (   (memcmp(achNewHeaderType, "LX", 2) == 0)
    927                                      || (memcmp(achNewHeaderType, "LE", 2) == 0)
    928                                                 // this is used by SMARTDRV.EXE
    929                                     )
    930                             {
    931                                 // OS/2 Linear Executable:
    932                                 pExec->ulExeFormat = EXEFORMAT_LX;
    933                                 // allocate LX header
    934                                 pExec->pLXHeader = (PLXHEADER)malloc(sizeof(LXHEADER));
    935                                 if (!(pExec->pLXHeader))
    936                                     arc = ERROR_NOT_ENOUGH_MEMORY;
    937                                 else
    938                                     // read in LX header
    939                                     if (!(arc = DosRead(hFile,
    940                                                         pExec->pLXHeader,
    941                                                         sizeof(LXHEADER),
    942                                                         &(pExec->cbLXHeader))))
    943                                         if (pExec->cbLXHeader == sizeof(LXHEADER))
    944                                             pbCheckOS = (PBYTE)(&(pExec->pLXHeader->usTargetOS));
    945                             }
    946                             else if (memcmp(achNewHeaderType, "PE", 2) == 0)
    947                             {
    948                                 PEHEADER PEHeader = {0};
    949 
    950                                 pExec->ulExeFormat = EXEFORMAT_PE;
    951                                 pExec->ulOS = EXEOS_WIN32;
    952                                 pExec->f32Bits = TRUE;
    953 
    954                                 // V0.9.10 (2001-04-08) [lafaix]
    955                                 // read in standard PE header
    956                                 if (!(arc = DosRead(hFile,
    957                                                     &PEHeader,
    958                                                     24,
    959                                                     &ulBytesRead)))
    960                                 {
    961                                     // allocate PE header
    962                                     pExec->pPEHeader = (PPEHEADER)malloc(24 + PEHeader.usHeaderSize);
    963                                     if (!(pExec->pPEHeader))
    964                                         arc = ERROR_NOT_ENOUGH_MEMORY;
    965                                     else
    966                                     {
    967                                         // copy standard PE header
    968                                         memcpy(pExec->pPEHeader,
    969                                                &PEHeader,
    970                                                24);
    971 
    972                                         // read in optional PE header
    973                                         if (!(arc = DosRead(hFile,
    974                                                             &(pExec->pPEHeader->usReserved3),
    975                                                             PEHeader.usHeaderSize,
    976                                                             &(pExec->cbPEHeader))))
    977                                             pExec->cbPEHeader += 24;
    978                                     }
     1022                                                        &(pExec->pPEHeader->usReserved3),
     1023                                                        PEHeader.usHeaderSize,
     1024                                                        &(pExec->cbPEHeader))))
     1025                                        pExec->cbPEHeader += 24;
    9791026                                }
    9801027                            }
    981                             else
    982                                 // strange type:
    983                                 arc = ERROR_INVALID_EXE_SIGNATURE;
    984 
    985                             if (pbCheckOS)
    986                                 // BYTE to check for operating system
    987                                 // (NE and LX):
    988                                 switch (*pbCheckOS)
    989                                 {
    990                                     case NEOS_OS2:
    991                                         pExec->ulOS = EXEOS_OS2;
    992                                         if (pExec->ulExeFormat == EXEFORMAT_LX)
    993                                             pExec->f32Bits = TRUE;
    994                                     break;
    995 
    996                                     case NEOS_WIN16:
    997                                         pExec->ulOS = EXEOS_WIN16;
    998                                     break;
    999 
    1000                                     case NEOS_DOS4:
    1001                                         pExec->ulOS = EXEOS_DOS4;
    1002                                     break;
    1003 
    1004                                     case NEOS_WIN386:
    1005                                         pExec->ulOS = EXEOS_WIN386;
     1028                        }
     1029                        else
     1030                            // strange type:
     1031                            arc = ERROR_INVALID_EXE_SIGNATURE;
     1032
     1033                        if (pbCheckOS)
     1034                            // BYTE to check for operating system
     1035                            // (NE and LX):
     1036                            switch (*pbCheckOS)
     1037                            {
     1038                                case NEOS_OS2:
     1039                                    pExec->ulOS = EXEOS_OS2;
     1040                                    if (pExec->ulExeFormat == EXEFORMAT_LX)
    10061041                                        pExec->f32Bits = TRUE;
    1007                                     break;
    1008                                 }
    1009                         } // end if (!(arc = DosSetFilePtr(hFile,
    1010                     }
     1042                                break;
     1043
     1044                                case NEOS_WIN16:
     1045                                    pExec->ulOS = EXEOS_WIN16;
     1046                                break;
     1047
     1048                                case NEOS_DOS4:
     1049                                    pExec->ulOS = EXEOS_DOS4;
     1050                                break;
     1051
     1052                                case NEOS_WIN386:
     1053                                    pExec->ulOS = EXEOS_WIN386;
     1054                                    pExec->f32Bits = TRUE;
     1055                                break;
     1056                            }
     1057                    } // end if (!(arc = DosSetFilePtr(hFile,
    10111058                }
    10121059            } // end if (!(arc = DosSetFilePtr(hFile,
     
    12731320 *@@changed V0.9.10 (2001-04-10) [lafaix]: added Win16 and Win386 support
    12741321 *@@changed V0.9.10 (2001-04-13) [lafaix]: removed 127 characters limit
     1322 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
    12751323 */
    12761324
     
    12911339        int i;
    12921340
     1341        ULONG ulNewHeaderOfs = 0;       // V0.9.12 (2001-05-03) [umoeller]
     1342
     1343        if (pExec->pDosExeHeader)
     1344            // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
     1345            ulNewHeaderOfs = pExec->pDosExeHeader->ulNewHeaderOfs;
     1346
    12931347        if (pExec->ulExeFormat == EXEFORMAT_LX)
    12941348        {
     
    12981352            if (cModules)
    12991353            {
    1300                 ULONG cb = sizeof(FSYSMODULE) * cModules; // V0.9.9 (2001-04-03) [umoeller]
    1301                 ULONG ulDummy;
     1354                ULONG   cb = sizeof(FSYSMODULE) * cModules; // V0.9.9 (2001-04-03) [umoeller]
     1355                ULONG   ulDummy;
    13021356
    13031357                paModules = (PFSYSMODULE)malloc(cb);
     
    13091363                ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    13101364                                          pExec->pLXHeader->ulImportModTblOfs
    1311                                             + pExec->pDosExeHeader->ulNewHeaderOfs,
     1365                                            + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    13121366                                          FILE_BEGIN,
    13131367                                          &ulDummy));
     
    13601414                    ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    13611415                                              pExec->pNEHeader->usModRefTblOfs
    1362                                                 + pExec->pDosExeHeader->ulNewHeaderOfs
     1416                                                + ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
    13631417                                                + sizeof(usOfs) * i,
    13641418                                              FILE_BEGIN,
     
    13691423                    ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    13701424                                              pExec->pNEHeader->usImportTblOfs
    1371                                                 + pExec->pDosExeHeader->ulNewHeaderOfs
     1425                                                + ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
    13721426                                                + usOfs,
    13731427                                              FILE_BEGIN,
     
    13841438                } // end for
    13851439            }
    1386         }
     1440        } // end NE
    13871441        else
    13881442            ENSURE_FAIL(ERROR_INVALID_EXE_SIGNATURE); // V0.9.9 (2001-04-03) [umoeller]
     
    14261480 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
    14271481 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
     1482 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
    14281483 */
    14291484
     
    14371492    int    i;
    14381493
     1494    ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
     1495
     1496    if (pExec->pDosExeHeader)
     1497        // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
     1498        ulNewHeaderOfs = pExec->pDosExeHeader->ulNewHeaderOfs;
     1499
    14391500    ENSURE(DosSetFilePtr(pExec->hfExe,
    14401501                         pExec->pLXHeader->ulEntryTblOfs
    1441                            + pExec->pDosExeHeader->ulNewHeaderOfs,
     1502                           + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    14421503                         FILE_BEGIN,
    14431504                         &ulDummy));
     
    16591720 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
    16601721 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
     1722 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
    16611723 */
    16621724
     
    16701732    int    i;
    16711733
     1734    ULONG ulNewHeaderOfs = 0;
     1735
     1736    if (pExec->pDosExeHeader)
     1737        // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
     1738        ulNewHeaderOfs = pExec->pDosExeHeader->ulNewHeaderOfs;
     1739
    16721740    ENSURE(DosSetFilePtr(pExec->hfExe,
    16731741                         pExec->pNEHeader->usEntryTblOfs
    1674                            + pExec->pDosExeHeader->ulNewHeaderOfs,
     1742                           + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    16751743                         FILE_BEGIN,
    16761744                         &ulDummy));
     
    18491917 *@@changed V0.9.9 (2001-04-05) [lafaix]: rewritten error checking code
    18501918 *@@changed V0.9.10 (2001-04-10) [lafaix]: added Win16 and Win386 support
     1919 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
    18511920 */
    18521921
     
    18681937        ULONG ulDummy;
    18691938
     1939        ULONG ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
     1940
     1941        if (pExec->pDosExeHeader)
     1942            // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
     1943            ulNewHeaderOfs = pExec->pDosExeHeader->ulNewHeaderOfs;
     1944
    18701945        if (pExec->ulExeFormat == EXEFORMAT_LX)
    18711946        {
     
    18981973                ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    18991974                                          pExec->pLXHeader->ulResdNameTblOfs
    1900                                             + pExec->pDosExeHeader->ulNewHeaderOfs,
     1975                                            + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    19011976                                          FILE_BEGIN,
    19021977                                          &ulDummy));
     
    19462021                ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    19472022                                          pExec->pNEHeader->usResdNameTblOfs
    1948                                             + pExec->pDosExeHeader->ulNewHeaderOfs,
     2023                                            + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    19492024                                          FILE_BEGIN,
    19502025                                          &ulDummy));
     
    20182093 *@@changed V0.9.9 (2001-04-03) [umoeller]: added tons of error checking, changed prototype to return APIRET
    20192094 *@@changed V0.9.10 (2001-04-10) [lafaix]: added Win16 and Win386 support
     2095 *@@changed V0.9.12 (2001-05-03) [umoeller]: adjusted for new NOSTUB support
    20202096 */
    20212097
     
    20342110        ULONG           cResources = 0;
    20352111        PFSYSRESOURCE   paResources = NULL;
     2112
     2113        ULONG           ulNewHeaderOfs = 0; // V0.9.12 (2001-05-03) [umoeller]
     2114
     2115        if (pExec->pDosExeHeader)
     2116            // executable has DOS stub: V0.9.12 (2001-05-03) [umoeller]
     2117            ulNewHeaderOfs = pExec->pDosExeHeader->ulNewHeaderOfs;
    20362118
    20372119        if (pExec->ulExeFormat == EXEFORMAT_LX)
     
    20752157                ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    20762158                                          pExec->pLXHeader->ulResTblOfs
    2077                                             + pExec->pDosExeHeader->ulNewHeaderOfs,
     2159                                            + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    20782160                                          FILE_BEGIN,
    20792161                                          &ulDummy));
     
    20952177                {
    20962178                    ULONG ulOfsThis =   pExec->pLXHeader->ulObjTblOfs
    2097                                       + pExec->pDosExeHeader->ulNewHeaderOfs
     2179                                      + ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
    20982180                                      + (   sizeof(ot)
    20992181                                          * (paResources[i].ulFlag - 1));
     
    21552237                    ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    21562238                                              pExec->pNEHeader->usResTblOfs
    2157                                                 + pExec->pDosExeHeader->ulNewHeaderOfs,
     2239                                                + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    21582240                                              FILE_BEGIN,
    21592241                                              &ulDummy));
     
    21722254                    {
    21732255                        ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    2174                                                   pExec->pDosExeHeader->ulNewHeaderOfs
     2256                                                  ulNewHeaderOfs // V0.9.12 (2001-05-03) [umoeller]
    21752257                                                    + pExec->pNEHeader->usSegTblOfs
    21762258                                                    + (sizeof(ns)
     
    22002282                ENSURE(DosSetFilePtr(pExec->hfExe,
    22012283                                     pExec->pNEHeader->usResTblOfs
    2202                                        + pExec->pDosExeHeader->ulNewHeaderOfs,
     2284                                       + ulNewHeaderOfs, // V0.9.12 (2001-05-03) [umoeller]
    22032285                                     FILE_BEGIN,
    22042286                                     &ulDummy));
     
    22542336                    ENSURE_SAFE(DosSetFilePtr(pExec->hfExe,
    22552337                                              pExec->pNEHeader->usResTblOfs
    2256                                                 + pExec->pDosExeHeader->ulNewHeaderOfs,
     2338                                                + ulNewHeaderOfs,
    22572339                                              FILE_BEGIN,
    22582340                                              &ulDummy));
Note: See TracChangeset for help on using the changeset viewer.