Ignore:
Timestamp:
Jun 18, 2009, 11:53:26 AM (16 years ago)
Author:
ydario
Message:

Kernel32 updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/hmfile.cpp

    r10564 r21302  
    3535#include "mmap.h"
    3636#include "oslibdos.h"
     37#include <customloader.h>
    3738
    3839#define DBG_LOCALLOG    DBG_hmfile
     
    9798  if (hFile != INVALID_HANDLE_ERROR)
    9899  {
    99         pHMHandleData->dwUserData = (DWORD) new HMFileInfo((LPSTR)lpFileName, lpSecurityAttributes);
     100        pHMHandleData->dwUserData = (DWORD) new HMFileInfo(hFile, (LPSTR)lpFileName, lpSecurityAttributes);
    100101        pHMHandleData->hHMHandle  = hFile;
    101102        return (NO_ERROR);
     
    261262   
    262263        if(hFile != HFILE_ERROR) {
    263             pHMHandleData->dwUserData = (DWORD) new HMFileInfo((LPSTR)lpFileName, NULL);
     264            pHMHandleData->dwUserData = (DWORD) new HMFileInfo(hFile, (LPSTR)lpFileName, NULL);
    264265        }
    265266        return (NO_ERROR);
     
    302303                                        PHMHANDLEDATA pHMSrcHandle,
    303304                                        HANDLE  destprocess,
    304                                         PHANDLE desthandle,
    305305                                        DWORD   fdwAccess,
    306306                                        BOOL    fInherit,
     
    311311 DWORD rc;
    312312
    313   dprintf(("KERNEL32:HMDeviceFileClass::DuplicateHandle (%08x,%08x,%08x,%08x,%08x)",
     313  dprintf(("KERNEL32:HMDeviceFileClass::DuplicateHandle (%08x,%08x,%08x,%08x)",
    314314           pHMHandleData,
    315315           srcprocess,
    316316           pHMSrcHandle->hHMHandle,
    317            destprocess,
    318            desthandle));
     317           destprocess));
    319318
    320319  //TODO: Inheritance of file handles won't work!
     
    345344            memcpy(pHMHandleData, &duphdata, sizeof(duphdata));
    346345
    347             if(fInherit) SetHandleInformation(pHMHandleData, ~HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
     346            SetHandleInformation(pHMHandleData, HANDLE_FLAG_INHERIT, (fInherit) ? HANDLE_FLAG_INHERIT : 0);
    348347
    349348            SetLastError(ERROR_SUCCESS);
     
    360359    }
    361360
    362     rc = OSLibDosDupHandle(pHMSrcHandle->hHMHandle,
    363                            desthandle);
     361    pHMHandleData->hHMHandle = 0;
     362    rc = OSLibDosDupHandle(pHMSrcHandle->hHMHandle, &pHMHandleData->hHMHandle);
    364363    if (rc)
    365364    {
     
    370369    }
    371370    else {
    372       if(fInherit) SetHandleInformation(pHMHandleData, ~HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT);
     371      SetHandleInformation(pHMHandleData, HANDLE_FLAG_INHERIT, (fInherit) ? HANDLE_FLAG_INHERIT : 0);
    373372
    374373      SetLastError(ERROR_SUCCESS);
    375       pHMHandleData->hHMHandle = *desthandle;
    376374      return TRUE;    // OK
    377375    }
     
    620618           nrpages++;
    621619 
    622        map->commitRange((ULONG)lpBuffer, offset & ~0xfff, TRUE, nrpages);
     620       map->commitRange((ULONG)lpBuffer, offset & ~0xfff, FALSE, nrpages);
    623621       map->Release();
    624622  }
     
    844842{
    845843  DWORD ret;
     844  HMFileInfo *fileInfo = (HMFileInfo *)pHMHandleData->dwUserData;
    846845
    847846  dprintfl(("KERNEL32: HMDeviceFileClass::SetFilePointer %s(%08xh,%08xh,%08xh,%08xh)\n",
     
    852851           dwMoveMethod));
    853852
     853  if(fileInfo && fileInfo->dwLXOffset)
     854  {
     855      switch(dwMoveMethod)
     856      {
     857      case FILE_BEGIN:
     858          dprintf(("SetFilePointer FILE_BEGIN (LX) -> change offset from %x to %x", lDistanceToMove, lDistanceToMove+fileInfo->dwLXOffset));
     859          lDistanceToMove += fileInfo->dwLXOffset;
     860          break;
     861
     862      case FILE_END:
     863          //Could overshoot the virtual beginning of the PE file
     864          lDistanceToMove -= MAGIC_STUBEXE_SIZE;
     865          dprintf(("SetFilePointer FILE_END -> might be dangerous!!"));
     866          break;
     867
     868      }
     869  }
    854870  ret = OSLibDosSetFilePointer(pHMHandleData->hHMHandle,
    855871                               lDistanceToMove,
     
    11691185  }
    11701186
    1171   pHMHandleData->dwUserData = (DWORD) new HMFileInfo((LPSTR)lpFileName, lpSecurityAttributes);
     1187  pHMHandleData->dwUserData = (DWORD) new HMFileInfo(0, (LPSTR)lpFileName, lpSecurityAttributes);
    11721188  pHMHandleData->hHMHandle  = 0x8000000;
    11731189  return (NO_ERROR);
     
    12821298  }
    12831299  FindClose(hFind);
     1300
     1301  if(fileInfo->dwLXOffset)
     1302  {
     1303      //subtract the LX header and magic qword from the file size
     1304      dprintf(("GetFileSize (LX) -> change size from %x to %x", finddata.nFileSizeLow, finddata.nFileSizeLow-fileInfo->dwLXOffset-MAGIC_STUBEXE_SIZE));
     1305      finddata.nFileSizeLow -= (fileInfo->dwLXOffset+MAGIC_STUBEXE_SIZE);
     1306  }
    12841307  return finddata.nFileSizeLow;
    12851308}
     
    13291352//******************************************************************************
    13301353//******************************************************************************
    1331 HMFileInfo::HMFileInfo(LPSTR lpszFileName, PVOID lpSecurityAttributes)
     1354HMFileInfo::HMFileInfo(HANDLE hFile, LPSTR lpszFileName, PVOID lpSecurityAttributes)
    13321355{
    13331356  this->lpszFileName = (LPSTR)malloc(strlen(lpszFileName)+1);
     
    13371360  strcpy(this->lpszFileName, lpszFileName);
    13381361  this->lpSecurityAttributes = lpSecurityAttributes;
     1362  dwLXOffset = 0;
     1363
     1364  //Only check files that end with .exe for now; they might be prepended with
     1365  //an LX header. We need to skip that to present the original file to the
     1366  //caller
     1367  if(hFile && !stricmp(lpszFileName + strlen(lpszFileName) - 4, ".EXE"))
     1368  {
     1369      ULONG action, ulRead, signature, ulFileSize;
     1370      ULONG magic[2];
     1371      IMAGE_DOS_HEADER doshdr;
     1372
     1373      //read dos header
     1374      if(!OSLibDosRead(hFile, (LPVOID)&doshdr, sizeof(doshdr), &ulRead)) {
     1375          goto failure;
     1376      }
     1377      if(OSLibDosSetFilePointer(hFile, doshdr.e_lfanew, NULL, FILE_BEGIN) != doshdr.e_lfanew) {
     1378          goto failure;
     1379      }
     1380      //read signature dword
     1381      if(!OSLibDosRead(hFile, (LPVOID)&signature, sizeof(signature), &ulRead)) {
     1382          goto failure;
     1383      }
     1384
     1385      //Make sure it's an LX executable before continueing
     1386      if(doshdr.e_magic != IMAGE_DOS_SIGNATURE || (WORD)signature != IMAGE_OS2_SIGNATURE_LX)
     1387      {
     1388          goto failure;
     1389      }
     1390
     1391      //magic signature located at 8 bytes from file end
     1392      if(OSLibDosSetFilePointer(hFile, -8, NULL, FILE_END) == -1) {
     1393          goto failure;
     1394      }
     1395      //read magic signature + pe offset
     1396      if(!OSLibDosRead(hFile, (LPVOID)&magic[0], sizeof(magic), &ulRead)) {
     1397          goto failure;
     1398      }
     1399      if(magic[0] != MAGIC_STUBEXE_SIGNATURE || magic[1] > ulFileSize) {
     1400          goto failure;
     1401      }
     1402      //this is the offset of the PE image inside our LX executable
     1403      dwLXOffset = magic[1];
     1404
     1405      dprintf(("LX wrapper: PE file %s starts at %x", lpszFileName, dwLXOffset));
     1406      //reset file pointer to PE image start
     1407      if(OSLibDosSetFilePointer(hFile, dwLXOffset, NULL, FILE_BEGIN) != dwLXOffset) {
     1408          DebugInt3();
     1409      }
     1410      goto end;
     1411
     1412failure:
     1413      //reset file pointer to file start
     1414      if(OSLibDosSetFilePointer(hFile, 0, NULL, FILE_BEGIN) != 0) {
     1415          DebugInt3();
     1416      }
     1417  }
     1418end:
     1419  return;
    13391420}
    13401421//******************************************************************************
Note: See TracChangeset for help on using the changeset viewer.