summaryrefslogtreecommitdiff
path: root/libbuf
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2019-09-25 19:17:09 +0100
committerFreeArtMan <dos21h@gmail.com>2019-09-25 19:17:09 +0100
commit3e187c094f12ef41ea6de2f55d128c3e037b5c12 (patch)
treef0dbbc5df260532e284ca5264297d0a8044eaeb4 /libbuf
parentbbd73385a0db271b457c63e4d6bab54f581410f9 (diff)
downloadihe-3e187c094f12ef41ea6de2f55d128c3e037b5c12.tar.gz
ihe-3e187c094f12ef41ea6de2f55d128c3e037b5c12.zip
Untested version, recompiled with new buf library and with c+
Diffstat (limited to 'libbuf')
-rw-r--r--libbuf/Makefile29
-rw-r--r--libbuf/buf.c534
-rw-r--r--libbuf/buf.h89
-rw-r--r--libbuf/buf_misc.c272
-rw-r--r--libbuf/buf_misc.h64
5 files changed, 988 insertions, 0 deletions
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;i<a->buf_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;i<get_size;i++)
+ {
+ char tc;
+ int r = b->getc(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;i<this->buf_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_size<this->buf_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;i<this->cur_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;i<mvsz;i++)
+ {
+ char c = buf[n+i];
+ //printf("%c",c);
+ buf[i] = c;
+ }
+ this->cur_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 <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+
+/*
+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;i<bbuf_size(new_data);i++)
+ {
+ if (0 == bbuf_circ_put(circ, new_data->buf[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