From: Nathan McSween <nwmcsween@gmail.com>
To: musl@lists.openwall.com
Cc: Nathan McSween <nwmcsween@gmail.com>
Subject: [RFC PATCH 3/5] string: add strscpy and modify functions to use strscpy
Date: Sat, 15 Jul 2017 19:55:39 +0000 [thread overview]
Message-ID: <20170715195541.3136-4-nwmcsween@gmail.com> (raw)
In-Reply-To: <20170715195541.3136-1-nwmcsween@gmail.com>
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 <string.h>
-#include <stdint.h>
-#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 <string.h>
-#include <stdint.h>
-#include <limits.h>
-#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 <string.h>
+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 <stddef.h>
+#include <stdint.h>
+
+#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
next prev parent reply other threads:[~2017-07-15 19:55 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-15 19:55 [RFC PATCH 0/5] Add explicit_bzero, vectorize and 'normalize' various string functions Nathan McSween
2017-07-15 19:55 ` [RFC PATCH 1/5] string: vectorize various functions Nathan McSween
2017-07-15 19:55 ` [RFC PATCH 2/5] string: modify wordwise functions to match new style Nathan McSween
2017-07-15 19:55 ` Nathan McSween [this message]
2017-07-15 19:55 ` [RFC PATCH 4/5] string: use strchrnul in strcasestr instead of bytewise iteration Nathan McSween
2017-07-15 19:55 ` [RFC PATCH 5/5] string: add memsset a 'secure' memset and bsd explicit_bzero Nathan McSween
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170715195541.3136-4-nwmcsween@gmail.com \
--to=nwmcsween@gmail.com \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).