forked from fuzziqersoftware/phosg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoding.hh
97 lines (79 loc) · 2.34 KB
/
Encoding.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#pragma once
#include <stdint.h>
#include <string>
static inline uint8_t bswap8(uint8_t a) {
return a;
}
static inline uint16_t bswap16(uint16_t a) {
return ((a >> 8) & 0x00FF) |
((a << 8) & 0xFF00);
}
static inline uint32_t bswap24(uint32_t a) {
return ((a >> 16) & 0x000000FF) |
( a & 0x0000FF00) |
((a << 16) & 0x00FF0000);
}
static inline int32_t bswap24s(int32_t a) {
int32_t r = bswap24(a);
if (r & 0x00800000) {
return r | 0xFF000000;
}
return r;
}
static inline uint32_t bswap32(uint32_t a) {
return ((a >> 24) & 0x000000FF) |
((a >> 8) & 0x0000FF00) |
((a << 8) & 0x00FF0000) |
((a << 24) & 0xFF000000);
}
static inline uint64_t bswap48(uint64_t a) {
return ((a >> 40) & 0x00000000000000FF) |
((a >> 24) & 0x000000000000FF00) |
((a >> 8) & 0x0000000000FF0000) |
((a << 8) & 0x00000000FF000000) |
((a << 24) & 0x000000FF00000000) |
((a << 40) & 0x0000FF0000000000);
}
static inline int64_t bswap48s(int64_t a) {
int64_t r = bswap48(a);
if (r & 0x0000800000000000) {
return r | 0xFFFF000000000000;
}
return r;
}
static inline uint64_t bswap64(uint64_t a) {
return ((a >> 56) & 0x00000000000000FF) |
((a >> 40) & 0x000000000000FF00) |
((a >> 24) & 0x0000000000FF0000) |
((a >> 8) & 0x00000000FF000000) |
((a << 8) & 0x000000FF00000000) |
((a << 24) & 0x0000FF0000000000) |
((a << 40) & 0x00FF000000000000) |
((a << 56) & 0xFF00000000000000);
}
static inline float bswap32f(uint32_t a) {
float f;
*(uint32_t*)(&f) = bswap32(a);
return f;
}
static inline double bswap64f(uint64_t a) {
double d;
*(uint64_t*)(&d) = bswap64(a);
return d;
}
static inline uint32_t bswap32f(float a) {
uint32_t i = *(uint32_t*)(&a);
return bswap32(i);
}
static inline uint64_t bswap64f(double a) {
uint64_t i = *(uint64_t*)(&a);
return bswap64(i);
}
extern const char* DEFAULT_ALPHABET;
extern const char* URLSAFE_ALPHABET;
std::string base64_encode(const void* data, size_t size,
const char* alphabet = NULL);
std::string base64_encode(const std::string& data, const char* alphabet = NULL);
std::string base64_decode(const void* data, size_t size,
const char* alphabet = NULL);
std::string base64_decode(const std::string& data, const char* alphabet = NULL);