Botan
3.8.1
Crypto and TLS for C&
Toggle main menu visibility
Main Page
Related Pages
Topics
Namespaces
Namespace List
Namespace Members
All
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
x
z
Functions
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
x
z
Variables
Typedefs
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
r
s
t
u
v
w
x
Enumerations
a
c
d
e
f
g
h
k
l
m
n
o
p
r
s
t
u
v
w
Enumerator
c
d
f
i
m
n
r
s
t
Concepts
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Variables
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
a
b
c
d
f
h
i
m
n
o
p
r
s
t
u
v
w
Enumerations
b
c
d
e
k
m
n
o
p
s
t
x
Enumerator
_
a
b
c
d
e
f
g
h
k
l
m
n
p
q
r
s
t
u
v
w
x
Related Symbols
b
c
d
e
f
k
o
p
s
t
x
Files
File List
File Members
All
_
a
b
c
d
e
f
k
m
n
p
t
w
Functions
b
c
Typedefs
b
c
e
p
t
Enumerations
Enumerator
b
c
d
e
k
n
Macros
_
a
b
c
f
m
n
t
w
src
lib
utils
bswap.h
Go to the documentation of this file.
1
/*
2
* Byte Swapping Operations
3
* (C) 1999-2011,2018 Jack Lloyd
4
* (C) 2007 Yves Jerschow
5
* (C) 2024 René Meusel - Rohde & Schwarz Cybersecurity
6
*
7
* TODO: C++23: replace this entire implementation with std::byteswap
8
*
9
* Botan is released under the Simplified BSD License (see license.txt)
10
*/
11
12
#ifndef BOTAN_BYTE_SWAP_H_
13
#define BOTAN_BYTE_SWAP_H_
14
15
#include <botan/types.h>
16
17
#include <botan/compiler.h>
18
#include <concepts>
19
20
namespace
Botan
{
21
22
/**
23
* Swap the byte order of an unsigned integer
24
*/
25
template
<std::
unsigned
_
int
egral T>
26
requires
(
sizeof
(T) == 1 ||
sizeof
(T) == 2 ||
sizeof
(T) == 4 ||
sizeof
(T) == 8)
27
inline
constexpr
T
reverse_bytes
(T x) {
28
if
constexpr
(
sizeof
(T) == 1) {
29
return
x;
30
}
else
if
constexpr
(
sizeof
(T) == 2) {
31
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap16)
32
return
static_cast<
T
>
(__builtin_bswap16(x));
33
#else
34
return
static_cast<
T
>
((x << 8) | (x >> 8));
35
#endif
36
}
else
if
constexpr
(
sizeof
(T) == 4) {
37
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap32)
38
return
static_cast<
T
>
(__builtin_bswap32(x));
39
#else
40
// MSVC at least recognizes this as a bswap
41
return
static_cast<
T
>
(((x & 0x000000FF) << 24) | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8) |
42
((x & 0xFF000000) >> 24));
43
#endif
44
}
else
if
constexpr
(
sizeof
(T) == 8) {
45
#if BOTAN_COMPILER_HAS_BUILTIN(__builtin_bswap64)
46
return
static_cast<
T
>
(__builtin_bswap64(x));
47
#else
48
uint32_t hi =
static_cast<
uint32_t
>
(x >> 32);
49
uint32_t lo =
static_cast<
uint32_t
>
(x);
50
51
hi =
reverse_bytes
(hi);
52
lo =
reverse_bytes
(lo);
53
54
return
(
static_cast<
T
>
(lo) << 32) | hi;
55
#endif
56
}
57
}
27
inline
constexpr
T
reverse_bytes
(T x) {
…
}
58
59
}
// namespace Botan
60
61
#endif
Botan
Definition
alg_id.cpp:13
Botan::reverse_bytes
constexpr T reverse_bytes(T x)
Definition
bswap.h:27
Generated by
1.13.2