Skip to content

Commit

Permalink
Fix #129, Reduce cyclomatic complexity of GetElfHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfiguer authored and jdfiguer committed Jul 13, 2023
1 parent 46b29f8 commit b0cba9c
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions elf2cfetbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1464,24 +1464,48 @@ int32 OpenDstFile(void)
*
*/

int32 GetElfHeader(void)
bool checkEndianness(int32 EndiannessCheck)
{
int32 Status = SUCCESS;
size_t NumHdrsRead = 0;
char VerboseStr[60];
int32 EndiannessCheck = 0x01020304;

if (((char *)&EndiannessCheck)[0] == 0x01)
{
ThisMachineIsLittleEndian = false;
return true;
}
else if (((char *)&EndiannessCheck)[0] != 0x04)
{
printf("Unable to determine endianness of this machine! (0x%02x, 0x%02x, 0x%02x, 0x%02x)\n",
((char *)&EndiannessCheck)[0], ((char *)&EndiannessCheck)[1], ((char *)&EndiannessCheck)[2],
((char *)&EndiannessCheck)[3]);
return FAILED;
return false;
}
return true;
}

/**
*
*/

int32 checkELFFileMagicNumber()
{
if (get_e_ident(&ElfHeader, EI_MAG0) != ELFMAG0 || get_e_ident(&ElfHeader, EI_MAG1) != ELFMAG1 ||
get_e_ident(&ElfHeader, EI_MAG2) != ELFMAG2 || get_e_ident(&ElfHeader, EI_MAG3) != ELFMAG3)
return FAILED;
return SUCCESS;
}

/**
*
*/

int32 GetElfHeader(void)
{
int32 Status = SUCCESS;
size_t NumHdrsRead = 0;
char VerboseStr[60];
int32 EndiannessCheck = 0x01020304;

if (!checkEndianness(EndiannessCheck))
return FAILED;

/* Begin by reading e_ident characters */
NumHdrsRead = fread(&ElfHeader, EI_NIDENT, 1, SrcFileDesc);
Expand All @@ -1493,20 +1517,15 @@ int32 GetElfHeader(void)
}

if (Verbose)
printf("ELF Header:\n");
if (Verbose)
printf(" e_ident[EI_MAG0..3] = 0x%02x,%c%c%c\n", get_e_ident(&ElfHeader, EI_MAG0),
get_e_ident(&ElfHeader, EI_MAG1), get_e_ident(&ElfHeader, EI_MAG2), get_e_ident(&ElfHeader, EI_MAG3));
{
printf("ELF Header:\n"
" e_ident[EI_MAG0..3] = 0x%02x,%c%c%c\n",
get_e_ident(&ElfHeader, EI_MAG0), get_e_ident(&ElfHeader, EI_MAG1), get_e_ident(&ElfHeader, EI_MAG2),
get_e_ident(&ElfHeader, EI_MAG3));
}

/* Verify the ELF file magic number */
if (get_e_ident(&ElfHeader, EI_MAG0) != ELFMAG0)
Status = FAILED;
if (get_e_ident(&ElfHeader, EI_MAG1) != ELFMAG1)
Status = FAILED;
if (get_e_ident(&ElfHeader, EI_MAG2) != ELFMAG2)
Status = FAILED;
if (get_e_ident(&ElfHeader, EI_MAG3) != ELFMAG3)
Status = FAILED;
Status = checkELFFileMagicNumber();

if (Status == FAILED)
{
Expand Down

0 comments on commit b0cba9c

Please sign in to comment.