chiark / gitweb /
macro: clean up usage of gcc attributes
[elogind.git] / src / shared / utf8.c
index 732f0f00ca20d2003cd3c96122cf0099abec15bf..98b68ef657c076e53f108df96e43aa8762fea79e 100644 (file)
@@ -3,6 +3,7 @@
 /***
   This file is part of systemd.
 
+  Copyright 2008-2011 Kay Sievers
   Copyright 2012 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
@@ -19,7 +20,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-/* This file is based on the GLIB utf8 validation functions. The
+/* Parts of this file are based on the GLIB utf8 validation functions. The
  * original license text follows. */
 
 /* gutf8.c - Operations on UTF-8 strings.
@@ -97,7 +98,7 @@ static int utf8_encoded_expected_len(const char *str) {
 }
 
 /* decode one unicode char */
-static int utf8_encoded_to_unichar(const char *str) {
+int utf8_encoded_to_unichar(const char *str) {
         int unichar;
         int len;
         int i;
@@ -140,14 +141,15 @@ bool utf8_is_printable(const char* str, size_t length) {
 
         assert(str);
 
-        for (p = (const uint8_t*) str; length; p++) {
+        for (p = (const uint8_t*) str; length;) {
                 int encoded_len = utf8_encoded_valid_unichar((const char *)p);
-                int32_t val = utf8_encoded_to_unichar((const char*)p);
+                int val = utf8_encoded_to_unichar((const char*)p);
 
                 if (encoded_len < 0 || val < 0 || is_unicode_control(val))
                         return false;
 
                 length -= encoded_len;
+                p += encoded_len;
         }
 
         return true;
@@ -285,49 +287,3 @@ int utf8_encoded_valid_unichar(const char *str) {
 
         return len;
 }
-
-int is_utf8_encoding_whitelisted(char c, const char *white) {
-        if ((c >= '0' && c <= '9') ||
-            (c >= 'A' && c <= 'Z') ||
-            (c >= 'a' && c <= 'z') ||
-            strchr("#+-.:=@_", c) != NULL ||
-            (white != NULL && strchr(white, c) != NULL))
-                return 1;
-        return 0;
-}
-
-int udev_encode_string(const char *str, char *str_enc, size_t len) {
-        size_t i, j;
-
-        if (str == NULL || str_enc == NULL)
-                return -1;
-
-        for (i = 0, j = 0; str[i] != '\0'; i++) {
-                int seqlen;
-
-                seqlen = utf8_encoded_valid_unichar(&str[i]);
-                if (seqlen > 1) {
-                        if (len-j < (size_t)seqlen)
-                                goto err;
-                        memcpy(&str_enc[j], &str[i], seqlen);
-                        j += seqlen;
-                        i += (seqlen-1);
-                } else if (str[i] == '\\' || !is_utf8_encoding_whitelisted(str[i], NULL)) {
-                        if (len-j < 4)
-                                goto err;
-                        sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
-                        j += 4;
-                } else {
-                        if (len-j < 1)
-                                goto err;
-                        str_enc[j] = str[i];
-                        j++;
-                }
-        }
-        if (len-j < 1)
-                goto err;
-        str_enc[j] = '\0';
-        return 0;
-err:
-        return -1;
-}