diff mbox series

[RESEND,v2,1/6] lib: add u16_strcpy/strdup functions

Message ID 20181214101043.14067-2-takahiro.akashi@linaro.org
State New
Headers show
Series subject: efi_loader: add HII database protocol | expand

Commit Message

AKASHI Takahiro Dec. 14, 2018, 10:10 a.m. UTC
From: "Akashi, Takahiro" <takahiro.akashi@linaro.org>

Add u16_strcpy() and u16_strdup(). The latter function will be
used later in implementing efi HII database protocol.

Signed-off-by: Akashi Takahiro <takahiro.akashi@linaro.org>
---
 include/charset.h | 23 +++++++++++++++++++++++
 lib/charset.c     | 29 +++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

Comments

Heinrich Schuchardt Dec. 14, 2018, 9:02 p.m. UTC | #1
On 12/14/18 11:10 AM, AKASHI Takahiro wrote:
> From: "Akashi, Takahiro" <takahiro.akashi@linaro.org>
> 
> Add u16_strcpy() and u16_strdup(). The latter function will be
> used later in implementing efi HII database protocol.
> 
> Signed-off-by: Akashi Takahiro <takahiro.akashi@linaro.org>

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

I just sent a further patch with the missing unit test.

Regards

Heinrich
diff mbox series

Patch

diff --git a/include/charset.h b/include/charset.h
index 4d45e246e515..65087f76d1fc 100644
--- a/include/charset.h
+++ b/include/charset.h
@@ -191,6 +191,29 @@  size_t u16_strlen(const u16 *in);
  */
 size_t u16_strnlen(const u16 *in, size_t count);
 
+/**
+ * u16_strcpy() - copy u16 string
+ *
+ * Copy u16 string pointed to by src, including terminating null word, to
+ * the buffer pointed to by dest.
+ *
+ * @dest:		destination buffer
+ * @src:		source buffer (null terminated)
+ * Return:		'dest' address
+ */
+u16 *u16_strcpy(u16 *dest, const u16 *src);
+
+/**
+ * u16_strdup() - duplicate u16 string
+ *
+ * Copy u16 string pointed to by src, including terminating null word, to a
+ * newly allocated buffer.
+ *
+ * @src:		source buffer (null terminated)
+ * Return:		allocated new buffer on success, NULL on failure
+ */
+u16 *u16_strdup(const u16 *src);
+
 /**
  * utf16_to_utf8() - Convert an utf16 string to utf8
  *
diff --git a/lib/charset.c b/lib/charset.c
index 10557b9e753d..5e349ed5ee45 100644
--- a/lib/charset.c
+++ b/lib/charset.c
@@ -349,6 +349,35 @@  size_t u16_strnlen(const u16 *in, size_t count)
 	return i;
 }
 
+u16 *u16_strcpy(u16 *dest, const u16 *src)
+{
+	u16 *tmp = dest;
+
+	for (;; dest++, src++) {
+		*dest = *src;
+		if (!*src)
+			break;
+	}
+
+	return tmp;
+}
+
+u16 *u16_strdup(const u16 *src)
+{
+	u16 *new;
+
+	if (!src)
+		return NULL;
+
+	new = malloc((u16_strlen(src) + 1) * sizeof(u16));
+	if (!new)
+		return NULL;
+
+	u16_strcpy(new, src);
+
+	return new;
+}
+
 /* Convert UTF-16 to UTF-8.  */
 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
 {