1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2008-2011 Kay Sievers
7 Copyright 2012 Lennart Poettering
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 /* Parts of this file are based on the GLIB utf8 validation functions. The
24 * original license text follows. */
26 /* gutf8.c - Operations on UTF-8 strings.
28 * Copyright (C) 1999 Tom Tromey
29 * Copyright (C) 2000 Red Hat, Inc.
31 * This library is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU Library General Public
33 * License as published by the Free Software Foundation; either
34 * version 2 of the License, or (at your option) any later version.
36 * This library is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * Library General Public License for more details.
41 * You should have received a copy of the GNU Library General Public
42 * License along with this library; if not, write to the Free Software
43 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
55 static inline bool is_unicode_valid(uint32_t ch) {
57 if (ch >= 0x110000) /* End of unicode space */
59 if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
61 if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
63 if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
69 static bool is_unicode_control(uint32_t ch) {
72 0 to ' '-1 is the C0 range.
73 DEL=0x7F, and DEL+1 to 0x9F is C1 range.
74 '\t' is in C0 range, but more or less harmless and commonly used.
77 return (ch < ' ' && ch != '\t' && ch != '\n') ||
78 (0x7F <= ch && ch <= 0x9F);
81 /* count of characters used to encode one unicode char */
82 static int utf8_encoded_expected_len(const char *str) {
83 unsigned char c = (unsigned char)str[0];
87 if ((c & 0xe0) == 0xc0)
89 if ((c & 0xf0) == 0xe0)
91 if ((c & 0xf8) == 0xf0)
93 if ((c & 0xfc) == 0xf8)
95 if ((c & 0xfe) == 0xfc)
100 /* decode one unicode char */
101 int utf8_encoded_to_unichar(const char *str) {
106 len = utf8_encoded_expected_len(str);
111 unichar = str[0] & 0x1f;
114 unichar = (int)str[0] & 0x0f;
117 unichar = (int)str[0] & 0x07;
120 unichar = (int)str[0] & 0x03;
123 unichar = (int)str[0] & 0x01;
129 for (i = 1; i < len; i++) {
130 if (((int)str[i] & 0xc0) != 0x80)
133 unichar |= (int)str[i] & 0x3f;
139 bool utf8_is_printable(const char* str, size_t length) {
144 for (p = (const uint8_t*) str; length;) {
145 int encoded_len = utf8_encoded_valid_unichar((const char *)p);
146 int val = utf8_encoded_to_unichar((const char*)p);
148 if (encoded_len < 0 || val < 0 || is_unicode_control(val))
151 length -= encoded_len;
158 const char *utf8_is_valid(const char *str) {
163 for (p = (const uint8_t*) str; *p; ) {
166 len = utf8_encoded_valid_unichar((const char *)p);
177 char *ascii_is_valid(const char *str) {
182 for (p = str; *p; p++)
183 if ((unsigned char) *p >= 128)
189 char *utf16_to_utf8(const void *s, size_t length) {
194 r = new(char, (length*3+1)/2 + 1);
200 for (f = s; f < (const uint8_t*) s + length; f += 2) {
203 c = (f[1] << 8) | f[0];
208 } else if (c < 0x80) {
209 *(t++) = (uint8_t) c;
210 } else if (c < 0x800) {
211 *(t++) = (uint8_t) (0xc0 | (c >> 6));
212 *(t++) = (uint8_t) (0x80 | (c & 0x3f));
214 *(t++) = (uint8_t) (0xe0 | (c >> 12));
215 *(t++) = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
216 *(t++) = (uint8_t) (0x80 | (c & 0x3f));
225 /* expected size used to encode one unicode char */
226 static int utf8_unichar_to_encoded_len(int unichar) {
231 if (unichar < 0x10000)
233 if (unichar < 0x200000)
235 if (unichar < 0x4000000)
240 /* validate one encoded unicode char and return its length */
241 int utf8_encoded_valid_unichar(const char *str) {
246 len = utf8_encoded_expected_len(str);
254 /* check if expected encoded chars are available */
255 for (i = 0; i < len; i++)
256 if ((str[i] & 0x80) != 0x80)
259 unichar = utf8_encoded_to_unichar(str);
261 /* check if encoded length matches encoded value */
262 if (utf8_unichar_to_encoded_len(unichar) != len)
265 /* check if value has valid range */
266 if (!is_unicode_valid(unichar))