diff options
author | ZoRo <dos21h@gmail.com> | 2018-01-15 19:25:03 +0000 |
---|---|---|
committer | ZoRo <dos21h@gmail.com> | 2018-01-15 19:25:03 +0000 |
commit | a8b9b15b4f02ce3c6c7eac0d9393c44cb3fc668d (patch) | |
tree | 5ff9a31d852e26185075156923dc86d5a3205682 | |
download | libbuf-a8b9b15b4f02ce3c6c7eac0d9393c44cb3fc668d.tar.gz libbuf-a8b9b15b4f02ce3c6c7eac0d9393c44cb3fc668d.zip |
Initial commit
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | Makefile | 17 | ||||
-rw-r--r-- | buf.c | 156 | ||||
-rw-r--r-- | buf.h | 39 | ||||
-rw-r--r-- | debug.h | 99 | ||||
-rw-r--r-- | test.c | 82 |
6 files changed, 397 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ad9b273 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +log.txt +*.so +*.o +*.a diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ff53f43 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +CC=gcc +CFLAGS=-g3 + +make: + $(CC) -c buf.c + $(CC) $(CFLAGS) -fPIC -lc -ldl buf.c -shared -o libbuf.so + +test: buf.c + $(CC) $(CFLAGS) buf.o test.c -o test + +leak: + valgrind --leak-check=full --track-origins=yes --log-file=log.txt ./test + +clean: + rm -rf *.so *.o + + @@ -0,0 +1,156 @@ +#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; +} + + +//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; +} + + +//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); + } +} + +int bbuf_concat(bbuf *a, bbuf *b) +{ + printf("Not implemented\n"); + return -1; +} + @@ -0,0 +1,39 @@ +#ifndef __BUF_H +#define __BUF_H + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct bbuf { + int size; + int buf_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); +//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 +void bbuf_free(bbuf *buf); + +int bbuf_concat(bbuf *a, bbuf *b); + +void bbuf_free(bbuf *buf); + +#endif
\ No newline at end of file @@ -0,0 +1,99 @@ +#ifndef __RB_DEBUG_UTILS_H +#define __RB_DEBUG_UTILS_H + +//what about kprintf? + +//config options +#define PRINTF printf +#define COLORIZE +#define PRINT_LINENUM +#define PRINT_FILENAME +#define PRINT_DEBUG + + +//use color +#ifdef COLORIZE + #define D_COLOR "1;32m" + #define D_COLOR_S "\033[" D_COLOR + #define D_COLOR_E "\033[0m" + #define E_COLOR "1;31m" + #define E_COLOR_S "\033[" E_COLOR + #define E_COLOR_E "\033[0m" + #define W_COLOR "1;35m" + #define W_COLOR_S "\033[" W_COLOR + #define W_COLOR_E "\033[0m" + #define PE_COLOR "1;33m" + #define PE_COLOR_S "\033[" PE_COLOR + #define PE_COLOR_E "\033[0m" +#else + #define D_COLOR + #define D_COLOR_S + #define D_COLOR_E + #define E_COLOR + #define E_COLOR_S + #define E_COLOR_E + #define W_COLOR + #define W_COLOR_S + #define W_COLOR_E + #define PE_COLOR + #define PE_COLOR_S + #define PE_COLOR_E +#endif + +//print debug line +#ifdef PRINT_LINENUM + #define PRINT_LINE_F "LINE:%d " + #define PRINT_LINE_D __LINE__ +#else + #define PRINT_LINE_F "" + #define PRINT_LINE_D "" +#endif + +//print +#ifdef PRINT_FILENAME + #define PRINT_FILE_F "FILE:%s " + #define PRINT_FILE_D __FILE__ +#else + #define PRINT_FILE_F "" + #define PRINT_FILE_D "" +#endif + +//print debug string +#ifdef PRINT_DEBUG + #define PRINT_DEBUG_F "Debug: " + #define PRINT_WARNING_F "WARN: " + #define PRINT_PERROR_F "PRME: " +#else + #define PRINT_DEBUG_F "" + #define PRINT_WARNING_F "" + #define PRINT_PERROR_F "" +#endif + +#define PRINT( format, args ... ) PRINTF( D_COLOR_S PRINT_DEBUG_F \ + PRINT_FILE_F PRINT_LINE_F format D_COLOR_E, PRINT_FILE_D, \ + PRINT_LINE_D, ##args); + +#define ERROR( format, args ... ) PRINTF( E_COLOR_S PRINT_DEBUG_F \ + PRINT_FILE_F PRINT_LINE_F format E_COLOR_E, PRINT_FILE_D, \ + PRINT_LINE_D, ##args); + +#define WARNING( format, args ... ) PRINTF( W_COLOR_S PRINT_WARNING_F \ + PRINT_FILE_F PRINT_LINE_F format W_COLOR_E, PRINT_FILE_D, \ + PRINT_LINE_D, ##args); + +#define PERROR( format, args ... ) PRINTF( PE_COLOR_S PRINT_WARNING_F \ + PRINT_FILE_F PRINT_LINE_F format PE_COLOR_E, PRINT_FILE_D, \ + PRINT_LINE_D, ##args); + +#define PNL() PRINT("\n"); + +#define ENL() ERROR("\n"); + +#define WRN() WARNING("\n"); + +#define PERM() PERROR(); + +/*ERROR LEVEL TYPES*/ + + +#endif @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "buf.h" +#include "debug.h" + +#define BUF_SIZE_1 1024 +#define BUF_SIZE_2 2048 + +void blow_if_error(int err) +{ + if (err < 0) + { + printf("Should be no error but there is\n"); + exit(1); + } +} + +#define B(X) PNL();blow_if_error(X); + +void error_is_good(int err) +{ + if (err >= 0) + { + printf("Should be error but there is no\n"); + exit(1); + } +} + +void print_s(char *str, int sz) +{ + int i = 0; + for (i=0;i<sz;i++) + printf("%c",str[i]); + fflush(stdout); +} + +int main() +{ + bbuf *b1=NULL, *b2=NULL; + int ret; + + printf("Start test\n"); + + bbuf_free(b1); + + printf("Create new buffer\n"); + b1 = bbuf_new(BUF_SIZE_1); + bbuf_free(b1); + + printf("Copy buffer\n"); + b1 = bbuf_new(BUF_SIZE_1); + b2 = bbuf_copy(b1); + bbuf_free(b1); + bbuf_free(b2); + + + printf("Get buffer\n"); + char name1[] = "Lets get buffer and print it out"; + char *get_val = NULL; + int get_size; + b1 = bbuf_new(BUF_SIZE_1); + B(bbuf_set(b1,name1,strlen(name1))); + B(bbuf_get(b1,&get_val,&get_size)); + print_s(get_val,get_size);printf("\n"); + + b2 = bbuf_copy(b1); + B(bbuf_get(b2,&get_val,&get_size)); + print_s(get_val,get_size);printf("\n"); + + bbuf_free(b1); + bbuf_free(b2); + + printf("Realloc buffer\n"); + b1 = bbuf_new(BUF_SIZE_1); + B(bbuf_realloc(b1,BUF_SIZE_2)); + bbuf_free(b1); + + + printf("End test\n"); + return 0; +}
\ No newline at end of file |