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