trimmed hexdump.c down

This commit is contained in:
Jack Halford 2017-02-20 16:53:03 +01:00
parent d8fe7600a2
commit 79234f9ed9
4 changed files with 14 additions and 72 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:36:10 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) # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM)
void dump_symtab(struct symtab_command *sym, void *file); 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 #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 03:09:12 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:08:14 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); int is_64 = IS_MAGIC_64(magic);
struct section_64 *sect; struct section_64 *sect;
sect = NULL;
if (is_fat) if (is_fat)
ft_printf("fat binary: not supported yet.\n"); ft_printf("fat binary: not supported yet.\n");
else if (is_64) else if (is_64)
{
sect = get_text_section(file); sect = get_text_section(file);
ft_printf("Contents of (__TEXT,__text) section\n");
hexdump(file, sect->offset, sect->size);
}
else else
ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic); 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) int main(int ac, char **av)

View file

@ -6,101 +6,42 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 15:14:33 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" #include "ft_nm_otool.h"
void ft_putnbr_base16(int nbr, int format) static void print_hex_contents(void *addr, unsigned int size)
{
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)
{ {
void *a; void *a;
a = addr; a = addr;
ft_putstr(" |");
while (a - addr < 16) while (a - addr < 16)
{ {
if ((a - addr) >= size) if ((a - addr) >= size)
break ; break ;
else if (*(char *)a < 32 || *(char *)a > 126)
ft_putchar('.');
else 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_printf("%02x", *(unsigned char*)a);
ft_putchar(' '); ft_putchar(' ');
a++; a++;
} }
} }
void *hexdump(void *addr, unsigned int size, int option) void *hexdump(void *addr, unsigned int offset, unsigned int size)
{ {
void *a; void *a;
addr += offset;
a = addr; a = addr;
if (addr == NULL) if (addr == NULL)
return (addr); return (addr);
while ((a - addr) < size) while ((a - addr) < size)
{ {
/* ft_putnbr_base16((int)(a - addr), 7 + option); */ ft_printf("%0*llx\t", 16, (a - addr) + (unsigned long)offset);
ft_printf("%0*x\t", 16, (a - addr)); print_hex_contents(a, (size - (a - addr)));
print_hex_contents(a, (size - (a - addr)), option);
if (option)
print_contents(a, (size - (a - addr)));
ft_putchar('\n'); ft_putchar('\n');
a += 16; 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); return (addr);
} }