From e0ffd268772d6e2a50b885892523449b259c1b53 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 | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/elf2cfetbl.c b/elf2cfetbl.c index 5e8d813..ee23241 100644 --- a/elf2cfetbl.c +++ b/elf2cfetbl.c @@ -59,6 +59,7 @@ int32 GetSrcFilename(void); int32 GetDstFilename(void); int32 OpenSrcFile(void); int32 OpenDstFile(void); +int32 checkELFFileMagicNumber(void); int32 GetElfHeader(void); void SwapElfHeader(void); int32 GetSectionHeader(int32 SectionIndex, union Elf_Shdr *SectionHeader); @@ -1460,6 +1461,18 @@ int32 OpenDstFile(void) return SUCCESS; } +/** + * + */ + +int32 checkELFFileMagicNumber(void) +{ + 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; +} + /** * */ @@ -1493,20 +1506,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) {