From 3e187c094f12ef41ea6de2f55d128c3e037b5c12 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Wed, 25 Sep 2019 19:17:09 +0100 Subject: Untested version, recompiled with new buf library and with c+ --- Makefile | 14 +- __cpp.h | 30 +++ buf.c | 122 ------------- buf.h | 46 ----- cmd/cmd_blk.c | 2 +- cmd/cmd_cd.c | 2 +- cmd/cmd_close.c | 10 +- cmd/cmd_dump.c | 10 +- cmd/cmd_dumps.c | 10 +- cmd/cmd_dumpx.c | 21 ++- cmd/cmd_flags.c | 2 +- cmd/cmd_info.c | 2 +- cmd/cmd_ls.c | 2 +- cmd/cmd_open.c | 2 +- cmd/cmd_pos.c | 2 +- cmd/cmd_pwd.c | 2 +- cmd/cmd_read.c | 17 +- cmd/cmd_resize.c | 2 +- cmd/cmd_seek.c | 2 +- cmd/cmd_size.c | 2 +- cmd/cmd_write.c | 24 ++- cmd/cmd_writes.c | 18 +- core.c | 36 ++-- core.h | 4 +- h64e/Makefile | 6 +- ihe.c | 14 +- ihe.h | 2 +- libbuf/Makefile | 29 +++ libbuf/buf.c | 534 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ libbuf/buf.h | 89 +++++++++ libbuf/buf_misc.c | 272 +++++++++++++++++++++++++++ libbuf/buf_misc.h | 64 +++++++ libcmd/Makefile | 4 +- libterm/Makefile | 4 +- 34 files changed, 1149 insertions(+), 253 deletions(-) create mode 100644 __cpp.h delete mode 100644 buf.c delete mode 100644 buf.h create mode 100644 libbuf/Makefile create mode 100644 libbuf/buf.c create mode 100644 libbuf/buf.h create mode 100644 libbuf/buf_misc.c create mode 100644 libbuf/buf_misc.h diff --git a/Makefile b/Makefile index bef2096..fa76dcd 100644 --- a/Makefile +++ b/Makefile @@ -1,25 +1,27 @@ PROJECT=ihe CC=gcc -CFLAGS=-I./ -SOURCES=buf.c core.c +CPP=g++ +CFLAGS=-I./ -I./libbuf -Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables +SOURCES=core.c SOURCES+=$(wildcard cmd/*.c) OBJECTS=$(SOURCES:.c=.o) -LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o h64e/core.o +LIB_OBJECTS=libcmd/libcmd.o libterm/libterm.o h64e/core.o libbuf/libbuf.o all: clean $(OBJECTS) $(PROJECT) $(PROJECT): - $(CC) -c $(PROJECT).c + $(CPP) -c $(PROJECT).c ld -r $(OBJECTS) $(PROJECT).o -o $(PROJECT)_.o - $(CC) $(CFLAGS) $(PROJECT)_.o $(LIB_OBJECTS) -o $(PROJECT) + $(CPP) $(CFLAGS) $(PROJECT)_.o $(LIB_OBJECTS) -o $(PROJECT) %.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ + $(CPP) $(CFLAGS) -c $< -o $@ clean: rm -f $(PROJECT) rm -f *.o + rm -f cmd/*.o leak: valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./ihe diff --git a/__cpp.h b/__cpp.h new file mode 100644 index 0000000..6a8e03e --- /dev/null +++ b/__cpp.h @@ -0,0 +1,30 @@ +#include + +extern "C" void* emulate_cc_new(unsigned long len) { \ + void *p = malloc(len); + if (p == 0) { + /* Don't use stdio (e.g. fputs), because that may want to allocate more + * memory. + */ + (void)!write(2, "out of memory\n", 14); + abort(); + } + return p; +} +extern "C" void emulate_cc_delete(void* p) { + if (p != 0) + free(p); +} + +extern "C" void emulate_cc_delete2(void* p, unsigned long len) { + if (p != 0) + free(p); +} + +void* operator new (unsigned long) __attribute__((alias("emulate_cc_new"))); +void* operator new[](unsigned long) __attribute__((alias("emulate_cc_new"))); +void operator delete (void* p) __attribute__((alias("emulate_cc_delete"))); +void operator delete[](void* p) __attribute__((alias("emulate_cc_delete"))); +void operator delete (void* p, unsigned long len) __attribute__((alias("emulate_cc_delete2"))); +void operator delete[](void* p, unsigned long len) __attribute__((alias("emulate_cc_delete2"))); +void* __cxa_pure_virtual = 0; \ No newline at end of file diff --git a/buf.c b/buf.c deleted file mode 100644 index d0519e5..0000000 --- a/buf.c +++ /dev/null @@ -1,122 +0,0 @@ -#include "buf.h" - -buf_t* buf_init() -{ - buf_t *ret = NULL; - - ret = malloc( sizeof(buf_t) ); - memset(ret, 0, sizeof(buf_t)); - - return ret; -} - - -int buf_not_null( buf_t *bf ) -{ - - if ( bf == NULL ) - return 0; - - if ( bf->buf == NULL ) - return 0; - - if ( bf->buf_size < 0 ) - return 0; - - return 1; -} - -int buf_size( buf_t *bf, int size ) -{ - if (bf->buf != NULL) - { - printf("Buffer should be empty\n"); - return -1; - } - - bf->buf = malloc( size ); - bf->size = size; - bf->buf_size = size; - - return 0; -} - - -int buf_used_size( buf_t *bf, int size ) -{ - if ( size > bf->buf_size ) - { - printf("Cannot set buffer size more then buffer itself\n"); - return -1; - } - - if ( size < 0) - { - printf("Cannot set buffer less then zero\n"); - return -1; - } - - bf->size = size; - - return 0; -} - - -int buf_resize( buf_t *bf, int size ) -{ - uint8_t *n=NULL; - - if ( size < 1) - { - printf("Cannot set buffer size less then 1\n"); - return -1; - } - - n = realloc( bf->buf, size ); //resized data is not nullified at the end? - if ( n == NULL ) - { - printf("Buffer realloc failed\n"); - return -1; - } - - if ( size > bf->buf_size ) - { - memset(bf->buf+bf->buf_size-1, 0, size-(bf->buf_size)); //hope its correct - } - - bf->buf_size = size; - bf->buf = n; - if (bf->size > bf->buf_size) - { - bf->size = bf->buf_size; - } - - return 0; -} - - -int buf_zero( buf_t *bf ) -{ - if ( bf->buf == NULL ) - return -1; - - memset( bf->buf, 0, bf->buf_size ); - - return 0; -} - - -void buf_free( buf_t *bf ) -{ - if ( bf == NULL ) - return; - - if ( bf->buf != NULL ) - { - free( bf->buf ); - bf->buf = NULL; - } - - free( bf ); - bf = NULL; -} diff --git a/buf.h b/buf.h deleted file mode 100644 index 72bbc45..0000000 --- a/buf.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef __IHE_BUF_H -#define __IHE_BUF_H - -#include -#include -#include -#include - -typedef struct buf_t -{ - uint8_t *buf; - int size; - int buf_size; -} buf_t; - -/* -create empty buffer structure -*/ -buf_t* buf_init(); - -int buf_not_null( buf_t *bf ); - -/* -if empty setup new buffer -if not empty resize? -*/ -int buf_size( buf_t *bf, int size ); -/* -set used buffer size -*/ -int buf_used_size( buf_t *bf, int size ); -/* -change buffer size -*/ -int buf_resize( buf_t *bf, int size ); -/* -make buffer full of zeros -*/ -int buf_zero( buf_t *bf ); -/* -clean all buffer -*/ -void buf_free( buf_t *bf ); - - -#endif \ No newline at end of file diff --git a/cmd/cmd_blk.c b/cmd/cmd_blk.c index 07f27f8..a568f98 100644 --- a/cmd/cmd_blk.c +++ b/cmd/cmd_blk.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; /* diff --git a/cmd/cmd_cd.c b/cmd/cmd_cd.c index 92c22b2..a24768c 100644 --- a/cmd/cmd_cd.c +++ b/cmd/cmd_cd.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_cd( cmd_arg_t *arg ) diff --git a/cmd/cmd_close.c b/cmd/cmd_close.c index d5a03b3..3db6a70 100644 --- a/cmd/cmd_close.c +++ b/cmd/cmd_close.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; /* @@ -23,10 +23,12 @@ int c_close( cmd_arg_t *arg ) } // clean buf - if ( g_buf->buf != NULL ) + if ( !g_buf->isempty() ) { - buf_zero( g_buf ); - buf_free( g_buf ); + //buf_zero( g_buf ); + g_buf->zero(); + //buf_free( g_buf ); + g_buf->empty(); } return 0; diff --git a/cmd/cmd_dump.c b/cmd/cmd_dump.c index bf2e5d1..999822e 100644 --- a/cmd/cmd_dump.c +++ b/cmd/cmd_dump.c @@ -5,22 +5,24 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_dump( cmd_arg_t *arg ) { int i; - if ( g_buf->buf == NULL) + if ( g_buf->isempty()) { printf("Buffer to print empty\n"); return -1; } - for (i=0; isize; i++) + for (i=0; isize(); i++) { - printf("%02x",(unsigned char)g_buf->buf[i]); + char c; + g_buf->getc(i,&c); + printf("%02x",(unsigned char)c); } printf("\n"); diff --git a/cmd/cmd_dumps.c b/cmd/cmd_dumps.c index ecc5143..61d4c84 100644 --- a/cmd/cmd_dumps.c +++ b/cmd/cmd_dumps.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_dumps( cmd_arg_t *arg ) @@ -20,11 +20,13 @@ int c_dumps( cmd_arg_t *arg ) return -1; } - for (i=0; isize; i++) + for (i=0; isize(); i++) { - if (isprint(g_buf->buf[i])) + char c; + g_buf->getc(i,&c); + if (isprint(c)) { - printf("%c", g_buf->buf[i]); + printf("%c", c); } else { printf("\e[7m.\e[0m"); diff --git a/cmd/cmd_dumpx.c b/cmd/cmd_dumpx.c index 3e6cdf1..ecca20c 100644 --- a/cmd/cmd_dumpx.c +++ b/cmd/cmd_dumpx.c @@ -8,26 +8,28 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_dumpx( cmd_arg_t *arg ) { int i,j; - if ( g_buf->buf == NULL) + if ( g_buf->isempty() ) { printf("Buffer to print empty\n"); return -1; } - for (i=0; isize; i+=16) + for (i=0; isize(); i+=16) { for (j=i; jsize ) + if ( jsize() ) { - printf("%02x ",(unsigned char)g_buf->buf[j]); + char c; + g_buf->getc(j,&c); + printf("%02x ",(unsigned char)c); } else { printf(" "); @@ -36,11 +38,14 @@ int c_dumpx( cmd_arg_t *arg ) for (j=i; jsize ) //wrong place move to cycle? + if ( jsize() ) //wrong place move to cycle? { - if ( isprint(g_buf->buf[j]) ) + char c; + g_buf->getc(j,&c); + if ( isprint(c) ) { - printf("%c",(unsigned char)g_buf->buf[j]); + + printf("%c",(unsigned char)c); } else { printf("\e[7m.\e[0m"); diff --git a/cmd/cmd_flags.c b/cmd/cmd_flags.c index 03f6b81..5e96651 100644 --- a/cmd/cmd_flags.c +++ b/cmd/cmd_flags.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_flags( cmd_arg_t *arg ) diff --git a/cmd/cmd_info.c b/cmd/cmd_info.c index 74324f6..01f58f6 100644 --- a/cmd/cmd_info.c +++ b/cmd/cmd_info.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; /* diff --git a/cmd/cmd_ls.c b/cmd/cmd_ls.c index 7e92db6..bb7781c 100644 --- a/cmd/cmd_ls.c +++ b/cmd/cmd_ls.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_ls( cmd_arg_t *arg ) diff --git a/cmd/cmd_open.c b/cmd/cmd_open.c index a4740c6..b7ea4f7 100644 --- a/cmd/cmd_open.c +++ b/cmd/cmd_open.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; /* diff --git a/cmd/cmd_pos.c b/cmd/cmd_pos.c index 05264d4..b040511 100644 --- a/cmd/cmd_pos.c +++ b/cmd/cmd_pos.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_pos( cmd_arg_t *arg ) diff --git a/cmd/cmd_pwd.c b/cmd/cmd_pwd.c index 4e21721..d35478d 100644 --- a/cmd/cmd_pwd.c +++ b/cmd/cmd_pwd.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_pwd( cmd_arg_t *arg ) diff --git a/cmd/cmd_read.c b/cmd/cmd_read.c index 68dab14..da1988c 100644 --- a/cmd/cmd_read.c +++ b/cmd/cmd_read.c @@ -5,23 +5,28 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_read( cmd_arg_t *arg ) { int ret; - if ( g_buf->buf == NULL ) + //if ( g_buf->buf == NULL ) + if (g_buf->isempty()) { - buf_resize( g_buf, g_file->blk_size ); + //buf_resize( g_buf, g_file->blk_size ); + g_buf->realloc(g_file->blk_size); } - ret = file_read_blk( g_file, g_buf->buf ); + char *buf_ptr; + int sz; + g_buf->get_ptr(&buf_ptr,&sz); + ret = file_read_blk( g_file, (uint8_t *)buf_ptr, sz ); printf("Readed %d bytes\n", ret); - if ( (ret >= 0) && (ret <= g_buf->buf_size) ) + if ( (ret >= 0) && (ret <= g_buf->size()) ) { - g_buf->size = ret; + g_buf->set_size(ret); } return 0; diff --git a/cmd/cmd_resize.c b/cmd/cmd_resize.c index 1e723cf..2b7fbd5 100644 --- a/cmd/cmd_resize.c +++ b/cmd/cmd_resize.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_resize( cmd_arg_t *arg ) diff --git a/cmd/cmd_seek.c b/cmd/cmd_seek.c index 7516811..d8bb768 100644 --- a/cmd/cmd_seek.c +++ b/cmd/cmd_seek.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_seek( cmd_arg_t *arg ) diff --git a/cmd/cmd_size.c b/cmd/cmd_size.c index 12723ef..3823923 100644 --- a/cmd/cmd_size.c +++ b/cmd/cmd_size.c @@ -5,7 +5,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; int c_size( cmd_arg_t *arg ) diff --git a/cmd/cmd_write.c b/cmd/cmd_write.c index d54c0b5..978e244 100644 --- a/cmd/cmd_write.c +++ b/cmd/cmd_write.c @@ -5,13 +5,10 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; -//support masks -int c_write( cmd_arg_t *arg) -{ - /* +/* anonymous function */ uint8_t hex2u8( uint8_t *buf ) @@ -30,6 +27,11 @@ int c_write( cmd_arg_t *arg) return ret; } +//support masks +int c_write( cmd_arg_t *arg) +{ + + int argc = arg->argc; char **argv = arg->argv; int i; @@ -57,7 +59,7 @@ int c_write( cmd_arg_t *arg) } } - if (strlen(argv[0]) > g_buf->size*2) + if (strlen(argv[0]) > g_buf->size()*2) { printf("Input param bigger then buffer\n"); return -1; @@ -70,14 +72,18 @@ int c_write( cmd_arg_t *arg) printf("\n"); - buf = malloc(strlen(argv[0])/2); + buf = (uint8_t *)malloc(strlen(argv[0])/2); for (i=0; i<(strlen(argv[0])/2); i++) { buf[i] = hex2u8((unsigned char*)&argv[0][i*2]); } - memcpy( g_buf->buf, buf, strlen(argv[0])/2 ); - fret = file_write_blk( g_file, g_buf->buf ); + char *buf_ptr; + int sz; + //memcpy( buf_ptr, buf, strlen(argv[0])/2 ); + g_buf->set((char *)buf,strlen(argv[0])/2); + g_buf->get_ptr(&buf_ptr,&sz); + fret = file_write_blk( g_file, (uint8_t *)buf_ptr, sz ); free( buf ); if ( fret < 0) diff --git a/cmd/cmd_writes.c b/cmd/cmd_writes.c index 7d7eeed..0b555be 100644 --- a/cmd/cmd_writes.c +++ b/cmd/cmd_writes.c @@ -7,7 +7,7 @@ #include "libcmd/cmd_parse.h" extern file_t *g_file; -extern buf_t *g_buf; +extern Buf *g_buf; extern int g_flags; //white spaces should be supported @@ -23,16 +23,22 @@ int c_writes( cmd_arg_t *arg ) return -1; } - if (((g_buf == NULL) || (g_file == NULL)) || (g_buf->buf == NULL)) + if (((g_buf == NULL) || (g_file == NULL))) { printf("Buffer or file not initialised"); return -1; } - if ( strlen(argv[0]) <= g_buf->size ) + if ( strlen(argv[0]) <= g_buf->size() ) { - memcpy( g_buf->buf, argv[0], strlen(argv[0]) ); - fret = file_write_blk( g_file, g_buf->buf ); + //memcpy( g_buf->buf, argv[0], strlen(argv[0]) ); + //check if can save all string in dat buffer + g_buf->set(argv[0],strlen(argv[0])); + + char *buf_ptr; + int sz; + g_buf->get_ptr(&buf_ptr,&sz); + fret = file_write_blk( g_file, (uint8_t *)buf_ptr, sz ); if ( fret < 0 ) { printf("Couldnt write block to file\n"); @@ -40,7 +46,7 @@ int c_writes( cmd_arg_t *arg ) } } else { - printf("Input bigger then buffer buf %d input %zu\n", g_buf->size, strlen(argv[0])); + printf("Input bigger then buffer buf %d input %zu\n", g_buf->size(), strlen(argv[0])); return -1; } diff --git a/core.c b/core.c index 7de3085..1fe2891 100644 --- a/core.c +++ b/core.c @@ -98,7 +98,7 @@ file_t *file_init() { file_t *ret = NULL; - ret = malloc( sizeof(file_t) ); + ret = (file_t *)malloc( sizeof(file_t) ); memset( ret, 0, sizeof(file_t) ); ret->flags = FD_RO; //di we really need that? @@ -123,7 +123,7 @@ int file_open_fn( file_t *ft, const char *filename, int flags ) ft->flags = flags; uint8_t fn_sz = strlen( filename ); - char *fn = malloc( fn_sz ); + char *fn = (char *)malloc( fn_sz ); memcpy( fn, filename, fn_sz ); ft->filename = fn; @@ -230,7 +230,7 @@ int file_resize( file_t *ft, size_t size ) { return -1; } - buf = malloc( size ); + buf = (uint8_t *)malloc( size ); memset( buf, 0, size ); fd_seek( ft->fd, cur_size, FD_SEEK_SET ); resize = fd_write( ft->fd, buf, size ); @@ -251,7 +251,7 @@ int file_open( file_t *ft, const char *filename, int flags, int mode ) } -int file_read_blk( file_t *ft, uint8_t *buf ) +int file_read_blk( file_t *ft, uint8_t *buf, int sz ) { int ret = 0; @@ -259,7 +259,12 @@ int file_read_blk( file_t *ft, uint8_t *buf ) return -1; file_pos( ft ); - ret = fd_read( ft->fd, buf, ft->blk_size ); + int read_sz = sz; + if (read_sz > ft->blk_size) + { + read_sz = ft->blk_size; + } + ret = fd_read( ft->fd, buf, read_sz ); fd_set_pos( ft->fd, ft->position ); //chck err? if ( ret < 0) @@ -288,21 +293,26 @@ int file_read( file_t *ft, uint8_t *buf, size_t count ) } -int file_write_blk( file_t *ft, uint8_t *buf ) +int file_write_blk( file_t *ft, uint8_t *buf, int sz ) { int ret = 0; - unsigned int sz; + unsigned int write_sz; file_pos( ft ); if ( ft->position + ft->blk_size <= ft->size ) { - sz = ft->blk_size; + write_sz = ft->blk_size; } else { - sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte + write_sz = ft->size - ft->position; //when pos 0 ans size 1 then will write 1 byte + } + + if (write_sz > sz) + { + write_sz = sz; } - ret = fd_write( ft->fd, buf, sz ); + ret = fd_write( ft->fd, buf, write_sz ); if ( ret < 0 ) { printf("Error while writing block to file\n"); @@ -394,12 +404,12 @@ uint8_t **dir_list( char *path) //count one more in da list cnt += 1; //lets alloc pointer on pointer where we put pointer - new_ptr = realloc( ret, sizeof(uint8_t*)*(cnt) ); + new_ptr = (uint8_t **)realloc( ret, sizeof(uint8_t*)*(cnt) ); if ( new_ptr == NULL ) goto failed_realloc; ret = new_ptr; str_sz = strlen(ep->d_name); - ret[cnt-1] = malloc( str_sz+1 ); + ret[cnt-1] = (uint8_t *)malloc( str_sz+1 ); memcpy( ret[cnt-1], ep->d_name, str_sz ); ret[cnt-1][str_sz] = 0; } @@ -412,7 +422,7 @@ uint8_t **dir_list( char *path) } //add NULL element at the end - new_ptr = realloc( ret, sizeof(uint8_t*)*(cnt+1) ); + new_ptr = (uint8_t **)realloc( ret, sizeof(uint8_t*)*(cnt+1) ); if ( new_ptr == NULL ) goto failed_add_entry; ret = new_ptr; diff --git a/core.h b/core.h index 638cf3c..152d6ca 100644 --- a/core.h +++ b/core.h @@ -98,9 +98,9 @@ typedef struct file_t file_t *file_init(); int file_open_fn( file_t *ft, const char *filename, int mode ); int file_open( file_t *ft, const char *filename, int flags, int mode ); -int file_read_blk( file_t *ft, uint8_t *buf ); +int file_read_blk( file_t *ft, uint8_t *buf, int sz ); int file_read( file_t *ft, uint8_t *buf, size_t count ); -int file_write_blk( file_t *ft, uint8_t *buf ); +int file_write_blk( file_t *ft, uint8_t *buf, int sz ); int file_write( file_t *ft, uint8_t *buf, size_t count ); int file_seek( file_t *ft, off_t offset ); //seek by offset int file_seekp( file_t *ft, off_t offset ); diff --git a/h64e/Makefile b/h64e/Makefile index 074800b..8bc4bab 100644 --- a/h64e/Makefile +++ b/h64e/Makefile @@ -1,4 +1,4 @@ -CC=gcc -CFLAGS= +CC=g++ +CFLAGS=-Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables make: - $(CC) -c core.c \ No newline at end of file + $(CC) $(CFLAGS) -c core.c \ No newline at end of file diff --git a/ihe.c b/ihe.c index 641b09b..4390586 100644 --- a/ihe.c +++ b/ihe.c @@ -1,6 +1,8 @@ #include "ihe.h" +#include "__cpp.h" #include "core.h" + /* Global variables */ cmd_mng_t cmd_mng; static int cmd_loop = 1; @@ -239,7 +241,8 @@ int cmd_buf_full( cmd_in_buf_t *buf ) GLOBAL VARIABLES */ file_t *g_file = NULL; -buf_t *g_buf = NULL; +//buf_t *g_buf = NULL; +Buf *g_buf = NULL; int g_flags = FD_RW; @@ -454,12 +457,15 @@ int main( int argc, char **argv ) memset( &tl, 0, sizeof( cmd_tok_t )); //preapre global stuff + g_file = file_init(); //init basic buffer - g_buf = buf_init(); - buf_size( g_buf, DEFAULT_BLK_SIZE ); - buf_zero( g_buf ); + //#warning "replace" + g_buf = new Buf(DEFAULT_BLK_SIZE); + //g_buf = buf_init(); + //buf_size( g_buf, DEFAULT_BLK_SIZE ); + //buf_zero( g_buf ); struct term_screen ts; memset( &ts, 0, sizeof(ts) ); diff --git a/ihe.h b/ihe.h index 621702f..6f553c6 100644 --- a/ihe.h +++ b/ihe.h @@ -6,7 +6,7 @@ #include #include -#include "buf.h" +#include "libbuf/buf.h" #include "core.h" #include "libcmd/cmd.h" diff --git a/libbuf/Makefile b/libbuf/Makefile new file mode 100644 index 0000000..ed2f1de --- /dev/null +++ b/libbuf/Makefile @@ -0,0 +1,29 @@ +CPP=g++ +CC=gcc +LD=ld +CFLAGS=-fPIC -g3 -Wall -Wwrite-strings -fno-exceptions -fno-unwind-tables -fpermissive +CPPFLAGS=-fPIC -g3 -Wall -Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables + +make: + $(CPP) $(CPPFLAGS) -c buf.c + $(CPP) $(CPPFLAGS) -c buf_misc.c + #$(CPP) $(CFLAGS) -c buf_sys.c + #$(CC) $(CFLAGS) -fPIC -lc -ldl buf_sys.o buf_misc.o buf.o -shared -o libbuf.so + $(CC) $(CFLAGS) -fPIC -lc -ldl buf.o buf_misc.o -shared -o libbuf.so + $(LD) -r buf.o buf_misc.o -o libbuf.o + +t: buf.c buf_misc.c buf_sys.c + $(CPP) $(CPPFLAGS) test.c -c + $(CPP) $(CPPFLAGS) test_line.c -c + $(CC) $(CFLAGS) buf.o test.o -o test + #$(CC) $(CFLAGS) buf.o buf_misc.o buf_sys.o test_circ.c -o test_circ + #$(CC) $(CFLAGS) buf.o buf_misc.o buf_sys.o test_circ.c -o test_sys + $(CC) $(CFLAGS) buf.o buf_misc.o test_line.o -o test_line + +leak: + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test + +clean: + rm -rf *.so *.o test test_circ test_sys + + diff --git a/libbuf/buf.c b/libbuf/buf.c new file mode 100644 index 0000000..83457e9 --- /dev/null +++ b/libbuf/buf.c @@ -0,0 +1,534 @@ +#include "buf.h" + +/* +bbuf* bbuf_new(int size) +{ + bbuf *ret = NULL; + ret = malloc(sizeof(bbuf)); + if (!ret) + { + return NULL; + } + ret->buf = malloc(size); + if (!ret->buf) + { + free(ret); + return NULL; + } + ret->buf_size = size; + ret->size = 0; + return ret; +} + +//set buffer value +int bbuf_set(bbuf *a, char *val, int size) +{ + if (!a) + return -1; + + if (!a->buf) + return -1; + + if (size<0) + return -1; + + if (size > a->buf_size) + { + a->size = a->buf_size; + } else + { + a->size = size; + } + memcpy(a->buf, val, a->size); + + return a->size; +} + + +//get copy of buffer +bbuf *bbuf_copy(bbuf *buf) +{ + bbuf *ret = NULL; + + if (!buf) + { + return NULL; + } + + ret = malloc(sizeof(bbuf)); + memcpy(ret, buf, sizeof(bbuf)); + ret->buf = malloc(buf->buf_size); + if (!ret->buf) + { + free(ret); + return NULL; + } + memcpy(ret->buf, buf->buf, buf->buf_size); + ret->size = buf->size; + ret->buf_size = buf->buf_size; + + return ret; +} + + +//get buffer from buffer, mapped pointer, plz dont modify +int bbuf_get(bbuf *buf, char **val, int *size) +{ + if (!buf) + { + return -1; + } + + if (!val) + { + return -1; + } + + *size = buf->size; + *val = buf->buf; + + return 0; +} + + +//resize buffer +int bbuf_realloc(bbuf *buf, int size) +{ + int ret = -1; + char *ptr=NULL; + if (!buf) + return -1; + if (size < 0) + return -1; + if (buf->buf_size == size) + return size; + + ret = buf->buf_size; + ptr = realloc(buf->buf, size); + if (ptr != NULL) + { + buf->buf = ptr; + ret = size; + } + + return ret; +} + +int bbuf_reduce(bbuf *buf) +{ + printf("Not implemented\n"); + return -1; +} + + +//increase buffer for size +int bbuf_inc(bbuf *a, char *b, int size) +{ + printf("Not implemented\n"); + return -1; +} + + +//decrease buffer for size +int bbuf_dec(bbuf *a, char *b, int size) +{ + printf("Not implemented\n"); + return -1; +} + +int bbuf_set_max(bbuf *buf) +{ + if (buf == NULL) + { + return -1; + } + + buf->buf_size = buf->size; + return 0; +} + + +int bbuf_memset(bbuf *buf, char c) +{ + if (buf == NULL) + { + return -1; + } + memset(buf->buf, c, buf->size); + return 0; +} + +int bbuf_concat(bbuf *a, bbuf *b) +{ + printf("Not implemented\n"); + return -1; +} + +int bbuf_size(bbuf *a) +{ + if (a == NULL) + { + return -1; + } + + //return used size + return a->buf_size; +} + +int bbuf_print(bbuf *a) +{ + int i; + + if (NULL == a) + { + return -1; + } + + for (i=0;ibuf_size;i++) + { + char ch = a->buf[i]; + if (isalpha(ch)) + { + printf("%c",ch); + } else + { + printf("."); + } + } + + return 0; +} + +//free buffer +void bbuf_free(bbuf *buf) +{ + if (buf == NULL) + { + return; + } + if (buf->buf != NULL) + { + memset(buf->buf, 0, buf->buf_size); + free(buf->buf); + buf->buf = NULL; + memset(buf, 0, sizeof(bbuf)); + free(buf); + buf = NULL; + } +} +*/ + +Buf::Buf(int size) +{ + this->buf = (char *)malloc(size); + if (!this->buf) + { + //put error here + } + + this->buf_size = size; + this->cur_size = 0; +} + +Buf::~Buf() +{ + if (this->buf != NULL) + { + ::memset(this->buf, 0, this->buf_size); + free(this->buf); + this->buf = NULL; + } +} + +int Buf::set(char *val, int size) +{ + if (!val) + { + return -1; + } + + if (size<0) + { + return -1; + } + + if (size > this->buf_size) + { + this->cur_size = this->buf_size; + } else + { + this->cur_size = size; + } + memcpy(this->buf, val, this->cur_size); + + return this->cur_size; +} + +int Buf::set(char *val) +{ + int size = strlen(val); + if (size > this->buf_size) + { + this->cur_size = this->buf_size; + } else + { + this->cur_size = size; + } + memcpy(this->buf, val, this->cur_size); + return this->cur_size; +} + +Buf* Buf::copy() +{ + Buf *ret = new Buf(this->buf_size); + char *data=NULL; + int data_size=0; + + this->get_ptr(&data, &data_size); + ret->set(data, data_size); + data=NULL; + return ret; +} + +int Buf::get_ptr(char **val, int *size) +{ + if (!val) + { + return -1; + } + + *val = this->buf; + *size = this->cur_size; + + return 0; +} + +int Buf::realloc(int size) +{ + char *ptr=NULL; + if (size < 0) + { + return -1; + } + if (this->buf_size == size) + { + return this->buf_size; + } + + ptr = (char *)::realloc(this->buf, size); + if (ptr != NULL) + { + this->buf = ptr; + this->buf_size = size; + } + + return this->buf_size; +} + +int Buf::reduce() +{ + printf("Not implemented\n"); + return -1; +} + +int Buf::inc(int size) +{ + printf("Not implemented\n"); + return -1; +} + +int Buf::dec(int size) +{ + printf("Not implemented\n"); + return -1; +} + +int Buf::set_max() +{ + this->cur_size = this->buf_size; + return 0; +} + +int Buf::memset(char c) +{ + ::memset(this->buf, c, this->cur_size); + return 0; +} + +int Buf::concat(Buf *b) +{ + + int i=0,j=this->cur_size; + int get_size=b->cursize(); + + for (i=0;igetc(i,&tc); + if (r==0) + { + this->pushc(tc); + if (j+1>=this->buf_size) + { + break; + } + } + } + + return i; +} + +int Buf::size() +{ + return this->buf_size; +} + +int Buf::cursize() +{ + return this->cur_size; +} + +int Buf::print() +{ + int i; + + for (i=0;ibuf_size;i++) + { + char ch = this->buf[i]; + if (isalpha(ch)) + { + printf("%c",ch); + } else + { + printf("."); + } + } + + return 0; +} + +int Buf::setc(int idx, char c) +{ + if ((idx < 0) || (idx > this->cur_size)) + { + return -1; + } + + buf[idx] = c; + + return 0; +} + +int Buf::pushc(char c) +{ + if (this->cur_sizebuf_size) + { + this->buf[this->cur_size] = c; + this->cur_size+=1; + return 0; + } + return -1; +} + +int Buf::getc(int idx, char *c) +{ + if ((idx < 0) || (idx > this->cur_size)) + { + return -1; + } + + *c = buf[idx]; + + return 0; +} + +int Buf::findc(char c, int *idx) +{ + int i=0; + + for (i=0;icur_size;i++) + { + if (buf[i] == c) + { + //found + *idx = i; + return 1; + } + } + + //not found + return 0; +} + +int Buf::popsubstring(int sz, char **val, int *size) +{ + int ret=-1; + + if (sz>this->cur_size) + { + sz = this->cur_size; + } + + *val = (char *)malloc(sz); + memcpy(*val,buf,sz); + *size = sz; + shiftleft(sz); + ret = sz; + + return ret; +} + +int Buf::shiftleft(int n) +{ + int i=0; + + if ((n>this->cur_size) || (n<0)) + { + //cant shift + return -1; + } + int mvsz = cur_size-n; + for (i=0;icur_size -= n; + + //amount of shifted bytes + return n; +} + +int Buf::zero() +{ + this->memset(0); + return 0; +} + +bool Buf::isempty() +{ + if (this->buf == NULL) + return true; + if (this->cur_size <= 0) + return true; + if (this->buf_size <= 0) + return true; + return true; +} + +int Buf::empty() +{ + this->cur_size = 0; + return 0; +} + +int Buf::set_size(int size) +{ + if (size < 0) + return -1; + if (size > this->buf_size) + return -1; + this->cur_size = size; + return 0; +} \ No newline at end of file diff --git a/libbuf/buf.h b/libbuf/buf.h new file mode 100644 index 0000000..ba5c7e7 --- /dev/null +++ b/libbuf/buf.h @@ -0,0 +1,89 @@ +#ifndef __BUF_H +#define __BUF_H + +#include +#include +#include +#include +#include + +/* +typedef struct bbuf { + int size; //total size + int buf_size; //used size + char *buf; +} bbuf; +*/ + +//operations on plain buffers also operations on bufer to buffer should be +//supoorted + +/* +//create new buffer without content +bbuf* bbuf_new(int size); + +//set buffer value +int bbuf_set(bbuf *a, char *val, int size); +//get copy of buffer +bbuf *bbuf_copy(bbuf *buf); +//get buffer from buffer +int bbuf_get(bbuf *buf, char **val, int *size); +//resize buffer +int bbuf_realloc(bbuf *buf, int size); +//set to minimal size +int bbuf_reduce(bbuf *buf); +//increase buffer for size +int bbuf_inc(bbuf *a, char *b, int size); +//decrease buffer for size +int bbuf_dec(bbuf *a, char *b, int size); +//free buffer +int bbuf_set_max(bbuf *buf); +int bbuf_memset(bbuf *buf, char c); + +int bbuf_concat(bbuf *a, bbuf *b); + +int bbuf_size(bbuf *a); + +int bbuf_print(bbuf *a); + +void bbuf_free(bbuf *buf); +*/ + +class Buf +{ +private: + int cur_size; + int buf_size; + char *buf; +public: + Buf(int size); + ~Buf(); + + int set(char *val, int size); + int set(char *val); + Buf* copy(); + int get_ptr(char **val, int *size); + int realloc(int size); + int reduce(); + int inc(int size); + int dec(int size); + + int set_max(); + int memset(char c); + int concat(Buf *b); + int size(); + int cursize(); + int print(); + int setc(int idx, char c); + int pushc(char c); + int getc(int idx, char *c); + int findc(char c, int *idx); + int popsubstring(int sz, char **val, int *size); + int shiftleft(int n); + int zero(); + bool isempty(); + int empty(); + int set_size(int size); +}; + +#endif \ No newline at end of file diff --git a/libbuf/buf_misc.c b/libbuf/buf_misc.c new file mode 100644 index 0000000..386be72 --- /dev/null +++ b/libbuf/buf_misc.c @@ -0,0 +1,272 @@ +#include "buf_misc.h" + +/* +int bbuf_line_new(bbuf_line *buf, int size) +{ + bbuf *newbuf=NULL; + bbuf_line *bline=NULL; + + newbuf = bbuf_new(size); + if (newbuf == NULL) + { + + return -1; + } + + bline = malloc(sizeof(bbuf_line)); + if (bline == NULL) + { + return -1; + } + + memset(bline, 0, sizeof(bbuf_line)); + + bline->buf = newbuf; + + buf = bline; + + return 0; +} + +int bbuf_line_pattern(bbuf_line *buf, char *patt, int size) +{ + return -1; +} + +int bbuf_line_add(bbuf_line *buf_line, bbuf *new_data) +{ + + return -1; +} + + +int bbuf_line_get_line(bbuf_line *buf_line, bbuf *line) +{ + + return -1; +} + + +int bbuf_line_free(bbuf_line *buf) +{ + + return -1; +} + +*/ + +BufLine::BufLine(int size) +{ + buf = new Buf(size); + sep=0x0; + memset(pattern,0,BUF_PATTER_SIZE); +} + +BufLine::~BufLine() +{ + delete buf; +} + +int BufLine::setpattern(char *pattern) +{ + return -1; +} + +int BufLine::setseperator(char s) +{ + this->sep = s; + return 0; +} + +int BufLine::add(char *string, int size) +{ + return -1; +} + +int BufLine::add(Buf *newdata) +{ + buf->concat(newdata); + return 0; +} + +int BufLine::pop_line(char **val, int *size) +{ + int idx; + if (1 == buf->findc('\n',&idx)) + { + buf->popsubstring(idx,val,size); + buf->shiftleft(1);//remove seperator character + return 0; + } + return -1; +} + +int BufLine::print() +{ + buf->print(); + return -1; +} + +/* +RETURN_IF_PATTERN_FOUND(*detect_pattern)(char *INPUT_BUFFER, int INPUT_SIZE) +*/ +int BufLine::pop_pattern(int (*detect_pattern)(char *, int, int*), char **val, int *size) +{ + int detected=0; + + char *buf; + int buf_size = 0; + int data_size = 0; + + this->buf->get_ptr(&buf, &buf_size); + if ((detected = detect_pattern(buf, buf_size, &data_size)) != 1) + { + return -1; + } + + this->buf->popsubstring(data_size, val, size); + + return 0; +} + +/* +int bbuf_circ_new(bbuf_circ **circ, int size) +{ + bbuf *buf = NULL; + bbuf_circ *cbuf = NULL; + + buf = bbuf_new(size); + if (buf == NULL) + { + return -1; + } + + cbuf = malloc(sizeof(bbuf_circ)); + if (cbuf == NULL) + { + return -1; + } + + cbuf->buf = buf; + cbuf->head = 0; + cbuf->tail = 0; + + *circ = cbuf; + + return 0; +} + +int bbuf_circ_add(bbuf_circ *circ, bbuf *new_data) +{ + int i=0; + int cnt=0; + + if (circ == NULL) + { + return -1; + } + + if (new_data == NULL) + { + return -1; + } + + if (bbuf_size(new_data) <= 0) + { + return -1; + } + + for (i=0;ibuf[i])) + { + cnt++; + } + } + + return cnt; +} + +int bbuf_circ_get_line(bbuf_circ *circ, bbuf *line) +{ + +} + +int bbuf_circ_reset(bbuf_circ *circ) +{ + if (circ == NULL) + { + return -1; + } + + circ->head = 0; + circ->tail = 0; + + return 0; +} + + +int bbuf_circ_get(bbuf_circ *circ, char *data) +{ + if (circ == NULL) + { + return -1; + } + + if (data == NULL) + { + return -1; + } + + if (0 == bbuf_circ_empty(circ)) + { + *data = circ->buf->buf[circ->tail]; + circ->tail = (circ->tail+1)%bbuf_size(circ->buf); + return 0; + } + + return -1; +} + +int bbuf_circ_put(bbuf_circ *circ, char data) +{ + if (NULL == circ) + { + return -1; + } + + circ->buf->buf[circ->head] = data; + circ->head = (circ->head+1)%bbuf_size(circ->buf); + + if (circ->head == circ->tail) + { + circ->tail = (circ->tail+1)%bbuf_size(circ->buf); + } + + return 0; +} + +int bbuf_circ_empty(bbuf_circ *circ) +{ + if (circ == NULL) + { + return -1; + } + + return (circ->tail == circ->head); +} + +int bbuf_circ_full(bbuf_circ *circ) +{ + //one lost byte + return ((circ->head+1)%bbuf_size(circ->buf) == (circ->tail)); +} + +int bbuf_circ_free(bbuf_circ *circ) +{ + bbuf_free(circ->buf); + circ->buf = NULL; + free(circ); + circ = NULL; +} +*/ \ No newline at end of file diff --git a/libbuf/buf_misc.h b/libbuf/buf_misc.h new file mode 100644 index 0000000..ded41d7 --- /dev/null +++ b/libbuf/buf_misc.h @@ -0,0 +1,64 @@ +#ifndef __LIBBUF_MISC_H +#define __LIBBUF_MISC_H + +#include "buf.h" + +#define BUF_PATTER_SIZE 2 + +/* +//line buffer, put data get line out +typedef struct bbuf_line +{ + bbuf *buf; + char pattern[BUF_PATTER_SIZE]; + char sep; //seperator character +} bbuf_line; + +int bbuf_line_new(bbuf_line *buf, int size); +int bbuf_line_pattern(bbuf_line *buf, char *patt, int size); +int bbuf_line_add(bbuf_line *buf_line, bbuf *new_data); +int bbuf_line_get_line(bbuf_line *buf_line, bbuf *line); +int bbuf_line_free(bbuf_line *buf); +*/ + +class BufLine +{ +private: + Buf *buf; + char pattern[BUF_PATTER_SIZE]; + char sep; //seperator character +public: + BufLine(int size); + ~BufLine(); + int setpattern(char *pattern); + int setseperator(char s); + int add(char *string, int size); + int add(Buf *newdata); + int pop_line(char **val, int *size); + int print(); + int pop_pattern(int (*detect_pattern)(char *, int, int*), char **val, int *size); +}; + + +//formating buf ops + +//circular buffer +/* +typedef struct bbuf_circ +{ + bbuf *buf; + int head; + int tail; +} bbuf_circ; + +int bbuf_circ_new(bbuf_circ **circ, int size); +int bbuf_circ_add(bbuf_circ *circ, bbuf *new_data); +int bbuf_circ_get_line(bbuf_circ *circ, bbuf *line); +int bbuf_circ_reset(bbuf_circ *circ); +int bbuf_circ_get(bbuf_circ *circ, char *data); +int bbuf_circ_put(bbuf_circ *circ, char data); +int bbuf_circ_empty(bbuf_circ *circ); +int bbuf_circ_full(bbuf_circ *circ); +int bbuf_circ_free(bbuf_circ *circ); +*/ +#endif diff --git a/libcmd/Makefile b/libcmd/Makefile index 7d67c5d..b1fafd2 100644 --- a/libcmd/Makefile +++ b/libcmd/Makefile @@ -1,6 +1,6 @@ PROJ=libcmd -CC=gcc -CFLAGS=-g3 +CC=g++ +CFLAGS=-Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables LDFLAGS= RAGEL=ragel DOT=dot diff --git a/libterm/Makefile b/libterm/Makefile index 759b3f5..2ba9f7d 100644 --- a/libterm/Makefile +++ b/libterm/Makefile @@ -1,7 +1,7 @@ PROJECT=libterm -CC=gcc +CC=g++ LD=ld -CFLAGS=-g3 +CFLAGS=-Wwrite-strings -fpermissive -fno-rtti -fno-exceptions -fno-unwind-tables make: $(CC) $(CFLAGS) -c term.c -- cgit v1.2.3