diff options
author | systemcoder <systemcoder@protonmail.com> | 2020-03-19 20:13:02 +0000 |
---|---|---|
committer | systemcoder <systemcoder@protonmail.com> | 2020-03-19 20:13:02 +0000 |
commit | 69969bb875a95ae7e0913d83c821539e62accba2 (patch) | |
tree | b82947efa6ffe3124f18a3d057fa1f91e8d82eb2 | |
parent | e1e7859ef4b3353a8ac182fd456b053c653239d1 (diff) | |
download | H64D-2-69969bb875a95ae7e0913d83c821539e62accba2.tar.gz H64D-2-69969bb875a95ae7e0913d83c821539e62accba2.zip |
Added single function that will be able to manage all output
-rw-r--r-- | H64E-2/h64e.c | 166 | ||||
-rw-r--r-- | H64E-2/h64e.h | 15 | ||||
-rw-r--r-- | H64E-2/main.c | 28 |
3 files changed, 191 insertions, 18 deletions
diff --git a/H64E-2/h64e.c b/H64E-2/h64e.c index decaff5..8a1325a 100644 --- a/H64E-2/h64e.c +++ b/H64E-2/h64e.c @@ -16,7 +16,7 @@ int h64e_init(H64E_t *s) s->fmt.flag_offset = 0; s->fmt.flag_no_group = 0; s->fmt.group = H64E_G_BYTE; - s->fmt.flag_output = 0; + s->fmt.flag_output_types = 0; s->fmt.output_type = H64E_O_NONE; s->sin = NULL; @@ -65,8 +65,14 @@ int h64e_fmt_byte(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *o //printf("h64e_fmt_byte in %d out %d\n",in_size,out_size); int ret=0; int cur_size=0; - int i,j=0; - const int SZ=16; + int i, j=0; + /* + offset - max size 64bit = 0x(2bytes)+0000_0000_0000_0000(16bytes)+ ":"1(bytes) + whitespace(1byte) = 19bytes + hex values - 00(2bytes)*32+whitespace(1byte)*32+space_between_columns(1byte)+asciichar(1byte)*32+sapce_betweeen_ascii_columns(1byte)*32 = 1121 + new line - 1byte + total = 19 + 1121 + 1 = 1141 + */ + const int SZ=2048; //offset values in hex , spaces, number of hex values, second ascii characters uint8_t buf[SZ+1]; int buf_sz=0; int trail_size=0; @@ -85,10 +91,10 @@ int h64e_fmt_byte(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *o } else { //group byte output buf_sz = snprintf(&buf[0], SZ, "%02x",in_data[i]); - } //count converted chars per line fs->column_pos += 1; + fs->total_output += 1; //add space if configured if (fs->f_space) { @@ -139,6 +145,158 @@ int h64e_fmt_byte(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *o return ret; } +/* + * out_size - allways give enought data to buffer otherwise it will partially write data, or flush data out more often + */ +int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size) +{ + //printf("h64e_fmt_byte in %d out %d\n",in_size,out_size); + int ret=0; + int cur_size=0; + int i, j=0; + /* + offset - max size 64bit = 0x(2bytes)+0000_0000_0000_0000(16bytes)+ ":"1(bytes) + whitespace(1byte) = 19bytes + hex values - 00(2bytes)*32+whitespace(1byte)*32+space_between_columns(1byte)+asciichar(1byte)*32+sapce_betweeen_ascii_columns(1byte)*32 = 1121 + new line - 1byte + total = 19 + 1121 + 1 = 1141 + */ + const int OUT_SZ=1024; //offset values in hex , spaces, number of hex values, second ascii characters + const int BUF_SZ=64; + uint8_t buf1[BUF_SZ+1]; + uint8_t buf2[BUF_SZ+1]; + uint8_t out_part1[OUT_SZ+1]; + uint8_t out_part2[OUT_SZ+1]; + int out_part1_sz=0; + int out_part2_sz=0; + int buf1_sz=0; + int buf2_sz=0; + int loop_size=0; + int trail_size=0; + + //set offset to output + if (fs->f_offset) + { + 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; + } + + 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 + for (i=0; i<loop_size; i++) + { + if (i>in_size) + { + //group byte output string + if (((fs->group_fmt == H64E_G_BYTE)||(fs->group_fmt == H64E_G_NONE)) && (fs->output_fmt == H64E_O_STRING)) + { + if (isprint(in_data[i])) + { + buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "%c",(unsigned char)in_data[i]); + } else { + buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "."); + } + } else { + //group byte output + buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, "%02x",in_data[i]); + } + //count converted chars per line + fs->column_pos += 1; + fs->total_output += 1; + //add space if configured + if (fs->f_space) + { + if (buf1_sz<BUF_SZ) + { + buf1[buf1_sz] = ' '; + buf1_sz += 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 (buf1_sz+1<BUF_SZ) + { + buf1[buf1_sz] = '\n'; + buf1_sz += 1; + } + } + } + } else { + //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, " "); + } else { + //group byte output + buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, " "); + } + } + + //copy data to buffer after each iteration + for (j=0; j<buf1_sz; j++) + { + out_part1[out_part1_sz+j] = buf1[j]; + } + out_part1_sz += buf1_sz; + + for (j=0; j<buf2_sz; j++) + { + out_part2[out_part2_sz+j] = buf2[j]; + } + out_part2_sz += buf2_sz; + } + + + //concat all data from 2 buffers + for (i=0;i<out_part1_sz;i++) + { + if (out_size<i) + { + out_data[i] = out_part1[i]; + trail_size += 1; + } else { + ret = trail_size; + } + } + + for (i=0;i<out_part2_sz;i++) + { + if (out_size < out_part1_sz+i) + { + out_data[i] = out_part2[i]; + } else { + ret = trail_size+i; + } + } + +#if 0 + printf("D:["); + for (i=0;i<ret;i++) + { + printf("%c",(unsigned char)out_data[i]); + } + printf("]\n"); +#endif + + return ret; +} + int h64e_fmt_word( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size) { return 0; diff --git a/H64E-2/h64e.h b/H64E-2/h64e.h index ac47b14..c8a210b 100644 --- a/H64E-2/h64e.h +++ b/H64E-2/h64e.h @@ -42,9 +42,10 @@ typedef struct H64E_params 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 */ + int flag_output_types; /* output convereted types */ uint8_t output_type; /* output in many different ways */ int flag_space; /*space between columns **/ + int flag_ascii; } H64E_params; typedef struct H64E_t @@ -84,14 +85,18 @@ typedef struct H64E_format { int state; int t_new_line; // if new line or buffer full int f_nw_pos; // if new line then save place where new line is + int f_ascii; + int f_offset; + int total_output; } H64E_format; int h64e_fmt_init( H64E_format *fs); int h64e_fmt_byte( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); -int h64e_fmt_word( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); -int h64e_fmt_dword( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); -int h64e_fmt_qword( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); +int h64e_fmt_byte_align16( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); +int h64e_fmt_word( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); +int h64e_fmt_dword( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); +int h64e_fmt_qword( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); int h64e_fmt_string( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size); -int h64e_fmt_finish(H64E_format *fs); +int h64e_fmt_finish( H64E_format *fs); #endif /* h64e_h */ diff --git a/H64E-2/main.c b/H64E-2/main.c index 818f165..29184b7 100644 --- a/H64E-2/main.c +++ b/H64E-2/main.c @@ -140,10 +140,12 @@ void helper( char *progname ) " -o - file offset\n" " -i - output hex\n" " -l - length of output\n" - " -c - column size in byts\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" - "\nVersion: 2.0.0 \n" + " -e - interp data: u8,i8,u16,i16,u32,i32,u64,i64\n" + " -s - disable space between columns\n" + " -t - extra output ascii output\n" + "Version: 2.0.2 \n" "\n" , progname); } @@ -194,18 +196,20 @@ int main(int argc, const char * argv[]) { { case 'a': h64e.fmt.group = H64E_G_BYTE; - h64e.fmt.flag_output = 1; + h64e.fmt.flag_output_types = 1; h64e.fmt.output_type = H64E_O_STRING; break; case 'b': h64e.fmt.flag_offset = 1; break; case 'o': - param_offset = atoi( optarg ); + //param_offset = atoi( optarg ); + h64e.fmt.flag_offset = 1; + h64e.fmt.offset_addr = atoi(optarg); break; case 'i': h64e.fmt.group = H64E_G_BYTE; - h64e.fmt.flag_output = 0; + h64e.fmt.flag_output_types = 0; h64e.fmt.flag_offset = 1; break; case 'l': @@ -218,12 +222,15 @@ int main(int argc, const char * argv[]) { h64e.fmt.group = group_name2int(optarg); break; case 'e': - h64e.fmt.flag_output = 1; + h64e.fmt.flag_output_types = 1; h64e.fmt.output_type = output_name2int(optarg); break; case 's': h64e.fmt.flag_space = 1; break; + case 't': + h64e.fmt.flag_ascii = 1; + break; case 'v': version(); exit(1); @@ -263,6 +270,8 @@ int main(int argc, const char * argv[]) { 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; @@ -342,7 +351,8 @@ int main(int argc, const char * argv[]) { //deinit all structures h64e_destroy(&h64e); file_close(fd); - - printf("Ending Execution\n"); + + printf("\nEnding Execution\n"); + return 0; } |