forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase64Decoder.cpp
More file actions
138 lines (108 loc) · 2.71 KB
/
Base64Decoder.cpp
File metadata and controls
138 lines (108 loc) · 2.71 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
//
// Base64Decoder.cpp
//
// $Id: //poco/1.4/Foundation/src/Base64Decoder.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/Base64Decoder.h"
#include "Poco/Base64Encoder.h"
#include "Poco/Exception.h"
#include "Poco/Mutex.h"
namespace Poco {
unsigned char Base64DecoderBuf::IN_ENCODING[256];
bool Base64DecoderBuf::IN_ENCODING_INIT = false;
namespace
{
static FastMutex mutex;
}
Base64DecoderBuf::Base64DecoderBuf(std::istream& istr):
_groupLength(0),
_groupIndex(0),
_buf(*istr.rdbuf())
{
FastMutex::ScopedLock lock(mutex);
if (!IN_ENCODING_INIT)
{
for (unsigned i = 0; i < sizeof(IN_ENCODING); i++)
{
IN_ENCODING[i] = 0xFF;
}
for (unsigned i = 0; i < sizeof(Base64EncoderBuf::OUT_ENCODING); i++)
{
IN_ENCODING[Base64EncoderBuf::OUT_ENCODING[i]] = i;
}
IN_ENCODING[static_cast<unsigned char>('=')] = '\0';
IN_ENCODING_INIT = true;
}
}
Base64DecoderBuf::~Base64DecoderBuf()
{
}
int Base64DecoderBuf::readFromDevice()
{
if (_groupIndex < _groupLength)
{
return _group[_groupIndex++];
}
else
{
unsigned char buffer[4];
int c;
if ((c = readOne()) == -1) return -1;
buffer[0] = (unsigned char) c;
if (IN_ENCODING[buffer[0]] == 0xFF) throw DataFormatException();
if ((c = readOne()) == -1) throw DataFormatException();
buffer[1] = (unsigned char) c;
if (IN_ENCODING[buffer[1]] == 0xFF) throw DataFormatException();
if ((c = readOne()) == -1) throw DataFormatException();
buffer[2] = c;
if (IN_ENCODING[buffer[2]] == 0xFF) throw DataFormatException();
if ((c = readOne()) == -1) throw DataFormatException();
buffer[3] = c;
if (IN_ENCODING[buffer[3]] == 0xFF) throw DataFormatException();
_group[0] = (IN_ENCODING[buffer[0]] << 2) | (IN_ENCODING[buffer[1]] >> 4);
_group[1] = ((IN_ENCODING[buffer[1]] & 0x0F) << 4) | (IN_ENCODING[buffer[2]] >> 2);
_group[2] = (IN_ENCODING[buffer[2]] << 6) | IN_ENCODING[buffer[3]];
if (buffer[2] == '=')
_groupLength = 1;
else if (buffer[3] == '=')
_groupLength = 2;
else
_groupLength = 3;
_groupIndex = 1;
return _group[0];
}
}
int Base64DecoderBuf::readOne()
{
int ch = _buf.sbumpc();
while (ch == ' ' || ch == '\r' || ch == '\t' || ch == '\n')
ch = _buf.sbumpc();
return ch;
}
Base64DecoderIOS::Base64DecoderIOS(std::istream& istr): _buf(istr)
{
poco_ios_init(&_buf);
}
Base64DecoderIOS::~Base64DecoderIOS()
{
}
Base64DecoderBuf* Base64DecoderIOS::rdbuf()
{
return &_buf;
}
Base64Decoder::Base64Decoder(std::istream& istr): Base64DecoderIOS(istr), std::istream(&_buf)
{
}
Base64Decoder::~Base64Decoder()
{
}
} // namespace Poco