From patchwork Sat Dec 21 22:41:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4012 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:998b:b0:5e7:b9eb:58e8 with SMTP id d11csp3205593mav; Sat, 21 Dec 2024 14:41:54 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCUPQtDkbAqO1lDDYxff8fJPyiXzz7ft0Mv4kWhqy6QqIgAhU1fgNq/XmDFmP5k12H4oSu182kIRMSs=@openvpn.net X-Google-Smtp-Source: AGHT+IG0ymZx9A1zW+kYiD3B8+jzeNbuyCzCzCQ9OWE1LTQV4hF1mKUBJSBis5oXq6Lmyjb5CmSl X-Received: by 2002:a05:6870:2dca:b0:29e:5f1f:152d with SMTP id 586e51a60fabf-2a7fb18704amr4321835fac.8.1734820913770; Sat, 21 Dec 2024 14:41:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1734820913; cv=none; d=google.com; s=arc-20240605; b=lzzZLlE0dhLUcpunzLHgros2aG+EaPx515eJeq2TjmQE7DBCjLPk094QmxB+KboTJR fNi823Ol5JKxyl05zTrssGEYvxBe1ozKcezrb++ujuH8Kt48N0YTrkwjenwwAc2tHK8n fv2/DcCZTU+CZgyHY5h5DZ9VPyp8c8a1QMQVeMLyTVgkNC2wvXXy8z58IhE776jzWJkH M9KB3F8/1uxVEVGscVKn/rEnkD5P7x8aWOB5gAUa5y1KFer1OLKAnXVjqThWDwC/OITb DHSudW3qPT6L3S3ul9ucviSgf2lZwWcP8dKqOpbsVld1AZkIBLsKcFRjSzkAoETvZpz9 jgLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature; bh=7V6Fw2xJQSpGRUlG5octLNnUDV04df3Xy0p2a+vdgTE=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=j9U/plALnMvTQDNfIFHZ1i+ppRQFASPoGQf4d8kRWwvuf7+75wS3LKqJU1INnPoWn7 +0TBm2RzjQ8e9LZP7vXSWZtlhxO2p/O4OfWo2T5wCXSHarVwwOPn1q0ppqRHLLvqSPpV /+oM5WiQYe4yOwFSSeqe3AV1FHoMXE0T/tFqlhP0G96pUuQ5C18s1+j3obwhz9Rvi7t+ pP8v2F8zfDsXgKEioX5oIKq768z2B8oSQ+8NpTWS+XRv3+VUcBZymQ9D6lq0q0AtLBW/ E8uvEjb//o4FkwCV91V8h+U6ebhGBEJGpPxhGpr4wC+e3aRii8hgQvazZ/1zSslINZPC ILfQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=e4PxIKhf; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=Q1BbrT3T; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 586e51a60fabf-2a7d7894ac4si3778412fac.269.2024.12.21.14.41.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 Dec 2024 14:41:53 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=e4PxIKhf; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=Q1BbrT3T; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1tP8AQ-0006kd-7s; Sat, 21 Dec 2024 22:41:50 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1tP8AO-0006kU-P0 for openvpn-devel@lists.sourceforge.net; Sat, 21 Dec 2024 22:41:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=D80OdUI6/iHyoWYB21C+JlIsysuMtTdHNrGNdRzO8q0=; b=e4PxIKhfcVAEALBrNgxHwMbvbG 9TfS6Tv9uYd1sVqBFBb94YLsoZse1K7nR+lRXs+qXv/XDJCqMv6S4FWTKRzXezORTrgLtvit6+DcJ brcNUg4pc+Jco+jvJ/wBBJwG/yB9QrTJaUcGOhhxBYx3fFdBqd995B8pYyrMy8kzRZk8=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=D80OdUI6/iHyoWYB21C+JlIsysuMtTdHNrGNdRzO8q0=; b=Q1BbrT3TkUOrk01JOJCIa26Kco GdO4NVx/dZLfba4RXdZjh4VnS2qFtlUCQfOvhV1bcLmcClQmeTdxfyLgg49jaXURjV6JKK9b8Pwf1 2zplbOqGNvWqyDqsQrbboeosoZGMDASu+gYbKxzorzlkYWskGCIYQ/CCbXz21fPJQJYg=; Received: from dhcp-174.greenie.muc.de ([193.149.48.174] helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1tP8AO-0006jR-H1 for openvpn-devel@lists.sourceforge.net; Sat, 21 Dec 2024 22:41:49 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.17.1.9/8.17.1.9) with ESMTP id 4BLMfb0W020994 for ; Sat, 21 Dec 2024 23:41:37 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.17.1.9/8.17.1.9/Submit) id 4BLMfbNI020993 for openvpn-devel@lists.sourceforge.net; Sat, 21 Dec 2024 23:41:37 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Sat, 21 Dec 2024 23:41:36 +0100 Message-ID: <20241221224136.20984-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 0.0 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Heiko Hund utf8to16_size() takes the size of the to be converted string. This is needed to convert MULTI_SZ strings, which contain inline NUL characters, but can be useful in other cases as well. Content analysis details: (0.0 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [193.149.48.174 listed in sa-accredit.habeas.com] 0.0 RCVD_IN_VALIDITY_RPBL_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [193.149.48.174 listed in bl.score.senderscore.com] X-Headers-End: 1tP8AO-0006jR-H1 Subject: [Openvpn-devel] [PATCH v2] service: add utf8to16 function that takes a size X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1818346068420090330?= X-GMAIL-MSGID: =?utf-8?q?1819091574884590754?= From: Heiko Hund utf8to16_size() takes the size of the to be converted string. This is needed to convert MULTI_SZ strings, which contain inline NUL characters, but can be useful in other cases as well. Change-Id: I6b4aa3d63c0b684bf95841271c04bc5d9c37793b Signed-off-by: Heiko Hund Acked-by: Gert Doering --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/836 This mail reflects revision 2 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering diff --git a/src/openvpnserv/common.c b/src/openvpnserv/common.c index 96bf800..61a7296 100644 --- a/src/openvpnserv/common.c +++ b/src/openvpnserv/common.c @@ -247,17 +247,16 @@ return error; } -/* Convert a utf8 string to utf16. Caller should free the result */ wchar_t * -utf8to16(const char *utf8) +utf8to16_size(const char *utf8, int size) { - int n = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); + int n = MultiByteToWideChar(CP_UTF8, 0, utf8, size, NULL, 0); wchar_t *utf16 = malloc(n * sizeof(wchar_t)); if (!utf16) { return NULL; } - MultiByteToWideChar(CP_UTF8, 0, utf8, -1, utf16, n); + MultiByteToWideChar(CP_UTF8, 0, utf8, size, utf16, n); return utf16; } diff --git a/src/openvpnserv/service.h b/src/openvpnserv/service.h index 6d0827d..6b559a5 100644 --- a/src/openvpnserv/service.h +++ b/src/openvpnserv/service.h @@ -89,8 +89,40 @@ DWORD MsgToEventLog(DWORD flags, LPCTSTR lpszMsg, ...); -/* Convert a utf8 string to utf16. Caller should free the result */ -wchar_t *utf8to16(const char *utf8); +/** + * Convert a UTF-8 string to UTF-16 + * + * The size parameter can be used to convert strings which contain inline NUL + * characters, like MULTI_SZ strings used as values in the registry do, + * or (sub)strings that are not zero terminated. If size is -1 the length + * of the string is determined automatically by the WIN32 API. Make sure + * you pass a terminated string or else bad things will happen. Note that + * the size you pass should always include the terminating zero as well. + * + * If the returned string is not NULL it must be freed by the caller. + * + * @param utf8 const string to be converted + * @param size the size of the string + * + * @return wchar_t* heap allocated result string + */ +wchar_t *utf8to16_size(const char *utf8, int size); + +/** + * Convert a zero terminated UTF-8 string to UTF-16 + * + * This is just a wrapper function that always passes -1 as string size + * to \ref utf8to16_size. + * + * @param utf8 const string to be converted + * + * @return wchar_t* heap allocated result string + */ +static inline wchar_t * +utf8to16(const char *utf8) +{ + return utf8to16_size(utf8, -1); +} /* return windows system directory as a pointer to a static string */ const wchar_t *get_win_sys_path(void);