mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-11 16:00:57 +00:00
legacy+core: properly handle non-printable ascii characters
(convert them to '_')
This commit is contained in:
parent
ab534c18d3
commit
1bdc83838b
@ -511,18 +511,43 @@ void display_printf(const char *fmt, ...) {
|
||||
|
||||
#if TREZOR_MODEL == T
|
||||
|
||||
static const uint8_t *get_glyph(int font, uint8_t c) {
|
||||
if (c >= ' ' && c <= '~') {
|
||||
// do nothing - valid ASCII
|
||||
} else
|
||||
// UTF-8 handling: https://en.wikipedia.org/wiki/UTF-8#Description
|
||||
if (c >= 0xC0) {
|
||||
// bytes 11xxxxxx are first byte of UTF-8 characters
|
||||
c = '_';
|
||||
} else {
|
||||
// bytes 10xxxxxx are successive UTF-8 characters
|
||||
return 0;
|
||||
static uint8_t convert_char(const uint8_t c) {
|
||||
static char last_was_utf8 = 0;
|
||||
|
||||
// non-printable ASCII character
|
||||
if (c < ' ') {
|
||||
last_was_utf8 = 0;
|
||||
return '_';
|
||||
}
|
||||
|
||||
// regular ASCII character
|
||||
if (c < 0x80) {
|
||||
last_was_utf8 = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
// UTF-8 handling: https://en.wikipedia.org/wiki/UTF-8#Description
|
||||
|
||||
// bytes 11xxxxxx are first bytes of UTF-8 characters
|
||||
if (c >= 0xC0) {
|
||||
last_was_utf8 = 1;
|
||||
return '_';
|
||||
}
|
||||
|
||||
if (last_was_utf8) {
|
||||
// bytes 10xxxxxx can be successive UTF-8 characters ...
|
||||
return 0; // skip glyph
|
||||
} else {
|
||||
// ... or they are just non-printable ASCII characters
|
||||
return '_';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const uint8_t *get_glyph(int font, uint8_t c) {
|
||||
c = convert_char(c);
|
||||
if (!c) return 0;
|
||||
switch (font) {
|
||||
#ifdef TREZOR_FONT_NORMAL_ENABLE
|
||||
case FONT_NORMAL:
|
||||
|
@ -269,12 +269,36 @@ void oledDrawChar(int x, int y, char c, int font) {
|
||||
}
|
||||
|
||||
char oledConvertChar(const char c) {
|
||||
uint8_t a = c;
|
||||
if (a < 0x80) return c;
|
||||
static char last_was_utf8 = 0;
|
||||
|
||||
// non-printable ASCII character
|
||||
if (c < ' ') {
|
||||
last_was_utf8 = 0;
|
||||
return '_';
|
||||
}
|
||||
|
||||
// regular ASCII character
|
||||
if (c < 0x80) {
|
||||
last_was_utf8 = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
// UTF-8 handling: https://en.wikipedia.org/wiki/UTF-8#Description
|
||||
// bytes 11xxxxxx are first byte of UTF-8 characters
|
||||
// bytes 10xxxxxx are successive UTF-8 characters
|
||||
if (a >= 0xC0) return '_';
|
||||
|
||||
// bytes 11xxxxxx are first bytes of UTF-8 characters
|
||||
if (c >= 0xC0) {
|
||||
last_was_utf8 = 1;
|
||||
return '_';
|
||||
}
|
||||
|
||||
if (last_was_utf8) {
|
||||
// bytes 10xxxxxx can be successive UTF-8 characters ...
|
||||
return 0; // skip glyph
|
||||
} else {
|
||||
// ... or they are just non-printable ASCII characters
|
||||
return '_';
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user