//---------------------------------------------------------------------------- // EgalTech 2014-2015 //---------------------------------------------------------------------------- // File : EgtBase64.h Data : 28.07.15 Versione : 1.6g8 // Contenuto : Funzioni per codifica/decodifica Base64. // // // // Modifiche : 10.09.14 DS Creazione modulo. // // //---------------------------------------------------------------------------- #pragma once #include //--------------------- Base64 Encoding/Decoding Tables ----------------------- static char EncB64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ; static char DecB64[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq" ; //----------------------------------------------------------------------------- // encodeblock - encode 3 8-bit binary bytes as 4 '6-bit' characters //----------------------------------------------------------------------------- inline void encodeblock( unsigned char in[], std::string& sB64Dest, int len) { sB64Dest.push_back( EncB64[ in[0] >> 2 ]) ; sB64Dest.push_back( EncB64[ ((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4) ]) ; sB64Dest.push_back( (unsigned char) (len > 1 ? EncB64[ ((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6) ] : '=')) ; sB64Dest.push_back( (unsigned char) (len > 2 ? EncB64[ in[2] & 0x3f ] : '=')) ; } //----------------------------------------------------------------------------- // encode - base64 encode a stream, adding padding if needed //----------------------------------------------------------------------------- inline bool B64Encode( const std::string& sSou, std::string& sB64Dest) { // sorgente e dstinazione devono essere diversi if ( &sSou == &sB64Dest) return false ; // eseguo conversione sB64Dest.clear() ; sB64Dest.reserve( (int) ( ceil( sSou.length() / 3.) * 4)) ; unsigned char in[3] ; for ( size_t j = 0 ; j < sSou.length() ;) { int len = 0 ; for ( size_t i = 0 ; i < 3 ; i++) { if ( j < sSou.length()) { in[i] = (unsigned char) sSou[j] ; ++ len ; ++ j ; } else in[i] = 0 ; } if ( len > 0) encodeblock( in, sB64Dest, len) ; } return true ; } //----------------------------------------------------------------------------- // decodeblock - decode 4 '6-bit' characters into 3 8-bit binary bytes //----------------------------------------------------------------------------- inline void decodeblock( char in[], std::string& sDest) { sDest.push_back( ( in[0] << 2 | in[1] >> 4)) ; sDest.push_back( ( in[1] << 4 | in[2] >> 2)) ; sDest.push_back( ( in[2] << 6 | in[3] >> 0)) ; } //----------------------------------------------------------------------------- inline bool B64Decode( const std::string& sB64Sou, std::string& sDest) { // sorgente e dstinazione devono essere diversi if ( &sB64Sou == &sDest) return false ; // eseguo conversione sDest.clear() ; sDest.reserve( sB64Sou.length()) ; int phase = 0 ; char in[4] = { '\0', '\0', '\0', '\0'} ; for ( size_t i = 0 ; i < sB64Sou.length() ; ++ i) { int c = (int) sB64Sou[i] ; if ( c == '=') { decodeblock( in, sDest) ; if ( in[3] == '\0') sDest.pop_back() ; if ( in[2] == '\0') sDest.pop_back() ; break ; } int nVal ; char* p = strchr( EncB64, c) ; if ( p != nullptr) nVal = char( p - EncB64) ; in[phase] = (( c < 43 || c > 122) ? -1 : (int) DecB64[ c - 43]) ; if ( in[phase] != 0 ) in[phase] = ((in[phase] == (int)'$') ? -1 : in[phase] - 62) ; if ( in[phase] != -1 ) { phase = (phase + 1) % 4 ; if ( phase == 0) { decodeblock( in, sDest) ; in[0] = in[1] = in[2] = in[3] = '\0' ; } } } return true ; }