25#ifndef DCCLBINARY20100713H
26#define DCCLBINARY20100713H
37#define BUFFERSIZE BUFSIZ
38#include "b64/decode.h"
39#include "b64/encode.h"
51inline 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;
86inline std::string
hex_decode(
const std::string& in)
99template <
typename CharIterator>
100inline 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);
133template <
typename CharIterator>
inline std::string hex_encode(CharIterator begin, CharIterator end)
136 hex_encode(begin, end, &out);
145inline void hex_encode(
const std::string& in, std::string* out,
bool upper_case =
false)
147 hex_encode(in.begin(), in.end(), out, upper_case);
150inline std::string hex_encode(
const std::string& in)
153 hex_encode(in, &out);
158inline 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();
167inline 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;
197inline double log2(
double d) {
return std::log2(d); }
Dynamic Compact Control Language namespace.
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...
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)