Ignore:
Timestamp:
Jan 17, 2012, 12:54:12 PM (14 years ago)
Author:
dmik
Message:

Fix Indentation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/peldr/pe.cpp

    r21916 r21940  
    1 /* $Id: pe.cpp,v 1.37 2003-01-05 17:05:20 sandervl Exp $ */
    2 
    31/*
    42 * PELDR main exe loader code
     
    6058typedef BOOL (* APIENTRY WINDESTROYMSGQUEUEPROC) (HMQ hmq);
    6159typedef ULONG (* APIENTRY WINMESSAGEBOXPROC) (HWND hwndParent,
    62                                               HWND hwndOwner,
    63                                               PCSZ  pszText,
    64                                               PCSZ  pszCaption,
    65                                               ULONG idWindow,
    66                                               ULONG flStyle);
     60                                              HWND hwndOwner,
     61                                              PCSZ  pszText,
     62                                              PCSZ  pszCaption,
     63                                              ULONG idWindow,
     64                                              ULONG flStyle);
    6765typedef void (* KRNL32EXCEPTPROC) (void *exceptframe);
    6866
     
    8684int main(int argc, char *argv[])
    8785{
    88  HAB    hab = 0;                             /* PM anchor block handle       */
    89  HMQ    hmq = 0;                             /* Message queue handle         */
    90  char   exeName[CCHMAXPATH];
    91  char   fullpath[CCHMAXPATH];
    92  char   errorMod[CCHMAXPATH];
    93  char  *pszErrorMsg = NULL;
    94  APIRET  rc;
    95  HMODULE hmodPMWin = 0, hmodKernel32 = 0;
    96  PTIB   ptib;
    97  PPIB   ppib;
    98  char  *cmdline, *win32cmdline, *peoptions, *newcmdline;
    99  BOOL   fQuote = FALSE, fVioConsole, fIsNEExe, fEndOfCmdLine = FALSE;
    100  int    nrTries = 1;
    101 
    102   if(DosGetInfoBlocks(&ptib, &ppib) == 0)
    103   {
    104       char *pszTemp;
    105 
    106       fullpath[0] = 0;
    107       DosQueryModuleName(ppib->pib_hmte, sizeof(fullpath), fullpath);
    108 
    109       strupr(fullpath);
     86    HAB    hab = 0;                             /* PM anchor block handle       */
     87    HMQ    hmq = 0;                             /* Message queue handle         */
     88    char   exeName[CCHMAXPATH];
     89    char   fullpath[CCHMAXPATH];
     90    char   errorMod[CCHMAXPATH];
     91    char  *pszErrorMsg = NULL;
     92    APIRET  rc;
     93    HMODULE hmodPMWin = 0, hmodKernel32 = 0;
     94    PTIB   ptib;
     95    PPIB   ppib;
     96    char  *cmdline, *win32cmdline, *peoptions, *newcmdline;
     97    BOOL   fQuote = FALSE, fVioConsole, fIsNEExe, fEndOfCmdLine = FALSE;
     98    int    nrTries = 1;
     99
     100    if(DosGetInfoBlocks(&ptib, &ppib) == 0)
     101    {
     102        char *pszTemp;
     103
     104        fullpath[0] = 0;
     105        DosQueryModuleName(ppib->pib_hmte, sizeof(fullpath), fullpath);
     106
     107        strupr(fullpath);
    110108#ifdef COMMAND_LINE_VERSION
    111       pszTemp = strstr(fullpath, "PEC.EXE");
     109        pszTemp = strstr(fullpath, "PEC.EXE");
    112110#else
    113       pszTemp = strstr(fullpath, "PE.EXE");
     111        pszTemp = strstr(fullpath, "PE.EXE");
    114112#endif
    115       if(pszTemp == NULL) {
    116           pszErrorMsg = szInteralErrorMsg1;
    117           goto failerror;
    118       }
    119       pszTemp--; //to go trailing backslash
    120       *pszTemp = 0;
    121       strcat(fullpath, ";%BeginLIBPATH%");
    122       DosSetExtLIBPATH(fullpath, BEGIN_LIBPATH);
    123   }
    124 
    125   if(argc >= 2) {
    126         if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
     113        if(pszTemp == NULL) {
     114            pszErrorMsg = szInteralErrorMsg1;
     115            goto failerror;
     116        }
     117        pszTemp--; //to go trailing backslash
     118        *pszTemp = 0;
     119        strcat(fullpath, ";%BeginLIBPATH%");
     120        DosSetExtLIBPATH(fullpath, BEGIN_LIBPATH);
     121    }
     122
     123    if(argc >= 2) {
     124        if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
    127125tryagain:
    128                 cmdline = ppib->pib_pchcmd;
    129                 cmdline += strlen(cmdline)+1;   //skip pe.exe
    130                 peoptions = strstr(cmdline, "/OPT:[");
    131                 if(peoptions) {
    132                         newcmdline = strchr(peoptions, ']');
    133                         if(newcmdline) {
    134                                 cmdline = newcmdline+1;
    135                         }
     126            cmdline = ppib->pib_pchcmd;
     127            cmdline += strlen(cmdline)+1;       //skip pe.exe
     128            peoptions = strstr(cmdline, "/OPT:[");
     129            if(peoptions) {
     130                newcmdline = strchr(peoptions, ']');
     131                if(newcmdline) {
     132                    cmdline = newcmdline+1;
     133                }
    136134#ifdef DEBUG
    137                         else    _interrupt(3);  //should not happen!
     135                else    _interrupt(3);  //should not happen!
    138136#endif
    139                 }
    140                 while(*cmdline == ' ')  cmdline++; //skip leading space
    141                 if(*cmdline == '"') {
    142                         cmdline++;
    143                         fQuote = TRUE;
    144                 }
    145                 win32cmdline = cmdline;
    146 
    147                 strncpy(exeName, cmdline, sizeof(exeName)-1);
    148                 exeName[sizeof(exeName)-1] = 0;
    149                 char *p = exeName;
    150                 if(fQuote) {
    151                         while(*p != '"' && *p != 0) p++;
    152                 }
    153                 else {
    154                         for(int i=0;i<nrTries;i++) {
    155                                 while(*p != ' ' && *p != 0) p++;
    156                                 if(*p == 0) break;
    157                                 if(i != nrTries-1) {
    158                                         while(*p == ' ' && *p != 0) p++;
    159                                 }
    160                         }
    161                         if(nrTries > 1 && *p == 0) {
    162                             fEndOfCmdLine = TRUE;
    163                         }
    164                 }
    165                 *p = 0;
    166                 strupr(exeName);
    167                 cmdline = strstr(exeName, ".EXE");
    168                 if(cmdline) {
    169                         cmdline[4] = 0;
    170                         win32cmdline += ((ULONG)cmdline - (ULONG)exeName) + 4;
    171                 }
    172                 else {
    173                         win32cmdline += strlen(exeName);
    174                         if(strstr(exeName, ".") == NULL) {
    175                                 strcat(exeName, ".EXE");
    176                         }
    177                 }
    178                 if(fQuote) win32cmdline++;
    179                 while(*win32cmdline == ' ') win32cmdline++; //skip spaces
    180 
    181                 cmdline = exeName + strlen(exeName) - 1;
    182                 while(*cmdline == ' ') cmdline--;
    183                 cmdline[1] = 0;
    184 
    185                 char drive[_MAX_DRIVE];
    186                 char dir[_MAX_DIR];
    187                 char fname[_MAX_FNAME];
    188                 char ext[_MAX_EXT];
    189                 char exeShortName[_MAX_FNAME+_MAX_EXT];
    190                 _splitpath(exeName, drive, dir, fname, ext);
    191 
    192                 strcpy(fullpath, drive);
    193                 strcat(fullpath, dir);
    194 
    195                 strcpy(exeShortName, fname);
    196                 strcat(exeShortName, ext);
    197 
    198                 if ( strlen(fullpath) == 0 )
     137            }
     138            while(*cmdline == ' ')      cmdline++; //skip leading space
     139            if(*cmdline == '"') {
     140                cmdline++;
     141                fQuote = TRUE;
     142            }
     143            win32cmdline = cmdline;
     144
     145            strncpy(exeName, cmdline, sizeof(exeName)-1);
     146            exeName[sizeof(exeName)-1] = 0;
     147            char *p = exeName;
     148            if(fQuote) {
     149                while(*p != '"' && *p != 0) p++;
     150            }
     151            else {
     152                for(int i=0;i<nrTries;i++) {
     153                    while(*p != ' ' && *p != 0) p++;
     154                    if(*p == 0) break;
     155                    if(i != nrTries-1) {
     156                        while(*p == ' ' && *p != 0) p++;
     157                    }
     158                }
     159                if(nrTries > 1 && *p == 0) {
     160                    fEndOfCmdLine = TRUE;
     161                }
     162            }
     163            *p = 0;
     164            strupr(exeName);
     165            cmdline = strstr(exeName, ".EXE");
     166            if(cmdline) {
     167                cmdline[4] = 0;
     168                win32cmdline += ((ULONG)cmdline - (ULONG)exeName) + 4;
     169            }
     170            else {
     171                win32cmdline += strlen(exeName);
     172                if(strstr(exeName, ".") == NULL) {
     173                    strcat(exeName, ".EXE");
     174                }
     175            }
     176            if(fQuote) win32cmdline++;
     177            while(*win32cmdline == ' ') win32cmdline++; //skip spaces
     178
     179            cmdline = exeName + strlen(exeName) - 1;
     180            while(*cmdline == ' ') cmdline--;
     181            cmdline[1] = 0;
     182
     183            char drive[_MAX_DRIVE];
     184            char dir[_MAX_DIR];
     185            char fname[_MAX_FNAME];
     186            char ext[_MAX_EXT];
     187            char exeShortName[_MAX_FNAME+_MAX_EXT];
     188            _splitpath(exeName, drive, dir, fname, ext);
     189
     190            strcpy(fullpath, drive);
     191            strcat(fullpath, dir);
     192
     193            strcpy(exeShortName, fname);
     194            strcat(exeShortName, ext);
     195
     196            if ( strlen(fullpath) == 0 )
     197            {
     198                char newExeName[CCHMAXPATH];
     199
     200                if(DosSearchPath( SEARCH_CUR_DIRECTORY | SEARCH_ENVIRONMENT | SEARCH_IGNORENETERRS
     201                                  , "WINDOWSPATH"           /* environment value */
     202                                  , exeShortName            /* Name of file to look for */
     203                                  , newExeName              /* Result of the search     */
     204                                  , sizeof(newExeName)      /* Length of search buffer  */
     205                                  ) == NO_ERROR)
    199206                {
    200                     char newExeName[CCHMAXPATH];
    201 
    202                         if(DosSearchPath( SEARCH_CUR_DIRECTORY | SEARCH_ENVIRONMENT | SEARCH_IGNORENETERRS
    203                                      , "WINDOWSPATH"           /* environment value */
    204                                      , exeShortName            /* Name of file to look for */
    205                                      , newExeName              /* Result of the search     */
    206                                      , sizeof(newExeName)      /* Length of search buffer  */
    207                                      ) == NO_ERROR)
    208                         {
    209                             strcpy(exeName, newExeName);
    210                         }
    211                 }
    212 
    213                 FILESTATUS3 fstat3;
    214                 if(DosQueryPathInfo(exeName, FIL_STANDARD, (PVOID)&fstat3, sizeof(fstat3)))
    215                 {
    216                         nrTries++;
    217                         if(fEndOfCmdLine) {
    218                             pszErrorMsg = szFileNotFound;
    219                             goto failerror;
    220                         }
    221 
    222                         if(*win32cmdline != NULL && !fQuote) {
    223                                 goto tryagain;
    224                         }
    225                 }
    226         }
    227         else {//should never happen!
    228                 pszErrorMsg = szDosInfoBlocks;
    229                 goto failerror;
    230         }
    231         AllocateExeMem(exeName, &fIsNEExe);
     207                    strcpy(exeName, newExeName);
     208                }
     209            }
     210
     211            FILESTATUS3 fstat3;
     212            if(DosQueryPathInfo(exeName, FIL_STANDARD, (PVOID)&fstat3, sizeof(fstat3)))
     213            {
     214                nrTries++;
     215                if(fEndOfCmdLine) {
     216                    pszErrorMsg = szFileNotFound;
     217                    goto failerror;
     218                }
     219
     220                if(*win32cmdline != NULL && !fQuote) {
     221                    goto tryagain;
     222                }
     223            }
     224        }
     225        else {//should never happen!
     226            pszErrorMsg = szDosInfoBlocks;
     227            goto failerror;
     228        }
     229        AllocateExeMem(exeName, &fIsNEExe);
    232230        if(fIsNEExe) {
    233231            STARTDATA sdata = {0};
     
    245243            rc = DosStartSession(&sdata, &idSession, &pid);
    246244            if(rc) {
    247                 sprintf(fullpath, szErrDosStartSession, rc);
     245                sprintf(fullpath, szErrDosStartSession, rc);
    248246                pszErrorMsg = fullpath;
    249247                goto failerror;
     
    251249            return 0;
    252250        }
    253   }
     251    }
    254252
    255253#ifdef COMMAND_LINE_VERSION
    256   if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
     254    if(DosGetInfoBlocks(&ptib, &ppib) == 0) {
    257255        //switch process type to PM so the command line app can create PM
    258256        //windows
    259         ppib->pib_ultype = 3;
    260   }
     257        ppib->pib_ultype = 3;
     258    }
    261259#endif
    262260
    263   rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN", &hmodPMWin);
    264   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
    265   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
    266   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
    267   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
    268   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
    269 
    270   if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */
    271         goto fail;
    272 
    273   hmq = MyWinCreateMsgQueue(hab, 0);
    274 
    275   if(argc < 2) {
    276         MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
     261    rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN", &hmodPMWin);
     262    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
     263    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
     264    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
     265    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
     266    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
     267
     268    if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */
    277269        goto fail;
    278   }
    279 
    280   errorMod[0] = 0;
    281   rc = DosLoadModule(errorMod, sizeof(errorMod), "KERNEL32", &hmodKernel32);
    282   if(rc) {
    283         sprintf(fullpath, szNoKernel32Msg, rc, errorMod);
    284         MyWinMessageBox(HWND_DESKTOP, NULL, fullpath, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
     270
     271    hmq = MyWinCreateMsgQueue(hab, 0);
     272
     273    if(argc < 2) {
     274        MyWinMessageBox(HWND_DESKTOP, NULL, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    285275        goto fail;
    286   }
    287   rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@36", (PFN *)&CreateWin32Exe);
     276    }
     277
     278    errorMod[0] = 0;
     279    rc = DosLoadModule(errorMod, sizeof(errorMod), "KERNEL32", &hmodKernel32);
     280    if(rc) {
     281        sprintf(fullpath, szNoKernel32Msg, rc, errorMod);
     282        MyWinMessageBox(HWND_DESKTOP, NULL, fullpath, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
     283        goto fail;
     284    }
     285    rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@36", (PFN *)&CreateWin32Exe);
    288286
    289287#ifdef COMMAND_LINE_VERSION
    290   fVioConsole = TRUE;
     288    fVioConsole = TRUE;
    291289#else
    292   fVioConsole = FALSE;
     290    fVioConsole = FALSE;
    293291#endif
    294   rc = CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, 0,
    295                       fConsoleApp, fVioConsole, errorMod, sizeof(errorMod));
    296   if(rc != LDRERROR_SUCCESS)
    297   {
     292    rc = CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, 0,
     293                        fConsoleApp, fVioConsole, errorMod, sizeof(errorMod));
     294    if(rc != LDRERROR_SUCCESS)
     295    {
    298296        char szErrorMsg[512];
    299297
     
    325323        MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    326324        goto fail;
    327   }
    328 
    329   if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
    330   MyWinTerminate( hab );                   /* Terminate the application    */
    331 
    332   DosFreeModule(hmodPMWin);
    333   DosFreeModule(hmodKernel32);
    334   return 0;
     325    }
     326
     327    if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
     328    MyWinTerminate( hab );                   /* Terminate the application    */
     329
     330    DosFreeModule(hmodPMWin);
     331    DosFreeModule(hmodKernel32);
     332    return 0;
    335333
    336334fail:
    337   if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
    338   if(hab) MyWinTerminate( hab );                   /* Terminate the application    */
    339 
    340   if(hmodPMWin)         DosFreeModule(hmodPMWin);
    341   if(hmodKernel32)      DosFreeModule(hmodKernel32);
    342   return(1);
     335    if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
     336    if(hab) MyWinTerminate( hab );                   /* Terminate the application    */
     337
     338    if(hmodPMWin)       DosFreeModule(hmodPMWin);
     339    if(hmodKernel32)    DosFreeModule(hmodKernel32);
     340    return(1);
    343341
    344342failerror:
    345   rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN", &hmodPMWin);
    346   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
    347   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
    348   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
    349   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
    350   rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
    351   if(rc == 0) {
    352       if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */
    353         goto fail;
    354 
    355       hmq = MyWinCreateMsgQueue(hab, 0);
    356 
    357       MyWinMessageBox(HWND_DESKTOP, NULL, pszErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    358   }
    359   if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
    360   if(hab) MyWinTerminate( hab );                   /* Terminate the application    */
    361   if(hmodPMWin)         DosFreeModule(hmodPMWin);
    362   return 1;
     343    rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN", &hmodPMWin);
     344    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
     345    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
     346    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
     347    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
     348    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
     349    if(rc == 0) {
     350        if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */
     351            goto fail;
     352
     353        hmq = MyWinCreateMsgQueue(hab, 0);
     354
     355        MyWinMessageBox(HWND_DESKTOP, NULL, pszErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
     356    }
     357    if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
     358    if(hab) MyWinTerminate( hab );                   /* Terminate the application    */
     359    if(hmodPMWin)       DosFreeModule(hmodPMWin);
     360    return 1;
    363361}
    364362//******************************************************************************
     
    369367BOOL AllocateExeMem(char *filename, BOOL *fNEExe)
    370368{
    371  HFILE  dllfile = 0;
    372  char   szFileName[CCHMAXPATH], *tmp;
    373  char   szResult[CCHMAXPATH];
    374  ULONG  action, ulRead, signature;
    375  APIRET rc;
    376  IMAGE_DOS_HEADER doshdr;
    377  IMAGE_OPTIONAL_HEADER oh;
    378  IMAGE_FILE_HEADER     fh;
    379  ULONG  address = 0;
    380  ULONG  *memallocs;
    381  ULONG  alloccnt = 0;
    382  ULONG  diff, i, baseAddress;
    383  ULONG  ulSysinfo, flAllocMem = 0;
    384  BOOL   ret = FALSE;
    385 
    386   *fNEExe = FALSE;
    387   strcpy(szFileName, filename);
    388 
    389   rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
    390   if(rc != 0) {
    391         if(!strstr(szFileName, ".EXE")) {
    392                 strcat(szFileName,".EXE");
    393         }
    394   }
    395   else  DosClose(dllfile);
    396 
    397   rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
    398   if(rc) {
     369    HFILE  dllfile = 0;
     370    char   szFileName[CCHMAXPATH], *tmp;
     371    char   szResult[CCHMAXPATH];
     372    ULONG  action, ulRead, signature;
     373    APIRET rc;
     374    IMAGE_DOS_HEADER doshdr;
     375    IMAGE_OPTIONAL_HEADER oh;
     376    IMAGE_FILE_HEADER     fh;
     377    ULONG  address = 0;
     378    ULONG  *memallocs;
     379    ULONG  alloccnt = 0;
     380    ULONG  diff, i, baseAddress;
     381    ULONG  ulSysinfo, flAllocMem = 0;
     382    BOOL   ret = FALSE;
     383
     384    *fNEExe = FALSE;
     385    strcpy(szFileName, filename);
     386
     387    rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
     388    if(rc != 0) {
     389        if(!strstr(szFileName, ".EXE")) {
     390            strcat(szFileName,".EXE");
     391        }
     392    }
     393    else        DosClose(dllfile);
     394
     395    rc = DosOpen(szFileName, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
     396    if(rc) {
    399397        if(DosSearchPath(SEARCH_IGNORENETERRS|SEARCH_ENVIRONMENT, "PATH",
    400398                         szFileName, szResult, sizeof(szResult)) != 0) {
    401                 goto end; //oops
    402         }
    403         rc = DosOpen(szResult, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
    404         if(rc) {
    405                 goto end; //oops
    406         }
    407   }
    408 
    409   //read dos header
    410   if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
    411         goto end;
    412   }
    413   if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) {
    414         goto end;
    415   }
    416   //read signature dword
    417   if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
    418         goto end;
    419   }
    420   //read pe header
    421   if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
    422         goto end;
    423   }
    424   //read optional header
    425   if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
    426         goto end;
    427   }
    428   if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
     399            goto end; //oops
     400        }
     401        rc = DosOpen(szResult, &dllfile, &action, 0, FILE_READONLY, OPEN_ACTION_OPEN_IF_EXISTS|OPEN_ACTION_FAIL_IF_NEW, OPEN_SHARE_DENYNONE|OPEN_ACCESS_READONLY, NULL);
     402        if(rc) {
     403            goto end; //oops
     404        }
     405    }
     406
     407    //read dos header
     408    if(DosRead(dllfile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
     409        goto end;
     410    }
     411    if(DosSetFilePtr(dllfile, doshdr.e_lfanew, FILE_BEGIN, &ulRead)) {
     412        goto end;
     413    }
     414    //read signature dword
     415    if(DosRead(dllfile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
     416        goto end;
     417    }
     418    //read pe header
     419    if(DosRead(dllfile, (LPVOID)&fh, sizeof(fh), &ulRead)) {
     420        goto end;
     421    }
     422    //read optional header
     423    if(DosRead(dllfile, (LPVOID)&oh, sizeof(oh), &ulRead)) {
     424        goto end;
     425    }
     426    if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || signature != IMAGE_NT_SIGNATURE) {
    429427        if(LOWORD(signature) == IMAGE_OS2_SIGNATURE) {
    430428            *fNEExe = TRUE;
    431429        }
    432         goto end;
    433   }
    434   fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
    435 
    436   // check for high memory support
    437   rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
    438   if (rc == 0 && ulSysinfo > 512)   //VirtualAddresslimit is in MB
    439   {
    440         flAllocMem = PAG_ANY;      // high memory support. Let's use it!
    441   }
    442 
    443   //Reserve enough space to store 4096 pointers to 1MB memory chunks
    444   memallocs = (ULONG *)alloca(4096*sizeof(ULONG *));
    445   if(memallocs == NULL) {
    446         goto end; //oops
    447   }
    448 
    449   if(oh.ImageBase < 512*1024*1024) {
    450         flAllocMem = 0;
    451   }
    452   else {
     430        goto end;
     431    }
     432    fConsoleApp = (oh.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_CUI);
     433
     434    // check for high memory support
     435    rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
     436    if (rc == 0 && ulSysinfo > 512)   //VirtualAddresslimit is in MB
     437    {
     438        flAllocMem = PAG_ANY;      // high memory support. Let's use it!
     439    }
     440
     441    //Reserve enough space to store 4096 pointers to 1MB memory chunks
     442    memallocs = (ULONG *)alloca(4096*sizeof(ULONG *));
     443    if(memallocs == NULL) {
     444        goto end; //oops
     445    }
     446
     447    if(oh.ImageBase < 512*1024*1024) {
     448        flAllocMem = 0;
     449    }
     450    else {
    453451        if(flAllocMem == 0) {
    454452            goto end; //no support for > 512 MB
    455         }
    456   }
    457   while(TRUE) {
    458         rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
    459         if(rc) break;
    460 
    461         if(address + FALLOC_SIZE >= oh.ImageBase) {
    462                 if(address > oh.ImageBase) {//we've passed it!
    463                         DosFreeMem((PVOID)address);
    464                         break;
    465                 }
    466                 //found the right address
    467                 DosFreeMem((PVOID)address);
    468 
    469                 diff = oh.ImageBase - address;
    470                 if(diff) {
    471                         rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem);
    472                         if(rc) break;
    473                 }
    474                 rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem);
    475                 if(rc) break;
    476 
    477                 if(diff) DosFreeMem((PVOID)address);
    478 
    479                 reservedMemory = baseAddress;
    480                 break;
    481         }
    482         memallocs[alloccnt++] = address;
    483   }
    484   for(i=0;i<alloccnt;i++) {
    485         DosFreeMem((PVOID)memallocs[i]);
    486   }
    487   ret = TRUE;
     453        }
     454    }
     455    while(TRUE) {
     456        rc = DosAllocMem((PPVOID)&address, FALLOC_SIZE, PAG_READ | flAllocMem);
     457        if(rc) break;
     458
     459        if(address + FALLOC_SIZE >= oh.ImageBase) {
     460            if(address > oh.ImageBase) {//we've passed it!
     461                DosFreeMem((PVOID)address);
     462                break;
     463            }
     464            //found the right address
     465            DosFreeMem((PVOID)address);
     466
     467            diff = oh.ImageBase - address;
     468            if(diff) {
     469                rc = DosAllocMem((PPVOID)&address, diff, PAG_READ | flAllocMem);
     470                if(rc) break;
     471            }
     472            rc = DosAllocMem((PPVOID)&baseAddress, oh.SizeOfImage, PAG_READ | PAG_WRITE | flAllocMem);
     473            if(rc) break;
     474
     475            if(diff) DosFreeMem((PVOID)address);
     476
     477            reservedMemory = baseAddress;
     478            break;
     479        }
     480        memallocs[alloccnt++] = address;
     481    }
     482    for(i=0;i<alloccnt;i++) {
     483        DosFreeMem((PVOID)memallocs[i]);
     484    }
     485    ret = TRUE;
    488486end:
    489   if(dllfile) DosClose(dllfile);
    490   return ret;
     487    if(dllfile) DosClose(dllfile);
     488    return ret;
    491489}
    492490//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.