25 #ifndef DCCLBINARY20100713H
26 #define DCCLBINARY20100713H
37 #define BUFFERSIZE BUFSIZ
38 #include "b64/decode.h"
39 #include "b64/encode.h"
51 inline void hex_decode(
const std::string& in, std::string* out)
53 static constexpr
short char0_9_to_number = 48;
54 static constexpr
short charA_F_to_number = 55;
55 static constexpr
short chara_f_to_number = 87;
57 int in_size = in.size();
58 int out_size = in_size >> 1;
62 out->assign(out_size,
'\0');
63 for (
int i = (in_size & 1) ? -1 : 0, n = in_size; i < n; i += 2)
65 int out_i = (in_size & 1) ? (i + 1) / 2 : i / 2;
69 if (in[i] >=
'0' && in[i] <=
'9')
70 (*out)[out_i] |= ((in[i] - char0_9_to_number) & 0x0f) << 4;
71 else if (in[i] >=
'A' && in[i] <=
'F')
72 (*out)[out_i] |= ((in[i] - charA_F_to_number) & 0x0f) << 4;
73 else if (in[i] >=
'a' && in[i] <=
'f')
74 (*out)[out_i] |= ((in[i] - chara_f_to_number) & 0x0f) << 4;
77 if (in[i + 1] >=
'0' && in[i + 1] <=
'9')
78 (*out)[out_i] |= (in[i + 1] - char0_9_to_number) & 0x0f;
79 else if (in[i + 1] >=
'A' && in[i + 1] <=
'F')
80 (*out)[out_i] |= (in[i + 1] - charA_F_to_number) & 0x0f;
81 else if (in[i + 1] >=
'a' && in[i + 1] <=
'f')
82 (*out)[out_i] |= (in[i + 1] - chara_f_to_number) & 0x0f;
86 inline std::string
hex_decode(
const std::string& in)
99 template <
typename CharIterator>
100 inline void hex_encode(CharIterator begin, CharIterator end, std::string* out,
101 bool upper_case =
false)
103 static constexpr
short char0_9_to_number = 48;
104 static constexpr
short charA_F_to_number = 55;
105 static constexpr
short chara_f_to_number = 87;
107 size_t in_size = std::distance(begin, end);
108 size_t out_size = in_size << 1;
111 out->resize(out_size);
114 for (CharIterator it = begin; it != end; ++it)
116 short msn = (*it >> 4) & 0x0f;
117 short lsn = *it & 0x0f;
119 if (msn >= 0 && msn <= 9)
120 (*out)[2 * i] = msn + char0_9_to_number;
121 else if (msn >= 10 && msn <= 15)
122 (*out)[2 * i] = msn + (upper_case ? charA_F_to_number : chara_f_to_number);
124 if (lsn >= 0 && lsn <= 9)
125 (*out)[2 * i + 1] = lsn + char0_9_to_number;
126 else if (lsn >= 10 && lsn <= 15)
127 (*out)[2 * i + 1] = lsn + (upper_case ? charA_F_to_number : chara_f_to_number);
133 template <
typename CharIterator>
inline std::string
hex_encode(CharIterator begin, CharIterator end)
145 inline void hex_encode(
const std::string& in, std::string* out,
bool upper_case =
false)
147 hex_encode(in.begin(), in.end(), out, upper_case);
150 inline std::string
hex_encode(
const std::string& in)
158 inline std::string b64_encode(
const std::string& in)
160 std::stringstream instream(in);
161 std::stringstream outstream;
163 D.encode(instream, outstream);
164 return outstream.str();
167 inline std::string b64_decode(
const std::string& in)
169 std::stringstream instream(in);
170 std::stringstream outstream;
172 D.decode(instream, outstream);
173 return outstream.str();
181 unsigned r = ((v & (v - 1)) == 0) ? 0 : 1;
197 inline double log2(
double d) {
return std::log2(d); }