// // main.c // H64E-2 // // Created by dianshi on 3/13/20. // Copyright © 2020 dianshi. All rights reserved. // #include #include #include #include #include #include #include #include "h64e.h" #include #include #include int file_open( const char *fname ) { int fd; fd = open( fname, O_RDONLY ); if ( fd == -1 ) { printf("Couldn open file %s\n", fname); return -1; } return fd; } int file_read( int fd, char *buf, int size ) { int ret; if ( size < 1 ) return -1; ret = (int)read( fd, buf, size ); return ret; } int file_seek( int fd, off_t pos ) { off_t rc; if (fd<1) { return -1; } rc = lseek( fd, pos, SEEK_SET ); if (rc == -1) return -1; return 0; } int file_close( int fd ) { close( fd ); return 0; } int group_name2int( char *name ) { if ( 0 == strncmp( name, "byte", 4 ) ) { return H64E_G_BYTE; } if ( 0 == strncmp( name, "word", 4 ) ) { return H64E_G_WORD; } if ( 0 == strncmp( name, "dword", 5 ) ) { return H64E_G_DWORD; } if ( 0 == strncmp( name, "qword", 5 ) ) { return H64E_G_QWORD; } return 1; } int output_name2int( char *name ) { if ( 0 == strncmp( name, "str", 3) ) { return H64E_O_STRING; } if ( 0 == strncmp( name, "i8", 2) ) { return H64E_O_INT8; } if ( 0 == strncmp( name, "u8", 2) ) { return H64E_O_UINT8; } if ( 0 == strncmp( name, "i16", 3) ) { return H64E_O_INT16; } if ( 0 == strncmp( name, "u16", 3) ) { return H64E_O_UINT16; } if ( 0 == strncmp( name, "i32", 3) ) { return H64E_O_INT32; } if ( 0 == strncmp( name, "u32", 3) ) { return H64E_O_UINT32; } if ( 0 == strncmp( name, "i64", 3) ) { return H64E_O_INT64; } if ( 0 == strncmp( name, "u64", 3) ) { return H64E_O_UINT64; } return 0; } void helper( char *progname ) { printf("Usage: %s [OPTS] \n\n" " -a - asciimode\n" " -b - show offset adress\n" " -o - file offset\n" " -i - output hex\n" " -l - length of output\n" " -c - column size in byts\n" " -g - datatype output: byte,word,dword,qword\n" " -e - interp data: u8,i8,u16,i16,u32,i32,u64,i64" "\nVersion: 2.0.0 \n" "\n" , progname); } void version() { printf("???\n"); } /* Main fucntion, of this program */ int main(int argc, const char * argv[]) { // insert code here... int c; int param_length = -1; int param_offset = -1; char *fname = NULL; int fd; 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"); //initialise structure H64E_t h64e; H64E_stream_in sin; H64E_stream_out sout; h64e_init(&h64e); h64e_si_init(&sin, 128); h64e_so_init(&sout, 512); h64e_set_input(&h64e, &sin); h64e_set_output(&h64e, &sout); H64E_format int8_fmt; //list of supported formats h64e_fmt_init(&int8_fmt); //set all params from arguments /*get cmd args and set options */ while ( (c = getopt(argc, argv, "abo:il:vc:g:e:s")) != -1 ) { switch(c) { case 'a': h64e.fmt.group = H64E_G_BYTE; h64e.fmt.flag_output = 1; h64e.fmt.output_type = H64E_O_STRING; break; case 'b': h64e.fmt.flag_offset = 1; break; case 'o': param_offset = atoi( optarg ); break; case 'i': h64e.fmt.group = H64E_G_BYTE; h64e.fmt.flag_output = 0; h64e.fmt.flag_offset = 1; break; case 'l': param_length = atoi( optarg ); break; case 'c': h64e.fmt.column_size = atoi(optarg); break; case 'g': h64e.fmt.group = group_name2int(optarg); break; case 'e': h64e.fmt.flag_output = 1; h64e.fmt.output_type = output_name2int(optarg); break; case 's': h64e.fmt.flag_space = 1; break; case 'v': version(); exit(1); break; default: helper( argv[0] ); exit(1); } } if ( argc < 2 ) { printf("At least one argument needed now\n"); helper( argv[0] ); return 1; } /* last argument is file name */ fname = argv[argc-1]; /* check params */ h64e_check_param( &h64e ); /* open file */ fd = file_open( fname ); if ( fd == -1 ) { printf("Cannot open file %s\n", fname); return -1; } //register formats //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; 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; //do conversation, reading input and outputing while ((in_bytes = file_read(fd,(char *)&buf_in,1)) != -1) { 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"); switch (h64e.fmt.group) { case H64E_G_BYTE: cnv_num = h64e_fmt_byte(&int8_fmt, &buf_cnv_in[0], cnv_bytes, buf_cnv_out, 512); 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); break; default: printf("Unknown grouping\n"); } //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); printf("Ending Execution\n"); return 0; }