8#ifndef INCLUDED_BDLDE_BASE64ENCODER
9#define INCLUDED_BDLDE_BASE64ENCODER
474#include <bdlscm_version.h>
483#include <bsl_cstddef.h>
484#include <bsl_limits.h>
521 const int d_maxLineLength;
526 const char *
const d_alphabet_p;
529 const bool d_isPadded;
542 bsl::size_t lengthWithoutCrlfs(
const EncoderOptions&
options,
543 bsl::size_t inputLength);
552 template <
class OUTPUT_ITERATOR>
553 void append(OUTPUT_ITERATOR *out,
char character,
int maxLength);
561 template <
class OUTPUT_ITERATOR>
562 void encode(OUTPUT_ITERATOR *out,
int maxLength);
565 void setState(State newState);
573 bool isResidualOutput(
int numBytes)
const;
587 bsl::size_t inputLength);
591 "use overload with 'options'")
625 bsl::
size_t inputLength);
724 template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
725 int convert(OUTPUT_ITERATOR out,
726 INPUT_ITERATOR begin,
728 template <class OUTPUT_ITERATOR, class INPUT_ITERATOR>
729 int convert(OUTPUT_ITERATOR out,
732 INPUT_ITERATOR begin,
748 template <class OUTPUT_ITERATOR>
750 template <class OUTPUT_ITERATOR>
751 int endConvert(OUTPUT_ITERATOR out,
int *numOut,
int maxNumOut = -1);
810 bsl::
size_t inputLength)
812 static const bsl::size_t maxSize_t = bsl::numeric_limits<
816 if (0 == inputLength) {
820 const bsl::size_t numTripletsRoundedDown = (inputLength + 2) / 3 - 1;
821 const bsl::size_t numResidual = inputLength - numTripletsRoundedDown * 3;
829 BSLS_ASSERT(numTripletsRoundedDown <= (maxSize_t - pad) / 4);
831 return numTripletsRoundedDown * 4 + pad;
835template <
class OUTPUT_ITERATOR>
836void Base64Encoder::append(OUTPUT_ITERATOR *out,
842 if (d_maxLineLength && d_lineLength >= d_maxLineLength) {
843 if (d_lineLength == d_maxLineLength) {
848 if (d_outputLength == maxLength) {
856 if (d_outputLength == maxLength) {
866template <
class OUTPUT_ITERATOR>
867void Base64Encoder::encode(OUTPUT_ITERATOR *out,
int maxLength)
871 if (d_maxLineLength && d_lineLength >= d_maxLineLength) {
872 if (d_lineLength == d_maxLineLength) {
877 if (d_outputLength == maxLength) {
885 if (d_outputLength == maxLength) {
890 **out = d_alphabet_p[(d_stack >> d_bitsInStack) & 0x3f];
897void Base64Encoder::setState(State newState)
904bool Base64Encoder::isResidualOutput(
int numBytes)
const
909 if (d_maxLineLength) {
910 const int lineSize = d_maxLineLength + 2;
911 const int linesSoFar = numBytes / lineSize;
912 const int bytesSinceLastCrlf = numBytes - linesSoFar * lineSize;
913 const int partialCrlf = d_maxLineLength < bytesSinceLastCrlf;
914 const int nonCrlfBytes = linesSoFar * d_maxLineLength +
915 bytesSinceLastCrlf - partialCrlf;
917 return 0 != nonCrlfBytes % 4;
920 return 0 != numBytes % 4;
925Base64Encoder::State Base64Encoder::state()
const
933 bsl::size_t inputLength)
935 static const bsl::size_t maxSize_t = bsl::numeric_limits<
939 if (0 == inputLength) {
943 const bsl::size_t length = lengthWithoutCrlfs(
options, inputLength);
952 return length + 2 * numCrlfs;
978 bsl::size_t inputLength)
1007template <
class OUTPUT_ITERATOR,
class INPUT_ITERATOR>
1009 INPUT_ITERATOR begin,
1015 return convert(out, &dummyNumOut, &dummyNumIn, begin, end, -1);
1018template <
class OUTPUT_ITERATOR,
class INPUT_ITERATOR>
1022 INPUT_ITERATOR begin,
1028 numOut = &dummyNumOut;
1032 numIn = &dummyNumIn;
1035 if (e_ERROR_STATE == state() || e_DONE_STATE == state()) {
1036 setState(e_ERROR_STATE);
1042 const int initialLength = d_outputLength;
1043 const int maxLength = d_outputLength + maxNumOut;
1047 while (6 <= d_bitsInStack && d_outputLength != maxLength) {
1048 encode(&out, maxLength);
1055 while (4 >= d_bitsInStack && begin != end) {
1056 const unsigned char byte =
static_cast<unsigned char>(*begin);
1061 d_stack = (d_stack << 8) |
byte;
1064 if (d_outputLength != maxLength) {
1065 encode(&out, maxLength);
1066 if (6 <= d_bitsInStack && d_outputLength != maxLength) {
1067 encode(&out, maxLength);
1073 *numOut = d_outputLength - initialLength;
1078template <
class OUTPUT_ITERATOR>
1086template <
class OUTPUT_ITERATOR>
1093 if (e_ERROR_STATE == state() ||
isDone()) {
1094 setState(e_ERROR_STATE);
1099 const int initialLength = d_outputLength;
1100 const int maxLength = d_outputLength + maxNumOut;
1104 const int residualBits = d_bitsInStack % 6;
1106 const int shift = 6 - residualBits;
1107 d_stack = d_stack << shift;
1108 d_bitsInStack += shift;
1115 while (6 <= d_bitsInStack && d_outputLength != maxLength) {
1116 encode(&out, maxLength);
1121 if (0 == d_bitsInStack) {
1123 if (!d_isPadded || !isResidualOutput(d_outputLength)) {
1124 setState(e_DONE_STATE);
1129 if (d_outputLength == maxLength) {
1133 append(&out,
'=', maxLength);
1137 *numOut = d_outputLength - initialLength;
1145 setState(e_INITIAL_STATE);
1162 return e_ERROR_STATE != state();
1168 return e_DONE_STATE == state()
1170 && (!d_isPadded || !isResidualOutput(d_outputLength));
1176 return e_ERROR_STATE == state();
1182 return 0 == d_outputLength && e_INITIAL_STATE == state();
1194 return d_maxLineLength;
1209 return d_outputLength;
Definition bdlde_base64encoderoptions.h:214
static Base64EncoderOptions custom(int maxLineLength, Base64Alphabet::Enum alphabet, bool padded)
Definition bdlde_base64encoderoptions.h:386
bool isPadded() const
Return the value of the isPadded attribute.
Definition bdlde_base64encoderoptions.h:446
int maxLineLength() const
Return the value of the maxLineLength attribute.
Definition bdlde_base64encoderoptions.h:452
static Base64EncoderOptions mime()
Definition bdlde_base64encoderoptions.h:395
Definition bdlde_base64encoder.h:497
void resetState()
Definition bdlde_base64encoder.h:1143
static const Alphabet e_URL
Definition bdlde_base64encoder.h:508
int maxLineLength
Definition bdlde_base64encoder.h:617
static bsl::size_t encodedLength(const EncoderOptions &options, bsl::size_t inputLength)
Definition bdlde_base64encoder.h:932
static bsl::size_t encodedLines(const EncoderOptions &options, bsl::size_t inputLength)
Definition bdlde_base64encoder.h:977
int outputLength() const
Definition bdlde_base64encoder.h:1207
bool isInitialState() const
Definition bdlde_base64encoder.h:1180
static const Alphabet e_BASIC
Definition bdlde_base64encoder.h:507
int convert(OUTPUT_ITERATOR out, INPUT_ITERATOR begin, INPUT_ITERATOR end)
Definition bdlde_base64encoder.h:1008
bool isPadded() const
Definition bdlde_base64encoder.h:1186
int endConvert(OUTPUT_ITERATOR out)
Definition bdlde_base64encoder.h:1079
bool isError() const
Definition bdlde_base64encoder.h:1174
bool isAcceptable() const
Definition bdlde_base64encoder.h:1160
bool isDone() const
Definition bdlde_base64encoder.h:1166
EncoderOptions options() const
Definition bdlde_base64encoder.h:1198
Alphabet alphabet
Definition bdlde_base64encoder.h:697
BSLS_DEPRECATE_FEATURE("bdl", "encodedLength", "use overload with 'options'") static int encodedLength(int inputLength)
Base64Alphabet::Enum Alphabet
Definition bdlde_base64encoder.h:504
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_DEPRECATE_FEATURE(UOR, FEATURE, MESSAGE)
Definition bsls_deprecatefeature.h:319
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlde_base64alphabet.h:118
Definition bdlb_printmethods.h:283
Enum
Definition bdlde_base64alphabet.h:135
@ e_URL
Definition bdlde_base64alphabet.h:137
@ e_BASIC
Definition bdlde_base64alphabet.h:136