From dea754a95c639fa3142e1c1b1b60a9a6500377e5 Mon Sep 17 00:00:00 2001 From: systemcoder Date: Sat, 21 Mar 2020 20:27:58 +0000 Subject: Hex output for different column sizes and different data types is working --- H64E-2/h64e-model.h | 2 + H64E-2/h64e.c | 413 +++++++++++++++++++++++++++++++++++++++++++++++++--- H64E-2/h64e.h | 11 +- H64E-2/main.c | 62 ++++++-- 4 files changed, 447 insertions(+), 41 deletions(-) diff --git a/H64E-2/h64e-model.h b/H64E-2/h64e-model.h index 761d327..84fcbf2 100644 --- a/H64E-2/h64e-model.h +++ b/H64E-2/h64e-model.h @@ -43,4 +43,6 @@ int h64e_so_data_out(H64E_stream_out *out, uint8_t *data, int32_t size); int h64e_so_ready(H64E_stream_out *out); int h64e_so_len(H64E_stream_out *out); + + #endif /* h64e_model_h */ diff --git a/H64E-2/h64e.c b/H64E-2/h64e.c index f381d89..15b883e 100644 --- a/H64E-2/h64e.c +++ b/H64E-2/h64e.c @@ -28,6 +28,11 @@ int h64e_init(H64E_t *s) int h64e_check_param( H64E_t *fmt ) { printf("Check parametrs set\n"); + if ((fmt->fmt.column_size % h64e_data_sz(fmt)) != 0) + { + printf("Column size %d vc data size %d should be congurent\n", fmt->fmt.column_size, h64e_data_sz(fmt)); + return -1; + } return 0; } @@ -41,6 +46,42 @@ int h64e_set_output(H64E_t *s, H64E_stream_out *sout) return 0; } +int h64e_data_sz(H64E_t *s) +{ + switch (s->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: + return -1; + } + return -1; +} + +int h64e_space_width(H64E_t *s) +{ + switch (s->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: + return -1; + } + return -1; +} + int h64e_convert(H64E_t *s) { return 0; @@ -256,15 +297,6 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui } else { //printf("Empty spaces\n"); //group byte output string - /* - if (((fs->group_fmt == H64E_G_BYTE)||(fs->group_fmt == H64E_G_NONE)) && (fs->output_fmt == H64E_O_STRING)) - { - buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, " "); - } else { - //group byte output - buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, " "); - } - */ if (fs->f_hex) { buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, " "); @@ -339,14 +371,293 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui } } } - /* else { + } + } + + //copy data to buffer after each iteration + for (j=0; jf_ascii,fs->f_hex,fs->f_space); + if ((fs->f_ascii==1) && (fs->f_hex==1) && (fs->f_space==0)) + { + if (trail_sizef_offset) + { + //printf("Print offset\n"); + int offset = fs->start_offset + fs->total_output; + buf_offset_sz = snprintf((char *)&buf_offset[0], BUF_SZ, "%08x: ",offset); //make it wider + } + + if (in_size%fs->column_size != 0) + { + loop_size = (in_size/fs->column_size+1)*fs->column_size; + } else { + loop_size = in_size; + } + + + //size shout be allways matching (size mod columnsize == 0) + //non matching size should be used at the end, on last line of output + //printf("Loop_size %d\n",loop_size); + for (i=0; if_hex) + { + //buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "!%02x",in_data[i]); + //int h64e_fmt_hex(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size, int32_t data_sz, int32_t type); + buf1_sz = h64e_fmt_hex(fs, &in_data[i], in_size-i, buf1, BUF_SZ, data_sz); + } + + //ascii output + if (fs->f_ascii) + { + + /* + if (isprint(in_data[i])) + { + buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, "!%c",(unsigned char)in_data[i]); + } else { + buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, "."); + } + */ + + buf2_sz = h64e_fmt_ascii(fs, &in_data[i], in_size-i, buf2, BUF_SZ, data_sz); + //printf("!buf2_sz %d\n", buf2_sz); + } + + if (fs->f_space) + { + //printf("Space enabled\n"); + if (fs->f_ascii) + { + if (buf2_szf_hex) + { + if (buf1_szf_output_types) + { + if (fs->output_fmt == H64E_O_INT8) + { + int ii = in_data[i]; + buf3_sz = snprintf((char *)&buf3[0], BUF_SZ, "%4d ", (int8_t)ii); + } else if (fs->output_fmt == H64E_O_UINT8) { + unsigned int uu = in_data[i]; + buf3_sz = snprintf((char *)&buf3[0], BUF_SZ, "%4u ", uu); + } + } + + } else { + //printf("Empty spaces\n"); + //group byte output string + if (fs->f_hex) + { + //buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, " "); + buf1_sz = h64e_fmt_space(fs, &buf1[0], BUF_SZ, data_sz, fs->output_fmt); + } + + if (fs->f_ascii) + { + buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, "! "); + //buf2_sz = h64e_fmt_ascii(fs, &buf2[0], BUF_SZ, data_sz); + } + + if (fs->f_space) + { + //printf("Space enabled\n"); + if (fs->f_ascii) + { + if (buf2_szf_hex) + { + if (buf1_szcolumn_pos += data_sz; + fs->total_output += data_sz; //potential bug + + if (fs->column_pos == fs->column_size) + { + if (fs->f_new_line) { + //set triger to newline + fs->t_new_line = 1; + fs->column_pos = 0; + //set new line to hex buffer + //printf("%d %d %d\n",fs->f_ascii,fs->f_hex,fs->f_output_types); + if ((fs->f_ascii==1) && (fs->f_output_types==0) ) + { + if (buf2_sz+1f_hex==1) && (fs->f_ascii==0) && (fs->f_output_types==0) ) + { if (buf1_sz+1f_output_types==1) + { + if (fs->f_output_types) + { + if (buf3_sz+1\n\n" @@ -141,11 +178,10 @@ void helper( char *progname ) " -i - output hex\n" " -l - length of output\n" " -c - column size in bytes\n" - " -g - datatype output: byte,word,dword,qword\n" " -e - interp data output: u8,i8,u16,i16,u32,i32,u64,i64\n" " -s - disable space between columns\n" " -h - extra hex output\n" - "Version: 2.0.2 \n" + "Version: 2.0.3 \n" "\n" , progname); } @@ -193,14 +229,11 @@ int main(int argc, const char * argv[]) { //set all params from arguments /*get cmd args and set options */ - while ( (c = getopt(argc, argv, "abo:il:vc:g:e:sh")) != -1 ) + while ( (c = getopt(argc, argv, "abo:il:vc:e:sh")) != -1 ) { switch(c) { case 'a': - //h64e.fmt.group = H64E_G_BYTE; - //h64e.fmt.flag_output_types = 1; - //h64e.fmt.output_type = H64E_O_STRING; h64e.fmt.flag_ascii = 1; break; case 'b': @@ -222,12 +255,10 @@ int main(int argc, const char * argv[]) { case 'c': h64e.fmt.column_size = atoi(optarg); break; - case 'g': - h64e.fmt.group = group_name2int(optarg); - break; case 'e': h64e.fmt.flag_output_types = 1; h64e.fmt.output_type = output_name2int(optarg); + h64e.fmt.group = group_typename2int(optarg); break; case 's': h64e.fmt.flag_space = 1; @@ -256,7 +287,12 @@ int main(int argc, const char * argv[]) { fname = argv[argc-1]; /* check params */ - h64e_check_param( &h64e ); + if (-1 == h64e_check_param( &h64e )) + { + return -1; + } + + /* open file */ fd = file_open( fname ); @@ -314,13 +350,9 @@ int main(int argc, const char * argv[]) { cnv_num = h64e_fmt_byte_align16(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048); break; case H64E_G_WORD: - cnv_num = h64e_fmt_word(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 512); - break; case H64E_G_DWORD: - cnv_num = h64e_fmt_dword(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 512); - break; case H64E_G_QWORD: - cnv_num = h64e_fmt_qword(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 512); + cnv_num = h64e_fmt_align(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048, (int32_t)h64e_data_sz(&h64e)); break; default: -- cgit v1.2.3