Base64.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "Base64.h"
  2. using namespace Framework;
  3. static Framework::Text base64_chars =
  4. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  5. "abcdefghijklmnopqrstuvwxyz"
  6. "0123456789+/";
  7. Text Framework::base64Encode(const char* data, int length)
  8. {
  9. Text result;
  10. int i = 0;
  11. int j = 0;
  12. unsigned char char_array_3[3];
  13. unsigned char char_array_4[4];
  14. while (length--)
  15. {
  16. char_array_3[i++] = *(data++);
  17. if (i == 3)
  18. {
  19. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  20. char_array_4[1] = (unsigned char)(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
  21. char_array_4[2] = (unsigned char)(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
  22. char_array_4[3] = char_array_3[2] & 0x3f;
  23. for (i = 0; (i < 4); i++)
  24. result.append((char)base64_chars[char_array_4[i]]);
  25. i = 0;
  26. }
  27. }
  28. if (i)
  29. {
  30. for (j = i; j < 3; j++)
  31. char_array_3[j] = '\0';
  32. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  33. char_array_4[1] = (unsigned char)(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
  34. char_array_4[2] = (unsigned char)(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
  35. char_array_4[3] = char_array_3[2] & 0x3f;
  36. for (j = 0; (j < i + 1); j++)
  37. result.append((char)base64_chars[char_array_4[j]]);
  38. while ((i++ < 3))
  39. result.append('=');
  40. }
  41. return result;
  42. }
  43. bool Framework::base64Decode(Text base64, char** data, int* length)
  44. {
  45. int i = 0;
  46. int j = 0;
  47. int k = 0;
  48. int l = 0;
  49. int n = 0;
  50. unsigned char char_array_4[4];
  51. unsigned char char_array_3[3];
  52. *length = 0;
  53. while (base64[l])
  54. {
  55. if (base64[l] == '=')
  56. break;
  57. if (!base64_chars.hat(base64[l]))
  58. return false;
  59. l++;
  60. }
  61. *length = (l * 3) / 4;
  62. *data = new char[*length + 1];
  63. while (base64[k])
  64. {
  65. char_array_4[i++] = base64[k];
  66. if (i == 4)
  67. {
  68. for (i = 0; i < 4; i++)
  69. char_array_4[i] = (unsigned char)(base64_chars.positionVon(char_array_4[i]));
  70. char_array_3[0] = (unsigned char)((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
  71. char_array_3[1] = (unsigned char)(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
  72. char_array_3[2] = (unsigned char)(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
  73. for (i = 0; (i < 3) && n < *length; i++)
  74. (*data)[n++] = char_array_3[i];
  75. i = 0;
  76. }
  77. k++;
  78. }
  79. if (i)
  80. {
  81. for (j = i; j < 4; j++)
  82. char_array_4[j] = 0;
  83. for (j = 0; j < 4; j++)
  84. char_array_4[j] = (unsigned char)(base64_chars.positionVon(char_array_4[j]));
  85. char_array_3[0] = (unsigned char)((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
  86. char_array_3[1] = (unsigned char)(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
  87. char_array_3[2] = (unsigned char)(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);
  88. for (j = 0; (j < i - 1) && n < *length; j++)
  89. (*data)[n++] = char_array_3[j];
  90. }
  91. (*data)[*length] = '\0';
  92. return true;
  93. }