Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20170715195541.3136-4-nwmcsween@gmail.com>
Date: Sat, 15 Jul 2017 19:55:39 +0000
From: Nathan McSween <nwmcsween@...il.com>
To: musl@...ts.openwall.com
Cc: Nathan McSween <nwmcsween@...il.com>
Subject: [RFC PATCH 3/5] string: add strscpy and modify functions to use strscpy

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.