base64.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. namespace nall::Encode {
  3. inline auto Base64(const void* vdata, uint size, const string& format = "MIME") -> string {
  4. static bool initialized = false;
  5. static char lookup[65] = {0};
  6. if(!initialized) {
  7. initialized = true;
  8. for(uint n : range(26)) lookup[n + 0] = 'A' + n;
  9. for(uint n : range(26)) lookup[n + 26] = 'a' + n;
  10. for(uint n : range(10)) lookup[n + 52] = '0' + n;
  11. }
  12. if(format == "MIME") {
  13. lookup[62] = '+';
  14. lookup[63] = '/';
  15. lookup[64] = '=';
  16. } else if(format == "URI") {
  17. lookup[62] = '-';
  18. lookup[63] = '_';
  19. lookup[64] = 0;
  20. } else return "";
  21. auto data = (const uint8_t*)vdata;
  22. uint overflow = (3 - (size % 3)) % 3; //bytes to round to nearest multiple of 3
  23. string result;
  24. uint8_t buffer;
  25. for(uint n : range(size)) {
  26. switch(n % 3) {
  27. case 0:
  28. buffer = data[n] >> 2;
  29. result.append(lookup[buffer]);
  30. buffer = (data[n] & 3) << 4;
  31. break;
  32. case 1:
  33. buffer |= data[n] >> 4;
  34. result.append(lookup[buffer]);
  35. buffer = (data[n] & 15) << 2;
  36. break;
  37. case 2:
  38. buffer |= data[n] >> 6;
  39. result.append(lookup[buffer]);
  40. buffer = (data[n] & 63);
  41. result.append(lookup[buffer]);
  42. break;
  43. }
  44. }
  45. if(overflow) result.append(lookup[buffer]);
  46. if(lookup[64]) {
  47. while(result.size() % 4) result.append(lookup[64]);
  48. }
  49. return result;
  50. }
  51. inline auto Base64(const vector<uint8_t>& buffer, const string& format = "MIME") -> string {
  52. return Base64(buffer.data(), buffer.size(), format);
  53. }
  54. inline auto Base64(const string& text, const string& format = "MIME") -> string {
  55. return Base64(text.data(), text.size(), format);
  56. }
  57. }