Simplified distributed block storage with strong consistency, like in Ceph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

56 lines
1.3 KiB

  1. // Copyright (c) Vitaliy Filippov, 2019+
  2. // License: VNPL-1.0 (see README.md for details)
  3. #include "base64.h"
  4. std::string base64_encode(const std::string &in)
  5. {
  6. std::string out;
  7. unsigned val = 0;
  8. int valb = -6;
  9. for (unsigned char c: in)
  10. {
  11. val = (val << 8) + c;
  12. valb += 8;
  13. while (valb >= 0)
  14. {
  15. out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(val>>valb) & 0x3F]);
  16. valb -= 6;
  17. }
  18. }
  19. if (valb > -6)
  20. out.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((val<<8)>>(valb+8)) & 0x3F]);
  21. while (out.size() % 4)
  22. out.push_back('=');
  23. return out;
  24. }
  25. static char T[256] = { 0 };
  26. std::string base64_decode(const std::string &in)
  27. {
  28. std::string out;
  29. if (T[0] == 0)
  30. {
  31. for (int i = 0; i < 256; i++)
  32. T[i] = -1;
  33. for (int i = 0; i < 64; i++)
  34. T[(unsigned char)("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i])] = i;
  35. }
  36. unsigned val = 0;
  37. int valb = -8;
  38. for (unsigned char c: in)
  39. {
  40. if (T[c] == -1)
  41. break;
  42. val = (val<<6) + T[c];
  43. valb += 6;
  44. if (valb >= 0)
  45. {
  46. out.push_back(char((val >> valb) & 0xFF));
  47. valb -= 8;
  48. }
  49. }
  50. return out;
  51. }