From 09f10018a7d1981bdf2e66d9c752b12482a8b22c Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Sat, 2 Jul 2016 22:44:54 +0100 Subject: Added h64e --- Makefile | 2 +- h64e/Makefile | 4 + h64e/core.c | 562 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ h64e/core.h | 79 +++++++++ ihe.h | 2 + 5 files changed, 648 insertions(+), 1 deletion(-) create mode 100644 h64e/Makefile create mode 100644 h64e/core.c create mode 100644 h64e/core.h diff --git a/Makefile b/Makefile index 444323d..bef2096 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS=-I./ SOURCES=buf.c core.c SOURCES+=$(wildcard cmd/*.c) OBJECTS=$(SOURCES:.c=.o) -LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o +LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o h64e/core.o all: clean $(OBJECTS) $(PROJECT) diff --git a/h64e/Makefile b/h64e/Makefile new file mode 100644 index 0000000..074800b --- /dev/null +++ b/h64e/Makefile @@ -0,0 +1,4 @@ +CC=gcc +CFLAGS= +make: + $(CC) -c core.c \ No newline at end of file diff --git a/h64e/core.c b/h64e/core.c new file mode 100644 index 0000000..8a8e2b0 --- /dev/null +++ b/h64e/core.c @@ -0,0 +1,562 @@ +#include "core.h" + + +int h64e_set_default( h64e_t *fmt ) +{ + if (NULL == fmt) + { + return -1; + } + + fmt->flag_offset = 0; + fmt->offset_addr = 0; + fmt->column_size = 16; + fmt->flag_no_group = 0; + fmt->group = H64E_G_BYTE; + fmt->flag_output = 0; + fmt->output_type = H64E_O_NONE; + + return 0; +} + +int h64e_check_param( h64e_t *fmt ) +{ + /* check NULL thats wrong */ + if (fmt == NULL) + { + printf("Checked param is null\n"); + return 0; + } + + if (fmt->flag_output) + { + /* groupint and param shoudl match by size */ + if (h64e_transform_sz(fmt)!=h64e_outpt_sz(fmt)) + { + printf("Hex grouping and output type should match\n"); + return 0; + } + + /* if output type sec correctly */ + if ((fmt->output_typeoutput_type>H64E_O_END)) + { + printf("Unknown output type\n"); + return 0; + } + } + + return 1; +} + + +int h64e_output_line( h64e_t *fmt, uint8_t *buf, size_t size ) +{ + int sz; + int err; + + if (fmt == NULL || buf == NULL) + { + printf("NULL arguments\n"); + return -1; + } + + if (fmt->column_size != size) + { + printf("Column size value wrong\n"); + return -1; + } + + if (fmt->flag_offset) + { + printf("%08x: ", fmt->offset_addr ); + } + + sz = h64e_transform_sz( fmt ); + err = h64e_outputf( fmt, buf, size/sz ); + if (err != 0) + { + printf("Cannot output buffer\n"); + return -1; + } + + if (fmt->flag_offset) + { + fmt->offset_addr += size; + } + + return 0; +} + +int h64e_output_buffer( h64e_t *fmt, uint8_t *buf, size_t size) +{ + int i; + uint8_t clmn; + + if (fmt == NULL || buf == NULL) + { + printf("fmt or buf is NULL\n"); + return -1; + } + + clmn = fmt->column_size; + i = 0; + do + { + + if (i+clmn < size) + { + h64e_output_line( fmt, buf+i, clmn ); + } else + { + if (fmt->flag_offset) + printf("%08x: ", fmt->offset_addr); + + h64e_outputf_padding( fmt, buf+i, (size-i), fmt->column_size ); + } + i += clmn; + } while (igroup) + { + case H64E_G_BYTE: + return 1; + case H64E_G_WORD: + return 2; + case H64E_G_DWORD: + return 4; + case H64E_G_QWORD: + return 8; + default: + printf("Unknown transform type\n"); + return -1; + } + return 1; +} + +int h64e_outpt_sz( h64e_t *fmt ) +{ + if (fmt == NULL) + { + return -1; + } + + switch(fmt->output_type) + { + case H64E_O_NONE: + printf("Output type unset\n"); + return -1; + case H64E_O_STRING: + case H64E_O_INT8: + case H64E_O_UINT8: + return 1; + case H64E_O_INT16: + case H64E_O_UINT16: + return 2; + case H64E_O_INT32: + case H64E_O_UINT32: + return 4; + case H64E_O_INT64: + case H64E_O_UINT64: + return 8; + default: + printf("Unknown output type\n"); + return -1; + } + + return 1; +} + +int h64e_space_sz( h64e_t *fmt ) +{ + if (fmt == NULL) + { + return -1; + } + + switch(fmt->group) + { + case H64E_G_BYTE: + return 2; + case H64E_G_WORD: + return 4; + case H64E_G_DWORD: + return 8; + case H64E_G_QWORD: + return 16; + default: + printf("Unknown group type\n"); + return 1; + } + + return -1; +} + +/* number of transform limbs there */ +int h64e_outputf( h64e_t *fmt, uint8_t *buf, size_t nsize ) +{ + int i; + int tsz,osz; + trnf_clb tclb = NULL; + output_clb oclb = NULL; + + if (fmt == NULL) + { + return -1; + } + + /* output grouped hex values */ + tsz = h64e_transform_sz( fmt ); + tclb = h64e_trnf_fun( fmt ); + + if (!fmt->flag_no_group) + { + if (!tclb) + { + printf("Couldnt register callback\n"); + return -1; + } + for (i=0; iflag_output ) + { + osz = h64e_outpt_sz( fmt ); + oclb = h64e_outpt_fun( fmt ); + if (fmt->flag_output) + { + if (!oclb) + { + printf("Couldnt register callback\n"); + return -1; + } + for (i=0;igroup; + switch (g) + { + case H64E_G_BYTE: + return trnf_byte; + case H64E_G_WORD: + return trnf_word; + case H64E_G_DWORD: + return trnf_dword; + case H64E_G_QWORD: + return trnf_qword; + default: + printf("Unsuported transformation func type\n"); + return NULL; + } + + return NULL; +} + +output_clb h64e_outpt_fun( h64e_t *fmt ) +{ + if (fmt == NULL) + { + return NULL; + } + + switch (fmt->output_type) + { + case H64E_O_STRING: + return outpt_str; + case H64E_O_INT8: + return outpt_int8; + case H64E_O_UINT8: + return outpt_uint8; + case H64E_O_INT16: + return outpt_int16; + case H64E_O_UINT16: + return outpt_uint16; + case H64E_O_INT32: + return outpt_int32; + case H64E_O_UINT32: + return outpt_uint32; + case H64E_O_INT64: + return outpt_int64; + case H64E_O_UINT64: + return outpt_uint64; + case H64E_O_NONE: + return NULL; + default: + printf("Unknown output format\n"); + return NULL; + } + + return NULL; +} + +/* +when group have reminder deal with reminder in normal way +nsize should be allways smaller then reminder +*/ +int h64e_outputf_padding( h64e_t *fmt, uint8_t *buf, size_t nsize, int column ) +{ + #define STATE_GROUP 1 + #define STATE_SPACE 2 + #define STATE_END 3 + int i,j,acc_i,acc_j,run,state; + int tsz,osz,spacesz; + int group=0,space=0,sgroup=0; + trnf_clb tclb = NULL; + output_clb oclb = NULL; + unsigned char str[128]; //could blow in future + + if (fmt == NULL) + { + return -1; + } + + /* output grouped hex values */ + tsz = h64e_transform_sz( fmt ); + spacesz = h64e_space_sz( fmt ); + tclb = h64e_trnf_fun( fmt ); + + if (!fmt->flag_no_group) + { + if (!tclb) + { + printf("Couldnt register callback\n"); + return -1; + } + + /* double counter */ + i = 0; //count total + j = 0; acc_j=0;//count columns + run = 1; + state = STATE_GROUP; + while (run) + { + switch (state) + { + case STATE_GROUP: + if (acc_jflag_output) + { + osz = h64e_outpt_sz( fmt ); + oclb = h64e_outpt_fun( fmt ); + if (fmt->flag_output) + { + if (!oclb) + { + printf("Couldnt register callback\n"); + return -1; + } + for (i=0;i<(nsize/tsz);i++) + { + oclb(buf+i*osz); + } + if ((nsize%tsz) != 0) + { + memset(str,0x00,sizeof(str)); + memcpy(str+tsz-(nsize%tsz), buf+i*osz, nsize%tsz); //dirty calc + oclb(str); + } + } + } + + printf("\n"); + + return 0; +} + +int trnf_byte( char *buf ) +{ + uint8_t *u = buf; + + printf("%02x", u[0]); + + return 0; +} + +int trnf_word( char *buf ) +{ + uint8_t *u = buf; + + printf("%02x%02x", u[0], u[1]); + + return 0; +} + +int trnf_dword( char *buf ) +{ + uint8_t *u = buf; + + printf("%02x%02x%02x%02x", u[0], u[1], u[2], u[3]); + + return 0; +} + +int trnf_qword( char *buf ) +{ + uint8_t *u = buf; + + printf("%02x%02x%02x%02x%02x%02x%02x%02x", u[0], u[1], u[2], u[3], u[4], u[5], u[6], u[7]); + + return 0; +} + +int outpt_str( char *buf ) +{ + uint8_t *s=buf; + + if ( isprint(*s) ) + { + printf("%c",*s); + } else + { + printf("."); + } + + return 0; +} + + +int outpt_int8( char *buf ) +{ + int8_t *i8=buf; + + printf("%4i",*i8); + + return 0; +} + + +int outpt_uint8( char *buf ) +{ + uint8_t *u8=buf; + + printf("%3d",*u8); + + return 0; +} + + +int outpt_int16( char *buf ) +{ + int16_t *i16 = (int16_t *)buf; + + printf("%6hd ",*i16); + + return 0; +} + + +int outpt_uint16( char *buf ) +{ + uint16_t *u16 = (uint16_t *)buf; + + printf("%5hu ",*u16); + + return 0; +} + +int outpt_int32( char *buf ) +{ + int32_t *i32 = (int32_t *)buf; + + printf("%9d ",*i32); + + return 0; +} + +int outpt_uint32( char *buf ) +{ + uint32_t *u32 = (uint32_t *)buf; + + printf("%8u ",*u32); + + return 0; +} + +int outpt_int64( char *buf ) +{ + int64_t *i64 = (int64_t *)buf; + + printf("%lld ",*i64); + + return 0; +} + +int outpt_uint64( char *buf ) +{ + uint64_t *u64 = (uint64_t *)buf; + + printf("%llu ",*u64); + + return 0; +} diff --git a/h64e/core.h b/h64e/core.h new file mode 100644 index 0000000..703c3aa --- /dev/null +++ b/h64e/core.h @@ -0,0 +1,79 @@ +#ifndef __H64E_CORE_H +#define __H64E_CORE_H + +#include +#include +#include +#include +#include + +#define H64E_G_NONE 0 +#define H64E_G_BYTE 1 +#define H64E_G_WORD 2 +#define H64E_G_DWORD 3 +#define H64E_G_QWORD 4 + + +#define H64E_O_START 0 +#define H64E_O_NONE 0 +#define H64E_O_STRING 1 +#define H64E_O_INT8 2 +#define H64E_O_UINT8 3 +#define H64E_O_INT16 4 +#define H64E_O_UINT16 5 +#define H64E_O_INT32 6 +#define H64E_O_UINT32 7 +#define H64E_O_INT64 8 +#define H64E_O_UINT64 9 +#define H64E_O_END H64E_O_UINT64 + +#define H64E_OK 0 +#define H64E_OESIZE 1 /*size mistmatch*/ + +typedef int (*trnf_clb)(char*); +typedef int (*output_clb)(char*); + +typedef struct h64e_t +{ + int flag_offset; /* output offset */ + uint64_t offset_addr; /* offset postion */ + uint8_t column_size; /* size of column to operate with */ + int flag_no_group; /* dont output hex values */ + uint8_t group; /* if there is need transdorm to specific type */ + int flag_output; /* output convereted types */ + uint8_t output_type; /* output in many different ways */ +} h64e_t; + +/* set default values */ +int h64e_set_default( h64e_t *fmt ); +/* check if all parametrs work well with each other */ +int h64e_check_param( h64e_t *fmt ); +int h64e_output( uint8_t *buf, size_t size ); +/* feed output to be printed out line by line */ +int h64e_output_line( h64e_t *fmt, uint8_t *buf, size_t size ); +/* feed output to be printed out as buffer */ +int h64e_output_buffer( h64e_t *fmt, uint8_t *buf, size_t size); +int h64e_transform_sz( h64e_t *fmt ); +int h64e_outpt_sz( h64e_t *fmt ); +int h64e_space_sz( h64e_t *fmt ); +int h64e_outputf( h64e_t *fmt, uint8_t *buf, size_t nsize ); +int h64e_read_req( h64e_t *fmt, uint8_t *buf, size_t size ); +trnf_clb h64e_trnf_fun( h64e_t *fmt ); +output_clb h64e_outpt_fun( h64e_t *fmt ); +int h64e_outputf_padding( h64e_t *fmt, uint8_t *buf, size_t nsize, int column ); + +int trnf_byte( char *buf ); +int trnf_word( char *buf ); +int trnf_dword( char *buf ); +int trnf_qword( char *buf ); + +int outpt_str( char *buf ); +int outpt_int8( char *buf ); +int outpt_uint8( char *buf ); +int outpt_int16( char *buf ); +int outpt_uint16( char *buf ); +int outpt_int32( char *buf ); +int outpt_uint32( char *buf ); +int outpt_int64( char *buf ); +int outpt_uint64( char *buf ); +#endif \ No newline at end of file diff --git a/ihe.h b/ihe.h index 798915a..8de09c7 100644 --- a/ihe.h +++ b/ihe.h @@ -15,6 +15,8 @@ #include "libterm/term.h" #include "libterm/term_io.h" +#include "h64e/core.h" + int c_blk( cmd_arg_t *arg ); int c_cd( cmd_arg_t *arg ); int c_close( cmd_arg_t *arg ); -- cgit v1.2.3