Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. static inline bool is_base64(unsigned char c) {
  2.    return (isalnum(c) || (c == '+') || (c == '/'));
  3. }
  4.  
  5. String base64_decode(String const& encoded_string) {
  6.    int in_len = encoded_string.length();
  7.    int i = 0;
  8.    int j = 0;
  9.    int in_ = 0;
  10.    unsigned char char_array_4[4], char_array_3[3];
  11.    String ret;
  12.  
  13.    while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  14.       char_array_4[i++] = encoded_string[in_]; in_++;
  15.       if (i == 4) {
  16.          for (i = 0; i <4; i++)
  17.             char_array_4 = (char)base64_chars.indexOf(char_array_4);
  18.  
  19.          char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  20.          char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  21.          char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  22.  
  23.          for (i = 0; (i < 3); i++)
  24.             ret += (char)char_array_3;
  25.          i = 0;
  26.       }
  27.    }
  28.  
  29.    if (i) {
  30.       for (j = 0; j < i; j++)
  31.          char_array_4[j] = (char)base64_chars.indexOf(char_array_4[j]);
  32.  
  33.       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  34.       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  35.  
  36.       for (j = 0; (j < i - 1); j++) ret += (char)char_array_3[j];
  37.    }
  38.  
  39.    return ret;
  40. }
  41.  
  42. Usage :
  43.  
  44. String Stringcontent = "base64encodedcontent"; // Feed the encoded base64 content here
  45. base64_decode(Stringcontent.c_str())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement