1 /*
2  * Copyright (c) 1998, 1999, 2000, 2001 X-Way Rights BV
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19 
20 /*!\file endianness.c
21  * \brief Endian-dependant encoding/decoding.
22  * \author Bob Deblier <bob.deblier@telenet.be>
23  */
24 
25 #define BEECRYPT_DLL_EXPORT
26 
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #if HAVE_ENDIAN_H && HAVE_ASM_BYTEORDER_H
32 # include <endian.h>
33 #endif
34 
35 #include "beecrypt/endianness.h"
36 
37 #undef swap16
38 #undef swapu16
39 #undef swap32
40 #undef swapu32
41 #undef swap64
42 #undef swapu64
43 
swap16(int16_t n)44 int16_t swap16(int16_t n)
45 {
46 	return (    ((n & 0xff) << 8) |
47 				((n & 0xff00) >> 8) );
48 }
49 
swapu16(uint16_t n)50 uint16_t swapu16(uint16_t n)
51 {
52 	return (    ((n & 0xffU) << 8) |
53 				((n & 0xff00U) >> 8) );
54 }
55 
swap32(int32_t n)56 int32_t swap32(int32_t n)
57 {
58 	return (    ((n & 0xff) << 24) |
59 				((n & 0xff00) << 8) |
60 				((n & 0xff0000) >> 8) |
61 				((n & 0xff000000) >> 24) );
62 }
63 
swapu32(uint32_t n)64 uint32_t swapu32(uint32_t n)
65 {
66 	return (    ((n & 0xffU) << 24) |
67 				((n & 0xff00U) << 8) |
68 				((n & 0xff0000U) >> 8) |
69 				((n & 0xff000000U) >> 24) );
70 }
71 
swap64(int64_t n)72 int64_t swap64(int64_t n)
73 {
74 	return (    ((n & (((int64_t) 0xff)      )) << 56) |
75 				((n & (((int64_t) 0xff) <<  8)) << 40) |
76 				((n & (((int64_t) 0xff) << 16)) << 24) |
77 				((n & (((int64_t) 0xff) << 24)) <<  8) |
78 				((n & (((int64_t) 0xff) << 32)) >>  8) |
79 				((n & (((int64_t) 0xff) << 40)) >> 24) |
80 				((n & (((int64_t) 0xff) << 48)) >> 40) |
81 				((n & (((int64_t) 0xff) << 56)) >> 56) );
82 }
83 
swapu64(uint64_t n)84 uint64_t swapu64(uint64_t n)
85 {
86 	return (    ((n & (((uint64_t) 0xff)      )) << 56) |
87 				((n & (((uint64_t) 0xff) <<  8)) << 40) |
88 				((n & (((uint64_t) 0xff) << 16)) << 24) |
89 				((n & (((uint64_t) 0xff) << 24)) <<  8) |
90 				((n & (((uint64_t) 0xff) << 32)) >>  8) |
91 				((n & (((uint64_t) 0xff) << 40)) >> 24) |
92 				((n & (((uint64_t) 0xff) << 48)) >> 40) |
93 				((n & (((uint64_t) 0xff) << 56)) >> 56) );
94 }
95