swab.c revision 1.18.56.1 1 1.18.56.1 martin /* $NetBSD: swab.c,v 1.18.56.1 2023/02/22 18:47:00 martin Exp $ */
2 1.4 christos
3 1.18.56.1 martin
4 1.18.56.1 martin /*-
5 1.18.56.1 martin * Copyright (c) 2022 The NetBSD Foundation, Inc.
6 1.18.56.1 martin * 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.18.56.1 martin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.18.56.1 martin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.18.56.1 martin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.18.56.1 martin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.18.56.1 martin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.18.56.1 martin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.18.56.1 martin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.18.56.1 martin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.18.56.1 martin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.18.56.1 martin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.18.56.1 martin * 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.18.56.1 martin __RCSID("$NetBSD: swab.c,v 1.18.56.1 2023/02/22 18:47:00 martin 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.18.56.1 martin swab(const void *__restrict src, void *__restrict dst, ssize_t nbytes)
40 1.1 cgd {
41 1.18.56.1 martin const char *p = src;
42 1.18.56.1 martin char *q = dst;
43 1.18.56.1 martin size_t n;
44 1.13 christos
45 1.18.56.1 martin _DIAGASSERT(src != NULL);
46 1.18.56.1 martin _DIAGASSERT(dst != NULL);
47 1.1 cgd
48 1.18.56.1 martin if (nbytes <= 1)
49 1.18 martin return;
50 1.18 martin
51 1.18.56.1 martin /*
52 1.18.56.1 martin * Round nbytes/2 down; we ignore the last byte altogether if
53 1.18.56.1 martin * nbytes is odd. This way, if the destination array is
54 1.18.56.1 martin * uninitialized, sanitizers will recognize the last byte as
55 1.18.56.1 martin * still uninitialized and flag it as undefined.
56 1.18.56.1 martin */
57 1.18.56.1 martin for (n = nbytes/2; n --> 0; p += 2, q += 2) {
58 1.18.56.1 martin /*
59 1.18.56.1 martin * According to POSIX (2018), the behaviour is
60 1.18.56.1 martin * undefined if src and dst overlap. However, there
61 1.18.56.1 martin * are uses in-tree (xsrc/external/mit/xfwp/dist/io.c)
62 1.18.56.1 martin * that rely on swab(ptr, ptr, n) to do the swabbing
63 1.18.56.1 martin * in-place. So make sure this works if src == dst.
64 1.18.56.1 martin */
65 1.18.56.1 martin const char p0 = p[0], p1 = p[1];
66 1.18.56.1 martin
67 1.18.56.1 martin q[0] = p1;
68 1.18.56.1 martin q[1] = p0;
69 1.1 cgd }
70 1.1 cgd }
71