diff options
author | systemcoder <systemcoder@protonmail.com> | 2020-03-15 08:34:44 +0000 |
---|---|---|
committer | systemcoder <systemcoder@protonmail.com> | 2020-03-15 08:34:44 +0000 |
commit | 784076d51c3d95443b53416e593c38c612c14bbe (patch) | |
tree | fdbe7e3870f7c5a17c3a9ce6b5f9fb4706b84804 /H64E-2 | |
parent | 0184655779b6e72449db0b711ee591b6cd991652 (diff) | |
download | H64D-2-784076d51c3d95443b53416e593c38c612c14bbe.tar.gz H64D-2-784076d51c3d95443b53416e593c38c612c14bbe.zip |
Output as hex works, first version that produces results.
Diffstat (limited to 'H64E-2')
-rw-r--r-- | H64E-2/h64e-model.c | 81 | ||||
-rw-r--r-- | H64E-2/h64e-model.h | 27 | ||||
-rw-r--r-- | H64E-2/h64e.c | 83 | ||||
-rw-r--r-- | H64E-2/h64e.h | 37 | ||||
-rw-r--r-- | H64E-2/main.c | 70 |
5 files changed, 250 insertions, 48 deletions
diff --git a/H64E-2/h64e-model.c b/H64E-2/h64e-model.c index b794f52..3bd0d1f 100644 --- a/H64E-2/h64e-model.c +++ b/H64E-2/h64e-model.c @@ -8,7 +8,7 @@ #include "h64e-model.h" -int h64e_si_init(H64E_stream_in *in, ssize_t size) +int h64e_si_init(H64E_stream_in *in, int32_t size) { in->size = size; in->cur_size = 0; @@ -17,42 +17,53 @@ int h64e_si_init(H64E_stream_in *in, ssize_t size) } //return amount of copied bytes -int h64e_si_data_in(H64E_stream_in *in, uint8_t *data, size_t size) +int h64e_si_data_in(H64E_stream_in *in, uint8_t *data, int32_t size) { - ssize_t start = in->cur_size; - ssize_t end = start+size; - ssize_t len = 0; + int32_t start = in->cur_size; + int32_t end = start+size; + int32_t len = 0; + if (end > in->size) { end = in->size; } len = end-start; - - memcpy(in->buf, data, len); - + memcpy(&in->buf[start], data, len); + in->cur_size += len; return (int)len; } -int h64e_si_data_out(H64E_stream_in *in, uint8_t *data, size_t size) +int h64e_si_data_out(H64E_stream_in *in, uint8_t *data, int32_t size) { - ssize_t len=0; + int32_t len=0; + int32_t i=0; - if (size<in->cur_size) + if (size < in->cur_size) { len = size; } else { len = in->cur_size; } - memcpy(data,in->buf,len); - memmove(in->buf,in->buf[len],len); + //memcpy(data,&in->buf[0],len); + for (i=0;i<len;i++) + { + data[i] = in->buf[i]; + } + + memmove(&in->buf[0],&in->buf[len],len); in->cur_size -= len; return len; } -int h64e_so_init(H64E_stream_out *out, ssize_t size) +int h64e_si_len(H64E_stream_in *in) +{ + return in->cur_size; +} + +int h64e_so_init(H64E_stream_out *out, int32_t size) { out->size = size; out->cur_size = 0; @@ -60,37 +71,55 @@ int h64e_so_init(H64E_stream_out *out, ssize_t size) return 0; } -int h64e_so_data_in(H64E_stream_in *out, uint8_t *data, size_t size) +int h64e_so_data_in(H64E_stream_out *out, uint8_t *data, int32_t size) { - ssize_t start = out->cur_size; - ssize_t end = start+size; - ssize_t len = 0; + int32_t start = out->cur_size; + int32_t end = start+size; + int32_t len = 0; + if (end > out->size) { end = out->size; } len = end-start; - - memcpy(out->buf, data, len); - + memcpy(&out->buf[start], data, len); + out->cur_size += len; return (int)len; } -int h64e_so_data_out(H64E_stream_in *out, uint8_t *data, size_t size) +int h64e_so_data_out(H64E_stream_out *out, uint8_t *data, int32_t size) { - ssize_t len=0; + int32_t len=0; + int32_t i=0; - if (size<out->cur_size) + if (size < out->cur_size) { len = size; } else { len = out->cur_size; } - memcpy(data,out->buf,len); - memmove(out->buf,out->buf[len],len); + //memcpy(data,&in->buf[0],len); + for (i=0;i<len;i++) + { + data[i] = out->buf[i]; + } + + memmove(&out->buf[0],&out->buf[len],len); out->cur_size -= len; return len; } + +int h64e_so_ready(H64E_stream_out *out) +{ + int ret = 0; + + return ret; +} + +int h64e_so_len(H64E_stream_out *out) +{ + return out->cur_size; +} diff --git a/H64E-2/h64e-model.h b/H64E-2/h64e-model.h index 516f8dc..761d327 100644 --- a/H64E-2/h64e-model.h +++ b/H64E-2/h64e-model.h @@ -10,30 +10,37 @@ #define h64e_model_h #include <stdio.h> +#include <stdlib.h> #include <string.h> typedef int (*trnf_clb)(char*); typedef struct H64E_stream_in { - int cur_size; - int size; + int32_t cur_size; + int32_t size; uint8_t *buf; } H64E_stream_in; //used to get collected data -int h64e_si_init(H64E_stream_in *in, ssize_t size); -int h64e_si_data_in(H64E_stream_in *in, uint8_t *data, size_t size); -int h64e_si_data_out(H64E_stream_in *in, uint8_t *data, size_t size); +int h64e_si_init(H64E_stream_in *in, int32_t size); +int h64e_si_data_in(H64E_stream_in *in, uint8_t *data, int32_t size); +int h64e_si_data_out(H64E_stream_in *in, uint8_t *data, int32_t size); +int h64e_si_len(H64E_stream_in *in); typedef struct H64E_stream_out { - int cur_size; - int size; + int32_t cur_size; + int32_t size; uint8_t *buf; } H64E_stream_out; //using to collect output formated data -int h64e_so_init(H64E_stream_out *out, ssize_t size); -int h64e_so_data_in(H64E_stream_in *out, uint8_t *data, size_t size); -int h64e_so_data_out(H64E_stream_in *out, uint8_t *data, size_t size); +int h64e_so_init(H64E_stream_out *out, int32_t size); +int h64e_so_data_in(H64E_stream_out *out, uint8_t *data, int32_t size); +int h64e_so_data_out(H64E_stream_out *out, uint8_t *data, int32_t size); +/* + * Trigger that buffer is full, or trigger new line in buffer + */ +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 0072030..1e0662c 100644 --- a/H64E-2/h64e.c +++ b/H64E-2/h64e.c @@ -49,3 +49,86 @@ 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; + const int SZ=16; + uint8_t buf[SZ]; + int buf_sz=0; + int trail_size=0; + + for (i=0; i<in_size; i++) + { + buf_sz = snprintf(&buf[0], SZ, "%02x",in_data[i]); + //count converted chars per line + fs->cur_pos += 1; + //add space if configured + if (fs->f_space) + { + if (buf_sz+1<SZ) + { + buf[buf_sz] = ' '; + buf_sz += 1; + } + } + + if (fs->cur_pos == fs->column_size) + { + if (fs->f_new_line) { + //set triger to newline + fs->t_new_line = 1; + fs->cur_pos = 0; + //set new line to buffer + if (buf_sz+1<SZ) + { + buf[buf_sz] = '\n'; + buf_sz += 1; + } + } + } + //printf("D: %s",buf); + if (trail_size+buf_sz<out_size) + { + for(j=0;j<buf_sz;j++) + { + out_data[trail_size+j] = buf[j]; + } + ret = buf_sz; + + } else { + //not enought output to handle data + ret=-1; + } + } +#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_finish(H64E_format *fs) +{ + + + return 0; +} diff --git a/H64E-2/h64e.h b/H64E-2/h64e.h index 397a36b..d8a95cd 100644 --- a/H64E-2/h64e.h +++ b/H64E-2/h64e.h @@ -13,16 +13,13 @@ #include "h64e-model.h" /* Set data structure from arguments and use to handle data - - */ - +*/ #define H64E_G_NONE 0 #define H64E_G_BYTE 1 #define H64E_G_WORD 2 #define H64E_G_DWORD 3 #define H64E_G_QWORD 4 - #define H64E_O_START 0 #define H64E_O_NONE 0 #define H64E_O_STRING 1 @@ -36,7 +33,7 @@ #define H64E_O_UINT64 9 #define H64E_O_END H64E_O_UINT64 -typedef struct H64E_format +typedef struct H64E_params { int flag_offset; /* output offset */ @@ -46,11 +43,11 @@ typedef struct H64E_format uint8_t group; /* if there is need transdorm to specific type */ int flag_output; /* output convereted types */ uint8_t output_type; /* output in many different ways */ -} H64E_format; +} H64E_params; typedef struct H64E_t { - H64E_format fmt; + H64E_params fmt; H64E_stream_in *sin; H64E_stream_out *sout; int fd_in; @@ -65,4 +62,30 @@ int h64e_convert(H64E_t *s); int h64e_destroy(H64E_t *s); +#define H64E_FMT_STATE_NONE 0 +#define H64E_FMT_STATE_INIT 1 +#define H64E_FMT_STATE_CNV 2 +#define H64E_FMT_STATE_FINISH 3 + +typedef struct H64E_format { + int group_fmt; + int output_fmt; + int min_input; + int max_output; + int column_size; + int f_space; + int cur_pos; //position where output stoped + int end_pos; //end postion before new line + int f_new_line; //should be new line set after + int start_offset; //start position of offset + int slide_offset; //offset since stream start + 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 +} 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_finish(H64E_format *fs); + #endif /* h64e_h */ diff --git a/H64E-2/main.c b/H64E-2/main.c index e1d3d7b..bd1910e 100644 --- a/H64E-2/main.c +++ b/H64E-2/main.c @@ -161,8 +161,10 @@ int main(int argc, const char * argv[]) { int param_offset = -1; char *fname = NULL; int fd; - int in_bytes = 0; - uint8_t buf[128]; + int in_bytes = 0, out_bytes=0, cnv_bytes=0; + int in_total = 0, out_total=0, cnv_total=0; + uint8_t buf_in[128+1],buf_cnv_in[128+1],buf_cnv_out[256+1],buf_out[512+1]; + int i=0; printf("H64E-2 Project started 1\n"); @@ -172,10 +174,14 @@ int main(int argc, const char * argv[]) { H64E_stream_out sout; h64e_init(&h64e); h64e_si_init(&sin, 128); - h64e_so_init(&sout, 128); + h64e_so_init(&sout, 512); h64e_set_input(&h64e, &sin); h64e_set_output(&h64e, &sout); + H64E_format int8_fmt; + + h64e_fmt_init(&int8_fmt); + //set all params from arguments /*get cmd args and set options */ @@ -244,13 +250,67 @@ int main(int argc, const char * argv[]) { } //register formats + //configure format of u8 to terminate on new line and have space between types + int8_fmt.f_space = 1; + int8_fmt.f_new_line = 1; + int8_fmt.column_size = 16; //do conversation, reading input and outputing - while ((in_bytes = file_read(fd,(char *)&buf,128)) != -1) + while ((in_bytes = file_read(fd,(char *)&buf_in,1)) != -1) { - printf("Reading %d bytes\n",in_bytes); + if (in_bytes == 0) + { + //printf("Stream ended\n"); + break; + } + + //write to input stream, connect all streams in a pipe + in_total += in_bytes; + //printf("Read from file %d bytes %d bytes total\n",in_bytes,in_total); + h64e_si_data_in(&sin, &buf_in[0], in_bytes); + + //if buffer have enought data then convert it + if (h64e_si_len(&sin)>1) + { + int cnv_num=-1; + //printf("Input Out\n"); + cnv_bytes = h64e_si_data_out(&sin, &buf_cnv_in[0], 1); + //cnv_total += cnv_bytes; + + + //Convert output data to desired format + //printf("Start converting to byte8\n"); + cnv_num = h64e_fmt_byte(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 512); + //printf("Conversation amount\n"); + //Push all data ot output buffer + //printf("Write to output stream data %d bytes\n",cnv_bytes); + cnv_bytes = h64e_so_data_in(&sout, &buf_cnv_out[0], cnv_num); + cnv_total += cnv_num; + //printf("Written bytes to output steam %d bytes %d bytes total\n",cnv_bytes,cnv_total); + } + + //Get data out of buffer + 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); + } + /* + out_bytes = h64e_so_data_out(&sout, &buf_out[0],512); + printf("Output out\n"); + out_total += out_bytes; + buf_out[out_bytes] = 0x00; + printf("%s\n",buf_out); + printf("Read converted data %d bytes %d total bytes\n",out_bytes,out_total); + */ + //printf("Total: in %d cnv %d out %d\n",in_total,cnv_total,out_total); + + //printf("Reading %d bytes\n",in_bytes); } + //check and empty all buffers + //deinit all structures h64e_destroy(&h64e); file_close(fd); |