From 32920a503c984e2a3eab1027640b5830f47ce334 Mon Sep 17 00:00:00 2001 From: "7u83@mail.ru" <7u83@mail.ru@noemail.net> Date: Fri, 4 Mar 2016 21:47:29 +0000 Subject: [PATCH] Stupid "copy" bstr_create_from_cfgstr FossilOrigin-Name: 37e640922757dc1ada24bd949cf8117c45b22123f2a3cc76dea08a4744ad72f6 --- src/cw/bstr16_create_from_cfgstr.c | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/cw/bstr16_create_from_cfgstr.c diff --git a/src/cw/bstr16_create_from_cfgstr.c b/src/cw/bstr16_create_from_cfgstr.c new file mode 100644 index 00000000..262f19eb --- /dev/null +++ b/src/cw/bstr16_create_from_cfgstr.c @@ -0,0 +1,89 @@ +/* + 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 bstr_create_from_cfgstr function + */ + +#include +#include +#include + +#include "bstr.h" + + +/** + * Create a bstr16_t string from a string read from config file. + * + * @param s String from config file. + * @return The create bstr_t string. + * + * The string from config file is an ASCII-text which is interpreted + * as hexadecimal string if it starts with ".x" + * + * @see bstr16_t + */ +uint8_t * bstr16_create_from_cfgstr(const char * s) +{ + int l = strlen(s); + + + if (s[0]!='.') + return bstr16_create((uint8_t*)s,l); + + if (l<=2) + return bstr16_create((uint8_t*)s,l); + + if (s[1]=='.') + return bstr16_create((uint8_t*)s+1,l-1); + + if (s[1]=='x'){ + uint8_t * ns=0; + int len=0; + + int ch,cl; + const char *ss = s+2; + int rc ; + do { + rc = sscanf(ss,"%01X",&ch); + if (rc!=1) + break; + ss++; + rc = sscanf(ss,"%01X",&cl); + if (rc!=1) + cl=0; + ss++; + int c=(ch<<4) | cl; + + len++; + ns = realloc(ns,len); + ns[len-1]=c; + + + }while (rc==1); + + + return bstr16_create(ns,len); + + } + + return NULL; +} + +