Commit 2a3537ae2272d9699312a0855a7b72472d1719c5

Authored by Akashi, Takahiro
Committed by Alexander Graf
1 parent 2859f446b0

lib: add u16_strcpy/strdup functions

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>
Signed-off-by: Alexander Graf <agraf@suse.de>

Showing 2 changed files with 52 additions and 0 deletions Side-by-side Diff

... ... @@ -192,6 +192,29 @@
192 192 size_t u16_strnlen(const u16 *in, size_t count);
193 193  
194 194 /**
  195 + * u16_strcpy() - copy u16 string
  196 + *
  197 + * Copy u16 string pointed to by src, including terminating null word, to
  198 + * the buffer pointed to by dest.
  199 + *
  200 + * @dest: destination buffer
  201 + * @src: source buffer (null terminated)
  202 + * Return: 'dest' address
  203 + */
  204 +u16 *u16_strcpy(u16 *dest, const u16 *src);
  205 +
  206 +/**
  207 + * u16_strdup() - duplicate u16 string
  208 + *
  209 + * Copy u16 string pointed to by src, including terminating null word, to a
  210 + * newly allocated buffer.
  211 + *
  212 + * @src: source buffer (null terminated)
  213 + * Return: allocated new buffer on success, NULL on failure
  214 + */
  215 +u16 *u16_strdup(const u16 *src);
  216 +
  217 +/**
195 218 * utf16_to_utf8() - Convert an utf16 string to utf8
196 219 *
197 220 * Converts 'size' characters of the utf16 string 'src' to utf8
... ... @@ -349,6 +349,35 @@
349 349 return i;
350 350 }
351 351  
  352 +u16 *u16_strcpy(u16 *dest, const u16 *src)
  353 +{
  354 + u16 *tmp = dest;
  355 +
  356 + for (;; dest++, src++) {
  357 + *dest = *src;
  358 + if (!*src)
  359 + break;
  360 + }
  361 +
  362 + return tmp;
  363 +}
  364 +
  365 +u16 *u16_strdup(const u16 *src)
  366 +{
  367 + u16 *new;
  368 +
  369 + if (!src)
  370 + return NULL;
  371 +
  372 + new = malloc((u16_strlen(src) + 1) * sizeof(u16));
  373 + if (!new)
  374 + return NULL;
  375 +
  376 + u16_strcpy(new, src);
  377 +
  378 + return new;
  379 +}
  380 +
352 381 /* Convert UTF-16 to UTF-8. */
353 382 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
354 383 {