Changeset 21943 for trunk/src


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

Remove evil goto and code duplication in PE/PEC.

This finalizes the major code cleanup started in r21942.

File:
1 edited

Legend:

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

    r21942 r21943  
    4242char szInteralErrorMsg1[]= "Internal Error";
    4343char szNoKernel32Msg[]  = "Can't load/find kernel32.dll (rc=%d, module %s)";
     44char szBadKernel32Msg[] = "Invalid or outdated kernel32.dll";
    4445char szErrorExports[]   = "Unable to process exports of %s";
    4546char szErrorMemory[]    = "Memory allocation failure while loading %s";
     
    238239            snprintf(szErrorMsg, sizeof(szErrorMsg), szFileNotFound, exeName);
    239240            pszErrorMsg = szErrorMsg;
    240             goto failerror;
    241         }
    242 
    243         AllocateExeMem(exeName, &fIsNEExe);
    244 
    245         if (fIsNEExe)
    246         {
    247             STARTDATA sdata = {0};
    248             ULONG idSession;
    249             PID   pid;
    250 
    251             sdata.Length    = sizeof(sdata);
    252             sdata.PgmName   = (PSZ)"w16odin.exe";
    253             strcpy(fullpath, exeName);
    254             strcat(fullpath, " ");
    255             strcat(fullpath, win32cmdline);
    256             sdata.PgmInputs = fullpath;
    257             sdata.FgBg      = SSF_FGBG_FORE;
    258             sdata.SessionType = SSF_TYPE_WINDOWEDVDM;
    259             rc = DosStartSession(&sdata, &idSession, &pid);
    260             if (rc)
     241        }
     242    }
     243    else
     244    {
     245        // No command line, show the usage message
     246        pszErrorMsg = INFO_BANNER;
     247    }
     248
     249    if (!pszErrorMsg)
     250    {
     251        if (AllocateExeMem(exeName, &fIsNEExe))
     252        {
     253            if (fIsNEExe)
    261254            {
    262                 snprintf(szErrorMsg, sizeof(szErrorMsg), szErrDosStartSession, sdata.PgmName, exeName, rc);
    263                 pszErrorMsg = szErrorMsg;
    264                 goto failerror;
    265             }
    266             return 0;
    267         }
    268     }
    269 
     255                STARTDATA sdata = {0};
     256                ULONG idSession;
     257                PID   pid;
     258
     259                sdata.Length    = sizeof(sdata);
     260                sdata.PgmName   = (PSZ)"w16odin.exe";
     261                strcpy(fullpath, exeName);
     262                strcat(fullpath, " ");
     263                strcat(fullpath, win32cmdline);
     264                sdata.PgmInputs = fullpath;
     265                sdata.FgBg      = SSF_FGBG_FORE;
     266                sdata.SessionType = SSF_TYPE_WINDOWEDVDM;
     267                rc = DosStartSession(&sdata, &idSession, &pid);
     268                if (rc)
     269                {
     270                    snprintf(szErrorMsg, sizeof(szErrorMsg), szErrDosStartSession, sdata.PgmName, exeName, rc);
     271                    pszErrorMsg = szErrorMsg;
     272                }
     273                else
     274                {
     275                    // we don't need to do anything else, return shortly
     276                    DBG(("PE: END (returning 0)\n"));
     277                    return 0;
     278                }
     279            }
     280        }
     281        else
     282        {
     283            snprintf(szErrorMsg, sizeof(szErrorMsg), szErrorMemory, exeName);
     284            pszErrorMsg = szErrorMsg;
     285        }
     286    }
     287
     288    // Resolve PM functions (needed to show the pszErrorMsg box too)
    270289    rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN", &hmodPMWin);
    271290    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
     
    275294    rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
    276295
    277     if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */
    278         goto fail;
    279 
    280     hmq = MyWinCreateMsgQueue(hab, 0);
    281 
    282     if (!cmdline) {
    283         MyWinMessageBox(HWND_DESKTOP, NULLHANDLE, INFO_BANNER, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    284         goto fail;
    285     }
    286 
    287     errorMod[0] = 0;
    288     rc = DosLoadModule(errorMod, sizeof(errorMod), "KERNEL32", &hmodKernel32);
    289     if(rc) {
    290         snprintf(fullpath, CCHMAXPATH, szNoKernel32Msg, rc, errorMod);
    291         MyWinMessageBox(HWND_DESKTOP, NULLHANDLE, fullpath, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    292         goto fail;
    293     }
    294     rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@36", (PFN *)&CreateWin32Exe);
    295 
     296    if ((hab = MyWinInitialize(0)) &&
     297        (hmq = MyWinCreateMsgQueue(hab, 0)))
     298    {
     299        if (!pszErrorMsg)
     300        {
     301            errorMod[0] = 0;
     302            rc = DosLoadModule(errorMod, sizeof(errorMod), "KERNEL32", &hmodKernel32);
     303            if (rc)
     304            {
     305                snprintf(szErrorMsg, sizeof(szErrorMsg), szNoKernel32Msg, rc, errorMod);
     306                pszErrorMsg = szErrorMsg;
     307            }
     308            else
     309            {
     310                rc = DosQueryProcAddr(hmodKernel32, 0, "_CreateWin32PeLdrExe@36", (PFN *)&CreateWin32Exe);
     311                if (rc)
     312                {
     313                    snprintf(szErrorMsg, sizeof(szErrorMsg), szBadKernel32Msg);
     314                    pszErrorMsg = szErrorMsg;
     315                }
     316            }
     317
     318            if (!pszErrorMsg)
     319            {
    296320#ifdef COMMAND_LINE_VERSION
    297     fVioConsole = TRUE;
     321                fVioConsole = TRUE;
    298322#else
    299     fVioConsole = FALSE;
     323                fVioConsole = FALSE;
    300324#endif
    301     rc = CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, 0,
    302                         fConsoleApp, fVioConsole, errorMod, sizeof(errorMod));
    303     if(rc != LDRERROR_SUCCESS)
    304     {
    305         switch(rc) {
    306         case LDRERROR_INVALID_MODULE:
    307             snprintf(szErrorMsg, sizeof(szErrorMsg), szLoadErrorMsg, exeName);
    308             break;
    309         case LDRERROR_INVALID_CPU:
    310             snprintf(szErrorMsg, sizeof(szErrorMsg), szCPUErrorMsg, exeName);
    311             break;
    312         case LDRERROR_FILE_SYSTEM:
    313             snprintf(szErrorMsg, sizeof(szErrorMsg), szExeErrorMsg, exeName);
    314             break;
    315         case LDRERROR_MEMORY:
    316             snprintf(szErrorMsg, sizeof(szErrorMsg), szErrorMemory, exeName);
    317             break;
    318         case LDRERROR_EXPORTS:
    319             snprintf(szErrorMsg, sizeof(szErrorMsg), szErrorExports, exeName);
    320             break;
    321         case LDRERROR_IMPORTS:
    322             snprintf(szErrorMsg, sizeof(szErrorMsg), szErrorImports, exeName, errorMod);
    323             break;
    324         case LDRERROR_INVALID_SECTION:
    325         default:
    326             snprintf(szErrorMsg, sizeof(szErrorMsg), szInteralErrorMsg, exeName);
    327             break;
    328         }
    329 
    330         MyWinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    331         goto fail;
    332     }
    333 
    334     if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
    335     MyWinTerminate( hab );                   /* Terminate the application    */
    336 
    337     DosFreeModule(hmodPMWin);
    338     DosFreeModule(hmodKernel32);
    339 
    340     DBG(("PE: END (returning 0)\n"));
    341     return 0;
    342 
    343 fail:
    344     if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
    345     if(hab) MyWinTerminate( hab );                   /* Terminate the application    */
    346 
    347     if(hmodPMWin)       DosFreeModule(hmodPMWin);
    348     if(hmodKernel32)    DosFreeModule(hmodKernel32);
    349 
    350     DBG(("PE: END (returning 1)\n"));
    351     return(1);
    352 
    353 failerror:
    354     rc = DosLoadModule(exeName, sizeof(exeName), "PMWIN", &hmodPMWin);
    355     rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32INITIALIZE, NULL, (PFN *)&MyWinInitialize);
    356     rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32TERMINATE, NULL, (PFN *)&MyWinTerminate);
    357     rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32CREATEMSGQUEUE, NULL, (PFN *)&MyWinCreateMsgQueue);
    358     rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32DESTROYMSGQUEUE, NULL, (PFN *)&MyWinDestroyMsgQueue);
    359     rc = DosQueryProcAddr(hmodPMWin, ORD_WIN32MESSAGEBOX, NULL, (PFN *)&MyWinMessageBox);
    360     if(rc == 0) {
    361         if ((hab = MyWinInitialize(0)) == 0L) /* Initialize PM     */
    362             goto fail;
    363 
    364         hmq = MyWinCreateMsgQueue(hab, 0);
    365 
    366         MyWinMessageBox(HWND_DESKTOP, NULLHANDLE, pszErrorMsg, szErrorTitle, 0, MB_OK | MB_ERROR | MB_MOVEABLE);
    367     }
    368     if(hmq) MyWinDestroyMsgQueue( hmq );             /* Tidy up...                   */
    369     if(hab) MyWinTerminate( hab );                   /* Terminate the application    */
    370     if(hmodPMWin)       DosFreeModule(hmodPMWin);
    371 
    372     DBG(("PE: END (returning 1)\n"));
    373     return 1;
     325                DBG(("PE: fVioConsole: %d\n", fVioConsole));
     326
     327                rc = CreateWin32Exe(exeName, win32cmdline, peoptions, reservedMemory, 0,
     328                                    fConsoleApp, fVioConsole, errorMod, sizeof(errorMod));
     329                if (rc != LDRERROR_SUCCESS)
     330                {
     331                    switch (rc)
     332                    {
     333                    case LDRERROR_INVALID_MODULE:
     334                        snprintf(szErrorMsg, sizeof(szErrorMsg), szLoadErrorMsg, exeName);
     335                        break;
     336                    case LDRERROR_INVALID_CPU:
     337                        snprintf(szErrorMsg, sizeof(szErrorMsg), szCPUErrorMsg, exeName);
     338                        break;
     339                    case LDRERROR_FILE_SYSTEM:
     340                        snprintf(szErrorMsg, sizeof(szErrorMsg), szExeErrorMsg, exeName);
     341                        break;
     342                    case LDRERROR_MEMORY:
     343                        snprintf(szErrorMsg, sizeof(szErrorMsg), szErrorMemory, exeName);
     344                        break;
     345                    case LDRERROR_EXPORTS:
     346                        snprintf(szErrorMsg, sizeof(szErrorMsg), szErrorExports, exeName);
     347                        break;
     348                    case LDRERROR_IMPORTS:
     349                        snprintf(szErrorMsg, sizeof(szErrorMsg), szErrorImports, exeName, errorMod);
     350                        break;
     351                    case LDRERROR_INVALID_SECTION:
     352                    default:
     353                        snprintf(szErrorMsg, sizeof(szErrorMsg), szInteralErrorMsg, exeName);
     354                        break;
     355                    }
     356
     357                    pszErrorMsg = szErrorMsg;
     358                }
     359            }
     360        }
     361
     362        if (pszErrorMsg)
     363            MyWinMessageBox(HWND_DESKTOP, NULLHANDLE, pszErrorMsg, szErrorTitle,
     364                            0, MB_OK | MB_ERROR | MB_MOVEABLE);
     365    }
     366    else
     367    {
     368        DBG(("PE: WinInitialize/WinCreateMsgQueue failed!\n"));
     369    }
     370
     371    if(hmodKernel32)
     372        DosFreeModule(hmodKernel32);
     373
     374    if (hmq)
     375        MyWinDestroyMsgQueue(hmq);
     376    if (hab)
     377        MyWinTerminate(hab);
     378
     379    if(hmodPMWin)
     380        DosFreeModule(hmodPMWin);
     381
     382#ifdef DEBUG
     383    if (pszErrorMsg)
     384        DBG(("PE: Error: '%s'\n", pszErrorMsg));
     385#endif
     386
     387    DBG(("PE: END (returning %d)\n", pszErrorMsg ? 1 : 0));
     388    return pszErrorMsg ? 1 : 0;
    374389}
     390
    375391//******************************************************************************
    376392//SvL: Reserve memory for win32 exes without fixups
Note: See TracChangeset for help on using the changeset viewer.