forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Encoder.cpp
More file actions
164 lines (130 loc) · 3.17 KB
/
Base64Encoder.cpp
File metadata and controls
164 lines (130 loc) · 3.17 KB
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
//
// Base64Encoder.cpp
//
// $Id: //poco/1.4/Foundation/src/Base64Encoder.cpp#2 $
//
// 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', '+', '/'
};
Base64EncoderBuf::Base64EncoderBuf(std::ostream& ostr):
_groupLength(0),
_pos(0),
_lineLength(72),
_buf(*ostr.rdbuf())
{
}
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(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = _group[2] & 0x3F;
if (_buf.sputc(OUT_ENCODING[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(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
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(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[0] & 0x03) << 4) | (_group[1] >> 4);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
idx = ((_group[1] & 0x0F) << 2) | (_group[2] >> 6);
if (_buf.sputc(OUT_ENCODING[idx]) == eof) return eof;
if (_buf.sputc('=') == eof) return eof;
}
_groupLength = 0;
return _buf.pubsync();
}
Base64EncoderIOS::Base64EncoderIOS(std::ostream& ostr): _buf(ostr)
{
poco_ios_init(&_buf);
}
Base64EncoderIOS::~Base64EncoderIOS()
{
}
int Base64EncoderIOS::close()
{
return _buf.close();
}
Base64EncoderBuf* Base64EncoderIOS::rdbuf()
{
return &_buf;
}
Base64Encoder::Base64Encoder(std::ostream& ostr): Base64EncoderIOS(ostr), std::ostream(&_buf)
{
}
Base64Encoder::~Base64Encoder()
{
}
} // namespace Poco