00001
00002
00003
00004
00005
00006
00007
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 {
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
00045 static void float_from_bytes(long double& value, const unsigned char *ptr, size_t num_exp_bits, size_t num_fraction_bits);
00046
00049 static void float_to_bytes(long double value, unsigned char *ptr, size_t num_exp_bits, size_t num_fraction_bits);
00050
00052 static inline boost::uint8_t to_uint8(unsigned char byte) {
00053 return boost::uint8_t(byte);
00054 }
00055
00057 static inline boost::int8_t to_int8(unsigned char byte) {
00058 return boost::int8_t(byte);
00059 }
00060
00062 static inline boost::uint8_t to_uint8(char byte) {
00063 return boost::uint8_t(byte);
00064 }
00065
00067 static inline boost::int8_t to_int8(char byte) {
00068 return boost::int8_t(byte);
00069 }
00070
00072 static inline boost::uint16_t to_uint16(unsigned char high, unsigned char low) {
00073 return (((boost::uint16_t)high) << 8) | ((boost::uint16_t)low);
00074 }
00075
00077 static inline boost::int16_t to_int16(unsigned char high, unsigned char low) {
00078 return (((boost::int16_t)high) << 8) | ((boost::int16_t)low);
00079 }
00080
00082 static inline boost::uint32_t to_uint24(unsigned char high, unsigned char mid, unsigned char low) {
00083 return (((boost::uint32_t)high) << 16) | (((boost::uint32_t)mid) << 8) | ((boost::uint32_t)low);
00084 }
00085
00087 static inline boost::int32_t to_int24(unsigned char high, unsigned char mid, unsigned char low) {
00088 return (((boost::int32_t)high) << 16) | (((boost::int32_t)mid) << 8) | ((boost::int32_t)low);
00089 }
00090
00092 static inline boost::uint32_t to_uint32(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char low) {
00093 return (((boost::uint32_t)high) << 24) | (((boost::uint32_t)mid1) << 16) | (((boost::uint32_t)mid2) << 8) | ((boost::uint32_t)low);
00094 }
00095
00097 static inline boost::int32_t to_int32(unsigned char high, unsigned char mid1, unsigned char mid2, unsigned char low) {
00098 return (((boost::int32_t)high) << 24) | (((boost::int32_t)mid1) << 16) | (((boost::int32_t)mid2) << 8) | ((boost::int32_t)low);
00099 }
00100
00102 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) {
00103 return (((boost::uint64_t)high) << 56) | (((boost::uint64_t)mid1) << 48) | (((boost::uint64_t)mid2) << 40) | (((boost::uint64_t)mid3) << 32)
00104 | (((boost::uint64_t)mid4) << 24) | (((boost::uint64_t)mid5) << 16) | (((boost::uint64_t)mid6) << 8) | ((boost::uint64_t)low);
00105 }
00106
00108 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) {
00109 return (((boost::int64_t)high) << 56) | (((boost::int64_t)mid1) << 48) | (((boost::int64_t)mid2) << 40) | (((boost::int64_t)mid3) << 32)
00110 | (((boost::int64_t)mid4) << 24) | (((boost::int64_t)mid5) << 16) | (((boost::int64_t)mid6) << 8) | ((boost::int64_t)low);
00111 }
00112
00114 template <typename T1, typename T2>
00115 static inline boost::uint16_t to_uint16(T1 high, T2 low) {
00116 return to_uint16(static_cast<unsigned char>(high), static_cast<unsigned char>(low));
00117 }
00118
00120 template <typename T1, typename T2>
00121 static inline boost::int16_t to_int16(T1 high, T2 low) {
00122 return to_int16(static_cast<unsigned char>(high), static_cast<unsigned char>(low));
00123 }
00124
00126 template <typename T1, typename T2, typename T3>
00127 static inline boost::uint32_t to_uint24(T1 high, T2 mid, T3 low) {
00128 return to_uint24(static_cast<unsigned char>(high),
00129 static_cast<unsigned char>(mid),
00130 static_cast<unsigned char>(low));
00131 }
00132
00134 template <typename T1, typename T2, typename T3>
00135 static inline boost::int32_t to_int24(T1 high, T2 mid, T3 low) {
00136 return to_int24(static_cast<unsigned char>(high),
00137 static_cast<unsigned char>(mid),
00138 static_cast<unsigned char>(low));
00139 }
00140
00142 template <typename T1, typename T2, typename T3, typename T4>
00143 static inline boost::uint32_t to_uint32(T1 high, T2 mid1, T3 mid2, T4 low) {
00144 return to_uint32(static_cast<unsigned char>(high),
00145 static_cast<unsigned char>(mid1),
00146 static_cast<unsigned char>(mid2),
00147 static_cast<unsigned char>(low));
00148 }
00149
00151 template <typename T1, typename T2, typename T3, typename T4>
00152 static inline boost::int32_t to_int32(T1 high, T2 mid1, T3 mid2, T4 low) {
00153 return to_int32(static_cast<unsigned char>(high),
00154 static_cast<unsigned char>(mid1),
00155 static_cast<unsigned char>(mid2),
00156 static_cast<unsigned char>(low));
00157 }
00158
00160 template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
00161 static inline boost::uint64_t to_uint64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low) {
00162 return to_uint64(static_cast<unsigned char>(high),
00163 static_cast<unsigned char>(mid1),
00164 static_cast<unsigned char>(mid2),
00165 static_cast<unsigned char>(mid3),
00166 static_cast<unsigned char>(mid4),
00167 static_cast<unsigned char>(mid5),
00168 static_cast<unsigned char>(mid6),
00169 static_cast<unsigned char>(low));
00170 }
00171
00173 template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
00174 static inline boost::int64_t to_int64(T1 high, T2 mid1, T3 mid2, T4 mid3, T5 mid4, T6 mid5, T7 mid6, T8 low) {
00175 return to_int64(static_cast<unsigned char>(high),
00176 static_cast<unsigned char>(mid1),
00177 static_cast<unsigned char>(mid2),
00178 static_cast<unsigned char>(mid3),
00179 static_cast<unsigned char>(mid4),
00180 static_cast<unsigned char>(mid5),
00181 static_cast<unsigned char>(mid6),
00182 static_cast<unsigned char>(low));
00183 }
00184
00185
00187 template <typename Byte>
00188 static inline boost::uint8_t to_uint8(const Byte *buf) {
00189 return to_uint8(buf[0]);
00190 }
00191
00193 template <typename Byte>
00194 static inline boost::int8_t to_int8(const Byte *buf) {
00195 return to_int8(buf[0]);
00196 }
00197
00199 template <typename Byte>
00200 static inline boost::uint16_t to_uint16(const Byte *buf) {
00201 return to_uint16(buf[0], buf[1]);
00202 }
00203
00205 template <typename Byte>
00206 static inline boost::int16_t to_int16(const Byte *buf) {
00207 return to_int16(buf[0], buf[1]);
00208 }
00209
00211 template <typename Byte>
00212 static inline boost::uint32_t to_uint24(const Byte *buf) {
00213 return to_uint24(buf[0], buf[1], buf[2]);
00214 }
00215
00217 template <typename Byte>
00218 static inline boost::int32_t to_int24(const Byte *buf) {
00219 return to_int24(buf[0], buf[1], buf[2]);
00220 }
00221
00223 template <typename Byte>
00224 static inline boost::uint32_t to_uint32(const Byte *buf) {
00225 return to_uint32(buf[0], buf[1], buf[2], buf[3]);
00226 }
00227
00229 template <typename Byte>
00230 static inline boost::int32_t to_int32(const Byte *buf) {
00231 return to_int32(buf[0], buf[1], buf[2], buf[3]);
00232 }
00233
00235 template <typename Byte>
00236 static inline boost::uint64_t to_uint64(const Byte *buf) {
00237 return to_uint64(buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
00238 }
00239
00241 template <typename Byte>
00242 static inline boost::int64_t to_int64(const Byte *buf) {
00243 return to_int64(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 void from_uint8(Byte *buf, const boost::uint8_t n) {
00249 buf[0] = n & 0xFF;
00250 }
00251
00253 template <typename Byte>
00254 static inline void from_int8(Byte *buf, const boost::int8_t n) {
00255 buf[0] = n & 0xFF;
00256 }
00257
00259 template <typename Byte>
00260 static inline void from_uint16(Byte *buf, const boost::uint16_t n) {
00261 buf[0] = (n >> 8) & 0xFF;
00262 buf[1] = n & 0xFF;
00263 }
00264
00266 template <typename Byte>
00267 static inline void from_int16(Byte *buf, const boost::int16_t n) {
00268 buf[0] = (n >> 8) & 0xFF;
00269 buf[1] = n & 0xFF;
00270 }
00271
00273 template <typename Byte>
00274 static inline void from_uint24(Byte *buf, const boost::uint32_t n) {
00275 buf[0] = (n >> 16) & 0xFF;
00276 buf[1] = (n >> 8) & 0xFF;
00277 buf[2] = n & 0xFF;
00278 }
00279
00281 template <typename Byte>
00282 static inline void from_int24(Byte *buf, const boost::int32_t n) {
00283 buf[0] = (n >> 16) & 0xFF;
00284 buf[1] = (n >> 8) & 0xFF;
00285 buf[2] = n & 0xFF;
00286 }
00287
00289 template <typename Byte>
00290 static inline void from_uint32(Byte *buf, const boost::uint32_t n) {
00291 buf[0] = (n >> 24) & 0xFF;
00292 buf[1] = (n >> 16) & 0xFF;
00293 buf[2] = (n >> 8) & 0xFF;
00294 buf[3] = n & 0xFF;
00295 }
00296
00298 template <typename Byte>
00299 static inline void from_int32(Byte *buf, const boost::int32_t n) {
00300 buf[0] = (n >> 24) & 0xFF;
00301 buf[1] = (n >> 16) & 0xFF;
00302 buf[2] = (n >> 8) & 0xFF;
00303 buf[3] = n & 0xFF;
00304 }
00305
00307 template <typename Byte>
00308 static inline void from_uint64(Byte *buf, const boost::uint64_t n) {
00309 buf[0] = (n >> 56) & 0xFF;
00310 buf[1] = (n >> 48) & 0xFF;
00311 buf[2] = (n >> 40) & 0xFF;
00312 buf[3] = (n >> 32) & 0xFF;
00313 buf[4] = (n >> 24) & 0xFF;
00314 buf[5] = (n >> 16) & 0xFF;
00315 buf[6] = (n >> 8) & 0xFF;
00316 buf[7] = n & 0xFF;
00317 }
00318
00320 template <typename Byte>
00321 static inline void from_int64(Byte *buf, const boost::int64_t n) {
00322 buf[0] = (n >> 56) & 0xFF;
00323 buf[1] = (n >> 48) & 0xFF;
00324 buf[2] = (n >> 40) & 0xFF;
00325 buf[3] = (n >> 32) & 0xFF;
00326 buf[4] = (n >> 24) & 0xFF;
00327 buf[5] = (n >> 16) & 0xFF;
00328 buf[6] = (n >> 8) & 0xFF;
00329 buf[7] = n & 0xFF;
00330 }
00331
00334 template <typename Byte>
00335 static inline float to_float(const Byte *ptr) {
00336 long double value;
00337 float_from_bytes(value, (unsigned char *)ptr, 8U, 23U);
00338 return value;
00339 }
00340
00343 template <typename Byte>
00344 static inline double to_double(const Byte *ptr) {
00345 long double value;
00346 float_from_bytes(value, (unsigned char *)ptr, 11U, 52U);
00347 return value;
00348 }
00349
00352 template <typename Byte>
00353 static inline long double to_long_double(const Byte *ptr) {
00354 long double value;
00355 float_from_bytes(value, (unsigned char *)ptr, 15U, 112U);
00356 return value;
00357 }
00358
00361 template <typename Byte>
00362 static inline void from_float(Byte *ptr, const float n) {
00363 float_to_bytes(n, (unsigned char*)ptr, 8U, 23U);
00364 }
00365
00368 template <typename Byte>
00369 static inline void from_double(Byte *ptr, const double n) {
00370 float_to_bytes(n, (unsigned char*)ptr, 11U, 52U);
00371 }
00372
00375 template <typename Byte>
00376 static inline void from_long_double(Byte *ptr, const long double n) {
00377 float_to_bytes(n, (unsigned char*)ptr, 15U, 112U);
00378 }
00379 };
00380
00381 }
00382
00383 #endif