chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
log: also set errno to the passed error code before processing format string in log_s...
[elogind.git]
/
src
/
shared
/
utf8.c
diff --git
a/src/shared/utf8.c
b/src/shared/utf8.c
index a8e28accd3b20e41aad631bda2d08a371cd5ee4a..8702ceb1b2f983b6a54e177f4191eac481885bf6 100644
(file)
--- a/
src/shared/utf8.c
+++ b/
src/shared/utf8.c
@@
-80,8
+80,11
@@
static bool is_unicode_control(uint32_t ch) {
/* count of characters used to encode one unicode char */
static int utf8_encoded_expected_len(const char *str) {
/* count of characters used to encode one unicode char */
static int utf8_encoded_expected_len(const char *str) {
- unsigned char c
= (unsigned char)str[0]
;
+ unsigned char c;
+ assert(str);
+
+ c = (unsigned char) str[0];
if (c < 0x80)
return 1;
if ((c & 0xe0) == 0xc0)
if (c < 0x80)
return 1;
if ((c & 0xe0) == 0xc0)
@@
-94,16
+97,18
@@
static int utf8_encoded_expected_len(const char *str) {
return 5;
if ((c & 0xfe) == 0xfc)
return 6;
return 5;
if ((c & 0xfe) == 0xfc)
return 6;
+
return 0;
}
/* decode one unicode char */
return 0;
}
/* decode one unicode char */
-
static
int utf8_encoded_to_unichar(const char *str) {
- int unichar;
- int len;
-
int i
;
+int utf8_encoded_to_unichar(const char *str) {
+ int unichar
, len, i
;
+
+
assert(str)
;
len = utf8_encoded_expected_len(str);
len = utf8_encoded_expected_len(str);
+
switch (len) {
case 1:
return (int)str[0];
switch (len) {
case 1:
return (int)str[0];
@@
-123,12
+128,12
@@
static int utf8_encoded_to_unichar(const char *str) {
unichar = (int)str[0] & 0x01;
break;
default:
unichar = (int)str[0] & 0x01;
break;
default:
- return -
1
;
+ return -
EINVAL
;
}
for (i = 1; i < len; i++) {
if (((int)str[i] & 0xc0) != 0x80)
}
for (i = 1; i < len; i++) {
if (((int)str[i] & 0xc0) != 0x80)
- return -
1
;
+ return -
EINVAL
;
unichar <<= 6;
unichar |= (int)str[i] & 0x3f;
}
unichar <<= 6;
unichar |= (int)str[i] & 0x3f;
}
@@
-136,19
+141,27
@@
static int utf8_encoded_to_unichar(const char *str) {
return unichar;
}
return unichar;
}
-bool utf8_is_printable
(const char* str, size_t length
) {
+bool utf8_is_printable
_newline(const char* str, size_t length, bool newline
) {
const uint8_t *p;
assert(str);
const uint8_t *p;
assert(str);
- for (p = (const uint8_t*) str; length; p++) {
- int encoded_len = utf8_encoded_valid_unichar((const char *)p);
- int32_t val = utf8_encoded_to_unichar((const char*)p);
+ for (p = (const uint8_t*) str; length;) {
+ int encoded_len, val;
+
+ encoded_len = utf8_encoded_valid_unichar((const char *) p);
+ if (encoded_len < 0 ||
+ (size_t) encoded_len > length)
+ return false;
- if (encoded_len < 0 || val < 0 || is_unicode_control(val))
+ val = utf8_encoded_to_unichar((const char*) p);
+ if (val < 0 ||
+ is_unicode_control(val) ||
+ (!newline && val == '\n'))
return false;
length -= encoded_len;
return false;
length -= encoded_len;
+ p += encoded_len;
}
return true;
}
return true;
@@
-160,8
+173,9
@@
const char *utf8_is_valid(const char *str) {
assert(str);
for (p = (const uint8_t*) str; *p; ) {
assert(str);
for (p = (const uint8_t*) str; *p; ) {
- int len
= utf8_encoded_valid_unichar((const char *)p)
;
+ int len;
+ len = utf8_encoded_valid_unichar((const char *)p);
if (len < 0)
return NULL;
if (len < 0)
return NULL;
@@
-171,37
+185,43
@@
const char *utf8_is_valid(const char *str) {
return str;
}
return str;
}
-char *
ascii_is_
valid(const char *str) {
- c
onst char *p
;
+char *
utf8_escape_in
valid(const char *str) {
+ c
har *p, *s
;
assert(str);
assert(str);
- for (p = str; *p; p++)
-
if ((unsigned char) *p >= 128
)
-
return NULL;
+ p = s = malloc(strlen(str) * 4 + 1);
+
if (!p
)
+ return NULL;
- return (char*) str;
-}
+ while (*str) {
+ int len;
+
+ len = utf8_encoded_valid_unichar(str);
+ if (len > 0) {
+ s = mempcpy(s, str, len);
+ str += len;
+ } else {
+ s = mempcpy(s, UTF8_REPLACEMENT_CHARACTER, strlen(UTF8_REPLACEMENT_CHARACTER));
+ str += 1;
+ }
+ }
-char *ascii_filter(const char *str) {
- const char *s;
- char *r, *d;
- size_t l;
+ *s = '\0';
- assert(str);
+ return p;
+}
- l = strlen(str);
- r = malloc(l + 1);
- if (!r)
- return NULL;
+char *ascii_is_valid(const char *str) {
+ const char *p;
- for (s = str, d = r; *s; s++)
- if ((unsigned char) *s < 128)
- *(d++) = *s;
+ assert(str);
- *d = 0;
+ for (p = str; *p; p++)
+ if ((unsigned char) *p >= 128)
+ return NULL;
- return r;
+ return
(char*) st
r;
}
char *utf16_to_utf8(const void *s, size_t length) {
}
char *utf16_to_utf8(const void *s, size_t length) {
@@
-242,6
+262,7
@@
char *utf16_to_utf8(const void *s, size_t length) {
/* expected size used to encode one unicode char */
static int utf8_unichar_to_encoded_len(int unichar) {
/* expected size used to encode one unicode char */
static int utf8_unichar_to_encoded_len(int unichar) {
+
if (unichar < 0x80)
return 1;
if (unichar < 0x800)
if (unichar < 0x80)
return 1;
if (unichar < 0x800)
@@
-252,18
+273,19
@@
static int utf8_unichar_to_encoded_len(int unichar) {
return 4;
if (unichar < 0x4000000)
return 5;
return 4;
if (unichar < 0x4000000)
return 5;
+
return 6;
}
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str) {
return 6;
}
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str) {
- int len;
- int unichar;
-
int i
;
+ int len
, unichar, i
;
+
+
assert(str)
;
len = utf8_encoded_expected_len(str);
if (len == 0)
len = utf8_encoded_expected_len(str);
if (len == 0)
- return -
1
;
+ return -
EINVAL
;
/* ascii is valid */
if (len == 1)
/* ascii is valid */
if (len == 1)
@@
-272,17
+294,17
@@
int utf8_encoded_valid_unichar(const char *str) {
/* check if expected encoded chars are available */
for (i = 0; i < len; i++)
if ((str[i] & 0x80) != 0x80)
/* check if expected encoded chars are available */
for (i = 0; i < len; i++)
if ((str[i] & 0x80) != 0x80)
- return -
1
;
+ return -
EINVAL
;
unichar = utf8_encoded_to_unichar(str);
/* check if encoded length matches encoded value */
if (utf8_unichar_to_encoded_len(unichar) != len)
unichar = utf8_encoded_to_unichar(str);
/* check if encoded length matches encoded value */
if (utf8_unichar_to_encoded_len(unichar) != len)
- return -
1
;
+ return -
EINVAL
;
/* check if value has valid range */
if (!is_unicode_valid(unichar))
/* check if value has valid range */
if (!is_unicode_valid(unichar))
- return -
1
;
+ return -
EINVAL
;
return len;
}
return len;
}