FlyFar

src/base64.h

Aug 24th, 2023
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | Cybersecurity | 0 0
  1. #ifndef BASE64
  2. #define BASE64
  3.  
  4. #include "Claw.h"
  5. #include <string>
  6. /*
  7. BASE 64 Encoding and Decoding
  8. Original : https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
  9. */
  10. static const std::string base64_chars =
  11.              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  12.              "abcdefghijklmnopqrstuvwxyz"
  13.              "0123456789+/";
  14.  
  15.  
  16. static inline bool is_base64(unsigned char c) {
  17.   return (isalnum(c) || (c == '+') || (c == '/'));
  18. }
  19.  
  20. std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
  21.   std::string ret;
  22.   int i = 0;
  23.   int j = 0;
  24.   unsigned char char_array_3[3];
  25.   unsigned char char_array_4[4];
  26.  
  27.   while (in_len--) {
  28.     char_array_3[i++] = *(bytes_to_encode++);
  29.     if (i == 3) {
  30.       char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  31.       char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  32.       char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  33.       char_array_4[3] = char_array_3[2] & 0x3f;
  34.  
  35.       for(i = 0; (i <4) ; i++)
  36.         ret += base64_chars[char_array_4[i]];
  37.       i = 0;
  38.     }
  39.   }
  40.  
  41.   if (i)
  42.   {
  43.     for(j = i; j < 3; j++)
  44.       char_array_3[j] = '\0';
  45.  
  46.     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  47.     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  48.     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  49.     char_array_4[3] = char_array_3[2] & 0x3f;
  50.  
  51.     for (j = 0; (j < i + 1); j++)
  52.       ret += base64_chars[char_array_4[j]];
  53.  
  54.     while((i++ < 3))
  55.       ret += '=';
  56.  
  57.   }
  58.  
  59.   return ret;
  60.  
  61. }
  62. std::string base64_decode(std::string const& encoded_string) {
  63.   int in_len = encoded_string.size();
  64.   int i = 0;
  65.   int j = 0;
  66.   int in_ = 0;
  67.   unsigned char char_array_4[4], char_array_3[3];
  68.   std::string ret;
  69.  
  70.   while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  71.     char_array_4[i++] = encoded_string[in_]; in_++;
  72.     if (i ==4) {
  73.       for (i = 0; i <4; i++)
  74.         char_array_4[i] = base64_chars.find(char_array_4[i]);
  75.  
  76.       char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  77.       char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  78.       char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  79.  
  80.       for (i = 0; (i < 3); i++)
  81.         ret += char_array_3[i];
  82.       i = 0;
  83.     }
  84.   }
  85.  
  86.   if (i) {
  87.     for (j = i; j <4; j++)
  88.       char_array_4[j] = 0;
  89.  
  90.     for (j = 0; j <4; j++)
  91.       char_array_4[j] = base64_chars.find(char_array_4[j]);
  92.  
  93.     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  94.     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  95.     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  96.  
  97.     for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  98.   }
  99.  
  100.   return ret;
  101. }
  102.  
  103. #endif // ! BASE64
Add Comment
Please, Sign In to add comment