// // h64e.c // H64E-2 // // Created by dianshi on 3/13/20. // Copyright © 2020 dianshi. All rights reserved. // #include "h64e.h" 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.flag_hex = 1; s->fmt.group = H64E_G_BYTE; s->fmt.flag_output_types = 0; s->fmt.output_type = H64E_O_NONE; s->sin = NULL; s->sout = NULL; return 0; } 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; } int h64e_set_input(H64E_t *s, H64E_stream_in *sin) { return 0; } 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; } int h64e_destroy(H64E_t *s) { return 0; } int h64e_fmt_init( H64E_format *fs) { memset(fs,0,sizeof(H64E_format)); return 0; } /* * out_size - allways give enought data to buffer otherwise it will partially write data, or flush data out more often */ int h64e_fmt_byte(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 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; for (i=0; igroup_fmt == H64E_G_BYTE)||(fs->group_fmt == H64E_G_NONE)) && (fs->output_fmt == H64E_O_STRING)) { if (isprint(in_data[i])) { buf_sz = snprintf(&buf[0], SZ, "%c",(unsigned char)in_data[i]); } else { buf_sz = snprintf(&buf[0], SZ, ".",(unsigned char)in_data[i]); } } 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) { if (buf_szcolumn_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 (buf_sz+1f_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]); } //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, "."); } } 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, " "); } if (fs->f_ascii) { buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, " "); } if (fs->f_space) { //printf("Space enabled\n"); if (fs->f_ascii) { if (buf2_szf_hex) { if (buf1_szcolumn_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 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+1f_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); } */ buf3_sz = h64e_fmt_type(fs, &in_data[i], in_size-i, buf3, BUF_SZ); //printf("buf3_sz %d\n",buf3_sz); } } 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+1f_ascii,fs->f_hex,fs->f_space); if ((fs->f_ascii==1) && (fs->f_hex==1) && (fs->f_space==0)) { if (trail_sizeoutput_fmt; //printf("!!!type %d\n",type); switch (type) { case H64E_O_UINT8: case H64E_O_INT8: //data_sz = 1; data_sz = 3; break; case H64E_O_UINT16: case H64E_O_INT16: //data_sz = 2; data_sz = 5; break; case H64E_O_UINT32: case H64E_O_INT32: //data_sz = 4; data_sz = 10; break; case H64E_O_UINT64: case H64E_O_INT64: //data_sz = 8; data_sz = 19; break; } switch(type) { case H64E_O_INT8: case H64E_O_INT16: case H64E_O_INT32: case H64E_O_INT64: data_sz += 1; break; } if (out_size < data_sz) { printf("Output buffer too small h64e_fmt_type\n"); return -1; } switch (type) { case H64E_O_UINT8: { //printf("H64E_O_UINT8\n"); uint8_t *u8 = (uint8_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%3u ", *u8); break; } case H64E_O_INT8: { //printf("H64E_O_INT8\n"); int8_t *i8 = (int8_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%4i ", *i8); break; } case H64E_O_UINT16: { //printf("H64E_O_UINT16\n"); uint16_t *u16 = (uint16_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%5hu ", *u16); break; } case H64E_O_INT16: { //printf("H64E_O_INT16\n"); int16_t *i16 = (int16_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%6hd ", *i16); break; } case H64E_O_UINT32: { //printf("H64E_O_UINT32\n"); uint32_t *u32 = (uint32_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%10u ", *u32); break; } case H64E_O_INT32: { //printf("H64E_O_INT32\n"); int32_t *i32 = (int32_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%11d ", *i32); break; } case H64E_O_UINT64: { //printf("H64E_O_UINT64\n"); uint64_t *u64 = (uint64_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%20llu ", *u64); break; } case H64E_O_INT64: { //printf("H64E_O_INT64\n"); int64_t *i64 = (int64_t *)in_data; buf_sz = snprintf((char *)out_data, out_size, "%20lld ", *i64); break; } } return buf_sz; }