From 79234f9ed9e12eb00bea5c2f00f27a89d33be1e6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 20 Feb 2017 16:53:03 +0100 Subject: [PATCH] trimmed hexdump.c down --- nm-otool/includes/ft_nm_otool.h | 4 +- nm-otool/src/ft_nm.c | 2 +- nm-otool/src/ft_otool.c | 9 +++-- nm-otool/src/hexdump.c | 71 +++------------------------------ 4 files changed, 14 insertions(+), 72 deletions(-) diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index 909db434..fabf0927 100644 --- a/nm-otool/includes/ft_nm_otool.h +++ b/nm-otool/includes/ft_nm_otool.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:36:10 by jhalford #+# #+# */ -/* Updated: 2017/02/20 15:33:32 by jhalford ### ########.fr */ +/* Updated: 2017/02/20 16:10:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,6 @@ # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) void dump_symtab(struct symtab_command *sym, void *file); -void *hexdump(void *addr, unsigned int size, int option); +void *hexdump(void *addr, unsigned int offset, unsigned int size); #endif diff --git a/nm-otool/src/ft_nm.c b/nm-otool/src/ft_nm.c index c6a4b519..7c65ba42 100644 --- a/nm-otool/src/ft_nm.c +++ b/nm-otool/src/ft_nm.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ -/* Updated: 2017/02/20 15:18:30 by jhalford ### ########.fr */ +/* Updated: 2017/02/20 16:45:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/src/ft_otool.c b/nm-otool/src/ft_otool.c index 554de701..e164d8b6 100644 --- a/nm-otool/src/ft_otool.c +++ b/nm-otool/src/ft_otool.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:08:14 by jhalford #+# #+# */ -/* Updated: 2017/02/20 15:27:00 by jhalford ### ########.fr */ +/* Updated: 2017/02/20 16:27:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,15 +46,16 @@ void otool(void *file) int is_64 = IS_MAGIC_64(magic); struct section_64 *sect; - sect = NULL; if (is_fat) ft_printf("fat binary: not supported yet.\n"); else if (is_64) + { sect = get_text_section(file); + ft_printf("Contents of (__TEXT,__text) section\n"); + hexdump(file, sect->offset, sect->size); + } else ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic); - ft_printf("Contents of (__TEXT,__text) section\n"); - hexdump(file + sect->offset, sect->size, 0); } int main(int ac, char **av) diff --git a/nm-otool/src/hexdump.c b/nm-otool/src/hexdump.c index de67eaac..ca6c12c3 100644 --- a/nm-otool/src/hexdump.c +++ b/nm-otool/src/hexdump.c @@ -6,101 +6,42 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 15:14:33 by jhalford #+# #+# */ -/* Updated: 2017/02/20 15:47:08 by jhalford ### ########.fr */ +/* Updated: 2017/02/20 16:52:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -void ft_putnbr_base16(int nbr, int format) -{ - int base_size; - int i; - int result[100]; - - base_size = 16; - i = 0; - if (base_size > 1) - { - if (nbr < 0) - { - ft_putchar('-'); - nbr = -nbr; - } - while (nbr) - { - result[i] = nbr % base_size; - nbr = nbr / base_size; - i++; - } - while (format-- > i) - ft_putchar('0'); - while (--i >= 0) - ft_putchar("0123456789abcdef"[result[i]]); - } -} - -static void print_contents(void *addr, unsigned int size) +static void print_hex_contents(void *addr, unsigned int size) { void *a; a = addr; - ft_putstr(" |"); while (a - addr < 16) { if ((a - addr) >= size) break ; - else if (*(char *)a < 32 || *(char *)a > 126) - ft_putchar('.'); else - ft_putchar(*(char *)a); - a++; - } - ft_putchar('|'); -} - -static void print_hex_contents(void *addr, unsigned int size, int option) -{ - void *a; - - a = addr; - (void)option; - while (a - addr < 16) - { - if ((a - addr) >= size) - break ; - if (option && FT_IS_DIV((a - addr), 8)) - ft_putchar(' '); - else - /* ft_putnbr_base16(*(unsigned char *)a, 2); */ ft_printf("%02x", *(unsigned char*)a); ft_putchar(' '); a++; } } -void *hexdump(void *addr, unsigned int size, int option) +void *hexdump(void *addr, unsigned int offset, unsigned int size) { void *a; + addr += offset; a = addr; if (addr == NULL) return (addr); while ((a - addr) < size) { - /* ft_putnbr_base16((int)(a - addr), 7 + option); */ - ft_printf("%0*x\t", 16, (a - addr)); - print_hex_contents(a, (size - (a - addr)), option); - if (option) - print_contents(a, (size - (a - addr))); + ft_printf("%0*llx\t", 16, (a - addr) + (unsigned long)offset); + print_hex_contents(a, (size - (a - addr))); ft_putchar('\n'); a += 16; - /* if (!ft_strncmp((char *)a, (char *)(a - 16), 16)) */ - /* { */ - /* ft_putstr("*\n"); */ - /* while (!ft_strncmp((char *)a, (char *)(a - 16), 16)) */ - /* a += 16; */ - /* } */ } return (addr); }