xref: /freebsd/contrib/file/src/swap.c (revision 7af41682a96bf7058b82665c33bb9b1bfa079c17)
1 /*
2  * Copyright (c) Ian F. Darwin 1986-1995.
3  * Software written by Ian F. Darwin and others;
4  * maintained 1995-present by Christos Zoulas and others.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice immediately at the beginning of the file, without modification,
11  *    this list of conditions, and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * apprentice - make one pass through /etc/magic, learning its secrets.
30  */
31 
32 #include "file.h"
33 
34 #ifndef	lint
35 FILE_RCSID("@(#)$File: swap.c,v 1.1 2026/04/19 19:56:49 christos Exp $")
36 #endif	/* lint */
37 
38 #include "swap.h"
39 
40 #if !defined(HAVE_BYTESWAP_H) && !defined(HAVE_SYS_BSWAP_H)
41 /*
42  * swap a short
43  */
44 file_protected uint16_t
file_swap2(uint16_t sv)45 file_swap2(uint16_t sv)
46 {
47 	uint16_t rv;
48 	uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
49 	uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
50 	d[0] = s[1];
51 	d[1] = s[0];
52 	return rv;
53 }
54 
55 /*
56  * swap an int
57  */
58 file_protected uint32_t
file_swap4(uint32_t sv)59 file_swap4(uint32_t sv)
60 {
61 	uint32_t rv;
62 	uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
63 	uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
64 	d[0] = s[3];
65 	d[1] = s[2];
66 	d[2] = s[1];
67 	d[3] = s[0];
68 	return rv;
69 }
70 
71 /*
72  * swap a quad
73  */
74 file_protected uint64_t
file_swap8(uint64_t sv)75 file_swap8(uint64_t sv)
76 {
77 	uint64_t rv;
78 	uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
79 	uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
80 # if 0
81 	d[0] = s[3];
82 	d[1] = s[2];
83 	d[2] = s[1];
84 	d[3] = s[0];
85 	d[4] = s[7];
86 	d[5] = s[6];
87 	d[6] = s[5];
88 	d[7] = s[4];
89 # else
90 	d[0] = s[7];
91 	d[1] = s[6];
92 	d[2] = s[5];
93 	d[3] = s[4];
94 	d[4] = s[3];
95 	d[5] = s[2];
96 	d[6] = s[1];
97 	d[7] = s[0];
98 # endif
99 	return rv;
100 }
101 #endif
102