MyBase64.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "stdafx.h"
  2. #include "MyBase64.h"
  3. namespace MyBase64{
  4. void Encode(const string plain,string& encoded)
  5. {
  6. unsigned int bytes = plain.size();
  7. unsigned int outSz = (bytes + 3 - 1) / 3 * 4;
  8. outSz += (outSz + pemLineSz - 1) / pemLineSz; // new lines
  9. unsigned int index = 0;
  10. while (bytes > 2) {
  11. unsigned char b1 = plain[index++];
  12. unsigned char b2 = plain[index++];
  13. unsigned char b3 = plain[index++];
  14. // encoded idx
  15. unsigned char e1 = b1 >> 2;
  16. unsigned char e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
  17. unsigned char e3 = ((b2 & 0xF) << 2) | (b3 >> 6);
  18. unsigned char e4 = b3 & 0x3F;
  19. // store
  20. encoded.append(1,base64Encode[e1]);
  21. encoded.append(1,base64Encode[e2]);
  22. encoded.append(1,base64Encode[e3]);
  23. encoded.append(1,base64Encode[e4]);
  24. bytes -= 3;
  25. //enter anther row
  26. /* if ((++j % 16) == 0 && bytes)
  27. encoded.append(1,'\n');*/
  28. }
  29. // last integral
  30. if (bytes) {
  31. bool twoBytes = (bytes == 2);
  32. unsigned char b1 = plain[index++];
  33. unsigned char b2 = (twoBytes) ? plain[index++]: 0;
  34. unsigned char e1 = b1 >> 2;
  35. unsigned char e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
  36. unsigned char e3 = (b2 & 0xF) << 2;
  37. encoded.append(1,base64Encode[e1]);
  38. encoded.append(1,base64Encode[e2]);
  39. encoded.append(1,(twoBytes) ? base64Encode[e3] : pad);
  40. encoded.append(1,pad);
  41. }
  42. encoded.append(1,'\0');
  43. }
  44. void Decode(const string coded,string& decoded)
  45. {
  46. unsigned int bytes = coded.size();
  47. unsigned int plainSz = bytes - ((bytes + (pemLineSz - 1)) / pemLineSz);
  48. plainSz = (plainSz * 3 + 3) / 4;
  49. //decoded_.New(plainSz);
  50. unsigned int i = 0;
  51. unsigned int j = 0;
  52. unsigned int index = 0;
  53. while (bytes > 3) {
  54. unsigned char e1 = coded[index++];
  55. unsigned char e2 = coded[index++];
  56. unsigned char e3 = coded[index++];
  57. unsigned char e4 = coded[index++];
  58. // do asserts first
  59. if (e1 == 0) // end file 0's
  60. break;
  61. bool pad3 = false;
  62. bool pad4 = false;
  63. if (e3 == pad)
  64. pad3 = true;
  65. if (e4 == pad)
  66. pad4 = true;
  67. e1 = base64Decode[e1 - 0x2B];
  68. e2 = base64Decode[e2 - 0x2B];
  69. e3 = (e3 == pad) ? 0 : base64Decode[e3 - 0x2B];
  70. e4 = (e4 == pad) ? 0 : base64Decode[e4 - 0x2B];
  71. unsigned char b1 = (e1 << 2) | (e2 >> 4);
  72. unsigned char b2 = ((e2 & 0xF) << 4) | (e3 >> 2);
  73. unsigned char b3 = ((e3 & 0x3) << 6) | e4;
  74. decoded.append(1,b1);
  75. if (!pad3)
  76. decoded.append(1,b2);
  77. if (!pad4)
  78. decoded.append(1,b3);
  79. else
  80. break;
  81. bytes -= 4;
  82. //if ((++j % 16) == 0) {
  83. // unsigned char endLine = coded[index++];//coded_.next();
  84. // bytes--;
  85. // while (endLine == ' ') { // remove possible whitespace
  86. // endLine = coded[index++];
  87. // bytes--;
  88. // }
  89. // if (endLine == '\r') {
  90. // endLine = coded[index++];
  91. // bytes--;
  92. // }
  93. // if (endLine != '\n') {
  94. // return;
  95. // }
  96. //}
  97. }
  98. }
  99. }