diff --git a/Makefile.am b/Makefile.am index ca874ed8..a375c3cd 100644 --- a/Makefile.am +++ b/Makefile.am @@ -60,7 +60,8 @@ monitor_iwmon_SOURCES = monitor/main.c linux/nl80211.h \ monitor/nlmon.h monitor/nlmon.c \ monitor/pcap.h monitor/pcap.c \ monitor/display.h monitor/display.c \ - src/ie.h src/ie.c + src/ie.h src/ie.c \ + src/util.h src/util.c monitor_iwmon_LDADD = ell/libell-internal.la noinst_PROGRAMS = tools/hwsim diff --git a/src/util.c b/src/util.c new file mode 100644 index 00000000..6735ecf4 --- /dev/null +++ b/src/util.c @@ -0,0 +1,76 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2014 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include + +#include "util.h" + +const char *util_ssid_to_utf8(size_t len, const uint8_t *ssid) +{ + static char buf[3* 32 + 1]; + size_t i = 0, pos = 0; + const uint8_t *start = ssid, *end; + + memset(buf, 0, sizeof(buf)); + + if (len > 32) + goto no_ssid; + + while (i < len && !ssid[i]) + i++; + + if (i == len) + goto no_ssid; + + i = len; + + while (i && (!l_utf8_validate((const char *)start, i, + (const char **)&end))) { + const char replacement[] = { 0xEF, 0xBF, 0xBD }; + int bytes = end - start; + + memcpy(&buf[pos], start, bytes); + pos += bytes; + + memcpy(&buf[pos], replacement, sizeof(replacement)); + pos += sizeof(replacement); + + start = end + 1; + i -= (bytes + 1); + } + + if (i) { + memcpy(&buf[pos], start, i); + pos += i; + } + +no_ssid: + buf[pos] = '\0'; + + return buf; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 00000000..87854580 --- /dev/null +++ b/src/util.h @@ -0,0 +1,31 @@ +/* + * + * Wireless daemon for Linux + * + * Copyright (C) 2013-2014 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __UTIL_H +#define __UTIL_H + +#include +#include + +const char *util_ssid_to_utf8(size_t len, const uint8_t *ssid); + +#endif /* __UTIL_H */