diff options
author | systemcoder <systemcoder@protonmail.com> | 2020-03-21 20:27:58 +0000 |
---|---|---|
committer | systemcoder <systemcoder@protonmail.com> | 2020-03-21 20:27:58 +0000 |
commit | dea754a95c639fa3142e1c1b1b60a9a6500377e5 (patch) | |
tree | 4d96dd504fe25fd0d8d9a97bd170c67f111b2a0f /H64E-2/h64e.c | |
parent | bb47949b70525a266ebc9badae1e87496001389d (diff) | |
download | H64D-2-dea754a95c639fa3142e1c1b1b60a9a6500377e5.tar.gz H64D-2-dea754a95c639fa3142e1c1b1b60a9a6500377e5.zip |
Hex output for different column sizes and different data types is working
Diffstat (limited to 'H64E-2/h64e.c')
-rw-r--r-- | H64E-2/h64e.c | 413 |
1 files changed, 391 insertions, 22 deletions
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; j<buf1_sz; j++) + { + out_part1[out_part1_sz+j] = buf1[j]; + } + out_part1_sz += buf1_sz; + buf1_sz = 0; + + for (j=0; j<buf2_sz; j++) + { + out_part2[out_part2_sz+j] = buf2[j]; + } + out_part2_sz += buf2_sz; + buf2_sz = 0; + + for (j=0; j<buf3_sz; j++) + { + out_part3[out_part3_sz+j] = buf3[j]; + } + out_part3_sz += buf3_sz; + buf3_sz = 0; + } + + //printf("buf_offset_sz %d\n",buf_offset_sz); + //printf("out_part1_sz %d\n",out_part1_sz); + //printf("out_part2_sz %d\n",out_part2_sz); + //printf("out_part3_sz %d\n",out_part3_sz); + + //concat all data from 3 buffers + for (i=0;i<buf_offset_sz;i++) + { + if (i<out_size) + { + out_data[i] = buf_offset[i]; + trail_size += 1; + } + } + + for (i=0,j=trail_size;i<out_part1_sz;i++) + { + if (j+i<out_size) + { + out_data[j+i] = out_part1[i]; + trail_size += 1; + } + } + + //if space between sapce disabled then put one space between ascii&hex output + //printf("%d %d %d\n",fs->f_ascii,fs->f_hex,fs->f_space); + if ((fs->f_ascii==1) && (fs->f_hex==1) && (fs->f_space==0)) + { + if (trail_size<out_size) + { + out_data[trail_size] = ' '; + trail_size += 1; + } + } + + for (i=0,j=trail_size;i<out_part2_sz;i++) + { + if (j+i<out_size) + { + out_data[j+i] = out_part2[i]; + trail_size += 1; + } + } + + + for (i=0,j=trail_size;i<out_part3_sz;i++) + { + if (j+i<out_size) + { + out_data[j+i] = out_part3[i]; + trail_size += 1; + } + } + + + ret = trail_size; + //printf("ret %d\n",ret); + +#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_align( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size, int32_t data_sz) +{ + //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 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; + + //set offset to output + if (fs->f_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; i<loop_size; i+=data_sz) + { + if (i<in_size) + { + //group byte output + if (fs->f_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_sz<BUF_SZ) + { + buf2[buf2_sz] = ' '; + buf2_sz += 1; + } + } + if (fs->f_hex) + { + if (buf1_sz<BUF_SZ) + { + buf1[buf1_sz] = ' '; + buf1_sz += 1; + } + } + } + + if (fs->f_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_sz<BUF_SZ) + { + buf2[buf2_sz] = ' '; + buf2_sz += 1; + } + } + if (fs->f_hex) + { + if (buf1_sz<BUF_SZ) + { + buf1[buf1_sz] = ' '; + buf1_sz += 1; + } + } + } + + + } + //count converted chars per line + fs->column_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+1<BUF_SZ) + { + buf2[buf2_sz] = '\n'; + buf2_sz += 1; + } + } + + if ((fs->f_hex==1) && (fs->f_ascii==0) && (fs->f_output_types==0) ) + { if (buf1_sz+1<BUF_SZ) { buf1[buf1_sz] = '\n'; buf1_sz += 1; } } - */ + + if (fs->f_output_types==1) + { + if (fs->f_output_types) + { + if (buf3_sz+1<BUF_SZ) + { + buf3[buf3_sz] = '\n'; + buf3_sz += 1; + } + } + } } } @@ -443,28 +754,86 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui 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) +int h64e_fmt_finish(H64E_format *fs) { + + return 0; } -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_space(H64E_format *fs, uint8_t *out_data, int32_t out_size, int32_t data_sz, int32_t type) { - return 0; + int buf_sz=-1; + + int i; + int ttl_sz = data_sz; + int trail_sz=0; + switch (type) + { + case H64E_O_INT8: + case H64E_O_INT16: + case H64E_O_INT32: + case H64E_O_INT64: + ttl_sz += 1; + } + + for (i=0;i<ttl_sz;i++) + { + if (i<out_size) + { + out_data[i] = ' '; + trail_sz += 1; + } + } + + return trail_sz; } -int h64e_fmt_qword( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size) -{ - return 0; -} -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_ascii(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size, int32_t data_sz) { - return 0; + int buf_sz=-1; + int ttl_sz = data_sz; + int i=0; + int trail_sz=0; + + for (i=0;i<ttl_sz;i++) + { + if ((i<out_size) && (i<in_size)) + { + //printf("[%c]",in_data[i]); + if (isprint(in_data[i])) + { + out_data[i] = in_data[i]; + } else { + out_data[i] = '.'; + } + trail_sz += 1; + //printf("[%c]\n",out_data[i]); + } + } + + return trail_sz; } -int h64e_fmt_finish(H64E_format *fs) + +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) { + int buf_sz=-1; + if (data_sz == 1) + { + buf_sz = snprintf((char*)out_data, out_size, "%02x", in_data[0]); + } else if (data_sz == 2) + { + buf_sz = snprintf((char*)out_data, out_size, "%02x%02x", in_data[0], in_data[1]); + } else if (data_sz == 4) + { + buf_sz = snprintf((char*)out_data, out_size, "%02x%02x%02x%02x", in_data[0], in_data[1], in_data[2], in_data[3]); + } else if (data_sz == 8) + { + buf_sz = snprintf((char*)out_data, out_size, "%02x%02x%02x%02x%02x%02x%02x%02x", in_data[0], in_data[1], in_data[2],in_data[3],in_data[4], in_data[5], in_data[6], in_data[7]); + } - return 0; + return buf_sz; } |