digestpp 1.0
C++11 header-only message digest library
Loading...
Searching...
No Matches
functions.hpp
Go to the documentation of this file.
1/*
2This code is written by kerukuro and released into public domain.
3*/
4
5#ifndef DIGESTPP_DETAIL_FUNCTIONS_HPP
6#define DIGESTPP_DETAIL_FUNCTIONS_HPP
7
8#include <cstdint>
9
10namespace digestpp
11{
12namespace detail
13{
14
15// Byte-swap a 16-bit unsigned integer.
16inline uint16_t byteswap(uint16_t val)
17{
18 return ((val & 0xff) << 8) | ((val & 0xff00) >> 8);
19}
20
21// Byte-swap a 32-bit unsigned integer.
22inline uint32_t byteswap(uint32_t val)
23{
24 return (((val & 0xff000000) >> 24) |
25 ((val & 0x00ff0000) >> 8) |
26 ((val & 0x0000ff00) << 8) |
27 ((val & 0x000000ff) << 24));
28}
29
30// Byte-swap a 64-bit unsigned integer.
31inline uint64_t byteswap(uint64_t val)
32{
33 return (((val & 0xff00000000000000ull) >> 56) |
34 ((val & 0x00ff000000000000ull) >> 40) |
35 ((val & 0x0000ff0000000000ull) >> 24) |
36 ((val & 0x000000ff00000000ull) >> 8) |
37 ((val & 0x00000000ff000000ull) << 8) |
38 ((val & 0x0000000000ff0000ull) << 24) |
39 ((val & 0x000000000000ff00ull) << 40) |
40 ((val & 0x00000000000000ffull) << 56));
41}
42
43#ifdef __APPLE__
44inline size_t byteswap(size_t val)
45{
46 switch(sizeof(size_t))
47 {
48 case sizeof(uint16_t):
49 return (size_t)byteswap((uint16_t)val);
50
51 case sizeof(uint32_t):
52 return (size_t)byteswap((uint32_t)val);
53
54 case sizeof(uint64_t):
55 return (size_t)byteswap((uint64_t)val);
56 }
57}
58#endif
59
60// Rotate 32-bit unsigned integer to the right.
61inline uint32_t rotate_right(uint32_t x, unsigned n)
62{
63 return (x >> n) | (x << (32 - n));
64}
65
66// Rotate 32-bit unsigned integer to the left.
67inline uint32_t rotate_left(uint32_t x, unsigned n)
68{
69 return (x << n) | (x >> (32 - n));
70}
71
72// Rotate 64-bit unsigned integer to the right.
73inline uint64_t rotate_right(uint64_t x, unsigned n)
74{
75 return (x >> n) | (x << (64 - n));
76}
77
78// Rotate 64-bit unsigned integer to the left.
79inline uint64_t rotate_left(uint64_t x, unsigned n)
80{
81 return (x << n) | (x >> (64 - n));
82}
83
84// Clear memory, suppressing compiler optimizations.
85inline void zero_memory(void *v, size_t n) {
86 volatile unsigned char *p = static_cast<volatile unsigned char *>(v);
87 while (n--) {
88 *p++ = 0;
89 }
90}
91
92// Clear memory occupied by an array, suppressing compiler optimizations.
93template<typename T, size_t N>
94inline void zero_memory(std::array<T, N>& ar)
95{
96 zero_memory(ar.data(), ar.size() * sizeof(T));
97}
98
99// Clear memory occupied by std::string
100inline void zero_memory(std::string& s)
101{
102 if (!s.empty())
103 zero_memory(&s[0], s.size());
104}
105
106
107} // namespace detail
108} // namespace digestpp
109
110#endif
uint16_t byteswap(uint16_t val)
Definition functions.hpp:16
uint32_t rotate_right(uint32_t x, unsigned n)
Definition functions.hpp:61
uint32_t rotate_left(uint32_t x, unsigned n)
Definition functions.hpp:67
void zero_memory(void *v, size_t n)
Definition functions.hpp:85
digestpp namespace
Definition ascon.hpp:14