22 #ifndef DCCLBINARY20100713H 23 #define DCCLBINARY20100713H 29 #include "dccl/common.h" 31 #define DCCL_HAS_B64 1 34 #include "b64/encode.h" 35 #include "b64/decode.h" 47 inline void hex_decode(
const std::string& in, std::string* out)
49 static const short char0_9_to_number = 48;
50 static const short charA_F_to_number = 55;
51 static const short chara_f_to_number = 87;
53 int in_size = in.size();
54 int out_size = in_size >> 1;
58 out->assign(out_size,
'\0');
59 for(
int i = (in_size & 1) ? -1 : 0, n = in_size;
63 int out_i = (in_size & 1) ? (i+1) / 2 : i/2;
67 if(in[i] >=
'0' && in[i] <=
'9')
68 (*out)[out_i] |= ((in[i]-char0_9_to_number) & 0x0f) << 4;
69 else if(in[i] >=
'A' && in[i] <=
'F')
70 (*out)[out_i] |= ((in[i]-charA_F_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;
75 if(in[i+1] >=
'0' && in[i+1] <=
'9')
76 (*out)[out_i] |= (in[i+1]-char0_9_to_number) & 0x0f;
77 else if(in[i+1] >=
'A' && in[i+1] <=
'F')
78 (*out)[out_i] |= (in[i+1]-charA_F_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;
84 inline std::string
hex_decode(
const std::string& in)
97 template <
typename CharIterator>
98 inline void hex_encode(CharIterator begin, CharIterator end, std::string* out,
bool upper_case =
false)
100 static const short char0_9_to_number = 48;
101 static const short charA_F_to_number = 55;
102 static const short chara_f_to_number = 87;
104 size_t in_size = std::distance(begin, end);
105 size_t out_size = in_size << 1;
108 out->resize(out_size);
111 for(CharIterator it = begin; it != end; ++it)
113 short msn = (*it >> 4) & 0x0f;
114 short lsn = *it & 0x0f;
116 if(msn >= 0 && msn <= 9)
117 (*out)[2*i] = msn + char0_9_to_number;
118 else if(msn >= 10 && msn <= 15)
119 (*out)[2*i] = msn + (upper_case ? charA_F_to_number : chara_f_to_number);
121 if(lsn >= 0 && lsn <= 9)
122 (*out)[2*i+1] = lsn + char0_9_to_number;
123 else if(lsn >= 10 && lsn <= 15)
124 (*out)[2*i+1] = lsn + (upper_case ? charA_F_to_number : chara_f_to_number);
130 template <
typename CharIterator>
131 inline std::string
hex_encode(CharIterator begin, CharIterator end)
144 inline void hex_encode(
const std::string& in, std::string* out,
bool upper_case =
false)
146 hex_encode(in.begin(), in.end(), out, upper_case);
149 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;
188 {
return ceil_log2(static_cast<dccl::uint64>(std::ceil(d))); }
191 {
return ceil_log2(static_cast<dccl::uint64>(i)); }
194 {
return ceil_log2(static_cast<dccl::uint64>(i)); }
196 inline unsigned long ceil_log2(
unsigned i)
197 {
return ceil_log2(static_cast<dccl::uint64>(i)); }
199 inline double log2(
double d)
201 static double log_2 = log(2);
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...
unsigned ceil_log2(dccl::uint64 v)
google::protobuf::uint64 uint64
an unsigned 64 bit integer
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
Dynamic Compact Control Language namespace.