-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.cpp
More file actions
125 lines (103 loc) · 3.35 KB
/
decode.cpp
File metadata and controls
125 lines (103 loc) · 3.35 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
/*
* Copyright 2025 Siddhartha Menon
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <array>
#include <cstdint>
#include <iostream>
#include <istream>
namespace {
// Convert a char value to it's position in the alphabet string.
// Return -1 otherwise.
int char_to_base64_index(char sym) {
if (sym >= 'A' && sym <= 'Z') {
return sym - 'A';
}
if (sym >= 'a' && sym <= 'z') {
return sym - 'a' + 26;
}
if (sym >= '0' && sym <= '9') {
return sym - '0' + 52;
}
switch (sym) {
case '+':
return 62;
case '/':
return 63;
case '=':
return 64;
}
return -1;
}
} // namespace
// Read in four base64 characters and output three bytes of binary data
void decode(std::istream& src) {
while (true) {
std::array<char, 4> chunk;
// total_size = data_size + padding_size;
size_t total_size = 0;
size_t padding_size = 0;
for (; total_size < chunk.size(); ++total_size) {
char sym;
src.get(sym);
if (src.gcount() == 0) {
break;
}
int base64_index = char_to_base64_index(sym);
// Ignore whitespace
switch (sym) {
case ' ':
case '\t':
case '\n':
case '\r':
--total_size;
continue;
}
if (base64_index < 0) {
throw std::runtime_error{
"Invalid input: Unrecognised base64 symbol '" +
std::to_string(sym) + "' encountered."};
}
if (base64_index == 64) {
++padding_size;
if (total_size < 2) {
throw std::runtime_error{
"Invalid input: Unexpected padding symbol "
"encountered."};
}
if (padding_size > 2) {
throw std::runtime_error{
"Invalid input: More than two consecutive "
"padding "
"symbols encountered."};
}
}
chunk[total_size] = static_cast<char>(base64_index);
}
// The whole chunk was not processed
if (total_size < chunk.size()) {
if (total_size == 0) {
return;
}
throw std::runtime_error{
"input length is expected to be a multiple of 4."};
}
uint32_t packed_data = 0x0 + (chunk[0] << 18) + (chunk[1] << 12) +
(chunk[2] << 6) + (chunk[3]);
size_t data_size = 3 - padding_size;
for (size_t j = 0; j < data_size; ++j) {
std::cout.put(static_cast<uint8_t>(packed_data >> (16 - 8 * j)));
}
}
}