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