summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/unicode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/fe310/eos/unicode.c')
-rw-r--r--fw/fe310/eos/unicode.c70
1 files changed, 43 insertions, 27 deletions
diff --git a/fw/fe310/eos/unicode.c b/fw/fe310/eos/unicode.c
index 29100c7..d660247 100644
--- a/fw/fe310/eos/unicode.c
+++ b/fw/fe310/eos/unicode.c
@@ -1,6 +1,6 @@
#include "unicode.h"
-int utf8_enc(utf32_t ch, utf8_t *str) {
+int utf8_enc(ucp_t ch, utf8_t *str) {
if (ch <= 0x7f) {
str[0] = ch;
return 1;
@@ -25,30 +25,30 @@ int utf8_enc(utf32_t ch, utf8_t *str) {
}
}
-int utf8_dec(utf8_t *str, utf32_t *ch) {
+int utf8_dec(utf8_t *str, ucp_t *ch) {
if ((str[0] & 0x80) == 0x00) {
*ch = str[0];
return 1;
} else if ((str[0] & 0xe0) == 0xc0) {
if ((str[1] & 0xc0) != 0x80) return UTF_ERR;
- *ch = (utf32_t)(str[0] & 0x1f) << 6;
- *ch |= (utf32_t)(str[1] & 0x3f);
+ *ch = (ucp_t)(str[0] & 0x1f) << 6;
+ *ch |= (ucp_t)(str[1] & 0x3f);
if (*ch < 0x80) return UTF_ERR;
return 2;
} else if ((str[0] & 0xf0) == 0xe0) {
if (((str[1] & 0xc0) != 0x80) || ((str[2] & 0xc0) != 0x80)) return UTF_ERR;
- *ch = (utf32_t)(str[0] & 0x0f) << 12;
- *ch |= (utf32_t)(str[1] & 0x3f) << 6;
- *ch |= (utf32_t)(str[2] & 0x3f);
+ *ch = (ucp_t)(str[0] & 0x0f) << 12;
+ *ch |= (ucp_t)(str[1] & 0x3f) << 6;
+ *ch |= (ucp_t)(str[2] & 0x3f);
if ((*ch >= 0xd800) && (*ch <= 0xdfff)) return UTF_ERR;
if (*ch < 0x800) return UTF_ERR;
return 3;
} else if ((str[0] & 0xf8) == 0xf0) {
if (((str[1] & 0xc0) != 0x80) || ((str[2] & 0xc0) != 0x80) || ((str[3] & 0xc0) != 0x80)) return UTF_ERR;
- *ch = (utf32_t)(str[0] & 0x07) << 18;
- *ch |= (utf32_t)(str[1] & 0x0f) << 12;
- *ch |= (utf32_t)(str[2] & 0x3f) << 6;
- *ch |= (utf32_t)(str[3] & 0x3f);
+ *ch = (ucp_t)(str[0] & 0x07) << 18;
+ *ch |= (ucp_t)(str[1] & 0x0f) << 12;
+ *ch |= (ucp_t)(str[2] & 0x3f) << 6;
+ *ch |= (ucp_t)(str[3] & 0x3f);
if (*ch < 0x010000) return UTF_ERR;
if (*ch > 0x10ffff) return UTF_ERR;
return 4;
@@ -57,16 +57,25 @@ int utf8_dec(utf8_t *str, utf32_t *ch) {
}
}
-int utf8_len(utf8_t *str) {
- if ((*str & 0xf8) == 0xf0) return 4;
- if ((*str & 0xf0) == 0xe0) return 3;
- if ((*str & 0xe0) == 0xc0) return 2;
+int utf8_len_ch(ucp_t ch) {
+ if (ch <= 0x7f) return 1;
+ if (ch <= 0x7ff) return 2;
+ if (ch <= 0xffff) return 3;
+ if (ch <= 0x10ffff) return 4;
+
+ return UTF_ERR;
+}
+
+int utf8_len_str(utf8_t *str) {
if ((*str & 0x80) == 0x00) return 1;
+ if ((*str & 0xe0) == 0xc0) return 2;
+ if ((*str & 0xf0) == 0xe0) return 3;
+ if ((*str & 0xf8) == 0xf0) return 4;
return UTF_ERR;
}
-int utf8_seek(utf8_t *str, int off, utf32_t *ch) {
+int utf8_seek(utf8_t *str, int off, ucp_t *ch) {
int i;
int len = 0;
@@ -93,15 +102,16 @@ int utf8_seek(utf8_t *str, int off, utf32_t *ch) {
return len;
}
-int utf8_verify(utf8_t *str, int str_size, int *str_len) {
- utf32_t ch;
- uint8_t ch_l;
- int len = 0;
+int utf8_verify(utf8_t *str, size_t str_size, size_t *str_len) {
+ ucp_t ch;
+ size_t len = 0;
+ int ch_l, rv;
while (len < str_size) {
if (str_size - len < 4) {
- int _len = utf8_len(str + len);
- if ((_len == UTF_ERR) || ((str_size - len) < _len)) break;
+ rv = utf8_len_str(str + len);
+ if (rv < 0) break;
+ if (str_size - len < rv) break;
}
ch_l = utf8_dec(str + len, &ch);
if (ch_l > 0) {
@@ -119,7 +129,7 @@ int utf8_verify(utf8_t *str, int str_size, int *str_len) {
return UTF_ERR;
}
-int utf16_enc(utf32_t ch, uint8_t *str) {
+int utf16_enc(ucp_t ch, utf16_t *str) {
if (ch <= 0xffff) {
if ((ch >= 0xd800) && (ch <= 0xdfff)) return UTF_ERR;
str[0] = ch >> 8;
@@ -142,7 +152,7 @@ int utf16_enc(utf32_t ch, uint8_t *str) {
}
}
-int utf16_dec(uint8_t *str, utf32_t *ch) {
+int utf16_dec(utf16_t *str, ucp_t *ch) {
*ch = (str[0] << 8) | str[1];
if ((*ch >= 0xd800) && (*ch <= 0xdfff)) {
uint16_t hi = *ch;
@@ -158,15 +168,21 @@ int utf16_dec(uint8_t *str, utf32_t *ch) {
}
}
-int utf16_len(uint8_t *str) {
+int utf16_len_ch(ucp_t ch) {
+ if (ch <= 0xffff) return 2;
+ if (ch <= 0x10ffff) return 4;
+
+ return UTF_ERR;
+}
+
+int utf16_len_str(utf16_t *str) {
uint16_t ch = (str[0] << 8) | str[1];
- if ((ch >= 0xdc00) && (ch <= 0xdfff)) return UTF_ERR;
if ((ch >= 0xd800) && (ch <= 0xdfff)) return 4;
return 2;
}
-int utf16_seek(uint8_t *str, int off, utf32_t *ch) {
+int utf16_seek(utf16_t *str, int off, ucp_t *ch) {
int i;
int len = 0;
uint16_t cu;