summaryrefslogtreecommitdiff
path: root/netbytes.c
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2017-06-04 00:17:22 +0100
committerFreeArtMan <dos21h@gmail.com>2017-06-04 00:17:22 +0100
commit18b2ebe3df26009ef6b2aeb0670a396e876fca25 (patch)
tree3b851d392330877513c460b03e87b4d0d52ef0fb /netbytes.c
parenta3c2eb86925cf35ef6b0fbb12249cee6a15060cb (diff)
downloadnetbytes-18b2ebe3df26009ef6b2aeb0670a396e876fca25.tar.gz
netbytes-18b2ebe3df26009ef6b2aeb0670a396e876fca25.zip
Added nb_match functionality
Diffstat (limited to 'netbytes.c')
-rw-r--r--netbytes.c184
1 files changed, 141 insertions, 43 deletions
diff --git a/netbytes.c b/netbytes.c
index 0bca647..ecbb87e 100644
--- a/netbytes.c
+++ b/netbytes.c
@@ -667,15 +667,19 @@ int nb_type( netbyte_store *store, int count, __NBT_TYPED **type )
return 0;
}
-int nb_val( netbyte_store *store, int count, uint8_t **val )
+int nb_val( netbyte_store *store, int count, __nb_type **type )
{
- if ( store == NULL )
+ if (!store)
+ {
return -1;
+ }
- if ( store->count < count )
- return -2;
+ if ((count < 0)||(count > store->count))
+ {
+ return -1;
+ }
- *val = store->types[count].nb_val;
+ *type = &store->types[count];
return 0;
}
@@ -771,6 +775,88 @@ int nb_print(netbyte_store *store)
return 0;
}
+int nb_match(netbyte_store *store, nb_tok_arr *pattern)
+{
+ int i;
+ int ret = 0;
+
+ if (store == NULL)
+ {
+ return -1;
+ }
+
+ if (pattern == NULL)
+ {
+ return -1;
+ }
+
+ if (store->count != pattern->len)
+ {
+ return 1;
+ }
+
+ printf("pat->len%d\n",pattern->len);
+ for (i=0;i < pattern->len;i++)
+ {
+ printf("[%d]\n",i);
+ __NBT_TYPED type = store->types[i].type;
+ void *val = store->types[i].nb_val;
+ nb_tok pat_tok = pattern->tok[i];
+ int eq = 0;
+
+ switch (type)
+ {
+ case NBT_U8:
+ {
+ printf("NBT_U8\n");
+ nb_u8 *u8 = (nb_u8 *)val;
+
+ if ((pat_tok.type_size == 8)&&
+ (pat_tok.sign == SIGN_UNSIGNED)&&
+ (pat_tok.arr == 0))
+ {
+ eq = 1;
+ }
+ break;
+ }
+ //if array 0 then its same as [] == [0]!
+ case NBT_U8ARRAY:
+ {
+ printf("NBT_U8ARRAY\n");
+ nb_u8arr *u8arr = (nb_u8arr *)val;
+
+ if ((pat_tok.type_size == 8)&&
+ (pat_tok.sign == SIGN_UNSIGNED)&&
+ (pat_tok.arr == 1)) //&&
+ //(u8arr->len == pat_tok.len)) //could be bug with []/[0]
+ {
+ eq = 1;
+ }
+ break;
+ }
+ case NBT_I32:
+ {
+ printf("NBT_I32\n");
+ if ((pat_tok.type_size == 32)&&
+ (pat_tok.sign == SIGN_SIGNED))
+ {
+ eq = 1;
+ }
+ break;
+ }
+ default:
+ printf(">>nb_match ERROR<<\n");
+ }
+ if (eq==0)
+ {
+ ret = 1;
+ break;
+ }
+ }
+
+ return ret;
+}
+
nb_tok_arr *nb_tok_create(int size)
{
nb_tok_arr *ret=NULL;
@@ -838,11 +924,11 @@ void nb_tok_destroy(nb_tok_arr *arr)
#define S_END 10
#define S_STARTE 11
-#define MATCH(CH) ((*c)==(CH))
-#define MATCHDIG() (isdigit(*c))
-#define MOVE() c++;
+#define NB_MATCH(CH) ((*c)==(CH))
+#define NB_MATCHDIG() (isdigit(*c))
+#define NB_MOVE() (c++);
-int parse_num(char *s, char *e)
+int nb_parse_num(char *s, char *e)
{
const int num_sz = 17;
int str_sz=-1;
@@ -861,9 +947,9 @@ int parse_num(char *s, char *e)
return atoi(num);
}
-int parse(char *str, nb_tok_arr *arr)
+int nb_parse(char *str, nb_tok_arr *arr)
{
- int cnt;
+ int cnt=0;
nb_tok tok;
int state = S_START;
char *c;
@@ -872,7 +958,7 @@ int parse(char *str, nb_tok_arr *arr)
int ret = 0;
memset(&tok, 0, sizeof(tok));
-
+ printf("\"%s\"\n",str);
while (*c!=0)
{
printf("[%c] ",*c);
@@ -882,18 +968,21 @@ int parse(char *str, nb_tok_arr *arr)
{
printf("START\n");
- if (MATCH('u'))
+ if (NB_MATCH('u'))
{
+ printf("!1\n");
tok.sign = SIGN_UNSIGNED;
state = S_SIGN;
- } else if (MATCH('i'))
+ } else if (NB_MATCH('i'))
{
+ printf("!2\n");
tok.sign = SIGN_SIGNED;
state = S_SIGN;
- } else
- {
+ } else {
+ printf("!3\n");
state = S_ERR;
}
+ printf("!\n");
break;
}
case S_STARTE:
@@ -910,14 +999,14 @@ int parse(char *str, nb_tok_arr *arr)
case S_SIGN:
{
printf("SIGN\n");
- if (MATCH('i'))
+ if (NB_MATCH('i'))
{
state = S_SIZE;
- MOVE();
- } else if (MATCH('u'))
+ NB_MOVE();
+ } else if (NB_MATCH('u'))
{
state = S_SIZE;
- MOVE();
+ NB_MOVE();
} else
{
state = S_ERR;
@@ -927,30 +1016,30 @@ int parse(char *str, nb_tok_arr *arr)
case S_SIZE:
{
printf("SIZE\n");
- if (MATCHDIG())
+ if (NB_MATCHDIG())
{
if (st==NULL) st=c;
- MOVE();
- } else if (MATCH(','))
+ NB_MOVE();
+ } else if (NB_MATCH(','))
{
en=c;
- tok.type_size = parse_num(st,en);
+ tok.type_size = nb_parse_num(st,en);
en=NULL; st=NULL;
state = S_STARTE;
- MOVE();
- } else if (MATCH('['))
+ NB_MOVE();
+ } else if (NB_MATCH('['))
{
en=c;
- tok.type_size = parse_num(st,en);
+ tok.type_size = nb_parse_num(st,en);
en=NULL; st=NULL;
state = S_LBREAK;
- MOVE();
- } else if (MATCH(' '))
+ NB_MOVE();
+ } else if (NB_MATCH(' '))
{
en=c;
- tok.type_size = parse_num(st,en);
+ tok.type_size = nb_parse_num(st,en);
en=NULL; st=NULL;
state = S_END;
@@ -963,20 +1052,20 @@ int parse(char *str, nb_tok_arr *arr)
case S_NUMBER:
{
printf("NUMBER\n");
- if (MATCHDIG())
+ if (NB_MATCHDIG())
{
if(st==NULL) st=c;
- MOVE();
- } else if (MATCH(']'))
+ NB_MOVE();
+ } else if (NB_MATCH(']'))
{
en=c;
- tok.type_size = parse_num(st,en);
+ tok.type_size = nb_parse_num(st,en);
tok.arr = 1;
en=NULL; st=NULL;
state = S_RBREAK;
- MOVE();
+ NB_MOVE();
}
break;
}
@@ -984,7 +1073,7 @@ int parse(char *str, nb_tok_arr *arr)
{
printf("COMM\n");
state = S_STARTE;
- MOVE();
+ NB_MOVE();
break;
}
case S_ERR:
@@ -996,13 +1085,14 @@ int parse(char *str, nb_tok_arr *arr)
case S_LBREAK:
{
printf("LBREAK\n");
- if (MATCHDIG())
+ if (NB_MATCHDIG())
{
state = S_NUMBER;
- } else if (MATCH(']'))
+ } else if (NB_MATCH(']'))
{
state = S_RBREAK;
- MOVE();
+ tok.arr = 1;
+ NB_MOVE();
} else
{
state = S_ERR;
@@ -1012,7 +1102,7 @@ int parse(char *str, nb_tok_arr *arr)
case S_RBREAK:
{
printf("RBREAK\n");
- if (MATCH(','))
+ if (NB_MATCH(','))
{
state = S_COMM;
} else
@@ -1039,13 +1129,21 @@ int parse(char *str, nb_tok_arr *arr)
}
default:
{
-
+ printf("Unknown state\n");
}
}
cnt++;
- if (cnt>32) break;
- if (state == -1) break;
+ if (cnt>32)
+ {
+ printf("Loop\n");
+ break;
+ }
+ if (state == -1)
+ {
+ printf("state\n");
+ break;
+ }
}
return ret;