From 748284077276887239cbb2f7eecb3aa59f7fedec Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Thu, 9 Dec 2010 12:14:41 +0100 Subject: [PATCH] properly check the player name length as utf-8 octet count --- qcsrc/common/util.qc | 20 +++++++++++++++++++ qcsrc/common/util.qh | 2 ++ qcsrc/menu/item/inputbox.c | 14 ++++++++++--- .../xonotic/dialog_multiplayer_playersetup.c | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/qcsrc/common/util.qc b/qcsrc/common/util.qc index 44f875173..83ea8e3e5 100644 --- a/qcsrc/common/util.qc +++ b/qcsrc/common/util.qc @@ -1953,3 +1953,23 @@ float vercmp(string v1, string v2) return vercmp_recursive(v1, v2); } + +float u8_strsize(string s) +{ + float l, i, c; + l = 0; + for(i = 0; ; ++i) + { + c = str2chr(s, i); + if(c <= 0) + break; + ++l; + if(c >= 0x80) + ++l; + if(c >= 0x800) + ++l; + if(c >= 0x10000) + ++l; + } + return l; +} diff --git a/qcsrc/common/util.qh b/qcsrc/common/util.qh index cfb955e35..79b93d3ed 100644 --- a/qcsrc/common/util.qh +++ b/qcsrc/common/util.qh @@ -247,3 +247,5 @@ vector NearestPointOnBox(entity box, vector org); #endif float vercmp(string v1, string v2); + +float u8_strsize(string s); diff --git a/qcsrc/menu/item/inputbox.c b/qcsrc/menu/item/inputbox.c index 2d6459030..750bc83b8 100644 --- a/qcsrc/menu/item/inputbox.c +++ b/qcsrc/menu/item/inputbox.c @@ -25,7 +25,7 @@ CLASS(InputBox) EXTENDS(Label) ATTRIB(InputBox, forbiddenCharacters, string, "") ATTRIB(InputBox, color, vector, '1 1 1') ATTRIB(InputBox, colorF, vector, '1 1 1') - ATTRIB(InputBox, maxLength, float, 255) + ATTRIB(InputBox, maxLength, float, 255) // if negative, it counts bytes, not chars ENDCLASS(InputBox) void InputBox_Clear_Click(entity btn, entity me); #endif @@ -79,8 +79,16 @@ void InputBox_enterText(entity me, string ch) for(i = 0; i < strlen(ch); ++i) if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1) return; - if(strlen(ch) + strlen(me.text) > me.maxLength) - return; + if(me.maxLength > 0) + { + if(strlen(ch) + strlen(me.text) > me.maxLength) + return; + } + else if(me.maxLength < 0) + { + if(u8_strsize(ch) + u8_strsize(me.text) > -me.maxLength) + return; + } me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos))); me.cursorPos += strlen(ch); } diff --git a/qcsrc/menu/xonotic/dialog_multiplayer_playersetup.c b/qcsrc/menu/xonotic/dialog_multiplayer_playersetup.c index 562c097ce..586896171 100644 --- a/qcsrc/menu/xonotic/dialog_multiplayer_playersetup.c +++ b/qcsrc/menu/xonotic/dialog_multiplayer_playersetup.c @@ -46,7 +46,7 @@ void XonoticPlayerSettingsTab_fill(entity me) me.TR(me); me.TD(me, 1, 3.0, box = makeXonoticInputBox(1, "_cl_name")); box.forbiddenCharacters = "\r\n\\\"$"; // don't care, isn't getting saved - box.maxLength = 63; + box.maxLength = -63; // negativ means encoded length in bytes label.textEntity = box; me.TR(me); me.TD(me, 5, 1, e = makeXonoticColorpicker(box)); -- 2.39.2