From a44c40fd5438ea3312d1cf4d93fdc5f8335ba578 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Mon, 3 Aug 2020 03:53:11 +0200 Subject: unicode moved --- code/fe310/eos/Makefile | 2 +- code/fe310/eos/eve/Makefile | 2 +- code/fe310/eos/eve/unicode.c | 180 ------------------------------------------- code/fe310/eos/eve/unicode.h | 17 ---- code/fe310/eos/unicode.c | 180 +++++++++++++++++++++++++++++++++++++++++++ code/fe310/eos/unicode.h | 17 ++++ 6 files changed, 199 insertions(+), 199 deletions(-) delete mode 100644 code/fe310/eos/eve/unicode.c delete mode 100644 code/fe310/eos/eve/unicode.h create mode 100644 code/fe310/eos/unicode.c create mode 100644 code/fe310/eos/unicode.h (limited to 'code') diff --git a/code/fe310/eos/Makefile b/code/fe310/eos/Makefile index 48b5319..71465a4 100644 --- a/code/fe310/eos/Makefile +++ b/code/fe310/eos/Makefile @@ -2,7 +2,7 @@ include ../common.mk CFLAGS += -I../include -I../drivers -obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o power.o i2s.o uart.o spi.o spi_dev.o net.o wifi.o cell.o sock.o +obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o power.o i2s.o uart.o spi.o spi_dev.o net.o wifi.o cell.o sock.o unicode.o %.o: %.c %.h diff --git a/code/fe310/eos/eve/Makefile b/code/fe310/eos/eve/Makefile index 3355dd5..0a737cc 100644 --- a/code/fe310/eos/eve/Makefile +++ b/code/fe310/eos/eve/Makefile @@ -2,7 +2,7 @@ include ../../common.mk CFLAGS += -I.. -I../../include -obj = eve.o eve_touch.o eve_track.o eve_kbd.o eve_text.o eve_platform.o unicode.o +obj = eve.o eve_touch.o eve_track.o eve_kbd.o eve_text.o eve_platform.o %.o: %.c %.h diff --git a/code/fe310/eos/eve/unicode.c b/code/fe310/eos/eve/unicode.c deleted file mode 100644 index 2915791..0000000 --- a/code/fe310/eos/eve/unicode.c +++ /dev/null @@ -1,180 +0,0 @@ -#include "unicode.h" - -int utf8_enc(utf32_t ch, utf8_t *str) { - if (ch <= 0x7f) { - str[0] = ch; - return 1; - } else if (ch <= 0x7ff) { - str[0] = 0xc0 | (ch >> 6); - str[1] = 0x80 | (ch & 0x3f); - return 2; - } else if (ch <= 0xffff) { - if ((ch >= 0xd800) && (ch <= 0xdfff)) return UTF_ERR; - str[0] = 0xe0 | (ch >> 12); - str[1] = 0x80 | ((ch >> 6) & 0x3f); - str[2] = 0x80 | (ch & 0x3f); - return 3; - } else if (ch <= 0x10ffff) { - str[0] = 0xf0 | (ch >> 18); - str[1] = 0x80 | ((ch >> 12) & 0x3f); - str[2] = 0x80 | ((ch >> 6) & 0x3f); - str[3] = 0x80 | (ch & 0x3f); - return 4; - } else { - return UTF_ERR; - } -} - -int utf8_dec(utf8_t *str, utf32_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); - 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); - 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); - if (*ch < 0x010000) return UTF_ERR; - if (*ch > 0x10ffff) return UTF_ERR; - return 4; - } else { - return UTF_ERR; - } -} - -int utf8_seek(utf8_t *str, int off, utf32_t *ch) { - int i; - int len = 0; - - if (off < 0) { - off = -off; - for (i=0; i 0) { - if (ch == 0) { - *str_len = len; - return UTF_OK; - } - len += ch_l; - } else { - break; - } - } - *str_len = len; - return UTF_ERR; -} - -int utf16_enc(utf32_t ch, uint8_t *str) { - if (ch <= 0xffff) { - if ((ch >= 0xd800) && (ch <= 0xdfff)) return UTF_ERR; - str[0] = ch >> 8; - str[1] = ch & 0xff; - return 2; - } else if (ch <= 0x10ffff) { - uint16_t hi; - uint16_t lo; - - ch -= 0x10000; - hi = (ch >> 10) + 0xd800; - lo = (ch & 0x3ff) + 0xdc00; - str[0] = hi >> 8; - str[1] = hi & 0xff; - str[2] = lo >> 8; - str[3] = lo & 0xff; - return 4; - } else { - return UTF_ERR; - } -} - -int utf16_dec(uint8_t *str, utf32_t *ch) { - *ch = (str[0] << 8) | str[1]; - if ((*ch >= 0xd800) && (*ch <= 0xdfff)) { - uint16_t hi = *ch; - uint16_t lo; - - if (hi > 0xdbff) return UTF_ERR; - lo = (str[2] << 8) | str[3]; - if ((lo < 0xdc00) || (lo > 0xdfff)) return UTF_ERR; - *ch = (((hi - 0xd800) << 10) | (lo - 0xdc00)) + 0x10000; - return 4; - } else { - return 2; - } -} - -int utf16_seek(uint8_t *str, int off, utf32_t *ch) { - int i; - int len = 0; - uint16_t cu; - - if (off < 0) { - off = -off; - for (i=0; i= 0xdc00) && (cu <= 0xdfff)) { - len -= 2; - } - } - } else { - for (i=0; i= 0xd800) && (cu <= 0xdbff)) { - len += 4; - } else { - len += 2; - } - } - } - utf16_dec(str + len, ch); - return len; -} diff --git a/code/fe310/eos/eve/unicode.h b/code/fe310/eos/eve/unicode.h deleted file mode 100644 index a3b9696..0000000 --- a/code/fe310/eos/eve/unicode.h +++ /dev/null @@ -1,17 +0,0 @@ -#include - -#define UTF_OK 0 -#define UTF_ERR -1 - -typedef uint8_t utf8_t; -typedef uint16_t utf16_t; -typedef uint32_t utf32_t; - -int utf8_enc(utf32_t ch, utf8_t *str); -int utf8_dec(utf8_t *str, utf32_t *ch); -int utf8_seek(utf8_t *str, int off, utf32_t *ch); -int utf8_verify(utf8_t *str, int str_size, int *str_len); - -int utf16_enc(utf32_t ch, uint8_t *str); -int utf16_dec(uint8_t *str, utf32_t *ch); -int utf16_seek(uint8_t *str, int off, utf32_t *ch); diff --git a/code/fe310/eos/unicode.c b/code/fe310/eos/unicode.c new file mode 100644 index 0000000..2915791 --- /dev/null +++ b/code/fe310/eos/unicode.c @@ -0,0 +1,180 @@ +#include "unicode.h" + +int utf8_enc(utf32_t ch, utf8_t *str) { + if (ch <= 0x7f) { + str[0] = ch; + return 1; + } else if (ch <= 0x7ff) { + str[0] = 0xc0 | (ch >> 6); + str[1] = 0x80 | (ch & 0x3f); + return 2; + } else if (ch <= 0xffff) { + if ((ch >= 0xd800) && (ch <= 0xdfff)) return UTF_ERR; + str[0] = 0xe0 | (ch >> 12); + str[1] = 0x80 | ((ch >> 6) & 0x3f); + str[2] = 0x80 | (ch & 0x3f); + return 3; + } else if (ch <= 0x10ffff) { + str[0] = 0xf0 | (ch >> 18); + str[1] = 0x80 | ((ch >> 12) & 0x3f); + str[2] = 0x80 | ((ch >> 6) & 0x3f); + str[3] = 0x80 | (ch & 0x3f); + return 4; + } else { + return UTF_ERR; + } +} + +int utf8_dec(utf8_t *str, utf32_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); + 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); + 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); + if (*ch < 0x010000) return UTF_ERR; + if (*ch > 0x10ffff) return UTF_ERR; + return 4; + } else { + return UTF_ERR; + } +} + +int utf8_seek(utf8_t *str, int off, utf32_t *ch) { + int i; + int len = 0; + + if (off < 0) { + off = -off; + for (i=0; i 0) { + if (ch == 0) { + *str_len = len; + return UTF_OK; + } + len += ch_l; + } else { + break; + } + } + *str_len = len; + return UTF_ERR; +} + +int utf16_enc(utf32_t ch, uint8_t *str) { + if (ch <= 0xffff) { + if ((ch >= 0xd800) && (ch <= 0xdfff)) return UTF_ERR; + str[0] = ch >> 8; + str[1] = ch & 0xff; + return 2; + } else if (ch <= 0x10ffff) { + uint16_t hi; + uint16_t lo; + + ch -= 0x10000; + hi = (ch >> 10) + 0xd800; + lo = (ch & 0x3ff) + 0xdc00; + str[0] = hi >> 8; + str[1] = hi & 0xff; + str[2] = lo >> 8; + str[3] = lo & 0xff; + return 4; + } else { + return UTF_ERR; + } +} + +int utf16_dec(uint8_t *str, utf32_t *ch) { + *ch = (str[0] << 8) | str[1]; + if ((*ch >= 0xd800) && (*ch <= 0xdfff)) { + uint16_t hi = *ch; + uint16_t lo; + + if (hi > 0xdbff) return UTF_ERR; + lo = (str[2] << 8) | str[3]; + if ((lo < 0xdc00) || (lo > 0xdfff)) return UTF_ERR; + *ch = (((hi - 0xd800) << 10) | (lo - 0xdc00)) + 0x10000; + return 4; + } else { + return 2; + } +} + +int utf16_seek(uint8_t *str, int off, utf32_t *ch) { + int i; + int len = 0; + uint16_t cu; + + if (off < 0) { + off = -off; + for (i=0; i= 0xdc00) && (cu <= 0xdfff)) { + len -= 2; + } + } + } else { + for (i=0; i= 0xd800) && (cu <= 0xdbff)) { + len += 4; + } else { + len += 2; + } + } + } + utf16_dec(str + len, ch); + return len; +} diff --git a/code/fe310/eos/unicode.h b/code/fe310/eos/unicode.h new file mode 100644 index 0000000..a3b9696 --- /dev/null +++ b/code/fe310/eos/unicode.h @@ -0,0 +1,17 @@ +#include + +#define UTF_OK 0 +#define UTF_ERR -1 + +typedef uint8_t utf8_t; +typedef uint16_t utf16_t; +typedef uint32_t utf32_t; + +int utf8_enc(utf32_t ch, utf8_t *str); +int utf8_dec(utf8_t *str, utf32_t *ch); +int utf8_seek(utf8_t *str, int off, utf32_t *ch); +int utf8_verify(utf8_t *str, int str_size, int *str_len); + +int utf16_enc(utf32_t ch, uint8_t *str); +int utf16_dec(uint8_t *str, utf32_t *ch); +int utf16_seek(uint8_t *str, int off, utf32_t *ch); -- cgit v1.2.3