From patchwork Thu Apr 28 08:09:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahisa Kojima X-Patchwork-Id: 567126 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:6886:0:0:0:0 with SMTP id m6csp5232216map; Thu, 28 Apr 2022 01:11:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwW8hu8Cv44QMqcklgjbM/ioI8lVQGR92062++9pGppyxZP+OzxFTYvIQn8BjrrenKYKYwn X-Received: by 2002:a17:907:3f86:b0:6df:ad43:583 with SMTP id hr6-20020a1709073f8600b006dfad430583mr31554271ejc.535.1651133462119; Thu, 28 Apr 2022 01:11:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651133462; cv=none; d=google.com; s=arc-20160816; b=ixY2eFcQytbrDz74wJY0Apu6c/w+Ag1ymMYOt06uZuQNugsRHJIf7wZrJF67Bm+6p3 dc3mM/Obk87KZID3qjiHzm9Vt5aNl54hhs1bdGk85nSra2FBLenCLS5cQbd9I6o8VESs BHVqXcQeEgi4dpiqs6kFtGLRIP6wpGxZAdInU1CB7HeiyB6JILM/34m0bQPCmc4tbrlq nFlfv9dJ/OHwThRqfG2B/ws8lQe++E7o4SdrX9wktZzXz5cllt1NRMIZ+Ha83ZeYs/ro SABiuNaeB9ByBUtvQaIM4Wu0AXoYEXHQPfe5sEA8JnmJiPogJPXCs/JaTFv+Gq0DZDZX Dg0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=B6ZTRPfwFqK5oz2mCyJUqTqHSpVyXj5iJbffRrIjLy8=; b=0L/9j22I0mVnL1HeCg8ZrFm5Y1ISLGwrrO8v/9+wu7OUuMKNmCWGUpCmv2enCFcvS9 9JD/9EEbcNJU1bF0ITy+wPdn/e/L8DAOmv4aFMXYSRVoxE496HWf3R8BMh9UTN4fv5/H rh65DLYXLd36s2Hwg+UcoHvnFkSdj2aA0GYV0G6ZbzQHReQeRDlsjuefREV4LXQKtgPb DjrCzvhbLt4CxqpYSpBNqfMDfCK2PWFGXHzQiXCsuTjNVqKjTUnKVeG0ZGI2CDTUsw8K NOctspSiuF16OVrf67IX1FyKp/zMRFdyNCtB5z4vtaV6IoqHrZYH6aOXq+eHfTPFkKjP HhIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="gZ7Cy/cD"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id g1-20020a1709061e0100b006e83fe14ac1si3325735ejj.554.2022.04.28.01.11.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:11:02 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="gZ7Cy/cD"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B19DA807E1; Thu, 28 Apr 2022 10:10:52 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="gZ7Cy/cD"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4567780818; Thu, 28 Apr 2022 10:10:44 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A0CAF80818 for ; Thu, 28 Apr 2022 10:10:40 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=masahisa.kojima@linaro.org Received: by mail-pj1-x1034.google.com with SMTP id cu23-20020a17090afa9700b001d98d8e53b7so5571900pjb.0 for ; Thu, 28 Apr 2022 01:10:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=B6ZTRPfwFqK5oz2mCyJUqTqHSpVyXj5iJbffRrIjLy8=; b=gZ7Cy/cD+4Us9BdZYp2LIoO7Srr8/s54D/2DAVZFMSZsMtc5QHzQzhhjlfV6DXic59 PiNowowOWd1m+P2YPPa/Lz2fEgKmGi+FvlSjdOZTvaD7rrldxm7KLXYl8Wa6Er4uk7Fo olfNT/FMuXdE0fWlX2oa+YwJGot4Wbtv51z6gmdFf7VOsFF8ykGBylAgeLwDRjkm6f9n 6ex3EIbTI4SpgIlLmsbuRTYUNRxcscN461BFxFWTIxuwe4pHOOjDzeaNfHTg1SdcGC2M 6ZJYw9pYQlpzRsYD8wlUinG/rDOE8Anc0lgvZQklDVXIRs6BCUHrMuxYtHbUyB2F/Er1 1GEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=B6ZTRPfwFqK5oz2mCyJUqTqHSpVyXj5iJbffRrIjLy8=; b=j0gbs1kAsrzLPARkCQx+nhgUYQJCPk1E0dvTyEMOHv+1/eIdEQuZwbYLHP4ntqnps2 SOebIqd+Q8urQ2kphUzkF1WWIe58OPdRBaHfFOnQKJThJzDZBsYHsf7AqA6+yoG+lkVi pXaDPFAG4lIq92DPgvIG1DfUwdZPLpxSORRnFsAxgL7SHET4EovhlYPFdH2uFMcHoHgA 4Xw3EVhFW2xAczOFzEsSrFrd9MFAs7+/VTvew9r7jqqWVdP8aobXMWUVy8VKZbmYtb/Z 3A7X2chqkNjTTUa16SFeW0bvVTfLDFoPU4UAT1P+gak7e1+4NmNyzcCxX6onEwS4w3VJ UDjQ== X-Gm-Message-State: AOAM532nwPHiOz83LLuGKoOw9odpoX4/M7FxUlJgNonezSwKp0UokOxL PoK4UciMCkENwCS8042Vmmr3umUkU/Gr3A== X-Received: by 2002:a17:90a:94c2:b0:1d9:3fbd:bbe1 with SMTP id j2-20020a17090a94c200b001d93fbdbbe1mr28248074pjw.59.1651133438546; Thu, 28 Apr 2022 01:10:38 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:cf7:5800:82fa:5bff:fe4b:26b1]) by smtp.gmail.com with ESMTPSA id b15-20020a17090a7acf00b001cd4989ff5fsm5684259pjl.38.2022.04.28.01.10.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Apr 2022 01:10:38 -0700 (PDT) From: Masahisa Kojima To: u-boot@lists.denx.de Cc: Heinrich Schuchardt , Ilias Apalodimas , Simon Glass , Takahiro Akashi , Francois Ozog , Mark Kettenis , Masahisa Kojima Subject: [PATCH v5 01/17] lib/charset: add u16_strlcat() function Date: Thu, 28 Apr 2022 17:09:34 +0900 Message-Id: <20220428080950.23509-2-masahisa.kojima@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220428080950.23509-1-masahisa.kojima@linaro.org> References: <20220428080950.23509-1-masahisa.kojima@linaro.org> X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean Provide u16 string version of strlcat(). Signed-off-by: Masahisa Kojima --- Changes in v5: - change 3rd argument from size to count, it indicates the maximum u16 string count that dest buffer can have. Other u16_strXXX functions in U-Boot use string count, not the buffer size. u16_strlcat() should follow. - update function comment to clealy describe the behavior - use strlen() instead of strnlen(), to correctly handle the case if the count is smaller than or equal to initial dest count Changes in v4: - add blank line above the return statement Changes in v2: - implement u16_strlcat(with the destination buffer size in argument) instead of u16_strcat include/charset.h | 17 +++++++++++++++++ lib/charset.c | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/charset.h b/include/charset.h index 38908e08f0..ae356ad9a5 100644 --- a/include/charset.h +++ b/include/charset.h @@ -261,6 +261,23 @@ u16 *u16_strcpy(u16 *dest, const u16 *src); */ u16 *u16_strdup(const void *src); +/** + * u16_strlcat() - Append a length-limited, %NUL-terminated string to another + * + * Append the src string to the dest string, overwriting the terminating + * null word at the end of dest, and then adds a terminating null word. + * + * @dest: destination buffer (must include the trailing 0x0000) + * @src: source buffer (must include the trailing 0x0000) + * @count: maximum number of code points that dest buffer can have, + * including the trailing 0x0000 + * Return: total count of the created u16 string + * u16_strlen(src) + min(count, u16_strlen(initial dest)), + * does not include trailing 0x0000. + * If return value >= count, truncation occurred. + */ +size_t u16_strlcat(u16 *dest, const u16 *src, size_t size); + /** * utf16_to_utf8() - Convert an utf16 string to utf8 * diff --git a/lib/charset.c b/lib/charset.c index de201cf3b9..f295dfc11a 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -416,6 +416,28 @@ u16 *u16_strdup(const void *src) return new; } +size_t u16_strlcat(u16 *dest, const u16 *src, size_t count) +{ + size_t destlen, srclen, ret; + + destlen = u16_strlen(dest); + srclen = u16_strlen(src); + ret = min(count, destlen) + srclen; /* does not include trailing 0x0000 */ + + if (destlen >= count) + return ret; + + dest += destlen; + count -= destlen; + if (srclen >= count) + srclen = count - 1 /* for trailing 0x0000 */; + + memcpy(dest, src, srclen * sizeof(u16)); + dest[srclen] = u'\0'; + + return ret; +} + /* Convert UTF-16 to UTF-8. */ uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size) {