
/*--------------------------------------------------------------------*/
/*                                                                    */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/*                                                                    */
/*--------------------------------------------------------------------*/

#include "os2def.h"

#ifndef WORD
  typedef USHORT  WORD;
  typedef ULONG   DWORD;
#endif

#include "stdio.h"

#define EXE386 1
#include "newexe.h"
#include "exe386.h"

#include "abscons.h"
#include "abstype.h"

#include "abspro.h"
#include "absextn.h"


ULONG OpenLXExe( PMODINFO pMI )
{
  INT           rc = 0;
  INT           LXSize;
  PEXE_HDR      pExe;
  PE32_EXE      pLXH;

  /*------------------------------------------*/
  /* Open input file                          */
  /*------------------------------------------*/

  if (!(pMI->hModFile = fopen(pMI->pModName, "rb" )) )
  {
    rc = errmsg( ERR_BAD_INFILE, SRCFILE, LINENO, pMI->pModName );
    goto OpenLX_Exit;
  }

  /*------------------------------------------*/
  /*                                          */
  /*                                          */
  /*------------------------------------------*/

  if ( !pScrBuf )
  {
    if ( !(pScrBuf=(PBYTE) malloc(PageSize)) )
    {
      rc = errmsg( ERR_BAD_MALLOC, SRCFILE, LINENO, pMI->pModName );
      goto OpenLX_Exit;
    }
  }

  /*------------------------------------------*/
  /* Read Compatibility Header                */
  /*                                          */
  /*------------------------------------------*/

  if ( ReadFileAt( pMI->hModFile, 0L, pScrBuf, sizeof(EXE_HDR) ) )
  {
    rc = errmsg( ERR_BAD_INFILE, SRCFILE, LINENO, pMI->pModName );
    goto OpenLX_Exit;
  }

  pExe = (PEXE_HDR) pScrBuf;

  pMI->LXHdrPos = pExe->e_lfanew;

  /*---------------------------------*/
  /* Position to and read LX header  */
  /*---------------------------------*/

  if ( ReadFileAt( pMI->hModFile, pMI->LXHdrPos, pScrBuf, sizeof(E32_EXE) ) )
  {
    rc = errmsg( ERR_BAD_INFILE, SRCFILE, LINENO, pMI->pModName );
    goto OpenLX_Exit;
  }

  pLXH = (PE32_EXE) pScrBuf;

  if ( pLXH->e32_magic[0] != E32MAGIC1 ||
       pLXH->e32_magic[1] != E32MAGIC2    )
  {
    rc = errmsg( ERR_NOT_LX, SRCFILE, LINENO, pMI->pModName );
    goto OpenLX_Exit;
  }

  /*-----------------------------------------------------*/
  /* Position to and reread LX Header and Loader Section */
  /*-----------------------------------------------------*/

  LXSize = sizeof(E32_EXE) + pLXH->e32_ldrsize + pLXH->e32_fixupsize;

  if( !(pLXH = pMI->pLXHData=(PE32_EXE) malloc( LXSize ) ) )
  {
    rc = errmsg( ERR_BAD_MALLOC, SRCFILE, LINENO, pMI->pModName );
    goto OpenLX_Exit;
  }

  if ( ReadFileAt( pMI->hModFile, pMI->LXHdrPos, pMI->pLXHData, LXSize ) )
  {
    rc = errmsg( ERR_BAD_INFILE, SRCFILE, LINENO, pMI->pModName );
    goto OpenLX_Exit;
  }

OpenLX_Exit:

  return ( rc );
}

/*---------------------------------------*/
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*---------------------------------------*/
ULONG OpenOutFile( PBYTE pFileName )
{
  pOutFileName = pFileName;

  if (!(hOutFile = fopen(pOutFileName, "wb+")) )
  {
    return( 1 );
  }

  return( 0 );
}


/*---------------------------------------*/
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*---------------------------------------*/

ULONG ReadFileAt( FILE *hFile, ULONG FilePos, PVOID pBuf, ULONG ReadLen )
{
  if( fseek( hFile, FilePos, SEEK_SET ) )
  {
    return( 1 );
  }

  if ( (ReadLen != fread( pBuf, sizeof(UCHAR), ReadLen, hFile)) )
  {
    return( 1 );
  }

  return( 0 );
}

/*---------------------------------------*/
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*---------------------------------------*/

ULONG WriteFileAt( FILE *hFile, ULONG FilePos, PVOID pBuf, ULONG WriteLen )
{
  if( fseek( hFile, FilePos, SEEK_SET ) )
  {
    return( 1 );
  }

  if ( (WriteLen != fwrite( pBuf, sizeof(UCHAR), WriteLen, hFile)) )
  {
    return( 1 );
  }

  return( 0 );
}

/*---------------------------------------*/
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*                                       */
/*---------------------------------------*/

ULONG errmsg( ULONG MsgId, PBYTE pSrcFile, ULONG LineNum, PBYTE pFileName )
{
  printf( "ABSLDR: %s(%d): %s",
          pSrcFile,
          LineNum,
          ErrInfo[MsgId].Text     );

  if ( pFileName )
  {
    printf( " - %s", pFileName );
  }

  printf( "\n" );

  return( ErrInfo[MsgId].RetCode );
}




