#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; }