10 #ifndef __PION_ALGORITHM_HEADER__
11 #define __PION_ALGORITHM_HEADER__
14 #include <boost/cstdint.hpp>
15 #include <pion/config.hpp>
27 static bool base64_decode(std::string
const &input, std::string & output);
35 static bool base64_encode(std::string
const &input, std::string & output);
38 static std::string url_decode(
const std::string& str);
41 static std::string url_encode(
const std::string& str);
47 static std::string xml_encode(
const std::string& str);
51 static void float_from_bytes(
long double& value,
const unsigned char *ptr,
size_t num_exp_bits,
size_t num_fraction_bits);
55 static void float_to_bytes(
long double value,
unsigned char *ptr,
size_t num_exp_bits,
size_t num_fraction_bits);
58 static inline boost::uint8_t
to_uint8(
unsigned char byte) {
59 return boost::uint8_t(byte);
63 static inline boost::int8_t
to_int8(
unsigned char byte) {
64 return boost::int8_t(byte);
68 static inline boost::uint8_t
to_uint8(
char byte) {
69 return boost::uint8_t(byte);
73 static inline boost::int8_t
to_int8(
char byte) {
74 return boost::int8_t(byte);
78 static inline boost::uint16_t
to_uint16(
unsigned char high,
unsigned char low) {
79 return (((boost::uint16_t)high) << 8) | ((boost::uint16_t)low);
83 static inline boost::int16_t
to_int16(
unsigned char high,
unsigned char low) {
84 return (((boost::int16_t)high) << 8) | ((boost::int16_t)low);
88 static inline boost::uint32_t
to_uint24(
unsigned char high,
unsigned char mid,
unsigned char low) {
89 return (((boost::uint32_t)high) << 16) | (((boost::uint32_t)mid) << 8) | ((boost::uint32_t)low);
93 static inline boost::int32_t
to_int24(
unsigned char high,
unsigned char mid,
unsigned char low) {
94 return (((boost::int32_t)high) << 16) | (((boost::int32_t)mid) << 8) | ((boost::int32_t)low);
98 static inline boost::uint32_t
to_uint32(
unsigned char high,
unsigned char mid1,
unsigned char mid2,
unsigned char low) {
99 return (((boost::uint32_t)high) << 24) | (((boost::uint32_t)mid1) << 16) | (((boost::uint32_t)mid2) << 8) | ((boost::uint32_t)low);
103 static inline boost::int32_t
to_int32(
unsigned char high,
unsigned char mid1,
unsigned char mid2,
unsigned char low) {
104 return (((boost::int32_t)high) << 24) | (((boost::int32_t)mid1) << 16) | (((boost::int32_t)mid2) << 8) | ((boost::int32_t)low);
108 static inline boost::uint64_t
to_uint64(
unsigned char high,
unsigned char mid1,
unsigned char mid2,
unsigned char mid3,
unsigned char mid4,
unsigned char mid5,
unsigned char mid6,
unsigned char low) {
109 return (((boost::uint64_t)high) << 56) | (((boost::uint64_t)mid1) << 48) | (((boost::uint64_t)mid2) << 40) | (((boost::uint64_t)mid3) << 32)
110 | (((boost::uint64_t)mid4) << 24) | (((boost::uint64_t)mid5) << 16) | (((boost::uint64_t)mid6) << 8) | ((boost::uint64_t)low);
114 static inline boost::int64_t
to_int64(
unsigned char high,
unsigned char mid1,
unsigned char mid2,
unsigned char mid3,
unsigned char mid4,
unsigned char mid5,
unsigned char mid6,
unsigned char low) {
115 return (((boost::int64_t)high) << 56) | (((boost::int64_t)mid1) << 48) | (((boost::int64_t)mid2) << 40) | (((boost::int64_t)mid3) << 32)
116 | (((boost::int64_t)mid4) << 24) | (((boost::int64_t)mid5) << 16) | (((boost::int64_t)mid6) << 8) | ((boost::int64_t)low);
120 template <
typename T1,
typename T2>
121 static inline boost::uint16_t
to_uint16(T1 high, T2 low) {
122 return to_uint16(static_cast<unsigned char>(high), static_cast<unsigned char>(low));
126 template <
typename T1,
typename T2>
127 static inline boost::int16_t
to_int16(T1 high, T2 low) {
128 return to_int16(static_cast<unsigned char>(high), static_cast<unsigned char>(low));
132 template <
typename T1,
typename T2,
typename T3>
133 static inline boost::uint32_t
to_uint24(T1 high, T2 mid, T3 low) {
134 return to_uint24(static_cast<unsigned char>(high),
135 static_cast<unsigned char>(mid),
136 static_cast<unsigned char>(low));
140 template <
typename T1,
typename T2,
typename T3>
141 static inline boost::int32_t
to_int24(T1 high, T2 mid, T3 low) {
142 return to_int24(static_cast<unsigned char>(high),
143 static_cast<unsigned char>(mid),
144 static_cast<unsigned char>(low));
148 template <
typename T1,
typename T2,
typename T3,
typename T4>
149 static inline boost::uint32_t
to_uint32(T1 high, T2 mid1, T3 mid2, T4 low) {
150 return to_uint32(static_cast<unsigned char>(high),
151 static_cast<unsigned char>(mid1),
152 static_cast<unsigned char>(mid2),
153 static_cast<unsigned char>(low));
157 template <
typename T1,
typename T2,
typename T3,
typename T4>
158 static inline boost::int32_t
to_int32(T1 high, T2 mid1, T3 mid2, T4 low) {
159 return to_int32(static_cast<unsigned char>(high),
160 static_cast<unsigned char>(mid1),
161 static_cast<unsigned char>(mid2),
162 static_cast<unsigned char>(low));
166 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8>
167 static inline boost::uint64_t
to_uint64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low) {
168 return to_uint64(static_cast<unsigned char>(high),
169 static_cast<unsigned char>(mid1),
170 static_cast<unsigned char>(mid2),
171 static_cast<unsigned char>(mid3),
172 static_cast<unsigned char>(mid4),
173 static_cast<unsigned char>(mid5),
174 static_cast<unsigned char>(mid6),
175 static_cast<unsigned char>(low));
179 template <
typename T1,
typename T2,
typename T3,
typename T4,
typename T5,
typename T6,
typename T7,
typename T8>
180 static inline boost::int64_t
to_int64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low) {
181 return to_int64(static_cast<unsigned char>(high),
182 static_cast<unsigned char>(mid1),
183 static_cast<unsigned char>(mid2),
184 static_cast<unsigned char>(mid3),
185 static_cast<unsigned char>(mid4),
186 static_cast<unsigned char>(mid5),
187 static_cast<unsigned char>(mid6),
188 static_cast<unsigned char>(low));
193 template <
typename Byte>
194 static inline boost::uint8_t
to_uint8(
const Byte *buf) {
195 return to_uint8(buf[0]);
199 template <
typename Byte>
200 static inline boost::int8_t
to_int8(
const Byte *buf) {
201 return to_int8(buf[0]);
205 template <
typename Byte>
206 static inline boost::uint16_t
to_uint16(
const Byte *buf) {
207 return to_uint16(buf[0], buf[1]);
211 template <
typename Byte>
212 static inline boost::int16_t
to_int16(
const Byte *buf) {
213 return to_int16(buf[0], buf[1]);
217 template <
typename Byte>
218 static inline boost::uint32_t
to_uint24(
const Byte *buf) {
219 return to_uint24(buf[0], buf[1], buf[2]);
223 template <
typename Byte>
224 static inline boost::int32_t
to_int24(
const Byte *buf) {
225 return to_int24(buf[0], buf[1], buf[2]);
229 template <
typename Byte>
230 static inline boost::uint32_t
to_uint32(
const Byte *buf) {
231 return to_uint32(buf[0], buf[1], buf[2], buf[3]);
235 template <
typename Byte>
236 static inline boost::int32_t
to_int32(
const Byte *buf) {
237 return to_int32(buf[0], buf[1], buf[2], buf[3]);
241 template <
typename Byte>
242 static inline boost::uint64_t
to_uint64(
const Byte *buf) {
243 return to_uint64(buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
247 template <
typename Byte>
248 static inline boost::int64_t
to_int64(
const Byte *buf) {
249 return to_int64(buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
253 template <
typename Byte>
254 static inline void from_uint8(Byte *buf,
const boost::uint8_t n) {
259 template <
typename Byte>
260 static inline void from_int8(Byte *buf,
const boost::int8_t n) {
265 template <
typename Byte>
266 static inline void from_uint16(Byte *buf,
const boost::uint16_t n) {
267 buf[0] = (n >> 8) & 0xFF;
272 template <
typename Byte>
273 static inline void from_int16(Byte *buf,
const boost::int16_t n) {
274 buf[0] = (n >> 8) & 0xFF;
279 template <
typename Byte>
280 static inline void from_uint24(Byte *buf,
const boost::uint32_t n) {
281 buf[0] = (n >> 16) & 0xFF;
282 buf[1] = (n >> 8) & 0xFF;
287 template <
typename Byte>
288 static inline void from_int24(Byte *buf,
const boost::int32_t n) {
289 buf[0] = (n >> 16) & 0xFF;
290 buf[1] = (n >> 8) & 0xFF;
295 template <
typename Byte>
296 static inline void from_uint32(Byte *buf,
const boost::uint32_t n) {
297 buf[0] = (n >> 24) & 0xFF;
298 buf[1] = (n >> 16) & 0xFF;
299 buf[2] = (n >> 8) & 0xFF;
304 template <
typename Byte>
305 static inline void from_int32(Byte *buf,
const boost::int32_t n) {
306 buf[0] = (n >> 24) & 0xFF;
307 buf[1] = (n >> 16) & 0xFF;
308 buf[2] = (n >> 8) & 0xFF;
313 template <
typename Byte>
314 static inline void from_uint64(Byte *buf,
const boost::uint64_t n) {
315 buf[0] = (n >> 56) & 0xFF;
316 buf[1] = (n >> 48) & 0xFF;
317 buf[2] = (n >> 40) & 0xFF;
318 buf[3] = (n >> 32) & 0xFF;
319 buf[4] = (n >> 24) & 0xFF;
320 buf[5] = (n >> 16) & 0xFF;
321 buf[6] = (n >> 8) & 0xFF;
326 template <
typename Byte>
327 static inline void from_int64(Byte *buf,
const boost::int64_t n) {
328 buf[0] = (n >> 56) & 0xFF;
329 buf[1] = (n >> 48) & 0xFF;
330 buf[2] = (n >> 40) & 0xFF;
331 buf[3] = (n >> 32) & 0xFF;
332 buf[4] = (n >> 24) & 0xFF;
333 buf[5] = (n >> 16) & 0xFF;
334 buf[6] = (n >> 8) & 0xFF;
340 template <
typename Byte>
343 float_from_bytes(value, (
unsigned char *)ptr, 8U, 23U);
349 template <
typename Byte>
352 float_from_bytes(value, (
unsigned char *)ptr, 11U, 52U);
358 template <
typename Byte>
361 float_from_bytes(value, (
unsigned char *)ptr, 15U, 112U);
367 template <
typename Byte>
369 float_to_bytes(n, (
unsigned char*)ptr, 8U, 23U);
374 template <
typename Byte>
376 float_to_bytes(n, (
unsigned char*)ptr, 11U, 52U);
381 template <
typename Byte>
383 float_to_bytes(n, (
unsigned char*)ptr, 15U, 112U);
static boost::int64_t to_int64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low)
convert sequence of eight bytes to 64-bit signed integer
static void from_int24(Byte *buf, const boost::int32_t n)
convert 24-bit signed integer into sequence of three bytes
static void from_float(Byte *ptr, const float n)
static boost::uint64_t to_uint64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low)
convert sequence of eight bytes to 64-bit unsigned integer
static boost::uint64_t to_uint64(const Byte *buf)
convert sequence of eight bytes to 64-bit unsigned integer
static boost::int16_t to_int16(unsigned char high, unsigned char low)
convert sequence of two bytes to 16-bit signed integer
static void from_int32(Byte *buf, const boost::int32_t n)
convert 32-bit signed integer into sequence of four bytes
static boost::uint8_t to_uint8(char byte)
convert sequence of one byte to 8-bit unsigned integer
static boost::int64_t to_int64(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char mid3, unsigned char mid4, unsigned char mid5, unsigned char mid6, unsigned char low)
convert sequence of eight bytes to 64-bit signed integer
static boost::uint16_t to_uint16(T1 high, T2 low)
convert sequence of two bytes to 16-bit unsigned integer
static boost::uint32_t to_uint32(T1 high, T2 mid1, T3 mid2, T4 low)
convert sequence of four bytes to 32-bit unsigned integer
static boost::int16_t to_int16(T1 high, T2 low)
convert sequence of two bytes to 16-bit signed integer
static long double to_long_double(const Byte *ptr)
static boost::int32_t to_int24(const Byte *buf)
convert sequence of three bytes to 24-bit signed integer
static boost::int32_t to_int32(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char low)
convert sequence of four bytes to 32-bit signed integer
static boost::int8_t to_int8(unsigned char byte)
convert sequence of one byte to 8-bit signed integer
static boost::uint32_t to_uint24(T1 high, T2 mid, T3 low)
convert sequence of three bytes to 24-bit unsigned integer
static void from_uint64(Byte *buf, const boost::uint64_t n)
convert 64-bit unsigned integer into sequence of eight bytes
static void from_uint24(Byte *buf, const boost::uint32_t n)
convert 24-bit unsigned integer into sequence of three bytes
static boost::int16_t to_int16(const Byte *buf)
convert sequence of two bytes to 16-bit signed integer
static boost::int8_t to_int8(char byte)
convert sequence of one byte to 8-bit signed integer
static void from_uint16(Byte *buf, const boost::uint16_t n)
convert 16-bit unsigned integer into sequence of two bytes
static boost::uint16_t to_uint16(unsigned char high, unsigned char low)
convert sequence of two bytes to 16-bit unsigned integer
static double to_double(const Byte *ptr)
static boost::uint8_t to_uint8(unsigned char byte)
convert sequence of one byte to 8-bit unsigned integer
static void from_int64(Byte *buf, const boost::int64_t n)
convert 64-bit signed integer into sequence of eight bytes
static void from_int8(Byte *buf, const boost::int8_t n)
convert 8-bit signed integer into sequence of one byte
static boost::uint32_t to_uint32(const Byte *buf)
convert sequence of four bytes to 32-bit unsigned integer
static boost::int32_t to_int24(unsigned char high, unsigned char mid, unsigned char low)
convert sequence of three bytes to 24-bit signed integer
static float to_float(const Byte *ptr)
static boost::uint64_t to_uint64(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char mid3, unsigned char mid4, unsigned char mid5, unsigned char mid6, unsigned char low)
convert sequence of eight bytes to 64-bit unsigned integer
static boost::uint32_t to_uint24(const Byte *buf)
convert sequence of three bytes to 24-bit unsigned integer
static boost::uint16_t to_uint16(const Byte *buf)
convert sequence of two bytes to 16-bit unsigned integer
static boost::int32_t to_int32(const Byte *buf)
convert sequence of four bytes to 32-bit signed integer
static boost::int32_t to_int32(T1 high, T2 mid1, T3 mid2, T4 low)
convert sequence of four bytes to 32-bit signed integer
static boost::uint32_t to_uint24(unsigned char high, unsigned char mid, unsigned char low)
convert sequence of three bytes to 24-bit unsigned integer
static void from_uint32(Byte *buf, const boost::uint32_t n)
convert 32-bit unsigned integer into sequence of four bytes
static boost::uint8_t to_uint8(const Byte *buf)
convert byte pointer into an 8-bit unsigned integer
static boost::int64_t to_int64(const Byte *buf)
convert sequence of eight bytes to 64-bit signed integer
static void from_double(Byte *ptr, const double n)
static void from_int16(Byte *buf, const boost::int16_t n)
convert 16-bit signed integer into sequence of two bytes
static boost::int8_t to_int8(const Byte *buf)
convert byte pointer into an 8-bit signed integer
static boost::uint32_t to_uint32(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char low)
convert sequence of four bytes to 32-bit unsigned integer
static void from_long_double(Byte *ptr, const long double n)
static boost::int32_t to_int24(T1 high, T2 mid, T3 low)
convert sequence of three bytes to 24-bit signed integer
static void from_uint8(Byte *buf, const boost::uint8_t n)
convert 8-bit unsigned integer into sequence of one byte