chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Prep v233: Unmask now needed functions in src/basic
[elogind.git]
/
src
/
basic
/
utf8.c
diff --git
a/src/basic/utf8.c
b/src/basic/utf8.c
index 800884ffee99c77b36c6c16f35015a70442409fa..6eae2b983d8dfd7943c479be9918d8539fd538fd 100644
(file)
--- a/
src/basic/utf8.c
+++ b/
src/basic/utf8.c
@@
-1,5
+1,3
@@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
/***
This file is part of systemd.
/***
This file is part of systemd.
@@
-44,15
+42,16
@@
*/
#include <errno.h>
*/
#include <errno.h>
+#include <stdbool.h>
#include <stdlib.h>
#include <stdlib.h>
-#include <inttypes.h>
#include <string.h>
#include <string.h>
-#include <stdbool.h>
+#include "alloc-util.h"
+#include "hexdecoct.h"
+#include "macro.h"
#include "utf8.h"
#include "utf8.h"
-#include "util.h"
-bool unichar_is_valid(
uint
32_t ch) {
+bool unichar_is_valid(
char
32_t ch) {
if (ch >= 0x110000) /* End of unicode space */
return false;
if (ch >= 0x110000) /* End of unicode space */
return false;
@@
-66,7
+65,7
@@
bool unichar_is_valid(uint32_t ch) {
return true;
}
return true;
}
-static bool unichar_is_control(
uint
32_t ch) {
+static bool unichar_is_control(
char
32_t ch) {
/*
0 to ' '-1 is the C0 range.
/*
0 to ' '-1 is the C0 range.
@@
-102,8
+101,9
@@
static int utf8_encoded_expected_len(const char *str) {
}
/* decode one unicode char */
}
/* decode one unicode char */
-int utf8_encoded_to_unichar(const char *str) {
- int unichar, len, i;
+int utf8_encoded_to_unichar(const char *str, char32_t *ret_unichar) {
+ char32_t unichar;
+ int len, i;
assert(str);
assert(str);
@@
-111,34
+111,37
@@
int utf8_encoded_to_unichar(const char *str) {
switch (len) {
case 1:
switch (len) {
case 1:
- return (int)str[0];
+ *ret_unichar = (char32_t)str[0];
+ return 0;
case 2:
unichar = str[0] & 0x1f;
break;
case 3:
case 2:
unichar = str[0] & 0x1f;
break;
case 3:
- unichar = (
in
t)str[0] & 0x0f;
+ unichar = (
char32_
t)str[0] & 0x0f;
break;
case 4:
break;
case 4:
- unichar = (
in
t)str[0] & 0x07;
+ unichar = (
char32_
t)str[0] & 0x07;
break;
case 5:
break;
case 5:
- unichar = (
in
t)str[0] & 0x03;
+ unichar = (
char32_
t)str[0] & 0x03;
break;
case 6:
break;
case 6:
- unichar = (
in
t)str[0] & 0x01;
+ unichar = (
char32_
t)str[0] & 0x01;
break;
default:
return -EINVAL;
}
for (i = 1; i < len; i++) {
break;
default:
return -EINVAL;
}
for (i = 1; i < len; i++) {
- if (((
in
t)str[i] & 0xc0) != 0x80)
+ if (((
char32_
t)str[i] & 0xc0) != 0x80)
return -EINVAL;
unichar <<= 6;
return -EINVAL;
unichar <<= 6;
- unichar |= (
in
t)str[i] & 0x3f;
+ unichar |= (
char32_
t)str[i] & 0x3f;
}
}
- return unichar;
+ *ret_unichar = unichar;
+
+ return 0;
}
bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
}
bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
@@
-147,15
+150,16
@@
bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
assert(str);
for (p = str; length;) {
assert(str);
for (p = str; length;) {
- int encoded_len, val;
+ int encoded_len, r;
+ char32_t val;
encoded_len = utf8_encoded_valid_unichar(p);
if (encoded_len < 0 ||
(size_t) encoded_len > length)
return false;
encoded_len = utf8_encoded_valid_unichar(p);
if (encoded_len < 0 ||
(size_t) encoded_len > length)
return false;
-
val = utf8_encoded_to_unichar(p
);
- if (
val
< 0 ||
+
r = utf8_encoded_to_unichar(p, &val
);
+ if (
r
< 0 ||
unichar_is_control(val) ||
(!newline && val == '\n'))
return false;
unichar_is_control(val) ||
(!newline && val == '\n'))
return false;
@@
-237,7
+241,7
@@
char *utf8_escape_non_printable(const char *str) {
*(s++) = hexchar((int) *str);
str += 1;
*(s++) = hexchar((int) *str);
str += 1;
- len
--;
+ len--;
}
}
} else {
}
}
} else {
@@
-275,7
+279,7
@@
char *ascii_is_valid(const char *str) {
* Returns: The length in bytes that the UTF-8 representation does or would
* occupy.
*/
* Returns: The length in bytes that the UTF-8 representation does or would
* occupy.
*/
-size_t utf8_encode_unichar(char *out_utf8,
uint
32_t g) {
+size_t utf8_encode_unichar(char *out_utf8,
char
32_t g) {
if (g < (1 << 7)) {
if (out_utf8)
if (g < (1 << 7)) {
if (out_utf8)
@@
-319,7
+323,7
@@
char *utf16_to_utf8(const void *s, size_t length) {
t = r;
while (f < (const uint8_t*) s + length) {
t = r;
while (f < (const uint8_t*) s + length) {
-
uint
16_t w1, w2;
+
char
16_t w1, w2;
/* see RFC 2781 section 2.2 */
/* see RFC 2781 section 2.2 */
@@
-353,7
+357,7
@@
char *utf16_to_utf8(const void *s, size_t length) {
}
/* expected size used to encode one unicode char */
}
/* expected size used to encode one unicode char */
-static int utf8_unichar_to_encoded_len(
in
t unichar) {
+static int utf8_unichar_to_encoded_len(
char32_
t unichar) {
if (unichar < 0x80)
return 1;
if (unichar < 0x80)
return 1;
@@
-371,7
+375,8
@@
static int utf8_unichar_to_encoded_len(int unichar) {
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str) {
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str) {
- int len, unichar, i;
+ int len, i, r;
+ char32_t unichar;
assert(str);
assert(str);
@@
-388,7
+393,9
@@
int utf8_encoded_valid_unichar(const char *str) {
if ((str[i] & 0x80) != 0x80)
return -EINVAL;
if ((str[i] & 0x80) != 0x80)
return -EINVAL;
- unichar = utf8_encoded_to_unichar(str);
+ r = utf8_encoded_to_unichar(str, &unichar);
+ if (r < 0)
+ return r;
/* check if encoded length matches encoded value */
if (utf8_unichar_to_encoded_len(unichar) != len)
/* check if encoded length matches encoded value */
if (utf8_unichar_to_encoded_len(unichar) != len)