From b2911a60e257d493b9d5839f40e923f95df3b525 Mon Sep 17 00:00:00 2001 From: systemcoder Date: Sat, 21 Mar 2020 15:57:41 +0000 Subject: making to work byte, offset, with no space, and hex output in any combination --- H64E-2/h64e.c | 199 +++++++++++++++++++++++++++++++++++++++++++++------------- H64E-2/h64e.h | 3 + H64E-2/main.c | 38 +++++++---- 3 files changed, 186 insertions(+), 54 deletions(-) (limited to 'H64E-2') diff --git a/H64E-2/h64e.c b/H64E-2/h64e.c index 35875a2..80b085d 100644 --- a/H64E-2/h64e.c +++ b/H64E-2/h64e.c @@ -11,13 +11,14 @@ int h64e_init(H64E_t *s) { printf("init structure\n"); - s->fmt.column_size = 16; - s->fmt.offset_addr = 0; - s->fmt.flag_offset = 0; - s->fmt.flag_no_group = 0; - s->fmt.group = H64E_G_BYTE; + s->fmt.column_size = 16; + s->fmt.offset_addr = 0; + s->fmt.flag_offset = 0; + s->fmt.flag_no_group = 0; + s->fmt.flag_hex = 1; + s->fmt.group = H64E_G_BYTE; s->fmt.flag_output_types = 0; - s->fmt.output_type = H64E_O_NONE; + s->fmt.output_type = H64E_O_NONE; s->sin = NULL; s->sout = NULL; @@ -162,14 +163,20 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui */ const int OUT_SZ=1024; //offset values in hex , spaces, number of hex values, second ascii characters const int BUF_SZ=64; + uint8_t buf_offset[BUF_SZ+1]; uint8_t buf1[BUF_SZ+1]; uint8_t buf2[BUF_SZ+1]; + uint8_t buf3[BUF_SZ+1]; uint8_t out_part1[OUT_SZ+1]; uint8_t out_part2[OUT_SZ+1]; + uint8_t out_part3[OUT_SZ+1]; int out_part1_sz=0; int out_part2_sz=0; + int out_part3_sz=0; + int buf_offset_sz=0; int buf1_sz=0; int buf2_sz=0; + int buf3_sz=0; int loop_size=0; int trail_size=0; @@ -178,10 +185,7 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui { //printf("Print offset\n"); int offset = fs->start_offset + fs->total_output; - buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "%08x: ",offset); - //no error check - memcpy(out_part1,buf1,buf1_sz); - out_part1_sz += buf1_sz; + buf_offset_sz = snprintf((char *)&buf_offset[0], BUF_SZ, "%08x: ",offset); } if (in_size%fs->column_size != 0) @@ -199,103 +203,214 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui { if (igroup_fmt == H64E_G_BYTE)||(fs->group_fmt == H64E_G_NONE)) && (fs->output_fmt == H64E_O_STRING)) + + + + //group byte output + if (fs->f_hex) + { + + buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "%02x",in_data[i]); + } + + //ascii output + if (fs->f_ascii) { if (isprint(in_data[i])) { - buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "%c",(unsigned char)in_data[i]); + buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, "%c",(unsigned char)in_data[i]); } else { - buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "."); + buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, "."); } - } else { - //group byte output - buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "%02x",in_data[i]); } + + if (fs->f_space) { //printf("Space enabled\n"); - if (buf1_szf_ascii) { - buf1[buf1_sz] = ' '; - buf1_sz += 1; + 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->group_fmt == H64E_G_BYTE)||(fs->group_fmt == H64E_G_NONE)) && (fs->output_fmt == H64E_O_STRING)) { - buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, " "); + buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, " "); } else { //group byte output - buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, " "); + buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, " "); } if (fs->f_space) { - if (buf1_szcolumn_pos += 1; - fs->total_output += 1; + //count converted chars per line + fs->column_pos += 1; + fs->total_output += 1; - 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 buffer + 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) + { + if (buf2_sz+1f_ascii,fs->f_hex,fs->f_space); + if ((fs->f_ascii==1) && (fs->f_hex==1) && (fs->f_space==0)) + { + if (trail_size\n\n" - " -a - asciimode\n" + " -a - ascii output\n" " -b - show offset adress\n" " -o - file offset\n" " -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: u8,i8,u16,i16,u32,i32,u64,i64\n" + " -e - interp data output: u8,i8,u16,i16,u32,i32,u64,i64\n" " -s - disable space between columns\n" - " -t - extra output ascii output\n" + " -h - extra hex output\n" "Version: 2.0.2 \n" "\n" , progname); @@ -193,14 +193,15 @@ 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:st")) != -1 ) + while ( (c = getopt(argc, argv, "abo:il:vc:g: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.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': h64e.fmt.flag_offset = 1; @@ -231,8 +232,8 @@ int main(int argc, const char * argv[]) { case 's': h64e.fmt.flag_space = 1; break; - case 't': - h64e.fmt.flag_ascii = 1; + case 'h': + h64e.fmt.flag_hex = 0; break; case 'v': version(); @@ -269,18 +270,22 @@ int main(int argc, const char * argv[]) { //configure format of u8 to terminate on new line and have space between types //printf("Column size set to %d\n",h64e.fmt.column_size); int8_fmt.f_space = !h64e.fmt.flag_space; - printf("h64e.fmt.flag_space %d\n",h64e.fmt.flag_space); + //printf("h64e.fmt.flag_space %d\n",h64e.fmt.flag_space); int8_fmt.f_new_line = 1; int8_fmt.column_size = h64e.fmt.column_size; int8_fmt.group_fmt = h64e.fmt.group; int8_fmt.output_fmt = h64e.fmt.output_type; int8_fmt.f_offset = h64e.fmt.flag_offset; int8_fmt.start_offset = h64e.fmt.offset_addr; + int8_fmt.f_output_types = h64e.fmt.flag_output_types; + int8_fmt.output_fmt = h64e.fmt.output_type; + int8_fmt.f_ascii = h64e.fmt.flag_ascii; + int8_fmt.f_hex = h64e.fmt.flag_hex; //do conversation, reading input and outputing - while ((in_bytes = file_read(fd,(char *)&buf_in,1)) != -1) + while ((in_bytes = file_read(fd,(char *)&buf_in,int8_fmt.column_size)) != -1) { if (in_bytes == 0) { @@ -301,7 +306,6 @@ int main(int argc, const char * argv[]) { cnv_bytes = h64e_si_data_out(&sin, &buf_cnv_in[0], int8_fmt.column_size); //cnv_total += cnv_bytes; - //Convert output data to desired format //printf("Start converting to byte8\n"); switch (h64e.fmt.group) @@ -351,6 +355,16 @@ int main(int argc, const char * argv[]) { } //check and empty all buffers + while (h64e_si_len(&sin)>0) + { + if (h64e_si_len(&sin)>int8_fmt.column_size) { + + } else { + + } + break; + } + //deinit all structures h64e_destroy(&h64e); -- cgit v1.2.3