#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_type<H64E_O_START) || (fmt->output_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 (i<size);
return 0;
}
int h64e_transform_sz( h64e_t *fmt )
{
if ( fmt == NULL )
return -1;
switch(fmt->group)
{
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; i<nsize; i++)
{
tclb(buf+i*tsz);
printf(" ");
}
}
/* output formated numbers */
if ( fmt->flag_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;i++)
{
oclb(buf+i*osz);
}
}
}
printf("\n");
return 0;
}
int h64e_read_req( h64e_t *fmt, uint8_t *buf, size_t size )
{
return 0;
}
trnf_clb h64e_trnf_fun( h64e_t *fmt )
{
uint8_t g;
if (fmt == NULL)
{
return NULL;
}
g = fmt->group;
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_j<nsize)
{
printf("%02x", (unsigned char)(buf[acc_j]));
} else
{
printf(" ");
}
j += 1;
acc_j += 1;
if (j==tsz)
{
j = 0;
state = STATE_SPACE;
}
break;
case STATE_SPACE:
printf(" ");
if (acc_j == column)
{
state = STATE_END;
} else
{
state = STATE_GROUP;
}
break;
case STATE_END:
run = 0;
break;
default:
printf("Wrong state\n");
return -1;
}
}
}
/* output formated numbers */
if (fmt->flag_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