pion
5.0.6
|
00001 // --------------------------------------------------------------------- 00002 // pion: a Boost C++ framework for building lightweight HTTP interfaces 00003 // --------------------------------------------------------------------- 00004 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion) 00005 // 00006 // Distributed under the Boost Software License, Version 1.0. 00007 // See http://www.boost.org/LICENSE_1_0.txt 00008 // 00009 00010 #ifndef __PION_ALGORITHM_HEADER__ 00011 #define __PION_ALGORITHM_HEADER__ 00012 00013 #include <string> 00014 #include <boost/cstdint.hpp> 00015 #include <pion/config.hpp> 00016 00017 namespace pion { // begin namespace pion 00018 00019 struct PION_API algorithm { 00020 00027 static bool base64_decode(std::string const &input, std::string & output); 00028 00035 static bool base64_encode(std::string const &input, std::string & output); 00036 00038 static std::string url_decode(const std::string& str); 00039 00041 static std::string url_encode(const std::string& str); 00042 00044 //static std::string xml_decode(const std::string& str); 00045 00047 static std::string xml_encode(const std::string& str); 00048 00051 static void float_from_bytes(long double& value, const unsigned char *ptr, size_t num_exp_bits, size_t num_fraction_bits); 00052 00055 static void float_to_bytes(long double value, unsigned char *ptr, size_t num_exp_bits, size_t num_fraction_bits); 00056 00058 static inline boost::uint8_t to_uint8(unsigned char byte) { 00059 return boost::uint8_t(byte); 00060 } 00061 00063 static inline boost::int8_t to_int8(unsigned char byte) { 00064 return boost::int8_t(byte); 00065 } 00066 00068 static inline boost::uint8_t to_uint8(char byte) { 00069 return boost::uint8_t(byte); 00070 } 00071 00073 static inline boost::int8_t to_int8(char byte) { 00074 return boost::int8_t(byte); 00075 } 00076 00078 static inline boost::uint16_t to_uint16(unsigned char high, unsigned char low) { 00079 return (((boost::uint16_t)high) << 8) | ((boost::uint16_t)low); 00080 } 00081 00083 static inline boost::int16_t to_int16(unsigned char high, unsigned char low) { 00084 return (((boost::int16_t)high) << 8) | ((boost::int16_t)low); 00085 } 00086 00088 static inline boost::uint32_t to_uint24(unsigned char high, unsigned char mid, unsigned char low) { 00089 return (((boost::uint32_t)high) << 16) | (((boost::uint32_t)mid) << 8) | ((boost::uint32_t)low); 00090 } 00091 00093 static inline boost::int32_t to_int24(unsigned char high, unsigned char mid, unsigned char low) { 00094 return (((boost::int32_t)high) << 16) | (((boost::int32_t)mid) << 8) | ((boost::int32_t)low); 00095 } 00096 00098 static inline boost::uint32_t to_uint32(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char low) { 00099 return (((boost::uint32_t)high) << 24) | (((boost::uint32_t)mid1) << 16) | (((boost::uint32_t)mid2) << 8) | ((boost::uint32_t)low); 00100 } 00101 00103 static inline boost::int32_t to_int32(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char low) { 00104 return (((boost::int32_t)high) << 24) | (((boost::int32_t)mid1) << 16) | (((boost::int32_t)mid2) << 8) | ((boost::int32_t)low); 00105 } 00106 00108 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) { 00109 return (((boost::uint64_t)high) << 56) | (((boost::uint64_t)mid1) << 48) | (((boost::uint64_t)mid2) << 40) | (((boost::uint64_t)mid3) << 32) 00110 | (((boost::uint64_t)mid4) << 24) | (((boost::uint64_t)mid5) << 16) | (((boost::uint64_t)mid6) << 8) | ((boost::uint64_t)low); 00111 } 00112 00114 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) { 00115 return (((boost::int64_t)high) << 56) | (((boost::int64_t)mid1) << 48) | (((boost::int64_t)mid2) << 40) | (((boost::int64_t)mid3) << 32) 00116 | (((boost::int64_t)mid4) << 24) | (((boost::int64_t)mid5) << 16) | (((boost::int64_t)mid6) << 8) | ((boost::int64_t)low); 00117 } 00118 00120 template <typename T1, typename T2> 00121 static inline boost::uint16_t to_uint16(T1 high, T2 low) { 00122 return to_uint16(static_cast<unsigned char>(high), static_cast<unsigned char>(low)); 00123 } 00124 00126 template <typename T1, typename T2> 00127 static inline boost::int16_t to_int16(T1 high, T2 low) { 00128 return to_int16(static_cast<unsigned char>(high), static_cast<unsigned char>(low)); 00129 } 00130 00132 template <typename T1, typename T2, typename T3> 00133 static inline boost::uint32_t to_uint24(T1 high, T2 mid, T3 low) { 00134 return to_uint24(static_cast<unsigned char>(high), 00135 static_cast<unsigned char>(mid), 00136 static_cast<unsigned char>(low)); 00137 } 00138 00140 template <typename T1, typename T2, typename T3> 00141 static inline boost::int32_t to_int24(T1 high, T2 mid, T3 low) { 00142 return to_int24(static_cast<unsigned char>(high), 00143 static_cast<unsigned char>(mid), 00144 static_cast<unsigned char>(low)); 00145 } 00146 00148 template <typename T1, typename T2, typename T3, typename T4> 00149 static inline boost::uint32_t to_uint32(T1 high, T2 mid1, T3 mid2, T4 low) { 00150 return to_uint32(static_cast<unsigned char>(high), 00151 static_cast<unsigned char>(mid1), 00152 static_cast<unsigned char>(mid2), 00153 static_cast<unsigned char>(low)); 00154 } 00155 00157 template <typename T1, typename T2, typename T3, typename T4> 00158 static inline boost::int32_t to_int32(T1 high, T2 mid1, T3 mid2, T4 low) { 00159 return to_int32(static_cast<unsigned char>(high), 00160 static_cast<unsigned char>(mid1), 00161 static_cast<unsigned char>(mid2), 00162 static_cast<unsigned char>(low)); 00163 } 00164 00166 template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> 00167 static inline boost::uint64_t to_uint64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low) { 00168 return to_uint64(static_cast<unsigned char>(high), 00169 static_cast<unsigned char>(mid1), 00170 static_cast<unsigned char>(mid2), 00171 static_cast<unsigned char>(mid3), 00172 static_cast<unsigned char>(mid4), 00173 static_cast<unsigned char>(mid5), 00174 static_cast<unsigned char>(mid6), 00175 static_cast<unsigned char>(low)); 00176 } 00177 00179 template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> 00180 static inline boost::int64_t to_int64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low) { 00181 return to_int64(static_cast<unsigned char>(high), 00182 static_cast<unsigned char>(mid1), 00183 static_cast<unsigned char>(mid2), 00184 static_cast<unsigned char>(mid3), 00185 static_cast<unsigned char>(mid4), 00186 static_cast<unsigned char>(mid5), 00187 static_cast<unsigned char>(mid6), 00188 static_cast<unsigned char>(low)); 00189 } 00190 00191 00193 template <typename Byte> 00194 static inline boost::uint8_t to_uint8(const Byte *buf) { 00195 return to_uint8(buf[0]); 00196 } 00197 00199 template <typename Byte> 00200 static inline boost::int8_t to_int8(const Byte *buf) { 00201 return to_int8(buf[0]); 00202 } 00203 00205 template <typename Byte> 00206 static inline boost::uint16_t to_uint16(const Byte *buf) { 00207 return to_uint16(buf[0], buf[1]); 00208 } 00209 00211 template <typename Byte> 00212 static inline boost::int16_t to_int16(const Byte *buf) { 00213 return to_int16(buf[0], buf[1]); 00214 } 00215 00217 template <typename Byte> 00218 static inline boost::uint32_t to_uint24(const Byte *buf) { 00219 return to_uint24(buf[0], buf[1], buf[2]); 00220 } 00221 00223 template <typename Byte> 00224 static inline boost::int32_t to_int24(const Byte *buf) { 00225 return to_int24(buf[0], buf[1], buf[2]); 00226 } 00227 00229 template <typename Byte> 00230 static inline boost::uint32_t to_uint32(const Byte *buf) { 00231 return to_uint32(buf[0], buf[1], buf[2], buf[3]); 00232 } 00233 00235 template <typename Byte> 00236 static inline boost::int32_t to_int32(const Byte *buf) { 00237 return to_int32(buf[0], buf[1], buf[2], buf[3]); 00238 } 00239 00241 template <typename Byte> 00242 static inline boost::uint64_t to_uint64(const Byte *buf) { 00243 return to_uint64(buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); 00244 } 00245 00247 template <typename Byte> 00248 static inline boost::int64_t to_int64(const Byte *buf) { 00249 return to_int64(buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); 00250 } 00251 00253 template <typename Byte> 00254 static inline void from_uint8(Byte *buf, const boost::uint8_t n) { 00255 buf[0] = n & 0xFF; 00256 } 00257 00259 template <typename Byte> 00260 static inline void from_int8(Byte *buf, const boost::int8_t n) { 00261 buf[0] = n & 0xFF; 00262 } 00263 00265 template <typename Byte> 00266 static inline void from_uint16(Byte *buf, const boost::uint16_t n) { 00267 buf[0] = (n >> 8) & 0xFF; 00268 buf[1] = n & 0xFF; 00269 } 00270 00272 template <typename Byte> 00273 static inline void from_int16(Byte *buf, const boost::int16_t n) { 00274 buf[0] = (n >> 8) & 0xFF; 00275 buf[1] = n & 0xFF; 00276 } 00277 00279 template <typename Byte> 00280 static inline void from_uint24(Byte *buf, const boost::uint32_t n) { 00281 buf[0] = (n >> 16) & 0xFF; 00282 buf[1] = (n >> 8) & 0xFF; 00283 buf[2] = n & 0xFF; 00284 } 00285 00287 template <typename Byte> 00288 static inline void from_int24(Byte *buf, const boost::int32_t n) { 00289 buf[0] = (n >> 16) & 0xFF; 00290 buf[1] = (n >> 8) & 0xFF; 00291 buf[2] = n & 0xFF; 00292 } 00293 00295 template <typename Byte> 00296 static inline void from_uint32(Byte *buf, const boost::uint32_t n) { 00297 buf[0] = (n >> 24) & 0xFF; 00298 buf[1] = (n >> 16) & 0xFF; 00299 buf[2] = (n >> 8) & 0xFF; 00300 buf[3] = n & 0xFF; 00301 } 00302 00304 template <typename Byte> 00305 static inline void from_int32(Byte *buf, const boost::int32_t n) { 00306 buf[0] = (n >> 24) & 0xFF; 00307 buf[1] = (n >> 16) & 0xFF; 00308 buf[2] = (n >> 8) & 0xFF; 00309 buf[3] = n & 0xFF; 00310 } 00311 00313 template <typename Byte> 00314 static inline void from_uint64(Byte *buf, const boost::uint64_t n) { 00315 buf[0] = (n >> 56) & 0xFF; 00316 buf[1] = (n >> 48) & 0xFF; 00317 buf[2] = (n >> 40) & 0xFF; 00318 buf[3] = (n >> 32) & 0xFF; 00319 buf[4] = (n >> 24) & 0xFF; 00320 buf[5] = (n >> 16) & 0xFF; 00321 buf[6] = (n >> 8) & 0xFF; 00322 buf[7] = n & 0xFF; 00323 } 00324 00326 template <typename Byte> 00327 static inline void from_int64(Byte *buf, const boost::int64_t n) { 00328 buf[0] = (n >> 56) & 0xFF; 00329 buf[1] = (n >> 48) & 0xFF; 00330 buf[2] = (n >> 40) & 0xFF; 00331 buf[3] = (n >> 32) & 0xFF; 00332 buf[4] = (n >> 24) & 0xFF; 00333 buf[5] = (n >> 16) & 0xFF; 00334 buf[6] = (n >> 8) & 0xFF; 00335 buf[7] = n & 0xFF; 00336 } 00337 00340 template <typename Byte> 00341 static inline float to_float(const Byte *ptr) { 00342 long double value; 00343 float_from_bytes(value, (unsigned char *)ptr, 8U, 23U); 00344 return value; 00345 } 00346 00349 template <typename Byte> 00350 static inline double to_double(const Byte *ptr) { 00351 long double value; 00352 float_from_bytes(value, (unsigned char *)ptr, 11U, 52U); 00353 return value; 00354 } 00355 00358 template <typename Byte> 00359 static inline long double to_long_double(const Byte *ptr) { 00360 long double value; 00361 float_from_bytes(value, (unsigned char *)ptr, 15U, 112U); 00362 return value; 00363 } 00364 00367 template <typename Byte> 00368 static inline void from_float(Byte *ptr, const float n) { 00369 float_to_bytes(n, (unsigned char*)ptr, 8U, 23U); 00370 } 00371 00374 template <typename Byte> 00375 static inline void from_double(Byte *ptr, const double n) { 00376 float_to_bytes(n, (unsigned char*)ptr, 11U, 52U); 00377 } 00378 00381 template <typename Byte> 00382 static inline void from_long_double(Byte *ptr, const long double n) { 00383 float_to_bytes(n, (unsigned char*)ptr, 15U, 112U); 00384 } 00385 }; 00386 00387 } // end namespace pion 00388 00389 #endif