diff options
author | 2014-12-11 23:05:38 +0000 | |
---|---|---|
committer | 2014-12-11 23:05:38 +0000 | |
commit | 0a2869cb84f502f2092fa3251feedea723042c8f (patch) | |
tree | 6ea9ed90e57d10194b2ac529aef90bc6e60235e7 /lib/libc/string/swab.c | |
parent | sync (diff) | |
download | wireguard-openbsd-0a2869cb84f502f2092fa3251feedea723042c8f.tar.xz wireguard-openbsd-0a2869cb84f502f2092fa3251feedea723042c8f.zip |
update swab() to match the current posix definition. "rationale: none."
rewrite the function to be simpler as well. the compiler can unroll the
loop for us if necessary.
ok schwarze
Diffstat (limited to 'lib/libc/string/swab.c')
-rw-r--r-- | lib/libc/string/swab.c | 74 |
1 files changed, 24 insertions, 50 deletions
diff --git a/lib/libc/string/swab.c b/lib/libc/string/swab.c index b74db7e62a5..c7d7d72ce09 100644 --- a/lib/libc/string/swab.c +++ b/lib/libc/string/swab.c @@ -1,61 +1,35 @@ -/* $OpenBSD: swab.c,v 1.8 2008/03/15 21:54:09 ray Exp $ */ +/* $OpenBSD: swab.c,v 1.9 2014/12/11 23:05:38 tedu Exp $ */ /* - * Copyright (c) 1988 Regents of the University of California. - * All rights reserved. + * Copyright (c) 2014 Ted Unangst <tedu@openbsd.org> * - * This code is derived from software contributed to Berkeley by - * Jeffrey Mogul. + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ - #include <unistd.h> void -swab(const void *from, void *to, size_t len) +swab(const void *__restrict from, void *__restrict to, ssize_t len) { - size_t n; - char *fp, *tp; - char temp; + const unsigned char *src = from; + unsigned char *dst = to; + unsigned char t0, t1; - n = (len / 2) + 1; - fp = (char *)from; - tp = (char *)to; -#define STEP do { \ - temp = *fp++; \ - *tp++ = *fp++; \ - *tp++ = temp; \ -} while (0) - /* round to multiple of 8 */ - while ((--n) & 07) - STEP; - n >>= 3; - if (n == 0) - return; - while (n-- != 0) { - STEP; STEP; STEP; STEP; - STEP; STEP; STEP; STEP; + while (len > 1) { + t0 = src[0]; + t1 = src[1]; + dst[0] = t1; + dst[1] = t0; + src += 2; + dst += 2; + len -= 2; } } |