1
0
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:
Pavol Rusnak 2019-10-10 15:23:34 +00:00
parent ab534c18d3
commit 1bdc83838b
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 65 additions and 16 deletions

View File

@ -511,18 +511,43 @@ void display_printf(const char *fmt, ...) {
#if TREZOR_MODEL == T #if TREZOR_MODEL == T
static const uint8_t *get_glyph(int font, uint8_t c) { static uint8_t convert_char(const uint8_t c) {
if (c >= ' ' && c <= '~') { static char last_was_utf8 = 0;
// do nothing - valid ASCII
} else // non-printable ASCII character
// UTF-8 handling: https://en.wikipedia.org/wiki/UTF-8#Description if (c < ' ') {
if (c >= 0xC0) { last_was_utf8 = 0;
// bytes 11xxxxxx are first byte of UTF-8 characters return '_';
c = '_';
} else {
// bytes 10xxxxxx are successive UTF-8 characters
return 0;
} }
// 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) { switch (font) {
#ifdef TREZOR_FONT_NORMAL_ENABLE #ifdef TREZOR_FONT_NORMAL_ENABLE
case FONT_NORMAL: case FONT_NORMAL:

View File

@ -269,12 +269,36 @@ void oledDrawChar(int x, int y, char c, int font) {
} }
char oledConvertChar(const char c) { char oledConvertChar(const char c) {
uint8_t a = c; static char last_was_utf8 = 0;
if (a < 0x80) return c;
// 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 // 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 // bytes 11xxxxxx are first bytes of UTF-8 characters
if (a >= 0xC0) return '_'; 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; return 0;
} }