imgtools/imglib/e32image/e32image.cpp
changeset 590 360bd6b35136
parent 0 044383f39525
equal deleted inserted replaced
588:c7c26511138f 590:360bd6b35136
    17 * These are independent of the original file format from which the
    17 * These are independent of the original file format from which the
    18 * E32Image file was derived.
    18 * E32Image file was derived.
    19 *
    19 *
    20 */
    20 */
    21 
    21 
    22 
    22 #include "h_utl.h"
    23 #include <time.h>
    23 #include <time.h>
    24 #include <malloc.h>
    24 #include <malloc.h>
    25 #include <string.h>
    25 #include <string.h>
    26 #include <stdlib.h>
    26 #include <stdlib.h>
    27 #include "h_utl.h"
    27 
    28 
    28 
    29 
    29 
    30 #if defined (__MSVCDOTNET__) || defined(__TOOLS2__)
    30 
       
    31 #if defined(__TOOLS2__) || defined(__MSVCDOTNET__) 
       
    32 #define IOS_BASE ios_base
    31 #include <fstream>
    33 #include <fstream>
    32 #else //!__MSVCDOTNET__
    34 #else
       
    35 #define IOS_BASE ios
    33 #include <fstream.h>
    36 #include <fstream.h>
    34 #endif //__MSVCDOTNET__
    37 #endif 
    35 
       
    36 #include <assert.h>
    38 #include <assert.h>
    37 #ifndef __LINUX__
    39 #ifndef __LINUX__
    38   #include <io.h>
    40   #include <io.h>
    39 #endif
    41 #endif
    40 #include "h_ver.h"
    42 #include "h_ver.h"
    64 #ifndef __LINUX__
    66 #ifndef __LINUX__
    65 	  , iWideFileName(NULL)
    67 	  , iWideFileName(NULL)
    66 #endif
    68 #endif
    67 , iError(0), iSource(EE32Image), iOrigHdrOffsetAdj(0), iExportBitMap(0), iBPE(aBPE)
    69 , iError(0), iSource(EE32Image), iOrigHdrOffsetAdj(0), iExportBitMap(0), iBPE(aBPE)
    68 	{
    70 	{
    69         }
    71 }
    70 
    72 
    71 E32ImageFile::~E32ImageFile()
    73 E32ImageFile::~E32ImageFile(){
    72 	{
    74 	if(iData){
    73 	free(iData);
    75 		free(iData);
    74 	delete [] iFileName;
    76 		iData = 0 ;
       
    77 	}
       
    78 	if (iHdr && iHdr != iOrigHdr){
       
    79 		delete iHdr;
       
    80 		iHdr = 0 ;
       
    81 	}
       
    82 	if(iExportBitMap){
       
    83 		delete []iExportBitMap;
       
    84 		iExportBitMap = 0 ;
       
    85 	}
       
    86 	if(iFileName){
       
    87 		delete []iFileName ;
       
    88 		iFileName = 0;
       
    89 	}
    75 #ifndef __LINUX__
    90 #ifndef __LINUX__
    76 	delete [] iWideFileName;
    91     if(iWideFileName){
       
    92 		delete []iWideFileName ;
       
    93 		iWideFileName = 0;
       
    94 	}
    77 #endif
    95 #endif
    78 	if (iHdr && iHdr != iOrigHdr)
    96 }
    79 		delete iHdr;
       
    80 	free(iExportBitMap);
       
    81 	}
       
    82 
    97 
    83 // dummy implementation
    98 // dummy implementation
    84 TBool E32ImageFile::Translate(const char*, TUint, TBool, TBool)
    99 TBool E32ImageFile::Translate(const char*, TUint, TBool, TBool)
    85 	{
   100 	{
    86 	return EFalse;
   101 	return EFalse;
   428 // Work out which exports are missing from the export directory
   443 // Work out which exports are missing from the export directory
   429 void E32ImageFile::CreateExportBitMap()
   444 void E32ImageFile::CreateExportBitMap()
   430 	{
   445 	{
   431 	TInt nexp = iOrigHdr->iExportDirCount;
   446 	TInt nexp = iOrigHdr->iExportDirCount;
   432 	TInt memsz = (nexp + 7) >> 3;
   447 	TInt memsz = (nexp + 7) >> 3;
   433 	iExportBitMap = (TUint8*)malloc(memsz);
   448 	iExportBitMap = new TUint8[memsz];
   434 	memset(iExportBitMap, 0xff, memsz);
   449 	memset(iExportBitMap, 0xff, memsz);
   435 	TUint* exports = (TUint*)(iData + iOrigHdr->iExportDirOffset);
   450 	TUint* exports = (TUint*)(iData + iOrigHdr->iExportDirOffset);
   436 	TUint absoluteEntryPoint = iOrigHdr->iEntryPoint + iOrigHdr->iCodeBase;
   451 	TUint absoluteEntryPoint = iOrigHdr->iEntryPoint + iOrigHdr->iCodeBase;
   437 	TUint impfmt = iOrigHdr->ImportFormat();
   452 	TUint impfmt = iOrigHdr->ImportFormat();
   438 	TUint hdrfmt = iOrigHdr->HeaderFormat();
   453 	TUint hdrfmt = iOrigHdr->HeaderFormat();
   695 	}
   710 	}
   696 	aImage.CreateExportBitMap();
   711 	aImage.CreateExportBitMap();
   697 	return is;
   712 	return is;
   698 	}
   713 	}
   699 
   714 
   700 TInt E32ImageFile::IsE32ImageFile(char *aFileName)
   715 TInt E32ImageFile::IsE32ImageFile(const char *aFileName) {
   701 	{
   716  
   702 
   717 	ifstream ifile(aFileName, IOS_BASE::in | IOS_BASE::binary);
   703 #ifdef __LINUX__
   718 	if(!ifile.is_open())
   704 	E32ImageFile f;
       
   705 	struct stat buf;
       
   706 	if (stat(aFileName, &buf) < 0) 
       
   707 		{
       
   708 		return FALSE;
       
   709 		}
       
   710 	f.iFileSize = buf.st_size;
       
   711 #else
       
   712 	_finddata_t fileinfo;
       
   713 	int ret=_findfirst((char *)aFileName,&fileinfo);
       
   714 	if (ret==-1) 
       
   715 		return FALSE;
   719 		return FALSE;
   716 	E32ImageFile f;
   720 	E32ImageFile f;
   717 	f.iFileSize = fileinfo.size;
   721 	ifile.seekg(0,IOS_BASE::end);
   718 #endif
   722 	f.iFileSize = ifile.tellg();
   719 	ifstream ifile(aFileName, ios::in | ios::binary);
   723 	ifile.seekg(0,IOS_BASE::beg);
   720 	if(!ifile.is_open())
       
   721 		return FALSE;
       
   722 	TInt r = f.ReadHeader(ifile);
   724 	TInt r = f.ReadHeader(ifile);
   723 	ifile.close();
   725 	ifile.close();
   724 	return (r == KErrNone);
   726 	return (r == KErrNone);
   725 	}
   727 }
   726 
   728 
   727 TInt E32ImageFile::IsValid()
   729  
   728 	{
       
   729 	return (iError == KErrNone);
       
   730 	}
       
   731 
       
   732 TInt E32ImageFile::Open(const char* aFileName)
       
   733 //
   730 //
   734 // Open an E32 Image file
   731 // Open an E32 Image file
   735 //
   732 //
   736 	{
   733 TInt E32ImageFile::Open(const char* aFileName) {	
   737 #ifdef __LINUX__
   734 	ifstream ifile(aFileName, IOS_BASE::in | IOS_BASE::binary);
   738 	struct stat buf;
   735 	if(!ifile.is_open()) {
   739 	if (stat(aFileName, &buf) < 0) 
       
   740 		{
       
   741 		Print(EError,"Cannot open %s for input.\n",aFileName);
   736 		Print(EError,"Cannot open %s for input.\n",aFileName);
   742 		return 1;
   737 		return 1;
   743 		}
   738 		}
   744 	iFileSize = buf.st_size;
   739 	ifile.seekg(0,IOS_BASE::end);
   745 #else
   740 	iFileSize = ifile.tellg();
   746 	_finddata_t fileinfo;
   741 	ifile.seekg(0,IOS_BASE::beg);
   747 	int ret=_findfirst((char *)aFileName,&fileinfo);
       
   748 	if (ret==-1) 
       
   749 		{
       
   750 		Print(EError,"Cannot open %s for input.\n",aFileName);
       
   751 		return 1;
       
   752 		}
       
   753 	iFileSize = fileinfo.size;
       
   754 #endif
       
   755 	Adjust(iFileSize);
   742 	Adjust(iFileSize);
   756 	ifstream ifile((char *)aFileName, ios::in | ios::binary);
       
   757 	if(!ifile.is_open())
       
   758 		{
       
   759 		Print(EError,"Cannot open %s for input.\n",aFileName);
       
   760 		return 1;
       
   761 		}
       
   762 	ifile >> *this;
   743 	ifile >> *this;
   763 	ifile.close();
   744 	ifile.close();
   764 	if (iError != KErrNone)
   745 	if (iError != KErrNone)
   765 		return iError;
   746 		return iError;
   766 	iFileName=strdup((char *)aFileName);
   747 	size_t len = strlen(aFileName) + 1;
   767 	if (iFileName==NULL)
   748 	iFileName =  new char[len] ;
   768 		return KErrNoMemory;
   749 	memcpy(iFileName ,aFileName,len);
   769 	return KErrNone;
   750 	return KErrNone;
   770 	}
   751 } 
   771 
   752 
   772 #ifndef __LINUX__
   753 #ifndef __LINUX__
   773 TInt E32ImageFile::Open(const wchar_t* aFileName)
   754 TInt E32ImageFile::Open(const wchar_t* aFileName)
   774 //
   755 //
   775 // Open an E32 Image file
   756 // Open an E32 Image file
   776 //
   757 //
   777 	{
   758 	{
   778 	_wfinddata_t fileinfo;
   759 	_wfinddata_t fileinfo;
   779 	int ret=_wfindfirst(aFileName,&fileinfo);
   760 	int ret=_wfindfirst((wchar_t*)aFileName,&fileinfo);
   780 	if (ret==-1) 
   761 	if (ret==-1) 
   781 		{
   762 		{
   782 		Print(EError,"Cannot open %ls for input.\n",aFileName);
   763 		Print(EError,"Cannot open %ls for input.\n",aFileName);
   783 		return 1;
   764 		return 1;
   784 		}
   765 		}
   817 		return KErrNoMemory;
   798 		return KErrNoMemory;
   818 	return KErrNone;
   799 	return KErrNone;
   819 	}
   800 	}
   820 #endif
   801 #endif
   821 
   802 
   822 TUint E32ImageFile::VaOfOrdinal(TUint aOrdinal)
   803 TUint E32ImageFile::VaOfOrdinal(TUint aOrdinal){
   823 // return the offset of the exported symbol
   804 // return the offset of the exported symbol
   824 	{
   805 	
   825 	TUint* exportdir = (TUint*)(iData + iOrigHdr->iExportDirOffset);
   806 	TUint* exportdir = (TUint*)(iData + iOrigHdr->iExportDirOffset);
   826 	return exportdir[aOrdinal-KOrdinalBase];
   807 	return exportdir[aOrdinal-KOrdinalBase];
   827 	}
   808 }
   828 
   809 
   829 // Determine the type of entry point in this module and set the flags
   810 // Determine the type of entry point in this module and set the flags
   830 // in the E32Image header accordingly.
   811 // in the E32Image header accordingly.
   831 TInt E32ImageFile::DetermineEntryPointType()
   812 TInt E32ImageFile::DetermineEntryPointType()
   832 	{
   813 	{