Home | History | Annotate | Line # | Download | only in string
      1  1.20  riastrad /*	$NetBSD: swab.c,v 1.20 2022/12/28 15:34:19 riastradh Exp $	*/
      2   1.4  christos 
      3  1.20  riastrad 
      4  1.20  riastrad /*-
      5  1.20  riastrad  * Copyright (c) 2022 The NetBSD Foundation, Inc.
      6  1.20  riastrad  * All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1       cgd  * modification, are permitted provided that the following conditions
     10   1.1       cgd  * are met:
     11   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1       cgd  *
     17  1.20  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.20  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.20  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.20  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.20  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.20  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.20  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.20  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.20  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.20  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.20  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     28   1.1       cgd  */
     29   1.1       cgd 
     30   1.4  christos #include <sys/cdefs.h>
     31   1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     32  1.20  riastrad __RCSID("$NetBSD: swab.c,v 1.20 2022/12/28 15:34:19 riastradh Exp $");
     33   1.4  christos #endif
     34   1.1       cgd 
     35   1.8     lukem #include <assert.h>
     36   1.5    kleink #include <unistd.h>
     37   1.1       cgd 
     38   1.1       cgd void
     39  1.20  riastrad swab(const void *__restrict src, void *__restrict dst, ssize_t nbytes)
     40   1.1       cgd {
     41  1.20  riastrad 	const char *p = src;
     42  1.20  riastrad 	char *q = dst;
     43  1.20  riastrad 	size_t n;
     44   1.8     lukem 
     45  1.20  riastrad 	_DIAGASSERT(src != NULL);
     46  1.20  riastrad 	_DIAGASSERT(dst != NULL);
     47  1.20  riastrad 
     48  1.20  riastrad 	if (nbytes <= 1)
     49  1.13  christos 		return;
     50  1.13  christos 
     51  1.20  riastrad 	/*
     52  1.20  riastrad 	 * Round nbytes/2 down; we ignore the last byte altogether if
     53  1.20  riastrad 	 * nbytes is odd.  This way, if the destination array is
     54  1.20  riastrad 	 * uninitialized, sanitizers will recognize the last byte as
     55  1.20  riastrad 	 * still uninitialized and flag it as undefined.
     56  1.20  riastrad 	 */
     57  1.20  riastrad 	for (n = nbytes/2; n --> 0; p += 2, q += 2) {
     58  1.20  riastrad 		/*
     59  1.20  riastrad 		 * According to POSIX (2018), the behaviour is
     60  1.20  riastrad 		 * undefined if src and dst overlap.  However, there
     61  1.20  riastrad 		 * are uses in-tree (xsrc/external/mit/xfwp/dist/io.c)
     62  1.20  riastrad 		 * that rely on swab(ptr, ptr, n) to do the swabbing
     63  1.20  riastrad 		 * in-place.  So make sure this works if src == dst.
     64  1.20  riastrad 		 */
     65  1.20  riastrad 		const char p0 = p[0], p1 = p[1];
     66  1.18    martin 
     67  1.20  riastrad 		q[0] = p1;
     68  1.20  riastrad 		q[1] = p0;
     69   1.1       cgd 	}
     70   1.1       cgd }
     71