summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsystemcoder <systemcoder@protonmail.com>2020-03-21 15:57:41 +0000
committersystemcoder <systemcoder@protonmail.com>2020-03-21 15:57:41 +0000
commitb2911a60e257d493b9d5839f40e923f95df3b525 (patch)
treef3f5862c66196fc1573993886605e701f1edcb44
parent6abd917945951cb1971fccdd649ee7f59050813c (diff)
downloadH64D-2-b2911a60e257d493b9d5839f40e923f95df3b525.tar.gz
H64D-2-b2911a60e257d493b9d5839f40e923f95df3b525.zip
making to work byte, offset, with no space, and hex output in any combination
-rw-r--r--H64E-2/h64e.c199
-rw-r--r--H64E-2/h64e.h3
-rw-r--r--H64E-2/main.c38
3 files changed, 186 insertions, 54 deletions
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 (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)
{
- buf1[buf1_sz] = ' ';
- buf1_sz += 1;
+ 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->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_sz<BUF_SZ)
+ if (buf2_sz<BUF_SZ)
{
- buf1[buf1_sz] = ' ';
- buf1_sz += 1;
+ buf2[buf2_sz] = ' ';
+ buf2_sz += 1;
+ }
+ if (buf3_sz<BUF_SZ)
+ {
+ buf3[buf3_sz] = ' ';
+ buf3_sz += 1;
}
}
}
- //count converted chars per line
- fs->column_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+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)
+ {
+ if (buf2_sz+1<BUF_SZ)
+ {
+ buf2[buf2_sz] = '\n';
+ buf2_sz += 1;
+ }
+ } else {
if (buf1_sz+1<BUF_SZ)
{
buf1[buf1_sz] = '\n';
buf1_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,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;i<out_part2_sz;i++)
+ /*
+ for (i=0;i<out_part3_sz;i++)
{
- if (out_part1_sz+i<out_size)
+ if (i<out_size)
{
- out_data[i] = out_part2[i];
+ out_data[j+i] = out_part3[i];
trail_size += 1;
}
}
+ */
+
ret = trail_size;
//printf("ret %d\n",ret);
diff --git a/H64E-2/h64e.h b/H64E-2/h64e.h
index c8a210b..5b2b4c4 100644
--- a/H64E-2/h64e.h
+++ b/H64E-2/h64e.h
@@ -46,6 +46,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
@@ -71,6 +72,7 @@ int h64e_destroy(H64E_t *s);
#define H64E_FMT_STATE_FINISH 3
typedef struct H64E_format {
+ int f_output_types;
int group_fmt;
int output_fmt;
int min_input;
@@ -87,6 +89,7 @@ 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;
diff --git a/H64E-2/main.c b/H64E-2/main.c
index 791cc18..673aa43 100644
--- a/H64E-2/main.c
+++ b/H64E-2/main.c
@@ -135,16 +135,16 @@ int output_name2int( char *name )
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"
+ " -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);