From: Jan Janssen Date: Tue, 24 May 2022 08:14:35 +0000 (+0200) Subject: boot: Add strcpy8/16 X-Git-Tag: v252-rc1~892^2~9 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ef4d71ad7f0ecbc19efb0df7f1ef0f821da25fdc;p=thirdparty%2Fsystemd.git boot: Add strcpy8/16 --- diff --git a/src/boot/efi/efi-string.c b/src/boot/efi/efi-string.c index 17372ae9d44..4df705b30d3 100644 --- a/src/boot/efi/efi-string.c +++ b/src/boot/efi/efi-string.c @@ -98,3 +98,26 @@ int strcasecmp8(const char *s1, const char *s2) { int strcasecmp16(const char16_t *s1, const char16_t *s2) { return strncasecmp16(s1, s2, SIZE_MAX); } + +#define DEFINE_STRCPY(type, name) \ + type *name(type * restrict dest, const type * restrict src) { \ + assert(dest); \ + type *ret = dest; \ + \ + if (!src) { \ + *dest = '\0'; \ + return ret; \ + } \ + \ + while (*src) { \ + *dest = *src; \ + dest++; \ + src++; \ + } \ + \ + *dest = '\0'; \ + return ret; \ + } + +DEFINE_STRCPY(char, strcpy8); +DEFINE_STRCPY(char16_t, strcpy16); diff --git a/src/boot/efi/efi-string.h b/src/boot/efi/efi-string.h index 2b9a1dfb97f..d3c1786b47b 100644 --- a/src/boot/efi/efi-string.h +++ b/src/boot/efi/efi-string.h @@ -57,3 +57,6 @@ static inline bool strcaseeq8(const char *s1, const char *s2) { static inline bool strcaseeq16(const char16_t *s1, const char16_t *s2) { return strcasecmp16(s1, s2) == 0; } + +char *strcpy8(char * restrict dest, const char * restrict src); +char16_t *strcpy16(char16_t * restrict dest, const char16_t * restrict src); diff --git a/src/boot/efi/test-efi-string.c b/src/boot/efi/test-efi-string.c index 83cb6456886..384d5868bad 100644 --- a/src/boot/efi/test-efi-string.c +++ b/src/boot/efi/test-efi-string.c @@ -174,4 +174,30 @@ TEST(strncasecmp16) { assert_se(strncasecmp16((char16_t[]){ UINT16_MAX }, (char16_t[]){ 0 }, 1) > 0); } +TEST(strcpy8) { + char buf[128]; + + assert_se(strcpy8(buf, "123") == buf); + assert_se(streq8(buf, "123")); + assert_se(strcpy8(buf, "") == buf); + assert_se(streq8(buf, "")); + assert_se(strcpy8(buf, "A") == buf); + assert_se(streq8(buf, "A")); + assert_se(strcpy8(buf, NULL) == buf); + assert_se(streq8(buf, "")); +} + +TEST(strcpy16) { + char16_t buf[128]; + + assert_se(strcpy16(buf, u"123") == buf); + assert_se(streq16(buf, u"123")); + assert_se(strcpy16(buf, u"") == buf); + assert_se(streq16(buf, u"")); + assert_se(strcpy16(buf, u"A") == buf); + assert_se(streq16(buf, u"A")); + assert_se(strcpy16(buf, NULL) == buf); + assert_se(streq16(buf, u"")); +} + DEFINE_TEST_MAIN(LOG_INFO);