1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 /* This file is based on the GLIB utf8 validation functions. The
23 * original license text follows. */
25 /* gutf8.c - Operations on UTF-8 strings.
27 * Copyright (C) 1999 Tom Tromey
28 * Copyright (C) 2000 Red Hat, Inc.
30 * This library is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU Library General Public
32 * License as published by the Free Software Foundation; either
33 * version 2 of the License, or (at your option) any later version.
35 * This library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * Library General Public License for more details.
40 * You should have received a copy of the GNU Library General Public
41 * License along with this library; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
54 static inline bool is_unicode_valid(uint32_t ch) {
56 if (ch >= 0x110000) /* End of unicode space */
58 if ((ch & 0xFFFFF800) == 0xD800) /* Reserved area for UTF-16 */
60 if ((ch >= 0xFDD0) && (ch <= 0xFDEF)) /* Reserved */
62 if ((ch & 0xFFFE) == 0xFFFE) /* BOM (Byte Order Mark) */
68 static bool is_unicode_control(uint32_t ch) {
71 0 to ' '-1 is the C0 range.
72 DEL=0x7F, and DEL+1 to 0x9F is C1 range.
73 '\t' is in C0 range, but more or less harmless and commonly used.
76 return (ch < ' ' && ch != '\t' && ch != '\n') ||
77 (0x7F <= ch && ch <= 0x9F);
80 /* count of characters used to encode one unicode char */
81 static int utf8_encoded_expected_len(const char *str) {
82 unsigned char c = (unsigned char)str[0];
86 if ((c & 0xe0) == 0xc0)
88 if ((c & 0xf0) == 0xe0)
90 if ((c & 0xf8) == 0xf0)
92 if ((c & 0xfc) == 0xf8)
94 if ((c & 0xfe) == 0xfc)
99 /* decode one unicode char */
100 static int utf8_encoded_to_unichar(const char *str) {
105 len = utf8_encoded_expected_len(str);
110 unichar = str[0] & 0x1f;
113 unichar = (int)str[0] & 0x0f;
116 unichar = (int)str[0] & 0x07;
119 unichar = (int)str[0] & 0x03;
122 unichar = (int)str[0] & 0x01;
128 for (i = 1; i < len; i++) {
129 if (((int)str[i] & 0xc0) != 0x80)
132 unichar |= (int)str[i] & 0x3f;
138 bool utf8_is_printable(const char* str, size_t length) {
143 for (p = (const uint8_t*) str; length; p++) {
144 int encoded_len = utf8_encoded_valid_unichar((const char *)p);
145 int32_t val = utf8_encoded_to_unichar((const char*)p);
147 if (encoded_len < 0 || val < 0 || is_unicode_control(val))
150 length -= encoded_len;
156 const char *utf8_is_valid(const char *str) {
161 for (p = (const uint8_t*) str; *p; ) {
162 int len = utf8_encoded_valid_unichar((const char *)p);
173 char *ascii_is_valid(const char *str) {
178 for (p = str; *p; p++)
179 if ((unsigned char) *p >= 128)
185 char *ascii_filter(const char *str) {
197 for (s = str, d = r; *s; s++)
198 if ((unsigned char) *s < 128)
206 char *utf16_to_utf8(const void *s, size_t length) {
211 r = new(char, (length*3+1)/2 + 1);
217 for (f = s; f < (const uint8_t*) s + length; f += 2) {
220 c = (f[1] << 8) | f[0];
225 } else if (c < 0x80) {
226 *(t++) = (uint8_t) c;
227 } else if (c < 0x800) {
228 *(t++) = (uint8_t) (0xc0 | (c >> 6));
229 *(t++) = (uint8_t) (0x80 | (c & 0x3f));
231 *(t++) = (uint8_t) (0xe0 | (c >> 12));
232 *(t++) = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
233 *(t++) = (uint8_t) (0x80 | (c & 0x3f));
242 /* expected size used to encode one unicode char */
243 static int utf8_unichar_to_encoded_len(int unichar) {
248 if (unichar < 0x10000)
250 if (unichar < 0x200000)
252 if (unichar < 0x4000000)
257 /* validate one encoded unicode char and return its length */
258 int utf8_encoded_valid_unichar(const char *str) {
263 len = utf8_encoded_expected_len(str);
271 /* check if expected encoded chars are available */
272 for (i = 0; i < len; i++)
273 if ((str[i] & 0x80) != 0x80)
276 unichar = utf8_encoded_to_unichar(str);
278 /* check if encoded length matches encoded value */
279 if (utf8_unichar_to_encoded_len(unichar) != len)
282 /* check if value has valid range */
283 if (!is_unicode_valid(unichar))
289 int is_utf8_encoding_whitelisted(char c, const char *white) {
290 if ((c >= '0' && c <= '9') ||
291 (c >= 'A' && c <= 'Z') ||
292 (c >= 'a' && c <= 'z') ||
293 strchr("#+-.:=@_", c) != NULL ||
294 (white != NULL && strchr(white, c) != NULL))
299 int udev_encode_string(const char *str, char *str_enc, size_t len) {
302 if (str == NULL || str_enc == NULL)
305 for (i = 0, j = 0; str[i] != '\0'; i++) {
308 seqlen = utf8_encoded_valid_unichar(&str[i]);
310 if (len-j < (size_t)seqlen)
312 memcpy(&str_enc[j], &str[i], seqlen);
315 } else if (str[i] == '\\' || !is_utf8_encoding_whitelisted(str[i], NULL)) {
318 sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);