summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--H64E-2/h64e-model.h2
-rw-r--r--H64E-2/h64e.c706
-rw-r--r--H64E-2/h64e.h14
-rw-r--r--H64E-2/main.c99
4 files changed, 745 insertions, 76 deletions
diff --git a/H64E-2/h64e-model.h b/H64E-2/h64e-model.h
index 6e5e32b..3692a26 100644
--- a/H64E-2/h64e-model.h
+++ b/H64E-2/h64e-model.h
@@ -44,4 +44,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 a508fd3..39884ca 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;
@@ -27,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;
}
@@ -40,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;
@@ -162,14 +204,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 +226,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); //make it wider
}
if (in_size%fs->column_size != 0)
@@ -199,56 +244,91 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui
{
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))
+ //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_sz<BUF_SZ)
+ if (fs->f_ascii)
+ {
+ if (buf2_sz<BUF_SZ)
+ {
+ buf2[buf2_sz] = ' ';
+ buf2_sz += 1;
+ }
+ }
+ if (fs->f_hex)
{
- buf1[buf1_sz] = ' ';
- buf1_sz += 1;
+ if (buf1_sz<BUF_SZ)
+ {
+ buf1[buf1_sz] = ' ';
+ buf1_sz += 1;
+ }
}
}
+
if (fs->f_output_types)
{
if (fs->output_fmt == H64E_O_INT8)
{
- buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, "%4d ", (int8_t)in_data[i]);
+ 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) {
- buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, "%4u ", (uint8_t)in_data[i]);
+ unsigned int uu = in_data[i];
+ buf3_sz = snprintf((char *)&buf3[0], BUF_SZ, "%4u ", uu);
}
}
-
+
} else {
- printf("Empty spaces\n");
+ //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))
+ if (fs->f_hex)
{
- buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, " ");
- } else {
- //group byte output
buf1_sz = snprintf((char *)&buf1[0], BUF_SZ, " ");
}
+
+ if (fs->f_ascii)
+ {
+ buf2_sz = snprintf((char *)&buf2[0], BUF_SZ, " ");
+ }
+
if (fs->f_space)
{
- if (buf1_sz<BUF_SZ)
+ //printf("Space enabled\n");
+ if (fs->f_ascii)
+ {
+ if (buf2_sz<BUF_SZ)
+ {
+ buf2[buf2_sz] = ' ';
+ buf2_sz += 1;
+ }
+ }
+ if (fs->f_hex)
{
- buf1[buf1_sz] = ' ';
- buf1_sz += 1;
+ if (buf1_sz<BUF_SZ)
+ {
+ buf1[buf1_sz] = ' ';
+ buf1_sz += 1;
+ }
}
}
+
+
}
//count converted chars per line
fs->column_pos += 1;
@@ -260,51 +340,409 @@ int h64e_fmt_byte_align16(H64E_format *fs, uint8_t *in_data, int32_t in_size, ui
//set triger to newline
fs->t_new_line = 1;
fs->column_pos = 0;
- //set new line to buffer
- if (buf1_sz+1<BUF_SZ)
+ //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) )
{
- buf1[buf1_sz] = '\n';
- buf1_sz += 1;
+ 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;
+ }
+ }
}
}
}
+ //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);
+ }
+ */
+ 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_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;
+ }
+ }
+ }
+ }
+ }
+
//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 2 buffers
- for (i=0;i<out_part1_sz;i++)
+ //concat all data from 3 buffers
+ for (i=0;i<buf_offset_sz;i++)
{
if (i<out_size)
{
- out_data[i] = out_part1[i];
+ out_data[i] = buf_offset[i];
trail_size += 1;
}
}
- for (i=0;i<out_part2_sz;i++)
+ for (i=0,j=trail_size;i<out_part1_sz;i++)
{
- if (out_part1_sz+i<out_size)
+ if (j+i<out_size)
{
- out_data[i] = out_part2[i];
+ 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);
@@ -320,28 +758,196 @@ 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)
+
+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_string( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size)
+
+
+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)
{
- return 0;
+ 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 buf_sz;
}
-int h64e_fmt_finish(H64E_format *fs)
+int h64e_fmt_type(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size)
{
+ int buf_sz = -1;
+ int data_sz = -1;
+ int type = fs->output_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;
+ }
- return 0;
+ 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;
}
diff --git a/H64E-2/h64e.h b/H64E-2/h64e.h
index 7cc2d41..7871d21 100644
--- a/H64E-2/h64e.h
+++ b/H64E-2/h64e.h
@@ -48,6 +48,7 @@ typedef struct H64E_params
uint8_t output_type; /* output in many different ways */
int flag_space; /*space between columns **/
int flag_ascii;
+ int flag_hex; /* hex outout */
} H64E_params;
typedef struct H64E_t
@@ -63,6 +64,8 @@ int h64e_init(H64E_t *s);
int h64e_check_param( H64E_t *s );
int h64e_set_input(H64E_t *s, H64E_stream_in *sin);
int h64e_set_output(H64E_t *s, H64E_stream_out *sout);
+int h64e_data_sz(H64E_t *s);
+int h64e_space_width(H64E_t *s);
int h64e_convert(H64E_t *s);
int h64e_destroy(H64E_t *s);
@@ -90,16 +93,19 @@ typedef struct H64E_format {
int f_nw_pos; // if new line then save place where new line is
int f_ascii;
int f_offset;
+ int f_hex;
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_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_align( H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size, int32_t data_sz);
int h64e_fmt_finish( H64E_format *fs);
+int h64e_fmt_space(H64E_format *fs, uint8_t *out_data, int32_t out_size, int32_t data_sz, int32_t type);
+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);
+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 h64e_fmt_type(H64E_format *fs, uint8_t *in_data, int32_t in_size, uint8_t *out_data, int32_t out_size);
+
#endif /* h64e_h */
diff --git a/H64E-2/main.c b/H64E-2/main.c
index e83ddf9..57f996a 100644
--- a/H64E-2/main.c
+++ b/H64E-2/main.c
@@ -132,20 +132,56 @@ int output_name2int( char *name )
return 0;
}
+int group_typename2int( char *name )
+{
+ if ( 0 == strncmp( name, "i8", 2) )
+ {
+ return H64E_G_BYTE;
+ }
+ if ( 0 == strncmp( name, "u8", 2) )
+ {
+ return H64E_G_BYTE;
+ }
+ if ( 0 == strncmp( name, "i16", 3) )
+ {
+ return H64E_G_WORD;
+ }
+ if ( 0 == strncmp( name, "u16", 3) )
+ {
+ return H64E_G_WORD;
+ }
+ if ( 0 == strncmp( name, "i32", 3) )
+ {
+ return H64E_G_DWORD;
+ }
+ if ( 0 == strncmp( name, "u32", 3) )
+ {
+ return H64E_G_DWORD;
+ }
+ if ( 0 == strncmp( name, "i64", 3) )
+ {
+ return H64E_G_QWORD;
+ }
+ if ( 0 == strncmp( name, "u64", 3) )
+ {
+ return H64E_G_QWORD;
+ }
+ return 0;
+}
+
void helper( char *progname )
{
printf("Usage: %s [OPTS] <filename>\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"
- "Version: 2.0.2 \n"
+ " -h - extra hex output\n"
+ "Version: 2.0.3 \n"
"\n"
, progname);
}
@@ -193,14 +229,12 @@ 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: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':
h64e.fmt.flag_offset = 1;
@@ -221,18 +255,16 @@ 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;
break;
- case 't':
- h64e.fmt.flag_ascii = 1;
+ case 'h':
+ h64e.fmt.flag_hex = 0;
break;
case 'v':
version();
@@ -255,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 );
@@ -278,6 +315,8 @@ int main(int argc, const char * argv[]) {
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;
@@ -303,7 +342,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)
@@ -312,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:
@@ -355,10 +389,31 @@ int main(int argc, const char * argv[]) {
//check and empty all buffers
while (h64e_si_len(&sin)>0)
{
+ int cnv_num = -1;
if (h64e_si_len(&sin)>int8_fmt.column_size) {
-
+ //printf("Full column buffer\n");
+ cnv_bytes = h64e_si_data_out(&sin, &buf_cnv_in[0], int8_fmt.column_size);
+ cnv_num = h64e_fmt_byte_align16(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048);
+
+ cnv_bytes = h64e_so_data_in(&sout, &buf_cnv_out[0], cnv_num);
+ cnv_total += cnv_num;
} else {
+ //printf("some buffer!!!\n");
+ //less then column size
+ cnv_bytes = h64e_si_data_out(&sin, &buf_cnv_in[0], int8_fmt.column_size);
+ //printf("cnv_bytes %d\n", cnv_bytes);
+ cnv_num = h64e_fmt_byte_align16(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 2048);
+ //printf("cnv_num %d\n", cnv_num);
+ cnv_bytes = h64e_so_data_in(&sout, &buf_cnv_out[0], cnv_num);
+ cnv_total += cnv_num;
+ }
+
+ if ((out_bytes = h64e_so_len(&sout)) > 0)
+ {
+ out_bytes = h64e_so_data_out(&sout, &buf_out[0],512);
+ buf_out[out_bytes] = 0x00;
+ printf("%s",buf_out);
}
break;
}