1 | // |
2 | // Base32Decoder.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: Base32 |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Base32Decoder.h" |
16 | #include "Poco/Base32Encoder.h" |
17 | #include "Poco/Exception.h" |
18 | #include "Poco/Mutex.h" |
19 | #include <cstring> |
20 | |
21 | |
22 | namespace Poco { |
23 | |
24 | |
25 | unsigned char Base32DecoderBuf::IN_ENCODING[256]; |
26 | bool Base32DecoderBuf::IN_ENCODING_INIT = false; |
27 | |
28 | |
29 | namespace |
30 | { |
31 | static FastMutex mutex; |
32 | } |
33 | |
34 | |
35 | Base32DecoderBuf::Base32DecoderBuf(std::istream& istr): |
36 | _groupLength(0), |
37 | _groupIndex(0), |
38 | _buf(*istr.rdbuf()) |
39 | { |
40 | FastMutex::ScopedLock lock(mutex); |
41 | if (!IN_ENCODING_INIT) |
42 | { |
43 | for (unsigned i = 0; i < sizeof(IN_ENCODING); i++) |
44 | { |
45 | IN_ENCODING[i] = 0xFF; |
46 | } |
47 | for (unsigned i = 0; i < sizeof(Base32EncoderBuf::OUT_ENCODING); i++) |
48 | { |
49 | IN_ENCODING[Base32EncoderBuf::OUT_ENCODING[i]] = static_cast<UInt8>(i); |
50 | } |
51 | IN_ENCODING[static_cast<unsigned char>('=')] = '\0'; |
52 | IN_ENCODING_INIT = true; |
53 | } |
54 | } |
55 | |
56 | |
57 | Base32DecoderBuf::~Base32DecoderBuf() |
58 | { |
59 | } |
60 | |
61 | |
62 | int Base32DecoderBuf::readFromDevice() |
63 | { |
64 | if (_groupIndex < _groupLength) |
65 | { |
66 | return _group[_groupIndex++]; |
67 | } |
68 | else |
69 | { |
70 | unsigned char buffer[8]; |
71 | std::memset(buffer, '=', sizeof(buffer)); |
72 | int c; |
73 | |
74 | // per RFC-4648, Section 6, permissible block lengths are: |
75 | // 2, 4, 5, 7, and 8 bytes. Any other length is malformed. |
76 | // |
77 | do { |
78 | if ((c = readOne()) == -1) return -1; |
79 | buffer[0] = (unsigned char) c; |
80 | if (IN_ENCODING[buffer[0]] == 0xFF) throw DataFormatException(); |
81 | if ((c = readOne()) == -1) throw DataFormatException(); |
82 | buffer[1] = (unsigned char) c; |
83 | if (IN_ENCODING[buffer[1]] == 0xFF) throw DataFormatException(); |
84 | if ((c = readOne()) == -1) break; |
85 | buffer[2] = (unsigned char) c; |
86 | if (IN_ENCODING[buffer[2]] == 0xFF) throw DataFormatException(); |
87 | if ((c = readOne()) == -1) throw DataFormatException(); |
88 | buffer[3] = (unsigned char) c; |
89 | if (IN_ENCODING[buffer[3]] == 0xFF) throw DataFormatException(); |
90 | if ((c = readOne()) == -1) break; |
91 | buffer[4] = (unsigned char) c; |
92 | if (IN_ENCODING[buffer[4]] == 0xFF) throw DataFormatException(); |
93 | if ((c = readOne()) == -1) break; |
94 | buffer[5] = (unsigned char) c; |
95 | if (IN_ENCODING[buffer[5]] == 0xFF) throw DataFormatException(); |
96 | if ((c = readOne()) == -1) throw DataFormatException(); |
97 | buffer[6] = (unsigned char) c; |
98 | if (IN_ENCODING[buffer[6]] == 0xFF) throw DataFormatException(); |
99 | if ((c = readOne()) == -1) break; |
100 | buffer[7] = (unsigned char) c; |
101 | if (IN_ENCODING[buffer[7]] == 0xFF) throw DataFormatException(); |
102 | } while (false); |
103 | |
104 | _group[0] = (IN_ENCODING[buffer[0]] << 3) | (IN_ENCODING[buffer[1]] >> 2); |
105 | _group[1] = ((IN_ENCODING[buffer[1]] & 0x03) << 6) | (IN_ENCODING[buffer[2]] << 1) | (IN_ENCODING[buffer[3]] >> 4); |
106 | _group[2] = ((IN_ENCODING[buffer[3]] & 0x0F) << 4) | (IN_ENCODING[buffer[4]] >> 1); |
107 | _group[3] = ((IN_ENCODING[buffer[4]] & 0x01) << 7) | (IN_ENCODING[buffer[5]] << 2) | (IN_ENCODING[buffer[6]] >> 3); |
108 | _group[4] = ((IN_ENCODING[buffer[6]] & 0x07) << 5) | IN_ENCODING[buffer[7]]; |
109 | |
110 | if (buffer[2] == '=') |
111 | _groupLength = 1; |
112 | else if (buffer[4] == '=') |
113 | _groupLength = 2; |
114 | else if (buffer[5] == '=') |
115 | _groupLength = 3; |
116 | else if (buffer[7] == '=') |
117 | _groupLength = 4; |
118 | else |
119 | _groupLength = 5; |
120 | _groupIndex = 1; |
121 | return _group[0]; |
122 | } |
123 | } |
124 | |
125 | |
126 | int Base32DecoderBuf::readOne() |
127 | { |
128 | int ch = _buf.sbumpc(); |
129 | return ch; |
130 | } |
131 | |
132 | |
133 | Base32DecoderIOS::Base32DecoderIOS(std::istream& istr): _buf(istr) |
134 | { |
135 | poco_ios_init(&_buf); |
136 | } |
137 | |
138 | |
139 | Base32DecoderIOS::~Base32DecoderIOS() |
140 | { |
141 | } |
142 | |
143 | |
144 | Base32DecoderBuf* Base32DecoderIOS::rdbuf() |
145 | { |
146 | return &_buf; |
147 | } |
148 | |
149 | |
150 | Base32Decoder::Base32Decoder(std::istream& istr): Base32DecoderIOS(istr), std::istream(&_buf) |
151 | { |
152 | } |
153 | |
154 | |
155 | Base32Decoder::~Base32Decoder() |
156 | { |
157 | } |
158 | |
159 | |
160 | } // namespace Poco |
161 | |