I'm writing something like Java, and I have problem with pointer (- - ) I have a struct:
struct _lnHeader32
{ unsigned char signature[2]; //LN
unsigned char architecture;
unsigned int length; //Without _lnHeader
unsigned int lnHeaderLength;
unsigned char permissions;
unsigned char typeOfExecutable;
unsigned int flowSegment;
unsigned int dataSegment;
unsigned int loaderSegment;
unsigned int cleanerSegment;
unsigned int errorSegment;
unsigned int exportTable;
unsigned int importTable;
unsigned int authenticationTable; //Encrypt it with GPG.
unsigned int loaderTable;
};
I load executable, which is in little-endian, by using std::fstream:
lnFile.open(argv[1], std::fstream::in | std::fstream::binary);
if (false == lnFile.is_open())
throw (unableToOpen);
lnSize = getFileSize(lnFile);
lnImage = new (std::nothrow) unsigned char [lnSize];
if (0 == lnImage)
throw (noMem);
lnFile.read(reinterpret_cast<char*>(lnImage), lnSize); //#1 Possible mistake?
if (!lnFile)
throw (unableToRead);
lnFile.close();
Then I point _lnHeader32* to allocated lnImage:
lnHeader32 = reinterpret_cast<_lnHeader32*>(lnImage);
At final I print whole struct by two methods:
//Method 1
std::cout << reinterpret_cast<unsigned int*>(lnImage) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+2) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+3) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+7) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+11) << "\n";
std::cout << reinterpret_cast<unsigned int*>(lnImage+12) << "\n\n";
//Method 2
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->signature) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->architecture) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->length) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->lnHeaderLength) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->permissions) << "\n";
std::cout << reinterpret_cast<unsigned int*>(&lnHeader32->typeOfExecutable)
<< "\n\n";
It gives Me output like:
0xe8b260
0xe8b262
0xe8b263 <---
0xe8b267
0xe8b26b
0xe8b26c
0xe8b260
0xe8b262
0xe8b264 <--- Should be 0xe8b263 | Here starts problem
0xe8b268
0xe8b26c
0xe8b26d
It prints well lnHeader32's fields by using first method, but I prefer to use second method. I calculated everything few times. Why it is happening? Executable is generated by compiler done in perl, has it any influence?
if( false == data.isOpen() )Arghhh, thats the most horrible boolean condition I have ever seen