From nobody Wed Nov 27 02:27:51 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1701983172625267.8490105331905; Thu, 7 Dec 2023 13:06:12 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rBLXw-0002hl-B7; Thu, 07 Dec 2023 16:04:36 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rBLXq-0002bS-Dk for qemu-devel@nongnu.org; Thu, 07 Dec 2023 16:04:30 -0500 Received: from smtp1.lauterbach.com ([62.154.241.196]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rBLXm-0005n6-9G for qemu-devel@nongnu.org; Thu, 07 Dec 2023 16:04:30 -0500 Received: (qmail 10238 invoked by uid 484); 7 Dec 2023 21:04:03 -0000 Received: from nedpc1.intern.lauterbach.com (Authenticated_SSL:neder@[10.2.11.92]) (envelope-sender ) by smtp1.lauterbach.com (qmail-ldap-1.03) with TLS_AES_256_GCM_SHA384 encrypted SMTP for ; 7 Dec 2023 21:04:02 -0000 X-Qmail-Scanner-Diagnostics: from nedpc1.intern.lauterbach.com by smtp1.lauterbach.com (envelope-from , uid 484) with qmail-scanner-2.11 (mhr: 1.0. clamdscan: 0.99/21437. spamassassin: 3.4.0. Clear:RC:1(10.2.11.92):. Processed in 0.081391 secs); 07 Dec 2023 21:04:03 -0000 From: Nicolas Eder To: qemu-devel@nongnu.org Cc: "Nicolas Eder" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , "Christian Boenig" , =?UTF-8?q?Alex=20Benn=C3=A9e?= Subject: [PATCH v4 02/17] gdbstub: hex conversion functions moved to cutils.h Date: Thu, 7 Dec 2023 22:03:43 +0100 Message-Id: <20231207210358.7409-3-nicolas.eder@lauterbach.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231207210358.7409-1-nicolas.eder@lauterbach.com> References: <20231207210358.7409-1-nicolas.eder@lauterbach.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Qmail-Scanner-2.11: added fake Content-Type header Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=62.154.241.196; envelope-from=nicolas.eder@lauterbach.com; helo=smtp1.lauterbach.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1701983173388100006 Content-Type: text/plain; charset="utf-8" --- debug/gdbstub/gdbstub.c | 19 ++++++++++--------- debug/gdbstub/internals.h | 26 -------------------------- include/qemu/cutils.h | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/debug/gdbstub/gdbstub.c b/debug/gdbstub/gdbstub.c index 46d752bbc2..f43d4355c0 100644 --- a/debug/gdbstub/gdbstub.c +++ b/debug/gdbstub/gdbstub.c @@ -80,8 +80,8 @@ void gdb_memtohex(GString *buf, const uint8_t *mem, int l= en) int i, c; for(i =3D 0; i < len; i++) { c =3D mem[i]; - g_string_append_c(buf, tohex(c >> 4)); - g_string_append_c(buf, tohex(c & 0xf)); + g_string_append_c(buf, nibble_to_hexchar(c >> 4)); + g_string_append_c(buf, nibble_to_hexchar(c & 0xf)); } g_string_append_c(buf, '\0'); } @@ -91,7 +91,8 @@ void gdb_hextomem(GByteArray *mem, const char *buf, int l= en) int i; =20 for(i =3D 0; i < len; i++) { - guint8 byte =3D fromhex(buf[0]) << 4 | fromhex(buf[1]); + guint8 byte =3D hexchar_to_nibble(buf[0]) << 4 | + hexchar_to_nibble(buf[1]); g_byte_array_append(mem, &byte, 1); buf +=3D 2; } @@ -118,8 +119,8 @@ static void hexdump(const char *buf, int len, if (i < len) { char value =3D buf[i]; =20 - line_buffer[hex_col + 0] =3D tohex((value >> 4) & 0xF); - line_buffer[hex_col + 1] =3D tohex((value >> 0) & 0xF); + line_buffer[hex_col + 0] =3D nibble_to_hexchar((value >> 4) & = 0xF); + line_buffer[hex_col + 1] =3D nibble_to_hexchar((value >> 0) & = 0xF); line_buffer[txt_col + 0] =3D (value >=3D ' ' && value < 127) ? value : '.'; @@ -151,8 +152,8 @@ int gdb_put_packet_binary(const char *buf, int len, boo= l dump) csum +=3D buf[i]; } footer[0] =3D '#'; - footer[1] =3D tohex((csum >> 4) & 0xf); - footer[2] =3D tohex((csum) & 0xf); + footer[1] =3D nibble_to_hexchar((csum >> 4) & 0xf); + footer[2] =3D nibble_to_hexchar((csum) & 0xf); g_byte_array_append(gdbserver_state.last_packet, footer, 3); =20 gdb_put_buffer(gdbserver_state.last_packet->data, @@ -2267,7 +2268,7 @@ void gdb_read_byte(uint8_t ch) break; } gdbserver_state.line_buf[gdbserver_state.line_buf_index] =3D '= \0'; - gdbserver_state.line_csum =3D fromhex(ch) << 4; + gdbserver_state.line_csum =3D hexchar_to_nibble(ch) << 4; gdbserver_state.state =3D RS_CHKSUM2; break; case RS_CHKSUM2: @@ -2277,7 +2278,7 @@ void gdb_read_byte(uint8_t ch) gdbserver_state.state =3D RS_GETLINE; break; } - gdbserver_state.line_csum |=3D fromhex(ch); + gdbserver_state.line_csum |=3D hexchar_to_nibble(ch); =20 if (gdbserver_state.line_csum !=3D (gdbserver_state.line_sum &= 0xff)) { trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_= sum, gdbserver_state.line_csum); diff --git a/debug/gdbstub/internals.h b/debug/gdbstub/internals.h index 5c0c725e54..4b67adfeda 100644 --- a/debug/gdbstub/internals.h +++ b/debug/gdbstub/internals.h @@ -75,32 +75,6 @@ typedef struct GDBState { /* lives in main gdbstub.c */ extern GDBState gdbserver_state; =20 -/* - * Inline utility function, convert from int to hex and back - */ - -static inline int fromhex(int v) -{ - if (v >=3D '0' && v <=3D '9') { - return v - '0'; - } else if (v >=3D 'A' && v <=3D 'F') { - return v - 'A' + 10; - } else if (v >=3D 'a' && v <=3D 'f') { - return v - 'a' + 10; - } else { - return 0; - } -} - -static inline int tohex(int v) -{ - if (v < 10) { - return v + '0'; - } else { - return v - 10 + 'a'; - } -} - /* * Connection helpers for both system and user backends */ diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 92c927a6a3..5ab1a4ffb0 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -267,4 +267,34 @@ void qemu_hexdump_line(char *line, unsigned int b, con= st void *bufptr, void qemu_hexdump(FILE *fp, const char *prefix, const void *bufptr, size_t size); =20 + +/** + * hexchar_to_nibble() - Converts hex character to nibble. + */ +static inline int hexchar_to_nibble(int v) +{ + if (v >=3D '0' && v <=3D '9') { + return v - '0'; + } else if (v >=3D 'A' && v <=3D 'F') { + return v - 'A' + 10; + } else if (v >=3D 'a' && v <=3D 'f') { + return v - 'a' + 10; + } else { + g_assert_not_reached(); + } +} + +/** + * nibble_to_hexchar() - Converts nibble to hex character. + */ +static inline int nibble_to_hexchar(int v) +{ + g_assert(v <=3D 0xf); + if (v < 10) { + return v + '0'; + } else { + return v - 10 + 'a'; + } +} + #endif --=20 2.34.1