22 #ifndef DCCLBINARY20100713H
23 #define DCCLBINARY20100713H
29 #include "dccl/common.h"
31 #define DCCL_HAS_B64 1
35 #define BUFFERSIZE BUFSIZ
36 #include "b64/encode.h"
37 #include "b64/decode.h"
49 inline void hex_decode(
const std::string& in, std::string* out)
51 static const short char0_9_to_number = 48;
52 static const short charA_F_to_number = 55;
53 static const short chara_f_to_number = 87;
55 int in_size = in.size();
56 int out_size = in_size >> 1;
60 out->assign(out_size,
'\0');
61 for(
int i = (in_size & 1) ? -1 : 0, n = in_size;
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,
bool upper_case =
false)
102 static const short char0_9_to_number = 48;
103 static const short charA_F_to_number = 55;
104 static const short chara_f_to_number = 87;
106 size_t in_size = std::distance(begin, end);
107 size_t out_size = in_size << 1;
110 out->resize(out_size);
113 for(CharIterator it = begin; it != end; ++it)
115 short msn = (*it >> 4) & 0x0f;
116 short lsn = *it & 0x0f;
118 if(msn >= 0 && msn <= 9)
119 (*out)[2*i] = msn + char0_9_to_number;
120 else if(msn >= 10 && msn <= 15)
121 (*out)[2*i] = msn + (upper_case ? charA_F_to_number : chara_f_to_number);
123 if(lsn >= 0 && lsn <= 9)
124 (*out)[2*i+1] = lsn + char0_9_to_number;
125 else if(lsn >= 10 && lsn <= 15)
126 (*out)[2*i+1] = lsn + (upper_case ? charA_F_to_number : chara_f_to_number);
132 template <
typename CharIterator>
133 inline std::string
hex_encode(CharIterator begin, CharIterator end)
146 inline void hex_encode(
const std::string& in, std::string* out,
bool upper_case =
false)
148 hex_encode(in.begin(), in.end(), out, upper_case);
151 inline std::string
hex_encode(
const std::string& in)
160 inline std::string b64_encode(
const std::string& in)
162 std::stringstream instream(in);
163 std::stringstream outstream;
165 D.encode(instream, outstream);
166 return outstream.str();
169 inline std::string b64_decode(
const std::string& in)
171 std::stringstream instream(in);
172 std::stringstream outstream;
174 D.decode(instream, outstream);
175 return outstream.str();
183 unsigned r = ((v & (v - 1)) == 0) ? 0 : 1;
198 inline unsigned long ceil_log2(
unsigned i)
201 inline double log2(
double d)
203 static double log_2 = log(2);