

#include <stdio.h>                     // Need this for fopen(..) fwrite(..) etc

// We use this simple function to write data to our file.
void fileout(char *str)
{
      FILE *fp;
      fp=fopen("output.txt", "a+");   // a+ -> if file doesn't exist create it, else
      fprintf(fp, "%s", str);         //       append to the end of the file
      fclose(fp); 
}// End fileout(..)

long FileSize( char * szFileName )
{
	FILE *file = fopen(szFileName, "rb"); // Open the file
	fseek(file, 0, SEEK_END);             // Seek to the end
	long file_size = ftell(file);         // Get the current position
	fclose(file);
	
	return file_size;                     // We have the file size, so return it
}// End FileSize(..)



//Program Entry Point
void main(int argc, char* argv[])
{
	char szFileName[] = "default.xbe";    // Our input xbe
	char buf[500];                        // Large temp char buffer

	long iFileSize = FileSize(szFileName);

	sprintf(buf, "File: %s - xbe filesize: %d bytes\n\n", szFileName, iFileSize);
	fileout(buf);

	// Lets allocate enough memory for the whole xbe and read it all in
	unsigned char * pXBE = new unsigned char[iFileSize];

	// Open our xbe file
	FILE* fp = fopen( szFileName, "r" );
	fseek(fp, 0, SEEK_SET);

	// Read all the contents into our allocated memory 
	fread(pXBE, iFileSize, 1, fp);

	// Close our file.
	fclose( fp );

	int iOffset = 0;
	//------------------------Our XBE Analysis Code-------------------------//

	// Write out the first 3 char's of the xbe to see what they are
	sprintf(buf, "Sig (0x4) pXBE[0..3] = %c%c%c%c\n", 
		    pXBE[iOffset+0], pXBE[iOffset+1], pXBE[iOffset+2], pXBE[iOffset+3] );
	fileout(buf);
	iOffset += 4; // Move along 4 bytes.

	// Skip the 0x100 bytes that are the authentication signature
	fileout("Authentication Signature (0x100)\n");
	iOffset +=0x100;

	// Lets take a looksy at what the next piece of juicy info is...the base
	// address
	// We need this next line, as if we just use pXBE[iOffset] it will give us
	// a byte...big endian...so our 0x0...if we have 0x00100000...so if we
	// cast the pointer to an unsigned int, we get a 4 bytes...which is what we want
	unsigned int * ptr = (unsigned int*)&pXBE[iOffset];
	sprintf(buf, "Base Address (0x4) :  0x%08X\n", *ptr);
	fileout(buf);
	iOffset += 0x4;

	// Goodbye

	// Remember, before exiting the program, release the memory we allcoated for 
	// the xbe data we read in
	delete[] pXBE;

}// End main(..)









