dd_swab.c revision 1.1.4.2 1 1.1.4.2 martin /* $NetBSD: dd_swab.c,v 1.1.4.2 2020/04/13 07:45:05 martin Exp $ */
2 1.1.4.2 martin
3 1.1.4.2 martin /*
4 1.1.4.2 martin * Copyright (c) 1988, 1993
5 1.1.4.2 martin * The Regents of the University of California. All rights reserved.
6 1.1.4.2 martin *
7 1.1.4.2 martin * This code is derived from software contributed to Berkeley by
8 1.1.4.2 martin * Jeffrey Mogul.
9 1.1.4.2 martin *
10 1.1.4.2 martin * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 martin * modification, are permitted provided that the following conditions
12 1.1.4.2 martin * are met:
13 1.1.4.2 martin * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 martin * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 martin * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 martin * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 martin * documentation and/or other materials provided with the distribution.
18 1.1.4.2 martin * 3. Neither the name of the University nor the names of its contributors
19 1.1.4.2 martin * may be used to endorse or promote products derived from this software
20 1.1.4.2 martin * without specific prior written permission.
21 1.1.4.2 martin *
22 1.1.4.2 martin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1.4.2 martin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1.4.2 martin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1.4.2 martin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1.4.2 martin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1.4.2 martin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1.4.2 martin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1.4.2 martin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1.4.2 martin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1.4.2 martin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1.4.2 martin * SUCH DAMAGE.
33 1.1.4.2 martin *
34 1.1.4.2 martin * from: NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp
35 1.1.4.2 martin */
36 1.1.4.2 martin
37 1.1.4.2 martin /* This copy has no restrict on it, that dd wants */
38 1.1.4.2 martin
39 1.1.4.2 martin #include <sys/cdefs.h>
40 1.1.4.2 martin #if defined(LIBC_SCCS) && !defined(lint)
41 1.1.4.2 martin #if 0
42 1.1.4.2 martin static char sccsid[] = "@(#)swab.c 8.1 (Berkeley) 6/4/93";
43 1.1.4.2 martin #else
44 1.1.4.2 martin __RCSID("$NetBSD: dd_swab.c,v 1.1.4.2 2020/04/13 07:45:05 martin Exp $");
45 1.1.4.2 martin #endif
46 1.1.4.2 martin #endif /* LIBC_SCCS and not lint */
47 1.1.4.2 martin
48 1.1.4.2 martin #include <sys/types.h>
49 1.1.4.2 martin
50 1.1.4.2 martin #include "dd.h"
51 1.1.4.2 martin #include "extern.h"
52 1.1.4.2 martin
53 1.1.4.2 martin void
54 1.1.4.2 martin dd_swab(const void * from, void * to, ssize_t len)
55 1.1.4.2 martin {
56 1.1.4.2 martin char temp;
57 1.1.4.2 martin const char *fp;
58 1.1.4.2 martin char *tp;
59 1.1.4.2 martin
60 1.1.4.2 martin if (len <= 1)
61 1.1.4.2 martin return;
62 1.1.4.2 martin
63 1.1.4.2 martin len /= 2;
64 1.1.4.2 martin fp = (const char *)from;
65 1.1.4.2 martin tp = (char *)to;
66 1.1.4.2 martin #define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp
67 1.1.4.2 martin
68 1.1.4.2 martin if (__predict_false(len == 1)) {
69 1.1.4.2 martin STEP;
70 1.1.4.2 martin return;
71 1.1.4.2 martin }
72 1.1.4.2 martin
73 1.1.4.2 martin /* round to multiple of 8 */
74 1.1.4.2 martin while ((--len % 8) != 0)
75 1.1.4.2 martin STEP;
76 1.1.4.2 martin len /= 8;
77 1.1.4.2 martin if (len == 0)
78 1.1.4.2 martin return;
79 1.1.4.2 martin while (len-- != 0) {
80 1.1.4.2 martin STEP; STEP; STEP; STEP;
81 1.1.4.2 martin STEP; STEP; STEP; STEP;
82 1.1.4.2 martin }
83 1.1.4.2 martin }
84