From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/11732 Path: news.gmane.org!.POSTED!not-for-mail From: Nathan McSween Newsgroups: gmane.linux.lib.musl.general Subject: [RFC PATCH 3/5] string: add strscpy and modify functions to use strscpy Date: Sat, 15 Jul 2017 19:55:39 +0000 Message-ID: <20170715195541.3136-4-nwmcsween@gmail.com> References: <20170715195541.3136-1-nwmcsween@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1500148719 31373 195.159.176.226 (15 Jul 2017 19:58:39 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 15 Jul 2017 19:58:39 +0000 (UTC) Cc: Nathan McSween To: musl@lists.openwall.com Original-X-From: musl-return-11746-gllmg-musl=m.gmane.org@lists.openwall.com Sat Jul 15 21:58:36 2017 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1dWTD1-00080M-Qp for gllmg-musl@m.gmane.org; Sat, 15 Jul 2017 21:58:35 +0200 Original-Received: (qmail 3505 invoked by uid 550); 15 Jul 2017 19:58:26 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 3327 invoked from network); 15 Jul 2017 19:58:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=EqBKpq4CVCqdj8Cp2H2vO8oTmW8akus0/2Yg5es1QV4=; b=M+x2ZsFRHyFkuxBT+K+P+RN9ge7KOmw3y4+YQXEN36vfoxdo35T+0tcexB4je4s63k EioMhilcddirrBSbgUOpMhxBGDABJAPLKJDlFozqx9i5aOMILl+OTmO4fAEph8ZV5bQy yYyo7s1Xm7w04+BLph7Wrdc+9Gb3IOjS2G/wiSo3MOtN82cBahpxaLUYDJiPH4I7ji+t +SdTI0XYOMlWqvwI0aK4/bkWknQ2Ed1UP9ELtcSkGv+O3zB+iUzeFl/gJJ1bpVNUiFm0 z2ojeHDO8E6pf4pFRbIXBqe+wIfF/IxIdtmLL0FVPUPB+OrWfe9Qo6L7amRbB17h8YeK 1U6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EqBKpq4CVCqdj8Cp2H2vO8oTmW8akus0/2Yg5es1QV4=; b=YcK6mq8rTi7v/inzz/yOobVlQw4acw+dsj4spv7p65fX/jnYv/lkKPqZCG2YCZoCK5 H9nNp1osU3j7borfSyYvFnA/iwWkvbzJYtcZqBnm2jNlG2TYOfjHbmPpEHZk52MakuXk sCaZ8FMKaR29w2jbW8jXTGJFKDlxytOESnXTYKBTnflqkNKDsvd/SdGt+2DYSvu7uj3X 2omoUKzRytChs088N3sIY8INNuoLp0npUQ2zYbvmWUez01kWTWJDuvz8YMUPVPmxVavB ryLD8V9IFlXi/Hles5FTKKIeRF6AhNm82iV7DIcP6ytNI6hEZX1duU2pAsrbkzxRlwLi 5zWg== X-Gm-Message-State: AIVw1116PgPpdJhV+9VfbQKk+EJnLgkkt5pli3f3d5namt3UKCcF0Yyd wsZHQcA13jp52JYw X-Received: by 10.98.2.149 with SMTP id 143mr11716383pfc.52.1500148692278; Sat, 15 Jul 2017 12:58:12 -0700 (PDT) X-Mailer: git-send-email 2.13.2 In-Reply-To: <20170715195541.3136-1-nwmcsween@gmail.com> Xref: news.gmane.org gmane.linux.lib.musl.general:11732 Archived-At: Text sizes w/ gcc 6.3.0 Before | After 309 | 37 strlcpy.lo 73 | 42 strncat.lo 240 | 85 stpncpy.lo N/A | 260 strscpy.lo 622 | 424 (TOTALS) strscpy is almost the same as strlcpy except it doesn't add strlen(src) to the result. --- src/string/stpncpy.c | 28 +++++++--------------------- src/string/strlcpy.c | 27 +++------------------------ src/string/strncat.c | 10 +++++----- src/string/strscpy.c | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 src/string/strscpy.c diff --git a/src/string/stpncpy.c b/src/string/stpncpy.c index 0b37da5d..6eaa4078 100644 --- a/src/string/stpncpy.c +++ b/src/string/stpncpy.c @@ -1,33 +1,19 @@ #include -#include -#define aliases __attribute__((__may_alias__)) -#define byte_repeat(x) ((size_t)~0 / 0xff * (x)) -#define word_has_zero(x) (((x) - byte_repeat(0x01)) & ~(x) & byte_repeat(0x80)) #define weak_alias(o, n) extern __typeof__(o) n __attribute__((weak, alias(#o))) +size_t __strscpy(char *, const char *, size_t); + char *__stpncpy(char *restrict d, const char *restrict s, size_t n) { - size_t *wd; - const size_t *ws; - - if (n < sizeof(size_t) * 3 || ((uintptr_t)d | (uintptr_t)s) - & sizeof(size_t) - 1) goto bytewise; - - for (; ((uintptr_t)s & sizeof(size_t) - 1) && (*d = *s); d++, s++, n--); - if (!*s) return memset(d, 0, n); + if (!n) return d; - wd = (void *)d; - ws = (const void *)s; - for (; n >= sizeof(size_t) && !word_has_zero(*ws) - ; n -= sizeof(size_t), wd++, ws++) *wd = *ws; - d = (void *)wd; - s = (const void *)ws; + size_t r = __strscpy(d, s, n) + 1; -bytewise: - for (; n && (*d = *s); d++, s++, n--); + if (s[r - 1]) d[r - 1] = s[r - 1]; + else memset(d + r, 0, n - r); - return memset(d, 0, n); + return d + r; } weak_alias(__stpncpy, stpncpy); diff --git a/src/string/strlcpy.c b/src/string/strlcpy.c index 193d7241..bb8ebb47 100644 --- a/src/string/strlcpy.c +++ b/src/string/strlcpy.c @@ -1,32 +1,11 @@ #define _BSD_SOURCE #include -#include -#include -#include "libc.h" -#define ALIGN (sizeof(size_t)-1) -#define ONES ((size_t)-1/UCHAR_MAX) -#define HIGHS (ONES * (UCHAR_MAX/2+1)) -#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) +size_t __strscpy(char *, const char *, size_t); size_t strlcpy(char *d, const char *s, size_t n) { - char *d0 = d; - size_t *wd; - const size_t *ws; + const size_t r = __strscpy(d, s, n); - if (!n--) goto finish; - if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { - for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); - if (n && *s) { - wd=(void *)d; ws=(const void *)s; - for (; n>=sizeof(size_t) && !HASZERO(*ws); - n-=sizeof(size_t), ws++, wd++) *wd = *ws; - d=(void *)wd; s=(const void *)ws; - } - } - for (; n && (*d=*s); n--, s++, d++); - *d = 0; -finish: - return d-d0 + strlen(s); + return r + strlen(s + r); } diff --git a/src/string/strncat.c b/src/string/strncat.c index 01ca2a23..f86dce8e 100644 --- a/src/string/strncat.c +++ b/src/string/strncat.c @@ -1,10 +1,10 @@ #include +size_t __strscpy(char *, const char *, size_t); + char *strncat(char *restrict d, const char *restrict s, size_t n) { - char *a = d; - d += strlen(d); - while (n && *s) n--, *d++ = *s++; - *d++ = 0; - return a; + __strscpy(d + strlen(d), s, n + 1); + + return d; } diff --git a/src/string/strscpy.c b/src/string/strscpy.c new file mode 100644 index 00000000..c2e25b7c --- /dev/null +++ b/src/string/strscpy.c @@ -0,0 +1,37 @@ +#include +#include + +#define hidden __attribute__((visibility("hidden"))) +#define aliases __attribute__((__may_alias__)) +#define byte_repeat(x) ((size_t)~0 / 0xff * (x)) +#define word_has_zero(x) (((x) - byte_repeat(0x01)) & ~(x) & byte_repeat(0x80)) + +hidden +size_t __strscpy(char *restrict d, const char *restrict s, size_t n) +{ + const char *const d0 = d; + size_t aliases *wd; + const size_t aliases *ws; + + if (!n--) return 0; + + if ( n < sizeof(size_t) * 3 || ((uintptr_t)d | (uintptr_t)s) + & sizeof(size_t) - 1) goto bytewise; + + for (; (uintptr_t)s & sizeof(size_t) - 1 && (*d = *s); d++, s++, n--); + if ((uintptr_t)s & sizeof(size_t) - 1) return d - d0; + + wd = (void *)d; + ws = (const void *)s; + for (; !word_has_zero(*ws) && n >= sizeof(size_t) + ; wd++, ws++, n -= sizeof(size_t)) *wd = *ws; + d = (void *)wd; + s = (const void *)ws; + +bytewise: + for (; n && (*d = *s); d++, s++, n--); + + *d = 0; + + return d - d0; +} -- 2.13.2