From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/2733 Path: news.gmane.org!not-for-mail From: Nathan McSween Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH 4/4] String: refactor to utilize word.h and always terminate string Date: Mon, 4 Feb 2013 00:12:15 +0000 Message-ID: <1359936735-31915-5-git-send-email-nwmcsween@gmail.com> References: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1359936775 26606 80.91.229.3 (4 Feb 2013 00:12:55 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 4 Feb 2013 00:12:55 +0000 (UTC) Cc: Nathan McSween To: musl@lists.openwall.com Original-X-From: musl-return-2734-gllmg-musl=m.gmane.org@lists.openwall.com Mon Feb 04 01:13:16 2013 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1U29gS-00073T-0n for gllmg-musl@plane.gmane.org; Mon, 04 Feb 2013 01:13:16 +0100 Original-Received: (qmail 1450 invoked by uid 550); 4 Feb 2013 00:12:57 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 1442 invoked from network); 4 Feb 2013 00:12:57 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=KO4BAMiMy3pF609lybJoSuEApbs2NLxNI3FTq5ayuMM=; b=H4JYbBTIwx36AtN9hPVNcI37awDWkIpbHrVD5neV/7eXHDjRj/QV9h/LNFw3vl4f2U 0vS9aYDC4A/7MAcauDXyaMofpIhhAjcNxirwLYU2kFqwzqtrRMGFYZR3yLaUqIQ64U+M wI7y0IUj3r3JEvXq+du+4H4uS+N6gAoRQiOjmvGRAuJwNy7mdqtzGU0TQdpBuYm+yQJP wZJALkXPtT8sGki1B3KO4zNajEoOTri4TBfb4HmsoG5cSXsKdEX7EvxcChN2ArnL3uWq d4YnwBSlUhZiGCxoj4RCfvt7rZhAcTGnpAVIKnXTemx2AX0BF4yeAZMMSN/xt4VwPDk/ amfQ== X-Received: by 10.68.136.132 with SMTP id qa4mr49517166pbb.166.1359936765349; Sun, 03 Feb 2013 16:12:45 -0800 (PST) X-Mailer: git-send-email 1.8.1.2 In-Reply-To: <1359936735-31915-1-git-send-email-nwmcsween@gmail.com> Xref: news.gmane.org gmane.linux.lib.musl.general:2733 Archived-At: --- 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 -#include +#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) +#include +#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