Base64.cpp 3.8 KB

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