From: Jiri Slaby <jslaby@suse.cz>
To: gregkh@linuxfoundation.org
Cc: linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org,
Jiri Slaby <jslaby@suse.cz>
Subject: [PATCH 12/38] vt: separate unicode handling into vc_translate_unicode
Date: Mon, 15 Jun 2020 09:48:44 +0200 [thread overview]
Message-ID: <20200615074910.19267-12-jslaby@suse.cz> (raw)
In-Reply-To: <20200615074910.19267-1-jslaby@suse.cz>
do_con_write is complicated enough. Extract unicode handling to a
separate function. For do_con_write, 249 LOCs lowered to 183 lines.
Use diff -w -b to see the difference is neligible -- mostly whitespace
and use of 'return's instead of 'continue's.
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
---
drivers/tty/vt/vt.c | 149 ++++++++++++++++++++++++--------------------
1 file changed, 81 insertions(+), 68 deletions(-)
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index a9e4924fa675..caaad820413a 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -2572,6 +2572,85 @@ static inline int vc_translate_ascii(const struct vc_data *vc, int c)
return c;
}
+/**
+ * vc_translate_unicode -- Combine UTF-8 into Unicode in @vc_utf_char
+ *
+ * @vc_utf_char is the being-constructed unicode character.
+ * @vc_utf_count is the number of continuation bytes still expected to arrive.
+ * @vc_npar is the number of continuation bytes arrived so far.
+ */
+static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan)
+{
+ static const u32 utf8_length_changes[] = {
+ 0x0000007f, 0x000007ff, 0x0000ffff,
+ 0x001fffff, 0x03ffffff, 0x7fffffff
+ };
+
+ if ((c & 0xc0) == 0x80) {
+ /* Continuation byte received */
+ if (vc->vc_utf_count) {
+ vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
+ vc->vc_npar++;
+ if (--vc->vc_utf_count) {
+ /* Still need some bytes */
+ return -1;
+ }
+ /* Got a whole character */
+ c = vc->vc_utf_char;
+ /* Reject overlong sequences */
+ if (c <= utf8_length_changes[vc->vc_npar - 1] ||
+ c > utf8_length_changes[vc->vc_npar])
+ return 0xfffd;
+ } else {
+ /* Unexpected continuation byte */
+ vc->vc_utf_count = 0;
+ return 0xfffd;
+ }
+ } else {
+ /* Single ASCII byte or first byte of a sequence received */
+ if (vc->vc_utf_count) {
+ /* Continuation byte expected */
+ *rescan = true;
+ vc->vc_utf_count = 0;
+ return 0xfffd;
+ } else if (c > 0x7f) {
+ /* First byte of a multibyte sequence received */
+ vc->vc_npar = 0;
+ if ((c & 0xe0) == 0xc0) {
+ vc->vc_utf_count = 1;
+ vc->vc_utf_char = (c & 0x1f);
+ } else if ((c & 0xf0) == 0xe0) {
+ vc->vc_utf_count = 2;
+ vc->vc_utf_char = (c & 0x0f);
+ } else if ((c & 0xf8) == 0xf0) {
+ vc->vc_utf_count = 3;
+ vc->vc_utf_char = (c & 0x07);
+ } else if ((c & 0xfc) == 0xf8) {
+ vc->vc_utf_count = 4;
+ vc->vc_utf_char = (c & 0x03);
+ } else if ((c & 0xfe) == 0xfc) {
+ vc->vc_utf_count = 5;
+ vc->vc_utf_char = (c & 0x01);
+ } else {
+ /* 254 and 255 are invalid */
+ return 0xfffd;
+ }
+ if (vc->vc_utf_count) {
+ /* Still need some bytes */
+ return -1;
+ }
+ }
+ /* Nothing to do if an ASCII byte was received */
+ }
+ /* End of UTF-8 decoding. */
+ /* c is the received character, or U+FFFD for invalid sequences. */
+ /* Replace invalid Unicode code points with U+FFFD too */
+ if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
+ return 0xfffd;
+
+ return c;
+}
+
/* acquires console_lock */
static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
{
@@ -2628,76 +2707,10 @@ static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int co
if (vc->vc_state != ESnormal) {
tc = c;
} else if (vc->vc_utf && !vc->vc_disp_ctrl) {
- /* Combine UTF-8 into Unicode in vc_utf_char.
- * vc_utf_count is the number of continuation bytes still
- * expected to arrive.
- * vc_npar is the number of continuation bytes arrived so
- * far
- */
rescan_last_byte:
- if ((c & 0xc0) == 0x80) {
- /* Continuation byte received */
- static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
- if (vc->vc_utf_count) {
- vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
- vc->vc_npar++;
- if (--vc->vc_utf_count) {
- /* Still need some bytes */
- continue;
- }
- /* Got a whole character */
- c = vc->vc_utf_char;
- /* Reject overlong sequences */
- if (c <= utf8_length_changes[vc->vc_npar - 1] ||
- c > utf8_length_changes[vc->vc_npar])
- c = 0xfffd;
- } else {
- /* Unexpected continuation byte */
- vc->vc_utf_count = 0;
- c = 0xfffd;
- }
- } else {
- /* Single ASCII byte or first byte of a sequence received */
- if (vc->vc_utf_count) {
- /* Continuation byte expected */
- rescan = true;
- vc->vc_utf_count = 0;
- c = 0xfffd;
- } else if (c > 0x7f) {
- /* First byte of a multibyte sequence received */
- vc->vc_npar = 0;
- if ((c & 0xe0) == 0xc0) {
- vc->vc_utf_count = 1;
- vc->vc_utf_char = (c & 0x1f);
- } else if ((c & 0xf0) == 0xe0) {
- vc->vc_utf_count = 2;
- vc->vc_utf_char = (c & 0x0f);
- } else if ((c & 0xf8) == 0xf0) {
- vc->vc_utf_count = 3;
- vc->vc_utf_char = (c & 0x07);
- } else if ((c & 0xfc) == 0xf8) {
- vc->vc_utf_count = 4;
- vc->vc_utf_char = (c & 0x03);
- } else if ((c & 0xfe) == 0xfc) {
- vc->vc_utf_count = 5;
- vc->vc_utf_char = (c & 0x01);
- } else {
- /* 254 and 255 are invalid */
- c = 0xfffd;
- }
- if (vc->vc_utf_count) {
- /* Still need some bytes */
+ tc = c = vc_translate_unicode(vc, c, &rescan);
+ if (tc == -1)
continue;
- }
- }
- /* Nothing to do if an ASCII byte was received */
- }
- /* End of UTF-8 decoding. */
- /* c is the received character, or U+FFFD for invalid sequences. */
- /* Replace invalid Unicode code points with U+FFFD too */
- if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
- c = 0xfffd;
- tc = c;
} else { /* no utf or alternate charset mode */
tc = vc_translate_ascii(vc, c);
}
--
2.27.0
next prev parent reply other threads:[~2020-06-15 7:49 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-06-15 7:48 [PATCH 01/38] vc: separate state Jiri Slaby
2020-06-15 7:48 ` [PATCH 02/38] vt: introduce enum vc_intensity for intensity Jiri Slaby
2020-06-15 7:48 ` [PATCH 03/38] vc: switch state to bool Jiri Slaby
2020-06-15 7:48 ` [PATCH 04/38] vt: deduplicate setGx code Jiri Slaby
2020-06-15 7:48 ` [PATCH 05/38] vt: switch G0/1_charset to an array Jiri Slaby
2020-06-15 7:48 ` [PATCH 06/38] vt: convert vc_tab_stop to bitmap Jiri Slaby
2020-06-15 7:48 ` [PATCH 07/38] vt: remove 25 years stale comment Jiri Slaby
2020-06-15 7:48 ` [PATCH 08/38] vt: use tty_insert_flip_string in respond_string Jiri Slaby
2020-06-15 7:48 ` [PATCH 09/38] vt: get rid of VT10.ID macros Jiri Slaby
2020-06-15 7:48 ` [PATCH 10/38] vt: move vc_translate to vt.c and rename it Jiri Slaby
2020-06-15 7:48 ` [PATCH 11/38] vt: use modern types in do_con_write Jiri Slaby
2020-06-15 7:48 ` Jiri Slaby [this message]
2020-06-15 7:48 ` [PATCH 13/38] vt: rearrange vc_translate_unicode Jiri Slaby
2020-06-15 7:48 ` [PATCH 14/38] vt: extract attribute inversion to vc_invert_attr Jiri Slaby
2020-06-15 7:48 ` [PATCH 15/38] vt: move rescan_last_byte label earlier Jiri Slaby
2020-06-15 7:48 ` [PATCH 16/38] vc: move translation out of do_con_write Jiri Slaby
2020-06-15 7:48 ` [PATCH 17/38] vc: introduce struct vc_draw_region Jiri Slaby
2020-06-15 7:48 ` [PATCH 18/38] vc: extract detecting control characters from do_con_write Jiri Slaby
2020-06-15 7:48 ` [PATCH 19/38] vc: move normal char processing " Jiri Slaby
2020-06-15 7:48 ` [PATCH 20/38] vc: simplify condition in vc_con_write_normal Jiri Slaby
2020-06-15 7:48 ` [PATCH 21/38] vt: simplify vc_attr handling " Jiri Slaby
2020-06-15 7:48 ` [PATCH 22/38] vt: make tc write more obvious " Jiri Slaby
2020-06-15 7:48 ` [PATCH 23/38] vt: synchronize types and use min in csi_X Jiri Slaby
2020-06-15 7:48 ` [PATCH 24/38] vt: whitespace and paren cleanup in add_softcursor Jiri Slaby
2020-06-15 7:48 ` [PATCH 25/38] vt: redefine world of cursor macros Jiri Slaby
2020-06-15 7:48 ` [PATCH 26/38] vt: use newly defined CUR_* macros Jiri Slaby
2020-06-15 20:31 ` Helge Deller
2020-06-15 7:48 ` [PATCH 27/38] vt: remove superfluous parens in invert_screen and build_attr Jiri Slaby
2020-06-15 7:49 ` [PATCH 28/38] vt: simplify noncolor attributes in build_attr Jiri Slaby
2020-06-15 7:49 ` [PATCH 29/38] vt_ioctl: eliminate ret & breaks in vt_ioctl Jiri Slaby
2020-06-15 7:49 ` [PATCH 30/38] vt_ioctl: eliminate use of uival and ucval Jiri Slaby
2020-06-15 7:49 ` [PATCH 31/38] vt_ioctl: move K* ioctls to a separate function Jiri Slaby
2020-06-15 7:49 ` [PATCH 32/38] vt_ioctl: move io " Jiri Slaby
2020-06-15 7:49 ` [PATCH 33/38] vt_ioctl: move vt_setactivate out of vt_ioctl Jiri Slaby
2020-06-15 7:49 ` [PATCH 34/38] vt_ioctl: move vt_reldisp " Jiri Slaby
2020-06-15 7:49 ` [PATCH 35/38] vt_ioctl: move vt_resizex " Jiri Slaby
2020-06-15 7:49 ` [PATCH 36/38] vt_ioctl: move vt_io_fontreset out of vt_io_ioctl Jiri Slaby
2020-06-15 7:49 ` [PATCH 37/38] vt_ioctl: move vt_kdsetmode out of vt_k_ioctl Jiri Slaby
2020-06-15 7:49 ` [PATCH 38/38] vt_ioctl: move perm checks level up Jiri Slaby
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200615074910.19267-12-jslaby@suse.cz \
--to=jslaby@suse.cz \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-serial@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).