summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-06-03 16:31:12 +0100
committerFreeArtMan <dos21h@gmail.com>2017-06-03 16:31:12 +0100
commita3c2eb86925cf35ef6b0fbb12249cee6a15060cb (patch)
tree641262b323159f85adfcb79143f4fbec79436083
parenta877a8ed7b508eb8cc5f7911dfcec8b8612470b3 (diff)
downloadnetbytes-a3c2eb86925cf35ef6b0fbb12249cee6a15060cb.tar.gz
netbytes-a3c2eb86925cf35ef6b0fbb12249cee6a15060cb.zip
Add type parser
-rw-r--r--netbytes.c288
-rw-r--r--netbytes.h25
2 files changed, 311 insertions, 2 deletions
diff --git a/netbytes.c b/netbytes.c
index 2bc34b7..0bca647 100644
--- a/netbytes.c
+++ b/netbytes.c
@@ -468,6 +468,7 @@ int nb_free(netbyte_store *store)
}
}
free(store);
+ store = NULL;
return 0;
}
@@ -688,11 +689,14 @@ int nb_fread( netbyte_store *store, int fd)
uint8_t *nb;
if ( store == NULL )
+ {
return -1;
+ }
if ( fd < 1 )
+ {
return -2;
-
+ }
nb_init( store );
@@ -722,6 +726,8 @@ int nb_fread( netbyte_store *store, int fd)
return -6;
}
+ free(nb); nb=NULL;
+
return 0;
}
@@ -763,4 +769,284 @@ int nb_print(netbyte_store *store)
}
return 0;
+}
+
+nb_tok_arr *nb_tok_create(int size)
+{
+ nb_tok_arr *ret=NULL;
+
+ ret = malloc(sizeof(nb_tok_arr));
+ memset(ret,0,sizeof(nb_tok_arr));
+
+ ret->tok = malloc(sizeof(nb_tok)*size);
+ memset(ret->tok, 0, sizeof(nb_tok)*size);
+
+ ret->size = size;
+ ret->len = 0;
+
+ return ret;
+}
+
+int nb_tok_add(nb_tok_arr *arr, int sign, int type_size, int len, int farr)
+{
+ if (arr==NULL)
+ {
+ return -1;
+ }
+
+ if (arr->len >= arr->size-1)
+ {
+ return -1;
+ }
+
+
+ arr->tok[arr->len].sign = sign;
+ arr->tok[arr->len].type_size = type_size;
+ arr->tok[arr->len].len = len;
+ arr->tok[arr->len].arr = farr;
+ arr->len += 1;
+
+ return 0;
+}
+
+void nb_tok_destroy(nb_tok_arr *arr)
+{
+ if (arr == NULL)
+ {
+ return;
+ }
+
+ if (arr->tok)
+ {
+ free(arr->tok);
+ arr->tok = NULL;
+ }
+
+ free(arr);
+ arr = NULL;
+}
+
+#define S_START 1
+#define S_SIGN 2
+#define S_SIZE 3
+#define S_NUMBER 4
+#define S_COMM 5
+#define S_ERR 6
+#define S_LBREAK 7
+#define S_RBREAK 8
+#define S_ARRSIZE 9
+#define S_END 10
+#define S_STARTE 11
+
+#define MATCH(CH) ((*c)==(CH))
+#define MATCHDIG() (isdigit(*c))
+#define MOVE() c++;
+
+int parse_num(char *s, char *e)
+{
+ const int num_sz = 17;
+ int str_sz=-1;
+ char num[num_sz];
+
+ memset(num, 0, num_sz);
+
+ str_sz = e-s;
+ if (str_sz>num_sz-1)
+ {
+ printf("ERROR\n");
+ return -1;
+ }
+ memcpy(num,s,str_sz);
+
+ return atoi(num);
+}
+
+int parse(char *str, nb_tok_arr *arr)
+{
+ int cnt;
+ nb_tok tok;
+ int state = S_START;
+ char *c;
+ c = str;
+ char *st=NULL,*en=NULL;
+ int ret = 0;
+
+ memset(&tok, 0, sizeof(tok));
+
+ while (*c!=0)
+ {
+ printf("[%c] ",*c);
+ switch (state)
+ {
+ case S_START:
+ {
+ printf("START\n");
+
+ if (MATCH('u'))
+ {
+ tok.sign = SIGN_UNSIGNED;
+ state = S_SIGN;
+ } else if (MATCH('i'))
+ {
+ tok.sign = SIGN_SIGNED;
+ state = S_SIGN;
+ } else
+ {
+ state = S_ERR;
+ }
+ break;
+ }
+ case S_STARTE:
+ {
+ printf("STARTE\n");
+ printf("S:%d TS:%d L:%d A:%d\n",
+ tok.sign, tok.type_size, tok.len, tok.arr
+ );
+ nb_tok_add(arr, tok.sign, tok.type_size, tok.len, tok.arr);
+ memset(&tok,0,sizeof(nb_tok));
+ state = S_START;
+ break;
+ }
+ case S_SIGN:
+ {
+ printf("SIGN\n");
+ if (MATCH('i'))
+ {
+ state = S_SIZE;
+ MOVE();
+ } else if (MATCH('u'))
+ {
+ state = S_SIZE;
+ MOVE();
+ } else
+ {
+ state = S_ERR;
+ }
+ break;
+ }
+ case S_SIZE:
+ {
+ printf("SIZE\n");
+ if (MATCHDIG())
+ {
+ if (st==NULL) st=c;
+ MOVE();
+ } else if (MATCH(','))
+ {
+ en=c;
+ tok.type_size = parse_num(st,en);
+ en=NULL; st=NULL;
+
+ state = S_STARTE;
+ MOVE();
+ } else if (MATCH('['))
+ {
+ en=c;
+ tok.type_size = parse_num(st,en);
+ en=NULL; st=NULL;
+
+ state = S_LBREAK;
+ MOVE();
+ } else if (MATCH(' '))
+ {
+ en=c;
+ tok.type_size = parse_num(st,en);
+ en=NULL; st=NULL;
+
+ state = S_END;
+ } else
+ {
+ state = S_ERR;
+ }
+ break;
+ }
+ case S_NUMBER:
+ {
+ printf("NUMBER\n");
+ if (MATCHDIG())
+ {
+ if(st==NULL) st=c;
+
+ MOVE();
+ } else if (MATCH(']'))
+ {
+ en=c;
+ tok.type_size = parse_num(st,en);
+ tok.arr = 1;
+ en=NULL; st=NULL;
+
+ state = S_RBREAK;
+ MOVE();
+ }
+ break;
+ }
+ case S_COMM:
+ {
+ printf("COMM\n");
+ state = S_STARTE;
+ MOVE();
+ break;
+ }
+ case S_ERR:
+ {
+ printf("ERR\n");
+ state = -1;
+ break;
+ }
+ case S_LBREAK:
+ {
+ printf("LBREAK\n");
+ if (MATCHDIG())
+ {
+ state = S_NUMBER;
+ } else if (MATCH(']'))
+ {
+ state = S_RBREAK;
+ MOVE();
+ } else
+ {
+ state = S_ERR;
+ }
+ break;
+ }
+ case S_RBREAK:
+ {
+ printf("RBREAK\n");
+ if (MATCH(','))
+ {
+ state = S_COMM;
+ } else
+ {
+ state = S_END;
+ }
+ break;
+ }
+ case S_ARRSIZE:
+ {
+ printf("ARRSIZE\n");
+ break;
+ }
+ case S_END:
+ {
+ printf("END\n");
+ printf("S:%d TS:%d L:%d A:%d\n",
+ tok.sign, tok.type_size, tok.len, tok.arr
+ );
+ nb_tok_add(arr, tok.sign, tok.type_size, tok.len, tok.arr);
+ memset(&tok,0,sizeof(nb_tok));
+ state = -1;
+ break;
+ }
+ default:
+ {
+
+ }
+
+ }
+ cnt++;
+ if (cnt>32) break;
+ if (state == -1) break;
+ }
+
+ return ret;
} \ No newline at end of file
diff --git a/netbytes.h b/netbytes.h
index 8b332b5..a73dcc6 100644
--- a/netbytes.h
+++ b/netbytes.h
@@ -5,6 +5,7 @@
#include <stdio.h>
#include <stdint.h>
#include <string.h>
+#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
@@ -156,6 +157,28 @@ int nb_fread( netbyte_store *store, int fd);
int nb_print(netbyte_store *store);
//check if netbyte matches particular format
-int nb_match(netbyte_store *store, char *frmt);
+int nb_match(netbyte_store *store, int size);
+
+#define SIGN_UNSIGNED 1
+#define SIGN_SIGNED 2
+
+typedef struct nb_tok
+{
+ int sign;
+ int type_size;
+ int len;
+ int arr;
+} nb_tok;
+
+typedef struct nb_tok_arr
+{
+ int size;
+ int len;
+ nb_tok *tok;
+} nb_tok_arr;
+
+nb_tok_arr *nb_tok_create(int size);
+int nb_tok_add(nb_tok_arr *arr, int sign, int type_size, int len, int farr);
+void nb_tok_destroy(nb_tok_arr *arr);
#endif \ No newline at end of file