From: Nathan McSween <nwmcsween@gmail.com>
To: musl@lists.openwall.com
Cc: Nathan McSween <nwmcsween@gmail.com>
Subject: [PATCH 4/4] String: refactor to utilize word.h and always terminate string
Date: Mon, 4 Feb 2013 00:12:15 +0000 [thread overview]
Message-ID: <1359936735-31915-5-git-send-email-nwmcsween@gmail.com> (raw)
In-Reply-To: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com>
---
src/string/strlcpy.c | 56 ++++++++++++++++++++++++++++++----------------------
1 file changed, 32 insertions(+), 24 deletions(-)
diff --git a/src/string/strlcpy.c b/src/string/strlcpy.c
index 4d3ff92..bc26441 100644
--- a/src/string/strlcpy.c
+++ b/src/string/strlcpy.c
@@ -1,32 +1,40 @@
-#include <string.h>
-#include <stdlib.h>
+#include <stddef.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)
+#include <string.h>
+#include "word.h"
+/**
+ * strlcpy - Word sized bsd strlcpy.
+ * @d: Destination
+ * @s: Source
+ * @n: Max @s
+ */
size_t strlcpy(char *d, const char *s, size_t n)
{
- char *d0 = d;
+ char *z = d;
size_t *wd;
const size_t *ws;
- 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);
+ /* A byte for nul */
+ if (!n--) goto terminate;
+
+ if ((uintptr_t)d % sizeof(size_t) != (uintptr_t)s % sizeof(size_t))
+ goto misaligned;
+
+ for (; (uintptr_t)s % sizeof(size_t); *d++ = *s++, n--)
+ if (!*s || !n) goto terminate;
+
+ for (wd = (size_t *)d, ws= (const size_t *)s
+ ; !word_has_zero(*ws) && n >= sizeof(size_t)
+ ; *wd = *ws, wd++, ws++, n -= sizeof(size_t))
+
+ d = (char *)wd;
+ s = (const char *)ws;
+
+misaligned:
+ for (; (*d = *s) && n; d++, s++, n--);
+terminate:
+ *d = '\0';
+
+ return d - z + strlen(s);
}
--
1.7.11.4
next prev parent reply other threads:[~2013-02-04 0:12 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-02-04 0:12 [PATCH 0/4] Refactor and expand string functions Nathan McSween
2013-02-04 0:12 ` [PATCH 1/4] Internal: Add word.h - word-at-a-time fns / macros Nathan McSween
2013-02-04 0:12 ` [PATCH 2/4] String: refactor to utilize word.h and optimize Nathan McSween
2013-02-04 0:12 ` [PATCH 3/4] String: expand to word-at-a-time Nathan McSween
2013-02-04 2:24 ` Isaac Dunham
2013-02-04 2:55 ` nwmcsween
2013-02-04 0:12 ` Nathan McSween [this message]
2013-02-05 4:25 ` [PATCH 0/4] Refactor and expand string functions Nathan McSween
2013-02-05 11:19 ` Szabolcs Nagy
2013-02-05 14:05 ` Rich Felker
2013-02-05 15:05 ` Szabolcs Nagy
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=1359936735-31915-5-git-send-email-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).