pion  5.0.6
algorithm.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_ALGORITHM_HEADER__
11 #define __PION_ALGORITHM_HEADER__
12 
13 #include <string>
14 #include <boost/cstdint.hpp>
15 #include <pion/config.hpp>
16 
17 namespace pion { // begin namespace pion
18 
19 struct PION_API algorithm {
20 
27  static bool base64_decode(std::string const &input, std::string & output);
28 
35  static bool base64_encode(std::string const &input, std::string & output);
36 
38  static std::string url_decode(const std::string& str);
39 
41  static std::string url_encode(const std::string& str);
42 
44  //static std::string xml_decode(const std::string& str);
45 
47  static std::string xml_encode(const std::string& str);
48 
51  static void float_from_bytes(long double& value, const unsigned char *ptr, size_t num_exp_bits, size_t num_fraction_bits);
52 
55  static void float_to_bytes(long double value, unsigned char *ptr, size_t num_exp_bits, size_t num_fraction_bits);
56 
58  static inline boost::uint8_t to_uint8(unsigned char byte) {
59  return boost::uint8_t(byte);
60  }
61 
63  static inline boost::int8_t to_int8(unsigned char byte) {
64  return boost::int8_t(byte);
65  }
66 
68  static inline boost::uint8_t to_uint8(char byte) {
69  return boost::uint8_t(byte);
70  }
71 
73  static inline boost::int8_t to_int8(char byte) {
74  return boost::int8_t(byte);
75  }
76 
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);
80  }
81 
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);
85  }
86 
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);
90  }
91 
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);
95  }
96 
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);
100  }
101 
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);
105  }
106 
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);
111  }
112 
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);
117  }
118 
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));
123  }
124 
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));
129  }
130 
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));
137  }
138 
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));
145  }
146 
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));
154  }
155 
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));
163  }
164 
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));
176  }
177 
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));
189  }
190 
191 
193  template <typename Byte>
194  static inline boost::uint8_t to_uint8(const Byte *buf) {
195  return to_uint8(buf[0]);
196  }
197 
199  template <typename Byte>
200  static inline boost::int8_t to_int8(const Byte *buf) {
201  return to_int8(buf[0]);
202  }
203 
205  template <typename Byte>
206  static inline boost::uint16_t to_uint16(const Byte *buf) {
207  return to_uint16(buf[0], buf[1]);
208  }
209 
211  template <typename Byte>
212  static inline boost::int16_t to_int16(const Byte *buf) {
213  return to_int16(buf[0], buf[1]);
214  }
215 
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]);
220  }
221 
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]);
226  }
227 
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]);
232  }
233 
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]);
238  }
239 
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]);
244  }
245 
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]);
250  }
251 
253  template <typename Byte>
254  static inline void from_uint8(Byte *buf, const boost::uint8_t n) {
255  buf[0] = n & 0xFF;
256  }
257 
259  template <typename Byte>
260  static inline void from_int8(Byte *buf, const boost::int8_t n) {
261  buf[0] = n & 0xFF;
262  }
263 
265  template <typename Byte>
266  static inline void from_uint16(Byte *buf, const boost::uint16_t n) {
267  buf[0] = (n >> 8) & 0xFF;
268  buf[1] = n & 0xFF;
269  }
270 
272  template <typename Byte>
273  static inline void from_int16(Byte *buf, const boost::int16_t n) {
274  buf[0] = (n >> 8) & 0xFF;
275  buf[1] = n & 0xFF;
276  }
277 
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;
283  buf[2] = n & 0xFF;
284  }
285 
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;
291  buf[2] = n & 0xFF;
292  }
293 
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;
300  buf[3] = n & 0xFF;
301  }
302 
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;
309  buf[3] = n & 0xFF;
310  }
311 
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;
322  buf[7] = n & 0xFF;
323  }
324 
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;
335  buf[7] = n & 0xFF;
336  }
337 
340  template <typename Byte>
341  static inline float to_float(const Byte *ptr) {
342  long double value;
343  float_from_bytes(value, (unsigned char *)ptr, 8U, 23U);
344  return value;
345  }
346 
349  template <typename Byte>
350  static inline double to_double(const Byte *ptr) {
351  long double value;
352  float_from_bytes(value, (unsigned char *)ptr, 11U, 52U);
353  return value;
354  }
355 
358  template <typename Byte>
359  static inline long double to_long_double(const Byte *ptr) {
360  long double value;
361  float_from_bytes(value, (unsigned char *)ptr, 15U, 112U);
362  return value;
363  }
364 
367  template <typename Byte>
368  static inline void from_float(Byte *ptr, const float n) {
369  float_to_bytes(n, (unsigned char*)ptr, 8U, 23U);
370  }
371 
374  template <typename Byte>
375  static inline void from_double(Byte *ptr, const double n) {
376  float_to_bytes(n, (unsigned char*)ptr, 11U, 52U);
377  }
378 
381  template <typename Byte>
382  static inline void from_long_double(Byte *ptr, const long double n) {
383  float_to_bytes(n, (unsigned char*)ptr, 15U, 112U);
384  }
385 };
386 
387 } // end namespace pion
388 
389 #endif
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
Definition: algorithm.hpp:180
static void from_int24(Byte *buf, const boost::int32_t n)
convert 24-bit signed integer into sequence of three bytes
Definition: algorithm.hpp:288
static void from_float(Byte *ptr, const float n)
Definition: algorithm.hpp:368
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
Definition: algorithm.hpp:167
static boost::uint64_t to_uint64(const Byte *buf)
convert sequence of eight bytes to 64-bit unsigned integer
Definition: algorithm.hpp:242
static boost::int16_t to_int16(unsigned char high, unsigned char low)
convert sequence of two bytes to 16-bit signed integer
Definition: algorithm.hpp:83
static void from_int32(Byte *buf, const boost::int32_t n)
convert 32-bit signed integer into sequence of four bytes
Definition: algorithm.hpp:305
static boost::uint8_t to_uint8(char byte)
convert sequence of one byte to 8-bit unsigned integer
Definition: algorithm.hpp:68
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
Definition: algorithm.hpp:114
static boost::uint16_t to_uint16(T1 high, T2 low)
convert sequence of two bytes to 16-bit unsigned integer
Definition: algorithm.hpp:121
static boost::uint32_t to_uint32(T1 high, T2 mid1, T3 mid2, T4 low)
convert sequence of four bytes to 32-bit unsigned integer
Definition: algorithm.hpp:149
static boost::int16_t to_int16(T1 high, T2 low)
convert sequence of two bytes to 16-bit signed integer
Definition: algorithm.hpp:127
static long double to_long_double(const Byte *ptr)
Definition: algorithm.hpp:359
static boost::int32_t to_int24(const Byte *buf)
convert sequence of three bytes to 24-bit signed integer
Definition: algorithm.hpp:224
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
Definition: algorithm.hpp:103
static boost::int8_t to_int8(unsigned char byte)
convert sequence of one byte to 8-bit signed integer
Definition: algorithm.hpp:63
static boost::uint32_t to_uint24(T1 high, T2 mid, T3 low)
convert sequence of three bytes to 24-bit unsigned integer
Definition: algorithm.hpp:133
static void from_uint64(Byte *buf, const boost::uint64_t n)
convert 64-bit unsigned integer into sequence of eight bytes
Definition: algorithm.hpp:314
static void from_uint24(Byte *buf, const boost::uint32_t n)
convert 24-bit unsigned integer into sequence of three bytes
Definition: algorithm.hpp:280
static boost::int16_t to_int16(const Byte *buf)
convert sequence of two bytes to 16-bit signed integer
Definition: algorithm.hpp:212
static boost::int8_t to_int8(char byte)
convert sequence of one byte to 8-bit signed integer
Definition: algorithm.hpp:73
static void from_uint16(Byte *buf, const boost::uint16_t n)
convert 16-bit unsigned integer into sequence of two bytes
Definition: algorithm.hpp:266
static boost::uint16_t to_uint16(unsigned char high, unsigned char low)
convert sequence of two bytes to 16-bit unsigned integer
Definition: algorithm.hpp:78
static double to_double(const Byte *ptr)
Definition: algorithm.hpp:350
static boost::uint8_t to_uint8(unsigned char byte)
convert sequence of one byte to 8-bit unsigned integer
Definition: algorithm.hpp:58
static void from_int64(Byte *buf, const boost::int64_t n)
convert 64-bit signed integer into sequence of eight bytes
Definition: algorithm.hpp:327
static void from_int8(Byte *buf, const boost::int8_t n)
convert 8-bit signed integer into sequence of one byte
Definition: algorithm.hpp:260
static boost::uint32_t to_uint32(const Byte *buf)
convert sequence of four bytes to 32-bit unsigned integer
Definition: algorithm.hpp:230
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
Definition: algorithm.hpp:93
static float to_float(const Byte *ptr)
Definition: algorithm.hpp:341
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
Definition: algorithm.hpp:108
static boost::uint32_t to_uint24(const Byte *buf)
convert sequence of three bytes to 24-bit unsigned integer
Definition: algorithm.hpp:218
static boost::uint16_t to_uint16(const Byte *buf)
convert sequence of two bytes to 16-bit unsigned integer
Definition: algorithm.hpp:206
static boost::int32_t to_int32(const Byte *buf)
convert sequence of four bytes to 32-bit signed integer
Definition: algorithm.hpp:236
static boost::int32_t to_int32(T1 high, T2 mid1, T3 mid2, T4 low)
convert sequence of four bytes to 32-bit signed integer
Definition: algorithm.hpp:158
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
Definition: algorithm.hpp:88
static void from_uint32(Byte *buf, const boost::uint32_t n)
convert 32-bit unsigned integer into sequence of four bytes
Definition: algorithm.hpp:296
static boost::uint8_t to_uint8(const Byte *buf)
convert byte pointer into an 8-bit unsigned integer
Definition: algorithm.hpp:194
static boost::int64_t to_int64(const Byte *buf)
convert sequence of eight bytes to 64-bit signed integer
Definition: algorithm.hpp:248
static void from_double(Byte *ptr, const double n)
Definition: algorithm.hpp:375
static void from_int16(Byte *buf, const boost::int16_t n)
convert 16-bit signed integer into sequence of two bytes
Definition: algorithm.hpp:273
static boost::int8_t to_int8(const Byte *buf)
convert byte pointer into an 8-bit signed integer
Definition: algorithm.hpp:200
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
Definition: algorithm.hpp:98
static void from_long_double(Byte *ptr, const long double n)
Definition: algorithm.hpp:382
static boost::int32_t to_int24(T1 high, T2 mid, T3 low)
convert sequence of three bytes to 24-bit signed integer
Definition: algorithm.hpp:141
static void from_uint8(Byte *buf, const boost::uint8_t n)
convert 8-bit unsigned integer into sequence of one byte
Definition: algorithm.hpp:254