From b0cba9c481fde2124c84be52f9fb2463364975e5 Mon Sep 17 00:00:00 2001 From: jdfiguer Date: Thu, 13 Jul 2023 17:23:14 -0400 Subject: [PATCH] Fix #129, Reduce cyclomatic complexity of GetElfHeader --- elf2cfetbl.c | 57 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/elf2cfetbl.c b/elf2cfetbl.c index 5e8d813..922a937 100644 --- a/elf2cfetbl.c +++ b/elf2cfetbl.c @@ -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); @@ -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) {