Add helper functions for escaping and unescaping UTF-8
authorBrian Gix <bgix@qce.qualcomm.com>
Fri, 5 Sep 2014 13:43:57 +0000 (16:43 +0300)
committerJouni Malinen <j@w1.fi>
Mon, 2 Feb 2015 12:09:18 +0000 (14:09 +0200)
Signed-off-by: Jouni Malinen <jouni@qca.qualcomm.com>
src/utils/common.c
src/utils/common.h

index 93f1722..5fd795f 100644 (file)
@@ -976,3 +976,89 @@ char * str_token(char *str, const char *delim, char **context)
        *context = end;
        return pos;
 }
+
+
+size_t utf8_unescape(const char *inp, size_t in_size,
+                    char *outp, size_t out_size)
+{
+       size_t res_size = 0;
+
+       if (!inp || !outp)
+               return 0;
+
+       if (!in_size)
+               in_size = os_strlen(inp);
+
+       /* Advance past leading single quote */
+       if (*inp == '\'' && in_size) {
+               inp++;
+               in_size--;
+       }
+
+       while (in_size--) {
+               if (res_size >= out_size)
+                       return 0;
+
+               switch (*inp) {
+               case '\'':
+                       /* Terminate on bare single quote */
+                       *outp = '\0';
+                       return res_size;
+
+               case '\\':
+                       if (!in_size--)
+                               return 0;
+                       inp++;
+                       /* fall through */
+
+               default:
+                       *outp++ = *inp++;
+                       res_size++;
+               }
+       }
+
+       /* NUL terminate if space allows */
+       if (res_size < out_size)
+               *outp = '\0';
+
+       return res_size;
+}
+
+
+size_t utf8_escape(const char *inp, size_t in_size,
+                  char *outp, size_t out_size)
+{
+       size_t res_size = 0;
+
+       if (!inp || !outp)
+               return 0;
+
+       /* inp may or may not be NUL terminated, but must be if 0 size
+        * is specified */
+       if (!in_size)
+               in_size = os_strlen(inp);
+
+       while (in_size--) {
+               if (res_size++ >= out_size)
+                       return 0;
+
+               switch (*inp) {
+               case '\\':
+               case '\'':
+                       if (res_size++ >= out_size)
+                               return 0;
+                       *outp++ = '\\';
+                       /* fall through */
+
+               default:
+                       *outp++ = *inp++;
+                       break;
+               }
+       }
+
+       /* NUL terminate if space allows */
+       if (res_size < out_size)
+               *outp = '\0';
+
+       return res_size;
+}
index 82a51e5..576e8e7 100644 (file)
@@ -548,6 +548,10 @@ int random_mac_addr(u8 *addr);
 int random_mac_addr_keep_oui(u8 *addr);
 
 char * str_token(char *str, const char *delim, char **context);
+size_t utf8_escape(const char *inp, size_t in_size,
+                  char *outp, size_t out_size);
+size_t utf8_unescape(const char *inp, size_t in_size,
+                    char *outp, size_t out_size);
 
 
 /*