Base64Encoder.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // Base64Encoder.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Base64Encoder.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: Base64
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Base64Encoder.h"
  16. namespace Poco {
  17. const unsigned char Base64EncoderBuf::OUT_ENCODING[64] =
  18. {
  19. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  20. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  21. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  22. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  23. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  24. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  25. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  26. '4', '5', '6', '7', '8', '9', '+', '/'
  27. };
  28. Base64EncoderBuf::Base64EncoderBuf(std::ostream& ostr):
  29. _groupLength(0),
  30. _pos(0),
  31. _lineLength(72),
  32. _buf(*ostr.rdbuf())
  33. {
  34. }
  35. Base64EncoderBuf::~Base64EncoderBuf()
  36. {
  37. try
  38. {
  39. close();
  40. }
  41. catch (...)
  42. {
  43. }
  44. }
  45. void Base64EncoderBuf::setLineLength(int lineLength)
  46. {
  47. _lineLength = lineLength;
  48. }
  49. int Base64EncoderBuf::getLineLength() const
  50. {
  51. return _lineLength;
  52. }
  53. int Base64EncoderBuf::writeToDevice(char c)
  54. {
  55. static const int eof = std::char_traits<char>::eof();
  56. _group[_groupLength++] = (unsigned char) c;
  57. if (_groupLength == 3)
  58. {
  59. unsigned char idx;
  60. idx = _group[0] >> 2;
  61. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  62. idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
  63. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  64. idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
  65. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  66. idx = _group[2] & 0x3F;
  67. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  68. _pos += 4;
  69. if (_lineLength > 0 && _pos >= _lineLength)
  70. {
  71. if (_buf.sputc('\r') == eof) return eof;
  72. if (_buf.sputc('\n') == eof) return eof;
  73. _pos = 0;
  74. }
  75. _groupLength = 0;
  76. }
  77. return charToInt(c);
  78. }
  79. int Base64EncoderBuf::close()
  80. {
  81. static const int eof = std::char_traits<char>::eof();
  82. if (sync() == eof) return eof;
  83. if (_groupLength == 1)
  84. {
  85. _group[1] = 0;
  86. unsigned char idx;
  87. idx = _group[0] >> 2;
  88. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  89. idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
  90. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  91. if (_buf.sputc('=') == eof) return eof;
  92. if (_buf.sputc('=') == eof) return eof;
  93. }
  94. else if (_groupLength == 2)
  95. {
  96. _group[2] = 0;
  97. unsigned char idx;
  98. idx = _group[0] >> 2;
  99. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  100. idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
  101. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  102. idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
  103. if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
  104. if (_buf.sputc('=') == eof) return eof;
  105. }
  106. _groupLength = 0;
  107. return _buf.pubsync();
  108. }
  109. Base64EncoderIOS::Base64EncoderIOS(std::ostream& ostr): _buf(ostr)
  110. {
  111. poco_ios_init(&_buf);
  112. }
  113. Base64EncoderIOS::~Base64EncoderIOS()
  114. {
  115. }
  116. int Base64EncoderIOS::close()
  117. {
  118. return _buf.close();
  119. }
  120. Base64EncoderBuf* Base64EncoderIOS::rdbuf()
  121. {
  122. return &_buf;
  123. }
  124. Base64Encoder::Base64Encoder(std::ostream& ostr): Base64EncoderIOS(ostr), std::ostream(&_buf)
  125. {
  126. }
  127. Base64Encoder::~Base64Encoder()
  128. {
  129. }
  130. } // namespace Poco