From 350b62a49b575eaec85e648b3248b9c1a395b6c5 Mon Sep 17 00:00:00 2001
From: "7u83@mail.ru" <7u83@mail.ru@noemail.net>
Date: Mon, 4 May 2015 05:28:55 +0000
Subject: [PATCH] Moved out from mbag.c and added methods.
FossilOrigin-Name: 95705056262cb62fac9d8d31a8bca2051332555f6d5521f9d5f0f30229369dd7
---
src/capwap/mbag_type_bstr16.c | 43 +++++++++++++++++
src/capwap/mbag_type_byte.c | 44 ++++++++++++++++++
src/capwap/mbag_type_dword.c | 43 +++++++++++++++++
src/capwap/mbag_type_mbag.c | 36 ++++++++++++++
src/capwap/mbag_type_sockaddr.c | 3 +-
src/capwap/mbag_type_vendorstr.c | 80 ++++++++++++++++++++++++++++++++
src/capwap/mbag_type_word.c | 5 +-
7 files changed, 249 insertions(+), 5 deletions(-)
create mode 100644 src/capwap/mbag_type_bstr16.c
create mode 100644 src/capwap/mbag_type_byte.c
create mode 100644 src/capwap/mbag_type_dword.c
create mode 100644 src/capwap/mbag_type_mbag.c
create mode 100644 src/capwap/mbag_type_vendorstr.c
diff --git a/src/capwap/mbag_type_bstr16.c b/src/capwap/mbag_type_bstr16.c
new file mode 100644
index 00000000..d91fac1c
--- /dev/null
+++ b/src/capwap/mbag_type_bstr16.c
@@ -0,0 +1,43 @@
+
+#include "mbag.h"
+#include "format.h"
+
+
+
+static int to_str(void *item,char *dst)
+{
+ mbag_item_t *i= item;
+
+// bstr16_t src = i->data;
+// printf("From str resul: '%.*s'\n",bstr16_len(src),bstr16_data(src));
+
+// exit(0);
+
+
+ char *d = dst;
+ int utf8 = cw_is_utf8(bstr16_data(i->data), bstr16_len(i->data));
+
+
+ if (utf8) {
+ d += sprintf(d, "%.*s", bstr16_len(i->data), bstr16_data(i->data));
+ } else {
+ d += sprintf(d, ".x");
+ d += cw_format_hex(d, bstr16_data(i->data), bstr16_len(i->data));
+ }
+ return d-dst;
+}
+
+
+static struct mbag_item * from_str(const char *src)
+{
+ mbag_item_t * i = mbag_item_new(MBAG_BSTR16);
+ if (!i)
+ return NULL;
+ i->data=bstr16_create_from_str(src);
+
+ return i;
+}
+
+const struct mbag_typedef mbag_type_bstr16 = {
+ "Bstr16",free,to_str,from_str
+};
diff --git a/src/capwap/mbag_type_byte.c b/src/capwap/mbag_type_byte.c
new file mode 100644
index 00000000..d065db61
--- /dev/null
+++ b/src/capwap/mbag_type_byte.c
@@ -0,0 +1,44 @@
+/*
+ This file is part of libcapwap.
+
+ libcapwap is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ libcapwap is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Foobar. If not, see .
+
+*/
+
+
+#include "mbag.h"
+
+
+static struct mbag_item * from_str(const char *src)
+{
+ mbag_item_t * i = mbag_item_new(MBAG_BYTE);
+ if (!i)
+ return NULL;
+ i->byte=atoi(src);
+ return i;
+}
+
+static int to_str(void *item,char *dst)
+{
+ mbag_item_t *i= item;
+ return sprintf(dst, "%d", i->byte);
+}
+
+
+/** Defines a word, two bytes. */
+const struct mbag_typedef mbag_type_byte = {
+ "Byte",NULL,to_str,from_str
+};
+
+
diff --git a/src/capwap/mbag_type_dword.c b/src/capwap/mbag_type_dword.c
new file mode 100644
index 00000000..9cd188ee
--- /dev/null
+++ b/src/capwap/mbag_type_dword.c
@@ -0,0 +1,43 @@
+/*
+ This file is part of libcapwap.
+
+ libcapwap is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ libcapwap is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Foobar. If not, see .
+
+*/
+
+
+#include "mbag.h"
+
+
+static struct mbag_item * from_str(const char *src)
+{
+ mbag_item_t * i = mbag_item_new(MBAG_DWORD);
+ if (!i)
+ return NULL;
+ i->word=atoi(src);
+ return i;
+}
+
+static int to_str(void *item,char *dst)
+{
+ mbag_item_t *i= item;
+ return sprintf(dst, "%d", i->dword);
+}
+
+
+const struct mbag_typedef mbag_type_dword = {
+ "DWORD",NULL,to_str,from_str
+};
+
+
diff --git a/src/capwap/mbag_type_mbag.c b/src/capwap/mbag_type_mbag.c
new file mode 100644
index 00000000..b6648a88
--- /dev/null
+++ b/src/capwap/mbag_type_mbag.c
@@ -0,0 +1,36 @@
+/*
+ This file is part of libcapwap.
+
+ libcapwap is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ libcapwap is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Foobar. If not, see .
+
+*/
+
+
+#include "mbag.h"
+
+static void del_fun(void *i)
+{
+ mbag_t x = (mbag_t)i;
+ mavl_destroy(x);
+}
+
+const struct mbag_typedef mbag_type_mbag = {
+ "Mbag",del_fun
+};
+
+const struct mbag_typedef mbag_type_mbag_dyn = {
+ "Mbag_Dyn",del_fun
+};
+
+
diff --git a/src/capwap/mbag_type_sockaddr.c b/src/capwap/mbag_type_sockaddr.c
index 6e84442c..aa4eadda 100644
--- a/src/capwap/mbag_type_sockaddr.c
+++ b/src/capwap/mbag_type_sockaddr.c
@@ -32,6 +32,7 @@
#include "mbag.h"
#include "format.h"
+/*
static struct mbag_item * from_str(const char *src)
{
struct mbag_item *i= malloc(sizeof(struct sockaddr_storage));
@@ -48,7 +49,7 @@ static int to_str(void *item,char *dst)
mbag_item_t *i= item;
return sprintf(dst, "%d", i->word);
}
-
+*/
/**
* MBAG items of this type hold an struct sockaddr element.
diff --git a/src/capwap/mbag_type_vendorstr.c b/src/capwap/mbag_type_vendorstr.c
new file mode 100644
index 00000000..4c81fc56
--- /dev/null
+++ b/src/capwap/mbag_type_vendorstr.c
@@ -0,0 +1,80 @@
+/*
+ This file is part of libcapwap.
+
+ libcapwap is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ libcapwap is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Foobar. If not, see .
+
+*/
+
+/**
+ *@file
+ *@brief Implementation of mbag_type vendorstr
+ *@addtogroup MbagFunctions
+ *@{
+ */
+
+#include "mbag.h"
+#include "format.h"
+
+
+static int to_str(void *item,char *dst)
+{
+
+ mbag_item_t *i= item;
+
+ char *d=dst;
+ d+=sprintf(d,"%d,",vendorstr_get_vendor_id(i->data));
+
+ if (cw_is_utf8(vendorstr_data(i->data), vendorstr_len(i->data))) {
+ d += sprintf(d, "%.*s", vendorstr_len(i->data),
+ vendorstr_data(i->data));
+ } else {
+ d += sprintf(d, ".x");
+ d += cw_format_hex(d, vendorstr_data(i->data), vendorstr_len(i->data));
+ }
+
+ return d-dst;
+}
+
+
+static struct mbag_item * from_str(const char *src)
+{
+ mbag_item_t * i = mbag_item_new(MBAG_VENDORSTR);
+ if (!i)
+ return NULL;
+
+ uint32_t vendor_id=atoi(src);
+ const char *s = strchr(src,',');
+
+
+ if (s){
+ i->data=vendorstr_create_from_str(vendor_id,s+1);
+ }
+ else{
+ i->data=vendorstr_create_from_str(vendor_id,"");
+ }
+
+ return i;
+}
+
+/**
+ * Defines the VendorStr type.
+ *
+ * MBAG items of this type containing a variable of type #vendorstr_t.
+ */
+const struct mbag_typedef mbag_type_vendorstr = {
+ "VendorStr",free,to_str,from_str
+};
+
+
+/**@}*/
diff --git a/src/capwap/mbag_type_word.c b/src/capwap/mbag_type_word.c
index 864db7d1..991065f7 100644
--- a/src/capwap/mbag_type_word.c
+++ b/src/capwap/mbag_type_word.c
@@ -18,16 +18,13 @@
#include "mbag.h"
-#include "format.h"
static struct mbag_item * from_str(const char *src)
{
- struct mbag_item *i= malloc(sizeof(mbag_item_t));
+ mbag_item_t * i = mbag_item_new(MBAG_WORD);
if (!i)
return NULL;
-
- i->type = MBAG_WORD;
i->word=atoi(src);
return i;
}